summaryrefslogtreecommitdiffstats
path: root/pathd/path_cli.c
blob: e22931c13e1e282257e355918644090f5576af48 (plain)
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2020  NetDEF, Inc.
 */

#include <zebra.h>

#include <float.h>
#include <math.h>
#include <zebra.h>

#include "memory.h"
#include "log.h"
#include "command.h"
#include "mpls.h"
#include "northbound_cli.h"
#include "termtable.h"

#include "pathd/pathd.h"
#include "pathd/path_nb.h"
#include "pathd/path_cli_clippy.c"
#include "pathd/path_ted.h"

#define XPATH_MAXATTRSIZE 64
#define XPATH_MAXKEYSIZE 42
#define XPATH_POLICY_BASELEN 100
#define XPATH_POLICY_MAXLEN (XPATH_POLICY_BASELEN + XPATH_MAXATTRSIZE)
#define XPATH_CANDIDATE_BASELEN (XPATH_POLICY_BASELEN + XPATH_MAXKEYSIZE)
#define XPATH_CANDIDATE_MAXLEN (XPATH_CANDIDATE_BASELEN + XPATH_MAXATTRSIZE)


static int config_write_segment_routing(struct vty *vty);
static int segment_list_has_src_dst(
	struct vty *vty, char *xpath, long index, const char *index_str,
	struct in_addr adj_src_ipv4, struct in_addr adj_dst_ipv4,
	struct in6_addr adj_src_ipv6, struct in6_addr adj_dst_ipv6,
	const char *adj_src_ipv4_str, const char *adj_dst_ipv4_str,
	const char *adj_src_ipv6_str, const char *adj_dst_ipv6_str);
static int segment_list_has_prefix(
	struct vty *vty, char *xpath, long index, const char *index_str,
	const struct prefix_ipv4 *prefix_ipv4, const char *prefix_ipv4_str,
	const struct prefix_ipv6 *prefix_ipv6, const char *prefix_ipv6_str,
	const char *has_algo, long algo, const char *algo_str,
	const char *has_iface_id, long iface_id, const char *iface_id_str);

DEFINE_MTYPE_STATIC(PATHD, PATH_CLI, "Client");

DEFINE_HOOK(pathd_srte_config_write, (struct vty *vty), (vty));

/* Vty node structures. */
static struct cmd_node segment_routing_node = {
	.name = "segment-routing",
	.node = SEGMENT_ROUTING_NODE,
	.parent_node = CONFIG_NODE,
	.prompt = "%s(config-sr)# ",
	.config_write = config_write_segment_routing,
};

static struct cmd_node sr_traffic_eng_node = {
	.name = "sr traffic-eng",
	.node = SR_TRAFFIC_ENG_NODE,
	.parent_node = SEGMENT_ROUTING_NODE,
	.prompt = "%s(config-sr-te)# ",
};

static struct cmd_node srte_segment_list_node = {
	.name = "srte segment-list",
	.node = SR_SEGMENT_LIST_NODE,
	.parent_node = SR_TRAFFIC_ENG_NODE,
	.prompt = "%s(config-sr-te-segment-list)# ",
};

static struct cmd_node srte_policy_node = {
	.name = "srte policy",
	.node = SR_POLICY_NODE,
	.parent_node = SR_TRAFFIC_ENG_NODE,
	.prompt = "%s(config-sr-te-policy)# ",
};

static struct cmd_node srte_candidate_dyn_node = {
	.name = "srte candidate-dyn",
	.node = SR_CANDIDATE_DYN_NODE,
	.parent_node = SR_POLICY_NODE,
	.prompt = "%s(config-sr-te-candidate)# ",
};


/*
 * Show SR-TE info
 */
DEFPY(show_srte_policy,
      show_srte_policy_cmd,
      "show sr-te policy",
      SHOW_STR
      "SR-TE info\n"
      "SR-TE Policy\n")
{
	struct ttable *tt;
	struct srte_policy *policy;
	char *table;

	if (RB_EMPTY(srte_policy_head, &srte_policies)) {
		vty_out(vty, "No SR Policies to display.\n\n");
		return CMD_SUCCESS;
	}

	/* Prepare table. */
	tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
	ttable_add_row(tt, "Endpoint|Color|Name|BSID|Status");
	tt->style.cell.rpad = 2;
	tt->style.corner = '+';
	ttable_restyle(tt);
	ttable_rowseps(tt, 0, BOTTOM, true, '-');

	RB_FOREACH (policy, srte_policy_head, &srte_policies) {
		char endpoint[ENDPOINT_STR_LENGTH];
		char binding_sid[16] = "-";

		ipaddr2str(&policy->endpoint, endpoint, sizeof(endpoint));
		if (policy->binding_sid != MPLS_LABEL_NONE)
			snprintf(binding_sid, sizeof(binding_sid), "%u",
				 policy->binding_sid);

		ttable_add_row(tt, "%s|%u|%s|%s|%s", endpoint, policy->color,
			       policy->name, binding_sid,
			       policy->status == SRTE_POLICY_STATUS_UP
				       ? "Active"
				       : "Inactive");
	}

	/* Dump the generated table. */
	table = ttable_dump(tt, "\n");
	vty_out(vty, "%s\n", table);
	XFREE(MTYPE_TMP, table);

	ttable_del(tt);

	return CMD_SUCCESS;
}


/*
 * Show detailed SR-TE info
 */
