Commit b78197fc authored by Tien Thinh NGUYEN's avatar Tien Thinh NGUYEN

Code refactor for ran_amf_id_2_ue_context

parent 29abe22b
......@@ -213,20 +213,13 @@ bool amf_app::is_ran_amf_id_2_ue_context(const string& ue_context_key) const {
return false;
}
//------------------------------------------------------------------------------
std::shared_ptr<ue_context> amf_app::ran_amf_id_2_ue_context(
const string& ue_context_key) const {
std::shared_lock lock(m_ue_ctx_key);
return ue_ctx_key.at(ue_context_key);
}
//------------------------------------------------------------------------------
bool amf_app::ran_amf_id_2_ue_context(
const std::string& ue_context_key, std::shared_ptr<ue_context>& uc) const {
std::shared_lock lock(m_ue_ctx_key);
if (ue_ctx_key.count(ue_context_key) > 0) {
if (ue_ctx_key.at(ue_context_key) == nullptr) return false;
uc = ue_ctx_key.at(ue_context_key);
if (uc == nullptr) return false;
return true;
}
return false;
......@@ -263,8 +256,8 @@ bool amf_app::supi_2_ue_context(
const std::string& supi, std::shared_ptr<ue_context>& uc) const {
std::shared_lock lock(m_supi2ue_ctx);
if (supi2ue_ctx.count(supi) > 0) {
if (supi2ue_ctx.at(supi) == nullptr) return false;
uc = supi2ue_ctx.at(supi);
if (uc == nullptr) return false;
return true;
}
return false;
......@@ -396,7 +389,7 @@ void amf_app::handle_itti_message(
Logger::amf_app().debug(
"No existing UE Context, Create a new one with ran_amf_id %s",
ue_context_key.c_str());
uc = std::shared_ptr<ue_context>(new ue_context());
uc = std::make_shared<ue_context>();
set_ran_amf_id_2_ue_context(ue_context_key, uc);
}
......@@ -565,7 +558,7 @@ void amf_app::handle_itti_message(itti_sbi_n1_message_notification& itti_msg) {
string ue_context_key =
conv::get_ue_context_key(ran_ue_ngap_id, amf_ue_ngap_id);
if (!is_ran_amf_id_2_ue_context(ue_context_key)) {
if (!ran_amf_id_2_ue_context(ue_context_key, uc)) {
Logger::amf_app().debug(
"No existing UE Context associated with UE Context Key %s",
ue_context_key.c_str());
......@@ -574,16 +567,9 @@ void amf_app::handle_itti_message(itti_sbi_n1_message_notification& itti_msg) {
Logger::amf_app().debug(
"Create a new UE Context with UE Context Key",
ue_context_key.c_str());
uc = std::shared_ptr<ue_context>(new ue_context());
uc = std::make_shared<ue_context>();
}
set_ran_amf_id_2_ue_context(ue_context_key, uc);
} else {
uc = ran_amf_id_2_ue_context(ue_context_key);
}
// Return if UE Context is still invalid
if (!uc) {
Logger::amf_app().error("Failed to get UE Context");
return;
}
// Update info for UE context
......@@ -897,18 +883,19 @@ uint32_t amf_app::generate_tmsi() {
bool amf_app::generate_5g_guti(
const uint32_t ranid, const long amfid, string& mcc, string& mnc,
uint32_t& tmsi) {
string ue_context_key = conv::get_ue_context_key(ranid, amfid);
if (!is_ran_amf_id_2_ue_context(ue_context_key)) {
string ue_context_key = conv::get_ue_context_key(ranid, amfid);
std::shared_ptr<ue_context> uc = {};
if (!ran_amf_id_2_ue_context(ue_context_key, uc)) {
Logger::amf_app().error(
"No UE context for ran_amf_id %s, exit", ue_context_key.c_str());
return false;
}
std::shared_ptr<ue_context> uc = {};
uc = ran_amf_id_2_ue_context(ue_context_key);
mcc = uc->tai.mcc;
mnc = uc->tai.mnc;
tmsi = generate_tmsi();
uc->tmsi = tmsi;
mcc = uc->tai.mcc;
mnc = uc->tai.mnc;
tmsi = generate_tmsi();
uc->tmsi = tmsi;
return true;
}
......
......@@ -198,14 +198,6 @@ class amf_app {
*/
bool is_ran_amf_id_2_ue_context(const std::string& ue_context_key) const;
/*
* Get UE context associated with an UE Context Key
* @param [const std::string&] ue_context_key: UE Context Key
* @return shared pointer to the context
*/
std::shared_ptr<ue_context> ran_amf_id_2_ue_context(
const std::string& ue_context_key) const;
/*
* Get UE context associated with an UE Context Key and verify if this pointer
* is nullptr
......
......@@ -369,14 +369,6 @@ void amf_n1::handle_itti_message(itti_uplink_nas_data_ind& nas_data_ind) {
std::string snn =
conv::get_serving_network_name(nas_data_ind.mnc, nas_data_ind.mcc);
/*
if (nas_data_ind.mnc.length() == 2) // TODO: remove hardcoded value
snn = "5G:mnc0" + nas_data_ind.mnc + ".mcc" + nas_data_ind.mcc +
".3gppnetwork.org";
else
snn = "5G:mnc" + nas_data_ind.mnc + ".mcc" + nas_data_ind.mcc +
".3gppnetwork.org";
*/
Logger::amf_n1().debug("Serving network name %s", snn.c_str());
plmn_t plmn = {};
......@@ -390,12 +382,11 @@ void amf_n1::handle_itti_message(itti_uplink_nas_data_ind& nas_data_ind) {
if (nas_data_ind.is_guti_valid) {
std::string guti = nas_data_ind.guti;
Logger::amf_n1().debug("GUTI valid %s", guti.c_str());
if (is_guti_2_nas_context(guti)) {
if (guti_2_nas_context(guti, nc)) {
Logger::amf_n1().debug(
"Existing nas_context with GUTI %s, update "
"amf_ue_ngap_id/ran_ue_ngap_id",
guti.c_str());
nc = guti_2_nas_context(guti);
// Update Nas Context
nc->amf_ue_ngap_id = nas_data_ind.amf_ue_ngap_id;
nc->ran_ue_ngap_id = nas_data_ind.ran_ue_ngap_id;
......@@ -404,10 +395,10 @@ void amf_n1::handle_itti_message(itti_uplink_nas_data_ind& nas_data_ind) {
set_supi_2_amf_id("imsi-" + nc->imsi, amf_ue_ngap_id);
set_supi_2_ran_id("imsi-" + nc->imsi, ran_ue_ngap_id);
set_imsi_2_nas_context("imsi-" + nc->imsi, nc);
} else {
Logger::amf_n1().error(
"No existing nas_context with GUTI %s", nas_data_ind.guti.c_str());
// TODO:
// return;
}
} else {
......@@ -735,32 +726,28 @@ void amf_n1::identity_response_handle(
string ue_context_key =
conv::get_ue_context_key(ran_ue_ngap_id, amf_ue_ngap_id);
if (amf_app_inst->is_ran_amf_id_2_ue_context(ue_context_key)) {
std::shared_ptr<ue_context> uc = {};
uc = amf_app_inst->ran_amf_id_2_ue_context(ue_context_key);
std::shared_ptr<ue_context> uc = {};
if (amf_app_inst->ran_amf_id_2_ue_context(ue_context_key, uc)) {
// Update UE context
if (uc != nullptr) {
uc->supi = "imsi-" + supi;
// associate SUPI with UC
// Verify if there's PDU session info in the old context
if (amf_app_inst->is_supi_2_ue_context(uc->supi)) {
std::shared_ptr<ue_context> old_uc = {};
old_uc = amf_app_inst->supi_2_ue_context(uc->supi);
uc->copy_pdu_sessions(old_uc);
}
amf_app_inst->set_supi_2_ue_context(uc->supi, uc);
Logger::amf_n1().debug("Update UC context, SUPI %s", uc->supi.c_str());
uc->supi = "imsi-" + supi;
// associate SUPI with UC
// Verify if there's PDU session info in the old context
if (amf_app_inst->is_supi_2_ue_context(uc->supi)) {
std::shared_ptr<ue_context> old_uc = {};
old_uc = amf_app_inst->supi_2_ue_context(uc->supi);
uc->copy_pdu_sessions(old_uc);
}
amf_app_inst->set_supi_2_ue_context(uc->supi, uc);
Logger::amf_n1().debug("Update UC context, SUPI %s", uc->supi.c_str());
}
std::shared_ptr<nas_context> nc = {};
if (is_amf_ue_id_2_nas_context(amf_ue_ngap_id)) {
nc = amf_ue_id_2_nas_context(amf_ue_ngap_id);
if (amf_ue_id_2_nas_context(amf_ue_ngap_id, nc)) {
Logger::amf_n1().debug(
"Find nas_context by amf_ue_ngap_id (" AMF_UE_NGAP_ID_FMT ")",
amf_ue_ngap_id);
} else {
nc = std::shared_ptr<nas_context>(new nas_context);
nc = std::make_shared<nas_context>();
set_amf_ue_ngap_id_2_nas_context(amf_ue_ngap_id, nc);
nc->ctx_avaliability_ind = false;
}
......@@ -850,8 +837,7 @@ void amf_n1::service_request_handle(
Logger::amf_app().debug(
"GUTI %s, 5G-TMSI %s", guti.c_str(), tmsi.c_str());
std::shared_ptr<nas_context> old_nc = {};
if (is_guti_2_nas_context(guti)) {
old_nc = guti_2_nas_context(guti);
if (guti_2_nas_context(guti, old_nc)) {
// nc->security_ctx = old_nc->security_ctx;
}
}
......@@ -1196,10 +1182,9 @@ void amf_n1::registration_request_handle(
Logger::amf_n1().debug("Exiting nas_context");
nc->is_5g_guti_present = true;
nc->to_be_register_by_new_suci = true;
} else if (is_guti_2_nas_context(guti)) {
} else if (guti_2_nas_context(guti, nc)) {
Logger::amf_n1().debug(
"nas_context existed with GUTI %s", guti.c_str());
nc = guti_2_nas_context(guti);
set_amf_ue_ngap_id_2_nas_context(amf_ue_ngap_id, nc);
// Update Nas Context
nc->amf_ue_ngap_id = amf_ue_ngap_id;
......@@ -1265,8 +1250,8 @@ void amf_n1::registration_request_handle(
// Create NAS context
if (nc == nullptr) {
// try to get the GUTI -> nas_context
if (is_guti_2_nas_context(guti)) {
nc = guti_2_nas_context(guti);
if (guti_2_nas_context(guti, nc)) {
// nc = guti_2_nas_context(guti);
set_amf_ue_ngap_id_2_nas_context(amf_ue_ngap_id, nc);
nc->amf_ue_ngap_id = amf_ue_ngap_id;
nc->ran_ue_ngap_id = ran_ue_ngap_id;
......@@ -1574,10 +1559,16 @@ bool amf_n1::is_guti_2_nas_context(const std::string& guti) const {
}
//------------------------------------------------------------------------------
std::shared_ptr<nas_context> amf_n1::guti_2_nas_context(
const std::string& guti) const {
bool amf_n1::guti_2_nas_context(
const std::string& guti, std::shared_ptr<nas_context>& nc) const {
std::shared_lock lock(m_guti2nas_context);
return guti2nas_context.at(guti);
if (guti2nas_context.count(guti) > 0) {
if (guti2nas_context.at(guti) != nullptr) {
nc = guti2nas_context.at(guti);
return true;
}
}
return false;
}
//------------------------------------------------------------------------------
......@@ -4253,19 +4244,11 @@ bool amf_n1::find_ue_context(
string ue_context_key =
conv::get_ue_context_key(nc->ran_ue_ngap_id, nc->amf_ue_ngap_id);
if (!amf_app_inst->is_ran_amf_id_2_ue_context(ue_context_key)) {
if (!amf_app_inst->ran_amf_id_2_ue_context(ue_context_key, uc)) {
Logger::amf_n1().error("No UE context with key %s", ue_context_key.c_str());
return false;
}
uc = amf_app_inst->ran_amf_id_2_ue_context(ue_context_key);
if (uc == nullptr) {
Logger::amf_n1().warn(
"Cannot find the UE context with key %s", ue_context_key.c_str());
return false;
}
return true;
}
......@@ -4276,19 +4259,11 @@ bool amf_n1::find_ue_context(
string ue_context_key =
conv::get_ue_context_key(ran_ue_ngap_id, amf_ue_ngap_id);
if (!amf_app_inst->is_ran_amf_id_2_ue_context(ue_context_key)) {
if (!amf_app_inst->ran_amf_id_2_ue_context(ue_context_key, uc)) {
Logger::amf_n1().error("No UE context with key %s", ue_context_key.c_str());
return false;
}
uc = amf_app_inst->ran_amf_id_2_ue_context(ue_context_key);
if (uc == nullptr) {
Logger::amf_n1().warn(
"Cannot find the UE context with key %s", ue_context_key.c_str());
return false;
}
return true;
}
......
......@@ -115,12 +115,13 @@ class amf_n1 {
bool is_guti_2_nas_context(const std::string& guti) const;
/*
* Get UE NAS context associated with a GUTI
* Get UE NAS context associated with a GUTI if exist and not null
* @param [const std::string&] guti: UE GUTI
* @return shared pointer to the UE NAS context
* @param [std::shared_ptr<nas_context>&] nc: UE NAS Context
* @return true if context exists and not null, otherwise return false
*/
std::shared_ptr<nas_context> guti_2_nas_context(
const std::string& guti) const;
bool guti_2_nas_context(
const std::string& guti, std::shared_ptr<nas_context>& nc) const;
/*
* Store an UE NAS context associated with a GUTI
......
......@@ -202,22 +202,13 @@ void amf_sbi::handle_itti_message(
string ue_context_key = conv::get_ue_context_key(
itti_msg.ran_ue_ngap_id, itti_msg.amf_ue_ngap_id);
std::shared_ptr<ue_context> uc = {};
if (!amf_app_inst->is_ran_amf_id_2_ue_context(ue_context_key)) {
if (!amf_app_inst->ran_amf_id_2_ue_context(ue_context_key, uc)) {
Logger::amf_sbi().error(
"No UE context for %s exit", ue_context_key.c_str());
return;
}
uc = amf_app_inst->ran_amf_id_2_ue_context(ue_context_key);
std::string supi = {};
if (uc != nullptr) {
supi = uc->supi;
} else {
Logger::amf_sbi().error(
"Could not find UE context with key %s", ue_context_key.c_str());
return;
}
std::string supi = uc->supi;
Logger::amf_sbi().debug(
"Send PDU Session Update SM Context Request to SMF (SUPI %s, PDU Session "
......@@ -301,14 +292,7 @@ void amf_sbi::handle_itti_message(itti_nsmf_pdusession_create_sm_context& smf) {
Logger::amf_sbi().info(
"Find ue_context in amf_app using UE Context Key: %s",
ue_context_key.c_str());
if (!amf_app_inst->is_ran_amf_id_2_ue_context(ue_context_key)) {
Logger::amf_sbi().error(
"No UE context for %s exit", ue_context_key.c_str());
return;
}
uc = amf_app_inst->ran_amf_id_2_ue_context(ue_context_key);
if (!uc) {
if (!amf_app_inst->ran_amf_id_2_ue_context(ue_context_key, uc)) {
Logger::amf_sbi().error(
"No UE context for %s exit", ue_context_key.c_str());
return;
......
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