16constexpr const char* LOG_TAG =
"SmReg";
20 static SmRegistry registry;
26 return ErrorCode::INVALID_PARAM;
35 const size_t rfd_len = sm->ran_function_data().size();
37 E3_LOG_ERROR(LOG_TAG) <<
"SM '" << sm->name() <<
"' advertises "
38 << rfd_len <<
" bytes of ran_function_data; E3AP"
40 return ErrorCode::INVALID_PARAM;
43 std::lock_guard lock(mutex_);
45 uint32_t ran_func = sm->ran_function_id();
48 if (sms_.count(ran_func) > 0) {
49 E3_LOG_ERROR(LOG_TAG) <<
"SM already registered for RAN function " << ran_func;
50 return ErrorCode::SM_ALREADY_REGISTERED;
55 if (init_result != ErrorCode::SUCCESS) {
56 E3_LOG_ERROR(LOG_TAG) <<
"Failed to initialize SM: " << sm->name();
60 E3_LOG_INFO(LOG_TAG) <<
"Registered SM '" << sm->name()
61 <<
"' for RAN function " << ran_func;
63 sms_[ran_func] = std::move(sm);
65 return ErrorCode::SUCCESS;
70 return ErrorCode::INVALID_PARAM;
73 std::lock_guard lock(mutex_);
75 if (sms_.count(ran_function_id) > 0 || factories_.count(ran_function_id) > 0) {
76 E3_LOG_ERROR(LOG_TAG) <<
"SM or factory already registered for RAN function "
78 return ErrorCode::SM_ALREADY_REGISTERED;
81 factories_[ran_function_id] = std::move(factory);
82 E3_LOG_INFO(LOG_TAG) <<
"Registered SM factory for RAN function " << ran_function_id;
84 return ErrorCode::SUCCESS;
88 std::lock_guard lock(mutex_);
90 auto it = sms_.find(ran_function_id);
91 if (it != sms_.end()) {
92 if (it->second->is_running()) {
95 it->second->destroy();
97 E3_LOG_INFO(LOG_TAG) <<
"Unregistered SM for RAN function " << ran_function_id;
98 return ErrorCode::SUCCESS;
101 auto factory_it = factories_.find(ran_function_id);
102 if (factory_it != factories_.end()) {
103 factories_.erase(factory_it);
104 return ErrorCode::SUCCESS;
107 return ErrorCode::SM_NOT_FOUND;
111 std::lock_guard lock(mutex_);
114 auto it = sms_.find(ran_function_id);
115 if (it != sms_.end()) {
116 return it->second.get();
120 auto factory_it = factories_.find(ran_function_id);
121 if (factory_it != factories_.end()) {
123 auto sm = factory_it->second();
126 if (init_result == ErrorCode::SUCCESS) {
128 sms_[ran_function_id] = std::move(sm);
129 factories_.erase(factory_it);
139 std::lock_guard lock(mutex_);
141 std::vector<uint32_t> result;
142 result.reserve(sms_.size() + factories_.size());
144 for (
const auto& [
id, sm] : sms_) {
145 result.push_back(
id);
148 for (
const auto& [
id, factory] : factories_) {
149 result.push_back(
id);
152 std::sort(result.begin(), result.end());
157 std::lock_guard lock(mutex_);
159 auto it = sms_.find(ran_function_id);
160 if (it == sms_.end()) {
161 E3_LOG_ERROR(LOG_TAG) <<
"SM not found for RAN function " << ran_function_id;
162 E3_LOG_ERROR(LOG_TAG) <<
"SM not found for RAN function " << ran_function_id;
163 return ErrorCode::SM_NOT_FOUND;
166 auto& sm = it->second;
168 if (sm->is_running()) {
169 E3_LOG_DEBUG(LOG_TAG) <<
"SM already running for RAN function " << ran_function_id;
170 return ErrorCode::SUCCESS;
174 if (result != ErrorCode::SUCCESS) {
175 E3_LOG_ERROR(LOG_TAG) <<
"Failed to start SM for RAN function " << ran_function_id;
179 E3_LOG_INFO(LOG_TAG) <<
"Started SM for RAN function " << ran_function_id;
180 return ErrorCode::SUCCESS;
184 std::lock_guard lock(mutex_);
186 auto it = sms_.find(ran_function_id);
187 if (it == sms_.end()) {
188 return ErrorCode::SM_NOT_FOUND;
191 auto& sm = it->second;
193 if (!sm->is_running()) {
194 return ErrorCode::SUCCESS;
198 E3_LOG_INFO(LOG_TAG) <<
"Stopped SM for RAN function " << ran_function_id;
199 return ErrorCode::SUCCESS;
203 std::lock_guard lock(mutex_);
205 auto it = sms_.find(ran_function_id);
206 if (it != sms_.end()) {
207 return it->second->is_running();
214 std::lock_guard lock(mutex_);
216 for (
auto& [
id, sm] : sms_) {
217 if (sm->is_running()) {
Abstract Service Model interface.
ErrorCode start_sm(uint32_t ran_function_id)
Start SM for a RAN function.
static SmRegistry & instance()
Get the singleton instance.
ServiceModel * get_by_ran_function(uint32_t ran_function_id)
Get SM by RAN function ID.
bool is_sm_running(uint32_t ran_function_id) const
Check if SM is running.
ErrorCode unregister_sm(uint32_t ran_function_id)
Unregister a Service Model by RAN function ID.
ErrorCode register_sm(std::unique_ptr< ServiceModel > sm)
Register a Service Model.
ErrorCode stop_sm(uint32_t ran_function_id)
Stop SM for a RAN function.
ErrorCode register_sm_factory(uint32_t ran_function_id, SmFactory factory)
Register a Service Model factory.
std::vector< uint32_t > get_available_ran_functions() const
Get all available RAN function IDs.
void clear()
Clear all registered SMs.
Logging facility for libe3.
#define E3_LOG_DEBUG(component)
#define E3_LOG_INFO(component)
#define E3_LOG_ERROR(component)
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
constexpr size_t MAX_PROTOCOL_DATA_SIZE
std::function< std::unique_ptr< ServiceModel >()> SmFactory
Factory function type for creating SM instances.
Service Model Interface - Extension point for RAN vendors.