DEFPY(show_srte_policy_detail,
      show_srte_policy_detail_cmd,
      "show sr-te policy detail",
      SHOW_STR
      "SR-TE info\n"
      "SR-TE Policy\n"
      "Show a detailed summary\n")
{
	struct srte_policy *policy;

	if (RB_EMPTY(srte_policy_head, &srte_policies)) {
		vty_out(vty, "No SR Policies to display.\n\n");
		return CMD_SUCCESS;
	}

	vty_out(vty, "\n");
	RB_FOREACH (policy, srte_policy_head, &srte_policies) {
		struct srte_candidate *candidate;
		char endpoint[ENDPOINT_STR_LENGTH];
		char binding_sid[16] = "-";
		char *segment_list_info;
		static char undefined_info[] = "(undefined)";
		static char created_by_pce_info[] = "(created by PCE)";


		ipaddr2str(&policy->endpoint, endpoint, sizeof(endpoint));
		if (policy->binding_sid != MPLS_LABEL_NONE)
			snprintf(binding_sid, sizeof(binding_sid), "%u",
				 policy->binding_sid);
		vty_out(vty,
			"Endpoint: %s  Color: %u  Name: %s  BSID: %s  Status: %s\n",
			endpoint, policy->color, policy->name, binding_sid,
			policy->status == SRTE_POLICY_STATUS_UP ? "Active"
								: "Inactive");

		RB_FOREACH (candidate, srte_candidate_head,
			    &policy->candidate_paths) {
			struct srte_segment_list *segment_list;

			segment_list = candidate->lsp->segment_list;
			if (segment_list == NULL)
				segment_list_info = undefined_info;
			else if (segment_list->protocol_origin
				 == SRTE_ORIGIN_PCEP)
				segment_list_info = created_by_pce_info;
			else
				segment_list_info =
					candidate->lsp->segment_list->name;

			vty_out(vty,
				"  %s Preference: %d  Name: %s  Type: %s  Segment-List: %s  Protocol-Origin: %s\n",
				CHECK_FLAG(candidate->flags, F_CANDIDATE_BEST)
					? "*"
					: " ",
				candidate->preference, candidate->name,
				candidate->type == SRTE_CANDIDATE_TYPE_EXPLICIT
					? "explicit"
					: "dynamic",
				segment_list_info,
				srte_origin2str(
					candidate->lsp->protocol_origin));
		}

		vty_out(vty, "\n");
	}

	return CMD_SUCCESS;
}

DEFPY_NOSH(
      segment_routing_list,
      segment_routing_cmd,
      "segment-routing",
      "Configure segment routing\n")
{
	VTY_PUSH_CONTEXT_NULL(SEGMENT_ROUTING_NODE);
	return CMD_SUCCESS;
}

DEFPY_NOSH(
      sr_traffic_eng_list,
      sr_traffic_eng_cmd,
      "traffic-eng",
      "Configure SR traffic engineering\n")
{
	VTY_PUSH_CONTEXT_NULL(SR_TRAFFIC_ENG_NODE);
	return CMD_SUCCESS;
}

/*
 * XPath: /frr-pathd:pathd/srte/segment-list
 */
DEFPY_NOSH(
      srte_segment_list,
      srte_segment_list_cmd,
      "segment-list WORD$name",
      "Segment List\n"
      "Segment List Name\n")
{
	char xpath[XPATH_MAXLEN];
	int ret;

	snprintf(xpath, sizeof(xpath),
		 "/frr-pathd:pathd/srte/segment-list[name='%s']", name);
	nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);

	snprintf(xpath, sizeof(xpath),
		 "/frr-pathd:pathd/srte/segment-list[name='%s']/protocol-origin",
		 name);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, "local");

	snprintf(xpath, sizeof(xpath),
		 "/frr-pathd:pathd/srte/segment-list[name='%s']/originator", name);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, "config");

	ret = nb_cli_apply_changes(vty, NULL);
	if (ret == CMD_SUCCESS) {
		snprintf(xpath, sizeof(xpath),
			 "/frr-pathd:pathd/srte/segment-list[name='%s']", name);
		VTY_PUSH_XPATH(SR_SEGMENT_LIST_NODE, xpath);
	}

	return ret;
}

DEFPY(srte_no_segment_list,
      srte_no_segment_list_cmd,
      "no segment-list WORD$name",
      NO_STR
      "Segment List\n"
      "Segment List Name\n")
{
	char xpath[XPATH_MAXLEN];

	snprintf(xpath, sizeof(xpath),
		 "/frr-pathd:pathd/srte/segment-list[name='%s']", name);
	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}

void cli_show_srte_segment_list(struct vty *vty, const struct lyd_node *dnode,
				bool show_defaults)
{
	vty_out(vty, "  segment-list %s\n",
		yang_dnode_get_string(dnode, "name"));
}

void cli_show_srte_segment_list_end(struct vty *vty,
				    const struct lyd_node *dnode)
{
	vty_out(vty, "  exit\n");
}

