forked from sysrepo/sysrepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_changes_example.c
214 lines (184 loc) · 5.98 KB
/
application_changes_example.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
/**
* @file application_changes_example.c
* @author Rastislav Szabo <raszabo@cisco.com>, Lukas Macko <lmacko@cisco.com>
* @brief Example application that uses sysrepo as the configuration datastore. It
* prints the changes made in running data store.
*
* @copyright
* Copyright 2016 Cisco Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include "sysrepo.h"
#include "sysrepo/values.h"
volatile int exit_application = 0;
#define XPATH_MAX_LEN 100
static void
print_change(sr_change_oper_t op, sr_val_t *old_val, sr_val_t *new_val) {
switch(op) {
case SR_OP_CREATED:
if (NULL != new_val) {
printf("CREATED: ");
sr_print_val(new_val);
}
break;
case SR_OP_DELETED:
if (NULL != old_val) {
printf("DELETED: ");
sr_print_val(old_val);
}
break;
case SR_OP_MODIFIED:
if (NULL != old_val && NULL != new_val) {
printf("MODIFIED: ");
printf("old value ");
sr_print_val(old_val);
printf("new value ");
sr_print_val(new_val);
}
break;
case SR_OP_MOVED:
if (NULL != new_val) {
printf("MOVED: %s after %s", new_val->xpath, NULL != old_val ? old_val->xpath : NULL);
}
break;
}
}
static void
print_current_config(sr_session_ctx_t *session, const char *module_name)
{
sr_val_t *values = NULL;
size_t count = 0;
int rc = SR_ERR_OK;
char select_xpath[XPATH_MAX_LEN];
snprintf(select_xpath, XPATH_MAX_LEN, "/%s:*//*", module_name);
rc = sr_get_items(session, select_xpath, &values, &count);
if (SR_ERR_OK != rc) {
printf("Error by sr_get_items: %s", sr_strerror(rc));
return;
}
for (size_t i = 0; i < count; i++){
sr_print_val(&values[i]);
}
sr_free_values(values, count);
}
const char *
ev_to_str(sr_notif_event_t ev) {
switch (ev) {
case SR_EV_VERIFY:
return "verify";
case SR_EV_APPLY:
return "apply";
case SR_EV_ABORT:
default:
return "abort";
}
}
static int
module_change_cb(sr_session_ctx_t *session, const char *module_name, sr_notif_event_t event, void *private_ctx)
{
sr_change_iter_t *it = NULL;
int rc = SR_ERR_OK;
sr_change_oper_t oper;
sr_val_t *old_value = NULL;
sr_val_t *new_value = NULL;
char change_path[XPATH_MAX_LEN] = {0,};
printf("\n\n ========== Notification %s =============================================", ev_to_str(event));
if (SR_EV_APPLY == event) {
printf("\n\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG: ==========\n\n");
print_current_config(session, module_name);
}
printf("\n\n ========== CHANGES: =============================================\n\n");
snprintf(change_path, XPATH_MAX_LEN, "/%s:*", module_name);
rc = sr_get_changes_iter(session, change_path , &it);
if (SR_ERR_OK != rc) {
printf("Get changes iter failed for xpath %s", change_path);
goto cleanup;
}
while (SR_ERR_OK == (rc = sr_get_change_next(session, it,
&oper, &old_value, &new_value))) {
print_change(oper, old_value, new_value);
sr_free_val(old_value);
sr_free_val(new_value);
}
printf("\n\n ========== END OF CHANGES =======================================\n\n");
cleanup:
sr_free_change_iter(it);
return SR_ERR_OK;
}
static void
sigint_handler(int signum)
{
exit_application = 1;
}
int
main(int argc, char **argv)
{
sr_conn_ctx_t *connection = NULL;
sr_session_ctx_t *session = NULL;
sr_subscription_ctx_t *subscription = NULL;
int rc = SR_ERR_OK;
char *module_name = "ietf-interfaces";
if (argc > 1) {
module_name = argv[1];
} else {
printf("\nYou can pass the module name to be subscribed as the first argument\n");
}
printf("Application will watch for changes in %s\n", module_name);
/* connect to sysrepo */
rc = sr_connect("example_application", SR_CONN_DEFAULT, &connection);
if (SR_ERR_OK != rc) {
fprintf(stderr, "Error by sr_connect: %s\n", sr_strerror(rc));
goto cleanup;
}
/* start session */
rc = sr_session_start(connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &session);
if (SR_ERR_OK != rc) {
fprintf(stderr, "Error by sr_session_start: %s\n", sr_strerror(rc));
goto cleanup;
}
/* read startup config */
printf("\n\n ========== READING STARTUP CONFIG: ==========\n\n");
print_current_config(session, module_name);
/* subscribe for changes in running config */
rc = sr_module_change_subscribe(session, module_name, module_change_cb, NULL,
0, SR_SUBSCR_DEFAULT, &subscription);
if (SR_ERR_OK != rc) {
fprintf(stderr, "Error by sr_module_change_subscribe: %s\n", sr_strerror(rc));
goto cleanup;
}
printf("\n\n ========== STARTUP CONFIG APPLIED AS RUNNING ==========\n\n");
/* loop until ctrl-c is pressed / SIGINT is received */
signal(SIGINT, sigint_handler);
signal(SIGPIPE, SIG_IGN);
while (!exit_application) {
sleep(1000); /* or do some more useful work... */
}
printf("Application exit requested, exiting.\n");
cleanup:
if (NULL != subscription) {
sr_unsubscribe(session, subscription);
}
if (NULL != session) {
sr_session_stop(session);
}
if (NULL != connection) {
sr_disconnect(connection);
}
return rc;
}