Commit d54a2ecf authored by Stefan Spettel's avatar Stefan Spettel

refact(smf): Replace folly AtomicHashMap with std unordered_map

Signed-off-by: default avatarStefan Spettel <stefan.spettel@eurecom.fr>
parent 5b9c8065
...@@ -45,27 +45,30 @@ extern smf_config smf_cfg; ...@@ -45,27 +45,30 @@ extern smf_config smf_cfg;
extern smf_sbi* smf_sbi_inst; extern smf_sbi* smf_sbi_inst;
uint32_t smf_n7::select_pcf(const SmPolicyContextData& context) { uint32_t smf_n7::select_pcf(const SmPolicyContextData& context) {
// TODO abstraction: Here we should choose between local PCC rules and PCF
// client
// TODO PCF selection // TODO PCF selection
if (policy_storages.empty()) { if (policy_storages.empty()) {
std::unique_lock policies_lock(policy_storages_mutex);
// TODO choose between local PCC rules and PCF client, for now only PCF // TODO choose between local PCC rules and PCF client, for now only PCF
// client // client
PlmnId plmn_id = {}; if (smf_cfg.use_local_pcc_rules) {
plmn_id.setMcc(context.getServingNetwork().getMcc()); Logger::smf_n7().warn("Local PCC rules are not supported yet");
plmn_id.setMnc(context.getServingNetwork().getMnc());
std::shared_ptr<smf_pcf_client> storage = smf_pcf_client::discover_pcf(
context.getSliceInfo(), plmn_id, context.getDnn());
if (storage) {
// TODO for now, only use the first PCF
policy_storages.insert(1, storage);
return 1; // ID is always 1, only one PF
} else {
Logger::smf_n7().info("Did not find PCF");
return 0; return 0;
} else {
PlmnId plmn_id = {};
plmn_id.setMcc(context.getServingNetwork().getMcc());
plmn_id.setMnc(context.getServingNetwork().getMnc());
std::shared_ptr<smf_pcf_client> storage = smf_pcf_client::discover_pcf(
context.getSliceInfo(), plmn_id, context.getDnn());
if (storage) {
// TODO for now, only use the first PCF
policy_storages.insert(std::make_pair(1, storage));
return 1; // ID is always 1, only one PF
} else {
Logger::smf_n7().info("Did not find PCF");
return 0;
}
} }
} }
...@@ -79,23 +82,15 @@ sm_policy_status_code smf_n7::create_sm_policy_association( ...@@ -79,23 +82,15 @@ sm_policy_status_code smf_n7::create_sm_policy_association(
if (pcf_id == 0) { if (pcf_id == 0) {
return sm_policy_status_code::PCF_NOT_AVAILABLE; return sm_policy_status_code::PCF_NOT_AVAILABLE;
} }
/*std::shared_ptr<policy_storage> store; association.pcf_id = pcf_id;
std::shared_ptr<policy_storage> storage =
get_policy_storage(association.pcf_id);
try { if (!storage) {
store = policy_storages.at(pcf_id);
} catch (std::exception) {
return sm_policy_status_code::PCF_NOT_AVAILABLE;
} */
folly::AtomicHashMap<uint32_t, std::shared_ptr<policy_storage>>::iterator it =
policy_storages.find(pcf_id);
if (it == policy_storages.end()) {
return sm_policy_status_code::PCF_NOT_AVAILABLE; return sm_policy_status_code::PCF_NOT_AVAILABLE;
} }
sm_policy_status_code res = sm_policy_status_code res = storage->create_policy_association(association);
it->second->create_policy_association(association);
if (res == sm_policy_status_code::CREATED) { if (res == sm_policy_status_code::CREATED) {
if (association.id == 0) { if (association.id == 0) {
...@@ -115,26 +110,32 @@ sm_policy_status_code smf_n7::create_sm_policy_association( ...@@ -115,26 +110,32 @@ sm_policy_status_code smf_n7::create_sm_policy_association(
sm_policy_status_code smf_n7::remove_sm_policy_association( sm_policy_status_code smf_n7::remove_sm_policy_association(
const policy_association& association, const policy_association& association,
const SmPolicyDeleteData& delete_data) { const SmPolicyDeleteData& delete_data) {
folly::AtomicHashMap<uint32_t, std::shared_ptr<policy_storage>>::iterator it = std::shared_ptr<policy_storage> storage =
policy_storages.find(association.pcf_id); get_policy_storage(association.pcf_id);
if (!storage) return sm_policy_status_code::PCF_NOT_AVAILABLE;
if (it == policy_storages.end()) { return storage->remove_policy_association(association, delete_data);
return sm_policy_status_code::PCF_NOT_AVAILABLE;
}
return it->second->remove_policy_association(association, delete_data);
} }
sm_policy_status_code smf_n7::update_sm_policy_association( sm_policy_status_code smf_n7::update_sm_policy_association(
policy_association& association, policy_association& association,
const SmPolicyUpdateContextData& update_data) { const SmPolicyUpdateContextData& update_data) {
folly::AtomicHashMap<uint32_t, std::shared_ptr<policy_storage>>::iterator it = std::shared_ptr<policy_storage> storage =
policy_storages.find(association.pcf_id); get_policy_storage(association.pcf_id);
if (!storage) return sm_policy_status_code::PCF_NOT_AVAILABLE;
return storage->update_policy_association(update_data, association);
}
std::shared_ptr<policy_storage> smf_n7::get_policy_storage(uint32_t pcf_id) {
std::shared_lock policies_lock(policy_storages_mutex);
auto it = policy_storages.find(pcf_id);
if (it == policy_storages.end()) { if (it == policy_storages.end()) {
return sm_policy_status_code::PCF_NOT_AVAILABLE; return nullptr;
} }
return it->second;
return it->second->update_policy_association(update_data, association);
} }
smf_n7::~smf_n7() { smf_n7::~smf_n7() {
...@@ -251,7 +252,7 @@ http_status_code_e smf_pcf_client::send_request( ...@@ -251,7 +252,7 @@ http_status_code_e smf_pcf_client::send_request(
} }
if (!res) { if (!res) {
Logger::smf_sbi().warn( Logger::smf_n7().warn(
"Could not create a new handle to send message to PCF"); "Could not create a new handle to send message to PCF");
smf_sbi_inst->remove_promise(promise_id); smf_sbi_inst->remove_promise(promise_id);
return http_status_code_e::HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR; return http_status_code_e::HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR;
......
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
#include <string> #include <string>
#include <memory> #include <memory>
//#include <folly/concurrency/ConcurrentHashMap.h> #include <unordered_map>
#include <folly/AtomicHashMap.h> #include <shared_mutex>
#include "Snssai.h" #include "Snssai.h"
#include "PlmnId.h" #include "PlmnId.h"
...@@ -253,9 +253,7 @@ class smf_pcf_client : public policy_storage { ...@@ -253,9 +253,7 @@ class smf_pcf_client : public policy_storage {
*/ */
class smf_n7 { class smf_n7 {
public: public:
const uint32_t PCF_CLIENTS = 16; smf_n7() : policy_storages(){};
smf_n7() : policy_storages(PCF_CLIENTS){};
smf_n7(smf_n7 const&) = delete; smf_n7(smf_n7 const&) = delete;
void operator=(smf_n7 const&) = delete; void operator=(smf_n7 const&) = delete;
virtual ~smf_n7(); virtual ~smf_n7();
...@@ -321,11 +319,16 @@ class smf_n7 { ...@@ -321,11 +319,16 @@ class smf_n7 {
uint32_t select_pcf( uint32_t select_pcf(
const oai::smf_server::model::SmPolicyContextData& context); const oai::smf_server::model::SmPolicyContextData& context);
// TODO the ConcurrentHashMap of folly would be much better, but I get a /**
// linker error, we should fix that Reason: AtomicHashMap requires that the * @brief Helper method to receive the policy storage (thread safe)
// amount of objects is known upfront. *
folly::AtomicHashMap<uint32_t, std::shared_ptr<policy_storage>> * @param pcf_id ID of the policy storage
policy_storages; * @return std::shared_ptr<policy_storage> -> nullptr in case not found
*/
std::shared_ptr<policy_storage> get_policy_storage(uint32_t pcf_id);
std::unordered_map<uint32_t, std::shared_ptr<policy_storage>> policy_storages;
mutable std::shared_mutex policy_storages_mutex;
}; };
} // namespace smf::n7 } // namespace smf::n7
#endif /* FILE_SMF_N4_HPP_SEEN */ #endif /* FILE_SMF_N4_HPP_SEEN */
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