static int segment_list_has_src_dst(
	struct vty *vty, char *xpath, long index, const char *index_str,
	struct in_addr adj_src_ipv4, struct in_addr adj_dst_ipv4,
	struct in6_addr adj_src_ipv6, struct in6_addr adj_dst_ipv6,
	const char *adj_src_ipv4_str, const char *adj_dst_ipv4_str,
	const char *adj_src_ipv6_str, const char *adj_dst_ipv6_str)
{
	const char *node_src_id;
	uint32_t ted_sid = MPLS_LABEL_NONE;

	struct ipaddr ip_src = {};
	struct ipaddr ip_dst = {};
	if (adj_src_ipv4_str != NULL) {
		ip_src.ipa_type = IPADDR_V4;
		ip_src.ip._v4_addr = adj_src_ipv4;
		ip_dst.ipa_type = IPADDR_V4;
		ip_dst.ip._v4_addr = adj_dst_ipv4;
	} else if (adj_src_ipv6_str != NULL) {
		ip_src.ipa_type = IPADDR_V6;
		ip_src.ip._v6_addr = adj_src_ipv6;
		ip_dst.ipa_type = IPADDR_V6;
		ip_dst.ip._v6_addr = adj_dst_ipv6;
	} else {
		return CMD_ERR_NO_MATCH;
	}
	ted_sid = path_ted_query_type_f(&ip_src, &ip_dst);
	if (ted_sid == MPLS_LABEL_NONE) {
		zlog_warn(
			"%s: [rcv ted] CLI NOT FOUND Continue query_type_f SRC (%pIA) DST (%pIA)!",
			__func__, &ip_src, &ip_dst);
	}
	/* type */
	snprintf(xpath, XPATH_MAXLEN, "./segment[index='%s']/nai/type",
		 index_str);
	if (adj_src_ipv4_str != NULL) {
		nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
				      "ipv4_adjacency");
		node_src_id = adj_src_ipv4_str;
	} else if (adj_src_ipv6_str != NULL) {
		nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
				      "ipv6_adjacency");
		node_src_id = adj_src_ipv6_str;
	} else {
		/*
		 * This is just to make the compiler happy about
		 * node_src_id not being initialized.  This
		 * should never happen unless we change the cli
		 * function.
		 */
		assert(!"We must have a adj_src_ipv4_str or a adj_src_ipv6_str");
	}

	/* addresses */
	snprintf(xpath, XPATH_MAXLEN, "./segment[index='%s']/nai/local-address",
		 index_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, node_src_id);
	snprintf(xpath, XPATH_MAXLEN,
		 "./segment[index='%s']/nai/remote-address", index_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
			      adj_dst_ipv4_str ? adj_dst_ipv4_str
					       : adj_dst_ipv6_str);
	return CMD_SUCCESS;
}
int segment_list_has_prefix(
	struct vty *vty, char *xpath, long index, const char *index_str,
	const struct prefix_ipv4 *prefix_ipv4, const char *prefix_ipv4_str,
	const struct prefix_ipv6 *prefix_ipv6, const char *prefix_ipv6_str,
	const char *has_algo, long algo, const char *algo_str,
	const char *has_iface_id, long iface_id, const char *iface_id_str)
{
	char buf_prefix[INET6_ADDRSTRLEN];

	uint32_t ted_sid = MPLS_LABEL_NONE;
	struct prefix prefix_cli = {};
	struct ipaddr pre_ipaddr = {};
	/* prefix with algorithm or local interface id */
	/* Type */
	snprintf(xpath, XPATH_MAXLEN, "./segment[index='%s']/nai/type",
		 index_str);
	if (has_iface_id != NULL) {
		if (prefix_ipv4_str != NULL) {
			nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
					      "ipv4_local_iface");
		} else if (prefix_ipv6_str != NULL) {
			nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
					      "ipv6_local_iface");
		} else {
			return CMD_ERR_NO_MATCH;
		}
	} else {
		if (prefix_ipv4_str != NULL) {
			nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
					      "ipv4_algo");
		} else if (prefix_ipv6_str != NULL) {
			nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
					      "ipv6_algo");
		} else {
			return CMD_ERR_NO_MATCH;
		}
	}
	/* Prefix */
	if (prefix_ipv4_str != NULL) {
		if (!str2prefix(prefix_ipv4_str, &prefix_cli)) {
			vty_out(vty, "%% Malformed prefix\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		inet_ntop(AF_INET, &prefix_cli.u.prefix4, buf_prefix,
			  sizeof(buf_prefix));
		pre_ipaddr.ipa_type = IPADDR_V4;
		pre_ipaddr.ip._v4_addr = prefix_cli.u.prefix4;
	} else if (prefix_ipv6_str != NULL) {
		if (!str2prefix(prefix_ipv6_str, &prefix_cli)) {
			vty_out(vty, "%% Malformed prefix\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		inet_ntop(AF_INET6, &prefix_cli.u.prefix6, buf_prefix,
			  sizeof(buf_prefix));
		pre_ipaddr.ipa_type = IPADDR_V6;
		pre_ipaddr.ip._v6_addr = prefix_cli.u.prefix6;
	}
	snprintf(xpath, XPATH_MAXLEN, "./segment[index='%s']/nai/local-address",
		 index_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, buf_prefix);
	snprintf(xpath, XPATH_MAXLEN,
		 "./segment[index='%s']/nai/local-prefix-len", index_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
			      prefix_ipv4_str
				      ? strchr(prefix_ipv4_str, '/') + 1
				      : strchr(prefix_ipv6_str, '/') + 1);
	/* Alg / Iface */
	if (has_algo != NULL) {
		snprintf(xpath, XPATH_MAXLEN,
			 "./segment[index='%s']/nai/algorithm", index_str);
		nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, algo_str);
	} else {
		if (has_iface_id != NULL) {
			snprintf(xpath, XPATH_MAXLEN,
				 "./segment[index='%s']/nai/local-interface",
				 index_str);
			nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
					      iface_id_str);
		}
	}
	if (has_algo != NULL) {
		ted_sid = path_ted_query_type_c(&prefix_cli, algo);
		if (ted_sid == MPLS_LABEL_NONE) {
			zlog_err(
				"%s: [rcv ted] CLI NOT FOUND Continue query_type_c PREFIX (%pIA/%d) ALGO (%ld) sid:(%d)!",
				__func__, &pre_ipaddr, prefix_cli.prefixlen,
				algo, ted_sid);
		}
	}
	if (has_iface_id != NULL) {
		ted_sid = path_ted_query_type_e(&prefix_cli, iface_id);
		if (ted_sid == MPLS_LABEL_NONE) {
			zlog_err(
				"%s: [rcv ted] CLI NOT FOUND Continue query_type_e PREFIX (%pIA/%d) IFACE (%ld) sid:(%d)!",
				__func__, &pre_ipaddr, prefix_cli.prefixlen,
				iface_id, ted_sid);
		}
	}
	return CMD_SUCCESS;
}
/*
 * XPath: /frr-pathd:pathd/srte/segment-list/segment
 */
/* clang-format off */
DEFPY(srte_segment_list_segment, srte_segment_list_segment_cmd,
      "index (0-4294967295)$index <[mpls$has_mpls_label label (16-1048575)$label] "
      "|"
      "[nai$has_nai <"
      "prefix <A.B.C.D/M$prefix_ipv4|X:X::X:X/M$prefix_ipv6>"
      "<algorithm$has_algo (0-1)$algo| iface$has_iface_id (0-4294967295)$iface_id>"
      "| adjacency$has_adj "
      "<A.B.C.D$adj_src_ipv4 A.B.C.D$adj_dst_ipv4|X:X::X:X$adj_src_ipv6 X:X::X:X$adj_dst_ipv6>"
      ">]"
      ">",
      "Index\n"
      "Index Value\n"
      "MPLS or IP Label\n"
      "Label\n"
      "Label Value\n"
      "Segment NAI\n"
      "NAI prefix identifier\n"
      "NAI IPv4 prefix identifier\n"
      "NAI IPv6 prefix identifier\n"
      "IGP Algorithm\n"
      "Algorithm Value SPF or Strict-SPF\n"
      "Interface Id\n"
      "Interface Value\n"
      "ADJ identifier\n"
      "ADJ IPv4 src identifier\n"
      "ADJ IPv4 dst identifier\n"
      "ADJ IPv6 src identifier\n"
      "ADJ IPv6 dst identifier\n")
