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;
46 E3_LOG_ERROR(LOG_TAG) <<
"SM '" << sm->name() <<
"' lists more than "
48 return ErrorCode::INVALID_PARAM;
51 std::lock_guard lock(mutex_);
54 E3_LOG_ERROR(LOG_TAG) <<
"Cannot register SM '" << sm->name() <<
"': "
56 return ErrorCode::INVALID_PARAM;
59 uint32_t ran_func = sm->ran_function_id();
62 if (sms_.count(ran_func) > 0) {
63 E3_LOG_ERROR(LOG_TAG) <<
"SM already registered for RAN function " << ran_func;
64 return ErrorCode::SM_ALREADY_REGISTERED;
69 if (init_result != ErrorCode::SUCCESS) {
70 E3_LOG_ERROR(LOG_TAG) <<
"Failed to initialize SM: " << sm->name();
74 E3_LOG_INFO(LOG_TAG) <<
"Registered SM '" << sm->name()
75 <<
"' for RAN function " << ran_func;
77 sms_[ran_func] = std::move(sm);
79 return ErrorCode::SUCCESS;
84 return ErrorCode::INVALID_PARAM;
87 std::lock_guard lock(mutex_);
89 if (sms_.count(ran_function_id) > 0 || factories_.count(ran_function_id) > 0) {
90 E3_LOG_ERROR(LOG_TAG) <<
"SM or factory already registered for RAN function "
92 return ErrorCode::SM_ALREADY_REGISTERED;
95 E3_LOG_ERROR(LOG_TAG) <<
"Cannot register a factory for RAN function " << ran_function_id
97 return ErrorCode::INVALID_PARAM;
100 factories_[ran_function_id] = std::move(factory);
101 E3_LOG_INFO(LOG_TAG) <<
"Registered SM factory for RAN function " << ran_function_id;
103 return ErrorCode::SUCCESS;
107 std::lock_guard lock(mutex_);
109 auto it = sms_.find(ran_function_id);
110 if (it != sms_.end()) {
111 if (it->second->is_running()) {
114 it->second->destroy();
116 E3_LOG_INFO(LOG_TAG) <<
"Unregistered SM for RAN function " << ran_function_id;
117 return ErrorCode::SUCCESS;
120 auto factory_it = factories_.find(ran_function_id);
121 if (factory_it != factories_.end()) {
122 factories_.erase(factory_it);
123 return ErrorCode::SUCCESS;
126 return ErrorCode::SM_NOT_FOUND;
130 std::lock_guard lock(mutex_);
133 auto it = sms_.find(ran_function_id);
134 if (it != sms_.end()) {
135 return it->second.get();
139 auto factory_it = factories_.find(ran_function_id);
140 if (factory_it != factories_.end()) {
142 auto sm = factory_it->second();
145 if (init_result == ErrorCode::SUCCESS) {
147 sms_[ran_function_id] = std::move(sm);
148 factories_.erase(factory_it);
158 std::lock_guard lock(mutex_);
160 std::vector<uint32_t> result;
161 result.reserve(sms_.size() + factories_.size());
163 for (
const auto& [
id, sm] : sms_) {
164 result.push_back(
id);
167 for (
const auto& [
id, factory] : factories_) {
168 result.push_back(
id);
171 std::sort(result.begin(), result.end());
176 std::lock_guard lock(mutex_);
178 auto it = sms_.find(ran_function_id);
179 if (it == sms_.end()) {
180 E3_LOG_ERROR(LOG_TAG) <<
"SM not found for RAN function " << ran_function_id;
181 E3_LOG_ERROR(LOG_TAG) <<
"SM not found for RAN function " << ran_function_id;
182 return ErrorCode::SM_NOT_FOUND;
185 auto& sm = it->second;
187 if (sm->is_running()) {
188 E3_LOG_DEBUG(LOG_TAG) <<
"SM already running for RAN function " << ran_function_id;
189 return ErrorCode::SUCCESS;
193 if (result != ErrorCode::SUCCESS) {
194 E3_LOG_ERROR(LOG_TAG) <<
"Failed to start SM for RAN function " << ran_function_id;
198 E3_LOG_INFO(LOG_TAG) <<
"Started 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 ErrorCode::SM_NOT_FOUND;
210 auto& sm = it->second;
212 if (!sm->is_running()) {
213 return ErrorCode::SUCCESS;
217 E3_LOG_INFO(LOG_TAG) <<
"Stopped SM for RAN function " << ran_function_id;
218 return ErrorCode::SUCCESS;
222 std::lock_guard lock(mutex_);
224 auto it = sms_.find(ran_function_id);
225 if (it != sms_.end()) {
226 return it->second->is_running();
233 std::lock_guard lock(mutex_);
235 for (
auto& [
id, sm] : sms_) {
236 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
Largest protocolData of an indication, in bytes.
std::function< std::unique_ptr< ServiceModel >()> SmFactory
Factory function type for creating SM instances.
constexpr size_t MAX_RAN_FUNCTIONS
ranFunctionList SIZE (0..64)
constexpr size_t MAX_IDENTIFIER_LIST_SIZE
telemetry and control lists SIZE (0..256)
Service Model Interface - Extension point for RAN vendors.