|
| 1 | +/* |
| 2 | + * Events library |
| 3 | + * |
| 4 | + * APIs are for publishing & receiving events with source, tag and params along with timestamp. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +class events_base; |
| 10 | + |
| 11 | +typedef events_base* event_handle_t; |
| 12 | + |
| 13 | +/* |
| 14 | + * Initialize an event publisher instance for an event source. |
| 15 | + * |
| 16 | + * A single publisher instance is maintained for a source. |
| 17 | + * Any duplicate init call for a source will return the same instance. |
| 18 | + * |
| 19 | + * Choosing cache will help read cached data, during downtime, if any. |
| 20 | + * |
| 21 | + * NOTE: |
| 22 | + * The initialization occurs asynchronously. |
| 23 | + * Any event published before init is complete, is blocked until the init |
| 24 | + * is complete. Hence recommend, do the init as soon as the process starts. |
| 25 | + * |
| 26 | + * Input: |
| 27 | + * event_source |
| 28 | + * The YANG module name for the event source. All events published with the handle |
| 29 | + * returned by this call is tagged with this source, transparently. The receiver |
| 30 | + * could subscribe with this source as filter. |
| 31 | + * Return |
| 32 | + * Non NULL handle |
| 33 | + * NULL on failure |
| 34 | + */ |
| 35 | + |
| 36 | +event_handle_t events_init_publisher(std::string &event_source); |
| 37 | + |
| 38 | +/* |
| 39 | + * De-init/free the publisher |
| 40 | + * |
| 41 | + * Input: |
| 42 | + * Handle returned from events_init_publisher |
| 43 | + * |
| 44 | + * Output: |
| 45 | + * None |
| 46 | + */ |
| 47 | +void events_deinit_publisher(event_handle_t &handle); |
| 48 | + |
| 49 | + |
| 50 | +/* |
| 51 | + * List of event params |
| 52 | + */ |
| 53 | +typedef std::map<std::string, std::string> event_params_t; |
| 54 | + |
| 55 | +/* |
| 56 | + * Publish an event |
| 57 | + * |
| 58 | + * Internally a globally unique sequence number is embedded in every published event, |
| 59 | + * The sequence numbers from same publishing instances can be compared |
| 60 | + * to see if there any missing events between. |
| 61 | + * |
| 62 | + * The sequence has two components as run-time-id that distinguishes |
| 63 | + * the running instance of a publisher and other a running sequence |
| 64 | + * starting from 0, which is local to this runtime-id. |
| 65 | + * |
| 66 | + * The receiver API keep next last received number for each runtime id |
| 67 | + * and use this info to compute missed event count upon next event. |
| 68 | + * |
| 69 | + * input: |
| 70 | + * handle - As obtained from events_init_publisher for a event-source. |
| 71 | + * |
| 72 | + * event_tag - |
| 73 | + * Name of the YANG container that defines this event in the |
| 74 | + * event-source module associated with this handle. |
| 75 | + * |
| 76 | + * YANG path formatted as "< event_source >:< event_tag >" |
| 77 | + * e.g. {"sonic-events-bgp:bgp-state": { "ip": "10.10.10.10", ...}} |
| 78 | + * |
| 79 | + * params - |
| 80 | + * Params associated with event; This may or may not contain |
| 81 | + * timestamp. In the absence, the timestamp is added, transparently. |
| 82 | + * |
| 83 | + */ |
| 84 | +void event_publish(event_handle_t handle, const std:string &event_tag, |
| 85 | + const event_params_t *params=NULL); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +typedef std::vector<std::string> event_subscribe_sources_t; |
| 90 | + |
| 91 | +/* |
| 92 | + * Initialize subscriber. |
| 93 | + * Init subscriber, optionally to filter by event-source. |
| 94 | + * |
| 95 | + * Input: |
| 96 | + * use_cache |
| 97 | + * When set to true, it will make use of the cache service transparently. |
| 98 | + * The cache service caches events during session down time (last deinit to this |
| 99 | + * init call). |
| 100 | + * |
| 101 | + * lst_subscribe_sources_t |
| 102 | + * List of subscription sources of interest. |
| 103 | + * The source value is the corresponding YANG module name. |
| 104 | + * e.g. "sonic-events-bgp " is the source modulr name for bgp. |
| 105 | + * |
| 106 | + * Return: |
| 107 | + * Non NULL handle on success |
| 108 | + * NULL on failure |
| 109 | + */ |
| 110 | +event_handle_t events_init_subscriber(bool use_cache=false, |
| 111 | + const event_subscribe_sources_t *sources=NULL); |
| 112 | + |
| 113 | +/* |
| 114 | + * De-init/free the subscriber |
| 115 | + * |
| 116 | + * Input: |
| 117 | + * Handle returned from events_init_subscriber |
| 118 | + * |
| 119 | + * Output: |
| 120 | + * None |
| 121 | + */ |
| 122 | +void events_deinit_subscriber(event_handle_t &handle); |
| 123 | + |
| 124 | +/* |
| 125 | + * Received event as JSON string as |
| 126 | + * < YANG path of schema >: { |
| 127 | + * event_params_t |
| 128 | + * } |
| 129 | + */ |
| 130 | +typedef std::string event_str_t; |
| 131 | + |
| 132 | +/* |
| 133 | + * Receive an event. |
| 134 | + * A blocking call. |
| 135 | + * |
| 136 | + * This API maintains an expected sequence number and use the received |
| 137 | + * sequence in event to compute missed events count. |
| 138 | + * |
| 139 | + * input: |
| 140 | + * handle - As obtained from events_init_subscriber |
| 141 | + * |
| 142 | + * output: |
| 143 | + * event - Received event. |
| 144 | + * |
| 145 | + * missed_cnt: |
| 146 | + * Count of missed events from this sender, before this event. Sum of |
| 147 | + * missed count from all received events will give the total missed. |
| 148 | + */ |
| 149 | + int missed_cnt; |
| 150 | + |
| 151 | + * |
| 152 | + * return: |
| 153 | + * 0 - On success |
| 154 | + * -1 - On failure. The handle is not valid. |
| 155 | + * |
| 156 | + */ |
| 157 | +int event_receive(event_handle_t handle, event_str_t &event, int &missed_cnt); |
| 158 | + |
0 commit comments