/* clang-format on */
{
	char xpath[XPATH_MAXLEN];
	int status = CMD_SUCCESS;


	snprintf(xpath, sizeof(xpath), "./segment[index='%s']", index_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);

	if (has_mpls_label != NULL) {
		snprintf(xpath, sizeof(xpath),
			 "./segment[index='%s']/sid-value", index_str);
		nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, label_str);
		return nb_cli_apply_changes(vty, NULL);
	}

	if (has_adj != NULL) {
		status = segment_list_has_src_dst(vty, xpath, index, index_str,
					 adj_src_ipv4, adj_dst_ipv4,
					 adj_src_ipv6, adj_dst_ipv6,
					 adj_src_ipv4_str, adj_dst_ipv4_str,
					 adj_src_ipv6_str, adj_dst_ipv6_str);
		if (status != CMD_SUCCESS)
			return status;
	} else {
		status = segment_list_has_prefix(
			vty, xpath, index, index_str, prefix_ipv4,
			prefix_ipv4_str, prefix_ipv6, prefix_ipv6_str, has_algo,
			algo, algo_str, has_iface_id, iface_id, iface_id_str);
		if (status != CMD_SUCCESS)
			return status;
	}

	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_segment_list_no_segment,
      srte_segment_list_no_segment_cmd,
      "no index (0-4294967295)$index",
      NO_STR
      "Index\n"
      "Index Value\n")
{
	char xpath[XPATH_MAXLEN];

	snprintf(xpath, sizeof(xpath), "./segment[index='%s']", index_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}

void cli_show_srte_segment_list_segment(struct vty *vty,
					const struct lyd_node *dnode,
					bool show_defaults)
{
	vty_out(vty, "   index %s", yang_dnode_get_string(dnode, "index"));
	if (yang_dnode_exists(dnode, "sid-value")) {
		vty_out(vty, " mpls label %s",
			yang_dnode_get_string(dnode, "sid-value"));
	}
	if (yang_dnode_exists(dnode, "nai")) {
		struct ipaddr addr;
		struct ipaddr addr_rmt;

		switch (yang_dnode_get_enum(dnode, "nai/type")) {
		case SRTE_SEGMENT_NAI_TYPE_IPV4_NODE:
		case SRTE_SEGMENT_NAI_TYPE_IPV4_LOCAL_IFACE:
		case SRTE_SEGMENT_NAI_TYPE_IPV4_ALGORITHM:
			yang_dnode_get_ip(&addr, dnode, "nai/local-address");
			vty_out(vty, " nai prefix %pI4", &addr.ipaddr_v4);
			break;
		case SRTE_SEGMENT_NAI_TYPE_IPV6_NODE:
		case SRTE_SEGMENT_NAI_TYPE_IPV6_LOCAL_IFACE:
		case SRTE_SEGMENT_NAI_TYPE_IPV6_ALGORITHM:
			yang_dnode_get_ip(&addr, dnode, "nai/local-address");
			vty_out(vty, " nai prefix %pI6", &addr.ipaddr_v6);
			break;
		case SRTE_SEGMENT_NAI_TYPE_IPV4_ADJACENCY:
			yang_dnode_get_ip(&addr, dnode, "nai/local-address");
			yang_dnode_get_ip(&addr_rmt, dnode,
					  "./nai/remote-address");
			vty_out(vty, " nai adjacency %pI4", &addr.ipaddr_v4);
			vty_out(vty, " %pI4", &addr_rmt.ipaddr_v4);
			break;
		case SRTE_SEGMENT_NAI_TYPE_IPV6_ADJACENCY:
			yang_dnode_get_ip(&addr, dnode, "nai/local-address");
			yang_dnode_get_ip(&addr_rmt, dnode,
					  "./nai/remote-address");
			vty_out(vty, " nai adjacency %pI6", &addr.ipaddr_v6);
			vty_out(vty, " %pI6", &addr_rmt.ipaddr_v6);
			break;
		default:
			break;
		}
		if (yang_dnode_exists(dnode, "nai/local-prefix-len")) {
			vty_out(vty, "/%s",
				yang_dnode_get_string(
					dnode, "./nai/local-prefix-len"));
		}
		if (yang_dnode_exists(dnode, "nai/local-interface")) {
			vty_out(vty, " iface %s",
				yang_dnode_get_string(dnode,
						      "./nai/local-interface"));
		}
		if (yang_dnode_exists(dnode, "nai/algorithm")) {
			vty_out(vty, " algorithm %s",
				yang_dnode_get_string(dnode,
						      "./nai/algorithm"));
		}
	}
	vty_out(vty, "\n");
}

/*
 * XPath: /frr-pathd:pathd/policy
 */
DEFPY_NOSH(
	srte_policy,
	srte_policy_cmd,
	"policy color (0-4294967295)$num endpoint <A.B.C.D|X:X::X:X>$endpoint",
	"Segment Routing Policy\n"
	"SR Policy color\n"
	"SR Policy color value\n"
	"SR Policy endpoint\n"
	"SR Policy endpoint IPv4 address\n"
	"SR Policy endpoint IPv6 address\n")
{
	char xpath[XPATH_POLICY_BASELEN];
	int ret;

	snprintf(xpath, sizeof(xpath),
		 "/frr-pathd:pathd/srte/policy[color='%s'][endpoint='%s']",
		 num_str, endpoint_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);

	ret = nb_cli_apply_changes(vty, NULL);
	if (ret == CMD_SUCCESS)
		VTY_PUSH_XPATH(SR_POLICY_NODE, xpath);

	return ret;
}

DEFPY(srte_no_policy,
      srte_no_policy_cmd,
      "no policy color (0-4294967295)$num endpoint <A.B.C.D|X:X::X:X>$endpoint",
      NO_STR
      "Segment Routing Policy\n"
      "SR Policy color\n"
      "SR Policy color value\n"
      "SR Policy endpoint\n"
      "SR Policy endpoint IPv4 address\n"
      "SR Policy endpoint IPv6 address\n")
{
	char xpath[XPATH_POLICY_BASELEN];

	snprintf(xpath, sizeof(xpath),
		 "/frr-pathd:pathd/srte/policy[color='%s'][endpoint='%s']",
		 num_str, endpoint_str);
	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}

void cli_show_srte_policy(struct vty *vty, const struct lyd_node *dnode,
			  bool show_defaults)
{
	vty_out(vty, "  policy color %s endpoint %s\n",
		yang_dnode_get_string(dnode, "color"),
		yang_dnode_get_string(dnode, "endpoint"));
}

void cli_show_srte_policy_end(struct vty *vty, const struct lyd_node *dnode)
{
	vty_out(vty, "  exit\n");
}

/*
 * XPath: /frr-pathd:pathd/srte/policy/name
 */
DEFPY(srte_policy_name,
      srte_policy_name_cmd,
      "name WORD$name",
      "Segment Routing Policy name\n"
      "SR Policy name value\n")
{
	nb_cli_enqueue_change(vty, "./name", NB_OP_CREATE, name);

	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_policy_no_name,
      srte_policy_no_name_cmd,
      "no name [WORD]",
      NO_STR
      "Segment Routing Policy name\n"
      "SR Policy name value\n")
{
	nb_cli_enqueue_change(vty, "./name", NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}


void cli_show_srte_policy_name(struct vty *vty, const struct lyd_node *dnode,
			       bool show_defaults)
{
	vty_out(vty, "   name %s\n", yang_dnode_get_string(dnode, NULL));
}

/*
 * XPath: /frr-pathd:pathd/srte/policy/binding-sid
 */
DEFPY(srte_policy_binding_sid,
      srte_policy_binding_sid_cmd,
      "binding-sid (16-1048575)$label",
      "Segment Routing Policy Binding-SID\n"
      "SR Policy Binding-SID label\n")
{
	nb_cli_enqueue_change(vty, "./binding-sid", NB_OP_CREATE, label_str);

	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_policy_no_binding_sid,
      srte_policy_no_binding_sid_cmd,
      "no binding-sid [(16-1048575)]",
      NO_STR
      "Segment Routing Policy Binding-SID\n"
      "SR Policy Binding-SID label\n")
{
	nb_cli_enqueue_change(vty, "./binding-sid", NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, NULL);
}

void cli_show_srte_policy_binding_sid(struct vty *vty,
				      const struct lyd_node *dnode,
				      bool show_defaults)
{
	vty_out(vty, "   binding-sid %s\n", yang_dnode_get_string(dnode, NULL));
}

/*
 * XPath: /frr-pathd:pathd/srte/policy/candidate-path
 */
DEFPY(srte_policy_candidate_exp,
      srte_policy_candidate_exp_cmd,
      "candidate-path preference (0-4294967295)$preference name WORD$name \
	 explicit segment-list WORD$list_name",
      "Segment Routing Policy Candidate Path\n"
      "Segment Routing Policy Candidate Path Preference\n"
      "Administrative Preference\n"
      "Segment Routing Policy Candidate Path Name\n"
      "Symbolic Name\n"
      "Explicit Path\n"
      "List of SIDs\n"
      "Name of the Segment List\n")
{
	nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, preference_str);
	nb_cli_enqueue_change(vty, "./name", NB_OP_MODIFY, name);
	nb_cli_enqueue_change(vty, "./protocol-origin", NB_OP_MODIFY, "local");
	nb_cli_enqueue_change(vty, "./originator", NB_OP_MODIFY, "config");
	nb_cli_enqueue_change(vty, "./type", NB_OP_MODIFY, "explicit");
	nb_cli_enqueue_change(vty, "./segment-list-name", NB_OP_MODIFY,
			      list_name);
	return nb_cli_apply_changes(vty, "./candidate-path[preference='%s']",
				    preference_str);
}

DEFPY_NOSH(
	srte_policy_candidate_dyn,
	srte_policy_candidate_dyn_cmd,
	"candidate-path preference (0-4294967295)$preference name WORD$name dynamic",
	"Segment Routing Policy Candidate Path\n"
	"Segment Routing Policy Candidate Path Preference\n"
	"Administrative Preference\n"
	"Segment Routing Policy Candidate Path Name\n"
	"Symbolic Name\n"
	"Dynamic Path\n")
{
	char xpath[XPATH_MAXLEN + XPATH_CANDIDATE_BASELEN];
	int ret;

	snprintf(xpath, sizeof(xpath), "%s/candidate-path[preference='%s']",
		 VTY_CURR_XPATH, preference_str);

	nb_cli_enqueue_change(vty, ".", NB_OP_CREATE, preference_str);
	nb_cli_enqueue_change(vty, "./name", NB_OP_MODIFY, name);
	nb_cli_enqueue_change(vty, "./protocol-origin", NB_OP_MODIFY, "local");
	nb_cli_enqueue_change(vty, "./originator", NB_OP_MODIFY, "config");
	nb_cli_enqueue_change(vty, "./type", NB_OP_MODIFY, "dynamic");
	ret = nb_cli_apply_changes(vty, "./candidate-path[preference='%s']",
				   preference_str);

	if (ret == CMD_SUCCESS)
		VTY_PUSH_XPATH(SR_CANDIDATE_DYN_NODE, xpath);

	return ret;
}

DEFPY(srte_candidate_bandwidth,
      srte_candidate_bandwidth_cmd,
      "bandwidth BANDWIDTH$value [required$required]",
      "Define a bandwidth constraint\n"
      "Bandwidth value\n"
      "Required constraint\n")
{
	nb_cli_enqueue_change(vty, "./constraints/bandwidth/required",
			      NB_OP_MODIFY, required ? "true" : "false");
	nb_cli_enqueue_change(vty, "./constraints/bandwidth/value",
	                      NB_OP_MODIFY, value);
	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_candidate_no_bandwidth,
      srte_candidate_no_bandwidth_cmd,
      "no bandwidth [BANDWIDTH$value] [required$required]",
      NO_STR
      "Remove a bandwidth constraint\n"
      "Bandwidth value\n"
      "Required constraint\n")
{
	nb_cli_enqueue_change(vty, "./constraints/bandwidth", NB_OP_DESTROY,
	                      NULL);
	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_candidate_affinity_filter, srte_candidate_affinity_filter_cmd,
      "affinity <exclude-any|include-any|include-all>$type BITPATTERN$value",
      "Affinity constraint\n"
      "Exclude any matching link\n"
      "Include any matching link\n"
      "Include all matching links\n"
      "Attribute filter bit pattern as an hexadecimal value from 0x00000000 to 0xFFFFFFFF\n")
{
	uint32_t filter;
	char xpath[XPATH_CANDIDATE_MAXLEN];
	char decimal_value[11];

	if (sscanf(value, "0x%x", &filter) != 1) {
		vty_out(vty, "affinity type: fscanf: %s\n",
			safe_strerror(errno));
		return CMD_WARNING_CONFIG_FAILED;
	}
	snprintf(decimal_value, sizeof(decimal_value), "%u", filter);
	snprintf(xpath, sizeof(xpath), "./constraints/affinity/%s", type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, decimal_value);

	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_candidate_no_affinity_filter, srte_candidate_no_affinity_filter_cmd,
      "no affinity <exclude-any|include-any|include-all>$type [BITPATTERN$value]",
      NO_STR
      "Affinity constraint\n"
      "Exclude any matching link\n"
      "Include any matching link\n"
      "Include all matching links\n"
      "Attribute filter bit pattern as an hexadecimal value from 0x00000000 to 0xFFFFFFFF\n")
{
	char xpath[XPATH_CANDIDATE_MAXLEN];

	snprintf(xpath, sizeof(xpath), "./constraints/affinity/%s", type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_candidate_metric,
      srte_candidate_metric_cmd,
      "metric [bound$bound] <igp|te|hc|abc|lmll|cigp|cte|pigp|pte|phc|msd|pd|pdv|pl|ppd|ppdv|ppl|nap|nlp|dc|bnc>$type METRIC$value [required$required] [computed$computed]",
      "Define a metric constraint\n"
      "If the metric is bounded\n"
      "IGP metric\n"
      "TE metric\n"
      "Hop Counts\n"
      "Aggregate bandwidth consumption\n"
      "Load of the most loaded link\n"
      "Cumulative IGP cost\n"
      "Cumulative TE cost\n"
      "P2MP IGP metric\n"
      "P2MP TE metric\n"
      "P2MP hop count metric\n"
      "Segment-ID (SID) Depth.\n"
      "Path Delay metric\n"
      "Path Delay Variation metric\n"
      "Path Loss metric\n"
      "P2MP Path Delay metric\n"
      "P2MP Path Delay variation metric\n"
      "P2MP Path Loss metric\n"
      "Number of adaptations on a path\n"
      "Number of layers on a path\n"
      "Domain Count metric\n"
      "Border Node Count metric\n"
      "Metric value\n"
      "Required constraint\n"
      "Force the PCE to provide the computed path metric\n")
{
	char xpath[XPATH_CANDIDATE_MAXLEN];
	snprintf(xpath, sizeof(xpath), "./constraints/metrics[type='%s']/value",
	         type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, value);
	snprintf(xpath, sizeof(xpath),
	         "./constraints/metrics[type='%s']/is-bound", type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
			      (bound != NULL) ? "true" : "false");
	snprintf(xpath, sizeof(xpath),
	         "./constraints/metrics[type='%s']/required", type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
			      required ? "true" : "false");
	snprintf(xpath, sizeof(xpath),
		 "./constraints/metrics[type='%s']/is-computed", type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
			      computed ? "true" : "false");
	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_candidate_no_metric,
      srte_candidate_no_metric_cmd,
      "no metric [bound] <igp|te|hc|abc|lmll|cigp|cte|pigp|pte|phc|msd|pd|pdv|pl|ppd|ppdv|ppl|nap|nlp|dc|bnc>$type [METRIC$value] [required$required] [computed$computed]",
      NO_STR
      "Remove a metric constraint\n"
      "If the metric is bounded\n"
      "IGP metric\n"
      "TE metric\n"
      "Hop Counts\n"
      "Aggregate bandwidth consumption\n"
      "Load of the most loaded link\n"
      "Cumulative IGP cost\n"
      "Cumulative TE cost\n"
      "P2MP IGP metric\n"
      "P2MP TE metric\n"
      "P2MP hop count metric\n"
      "Segment-ID (SID) Depth.\n"
      "Path Delay metric\n"
      "Path Delay Variation metric\n"
      "Path Loss metric\n"
      "P2MP Path Delay metric\n"
      "P2MP Path Delay variation metric\n"
      "P2MP Path Loss metric\n"
      "Number of adaptations on a path\n"
      "Number of layers on a path\n"
      "Domain Count metric\n"
      "Border Node Count metric\n"
      "Metric value\n"
      "Required constraint\n"
      "Force the PCE to provide the computed path metric\n")
{
	char xpath[XPATH_CANDIDATE_MAXLEN];
	snprintf(xpath, sizeof(xpath), "./constraints/metrics[type='%s']",
	         type);
	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_policy_no_candidate,
      srte_policy_no_candidate_cmd,
      "no candidate-path\
	preference (0-4294967295)$preference\
	[name WORD\
	<\
	  explicit segment-list WORD\
	  |dynamic\
	>]",
      NO_STR
      "Segment Routing Policy Candidate Path\n"
      "Segment Routing Policy Candidate Path Preference\n"
      "Administrative Preference\n"
      "Segment Routing Policy Candidate Path Name\n"
      "Symbolic Name\n"
      "Explicit Path\n"
      "List of SIDs\n"
      "Name of the Segment List\n"
      "Dynamic Path\n")
{
	nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);

	return nb_cli_apply_changes(vty, "./candidate-path[preference='%s']",
				    preference_str);
}

DEFPY(srte_candidate_objfun,
      srte_candidate_objfun_cmd,
      "objective-function <mcp|mlp|mbp|mbc|mll|mcc|spt|mct|mplp|mup|mrup|mtd|mbn|mctd|msl|mss|msn>$type [required$required]",
      "Define an objective function constraint\n"
      "Minimum Cost Path\n"
      "Minimum Load Path\n"
      "Maximum residual Bandwidth Path\n"
      "Minimize aggregate Bandwidth Consumption\n"
      "Minimize the Load of the most loaded Link\n"
      "Minimize the Cumulative Cost of a set of paths\n"
      "Shortest Path Tree\n"
      "Minimum Cost Tree\n"
      "Minimum Packet Loss Path\n"
      "Maximum Under-Utilized Path\n"
      "Maximum Reserved Under-Utilized Path\n"
      "Minimize the number of Transit Domains\n"
      "Minimize the number of Border Nodes\n"
      "Minimize the number of Common Transit Domains\n"
      "Minimize the number of Shared Links\n"
      "Minimize the number of Shared SRLGs\n"
      "Minimize the number of Shared Nodes\n"
      "Required constraint\n")
{
	char xpath[XPATH_CANDIDATE_MAXLEN];
	nb_cli_enqueue_change(vty, "./constraints/objective-function",
	                      NB_OP_DESTROY, NULL);
	snprintf(xpath, sizeof(xpath),
	         "./constraints/objective-function/required");
	nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY,
			      required ? "true" : "false");
	nb_cli_enqueue_change(vty, "./constraints/objective-function/type",
	                      NB_OP_MODIFY, type);
	return nb_cli_apply_changes(vty, NULL);
}

