Commit 8c217ae8 authored by aligungr's avatar aligungr

JSON utility improvement

parent dc9aaded
......@@ -92,19 +92,18 @@ void GnbCmdHandler::HandleCmdImpl(TaskBase &base, NwGnbCliCommand &msg)
switch (msg.cmd->present)
{
case app::GnbCliCommand::STATUS: {
msg.sendResult(base.appTask->m_statusInfo.toString());
msg.sendResult(ToJson(base.appTask->m_statusInfo).dumpYaml());
break;
}
case app::GnbCliCommand::INFO: {
msg.sendResult(base.config->toString());
msg.sendResult(ToJson(*base.config).dumpYaml());
break;
}
case app::GnbCliCommand::AMF_LIST: {
std::stringstream ss{};
Json json = Json::Arr({});
for (auto &amf : base.ngapTask->m_amfCtx)
ss << "- ID[" << amf.first << "]\n";
utils::Trim(ss);
msg.sendResult(ss.str());
json.push(Json::Obj({{"id", amf.first}}));
msg.sendResult(json.dumpYaml());
break;
}
case app::GnbCliCommand::AMF_INFO: {
......@@ -113,33 +112,21 @@ void GnbCmdHandler::HandleCmdImpl(TaskBase &base, NwGnbCliCommand &msg)
else
{
auto amf = base.ngapTask->m_amfCtx[msg.cmd->amfId];
Printer printer{};
printer.appendKeyValue({
{"address", amf->address},
{"port", std::to_string(amf->port)},
{"name", amf->amfName},
{"capacity", std::to_string(amf->relativeCapacity)},
{"state", EnumToString(amf->state)},
});
printer.trim();
msg.sendResult(printer.makeString());
msg.sendResult(ToJson(*amf).dumpYaml());
}
break;
}
case app::GnbCliCommand::UE_LIST: {
Printer printer{};
Json json = Json::Arr({});
for (auto &ue : base.ngapTask->m_ueCtx)
{
printer.appendKeyValueList({
json.push(Json::Obj({
{"ue-name", base.mrTask->m_ueMap[ue.first].name},
{"ran-ngap-id", std::to_string(ue.second->ranUeNgapId)},
{"amf-ngap-id", std::to_string(ue.second->amfUeNgapId)},
});
printer.decrement(2);
{"ran-ngap-id", ue.second->ranUeNgapId},
{"amf-ngap-id", ue.second->amfUeNgapId},
}));
}
printer.trim();
msg.sendResult(printer.makeString());
msg.sendResult(json.dumpYaml());
break;
}
case app::GnbCliCommand::UE_COUNT: {
......
......@@ -14,31 +14,32 @@
namespace nr::gnb
{
std::string GnbStatusInfo::toString() const
Json ToJson(const GnbStatusInfo &v)
{
Printer printer{};
printer.appendKeyValue({{"is-ngap-up", this->isNgapUp ? "true" : "false"}});
printer.trim();
return printer.makeString();
return Json::Obj({{"is-ngap-up", v.isNgapUp}});
}
std::string GnbConfig::toString() const
Json ToJson(const GnbConfig &v)
{
Printer printer{};
printer.appendKeyValue({
{"nci", std::to_string(this->nci)},
{"gnb-id", std::to_string(this->getGnbId())},
{"cell-id", std::to_string(this->getCellId())},
{"mcc", std::to_string(this->plmn.mcc)},
{"mnc", std::to_string(this->plmn.mnc)},
{"tac", std::to_string(this->tac)},
{"name", this->name},
return Json::Obj({
{"nci", v.nci},
{"plmn", ToJson(v.plmn)},
{"tac", v.tac},
{"name", v.name},
});
printer.trim();
return printer.makeString();
}
const char *EnumToString(EAmfState v)
Json ToJson(const NgapAmfContext &v)
{
return Json::Obj({
{"name", v.amfName},
{"capacity", v.relativeCapacity},
{"state", ToJson(v.state).str()},
{"address", v.address + ":" + std::to_string(v.port)},
});
}
Json ToJson(const EAmfState &v)
{
switch (v)
{
......@@ -49,7 +50,7 @@ const char *EnumToString(EAmfState v)
case EAmfState::CONNECTED:
return "CONNECTED";
default:
return "<?>";
return "?";
}
}
......
......@@ -219,8 +219,6 @@ struct PduSessionResource
struct GnbStatusInfo
{
bool isNgapUp;
[[nodiscard]] std::string toString() const;
};
struct GtpUeContext
......@@ -278,8 +276,6 @@ struct GnbConfig
{
return static_cast<int>(nci & static_cast<uint64_t>((1 << (36 - gnbIdLength)) - 1));
}
[[nodiscard]] std::string toString() const;
};
struct TaskBase
......@@ -302,6 +298,9 @@ struct MrUeContext
std::string name;
};
const char *EnumToString(EAmfState v);
Json ToJson(const GnbStatusInfo &v);
Json ToJson(const GnbConfig &v);
Json ToJson(const NgapAmfContext &v);
Json ToJson(const EAmfState &v);
} // namespace nr::gnb
\ No newline at end of file
......@@ -83,27 +83,23 @@ void UeCmdHandler::HandleCmdImpl(TaskBase &base, NwUeCliCommand &msg)
switch (msg.cmd->present)
{
case app::UeCliCommand::STATUS: {
Printer printer{};
printer.appendKeyValue({
{"cm-state", nr::ue::CmStateName(base.nasTask->mm->m_cmState)},
{"rm-state", nr::ue::RmStateName(base.nasTask->mm->m_rmState)},
{"mm-state", nr::ue::MmSubStateName(base.nasTask->mm->m_mmSubState)},
{"sim-inserted", base.nasTask->mm->m_validSim ? "true" : "false"},
Json json = Json::Obj({
{"cm-state", ToJson(base.nasTask->mm->m_cmState)},
{"rm-state", ToJson(base.nasTask->mm->m_rmState)},
{"mm-state", ToJson(base.nasTask->mm->m_mmSubState)},
{"sim-inserted", base.nasTask->mm->m_validSim},
});
printer.trim();
msg.sendResult(printer.makeString());
msg.sendResult(json.dumpYaml());
break;
}
case app::UeCliCommand::INFO: {
Printer printer{};
printer.appendKeyValue({
{"supi", base.config->supi.has_value() ? base.config->supi->toString() : ""},
{"plmn", base.config->plmn.toString()},
{"imei", base.config->imei.has_value() ? *base.config->imei : ""},
{"imeisv", base.config->imeiSv.has_value() ? *base.config->imeiSv : ""},
Json json = Json::Obj({
{"supi", ToJson(base.config->supi)},
{"plmn", ToJson(base.config->plmn)},
{"imei", ::ToJson(base.config->imei)},
{"imeisv", ::ToJson(base.config->imeiSv)},
});
printer.trim();
msg.sendResult(printer.makeString());
msg.sendResult(json.dumpYaml());
break;
}
}
......
......@@ -115,13 +115,13 @@ void NasMm::switchMmState(EMmState state, EMmSubState subState)
if (m_base->nodeListener)
{
m_base->nodeListener->onSwitch(app::NodeType::UE, m_base->config->getNodeName(), app::StateType::MM,
MmSubStateName(oldSubState), MmSubStateName(subState));
ToJson(oldSubState).str(), ToJson(subState).str());
m_base->nodeListener->onSwitch(app::NodeType::UE, m_base->config->getNodeName(), app::StateType::MM_SUB,
MmStateName(oldState), MmStateName(state));
ToJson(oldState).str(), ToJson(state).str());
}
if (state != oldState || subState != oldSubState)
m_logger->info("UE switches to state: %s", MmSubStateName(subState));
m_logger->info("UE switches to state: %s", ToJson(subState).str().c_str());
triggerMmCycle();
}
......@@ -136,7 +136,7 @@ void NasMm::switchRmState(ERmState state)
if (m_base->nodeListener)
{
m_base->nodeListener->onSwitch(app::NodeType::UE, m_base->config->getNodeName(), app::StateType::RM,
RmStateName(oldState), RmStateName(m_rmState));
ToJson(oldState).str(), ToJson(m_rmState).str());
}
// No need to log it
......@@ -155,11 +155,11 @@ void NasMm::switchCmState(ECmState state)
if (m_base->nodeListener)
{
m_base->nodeListener->onSwitch(app::NodeType::UE, m_base->config->getNodeName(), app::StateType::CM,
CmStateName(oldState), CmStateName(m_cmState));
ToJson(oldState).str(), ToJson(m_cmState).str());
}
if (state != oldState)
m_logger->info("UE switches to state: %s", CmStateName(state));
m_logger->info("UE switches to state: %s", ToJson(state).str().c_str());
triggerMmCycle();
}
......
......@@ -22,7 +22,7 @@ UeTimers::UeTimers()
{
}
const char *CmStateName(ECmState state)
Json ToJson(ECmState state)
{
switch (state)
{
......@@ -35,7 +35,7 @@ const char *CmStateName(ECmState state)
}
}
const char *RmStateName(ERmState state)
Json ToJson(ERmState state)
{
switch (state)
{
......@@ -48,7 +48,7 @@ const char *RmStateName(ERmState state)
}
}
const char *MmStateName(EMmState state)
Json ToJson(EMmState state)
{
switch (state)
{
......@@ -69,7 +69,7 @@ const char *MmStateName(EMmState state)
}
}
const char *MmSubStateName(EMmSubState state)
Json ToJson(EMmSubState state)
{
switch (state)
{
......
......@@ -12,6 +12,7 @@
#include <nas/nas.hpp>
#include <nas/timer.hpp>
#include <utils/common_types.hpp>
#include <utils/json.hpp>
#include <utils/logger.hpp>
#include <utils/nts.hpp>
#include <utils/octet_string.hpp>
......@@ -71,7 +72,7 @@ struct UeConfig
[[nodiscard]] std::string getNodeName() const
{
if (supi.has_value())
return supi->toString();
return ToJson(supi).str();
if (imei.has_value())
return "imei-" + *imei;
if (imeiSv.has_value())
......@@ -358,9 +359,9 @@ struct UeStatusInfo
std::optional<UePduSessionInfo> pduSessions[16]{};
};
const char *CmStateName(ECmState state);
const char *RmStateName(ERmState state);
const char *MmStateName(EMmState state);
const char *MmSubStateName(EMmSubState state);
Json ToJson(ECmState state);
Json ToJson(ERmState state);
Json ToJson(EMmState state);
Json ToJson(EMmSubState state);
} // namespace nr::ue
......@@ -25,15 +25,15 @@ Supi Supi::Parse(const std::string &supi)
throw std::runtime_error("invalid SUPI value");
}
std::string Supi::toString() const
Json ToJson(const Supi &v)
{
return type + "-" + value;
return v.type + "-" + v.value;
}
std::string Plmn::toString() const
Json ToJson(const Plmn &v)
{
std::stringstream ss{};
ss << std::setfill('0') << std::setw(3) << mcc << "/";
ss << std::setfill('0') << std::setw(isLongMnc ? 3 : 2) << mnc;
ss << std::setfill('0') << std::setw(3) << v.mcc << "/";
ss << std::setfill('0') << std::setw(v.isLongMnc ? 3 : 2) << v.mnc;
return ss.str();
}
......@@ -8,6 +8,7 @@
#pragma once
#include "json.hpp"
#include "octet.hpp"
#include <memory>
#include <optional>
......@@ -27,8 +28,6 @@ struct Plmn
int mcc;
int mnc;
bool isLongMnc;
[[nodiscard]] std::string toString() const;
};
struct SliceSupport
......@@ -117,6 +116,7 @@ struct Supi
}
static Supi Parse(const std::string &supi);
};
[[nodiscard]] std::string toString() const;
};
\ No newline at end of file
Json ToJson(const Supi &v);
Json ToJson(const Plmn &v);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment