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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
/*
* Copyright (c) 2018 PANTHEON.tech.
*
* 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 "openconfig_local_routing.h"
#include "sys_util.h"
#include "sc_vpp_operation.h"
#include "../bapi/bapi.h"
#include "../bapi/bapi_interface.h"
#include "../bapi/bapi_ip.h"
#include <assert.h>
#include <string.h>
#include <sysrepo/xpath.h>
#include <sysrepo/values.h>
#define XPATH_SIZE 2000
int openconfig_local_routing_mod_cb(
__attribute__((unused)) sr_session_ctx_t* session,
__attribute__((unused)) const char* module_name,
__attribute__((unused)) sr_notif_event_t event,
__attribute__((unused)) void* private_ctx)
{
SRP_LOG_DBG("Local routing module subscribe: %s", module_name);
return SR_ERR_OK;
}
static int set_route(sr_session_ctx_t *sess, const char *index,
const char *n_interface /*NULLABLE*/,
const char *n_next_hop /*NULLABLE*/,
const char *prefix, bool is_add)
{
int rc = SR_ERR_OK;
sr_val_t *value = NULL;
char xpath[XPATH_SIZE] = {0};
const char *interface = NULL;
const char *next_hop = NULL;
ARG_CHECK3(SR_ERR_INVAL_ARG, sess, index, prefix);
if (NULL == n_interface) {
snprintf(xpath, XPATH_SIZE,
"/openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/next-hops/next-hop[index='%s']/interface-ref/config/interface", prefix, index);
rc = sr_get_item(sess, xpath, &value);
if (SR_ERR_OK != rc) {
SRP_LOG_DBG_MSG("Interface not set");
return 0;
}
interface = value->data.string_val;
} else {
interface = n_interface;
}
if (NULL == n_next_hop) {
snprintf(xpath, XPATH_SIZE,
"/openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/next-hops/next-hop[index='%s']/config/next-hop", prefix, index);
rc = sr_get_item(sess, xpath, &value);
if (SR_ERR_OK != rc) {
SRP_LOG_DBG_MSG("Next-hop not set");
return 0;
}
next_hop = value->data.string_val;
} else {
next_hop = n_next_hop;
}
int mask = ip_prefix_split(prefix);
if (mask < 1) {
return SR_ERR_INVAL_ARG;
}
vapi_payload_ip_add_del_route_reply reply = {0};
vapi_error_e rv = bin_api_ip_add_del_route(&reply, prefix, mask,
next_hop, is_add, 0, interface);
if (VAPI_OK != rv || reply.retval > 0) {
return SR_ERR_INVAL_ARG;
}
return VAPI_OK;
}
// XPATH: /openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/next-hops/next-hop[index='%s']/config/
int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_config_cb(
sr_session_ctx_t *ds, const char *xpath, sr_notif_event_t event,
__attribute__((unused)) void *private_ctx)
{
sr_change_iter_t *it = NULL;
sr_change_oper_t oper;
sr_val_t *old_val = NULL;
sr_val_t *new_val = NULL;
int rc = SR_ERR_OK;
char *tmp = NULL;
char static_prefix[XPATH_SIZE] = {0};
char next_hop_index[XPATH_SIZE] = {0};
char next_hop[XPATH_SIZE] = {0};
char old_next_hop_index[XPATH_SIZE] = {0};
char old_next_hop[XPATH_SIZE] = {0};
bool index_set = false, next_hop_set = false;
bool old_index_set = false, old_next_hop_set = false;
ARG_CHECK2(SR_ERR_INVAL_ARG, ds, xpath);
// if we receive event SR_EV_APPLY - config has changed
log_recv_event(event, "subtree_change_cb received");
SRP_LOG_DBG("[SRP_LOG_DBG] xpath: %s", xpath);
if (event != SR_EV_APPLY) {
return SR_ERR_OK;
}
if (sr_get_changes_iter(ds, (char *)xpath, &it) != SR_ERR_OK) {
// in example he calls even on fale
sr_free_change_iter(it);
return SR_ERR_OK;
}
while (sr_get_change_next(ds, it, &oper,
&old_val, &new_val) == SR_ERR_OK) {
log_recv_oper(oper, "subtree_change_cb received");
SRP_LOG_DBG("xpath: %s", new_val->xpath);
sr_xpath_ctx_t state = {0};
tmp = xpath_find_first_key(new_val->xpath, "prefix", &state);
if (NULL == tmp) {
SRP_LOG_DBG_MSG("static_prefix NOT found.");
continue;
}
strncpy(static_prefix, tmp, XPATH_SIZE);
sr_xpath_recover(&state);
switch (oper) {
case SR_OP_CREATED:
if (sr_xpath_node_name_eq(new_val->xpath, "index")) {
strncpy(next_hop_index, new_val->data.string_val,
XPATH_SIZE);
index_set = true;
} else if(sr_xpath_node_name_eq(new_val->xpath, "next-hop")) {
next_hop_set = true;
strncpy(next_hop, new_val->data.string_val, XPATH_SIZE);
} else if(sr_xpath_node_name_eq(new_val->xpath, "metric")) {
//TODO: LEAF: metric, type: uint32
} else if(sr_xpath_node_name_eq(new_val->xpath, "recurse")) {
//TODO: LEAF: recurse, type: boolean
}
if (index_set && next_hop_set) {
rc = set_route(ds, next_hop_index, NULL, next_hop,
static_prefix, true);
}
break;
case SR_OP_MODIFIED:
if (sr_xpath_node_name_eq(old_val->xpath, "index")) {
strncpy(old_next_hop_index, old_val->data.string_val,
XPATH_SIZE);
old_index_set = true;
} else if(sr_xpath_node_name_eq(old_val->xpath, "next-hop")) {
old_next_hop_set = true;
strncpy(old_next_hop, old_val->data.string_val, XPATH_SIZE);
} else if(sr_xpath_node_name_eq(old_val->xpath, "metric")) {
//TODO: LEAF: metric, type: uint32
} else if(sr_xpath_node_name_eq(old_val->xpath, "recurse")) {
//TODO: LEAF: recurse, type: boolean
}
if (sr_xpath_node_name_eq(new_val->xpath, "index")) {
strncpy(next_hop_index, new_val->data.string_val,
XPATH_SIZE);
index_set = true;
} else if(sr_xpath_node_name_eq(new_val->xpath, "next-hop")) {
next_hop_set = true;
strncpy(next_hop, new_val->data.string_val, XPATH_SIZE);
} else if(sr_xpath_node_name_eq(new_val->xpath, "metric")) {
//TODO: LEAF: metric, type: uint32
} else if(sr_xpath_node_name_eq(new_val->xpath, "recurse")) {
//TODO: LEAF: recurse, type: boolean
}
if (old_index_set && old_next_hop_set) {
rc = set_route(ds, old_next_hop_index, NULL, old_next_hop,
static_prefix, false);
}
if (index_set && next_hop_set) {
rc = set_route(ds, next_hop_index, NULL, next_hop,
static_prefix, true);
}
break;
case SR_OP_MOVED:
break;
case SR_OP_DELETED:
if (sr_xpath_node_name_eq(old_val->xpath, "index")) {
strncpy(old_next_hop_index, old_val->data.string_val,
XPATH_SIZE);
old_index_set = true;
} else if(sr_xpath_node_name_eq(old_val->xpath, "next-hop")) {
old_next_hop_set = true;
strncpy(old_next_hop, old_val->data.string_val, XPATH_SIZE);
} else if(sr_xpath_node_name_eq(old_val->xpath, "metric")) {
//TODO: LEAF: metric, type: uint32
} else if(sr_xpath_node_name_eq(old_val->xpath, "recurse")) {
//TODO: LEAF: recurse, type: boolean
}
if (old_index_set && old_next_hop_set) {
rc = set_route(ds, old_next_hop_index, NULL, old_next_hop,
static_prefix, false);
}
break;
}
if (SR_ERR_OK != rc) {
sr_xpath_recover(&state);
sr_free_val(old_val);
sr_free_val(new_val);
sr_free_change_iter(it);
return SR_ERR_OPERATION_FAILED;
}
sr_xpath_recover(&state);
sr_free_val(old_val);
sr_free_val(new_val);
}
sr_free_change_iter(it);
return SR_ERR_OK;
}
// XPATH: /openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/next-hops/next-hop[index='%s']/interface-ref/config/
int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_interface_ref_config_cb(
sr_session_ctx_t *ds, const char *xpath, sr_notif_event_t event,
__attribute__((unused)) void *private_ctx)
{
sr_change_iter_t *it = NULL;
sr_change_oper_t oper;
sr_val_t *old_val = NULL;
sr_val_t *new_val = NULL;
char *tmp = NULL;
char static_prefix[XPATH_SIZE] = {0};
char next_hop_index[XPATH_SIZE] = {0};
char interface[XPATH_SIZE] = {0};
char old_interface[XPATH_SIZE] = {0};
int rc = SR_ERR_OK;
// if we receive event SR_EV_APPLY - config has changed
log_recv_event(event, "subtree_change_cb received");
SRP_LOG_DBG("[SRP_LOG_DBG] xpath: %s", xpath);
if (event != SR_EV_APPLY) {
return SR_ERR_OK;
}
if (sr_get_changes_iter(ds, (char *)xpath, &it) != SR_ERR_OK) {
// in example he calls even on fale
sr_free_change_iter(it);
return SR_ERR_OK;
}
while (sr_get_change_next(ds, it, &oper,
&old_val, &new_val) == SR_ERR_OK) {
log_recv_oper(oper, "subtree_change_cb received");
SRP_LOG_DBG("xpath: %s", new_val->xpath);
sr_xpath_ctx_t state = {0};
tmp = xpath_find_first_key(new_val->xpath, "prefix", &state);
if (NULL == tmp) {
SRP_LOG_DBG_MSG("static_prefix NOT found.");
continue;
}
strncpy(static_prefix, tmp, XPATH_SIZE);
sr_xpath_recover(&state);
tmp = xpath_find_first_key(new_val->xpath, "index", &state);
if (NULL == tmp) {
SRP_LOG_DBG_MSG("next-hop_index NOT found.");
continue;
}
strncpy(next_hop_index, tmp, XPATH_SIZE);
sr_xpath_recover(&state);
switch (oper) {
case SR_OP_CREATED:
if (sr_xpath_node_name_eq(new_val->xpath, "interface")) {
strncpy(interface, new_val->data.string_val, XPATH_SIZE);
rc = set_route(ds, next_hop_index, interface, NULL,
static_prefix, true);
} else if (sr_xpath_node_name_eq(new_val->xpath,
"subinterface")) {
}
break;
case SR_OP_MODIFIED:
if (sr_xpath_node_name_eq(old_val->xpath, "interface")) {
strncpy(old_interface, old_val->data.string_val,
XPATH_SIZE);
rc = set_route(ds, next_hop_index, old_interface, NULL,
static_prefix, false);
} else if (sr_xpath_node_name_eq(old_val->xpath,
"subinterface")) {
}
if (sr_xpath_node_name_eq(new_val->xpath, "interface")) {
strncpy(interface, new_val->data.string_val, XPATH_SIZE);
rc = set_route(ds, next_hop_index, interface, NULL,
static_prefix, true);
} else if (sr_xpath_node_name_eq(new_val->xpath,
"subinterface")) {
}
break;
case SR_OP_MOVED:
break;
case SR_OP_DELETED:
if (sr_xpath_node_name_eq(old_val->xpath, "interface")) {
strncpy(old_interface, old_val->data.string_val,
XPATH_SIZE);
rc = set_route(ds, next_hop_index, old_interface, NULL,
static_prefix, false);
} else if (sr_xpath_node_name_eq(old_val->xpath,
"subinterface")) {
}
break;
}
if (SR_ERR_OK != rc) {
sr_xpath_recover(&state);
sr_free_val(old_val);
sr_free_val(new_val);
sr_free_change_iter(it);
return SR_ERR_OPERATION_FAILED;
}
sr_xpath_recover(&state);
sr_free_val(old_val);
sr_free_val(new_val);
}
sr_free_change_iter(it);
return SR_ERR_OK;
}
typedef struct
{
u8 length;
u8 address[4];
} address_prefix_t;
typedef struct {
char *next_hop_index;
address_prefix_t address_prefix;
const bool is_interface_ref;
sw_interface_details_query_t sw_interface_details_query;
sysr_values_ctx_t sysr_values_ctx;
} sysr_ip_fib_details_ctx_t;
static
bool address_prefix_cmp(address_prefix_t* address_prefix,
vapi_payload_ip_fib_details* reply)
{
ARG_CHECK2(false, address_prefix, reply);
if (address_prefix->length == reply->address_length)
{
if (0 == memcmp(address_prefix->address, reply->address, sizeof(reply->address)))
return true;
}
return false;
}
static
bool address_prefix_init(address_prefix_t* address_prefix, char* str_prefix)
{
ARG_CHECK2(false, address_prefix, str_prefix);
address_prefix->length = ip_prefix_split(str_prefix);
if(address_prefix->length < 1)
{
SRP_LOG_ERR_MSG("not a valid prefix");
return false;
}
return bapi_aton(str_prefix, address_prefix->address);
}
// XPATH: /openconfig-local-routing:local-routes/static-routes/static/state
int openconfig_local_routing_local_routes_static_routes_static_state_vapi_cb(
vapi_payload_ip_fib_details *reply,
sysr_ip_fib_details_ctx_t *sysr_ip_fib_details_ctx)
{
sr_val_t *vals = NULL;
int rc = 0;
int vc = 0;
char address_prefix[INET_ADDRSTRLEN + 3] = {0};
vc = 1;
ARG_CHECK2(SR_ERR_INVAL_ARG, reply, sysr_ip_fib_details_ctx);
rc = sr_new_values(vc, &vals);
if (SR_ERR_OK != rc) {
return rc;
}
//Filling the structure
snprintf(address_prefix, sizeof(address_prefix), "%s/%u",
bapi_ntoa(reply->address), reply->address_length);
sr_val_build_xpath(&vals[0], "%s/prefix",
sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root);
sr_val_set_str_data(&vals[0], SR_STRING_T, address_prefix);
sysr_ip_fib_details_ctx->sysr_values_ctx.values = vals;
sysr_ip_fib_details_ctx->sysr_values_ctx.values_cnt = vc;
return SR_ERR_OK;
}
vapi_error_e
ip_routing_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
vapi_error_e rv, bool is_last,
vapi_payload_ip_fib_details * reply)
{
if (is_last)
{
assert (NULL == reply);
}
else
{
sysr_ip_fib_details_ctx_t *dctx = callback_ctx;
assert(dctx && reply);
if (address_prefix_cmp(&dctx->address_prefix, reply))
{
openconfig_local_routing_local_routes_static_routes_static_state_vapi_cb(reply,
dctx);
}
}
return VAPI_OK;
}
int openconfig_local_routing_local_routes_static_routes_static_state_cb(
const char *xpath, sr_val_t **values, size_t *values_cnt,
__attribute__((unused)) uint64_t request_id,
__attribute__((unused)) void *private_ctx)
{
sr_xpath_ctx_t state = {0};
vapi_error_e rv = 0;
char *tmp = NULL;
char static_prefix[XPATH_SIZE] = {0};
sysr_ip_fib_details_ctx_t dctx = {0};
ARG_CHECK3(SR_ERR_INVAL_ARG, xpath, values, values_cnt);
tmp = xpath_find_first_key(xpath, "prefix", &state);
if (NULL == tmp) {
SRP_LOG_ERR_MSG("static_prefix not found.");
return SR_ERR_INVAL_ARG;
}
strncpy(static_prefix, tmp, XPATH_SIZE);
sr_xpath_recover(&state);
//VPP callback
snprintf(dctx.sysr_values_ctx.xpath_root, XPATH_SIZE, "/openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/state", static_prefix);
if (!address_prefix_init(&dctx.address_prefix, static_prefix))
{
return SR_ERR_INVAL_ARG;
}
vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx);
VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx, mp, ip_routing_dump_cb, &dctx));
if(VAPI_OK != rv)
{
SRP_LOG_ERR_MSG("VAPI call failed");
return SR_ERR_INVAL_ARG;
}
sr_xpath_recover(&state);
*values = dctx.sysr_values_ctx.values;
*values_cnt = dctx.sysr_values_ctx.values_cnt;
return SR_ERR_OK;
}
// // XPATH: /openconfig-local-routing:local-routes/static-routes/l/next-hops/next-hop/state
int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_state_vapi_cb(
vapi_type_fib_path *reply,
sysr_ip_fib_details_ctx_t *sysr_ip_fib_details_ctx)
{
sr_val_t *vals = NULL;
int rc = 0;
int vc = 0;
char next_hop[INET_ADDRSTRLEN] = {0};
ARG_CHECK2(SR_ERR_INVAL_ARG, reply, sysr_ip_fib_details_ctx);
vc = 3;
/* convenient functions such as this can be found in sysrepo/values.h */
rc = sr_new_values(vc, &vals);
if (SR_ERR_OK != rc) {
return rc;
}
sr_val_build_xpath(&vals[0], "%s/index",
sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root);
sr_val_set_str_data(&vals[0], SR_STRING_T,
sysr_ip_fib_details_ctx->next_hop_index);
strncpy(next_hop, bapi_ntoa(reply->next_hop), sizeof(next_hop));
sr_val_build_xpath(&vals[1], "%s/next-hop", sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root);
sr_val_set_str_data(&vals[1], SR_STRING_T, next_hop);
sr_val_build_xpath(&vals[2], "%s/metric", sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root);
vals[2].type = SR_UINT32_T;
vals[2].data.uint32_val = reply->weight;
// sr_val_build_xpath(&vals[3], "%s/recurse", sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root);
// vals[3].type = SR_BOOL_T;
// vals[3].data.bool_val = YANG INPUT TYPE: boolean;
sysr_ip_fib_details_ctx->sysr_values_ctx.values = vals;
sysr_ip_fib_details_ctx->sysr_values_ctx.values_cnt = vc;
return SR_ERR_OK;
}
// // XPATH: /openconfig-local-routing:local-routes/static-routes/l/next-hops/next-hop/interface-ref/state
int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_interface_ref_state_vapi_cb(
sysr_ip_fib_details_ctx_t * dctx)
{
sr_val_t *vals = NULL;
int rc = 0;
int vc = 0;
ARG_CHECK(SR_ERR_INVAL_ARG, dctx);
vc = 2;
/* convenient functions such as this can be found in sysrepo/values.h */
rc = sr_new_values(vc, &vals);
if (SR_ERR_OK != rc) {
return rc;
}
sr_val_build_xpath(&vals[0], "%s/interface",
dctx->sysr_values_ctx.xpath_root);
sr_val_set_str_data(&vals[0], SR_STRING_T,
(const char*)dctx->sw_interface_details_query.sw_interface_details.interface_name);
sr_val_build_xpath(&vals[1], "%s/subinterface",
dctx->sysr_values_ctx.xpath_root);
vals[1].type = SR_UINT32_T;
vals[1].data.uint32_val = 0;
dctx->sysr_values_ctx.values = vals;
dctx->sysr_values_ctx.values_cnt = vc;
return SR_ERR_OK;
}
vapi_error_e
ip_routing_next_hop_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
vapi_error_e rv, bool is_last,
vapi_payload_ip_fib_details * reply)
{
ARG_CHECK(VAPI_EINVAL, callback_ctx);
sysr_ip_fib_details_ctx_t *dctx = callback_ctx;
if (is_last)
{
assert (NULL == reply);
}
else
{
assert (NULL != reply);
if (reply->count > 0 && address_prefix_cmp(&dctx->address_prefix, reply))
{
if (dctx->is_interface_ref)
{
dctx->sw_interface_details_query.interface_found = true;
dctx->sw_interface_details_query.sw_interface_details.sw_if_index = reply->path[0].sw_if_index;
//sw_interface_dump will have to be called outside this VAPI
}
else
{
openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_state_vapi_cb(reply->path, dctx);
}
}
}
return VAPI_OK;
}
int next_hop_inner(
bool is_interface_ref, const char *xpath, sr_val_t **values,
size_t *values_cnt, __attribute__((unused)) uint64_t request_id,
__attribute__((unused)) void *private_ctx)
{
sr_xpath_ctx_t state = {0};
vapi_error_e rv;
char *tmp = NULL;
char static_prefix[XPATH_SIZE] = {0};
char next_hop_index[XPATH_SIZE] = {0};
sysr_ip_fib_details_ctx_t dctx = {.is_interface_ref = is_interface_ref};
ARG_CHECK3(SR_ERR_INVAL_ARG, xpath, values, values_cnt);
tmp = xpath_find_first_key(xpath, "prefix", &state);
if (NULL == tmp) {
SRP_LOG_ERR_MSG("static_prefix not found.");
return SR_ERR_INVAL_ARG;
}
strncpy(static_prefix, tmp, XPATH_SIZE);
sr_xpath_recover(&state);
tmp = xpath_find_first_key(xpath, "index", &state);
if (NULL == tmp) {
SRP_LOG_ERR_MSG("index not found.");
return SR_ERR_INVAL_ARG;
}
strncpy(next_hop_index, tmp, XPATH_SIZE);
sr_xpath_recover(&state);
//VPP callback
dctx.next_hop_index = next_hop_index;
if (is_interface_ref) {
snprintf(dctx.sysr_values_ctx.xpath_root, XPATH_SIZE,
"/openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/next-hops/next-hop[index='%s']/interface-ref/state",
static_prefix, next_hop_index);
} else {
snprintf(dctx.sysr_values_ctx.xpath_root, XPATH_SIZE,
"/openconfig-local-routing:local-routes/static-routes/static[prefix='%s']/next-hops/next-hop[index='%s']/state",
static_prefix, next_hop_index);
}
if (!address_prefix_init(&dctx.address_prefix, static_prefix))
{
return SR_ERR_INVAL_ARG;
}
vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx);
VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx, mp, ip_routing_next_hop_dump_cb, &dctx));
if (VAPI_OK != rv)
{
SRP_LOG_ERR_MSG("VAPI call failed");
return SR_ERR_INVAL_ARG;
}
if (is_interface_ref)
{
if (dctx.sw_interface_details_query.interface_found)
{
if (!get_interface_name(&dctx.sw_interface_details_query))
{
return SR_ERR_INVAL_ARG;
}
if (strlen((const char*)
dctx.sw_interface_details_query.sw_interface_details.interface_name)) {
openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_interface_ref_state_vapi_cb(&dctx);
}
}
}
sr_xpath_recover(&state);
*values = dctx.sysr_values_ctx.values;
*values_cnt = dctx.sysr_values_ctx.values_cnt;
return SR_ERR_OK;
}
int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_state_cb(
const char *xpath, sr_val_t **values, size_t *values_cnt,
uint64_t request_id, void *private_ctx)
{
return next_hop_inner(false, xpath, values, values_cnt, request_id,
private_ctx);
}
int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_interface_ref_state_cb(
const char *xpath, sr_val_t **values, size_t *values_cnt,
uint64_t request_id, void *private_ctx)
{
return next_hop_inner(true, xpath, values, values_cnt, request_id,
private_ctx);
}
|