DEFPY(srte_candidate_no_objfun,
      srte_candidate_no_objfun_cmd,
      "no objective-function [<mcp|mlp|mbp|mbc|mll|mcc|spt|mct|mplp|mup|mrup|mtd|mbn|mctd|msl|mss|msn>] [required$required]",
      NO_STR
      "Remove an objective function constraint\n"
      "Minimum Cost Path\n"
      "Minimum Load Path\n"
      "Maximum residual Bandwidth Path\n"
      "Minimize aggregate Bandwidth Consumption\n"
      "Minimize the Load of the most loaded Link\n"
      "Minimize the Cumulative Cost of a set of paths\n"
      "Shortest Path Tree\n"
      "Minimum Cost Tree\n"
      "Minimum Packet Loss Path\n"
      "Maximum Under-Utilized Path\n"
      "Maximum Reserved Under-Utilized Path\n"
      "Minimize the number of Transit Domains\n"
      "Minimize the number of Border Nodes\n"
      "Minimize the number of Common Transit Domains\n"
      "Minimize the number of Shared Links\n"
      "Minimize the number of Shared SRLGs\n"
      "Minimize the number of Shared Nodes\n"
      "Required constraint\n")
{
	nb_cli_enqueue_change(vty, "./constraints/objective-function",
	                      NB_OP_DESTROY, NULL);
	return nb_cli_apply_changes(vty, NULL);
}

