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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2018 NetDEF, Inc.
* Renato Westphal
* Copyright (C) 2025 LabN Consulting, L.L.C.
*/
#include <zebra.h>
#include <sys/stat.h>
#include "debug.h"
#include "frrevent.h"
#include "vty.h"
#include "command.h"
#include "memory.h"
#include "lib_vty.h"
#include "log.h"
#include "northbound.h"
#include "northbound_cli.h"
static struct event_loop *master;
static struct lyd_node *data_tree;
static uint data_tree_lock;
const char *data_json = "\n"
"{\n"
" \"frr-test-module:frr-test-module\": {\n"
" \"vrfs\": {\n"
" \"vrf\": [\n"
" {\n"
" \"name\": \"vrf0\",\n"
" \"interfaces\": {\n"
" \"interface\": [\n"
" \"eth0\",\n"
" \"eth1\",\n"
" \"eth2\",\n"
" \"eth3\"\n"
" ],\n"
" \"interface-new\": [\n"
" \"eth0\",\n"
" \"eth1\",\n"
" \"eth2\",\n"
" \"eth3\"\n"
" ]\n"
" },\n"
" \"routes\": {\n"
" \"route\": [\n"
" {\n"
" \"prefix\": \"10.0.0.0/32\",\n"
" \"next-hop\": \"172.16.0.0\",\n"
" \"interface\": \"eth0\",\n"
" \"metric\": 0,\n"
" \"active\": [null]\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.1/32\",\n"
" \"next-hop\": \"172.16.0.1\",\n"
" \"interface\": \"eth1\",\n"
" \"metric\": 1\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.2/32\",\n"
" \"next-hop\": \"172.16.0.2\",\n"
" \"interface\": \"eth2\",\n"
" \"metric\": 2,\n"
" \"active\": [null]\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.3/32\",\n"
" \"next-hop\": \"172.16.0.3\",\n"
" \"interface\": \"eth3\",\n"
" \"metric\": 3\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.4/32\",\n"
" \"next-hop\": \"172.16.0.4\",\n"
" \"interface\": \"eth4\",\n"
" \"metric\": 4,\n"
" \"active\": [null]\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.5/32\",\n"
" \"next-hop\": \"172.16.0.5\",\n"
" \"interface\": \"eth5\",\n"
" \"metric\": 5\n"
" }\n"
" ]\n"
" }\n"
" },\n"
" {\n"
" \"name\": \"vrf1\",\n"
" \"interfaces\": {\n"
" \"interface\": [\n"
" \"eth0\",\n"
" \"eth1\",\n"
" \"eth2\",\n"
" \"eth3\"\n"
" ],\n"
" \"interface-new\": [\n"
" \"eth0\",\n"
" \"eth1\",\n"
" \"eth2\",\n"
" \"eth3\"\n"
" ]\n"
" },\n"
" \"routes\": {\n"
" \"route\": [\n"
" {\n"
" \"prefix\": \"10.0.0.0/32\",\n"
" \"next-hop\": \"172.16.0.0\",\n"
" \"interface\": \"eth0\",\n"
" \"metric\": 0,\n"
" \"active\": [null]\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.1/32\",\n"
" \"next-hop\": \"172.16.0.1\",\n"
" \"interface\": \"eth1\",\n"
" \"metric\": 1\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.2/32\",\n"
" \"next-hop\": \"172.16.0.2\",\n"
" \"interface\": \"eth2\",\n"
" \"metric\": 2,\n"
" \"active\": [null]\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.3/32\",\n"
" \"next-hop\": \"172.16.0.3\",\n"
" \"interface\": \"eth3\",\n"
" \"metric\": 3\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.4/32\",\n"
" \"next-hop\": \"172.16.0.4\",\n"
" \"interface\": \"eth4\",\n"
" \"metric\": 4,\n"
" \"active\": [null]\n"
" },\n"
" {\n"
" \"prefix\": \"10.0.0.5/32\",\n"
" \"next-hop\": \"172.16.0.5\",\n"
" \"interface\": \"eth5\",\n"
" \"metric\": 5\n"
" }\n"
" ]\n"
" }\n"
" }\n"
" ]\n"
" },\n"
" \"c2cont\": {\n"
" \"c2value\": 2868969987\n"
" },\n"
" \"c3value\": 21\n"
" }\n"
"}\n";
static const struct lyd_node *test_oper_get_tree_locked(const char *xpath __attribute__((unused)),
void **lock __attribute__((unused)))
{
++data_tree_lock;
return data_tree;
}
static void test_oper_unlock_tree(const struct lyd_node *tree __attribute__((unused)),
void *lock __attribute__((unused)))
{
data_tree_lock--;
}
static int __rpc_return_ok(struct nb_cb_rpc_args *args)
{
return NB_OK;
}
/* clang-format off */
const struct frr_yang_module_info frr_test_module_info = {
.name = "frr-test-module",
.get_tree_locked = test_oper_get_tree_locked,
.unlock_tree = test_oper_unlock_tree,
.nodes = {
{
.xpath = "/frr-test-module:frr-test-module/vrfs/vrf/ping",
.cbs.rpc = __rpc_return_ok,
},
{
.xpath = NULL,
},
}
};
/* clang-format on */
static const struct frr_yang_module_info *const modules[] = {
&frr_test_module_info,
};
static void vty_do_exit(int isexit)
{
printf("\nend.\n");
lyd_free_all(data_tree);
cmd_terminate();
vty_terminate();
nb_terminate();
yang_terminate();
event_master_free(master);
log_memstats(NULL, true);
if (!isexit)
exit(0);
}
static struct lyd_node *load_data(void)
{
struct ly_in *in = NULL;
struct lyd_node *tree = NULL;
LY_ERR err;
err = ly_in_new_memory(data_json, &in);
if (!err)
err = lyd_parse_data(ly_native_ctx, NULL, in, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_OPERATIONAL, &tree);
ly_in_free(in, 0);
if (err) {
fprintf(stderr, "LYERR: %s\n", getcwd(NULL, 0));
fprintf(stderr, "LYERR: %s\n", ly_last_errmsg());
exit(1);
}
return tree;
}
/* main routine. */
int main(int argc, char **argv)
{
struct event thread;
/* Set umask before anything for security */
umask(0027);
/* master init. */
master = event_master_create(NULL);
// zlog_aux_init("NONE: ", ZLOG_DISABLED);
/* Library inits. */
cmd_init(1);
cmd_hostname_set("test");
vty_init(master, false);
lib_cmd_init();
debug_init();
nb_init(master, modules, array_size(modules), false, false);
/* Create artificial data. */
data_tree = load_data();
/* Read input from .in file. */
vty_stdio(vty_do_exit);
/* Fetch next active thread. */
while (event_fetch(master, &thread))
event_call(&thread);
/* Not reached. */
exit(0);
}
|