libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
subscription_manager.hpp
Go to the documentation of this file.
1/**
2 * @file subscription_manager.hpp
3 * @brief E3 Subscription Manager for tracking dApp registrations
4 *
5 * This module manages the associations between dApps and RAN functions.
6 * Ported from the original C implementation's e3_subscription_manager.
7 *
8 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12#ifndef LIBE3_SUBSCRIPTION_MANAGER_HPP
13#define LIBE3_SUBSCRIPTION_MANAGER_HPP
14
15#include "types.hpp"
16#include <chrono>
17#include <mutex>
18#include <optional>
19#include <shared_mutex>
20#include <utility>
21#include <vector>
22#include <unordered_map>
23#include <unordered_set>
24#include <functional>
25
26namespace libe3 {
27
28/**
29 * @brief Per-subscription metadata stored by the SubscriptionManager.
30 */
32 std::vector<uint32_t> telemetry_ids; ///< Telemetry identifiers the dApp asked for
33 std::vector<uint32_t> control_ids; ///< Control identifiers the dApp asked for
34 uint32_t periodicity_us{0}; ///< 0 = use SM default
35 uint32_t subscription_time_s{0}; ///< How long the subscription lasts, 0 = no limit
36 /// When the subscription ends. Unset when `subscription_time_s` is 0.
37 std::optional<std::chrono::steady_clock::time_point> expires_at;
38};
39
40/**
41 * @brief A subscription ended because its subscriptionTime ran out.
42 */
44 uint32_t dapp_id{0}; ///< dApp whose subscription ended
45 uint32_t ran_function_id{0}; ///< RAN function it was for
46 uint32_t subscription_id{0}; ///< Id of the subscription that ended
47};
48
49/// Source of "now" for subscription expiry. Empty means std::chrono::steady_clock.
50using SubscriptionClock = std::function<std::chrono::steady_clock::time_point()>;
51
52/**
53 * @brief Callback type for SM lifecycle events
54 *
55 * Called when a RAN function gains its first subscriber or loses
56 * its last subscriber. This allows the E3Agent to start/stop
57 * Service Models as needed.
58 *
59 * @param ran_function_id The RAN function ID
60 * @param should_start True if SM should start, false if it should stop
61 */
62using SmLifecycleCallback = std::function<void(uint32_t ran_function_id, bool should_start)>;
63
64/**
65 * @brief E3 Subscription Manager
66 *
67 * Thread-safe manager for dApp registrations and RAN function subscriptions.
68 * Handles:
69 * - dApp registration from E3 Setup requests
70 * - Subscription management from E3 Subscription requests
71 * - RAN function lifecycle coordination
72 *
73 * This class is ported from the original C e3_subscription_manager
74 * with modern C++ idioms for thread safety and memory management.
75 */
77public:
78 /**
79 * @brief Construct a new Subscription Manager
80 *
81 * @param clock Source of "now" for subscription expiry, so tests can move
82 * time without sleeping. Defaults to std::chrono::steady_clock.
83 */
84 explicit SubscriptionManager(SubscriptionClock clock = {});
85
86 /**
87 * @brief Destructor
88 */
90
91 // Non-copyable, non-movable (contains mutex)
96
97 /**
98 * @brief Set callback for SM lifecycle events
99 * @param callback Called when a RAN function gains its first or loses its last subscriber
100 */
102
103 // =========================================================================
104 // dApp Registration Management (E3 Setup flow)
105 // =========================================================================
106
107 /**
108 * @brief Register a dApp from E3 Setup Request
109 *
110 * The SubscriptionManager assigns a unique dApp ID automatically.
111 *
112 * @return std::pair containing:
113 * - ErrorCode::SUCCESS on success, or error code on failure
114 * - The assigned dApp ID (valid only if ErrorCode::SUCCESS)
115 * @return ErrorCode::INTERNAL_ERROR if no IDs available (max 101 dApps)
116 */
117 std::pair<ErrorCode, uint32_t> register_dapp();
118
119 /**
120 * @brief Unregister a dApp and clean up all its subscriptions
121 *
122 * @param dapp_id dApp identifier
123 * @return ErrorCode::SUCCESS on success
124 * @return ErrorCode::DAPP_NOT_REGISTERED if dApp not found
125 */
126 ErrorCode unregister_dapp(uint32_t dapp_id);
127
128 /**
129 * @brief Check if a dApp is registered
130 *
131 * @param dapp_id dApp identifier
132 * @return true if registered
133 */
134 bool is_dapp_registered(uint32_t dapp_id) const;
135
136 /**
137 * @brief Get list of registered dApp IDs
138 * @return The dApp IDs
139 */
140 std::vector<uint32_t> get_registered_dapps() const;
141
142 // =========================================================================
143 // Subscription Management (E3 Subscription flow)
144 // =========================================================================
145
146 /**
147 * @brief Add a subscription between dApp and RAN function
148 *
149 * @param dapp_id dApp identifier
150 * @param ran_function_id RAN function identifier (0-255)
151 * @param telemetry_ids Telemetry IDs the dApp wants
152 * @param control_ids Control IDs the dApp wants
153 * @param periodicity_us Reporting periodicity in microseconds (0 = SM default)
154 * @param subscription_time_s Seconds the subscription lasts (0 = no limit).
155 * When it runs out the subscription is removed as if the dApp had
156 * deleted it, see expire_subscriptions().
157 * @return std::pair containing:
158 * - ErrorCode::SUCCESS on success, or error code on failure
159 * - The assigned subscription ID (valid only if ErrorCode::SUCCESS)
160 * @return ErrorCode::DAPP_NOT_REGISTERED if dApp not registered
161 * @return ErrorCode::SUBSCRIPTION_EXISTS if already subscribed. A subscription
162 * that has run out but not yet been removed does not count: the
163 * request starts a new one under the same subscription ID.
164 */
165 std::pair<ErrorCode, uint32_t> add_subscription(
166 uint32_t dapp_id,
167 uint32_t ran_function_id,
168 const std::vector<uint32_t>& telemetry_ids = {},
169 const std::vector<uint32_t>& control_ids = {},
170 uint32_t periodicity_us = 0,
171 uint32_t subscription_time_s = 0);
172
173 /**
174 * @brief Remove every subscription whose time has run out
175 *
176 * Removal is silent towards the dApp: E3AP has no "subscription expired"
177 * PDU. The SM lifecycle callback fires for a RAN function that loses its
178 * last subscriber. Call this periodically; get_subscribed_dapps() already
179 * leaves out an expired subscription in between.
180 *
181 * @return the subscriptions that were removed
182 */
183 std::vector<ExpiredSubscription> expire_subscriptions();
184
185 /**
186 * @brief Remove a subscription between dApp and RAN function
187 *
188 * @param dapp_id dApp identifier
189 * @param ran_function_id RAN function identifier
190 * @return ErrorCode::SUCCESS on success
191 * @return ErrorCode::SUBSCRIPTION_NOT_FOUND if subscription doesn't exist
192 */
193 ErrorCode remove_subscription(uint32_t dapp_id, uint32_t ran_function_id);
194
195 /**
196 * @brief Remove a subscription by its ID
197 *
198 * @param dapp_id dApp identifier
199 * @param subscription_id Subscription ID
200 * @return ErrorCode::SUCCESS on success
201 * @return ErrorCode::SUBSCRIPTION_NOT_FOUND if subscription doesn't exist
202 */
203 ErrorCode remove_subscription_by_id(uint32_t dapp_id, uint32_t subscription_id);
204
205 /**
206 * @brief Check if dApp is subscribed to a RAN function
207 * @param dapp_id The dApp
208 * @param ran_function_id The RAN function
209 * @return true if the dApp is subscribed to the RAN function
210 */
211 bool is_subscribed(uint32_t dapp_id, uint32_t ran_function_id) const;
212
213 /**
214 * @brief Get all RAN functions a dApp is subscribed to
215 * @param dapp_id The dApp
216 * @return The RAN function IDs
217 */
218 std::vector<uint32_t> get_dapp_subscriptions(uint32_t dapp_id) const;
219
220 /**
221 * @brief Record that a message from a dApp has just arrived
222 *
223 * Only messages the dApp sends count. A message to the dApp proves nothing:
224 * the RAN broadcasts, so it is sent whether or not the dApp is still there.
225 *
226 * @param dapp_id dApp identifier; an id that is not registered is ignored
227 */
228 void touch_dapp(uint32_t dapp_id);
229
230 /**
231 * @brief Which registration of a dApp id is current
232 *
233 * An id is handed out again after its dApp is removed, so a record that
234 * names a dApp by id alone can end up naming a later one. Remember the
235 * generation next to the id and compare before acting on it.
236 *
237 * @return The registration's generation, or nothing if the id is not registered
238 * @param dapp_id The dApp
239 */
240 std::optional<uint64_t> dapp_generation(uint32_t dapp_id) const;
241
242 /**
243 * @brief Registered dApps that have not sent a message for at least @p idle_for
244 *
245 * A dApp that registered and never spoke counts from its registration.
246 * Nothing is removed; the caller decides, then calls unregister_dapp().
247 *
248 * @param idle_for Time without a message from the dApp
249 * @return Identifiers of the idle dApps, lowest first
250 */
251 std::vector<uint32_t> idle_dapps(std::chrono::seconds idle_for) const;
252
253 /**
254 * @brief Get all dApps subscribed to a RAN function
255 *
256 * Leaves out a dApp whose subscription time has run out, even before
257 * expire_subscriptions() removes it, so no indication goes past the deadline.
258 *
259 * @param ran_function_id RAN function to look up
260 * @return ids of the dApps with a live subscription to it
261 */
262 std::vector<uint32_t> get_subscribed_dapps(uint32_t ran_function_id) const;
263
264 /**
265 * @brief Get subscription details for a specific dApp + RAN function pair
266 *
267 * @param dapp_id dApp that holds the subscription
268 * @param ran_function_id RAN function it is for
269 * @return A copy taken under the lock, or nullopt if no such subscription
270 * exists. A copy, not a pointer: the subscription can be removed or
271 * renewed by another thread (including the expiry sweep) at any time.
272 */
273 std::optional<SubscriptionDetails> get_subscription_details(uint32_t dapp_id, uint32_t ran_function_id) const;
274
275 /**
276 * @brief Get count of subscribers for a RAN function
277 * @param ran_function_id The RAN function
278 * @return The number of dApps subscribed to it
279 */
280 size_t get_subscriber_count(uint32_t ran_function_id) const;
281
282 // =========================================================================
283 // RAN Function Management
284 // =========================================================================
285
286 /**
287 * @brief Get all RAN functions that have at least one subscriber
288 * @return The RAN function IDs with a subscriber
289 */
290 std::vector<uint32_t> get_active_ran_functions() const;
291
292 /**
293 * @brief Check if any dApp is subscribed to a RAN function
294 * @param ran_function_id The RAN function
295 * @return true if a dApp is subscribed to it
296 */
297 bool has_subscribers(uint32_t ran_function_id) const;
298
299 // =========================================================================
300 // Statistics
301 // =========================================================================
302
303 /**
304 * @brief Get total number of registered dApps
305 * @return The number of registered dApps
306 */
307 size_t dapp_count() const;
308
309 /**
310 * @brief Get total number of active subscriptions
311 * @return The number of subscriptions
312 */
313 size_t subscription_count() const;
314
315 /**
316 * @brief Clear all registrations and subscriptions
317 */
318 void clear();
319
320private:
321 // Using shared_mutex for read-heavy workloads
322 mutable std::shared_mutex mutex_;
323
324 // dApp ID -> registration entry
325 std::unordered_map<uint32_t, DAppEntry> registered_dapps_;
326 uint64_t last_generation_{0}; ///< Generation of the latest registration, under mutex_
327
328 // dApp ID -> set of subscribed RAN function IDs
329 std::unordered_map<uint32_t, std::unordered_set<uint32_t>> dapp_subscriptions_;
330
331 // RAN function ID -> set of subscribed dApp IDs (reverse index for fast lookup)
332 std::unordered_map<uint32_t, std::unordered_set<uint32_t>> ran_function_subscribers_;
333
334 // Subscription ID -> (dApp ID, RAN function ID) mapping
335 std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> subscription_ids_;
336
337 // (dApp ID, RAN function ID) -> Subscription ID reverse mapping
338 std::unordered_map<uint64_t, uint32_t> subscription_id_reverse_;
339
340 // (dApp ID, RAN function ID) -> per-subscription metadata
341 std::unordered_map<uint64_t, SubscriptionDetails> subscription_details_;
342
343 // Callback for SM lifecycle events
344 SmLifecycleCallback sm_lifecycle_callback_;
345
346 SubscriptionClock clock_;
347
348 std::chrono::steady_clock::time_point now() const {
349 return clock_ ? clock_() : std::chrono::steady_clock::now();
350 }
351
352 static bool is_expired(const SubscriptionDetails& details,
353 std::chrono::steady_clock::time_point at) {
354 return details.expires_at && at >= *details.expires_at;
355 }
356
357 // Helper to create composite key for subscription
358 static uint64_t make_sub_key(uint32_t dapp_id, uint32_t ran_func_id) {
359 return (static_cast<uint64_t>(dapp_id) << 32) | ran_func_id;
360 }
361
362 /**
363 * @brief Check if SM should be started/stopped and invoke callback
364 *
365 * Called after subscription changes to notify the E3Agent about
366 * RAN functions that need their SMs started or stopped.
367 *
368 * @param ran_function_id RAN function that changed
369 * @param had_subscribers Whether it had subscribers before the change
370 */
371 void check_sm_lifecycle(uint32_t ran_function_id, bool had_subscribers);
372};
373
374} // namespace libe3
375
376#endif // LIBE3_SUBSCRIPTION_MANAGER_HPP
std::vector< uint32_t > get_active_ran_functions() const
Get all RAN functions that have at least one subscriber.
std::vector< uint32_t > get_registered_dapps() const
Get list of registered dApp IDs.
ErrorCode remove_subscription(uint32_t dapp_id, uint32_t ran_function_id)
Remove a subscription between dApp and RAN function.
bool is_subscribed(uint32_t dapp_id, uint32_t ran_function_id) const
Check if dApp is subscribed to a RAN function.
std::optional< uint64_t > dapp_generation(uint32_t dapp_id) const
Which registration of a dApp id is current.
std::vector< ExpiredSubscription > expire_subscriptions()
Remove every subscription whose time has run out.
void set_sm_lifecycle_callback(SmLifecycleCallback callback)
Set callback for SM lifecycle events.
ErrorCode unregister_dapp(uint32_t dapp_id)
Unregister a dApp and clean up all its subscriptions.
SubscriptionManager(const SubscriptionManager &)=delete
SubscriptionManager & operator=(SubscriptionManager &&)=delete
std::pair< ErrorCode, uint32_t > register_dapp()
Register a dApp from E3 Setup Request.
void touch_dapp(uint32_t dapp_id)
Record that a message from a dApp has just arrived.
std::pair< ErrorCode, uint32_t > add_subscription(uint32_t dapp_id, uint32_t ran_function_id, const std::vector< uint32_t > &telemetry_ids={}, const std::vector< uint32_t > &control_ids={}, uint32_t periodicity_us=0, uint32_t subscription_time_s=0)
Add a subscription between dApp and RAN function.
SubscriptionManager & operator=(const SubscriptionManager &)=delete
size_t get_subscriber_count(uint32_t ran_function_id) const
Get count of subscribers for a RAN function.
bool has_subscribers(uint32_t ran_function_id) const
Check if any dApp is subscribed to a RAN function.
void clear()
Clear all registrations and subscriptions.
std::vector< uint32_t > get_dapp_subscriptions(uint32_t dapp_id) const
Get all RAN functions a dApp is subscribed to.
SubscriptionManager(SubscriptionManager &&)=delete
std::vector< uint32_t > idle_dapps(std::chrono::seconds idle_for) const
Registered dApps that have not sent a message for at least idle_for.
bool is_dapp_registered(uint32_t dapp_id) const
Check if a dApp is registered.
std::vector< uint32_t > get_subscribed_dapps(uint32_t ran_function_id) const
Get all dApps subscribed to a RAN function.
size_t subscription_count() const
Get total number of active subscriptions.
std::optional< SubscriptionDetails > get_subscription_details(uint32_t dapp_id, uint32_t ran_function_id) const
Get subscription details for a specific dApp + RAN function pair.
ErrorCode remove_subscription_by_id(uint32_t dapp_id, uint32_t subscription_id)
Remove a subscription by its ID.
size_t dapp_count() const
Get total number of registered dApps.
SubscriptionManager(SubscriptionClock clock={})
Construct a new Subscription Manager.
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:177
std::function< void(uint32_t ran_function_id, bool should_start)> SmLifecycleCallback
Callback type for SM lifecycle events.
std::function< std::chrono::steady_clock::time_point()> SubscriptionClock
Source of "now" for subscription expiry. Empty means std::chrono::steady_clock.
A subscription ended because its subscriptionTime ran out.
uint32_t ran_function_id
RAN function it was for.
uint32_t subscription_id
Id of the subscription that ended.
uint32_t dapp_id
dApp whose subscription ended
Per-subscription metadata stored by the SubscriptionManager.
uint32_t periodicity_us
0 = use SM default
std::vector< uint32_t > control_ids
Control identifiers the dApp asked for.
std::vector< uint32_t > telemetry_ids
Telemetry identifiers the dApp asked for.
std::optional< std::chrono::steady_clock::time_point > expires_at
When the subscription ends. Unset when subscription_time_s is 0.
E3AP Protocol Types - Vendor-neutral type definitions.