static const char *objfun_type_name(enum objfun_type type)
{
	switch (type) {
	case OBJFUN_MCP:
		return "mcp";
	case OBJFUN_MLP:
		return "mlp";
	case OBJFUN_MBP:
		return "mbp";
	case OBJFUN_MBC:
		return "mbc";
	case OBJFUN_MLL:
		return "mll";
	case OBJFUN_MCC:
		return "mcc";
	case OBJFUN_SPT:
		return "spt";
	case OBJFUN_MCT:
		return "mct";
	case OBJFUN_MPLP:
		return "mplp";
	case OBJFUN_MUP:
		return "mup";
	case OBJFUN_MRUP:
		return "mrup";
	case OBJFUN_MTD:
		return "mtd";
	case OBJFUN_MBN:
		return "mbn";
	case OBJFUN_MCTD:
		return "mctd";
	case OBJFUN_MSL:
		return "msl";
	case OBJFUN_MSS:
		return "mss";
	case OBJFUN_MSN:
		return "msn";
	case OBJFUN_UNDEFINED:
		return NULL;
	}

	assert(!"Reached end of function we should never hit");
}

DEFPY_NOSH(show_debugging_pathd, show_debugging_pathd_cmd,
	   "show debugging [pathd]",
	   SHOW_STR
	   "State of each debugging option\n"
	   "pathd module debugging\n")
{

	vty_out(vty, "Path debugging status:\n");

	cmd_show_lib_debugs(vty);
	/* nothing to do here */
	path_ted_show_debugging(vty);
	path_policy_show_debugging(vty);
	return CMD_SUCCESS;
}

