-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroltsn_plugin.c
498 lines (432 loc) · 17.3 KB
/
controltsn_plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (C) 2023 Institute for Control Engineering of Machine Tools and Manufacturing Units at the University of Stuttgart
* Author Stefan Oechsle <stefan.oechsle@isw.uni-stuttgart.de>
*
* This plugin monitors the changes in the sysrepo configuration for the ControlTSN module.
* If certain changes occur, such as the request for a new stream, corresponding notifications are sent.
*/
#include <sysrepo.h>
#include <string.h>
#include <stdio.h>
#include "../../events_definitions.h"
// Subscriptions
sr_subscription_ctx_t *_subscriptions = NULL;
/*
static int
_test_print_mask(uint32_t mask) {
if (mask & EVENT_STREAM_REQUESTED) {
printf("EVENT_STREAM_REQUESTED | ");
}
if (mask & EVENT_STREAM_CONFIGURED) {
printf("EVENT_STREAM_CONFIGURED | ");
}
if (mask & EVENT_MODULE_ADDED) {
printf("EVENT_MODULE_ADDED | ");
}
if (mask & EVENT_MODULE_REGISTERED) {
printf("EVENT_MODULE_REGISTERED | ");
}
if (mask & EVENT_MODULE_DATA_UPDATED) {
printf("EVENT_MODULE_DATA_UPDATED | ");
}
if (mask & EVENT_MODULE_UNREGISTERED) {
printf("EVENT_MODULE_UNREGISTERED | ");
}
if (mask & EVENT_MODULE_DELETED) {
printf("EVENT_MODULE_DELETED | ");
}
if (mask & EVENT_TOPOLOGY_DISCOVERY_REQUESTED) {
printf("EVENT_TOPOLOGY_DISCOVERY_REQUESTED | ");
}
if (mask & EVENT_TOPOLOGY_DISCOVERED) {
printf("EVENT_TOPOLOGY_DISCOVERED | ");
}
printf("\n");
}
*/
static int
//_send_notification(sr_session_ctx_t *session, uint32_t event_id, char *entry_id, char *msg)
_send_notification(sr_session_ctx_t *session, uint64_t event_id, char *entry_id, char *msg)
{
sr_val_t notif_values[3];
notif_values[0].xpath = strdup("/control-tsn-uni:notif-generic/event-id");
//notif_values[0].type = SR_UINT32_T;
//notif_values[0].data.uint32_val = event_id;
notif_values[0].type = SR_UINT64_T;
notif_values[0].data.uint64_val = event_id;
notif_values[1].xpath = strdup("/control-tsn-uni:notif-generic/entry-id");
notif_values[1].type = SR_STRING_T;
notif_values[1].data.string_val = entry_id;
notif_values[2].xpath = strdup("/control-tsn-uni:notif-generic/msg");
notif_values[2].type = SR_STRING_T;
notif_values[2].data.string_val = msg;
int rc = sr_event_notif_send(session, "/control-tsn-uni:notif-generic", notif_values, 3);
if (rc != SR_ERR_OK)
{
printf("[SYSREPO] Failure while sending notification for event id %ld!\n", event_id);
}
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
static char *
_extract_key(char *xpath, char *search_key)
{
char *ptr = NULL;
char *search_term = NULL;
search_term = malloc(strlen(search_key) + 2);
sprintf(search_term, "%s=", search_key);
ptr = strstr(xpath, search_term);
free(search_term);
if (ptr)
{
ptr = strtok(ptr, "'");
ptr = strtok(NULL, "'");
}
return ptr;
}
static int
_save_to_startup(sr_session_ctx_t *session)
{
// Switch to startup datastore
int rc = sr_session_switch_ds(session, SR_DS_STARTUP);
if (rc != SR_ERR_OK)
{
goto cleanup;
}
// Copy configuration from running to startup
rc = sr_copy_config(session, "control-tsn-uni", SR_DS_RUNNING, 0, 1);
if (rc != SR_ERR_OK)
{
goto cleanup;
}
// Switch back to running
// (TODO: is this even necessary?)
rc = sr_session_switch_ds(session, SR_DS_RUNNING);
if (rc != SR_ERR_OK)
{
goto cleanup;
}
cleanup:
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
// -------------------------------------------------------- //
// Module Change Callback
// -------------------------------------------------------- //
static int
_module_change_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event,
uint32_t request_id, void *private_data)
{
(void)module_name;
(void)request_id;
(void)private_data;
int rc = SR_ERR_OK;
sr_val_t *val = NULL;
sr_val_t *old_val = NULL;
sr_val_t *new_val = NULL;
sr_change_iter_t *iter = NULL;
sr_change_oper_t op;
//uint32_t occured_mask = 0;
uint64_t occured_mask = 0;
//uint32_t already_send_mask = 0;
uint64_t already_send_mask = 0;
// TSN_Event_CB_Data notif_data;
// char *notif_data_event_name = NULL;
rc = sr_get_changes_iter(session, "//.", &iter);
if (rc != SR_ERR_OK)
{
printf("[PLUGIN] Failed to get changes!\n");
char err_code[2];
sprintf(err_code, "%d", rc);
_send_notification(session, EVENT_ERROR, err_code, "[PLUGIN] Failed to get changes!");
goto cleanup;
}
// Monitor changes and send corresponding notifications
while (sr_get_change_next(session, iter, &op, &old_val, &new_val) == SR_ERR_OK)
{
val = new_val ? new_val : old_val;
// For each occured change associated with an event
// we add this event to the mask.
// We do not send the notification immediately
// because e.g. a added stream configuration to a stream
// will result in multiple xpaths in this while loop
// STREAM ---------------------------------------------------
if (strstr(val->xpath, "/streams/") != NULL)
{
// Deleted
if ((strstr(val->xpath, "/stream-id") != NULL) &&
(op == SR_OP_DELETED))
{
if ((already_send_mask & EVENT_STREAM_DELETED) == 0)
{
char *key = _extract_key(val->xpath, "stream-id");
rc = _send_notification(session, EVENT_STREAM_DELETED, key, "stream deleted");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_STREAM_DELETED'!\n");
}
else
{
already_send_mask |= EVENT_STREAM_DELETED;
}
}
}
// Requested
if ((strstr(val->xpath, "/request/") != NULL) &&
(op == SR_OP_CREATED))
{
if ((already_send_mask & EVENT_STREAM_REQUESTED) == 0)
{
char *key = _extract_key(val->xpath, "stream-id");
rc = _send_notification(session, EVENT_STREAM_REQUESTED, key, "stream request added");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_STREAM_REQUESTED'!\n");
}
else
{
already_send_mask |= EVENT_STREAM_REQUESTED;
}
}
}
// Configured
if ((!strcmp(val->xpath + strlen(val->xpath) - strlen("/configured"), "/configured")) && (op == SR_OP_MODIFIED || op == SR_OP_CREATED) && (val->data.bool_val == 1))
{
if ((already_send_mask & EVENT_STREAM_CONFIGURED) == 0)
{
char *key = _extract_key(val->xpath, "stream-id");
rc = _send_notification(session, EVENT_STREAM_CONFIGURED, key, "stream configuration completed");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_STREAM_CONFIGURED'!\n");
}
else
{
already_send_mask |= EVENT_STREAM_CONFIGURED;
}
}
}
// Modified (currently not used)
// MODULES ---------------------------------------------------
}
else if (strstr(val->xpath, "/modules/") != NULL)
{
// Added
if ((strstr(val->xpath, "/id") != NULL) && (op = SR_OP_CREATED))
{
if ((already_send_mask & EVENT_MODULE_ADDED) == 0)
{
char *key = _extract_key(val->xpath, "id");
rc = _send_notification(session, EVENT_MODULE_ADDED, key, "module added");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_MODULE_ADDED'!\n");
}
else
{
already_send_mask |= EVENT_MODULE_ADDED;
}
}
}
// Registered / Unregistered
// Check if /registered is at the end of the xpath
if ((!strcmp(val->xpath + strlen(val->xpath) - strlen("/registered"), "/registered")) && (op == SR_OP_MODIFIED))
{
char *key = _extract_key(val->xpath, "id");
uint8_t registered = val->data.uint8_val;
rc = _send_notification(session, registered ? EVENT_MODULE_REGISTERED : EVENT_MODULE_UNREGISTERED, key, registered ? "module registered" : "module unregistered");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification '%s'!\n", (registered ? "EVENT_MODULE_REGISTERED" : "EVENT_MODULE_UNREGISTERED"));
}
else
{
already_send_mask |= (registered ? EVENT_MODULE_REGISTERED : EVENT_MODULE_UNREGISTERED);
}
}
// Data updated
if ((strstr(val->xpath, "/data/entry") != NULL) && ((op == SR_OP_CREATED) || (op == SR_OP_MODIFIED)))
{
if ((already_send_mask & EVENT_MODULE_DATA_UPDATED) == 0)
{
char *key = _extract_key(val->xpath, "id");
rc = _send_notification(session, EVENT_MODULE_DATA_UPDATED, key, "module data updated");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_MODULE_DATA_UPDATED'!\n");
}
else
{
already_send_mask |= EVENT_MODULE_DATA_UPDATED;
}
}
}
// Deleted
if ((strstr(val->xpath, "/id") != NULL) && (op == SR_OP_DELETED))
{
if ((already_send_mask & EVENT_MODULE_DELETED) == 0)
{
char *key = _extract_key(val->xpath, "id");
rc = _send_notification(session, EVENT_MODULE_DELETED, key, "module deleted");
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_MODULE_DELETED'!\n");
}
else
{
already_send_mask |= EVENT_MODULE_DELETED;
}
}
}
// TOPOLOGY ---------------------------------------------------
}
else if (strstr(val->xpath, "/topology/") != NULL)
{
// Discovery requested is send directly and not reflected in changes in the datastore
if ((!strcmp(val->xpath + strlen(val->xpath) - strlen("/topology"), "/topology")) && op == SR_OP_CREATED)
{
// Discovered
if ((already_send_mask & EVENT_TOPOLOGY_DISCOVERED) == 0)
{
rc = _send_notification(session, EVENT_TOPOLOGY_DISCOVERED, NULL, "topology discovered");
already_send_mask |= EVENT_TOPOLOGY_DISCOVERED;
if (rc == EXIT_FAILURE)
{
printf("[PLUGIN] Failed to send notification 'EVENT_TOPOLOGY_DISCOVERED'!\n");
}
else
{
already_send_mask |= EVENT_TOPOLOGY_DISCOVERED;
}
// notif_data.event_id = EVENT_TOPOLOGY_DISCOVERED;
// notif_data.entry_id = NULL;
// notif_data.msg = NULL;
// notif_data_event_name = strdup("EVENT_TOPOLOGY_DISCOVERED");
// already_send_mask |= EVENT_TOPOLOGY_DISCOVERED;
}
}
// APPLICATION ---------------------------------------------------
}
else if (strstr(val->xpath, "/application/") != NULL)
{
// EVENT_APPLICATION_LIST_OF_IMAGES_REQUESTED (directly from sysrepo)
// EVENT_APPLICATION_LIST_OF_APPS_REQUESTED (currently not used)
// EVENT_APPLICATION_APP_START_REQUESTED (directly from sysrepo)
// EVENT_APPLICATION_APP_STOP_REQUESTED (directly from sysrepo)
if (strstr(val->xpath, "/apps") != NULL) {
// APPS ---------------------------------------------------
if (strstr(val->xpath, "/parameters") != NULL && (op == SR_OP_MODIFIED || op == SR_OP_CREATED)) {
if ((already_send_mask & EVENT_CONFIGURATION_CHANGED) == 0) {
// App Parameters changed/created --> Send Event to deploy the new parameters
char *key = _extract_key(val->xpath, "id");
rc = _send_notification(session, EVENT_CONFIGURATION_CHANGED, key, "Application parameters changed!");
if (rc == EXIT_FAILURE) {
printf("[PLUGIN] Failed to send notification '%s'!\n", "EVENT_CONFIGURATION_CHANGED");
} else {
already_send_mask |= EVENT_CONFIGURATION_CHANGED;
}
}
}
}
}
}
// rc = _send_notification(session, notif_data.event_id, notif_data.entry_id, notif_data.msg);
// if (rc == EXIT_FAILURE) {
// printf("[PLUGIN] Failed to send notification '%s'!\n", notif_data_event_name);
// }
cleanup:
return rc;
}
// -------------------------------------------------------- //
// RPC Callbacks
// -------------------------------------------------------- //
/*
static int
_rpc_trigger_topology_discovery_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *path, const sr_val_t *input, const size_t input_cnt,
sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt, void *private_data)
{
(void) session;
(void) sub_id;
(void) path;
(void) input;
(void) input_cnt;
(void) event;
(void) request_id;
(void) output;
(void) output_cnt;
(void) private_data;
// TODO Send Notification
return SR_ERR_OK;
}
*/
// -------------------------------------------------------- //
// Necessary Plugin Functions
// -------------------------------------------------------- //
int sr_plugin_init_cb(sr_session_ctx_t *session, void **private_data)
{
int rc;
sr_val_t *val_plugin_running = NULL;
(void)private_data;
/*
// Check if the plugin is already running
rc = sr_get_item(session, "/control-tsn-uni:tsn-uni/plugin-running", 0, &val_plugin_running);
if (rc != SR_ERR_OK) {
goto error;
}
if (!val_plugin_running->data.bool_val) {
// If not running then set flag to true
sr_val_t val_running;
val_running.type = SR_BOOL_T;
val_running.data.bool_val = 1;
rc = sr_set_item(session, "/control-tsn-uni:tsn-uni/plugin-running", &val_running, 0);
if (rc != SR_ERR_OK) {
goto error;
}
rc = sr_apply_changes(session, 0, 1);
if (rc != SR_ERR_OK) {
goto error;
}
} else {
printf("[PLUGIN] A instance of the plugin is already running!\n");
return SR_ERR_OPERATION_FAILED;
}
*/
// Subscribe for module changes (also causes startup data to be copied into running and enabling the module)
rc = sr_module_change_subscribe(session, "control-tsn-uni", NULL, _module_change_cb, NULL, 0, SR_SUBSCR_DONE_ONLY, &_subscriptions);
if (rc != SR_ERR_OK)
{
printf("[PLUGIN] Error subscribing for module changes!\n");
goto error;
}
/*
// Subscribe for RPC: trigger topology discovery
rc = sr_rpc_subscribe(session, "/control-tsn-uni:rpc-trigger-topology-discovery", _rpc_trigger_topology_discovery_cb, NULL, 0, SR_SUBSCR_CTX_REUSE, &subscriptions);
if (rc != SR_ERR_OK) {
printf("[PLUGIN] Error subscribing for RPC 'trigger-topology-discovery'!\n");
goto error;
}
*/
printf("[PLUGIN] Plugin successfully initialized!\n");
return SR_ERR_OK;
error:
sr_free_val(val_plugin_running);
printf("[PLUGIN] Error! Plugin couldn't be initialized: %s\n", sr_strerror(rc));
sr_unsubscribe(_subscriptions);
return rc;
}
int sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *private_data)
{
(void)session;
(void)private_data;
int rc;
// Reset the 'plugin-running' flag
sr_val_t val_running;
val_running.type = SR_BOOL_T;
val_running.data.bool_val = 0;
rc = sr_set_item(session, "/control-tsn-uni:tsn-uni/plugin-running", &val_running, 0);
rc = sr_apply_changes(session, 0, 1);
// Saving the current configuration by writing the data to the startup datastore
rc = _save_to_startup(session);
// Freeing the subscriptions
rc = sr_unsubscribe(_subscriptions);
printf("[PLUGIN] Plugin cleanup finished.\n");
return rc;
}