DEFPY(debug_path_policy, debug_path_policy_cmd, "[no] debug pathd policy",
      NO_STR DEBUG_STR
      "path debugging\n"
      "policy debugging\n")
{
	uint32_t mode = DEBUG_NODE2MODE(vty->node);
	bool no_debug = no;

	DEBUG_MODE_SET(&path_policy_debug, mode, !no);
	DEBUG_FLAGS_SET(&path_policy_debug, PATH_POLICY_DEBUG_BASIC, !no_debug);
	return CMD_SUCCESS;
}

static const char *metric_type_name(enum srte_candidate_metric_type type)
{
	switch (type) {
	case SRTE_CANDIDATE_METRIC_TYPE_IGP:
		return "igp";
	case SRTE_CANDIDATE_METRIC_TYPE_TE:
		return "te";
	case SRTE_CANDIDATE_METRIC_TYPE_HC:
		return "hc";
	case SRTE_CANDIDATE_METRIC_TYPE_ABC:
		return "abc";
	case SRTE_CANDIDATE_METRIC_TYPE_LMLL:
		return "lmll";
	case SRTE_CANDIDATE_METRIC_TYPE_CIGP:
		return "cigp";
	case SRTE_CANDIDATE_METRIC_TYPE_CTE:
		return "cte";
	case SRTE_CANDIDATE_METRIC_TYPE_PIGP:
		return "pigp";
	case SRTE_CANDIDATE_METRIC_TYPE_PTE:
		return "pte";
	case SRTE_CANDIDATE_METRIC_TYPE_PHC:
		return "phc";
	case SRTE_CANDIDATE_METRIC_TYPE_MSD:
		return "msd";
	case SRTE_CANDIDATE_METRIC_TYPE_PD:
		return "pd";
	case SRTE_CANDIDATE_METRIC_TYPE_PDV:
		return "pdv";
	case SRTE_CANDIDATE_METRIC_TYPE_PL:
		return "pl";
	case SRTE_CANDIDATE_METRIC_TYPE_PPD:
		return "ppd";
	case SRTE_CANDIDATE_METRIC_TYPE_PPDV:
		return "ppdv";
	case SRTE_CANDIDATE_METRIC_TYPE_PPL:
		return "ppl";
	case SRTE_CANDIDATE_METRIC_TYPE_NAP:
		return "nap";
	case SRTE_CANDIDATE_METRIC_TYPE_NLP:
		return "nlp";
	case SRTE_CANDIDATE_METRIC_TYPE_DC:
		return "dc";
	case SRTE_CANDIDATE_METRIC_TYPE_BNC:
		return "bnc";
	default:
		return NULL;
	}
}

static void config_write_float(struct vty *vty, float value)
{
	if (fabs(truncf(value) - value) < FLT_EPSILON) {
		vty_out(vty, " %d", (int)value);
		return;
	} else {
		vty_out(vty, " %f", value);
	}
}

static void config_write_metric(struct vty *vty,
				enum srte_candidate_metric_type type,
				float value, bool required, bool is_bound,
				bool is_computed)
{
	const char *name = metric_type_name(type);
	if (name == NULL)
		return;
	vty_out(vty, "    metric %s%s", is_bound ? "bound " : "",
	        metric_type_name(type));
	config_write_float(vty, value);
	vty_out(vty, required ? " required" : "");
	vty_out(vty, is_computed ? " computed" : "");
	vty_out(vty, "\n");
}

static int config_write_metric_cb(const struct lyd_node *dnode, void *arg)
{
	struct vty *vty = arg;
	enum srte_candidate_metric_type type;
	bool required, is_bound = false, is_computed = false;
	float value;

	type = yang_dnode_get_enum(dnode, "type");
	value = (float)yang_dnode_get_dec64(dnode, "value");
	required = yang_dnode_get_bool(dnode, "required");
	if (yang_dnode_exists(dnode, "is-bound"))
		is_bound = yang_dnode_get_bool(dnode, "is-bound");
	if (yang_dnode_exists(dnode, "is-computed"))
		is_computed = yang_dnode_get_bool(dnode, "is-computed");

	config_write_metric(vty, type, value, required, is_bound, is_computed);
	return YANG_ITER_CONTINUE;
}

void cli_show_srte_policy_candidate_path(struct vty *vty,
					 const struct lyd_node *dnode,
					 bool show_defaults)
{
	float bandwidth;
	uint32_t affinity;
	bool required;
	enum objfun_type objfun_type;
	const char *type = yang_dnode_get_string(dnode, "type");

	vty_out(vty, "   candidate-path preference %s name %s %s",
		yang_dnode_get_string(dnode, "preference"),
		yang_dnode_get_string(dnode, "name"), type);
	if (strmatch(type, "explicit"))
		vty_out(vty, " segment-list %s",
			yang_dnode_get_string(dnode, "segment-list-name"));
	vty_out(vty, "\n");

	if (strmatch(type, "dynamic")) {
		if (yang_dnode_exists(dnode, "constraints/bandwidth")) {
			bandwidth = (float)yang_dnode_get_dec64(
				dnode, "./constraints/bandwidth/value");
			required = yang_dnode_get_bool(
				dnode, "./constraints/bandwidth/required");
			vty_out(vty, "    bandwidth");
			config_write_float(vty, bandwidth);
			if (required)
				vty_out(vty, " required");
			vty_out(vty, "\n");
		}
		if (yang_dnode_exists(dnode,
				      "./constraints/affinity/exclude-any")) {
			affinity = yang_dnode_get_uint32(
				dnode, "./constraints/affinity/exclude-any");
			vty_out(vty, "    affinity exclude-any 0x%08x\n",
				affinity);
		}
		if (yang_dnode_exists(dnode,
				      "./constraints/affinity/include-any")) {
			affinity = yang_dnode_get_uint32(
				dnode, "./constraints/affinity/include-any");
			vty_out(vty, "    affinity include-any 0x%08x\n",
				affinity);
		}
		if (yang_dnode_exists(dnode,
				      "./constraints/affinity/include-all")) {
			affinity = yang_dnode_get_uint32(
				dnode, "./constraints/affinity/include-all");
			vty_out(vty, "    affinity include-all 0x%08x\n",
				affinity);
		}
		yang_dnode_iterate(config_write_metric_cb, vty, dnode,
				   "./constraints/metrics");
		if (yang_dnode_exists(dnode,
		                      "./constraints/objective-function")) {
			objfun_type = yang_dnode_get_enum(dnode,
				"./constraints/objective-function/type");
			required = yang_dnode_get_bool(dnode,
				"./constraints/objective-function/required");
			vty_out(vty, "    objective-function %s%s\n",
			        objfun_type_name(objfun_type),
				required ? " required" : "");
		}
	}
}

void cli_show_srte_policy_candidate_path_end(struct vty *vty,
					     const struct lyd_node *dnode)
{
	const char *type = yang_dnode_get_string(dnode, "type");

	if (strmatch(type, "dynamic"))
		vty_out(vty, "   exit\n");
}

static int config_write_dnode(const struct lyd_node *dnode, void *arg)
{
	struct vty *vty = arg;

	nb_cli_show_dnode_cmds(vty, dnode, false);

	return YANG_ITER_CONTINUE;
}

int config_write_segment_routing(struct vty *vty)
{
	vty_out(vty, "segment-routing\n");
	vty_out(vty, " traffic-eng\n");

	path_ted_config_write(vty);

	yang_dnode_iterate(config_write_dnode, vty, running_config->dnode,
			   "/frr-pathd:pathd/srte/segment-list");
	yang_dnode_iterate(config_write_dnode, vty, running_config->dnode,
			   "/frr-pathd:pathd/srte/policy");

	hook_call(pathd_srte_config_write, vty);

	vty_out(vty, " exit\n");
	vty_out(vty, "exit\n");

	return 1;
}

static int path_policy_cli_debug_config_write(struct vty *vty)
{
	if (DEBUG_MODE_CHECK(&path_policy_debug, DEBUG_MODE_CONF)) {
		if (DEBUG_FLAGS_CHECK(&path_policy_debug,
				      PATH_POLICY_DEBUG_BASIC))
			vty_out(vty, "debug pathd policy\n");
		return 1;
	}
	return 0;
}

static int path_policy_cli_debug_set_all(uint32_t flags, bool set)
{
	DEBUG_FLAGS_SET(&path_policy_debug, flags, set);

	/* If all modes have been turned off, don't preserve options. */
	if (!DEBUG_MODE_CHECK(&path_policy_debug, DEBUG_MODE_ALL))
		DEBUG_CLEAR(&path_policy_debug);

	return 0;
}

void path_cli_init(void)
{
	hook_register(nb_client_debug_config_write,
		      path_policy_cli_debug_config_write);
	hook_register(nb_client_debug_set_all, path_policy_cli_debug_set_all);

	install_node(&segment_routing_node);
	install_node(&sr_traffic_eng_node);
	install_node(&srte_segment_list_node);
	install_node(&srte_policy_node);
	install_node(&srte_candidate_dyn_node);
	install_default(SEGMENT_ROUTING_NODE);
	install_default(SR_TRAFFIC_ENG_NODE);
	install_default(SR_SEGMENT_LIST_NODE);
	install_default(SR_POLICY_NODE);
	install_default(SR_CANDIDATE_DYN_NODE);

	install_element(ENABLE_NODE, &show_debugging_pathd_cmd);
	install_element(ENABLE_NODE, &show_srte_policy_cmd);
	install_element(ENABLE_NODE, &show_srte_policy_detail_cmd);

	install_element(ENABLE_NODE, &debug_path_policy_cmd);
	install_element(CONFIG_NODE, &debug_path_policy_cmd);

	install_element(CONFIG_NODE, &segment_routing_cmd);
	install_element(SEGMENT_ROUTING_NODE, &sr_traffic_eng_cmd);
	install_element(SR_TRAFFIC_ENG_NODE, &srte_segment_list_cmd);
	install_element(SR_TRAFFIC_ENG_NODE, &srte_no_segment_list_cmd);
	install_element(SR_SEGMENT_LIST_NODE,
			&srte_segment_list_segment_cmd);
	install_element(SR_SEGMENT_LIST_NODE,
			&srte_segment_list_no_segment_cmd);
	install_element(SR_TRAFFIC_ENG_NODE, &srte_policy_cmd);
	install_element(SR_TRAFFIC_ENG_NODE, &srte_no_policy_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_name_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_no_name_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_binding_sid_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_no_binding_sid_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_candidate_exp_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_candidate_dyn_cmd);
	install_element(SR_POLICY_NODE, &srte_policy_no_candidate_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_bandwidth_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_no_bandwidth_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_affinity_filter_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_no_affinity_filter_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_metric_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_no_metric_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_objfun_cmd);
	install_element(SR_CANDIDATE_DYN_NODE,
			&srte_candidate_no_objfun_cmd);
}