]>
Commit | Line | Data |
---|---|---|
1 | # SPDX-License-Identifier: ISC | |
2 | # | |
3 | # Copyright (c) 2020 by VMware, Inc. ("VMware") | |
4 | # Used Copyright (c) 2018 by Network Device Education Foundation, Inc. | |
5 | # ("NetDEF") in this file. | |
6 | # | |
7 | ||
8 | import ipaddress | |
9 | import sys | |
10 | from copy import deepcopy | |
11 | from time import sleep | |
12 | ||
13 | # Import common_config to use commomnly used APIs | |
14 | from lib.common_config import ( | |
15 | create_common_configurations, | |
16 | InvalidCLIError, | |
17 | generate_ips, | |
18 | retry, | |
19 | run_frr_cmd, | |
20 | validate_ip_address, | |
21 | ) | |
22 | from lib.topolog import logger | |
23 | from lib.topotest import frr_unicode | |
24 | ||
25 | ################################ | |
26 | # Configure procs | |
27 | ################################ | |
28 | ||
29 | ||
30 | def create_router_ospf(tgen, topo=None, input_dict=None, build=False, load_config=True): | |
31 | """ | |
32 | API to configure ospf on router. | |
33 | ||
34 | Parameters | |
35 | ---------- | |
36 | * `tgen` : Topogen object | |
37 | * `topo` : json file data | |
38 | * `input_dict` : Input dict data, required when configuring from testcase | |
39 | * `build` : Only for initial setup phase this is set as True. | |
40 | * `load_config` : Loading the config to router this is set as True. | |
41 | ||
42 | Usage | |
43 | ----- | |
44 | input_dict = { | |
45 | "r1": { | |
46 | "ospf": { | |
47 | "router_id": "22.22.22.22", | |
48 | "area": [{ "id": "0.0.0.0", "type": "nssa"}] | |
49 | } | |
50 | } | |
51 | ||
52 | result = create_router_ospf(tgen, topo, input_dict) | |
53 | ||
54 | Returns | |
55 | ------- | |
56 | True or False | |
57 | """ | |
58 | logger.debug("Entering lib API: create_router_ospf()") | |
59 | result = False | |
60 | ||
61 | if topo is None: | |
62 | topo = tgen.json_topo | |
63 | ||
64 | if not input_dict: | |
65 | input_dict = deepcopy(topo) | |
66 | else: | |
67 | topo = topo["routers"] | |
68 | input_dict = deepcopy(input_dict) | |
69 | ||
70 | for ospf in ["ospf", "ospf6"]: | |
71 | config_data_dict = {} | |
72 | ||
73 | for router in input_dict.keys(): | |
74 | if ospf not in input_dict[router]: | |
75 | logger.debug("Router %s: %s not present in input_dict", router, ospf) | |
76 | continue | |
77 | ||
78 | config_data = __create_ospf_global( | |
79 | tgen, input_dict, router, build, load_config, ospf | |
80 | ) | |
81 | if config_data: | |
82 | if router not in config_data_dict: | |
83 | config_data_dict[router] = config_data | |
84 | else: | |
85 | config_data_dict[router].extend(config_data) | |
86 | try: | |
87 | result = create_common_configurations( | |
88 | tgen, config_data_dict, ospf, build, load_config | |
89 | ) | |
90 | except InvalidCLIError: | |
91 | logger.error("create_router_ospf (ipv4)", exc_info=True) | |
92 | result = False | |
93 | ||
94 | logger.debug("Exiting lib API: create_router_ospf()") | |
95 | return result | |
96 | ||
97 | ||
98 | def __create_ospf_global(tgen, input_dict, router, build, load_config, ospf): | |
99 | """ | |
100 | Helper API to create ospf global configuration. | |
101 | ||
102 | Parameters | |
103 | ---------- | |
104 | * `tgen` : Topogen object | |
105 | * `input_dict` : Input dict data, required when configuring from testcase | |
106 | * `router` : router to be configured. | |
107 | * `build` : Only for initial setup phase this is set as True. | |
108 | * `load_config` : Loading the config to router this is set as True. | |
109 | * `ospf` : either 'ospf' or 'ospf6' | |
110 | ||
111 | Usage | |
112 | ----- | |
113 | input_dict = { | |
114 | "routers": { | |
115 | "r1": { | |
116 | "links": { | |
117 | "r3": { | |
118 | "ipv6": "2013:13::1/64", | |
119 | "ospf6": { | |
120 | "hello_interval": 1, | |
121 | "dead_interval": 4, | |
122 | "network": "point-to-point" | |
123 | } | |
124 | } | |
125 | }, | |
126 | "ospf6": { | |
127 | "router_id": "1.1.1.1", | |
128 | "neighbors": { | |
129 | "r3": { | |
130 | "area": "1.1.1.1" | |
131 | } | |
132 | } | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | Returns | |
138 | ------- | |
139 | list of configuration commands | |
140 | """ | |
141 | ||
142 | config_data = [] | |
143 | ||
144 | if ospf not in input_dict[router]: | |
145 | return config_data | |
146 | ||
147 | logger.debug("Entering lib API: __create_ospf_global()") | |
148 | ||
149 | ospf_data = input_dict[router][ospf] | |
150 | del_ospf_action = ospf_data.setdefault("delete", False) | |
151 | if del_ospf_action: | |
152 | config_data = ["no router {}".format(ospf)] | |
153 | return config_data | |
154 | ||
155 | cmd = "router {}".format(ospf) | |
156 | ||
157 | config_data.append(cmd) | |
158 | ||
159 | # router id | |
160 | router_id = ospf_data.setdefault("router_id", None) | |
161 | del_router_id = ospf_data.setdefault("del_router_id", False) | |
162 | if del_router_id: | |
163 | config_data.append("no {} router-id".format(ospf)) | |
164 | if router_id: | |
165 | config_data.append("{} router-id {}".format(ospf, router_id)) | |
166 | ||
167 | # log-adjacency-changes | |
168 | log_adj_changes = ospf_data.setdefault("log_adj_changes", None) | |
169 | del_log_adj_changes = ospf_data.setdefault("del_log_adj_changes", False) | |
170 | if del_log_adj_changes: | |
171 | config_data.append("no log-adjacency-changes detail") | |
172 | if log_adj_changes: | |
173 | config_data.append("log-adjacency-changes {}".format(log_adj_changes)) | |
174 | ||
175 | # aggregation timer | |
176 | aggr_timer = ospf_data.setdefault("aggr_timer", None) | |
177 | del_aggr_timer = ospf_data.setdefault("del_aggr_timer", False) | |
178 | if del_aggr_timer: | |
179 | config_data.append("no aggregation timer") | |
180 | if aggr_timer: | |
181 | config_data.append("aggregation timer {}".format(aggr_timer)) | |
182 | ||
183 | # maximum path information | |
184 | ecmp_data = ospf_data.setdefault("maximum-paths", {}) | |
185 | if ecmp_data: | |
186 | cmd = "maximum-paths {}".format(ecmp_data) | |
187 | del_action = ospf_data.setdefault("del_max_path", False) | |
188 | if del_action: | |
189 | cmd = "no maximum-paths" | |
190 | config_data.append(cmd) | |
191 | ||
192 | # Flood reduction. | |
193 | flood_data = ospf_data.setdefault("flood-reduction", {}) | |
194 | if flood_data: | |
195 | cmd = "flood-reduction" | |
196 | del_action = ospf_data.setdefault("del_flood_reduction", False) | |
197 | if del_action: | |
198 | cmd = "no flood-reduction" | |
199 | config_data.append(cmd) | |
200 | ||
201 | # LSA refresh timer - A hidden command. | |
202 | refresh_data = ospf_data.setdefault("lsa-refresh", {}) | |
203 | if refresh_data: | |
204 | cmd = "ospf lsa-refresh {}".format(refresh_data) | |
205 | del_action = ospf_data.setdefault("del_lsa_refresh", False) | |
206 | if del_action: | |
207 | cmd = "no ospf lsa-refresh" | |
208 | config_data.append(cmd) | |
209 | ||
210 | # redistribute command | |
211 | redistribute_data = ospf_data.setdefault("redistribute", {}) | |
212 | if redistribute_data: | |
213 | for redistribute in redistribute_data: | |
214 | if "redist_type" not in redistribute: | |
215 | logger.debug( | |
216 | "Router %s: 'redist_type' not present in " "input_dict", router | |
217 | ) | |
218 | else: | |
219 | cmd = "redistribute {}".format(redistribute["redist_type"]) | |
220 | for red_type in redistribute_data: | |
221 | if "route_map" in red_type: | |
222 | cmd = cmd + " route-map {}".format(red_type["route_map"]) | |
223 | del_action = redistribute.setdefault("delete", False) | |
224 | if del_action: | |
225 | cmd = "no {}".format(cmd) | |
226 | config_data.append(cmd) | |
227 | ||
228 | # area information | |
229 | area_data = ospf_data.setdefault("area", {}) | |
230 | if area_data: | |
231 | for area in area_data: | |
232 | if "id" not in area: | |
233 | logger.debug( | |
234 | "Router %s: 'area id' not present in " "input_dict", router | |
235 | ) | |
236 | else: | |
237 | cmd = "area {}".format(area["id"]) | |
238 | ||
239 | if "type" in area: | |
240 | cmd = cmd + " {}".format(area["type"]) | |
241 | ||
242 | if "flood-reduction" in area: | |
243 | cmd = cmd + " flood-reduction" | |
244 | ||
245 | del_action = area.setdefault("delete", False) | |
246 | if del_action: | |
247 | cmd = "no {}".format(cmd) | |
248 | config_data.append(cmd) | |
249 | ||
250 | # def route information | |
251 | def_rte_data = ospf_data.setdefault("default-information", {}) | |
252 | if def_rte_data: | |
253 | if "originate" not in def_rte_data: | |
254 | logger.debug( | |
255 | "Router %s: 'originate key' not present in " "input_dict", router | |
256 | ) | |
257 | else: | |
258 | cmd = "default-information originate" | |
259 | ||
260 | if "always" in def_rte_data: | |
261 | cmd = cmd + " always" | |
262 | ||
263 | if "metric" in def_rte_data: | |
264 | cmd = cmd + " metric {}".format(def_rte_data["metric"]) | |
265 | ||
266 | if "metric-type" in def_rte_data: | |
267 | cmd = cmd + " metric-type {}".format(def_rte_data["metric-type"]) | |
268 | ||
269 | if "route-map" in def_rte_data: | |
270 | cmd = cmd + " route-map {}".format(def_rte_data["route-map"]) | |
271 | ||
272 | del_action = def_rte_data.setdefault("delete", False) | |
273 | if del_action: | |
274 | cmd = "no {}".format(cmd) | |
275 | config_data.append(cmd) | |
276 | ||
277 | # summary information | |
278 | summary_data = ospf_data.setdefault("summary-address", {}) | |
279 | if summary_data: | |
280 | for summary in summary_data: | |
281 | if "prefix" not in summary: | |
282 | logger.debug( | |
283 | "Router %s: 'summary-address' not present in " "input_dict", | |
284 | router, | |
285 | ) | |
286 | else: | |
287 | cmd = "summary {}/{}".format(summary["prefix"], summary["mask"]) | |
288 | ||
289 | _tag = summary.setdefault("tag", None) | |
290 | if _tag: | |
291 | cmd = "{} tag {}".format(cmd, _tag) | |
292 | ||
293 | _advertise = summary.setdefault("advertise", True) | |
294 | if not _advertise: | |
295 | cmd = "{} no-advertise".format(cmd) | |
296 | ||
297 | del_action = summary.setdefault("delete", False) | |
298 | if del_action: | |
299 | cmd = "no {}".format(cmd) | |
300 | config_data.append(cmd) | |
301 | ||
302 | # ospf gr information | |
303 | gr_data = ospf_data.setdefault("graceful-restart", {}) | |
304 | if gr_data: | |
305 | ||
306 | if "opaque" in gr_data and gr_data["opaque"]: | |
307 | cmd = "capability opaque" | |
308 | if gr_data.setdefault("delete", False): | |
309 | cmd = "no {}".format(cmd) | |
310 | config_data.append(cmd) | |
311 | ||
312 | if "helper enable" in gr_data and not gr_data["helper enable"]: | |
313 | cmd = "graceful-restart helper enable" | |
314 | if gr_data.setdefault("delete", False): | |
315 | cmd = "no {}".format(cmd) | |
316 | config_data.append(cmd) | |
317 | elif "helper enable" in gr_data and type(gr_data["helper enable"]) is list: | |
318 | for rtrs in gr_data["helper enable"]: | |
319 | cmd = "graceful-restart helper enable {}".format(rtrs) | |
320 | if gr_data.setdefault("delete", False): | |
321 | cmd = "no {}".format(cmd) | |
322 | config_data.append(cmd) | |
323 | ||
324 | if "helper" in gr_data: | |
325 | if type(gr_data["helper"]) is not list: | |
326 | gr_data["helper"] = list(gr_data["helper"]) | |
327 | for helper_role in gr_data["helper"]: | |
328 | cmd = "graceful-restart helper {}".format(helper_role) | |
329 | if gr_data.setdefault("delete", False): | |
330 | cmd = "no {}".format(cmd) | |
331 | config_data.append(cmd) | |
332 | ||
333 | if "supported-grace-time" in gr_data: | |
334 | cmd = "graceful-restart helper supported-grace-time {}".format( | |
335 | gr_data["supported-grace-time"] | |
336 | ) | |
337 | if gr_data.setdefault("delete", False): | |
338 | cmd = "no {}".format(cmd) | |
339 | config_data.append(cmd) | |
340 | ||
341 | logger.debug("Exiting lib API: create_ospf_global()") | |
342 | ||
343 | return config_data | |
344 | ||
345 | ||
346 | def config_ospf_interface( | |
347 | tgen, topo=None, input_dict=None, build=False, load_config=True | |
348 | ): | |
349 | """ | |
350 | API to configure ospf on router. | |
351 | ||
352 | Parameters | |
353 | ---------- | |
354 | * `tgen` : Topogen object | |
355 | * `topo` : json file data | |
356 | * `input_dict` : Input dict data, required when configuring from testcase | |
357 | * `build` : Only for initial setup phase this is set as True. | |
358 | * `load_config` : Loading the config to router this is set as True. | |
359 | ||
360 | Usage | |
361 | ----- | |
362 | r1_ospf_auth = { | |
363 | "r1": { | |
364 | "links": { | |
365 | "r2": { | |
366 | "ospf": { | |
367 | "authentication": "message-digest", | |
368 | "authentication-key": "ospf", | |
369 | "message-digest-key": "10" | |
370 | } | |
371 | } | |
372 | } | |
373 | } | |
374 | } | |
375 | result = config_ospf_interface(tgen, topo, r1_ospf_auth) | |
376 | ||
377 | Returns | |
378 | ------- | |
379 | True or False | |
380 | """ | |
381 | logger.debug("Enter lib config_ospf_interface") | |
382 | result = False | |
383 | ||
384 | if topo is None: | |
385 | topo = tgen.json_topo | |
386 | ||
387 | if not input_dict: | |
388 | input_dict = deepcopy(topo) | |
389 | else: | |
390 | input_dict = deepcopy(input_dict) | |
391 | ||
392 | config_data_dict = {} | |
393 | ||
394 | for router in input_dict.keys(): | |
395 | config_data = [] | |
396 | for lnk in input_dict[router]["links"].keys(): | |
397 | if "ospf" not in input_dict[router]["links"][lnk]: | |
398 | logger.debug( | |
399 | "Router %s: ospf config is not present in" "input_dict", router | |
400 | ) | |
401 | continue | |
402 | ospf_data = input_dict[router]["links"][lnk]["ospf"] | |
403 | data_ospf_area = ospf_data.setdefault("area", None) | |
404 | data_ospf_auth = ospf_data.setdefault("authentication", None) | |
405 | data_ospf_dr_priority = ospf_data.setdefault("priority", None) | |
406 | data_ospf_cost = ospf_data.setdefault("cost", None) | |
407 | data_ospf_mtu = ospf_data.setdefault("mtu_ignore", None) | |
408 | ||
409 | try: | |
410 | intf = topo["routers"][router]["links"][lnk]["interface"] | |
411 | except KeyError: | |
412 | intf = topo["switches"][router]["links"][lnk]["interface"] | |
413 | ||
414 | # interface | |
415 | cmd = "interface {}".format(intf) | |
416 | ||
417 | config_data.append(cmd) | |
418 | # interface area config | |
419 | if data_ospf_area: | |
420 | cmd = "ip ospf area {}".format(data_ospf_area) | |
421 | config_data.append(cmd) | |
422 | ||
423 | # interface ospf auth | |
424 | if data_ospf_auth: | |
425 | if data_ospf_auth == "null": | |
426 | cmd = "ip ospf authentication null" | |
427 | elif data_ospf_auth == "message-digest": | |
428 | cmd = "ip ospf authentication message-digest" | |
429 | else: | |
430 | cmd = "ip ospf authentication" | |
431 | ||
432 | if "del_action" in ospf_data: | |
433 | cmd = "no {}".format(cmd) | |
434 | config_data.append(cmd) | |
435 | ||
436 | if "message-digest-key" in ospf_data: | |
437 | cmd = "ip ospf message-digest-key {} md5 {}".format( | |
438 | ospf_data["message-digest-key"], ospf_data["authentication-key"] | |
439 | ) | |
440 | if "del_action" in ospf_data: | |
441 | cmd = "no {}".format(cmd) | |
442 | config_data.append(cmd) | |
443 | ||
444 | if ( | |
445 | "authentication-key" in ospf_data | |
446 | and "message-digest-key" not in ospf_data | |
447 | ): | |
448 | cmd = "ip ospf authentication-key {}".format( | |
449 | ospf_data["authentication-key"] | |
450 | ) | |
451 | if "del_action" in ospf_data: | |
452 | cmd = "no {}".format(cmd) | |
453 | config_data.append(cmd) | |
454 | ||
455 | # interface ospf dr priority | |
456 | if data_ospf_dr_priority: | |
457 | cmd = "ip ospf priority {}".format(ospf_data["priority"]) | |
458 | if "del_action" in ospf_data: | |
459 | cmd = "no {}".format(cmd) | |
460 | config_data.append(cmd) | |
461 | ||
462 | # interface ospf cost | |
463 | if data_ospf_cost: | |
464 | cmd = "ip ospf cost {}".format(ospf_data["cost"]) | |
465 | if "del_action" in ospf_data: | |
466 | cmd = "no {}".format(cmd) | |
467 | config_data.append(cmd) | |
468 | ||
469 | # interface ospf mtu | |
470 | if data_ospf_mtu: | |
471 | cmd = "ip ospf mtu-ignore" | |
472 | if "del_action" in ospf_data: | |
473 | cmd = "no {}".format(cmd) | |
474 | config_data.append(cmd) | |
475 | ||
476 | if build: | |
477 | return config_data | |
478 | ||
479 | if config_data: | |
480 | config_data_dict[router] = config_data | |
481 | ||
482 | result = create_common_configurations( | |
483 | tgen, config_data_dict, "interface_config", build=build | |
484 | ) | |
485 | ||
486 | logger.debug("Exiting lib API: config_ospf_interface()") | |
487 | return result | |
488 | ||
489 | ||
490 | def clear_ospf(tgen, router, ospf=None): | |
491 | """ | |
492 | This API is to clear ospf neighborship by running | |
493 | clear ip ospf interface * command, | |
494 | ||
495 | Parameters | |
496 | ---------- | |
497 | * `tgen`: topogen object | |
498 | * `router`: device under test | |
499 | ||
500 | Usage | |
501 | ----- | |
502 | clear_ospf(tgen, "r1") | |
503 | """ | |
504 | ||
505 | logger.debug("Entering lib API: clear_ospf()") | |
506 | if router not in tgen.routers(): | |
507 | return False | |
508 | ||
509 | rnode = tgen.routers()[router] | |
510 | # Clearing OSPF | |
511 | if ospf: | |
512 | version = "ipv6" | |
513 | else: | |
514 | version = "ip" | |
515 | ||
516 | cmd = "clear {} ospf interface".format(version) | |
517 | logger.info("Clearing ospf process on router %s.. using command '%s'", router, cmd) | |
518 | run_frr_cmd(rnode, cmd) | |
519 | ||
520 | logger.debug("Exiting lib API: clear_ospf()") | |
521 | ||
522 | ||
523 | def redistribute_ospf(tgen, topo, dut, route_type, **kwargs): | |
524 | """ | |
525 | Redstribution of routes inside ospf. | |
526 | ||
527 | Parameters | |
528 | ---------- | |
529 | * `tgen`: Topogen object | |
530 | * `topo` : json file data | |
531 | * `dut`: device under test | |
532 | * `route_type`: "static" or "connected" or .... | |
533 | * `kwargs`: pass extra information (see below) | |
534 | ||
535 | Usage | |
536 | ----- | |
537 | redistribute_ospf(tgen, topo, "r0", "static", delete=True) | |
538 | redistribute_ospf(tgen, topo, "r0", "static", route_map="rmap_ipv4") | |
539 | """ | |
540 | ||
541 | ospf_red = {dut: {"ospf": {"redistribute": [{"redist_type": route_type}]}}} | |
542 | for k, v in kwargs.items(): | |
543 | ospf_red[dut]["ospf"]["redistribute"][0][k] = v | |
544 | ||
545 | result = create_router_ospf(tgen, topo, ospf_red) | |
546 | assert result is True, "Testcase : Failed \n Error: {}".format(result) | |
547 | ||
548 | ||
549 | ################################ | |
550 | # Verification procs | |
551 | ################################ | |
552 | @retry(retry_timeout=80) | |
553 | def verify_ospf_neighbor( | |
554 | tgen, topo=None, dut=None, input_dict=None, lan=False, expected=True | |
555 | ): | |
556 | """ | |
557 | This API is to verify ospf neighborship by running | |
558 | show ip ospf neighbour command, | |
559 | ||
560 | Parameters | |
561 | ---------- | |
562 | * `tgen` : Topogen object | |
563 | * `topo` : json file data | |
564 | * `dut`: device under test | |
565 | * `input_dict` : Input dict data, required when configuring from testcase | |
566 | * `lan` : verify neighbors in lan topology | |
567 | * `expected` : expected results from API, by-default True | |
568 | ||
569 | Usage | |
570 | ----- | |
571 | 1. To check FULL neighbors. | |
572 | verify_ospf_neighbor(tgen, topo, dut=dut) | |
573 | ||
574 | 2. To check neighbors with their roles. | |
575 | input_dict = { | |
576 | "r0": { | |
577 | "ospf": { | |
578 | "neighbors": { | |
579 | "r1": { | |
580 | "state": "Full", | |
581 | "role": "DR" | |
582 | }, | |
583 | "r2": { | |
584 | "state": "Full", | |
585 | "role": "DROther" | |
586 | }, | |
587 | "r3": { | |
588 | "state": "Full", | |
589 | "role": "DROther" | |
590 | } | |
591 | } | |
592 | } | |
593 | } | |
594 | } | |
595 | result = verify_ospf_neighbor(tgen, topo, dut, input_dict, lan=True) | |
596 | ||
597 | Returns | |
598 | ------- | |
599 | True or False (Error Message) | |
600 | """ | |
601 | logger.debug("Entering lib API: verify_ospf_neighbor()") | |
602 | result = False | |
603 | if topo is None: | |
604 | topo = tgen.json_topo | |
605 | ||
606 | if input_dict: | |
607 | for router, rnode in tgen.routers().items(): | |
608 | if "ospf" not in topo["routers"][router]: | |
609 | continue | |
610 | ||
611 | if dut is not None and dut != router: | |
612 | continue | |
613 | ||
614 | logger.info("Verifying OSPF neighborship on router %s:", router) | |
615 | show_ospf_json = run_frr_cmd( | |
616 | rnode, "show ip ospf neighbor all json", isjson=True | |
617 | ) | |
618 | ||
619 | # Verifying output dictionary show_ospf_json is empty or not | |
620 | if not bool(show_ospf_json): | |
621 | errormsg = "OSPF is not running" | |
622 | return errormsg | |
623 | ||
624 | ospf_data_list = input_dict[router]["ospf"] | |
625 | ospf_nbr_list = ospf_data_list["neighbors"] | |
626 | ||
627 | for ospf_nbr, nbr_data in ospf_nbr_list.items(): | |
628 | data_ip = topo["routers"][ospf_nbr]["links"] | |
629 | data_rid = topo["routers"][ospf_nbr]["ospf"]["router_id"] | |
630 | if ospf_nbr in data_ip: | |
631 | nbr_details = nbr_data[ospf_nbr] | |
632 | elif lan: | |
633 | for switch in topo["switches"]: | |
634 | if "ospf" in topo["switches"][switch]["links"][router]: | |
635 | neighbor_ip = data_ip[switch]["ipv4"].split("/")[0] | |
636 | else: | |
637 | continue | |
638 | else: | |
639 | neighbor_ip = data_ip[router]["ipv4"].split("/")[0] | |
640 | ||
641 | nh_state = None | |
642 | neighbor_ip = neighbor_ip.lower() | |
643 | nbr_rid = data_rid | |
644 | try: | |
645 | nh_state = show_ospf_json[nbr_rid][0]["state"].split("/")[0] | |
646 | intf_state = show_ospf_json[nbr_rid][0]["state"].split("/")[1] | |
647 | except KeyError: | |
648 | errormsg = "[DUT: {}] OSPF peer {} missing".format(router, nbr_rid) | |
649 | return errormsg | |
650 | ||
651 | nbr_state = nbr_data.setdefault("state", None) | |
652 | nbr_role = nbr_data.setdefault("role", None) | |
653 | ||
654 | if nbr_state: | |
655 | if nbr_state == nh_state: | |
656 | logger.info( | |
657 | "[DUT: {}] OSPF Nbr is {}:{} State {}".format( | |
658 | router, ospf_nbr, nbr_rid, nh_state | |
659 | ) | |
660 | ) | |
661 | result = True | |
662 | else: | |
663 | errormsg = ( | |
664 | "[DUT: {}] OSPF is not Converged, neighbor" | |
665 | " state is {}".format(router, nh_state) | |
666 | ) | |
667 | return errormsg | |
668 | if nbr_role: | |
669 | if nbr_role == intf_state: | |
670 | logger.info( | |
671 | "[DUT: {}] OSPF Nbr is {}: {} Role {}".format( | |
672 | router, ospf_nbr, nbr_rid, nbr_role | |
673 | ) | |
674 | ) | |
675 | else: | |
676 | errormsg = ( | |
677 | "[DUT: {}] OSPF is not Converged with rid" | |
678 | "{}, role is {}".format(router, nbr_rid, intf_state) | |
679 | ) | |
680 | return errormsg | |
681 | continue | |
682 | else: | |
683 | for router, rnode in tgen.routers().items(): | |
684 | if "ospf" not in topo["routers"][router]: | |
685 | continue | |
686 | ||
687 | if dut is not None and dut != router: | |
688 | continue | |
689 | ||
690 | logger.info("Verifying OSPF neighborship on router %s:", router) | |
691 | show_ospf_json = run_frr_cmd( | |
692 | rnode, "show ip ospf neighbor all json", isjson=True | |
693 | ) | |
694 | # Verifying output dictionary show_ospf_json is empty or not | |
695 | if not bool(show_ospf_json): | |
696 | errormsg = "OSPF is not running" | |
697 | return errormsg | |
698 | ||
699 | ospf_data_list = topo["routers"][router]["ospf"] | |
700 | ospf_neighbors = ospf_data_list["neighbors"] | |
701 | total_peer = 0 | |
702 | total_peer = len(ospf_neighbors.keys()) | |
703 | no_of_ospf_nbr = 0 | |
704 | ospf_nbr_list = ospf_data_list["neighbors"] | |
705 | no_of_peer = 0 | |
706 | for ospf_nbr, nbr_data in ospf_nbr_list.items(): | |
707 | if nbr_data: | |
708 | data_ip = topo["routers"][nbr_data["nbr"]]["links"] | |
709 | data_rid = topo["routers"][nbr_data["nbr"]]["ospf"]["router_id"] | |
710 | else: | |
711 | data_ip = topo["routers"][ospf_nbr]["links"] | |
712 | data_rid = topo["routers"][ospf_nbr]["ospf"]["router_id"] | |
713 | if ospf_nbr in data_ip: | |
714 | nbr_details = nbr_data[ospf_nbr] | |
715 | elif lan: | |
716 | for switch in topo["switches"]: | |
717 | if "ospf" in topo["switches"][switch]["links"][router]: | |
718 | neighbor_ip = data_ip[switch]["ipv4"].split("/")[0] | |
719 | else: | |
720 | continue | |
721 | else: | |
722 | neighbor_ip = data_ip[router]["ipv4"].split("/")[0] | |
723 | ||
724 | nh_state = None | |
725 | neighbor_ip = neighbor_ip.lower() | |
726 | nbr_rid = data_rid | |
727 | try: | |
728 | nh_state = show_ospf_json[nbr_rid][0]["state"].split("/")[0] | |
729 | except KeyError: | |
730 | errormsg = "[DUT: {}] OSPF peer {} missing,from " "{} ".format( | |
731 | router, nbr_rid, ospf_nbr | |
732 | ) | |
733 | return errormsg | |
734 | ||
735 | if nh_state == "Full": | |
736 | no_of_peer += 1 | |
737 | ||
738 | if no_of_peer == total_peer: | |
739 | logger.info("[DUT: {}] OSPF is Converged".format(router)) | |
740 | result = True | |
741 | else: | |
742 | errormsg = "[DUT: {}] OSPF is not Converged".format(router) | |
743 | return errormsg | |
744 | ||
745 | logger.debug("Exiting API: verify_ospf_neighbor()") | |
746 | return result | |
747 | ||
748 | ||
749 | @retry(retry_timeout=50) | |
750 | def verify_ospf6_neighbor(tgen, topo=None, dut=None, input_dict=None, lan=False): | |
751 | """ | |
752 | This API is to verify ospf neighborship by running | |
753 | show ipv6 ospf neighbour command, | |
754 | ||
755 | Parameters | |
756 | ---------- | |
757 | * `tgen` : Topogen object | |
758 | * `topo` : json file data | |
759 | * `dut`: device under test | |
760 | * `input_dict` : Input dict data, required when configuring from testcase | |
761 | * `lan` : verify neighbors in lan topology | |
762 | ||
763 | Usage | |
764 | ----- | |
765 | 1. To check FULL neighbors. | |
766 | verify_ospf_neighbor(tgen, topo, dut=dut) | |
767 | ||
768 | 2. To check neighbors with their roles. | |
769 | input_dict = { | |
770 | "r0": { | |
771 | "ospf6": { | |
772 | "neighbors": { | |
773 | "r1": { | |
774 | "state": "Full", | |
775 | "role": "DR" | |
776 | }, | |
777 | "r2": { | |
778 | "state": "Full", | |
779 | "role": "DROther" | |
780 | }, | |
781 | "r3": { | |
782 | "state": "Full", | |
783 | "role": "DROther" | |
784 | } | |
785 | } | |
786 | } | |
787 | } | |
788 | } | |
789 | result = verify_ospf6_neighbor(tgen, topo, dut, input_dict, lan=True) | |
790 | ||
791 | 3. To check there are no neighbors. | |
792 | input_dict = { | |
793 | "r0": { | |
794 | "ospf6": { | |
795 | "neighbors": [] | |
796 | } | |
797 | } | |
798 | } | |
799 | result = verify_ospf6_neighbor(tgen, topo, dut, input_dict) | |
800 | ||
801 | Returns | |
802 | ------- | |
803 | True or False (Error Message) | |
804 | """ | |
805 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
806 | result = False | |
807 | ||
808 | if topo is None: | |
809 | topo = tgen.json_topo | |
810 | ||
811 | if input_dict: | |
812 | for router, rnode in tgen.routers().items(): | |
813 | if "ospf6" not in topo["routers"][router]: | |
814 | continue | |
815 | ||
816 | if dut is not None and dut != router: | |
817 | continue | |
818 | ||
819 | logger.info("Verifying OSPF neighborship on router %s:", router) | |
820 | show_ospf_json = run_frr_cmd( | |
821 | rnode, "show ipv6 ospf neighbor json", isjson=True | |
822 | ) | |
823 | # Verifying output dictionary show_ospf_json is empty or not | |
824 | if not bool(show_ospf_json): | |
825 | errormsg = "OSPF6 is not running" | |
826 | return errormsg | |
827 | ||
828 | ospf_data_list = input_dict[router]["ospf6"] | |
829 | ospf_nbr_list = ospf_data_list["neighbors"] | |
830 | ||
831 | # Check if looking for no neighbors | |
832 | if ospf_nbr_list == []: | |
833 | if show_ospf_json["neighbors"] == []: | |
834 | logger.info("[DUT: {}] OSPF6 no neighbors found".format(router)) | |
835 | return True | |
836 | else: | |
837 | errormsg = ( | |
838 | "[DUT: {}] OSPF6 active neighbors found, expected None".format( | |
839 | router | |
840 | ) | |
841 | ) | |
842 | return errormsg | |
843 | ||
844 | for ospf_nbr, nbr_data in ospf_nbr_list.items(): | |
845 | ||
846 | try: | |
847 | data_ip = data_rid = topo["routers"][ospf_nbr]["ospf6"]["router_id"] | |
848 | except KeyError: | |
849 | data_ip = data_rid = topo["routers"][nbr_data["nbr"]]["ospf6"][ | |
850 | "router_id" | |
851 | ] | |
852 | ||
853 | if ospf_nbr in data_ip: | |
854 | nbr_details = nbr_data[ospf_nbr] | |
855 | elif lan: | |
856 | for switch in topo["switches"]: | |
857 | if "ospf6" in topo["switches"][switch]["links"][router]: | |
858 | neighbor_ip = data_ip | |
859 | else: | |
860 | continue | |
861 | else: | |
862 | neighbor_ip = data_ip[router]["ipv6"].split("/")[0] | |
863 | ||
864 | nh_state = None | |
865 | neighbor_ip = neighbor_ip.lower() | |
866 | nbr_rid = data_rid | |
867 | get_index_val = dict( | |
868 | (d["neighborId"], dict(d, index=index)) | |
869 | for (index, d) in enumerate(show_ospf_json["neighbors"]) | |
870 | ) | |
871 | try: | |
872 | nh_state = get_index_val.get(neighbor_ip)["state"] | |
873 | intf_state = get_index_val.get(neighbor_ip)["ifState"] | |
874 | except TypeError: | |
875 | errormsg = "[DUT: {}] OSPF peer {} missing,from " "{} ".format( | |
876 | router, nbr_rid, ospf_nbr | |
877 | ) | |
878 | return errormsg | |
879 | ||
880 | nbr_state = nbr_data.setdefault("state", None) | |
881 | nbr_role = nbr_data.setdefault("role", None) | |
882 | ||
883 | if nbr_state: | |
884 | if nbr_state == nh_state: | |
885 | logger.info( | |
886 | "[DUT: {}] OSPF6 Nbr is {}:{} State {}".format( | |
887 | router, ospf_nbr, nbr_rid, nh_state | |
888 | ) | |
889 | ) | |
890 | result = True | |
891 | else: | |
892 | errormsg = ( | |
893 | "[DUT: {}] OSPF6 is not Converged, neighbor" | |
894 | " state is {} , Expected state is {}".format( | |
895 | router, nh_state, nbr_state | |
896 | ) | |
897 | ) | |
898 | return errormsg | |
899 | if nbr_role: | |
900 | if nbr_role == intf_state: | |
901 | logger.info( | |
902 | "[DUT: {}] OSPF6 Nbr is {}: {} Role {}".format( | |
903 | router, ospf_nbr, nbr_rid, nbr_role | |
904 | ) | |
905 | ) | |
906 | else: | |
907 | errormsg = ( | |
908 | "[DUT: {}] OSPF6 is not Converged with rid" | |
909 | "{}, role is {}, Expected role is {}".format( | |
910 | router, nbr_rid, intf_state, nbr_role | |
911 | ) | |
912 | ) | |
913 | return errormsg | |
914 | continue | |
915 | else: | |
916 | ||
917 | for router, rnode in tgen.routers().items(): | |
918 | if "ospf6" not in topo["routers"][router]: | |
919 | continue | |
920 | ||
921 | if dut is not None and dut != router: | |
922 | continue | |
923 | ||
924 | logger.info("Verifying OSPF6 neighborship on router %s:", router) | |
925 | show_ospf_json = run_frr_cmd( | |
926 | rnode, "show ipv6 ospf neighbor json", isjson=True | |
927 | ) | |
928 | # Verifying output dictionary show_ospf_json is empty or not | |
929 | if not bool(show_ospf_json): | |
930 | errormsg = "OSPF6 is not running" | |
931 | return errormsg | |
932 | ||
933 | ospf_data_list = topo["routers"][router]["ospf6"] | |
934 | ospf_neighbors = ospf_data_list["neighbors"] | |
935 | total_peer = 0 | |
936 | total_peer = len(ospf_neighbors.keys()) | |
937 | no_of_ospf_nbr = 0 | |
938 | ospf_nbr_list = ospf_data_list["neighbors"] | |
939 | no_of_peer = 0 | |
940 | for ospf_nbr, nbr_data in ospf_nbr_list.items(): | |
941 | try: | |
942 | data_ip = data_rid = topo["routers"][ospf_nbr]["ospf6"]["router_id"] | |
943 | except KeyError: | |
944 | data_ip = data_rid = topo["routers"][nbr_data["nbr"]]["ospf6"][ | |
945 | "router_id" | |
946 | ] | |
947 | ||
948 | if ospf_nbr in data_ip: | |
949 | nbr_details = nbr_data[ospf_nbr] | |
950 | elif lan: | |
951 | for switch in topo["switches"]: | |
952 | if "ospf6" in topo["switches"][switch]["links"][router]: | |
953 | neighbor_ip = data_ip | |
954 | else: | |
955 | continue | |
956 | else: | |
957 | neighbor_ip = data_ip | |
958 | ||
959 | nh_state = None | |
960 | neighbor_ip = neighbor_ip.lower() | |
961 | nbr_rid = data_rid | |
962 | get_index_val = dict( | |
963 | (d["neighborId"], dict(d, index=index)) | |
964 | for (index, d) in enumerate(show_ospf_json["neighbors"]) | |
965 | ) | |
966 | try: | |
967 | nh_state = get_index_val.get(neighbor_ip)["state"] | |
968 | intf_state = get_index_val.get(neighbor_ip)["ifState"] | |
969 | except TypeError: | |
970 | errormsg = "[DUT: {}] OSPF peer {} missing,from " "{} ".format( | |
971 | router, nbr_rid, ospf_nbr | |
972 | ) | |
973 | return errormsg | |
974 | ||
975 | if nh_state == "Full": | |
976 | no_of_peer += 1 | |
977 | ||
978 | if no_of_peer == total_peer: | |
979 | logger.info("[DUT: {}] OSPF6 is Converged".format(router)) | |
980 | result = True | |
981 | else: | |
982 | errormsg = "[DUT: {}] OSPF6 is not Converged".format(router) | |
983 | return errormsg | |
984 | ||
985 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
986 | return result | |
987 | ||
988 | ||
989 | @retry(retry_timeout=40) | |
990 | def verify_ospf_rib( | |
991 | tgen, dut, input_dict, next_hop=None, tag=None, metric=None, fib=None, expected=True | |
992 | ): | |
993 | """ | |
994 | This API is to verify ospf routes by running | |
995 | show ip ospf route command. | |
996 | ||
997 | Parameters | |
998 | ---------- | |
999 | * `tgen` : Topogen object | |
1000 | * `dut`: device under test | |
1001 | * `input_dict` : Input dict data, required when configuring from testcase | |
1002 | * `next_hop` : next to be verified | |
1003 | * `tag` : tag to be verified | |
1004 | * `metric` : metric to be verified | |
1005 | * `fib` : True if the route is installed in FIB. | |
1006 | * `expected` : expected results from API, by-default True | |
1007 | ||
1008 | Usage | |
1009 | ----- | |
1010 | input_dict = { | |
1011 | "r1": { | |
1012 | "static_routes": [ | |
1013 | { | |
1014 | "network": ip_net, | |
1015 | "no_of_ip": 1, | |
1016 | "routeType": "N" | |
1017 | } | |
1018 | ] | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | result = verify_ospf_rib(tgen, dut, input_dict,next_hop=nh) | |
1023 | ||
1024 | Returns | |
1025 | ------- | |
1026 | True or False (Error Message) | |
1027 | """ | |
1028 | ||
1029 | logger.info("Entering lib API: verify_ospf_rib()") | |
1030 | result = False | |
1031 | router_list = tgen.routers() | |
1032 | additional_nexthops_in_required_nhs = [] | |
1033 | found_hops = [] | |
1034 | for routerInput in input_dict.keys(): | |
1035 | for router, rnode in router_list.items(): | |
1036 | if router != dut: | |
1037 | continue | |
1038 | ||
1039 | logger.info("Checking router %s RIB:", router) | |
1040 | ||
1041 | # Verifying RIB routes | |
1042 | command = "show ip ospf route" | |
1043 | ||
1044 | found_routes = [] | |
1045 | missing_routes = [] | |
1046 | ||
1047 | if ( | |
1048 | "static_routes" in input_dict[routerInput] | |
1049 | or "prefix" in input_dict[routerInput] | |
1050 | ): | |
1051 | if "prefix" in input_dict[routerInput]: | |
1052 | static_routes = input_dict[routerInput]["prefix"] | |
1053 | else: | |
1054 | static_routes = input_dict[routerInput]["static_routes"] | |
1055 | ||
1056 | for static_route in static_routes: | |
1057 | cmd = "{}".format(command) | |
1058 | ||
1059 | cmd = "{} json".format(cmd) | |
1060 | ||
1061 | ospf_rib_json = run_frr_cmd(rnode, cmd, isjson=True) | |
1062 | ||
1063 | # Verifying output dictionary ospf_rib_json is not empty | |
1064 | if bool(ospf_rib_json) is False: | |
1065 | errormsg = ( | |
1066 | "[DUT: {}] No routes found in OSPF route " | |
1067 | "table".format(router) | |
1068 | ) | |
1069 | return errormsg | |
1070 | ||
1071 | network = static_route["network"] | |
1072 | no_of_ip = static_route.setdefault("no_of_ip", 1) | |
1073 | _tag = static_route.setdefault("tag", None) | |
1074 | _rtype = static_route.setdefault("routeType", None) | |
1075 | ||
1076 | # Generating IPs for verification | |
1077 | ip_list = generate_ips(network, no_of_ip) | |
1078 | st_found = False | |
1079 | nh_found = False | |
1080 | ||
1081 | for st_rt in ip_list: | |
1082 | st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) | |
1083 | ||
1084 | _addr_type = validate_ip_address(st_rt) | |
1085 | if _addr_type != "ipv4": | |
1086 | continue | |
1087 | ||
1088 | if st_rt in ospf_rib_json: | |
1089 | st_found = True | |
1090 | found_routes.append(st_rt) | |
1091 | ||
1092 | if fib and next_hop: | |
1093 | if type(next_hop) is not list: | |
1094 | next_hop = [next_hop] | |
1095 | ||
1096 | for mnh in range(0, len(ospf_rib_json[st_rt])): | |
1097 | if ( | |
1098 | "fib" | |
1099 | in ospf_rib_json[st_rt][mnh]["nexthops"][0] | |
1100 | ): | |
1101 | found_hops.append( | |
1102 | [ | |
1103 | rib_r["ip"] | |
1104 | for rib_r in ospf_rib_json[st_rt][mnh][ | |
1105 | "nexthops" | |
1106 | ] | |
1107 | ] | |
1108 | ) | |
1109 | ||
1110 | if found_hops[0]: | |
1111 | missing_list_of_nexthops = set( | |
1112 | found_hops[0] | |
1113 | ).difference(next_hop) | |
1114 | additional_nexthops_in_required_nhs = set( | |
1115 | next_hop | |
1116 | ).difference(found_hops[0]) | |
1117 | ||
1118 | if additional_nexthops_in_required_nhs: | |
1119 | logger.info( | |
1120 | "Nexthop " | |
1121 | "%s is not active for route %s in " | |
1122 | "RIB of router %s\n", | |
1123 | additional_nexthops_in_required_nhs, | |
1124 | st_rt, | |
1125 | dut, | |
1126 | ) | |
1127 | errormsg = ( | |
1128 | "Nexthop {} is not active" | |
1129 | " for route {} in RIB of router" | |
1130 | " {}\n".format( | |
1131 | additional_nexthops_in_required_nhs, | |
1132 | st_rt, | |
1133 | dut, | |
1134 | ) | |
1135 | ) | |
1136 | return errormsg | |
1137 | else: | |
1138 | nh_found = True | |
1139 | ||
1140 | elif next_hop and fib is None: | |
1141 | if type(next_hop) is not list: | |
1142 | next_hop = [next_hop] | |
1143 | found_hops = [ | |
1144 | rib_r["ip"] | |
1145 | for rib_r in ospf_rib_json[st_rt]["nexthops"] | |
1146 | ] | |
1147 | ||
1148 | if found_hops: | |
1149 | missing_list_of_nexthops = set( | |
1150 | found_hops | |
1151 | ).difference(next_hop) | |
1152 | additional_nexthops_in_required_nhs = set( | |
1153 | next_hop | |
1154 | ).difference(found_hops) | |
1155 | ||
1156 | if additional_nexthops_in_required_nhs: | |
1157 | logger.info( | |
1158 | "Missing nexthop %s for route" | |
1159 | " %s in RIB of router %s\n", | |
1160 | additional_nexthops_in_required_nhs, | |
1161 | st_rt, | |
1162 | dut, | |
1163 | ) | |
1164 | errormsg = ( | |
1165 | "Nexthop {} is Missing for " | |
1166 | "route {} in RIB of router {}\n".format( | |
1167 | additional_nexthops_in_required_nhs, | |
1168 | st_rt, | |
1169 | dut, | |
1170 | ) | |
1171 | ) | |
1172 | return errormsg | |
1173 | else: | |
1174 | nh_found = True | |
1175 | if _rtype: | |
1176 | if "routeType" not in ospf_rib_json[st_rt]: | |
1177 | errormsg = ( | |
1178 | "[DUT: {}]: routeType missing" | |
1179 | " for route {} in OSPF RIB \n".format( | |
1180 | dut, st_rt | |
1181 | ) | |
1182 | ) | |
1183 | return errormsg | |
1184 | elif _rtype != ospf_rib_json[st_rt]["routeType"]: | |
1185 | errormsg = ( | |
1186 | "[DUT: {}]: routeType mismatch" | |
1187 | " for route {} in OSPF RIB \n".format( | |
1188 | dut, st_rt | |
1189 | ) | |
1190 | ) | |
1191 | return errormsg | |
1192 | else: | |
1193 | logger.info( | |
1194 | "[DUT: {}]: Found routeType {}" | |
1195 | " for route {}".format(dut, _rtype, st_rt) | |
1196 | ) | |
1197 | if tag: | |
1198 | if "tag" not in ospf_rib_json[st_rt]: | |
1199 | errormsg = ( | |
1200 | "[DUT: {}]: tag is not" | |
1201 | " present for" | |
1202 | " route {} in RIB \n".format(dut, st_rt) | |
1203 | ) | |
1204 | return errormsg | |
1205 | ||
1206 | if _tag != ospf_rib_json[st_rt]["tag"]: | |
1207 | errormsg = ( | |
1208 | "[DUT: {}]: tag value {}" | |
1209 | " is not matched for" | |
1210 | " route {} in RIB \n".format( | |
1211 | dut, | |
1212 | _tag, | |
1213 | st_rt, | |
1214 | ) | |
1215 | ) | |
1216 | return errormsg | |
1217 | ||
1218 | if metric is not None: | |
1219 | if "type2cost" not in ospf_rib_json[st_rt]: | |
1220 | errormsg = ( | |
1221 | "[DUT: {}]: metric is" | |
1222 | " not present for" | |
1223 | " route {} in RIB \n".format(dut, st_rt) | |
1224 | ) | |
1225 | return errormsg | |
1226 | ||
1227 | if metric != ospf_rib_json[st_rt]["type2cost"]: | |
1228 | errormsg = ( | |
1229 | "[DUT: {}]: metric value " | |
1230 | "{} is not matched for " | |
1231 | "route {} in RIB \n".format( | |
1232 | dut, | |
1233 | metric, | |
1234 | st_rt, | |
1235 | ) | |
1236 | ) | |
1237 | return errormsg | |
1238 | ||
1239 | else: | |
1240 | missing_routes.append(st_rt) | |
1241 | ||
1242 | if nh_found: | |
1243 | logger.info( | |
1244 | "[DUT: {}]: Found next_hop {} for all OSPF" | |
1245 | " routes in RIB".format(router, next_hop) | |
1246 | ) | |
1247 | ||
1248 | if len(missing_routes) > 0: | |
1249 | errormsg = "[DUT: {}]: Missing route in RIB, " "routes: {}".format( | |
1250 | dut, missing_routes | |
1251 | ) | |
1252 | return errormsg | |
1253 | ||
1254 | if found_routes: | |
1255 | logger.info( | |
1256 | "[DUT: %s]: Verified routes in RIB, found" " routes are: %s\n", | |
1257 | dut, | |
1258 | found_routes, | |
1259 | ) | |
1260 | result = True | |
1261 | ||
1262 | logger.info("Exiting lib API: verify_ospf_rib()") | |
1263 | return result | |
1264 | ||
1265 | ||
1266 | @retry(retry_timeout=20) | |
1267 | def verify_ospf_interface( | |
1268 | tgen, topo=None, dut=None, lan=False, input_dict=None, expected=True | |
1269 | ): | |
1270 | """ | |
1271 | This API is to verify ospf routes by running | |
1272 | show ip ospf interface command. | |
1273 | ||
1274 | Parameters | |
1275 | ---------- | |
1276 | * `tgen` : Topogen object | |
1277 | * `topo` : topology descriptions | |
1278 | * `dut`: device under test | |
1279 | * `lan`: if set to true this interface belongs to LAN. | |
1280 | * `input_dict` : Input dict data, required when configuring from testcase | |
1281 | * `expected` : expected results from API, by-default True | |
1282 | ||
1283 | Usage | |
1284 | ----- | |
1285 | input_dict= { | |
1286 | 'r0': { | |
1287 | 'links':{ | |
1288 | 's1': { | |
1289 | 'ospf':{ | |
1290 | 'priority':98, | |
1291 | 'timerDeadSecs': 4, | |
1292 | 'area': '0.0.0.3', | |
1293 | 'mcastMemberOspfDesignatedRouters': True, | |
1294 | 'mcastMemberOspfAllRouters': True, | |
1295 | 'ospfEnabled': True, | |
1296 | ||
1297 | } | |
1298 | } | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | result = verify_ospf_interface(tgen, topo, dut=dut, input_dict=input_dict) | |
1303 | ||
1304 | Returns | |
1305 | ------- | |
1306 | True or False (Error Message) | |
1307 | """ | |
1308 | ||
1309 | logger.debug("Entering lib API: verify_ospf_interface()") | |
1310 | result = False | |
1311 | if topo is None: | |
1312 | topo = tgen.json_topo | |
1313 | ||
1314 | for router, rnode in tgen.routers().items(): | |
1315 | if "ospf" not in topo["routers"][router]: | |
1316 | continue | |
1317 | ||
1318 | if dut is not None and dut != router: | |
1319 | continue | |
1320 | ||
1321 | logger.info("Verifying OSPF interface on router %s:", router) | |
1322 | show_ospf_json = run_frr_cmd(rnode, "show ip ospf interface json", isjson=True) | |
1323 | ||
1324 | # Verifying output dictionary show_ospf_json is empty or not | |
1325 | if not bool(show_ospf_json): | |
1326 | errormsg = "OSPF is not running" | |
1327 | return errormsg | |
1328 | ||
1329 | # To find neighbor ip type | |
1330 | ospf_intf_data = input_dict[router]["links"] | |
1331 | for ospf_intf, intf_data in ospf_intf_data.items(): | |
1332 | intf = topo["routers"][router]["links"][ospf_intf]["interface"] | |
1333 | if intf in show_ospf_json["interfaces"]: | |
1334 | for intf_attribute in intf_data["ospf"]: | |
1335 | if ( | |
1336 | intf_data["ospf"][intf_attribute] | |
1337 | == show_ospf_json["interfaces"][intf][intf_attribute] | |
1338 | ): | |
1339 | logger.info( | |
1340 | "[DUT: %s] OSPF interface %s: %s is %s", | |
1341 | router, | |
1342 | intf, | |
1343 | intf_attribute, | |
1344 | intf_data["ospf"][intf_attribute], | |
1345 | ) | |
1346 | else: | |
1347 | errormsg = "[DUT: {}] OSPF interface {}: {} is {}, \ | |
1348 | Expected is {}".format( | |
1349 | router, | |
1350 | intf, | |
1351 | intf_attribute, | |
1352 | intf_data["ospf"][intf_attribute], | |
1353 | show_ospf_json["interfaces"][intf][intf_attribute], | |
1354 | ) | |
1355 | return errormsg | |
1356 | result = True | |
1357 | logger.debug("Exiting API: verify_ospf_interface()") | |
1358 | return result | |
1359 | ||
1360 | ||
1361 | @retry(retry_timeout=40) | |
1362 | def verify_ospf_database( | |
1363 | tgen, topo, dut, input_dict, vrf=None, lsatype=None, rid=None, expected=True | |
1364 | ): | |
1365 | """ | |
1366 | This API is to verify ospf lsa's by running | |
1367 | show ip ospf database command. | |
1368 | ||
1369 | Parameters | |
1370 | ---------- | |
1371 | * `tgen` : Topogen object | |
1372 | * `dut`: device under test | |
1373 | * `input_dict` : Input dict data, required when configuring from testcase | |
1374 | * `topo` : next to be verified | |
1375 | * `expected` : expected results from API, by-default True | |
1376 | ||
1377 | Usage | |
1378 | ----- | |
1379 | input_dict = { | |
1380 | "areas": { | |
1381 | "0.0.0.0": { | |
1382 | "Router Link States": { | |
1383 | "100.1.1.0-100.1.1.0": { | |
1384 | "LSID": "100.1.1.0", | |
1385 | "Advertised router": "100.1.1.0", | |
1386 | "LSA Age": 130, | |
1387 | "Sequence Number": "80000006", | |
1388 | "Checksum": "a703", | |
1389 | "Router links": 3 | |
1390 | } | |
1391 | }, | |
1392 | "Net Link States": { | |
1393 | "10.0.0.2-100.1.1.1": { | |
1394 | "LSID": "10.0.0.2", | |
1395 | "Advertised router": "100.1.1.1", | |
1396 | "LSA Age": 137, | |
1397 | "Sequence Number": "80000001", | |
1398 | "Checksum": "9583" | |
1399 | } | |
1400 | }, | |
1401 | }, | |
1402 | } | |
1403 | } | |
1404 | result = verify_ospf_database(tgen, topo, dut, input_dict) | |
1405 | ||
1406 | Returns | |
1407 | ------- | |
1408 | True or False (Error Message) | |
1409 | """ | |
1410 | ||
1411 | result = False | |
1412 | router = dut | |
1413 | logger.debug("Entering lib API: verify_ospf_database()") | |
1414 | ||
1415 | if "ospf" not in topo["routers"][dut]: | |
1416 | errormsg = "[DUT: {}] OSPF is not configured on the router.".format(dut) | |
1417 | return errormsg | |
1418 | ||
1419 | rnode = tgen.routers()[dut] | |
1420 | ||
1421 | logger.info("Verifying OSPF interface on router %s:", dut) | |
1422 | ||
1423 | if not rid: | |
1424 | rid = "self-originate" | |
1425 | if lsatype: | |
1426 | if vrf is None: | |
1427 | command = "show ip ospf database {} {} json".format(lsatype, rid) | |
1428 | else: | |
1429 | command = "show ip ospf database {} {} vrf {} json".format( | |
1430 | lsatype, rid, vrf | |
1431 | ) | |
1432 | else: | |
1433 | if vrf is None: | |
1434 | command = "show ip ospf database json" | |
1435 | else: | |
1436 | command = "show ip ospf database vrf {} json".format(vrf) | |
1437 | ||
1438 | show_ospf_json = run_frr_cmd(rnode, command, isjson=True) | |
1439 | # Verifying output dictionary show_ospf_json is empty or not | |
1440 | if not bool(show_ospf_json): | |
1441 | errormsg = "OSPF is not running" | |
1442 | return errormsg | |
1443 | ||
1444 | # for inter and inter lsa's | |
1445 | ospf_db_data = input_dict.setdefault("areas", None) | |
1446 | ospf_external_lsa = input_dict.setdefault("AS External Link States", None) | |
1447 | # import pdb; pdb.set_trace() | |
1448 | if ospf_db_data: | |
1449 | for ospf_area, area_lsa in ospf_db_data.items(): | |
1450 | if ospf_area in show_ospf_json["routerLinkStates"]["areas"]: | |
1451 | if "routerLinkStates" in area_lsa: | |
1452 | for lsa in area_lsa["routerLinkStates"]: | |
1453 | _advrtr = lsa.setdefault("advertisedRouter", None) | |
1454 | _options = lsa.setdefault("options", None) | |
1455 | ||
1456 | if ( | |
1457 | _options | |
1458 | and lsa["lsaId"] | |
1459 | == show_ospf_json["routerLinkStates"]["areas"][ospf_area][ | |
1460 | 0 | |
1461 | ]["linkStateId"] | |
1462 | and lsa["options"] | |
1463 | == show_ospf_json["routerLinkStates"]["areas"][ospf_area][ | |
1464 | 0 | |
1465 | ]["options"] | |
1466 | ): | |
1467 | result = True | |
1468 | break | |
1469 | else: | |
1470 | errormsg = '[DUT: {}] OSPF LSA options: expected {}, Received Options are {} lsa["options"] {} OSPF LSAID: expected lsaid {}, Received lsaid {}'.format( | |
1471 | dut, | |
1472 | show_ospf_json["routerLinkStates"]["areas"][ospf_area][ | |
1473 | 0 | |
1474 | ]["options"], | |
1475 | _options, | |
1476 | lsa["options"], | |
1477 | show_ospf_json["routerLinkStates"]["areas"][ospf_area][ | |
1478 | 0 | |
1479 | ]["linkStateId"], | |
1480 | lsa["lsaId"], | |
1481 | ) | |
1482 | return errormsg | |
1483 | if "Net Link States" in area_lsa: | |
1484 | for lsa in area_lsa["Net Link States"]: | |
1485 | if lsa in show_ospf_json["areas"][ospf_area]["Net Link States"]: | |
1486 | logger.info( | |
1487 | "[DUT: %s] OSPF LSDB area %s:Network " "LSA %s", | |
1488 | router, | |
1489 | ospf_area, | |
1490 | lsa, | |
1491 | ) | |
1492 | result = True | |
1493 | else: | |
1494 | errormsg = ( | |
1495 | "[DUT: {}] OSPF LSDB area {}: expected" | |
1496 | " Network LSA is {}".format(router, ospf_area, lsa) | |
1497 | ) | |
1498 | return errormsg | |
1499 | if "Summary Link States" in area_lsa: | |
1500 | for lsa in area_lsa["Summary Link States"]: | |
1501 | if ( | |
1502 | lsa | |
1503 | in show_ospf_json["areas"][ospf_area]["Summary Link States"] | |
1504 | ): | |
1505 | logger.info( | |
1506 | "[DUT: %s] OSPF LSDB area %s:Summary " "LSA %s", | |
1507 | router, | |
1508 | ospf_area, | |
1509 | lsa, | |
1510 | ) | |
1511 | result = True | |
1512 | else: | |
1513 | errormsg = ( | |
1514 | "[DUT: {}] OSPF LSDB area {}: expected" | |
1515 | " Summary LSA is {}".format(router, ospf_area, lsa) | |
1516 | ) | |
1517 | return errormsg | |
1518 | if "ASBR-Summary Link States" in area_lsa: | |
1519 | for lsa in area_lsa["ASBR-Summary Link States"]: | |
1520 | if ( | |
1521 | lsa | |
1522 | in show_ospf_json["areas"][ospf_area][ | |
1523 | "ASBR-Summary Link States" | |
1524 | ] | |
1525 | ): | |
1526 | logger.info( | |
1527 | "[DUT: %s] OSPF LSDB area %s:ASBR Summary " "LSA %s", | |
1528 | router, | |
1529 | ospf_area, | |
1530 | lsa, | |
1531 | ) | |
1532 | result = True | |
1533 | else: | |
1534 | errormsg = ( | |
1535 | "[DUT: {}] OSPF LSDB area {}: expected" | |
1536 | " ASBR Summary LSA is {}".format(router, ospf_area, lsa) | |
1537 | ) | |
1538 | return errormsg | |
1539 | if ospf_external_lsa: | |
1540 | for ospf_ext_lsa, ext_lsa_data in ospf_external_lsa.items(): | |
1541 | if ospf_ext_lsa in show_ospf_json["AS External Link States"]: | |
1542 | logger.info( | |
1543 | "[DUT: %s] OSPF LSDB:External LSA %s", router, ospf_ext_lsa | |
1544 | ) | |
1545 | result = True | |
1546 | else: | |
1547 | errormsg = ( | |
1548 | "[DUT: {}] OSPF LSDB : expected" | |
1549 | " External LSA is {}".format(router, ospf_ext_lsa) | |
1550 | ) | |
1551 | return errormsg | |
1552 | ||
1553 | logger.debug("Exiting API: verify_ospf_database()") | |
1554 | return result | |
1555 | ||
1556 | ||
1557 | @retry(retry_timeout=20) | |
1558 | def verify_ospf_summary(tgen, topo, dut, input_dict, ospf=None, expected=True): | |
1559 | """ | |
1560 | This API is to verify ospf routes by running | |
1561 | show ip ospf interface command. | |
1562 | ||
1563 | Parameters | |
1564 | ---------- | |
1565 | * `tgen` : Topogen object | |
1566 | * `topo` : topology descriptions | |
1567 | * `dut`: device under test | |
1568 | * `input_dict` : Input dict data, required when configuring from testcase | |
1569 | ||
1570 | Usage | |
1571 | ----- | |
1572 | input_dict = { | |
1573 | "11.0.0.0/8": { | |
1574 | "summaryAddress": "11.0.0.0/8", | |
1575 | "metricType": "E2", | |
1576 | "metric": 20, | |
1577 | "tag": 0, | |
1578 | "externalRouteCount": 5 | |
1579 | } | |
1580 | } | |
1581 | result = verify_ospf_summary(tgen, topo, dut, input_dict) | |
1582 | ||
1583 | Returns | |
1584 | ------- | |
1585 | True or False (Error Message) | |
1586 | """ | |
1587 | ||
1588 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
1589 | result = False | |
1590 | router = dut | |
1591 | ||
1592 | logger.info("Verifying OSPF summary on router %s:", router) | |
1593 | ||
1594 | rnode = tgen.routers()[dut] | |
1595 | ||
1596 | if ospf: | |
1597 | if "ospf6" not in topo["routers"][dut]: | |
1598 | errormsg = "[DUT: {}] OSPF6 is not configured on the router.".format(router) | |
1599 | return errormsg | |
1600 | ||
1601 | show_ospf_json = run_frr_cmd( | |
1602 | rnode, "show ipv6 ospf summary detail json", isjson=True | |
1603 | ) | |
1604 | else: | |
1605 | if "ospf" not in topo["routers"][dut]: | |
1606 | errormsg = "[DUT: {}] OSPF is not configured on the router.".format(router) | |
1607 | return errormsg | |
1608 | ||
1609 | show_ospf_json = run_frr_cmd( | |
1610 | rnode, "show ip ospf summary detail json", isjson=True | |
1611 | ) | |
1612 | ||
1613 | # Verifying output dictionary show_ospf_json is empty or not | |
1614 | if not bool(show_ospf_json): | |
1615 | errormsg = "OSPF is not running" | |
1616 | return errormsg | |
1617 | ||
1618 | # To find neighbor ip type | |
1619 | ospf_summary_data = input_dict | |
1620 | ||
1621 | if ospf: | |
1622 | show_ospf_json = show_ospf_json["default"] | |
1623 | ||
1624 | for ospf_summ, summ_data in ospf_summary_data.items(): | |
1625 | if ospf_summ not in show_ospf_json: | |
1626 | continue | |
1627 | summary = ospf_summary_data[ospf_summ]["summaryAddress"] | |
1628 | ||
1629 | if summary in show_ospf_json: | |
1630 | for summ in summ_data: | |
1631 | if summ_data[summ] == show_ospf_json[summary][summ]: | |
1632 | logger.info( | |
1633 | "[DUT: %s] OSPF summary %s:%s is %s", | |
1634 | router, | |
1635 | summary, | |
1636 | summ, | |
1637 | summ_data[summ], | |
1638 | ) | |
1639 | result = True | |
1640 | else: | |
1641 | errormsg = ( | |
1642 | "[DUT: {}] OSPF summary {} : {} is {}, " | |
1643 | "Expected is {}".format( | |
1644 | router, | |
1645 | summary, | |
1646 | summ, | |
1647 | show_ospf_json[summary][summ], | |
1648 | summ_data[summ], | |
1649 | ) | |
1650 | ) | |
1651 | return errormsg | |
1652 | ||
1653 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
1654 | return result | |
1655 | ||
1656 | ||
1657 | @retry(retry_timeout=30) | |
1658 | def verify_ospf6_rib( | |
1659 | tgen, dut, input_dict, next_hop=None, tag=None, metric=None, fib=None | |
1660 | ): | |
1661 | """ | |
1662 | This API is to verify ospf routes by running | |
1663 | show ip ospf route command. | |
1664 | ||
1665 | Parameters | |
1666 | ---------- | |
1667 | * `tgen` : Topogen object | |
1668 | * `dut`: device under test | |
1669 | * `input_dict` : Input dict data, required when configuring from testcase | |
1670 | * `next_hop` : next to be verified | |
1671 | * `tag` : tag to be verified | |
1672 | * `metric` : metric to be verified | |
1673 | * `fib` : True if the route is installed in FIB. | |
1674 | ||
1675 | Usage | |
1676 | ----- | |
1677 | input_dict = { | |
1678 | "r1": { | |
1679 | "static_routes": [ | |
1680 | { | |
1681 | "network": ip_net, | |
1682 | "no_of_ip": 1, | |
1683 | "routeType": "N" | |
1684 | } | |
1685 | ] | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | result = verify_ospf6_rib(tgen, dut, input_dict,next_hop=nh) | |
1690 | ||
1691 | Returns | |
1692 | ------- | |
1693 | True or False (Error Message) | |
1694 | """ | |
1695 | ||
1696 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
1697 | result = False | |
1698 | router_list = tgen.routers() | |
1699 | additional_nexthops_in_required_nhs = [] | |
1700 | found_hops = [] | |
1701 | for routerInput in input_dict.keys(): | |
1702 | for router, rnode in router_list.items(): | |
1703 | if router != dut: | |
1704 | continue | |
1705 | ||
1706 | logger.info("Checking router %s RIB:", router) | |
1707 | ||
1708 | # Verifying RIB routes | |
1709 | command = "show ipv6 ospf route detail" | |
1710 | ||
1711 | found_routes = [] | |
1712 | missing_routes = [] | |
1713 | ||
1714 | if ( | |
1715 | "static_routes" in input_dict[routerInput] | |
1716 | or "prefix" in input_dict[routerInput] | |
1717 | ): | |
1718 | if "prefix" in input_dict[routerInput]: | |
1719 | static_routes = input_dict[routerInput]["prefix"] | |
1720 | else: | |
1721 | static_routes = input_dict[routerInput]["static_routes"] | |
1722 | ||
1723 | for static_route in static_routes: | |
1724 | cmd = "{}".format(command) | |
1725 | ||
1726 | cmd = "{} json".format(cmd) | |
1727 | ||
1728 | ospf_rib_json = run_frr_cmd(rnode, cmd, isjson=True) | |
1729 | ||
1730 | # Fix for PR 2644182 | |
1731 | try: | |
1732 | ospf_rib_json = ospf_rib_json["routes"] | |
1733 | except KeyError: | |
1734 | pass | |
1735 | ||
1736 | # Verifying output dictionary ospf_rib_json is not empty | |
1737 | if bool(ospf_rib_json) is False: | |
1738 | errormsg = ( | |
1739 | "[DUT: {}] No routes found in OSPF6 route " | |
1740 | "table".format(router) | |
1741 | ) | |
1742 | return errormsg | |
1743 | ||
1744 | network = static_route["network"] | |
1745 | no_of_ip = static_route.setdefault("no_of_ip", 1) | |
1746 | _tag = static_route.setdefault("tag", None) | |
1747 | _rtype = static_route.setdefault("routeType", None) | |
1748 | ||
1749 | # Generating IPs for verification | |
1750 | ip_list = generate_ips(network, no_of_ip) | |
1751 | if len(ip_list) == 1: | |
1752 | ip_list = [network] | |
1753 | st_found = False | |
1754 | nh_found = False | |
1755 | for st_rt in ip_list: | |
1756 | st_rt = str(ipaddress.ip_network(frr_unicode(st_rt))) | |
1757 | ||
1758 | _addr_type = validate_ip_address(st_rt) | |
1759 | if _addr_type != "ipv6": | |
1760 | continue | |
1761 | ||
1762 | if st_rt in ospf_rib_json: | |
1763 | ||
1764 | st_found = True | |
1765 | found_routes.append(st_rt) | |
1766 | ||
1767 | if fib and next_hop: | |
1768 | if type(next_hop) is not list: | |
1769 | next_hop = [next_hop] | |
1770 | ||
1771 | for mnh in range(0, len(ospf_rib_json[st_rt])): | |
1772 | if ( | |
1773 | "fib" | |
1774 | in ospf_rib_json[st_rt][mnh]["nextHops"][0] | |
1775 | ): | |
1776 | found_hops.append( | |
1777 | [ | |
1778 | rib_r["ip"] | |
1779 | for rib_r in ospf_rib_json[st_rt][mnh][ | |
1780 | "nextHops" | |
1781 | ] | |
1782 | ] | |
1783 | ) | |
1784 | ||
1785 | if found_hops[0]: | |
1786 | missing_list_of_nexthops = set( | |
1787 | found_hops[0] | |
1788 | ).difference(next_hop) | |
1789 | additional_nexthops_in_required_nhs = set( | |
1790 | next_hop | |
1791 | ).difference(found_hops[0]) | |
1792 | ||
1793 | if additional_nexthops_in_required_nhs: | |
1794 | logger.info( | |
1795 | "Nexthop " | |
1796 | "%s is not active for route %s in " | |
1797 | "RIB of router %s\n", | |
1798 | additional_nexthops_in_required_nhs, | |
1799 | st_rt, | |
1800 | dut, | |
1801 | ) | |
1802 | errormsg = ( | |
1803 | "Nexthop {} is not active" | |
1804 | " for route {} in RIB of router" | |
1805 | " {}\n".format( | |
1806 | additional_nexthops_in_required_nhs, | |
1807 | st_rt, | |
1808 | dut, | |
1809 | ) | |
1810 | ) | |
1811 | return errormsg | |
1812 | else: | |
1813 | nh_found = True | |
1814 | ||
1815 | elif next_hop and fib is None: | |
1816 | if type(next_hop) is not list: | |
1817 | next_hop = [next_hop] | |
1818 | found_hops = [ | |
1819 | rib_r["nextHop"] | |
1820 | for rib_r in ospf_rib_json[st_rt]["nextHops"] | |
1821 | ] | |
1822 | ||
1823 | if found_hops: | |
1824 | missing_list_of_nexthops = set( | |
1825 | found_hops | |
1826 | ).difference(next_hop) | |
1827 | additional_nexthops_in_required_nhs = set( | |
1828 | next_hop | |
1829 | ).difference(found_hops) | |
1830 | if additional_nexthops_in_required_nhs: | |
1831 | logger.info( | |
1832 | "Missing nexthop %s for route" | |
1833 | " %s in RIB of router %s\n", | |
1834 | additional_nexthops_in_required_nhs, | |
1835 | st_rt, | |
1836 | dut, | |
1837 | ) | |
1838 | errormsg = ( | |
1839 | "Nexthop {} is Missing for " | |
1840 | "route {} in RIB of router {}\n".format( | |
1841 | additional_nexthops_in_required_nhs, | |
1842 | st_rt, | |
1843 | dut, | |
1844 | ) | |
1845 | ) | |
1846 | return errormsg | |
1847 | else: | |
1848 | nh_found = True | |
1849 | if _rtype: | |
1850 | if "destinationType" not in ospf_rib_json[st_rt]: | |
1851 | errormsg = ( | |
1852 | "[DUT: {}]: destinationType missing" | |
1853 | "for route {} in OSPF RIB \n".format(dut, st_rt) | |
1854 | ) | |
1855 | return errormsg | |
1856 | elif _rtype != ospf_rib_json[st_rt]["destinationType"]: | |
1857 | errormsg = ( | |
1858 | "[DUT: {}]: destinationType mismatch" | |
1859 | "for route {} in OSPF RIB \n".format(dut, st_rt) | |
1860 | ) | |
1861 | return errormsg | |
1862 | else: | |
1863 | logger.info( | |
1864 | "DUT: {}]: Found destinationType {}" | |
1865 | "for route {}".format(dut, _rtype, st_rt) | |
1866 | ) | |
1867 | if tag: | |
1868 | if "tag" not in ospf_rib_json[st_rt]: | |
1869 | errormsg = ( | |
1870 | "[DUT: {}]: tag is not" | |
1871 | " present for" | |
1872 | " route {} in RIB \n".format(dut, st_rt) | |
1873 | ) | |
1874 | return errormsg | |
1875 | ||
1876 | if _tag != ospf_rib_json[st_rt]["tag"]: | |
1877 | errormsg = ( | |
1878 | "[DUT: {}]: tag value {}" | |
1879 | " is not matched for" | |
1880 | " route {} in RIB \n".format( | |
1881 | dut, | |
1882 | _tag, | |
1883 | st_rt, | |
1884 | ) | |
1885 | ) | |
1886 | return errormsg | |
1887 | ||
1888 | if metric is not None: | |
1889 | if "metricCostE2" not in ospf_rib_json[st_rt]: | |
1890 | errormsg = ( | |
1891 | "[DUT: {}]: metric is" | |
1892 | " not present for" | |
1893 | " route {} in RIB \n".format(dut, st_rt) | |
1894 | ) | |
1895 | return errormsg | |
1896 | ||
1897 | if metric != ospf_rib_json[st_rt]["metricCostE2"]: | |
1898 | errormsg = ( | |
1899 | "[DUT: {}]: metric value " | |
1900 | "{} is not matched for " | |
1901 | "route {} in RIB \n".format( | |
1902 | dut, | |
1903 | metric, | |
1904 | st_rt, | |
1905 | ) | |
1906 | ) | |
1907 | return errormsg | |
1908 | ||
1909 | else: | |
1910 | missing_routes.append(st_rt) | |
1911 | ||
1912 | if nh_found: | |
1913 | logger.info( | |
1914 | "[DUT: {}]: Found next_hop {} for all OSPF" | |
1915 | " routes in RIB".format(router, next_hop) | |
1916 | ) | |
1917 | ||
1918 | if len(missing_routes) > 0: | |
1919 | errormsg = "[DUT: {}]: Missing route in RIB, " "routes: {}".format( | |
1920 | dut, missing_routes | |
1921 | ) | |
1922 | return errormsg | |
1923 | ||
1924 | if found_routes: | |
1925 | logger.info( | |
1926 | "[DUT: %s]: Verified routes in RIB, found" " routes are: %s\n", | |
1927 | dut, | |
1928 | found_routes, | |
1929 | ) | |
1930 | result = True | |
1931 | ||
1932 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
1933 | return result | |
1934 | ||
1935 | ||
1936 | @retry(retry_timeout=6) | |
1937 | def verify_ospf6_interface(tgen, topo=None, dut=None, lan=False, input_dict=None): | |
1938 | """ | |
1939 | This API is to verify ospf routes by running | |
1940 | show ip ospf interface command. | |
1941 | ||
1942 | Parameters | |
1943 | ---------- | |
1944 | * `tgen` : Topogen object | |
1945 | * `topo` : topology descriptions | |
1946 | * `dut`: device under test | |
1947 | * `lan`: if set to true this interface belongs to LAN. | |
1948 | * `input_dict` : Input dict data, required when configuring from testcase | |
1949 | ||
1950 | Usage | |
1951 | ----- | |
1952 | input_dict= { | |
1953 | 'r0': { | |
1954 | 'links':{ | |
1955 | 's1': { | |
1956 | 'ospf6':{ | |
1957 | 'priority':98, | |
1958 | 'timerDeadSecs': 4, | |
1959 | 'area': '0.0.0.3', | |
1960 | 'mcastMemberOspfDesignatedRouters': True, | |
1961 | 'mcastMemberOspfAllRouters': True, | |
1962 | 'ospfEnabled': True, | |
1963 | ||
1964 | } | |
1965 | } | |
1966 | } | |
1967 | } | |
1968 | } | |
1969 | result = verify_ospf_interface(tgen, topo, dut=dut, input_dict=input_dict) | |
1970 | ||
1971 | Returns | |
1972 | ------- | |
1973 | True or False (Error Message) | |
1974 | """ | |
1975 | ||
1976 | logger.debug("Entering lib API: verify_ospf6_interface") | |
1977 | result = False | |
1978 | ||
1979 | if topo is None: | |
1980 | topo = tgen.json_topo | |
1981 | ||
1982 | for router, rnode in tgen.routers().items(): | |
1983 | if "ospf6" not in topo["routers"][router]: | |
1984 | continue | |
1985 | ||
1986 | if dut is not None and dut != router: | |
1987 | continue | |
1988 | ||
1989 | logger.info("Verifying OSPF interface on router %s:", router) | |
1990 | show_ospf_json = run_frr_cmd( | |
1991 | rnode, "show ipv6 ospf interface json", isjson=True | |
1992 | ) | |
1993 | ||
1994 | # Verifying output dictionary show_ospf_json is empty or not | |
1995 | if not bool(show_ospf_json): | |
1996 | errormsg = "OSPF6 is not running" | |
1997 | return errormsg | |
1998 | ||
1999 | # To find neighbor ip type | |
2000 | ospf_intf_data = input_dict[router]["links"] | |
2001 | for ospf_intf, intf_data in ospf_intf_data.items(): | |
2002 | intf = topo["routers"][router]["links"][ospf_intf]["interface"] | |
2003 | if intf in show_ospf_json: | |
2004 | for intf_attribute in intf_data["ospf6"]: | |
2005 | if intf_data["ospf6"][intf_attribute] is not list: | |
2006 | if ( | |
2007 | intf_data["ospf6"][intf_attribute] | |
2008 | == show_ospf_json[intf][intf_attribute] | |
2009 | ): | |
2010 | logger.info( | |
2011 | "[DUT: %s] OSPF6 interface %s: %s is %s", | |
2012 | router, | |
2013 | intf, | |
2014 | intf_attribute, | |
2015 | intf_data["ospf6"][intf_attribute], | |
2016 | ) | |
2017 | elif intf_data["ospf6"][intf_attribute] is list: | |
2018 | for addr_list in len(show_ospf_json[intf][intf_attribute]): | |
2019 | if ( | |
2020 | show_ospf_json[intf][intf_attribute][addr_list][ | |
2021 | "address" | |
2022 | ].split("/")[0] | |
2023 | == intf_data["ospf6"]["internetAddress"][0]["address"] | |
2024 | ): | |
2025 | break | |
2026 | else: | |
2027 | errormsg = "[DUT: {}] OSPF6 interface {}: {} is {}, \ | |
2028 | Expected is {}".format( | |
2029 | router, | |
2030 | intf, | |
2031 | intf_attribute, | |
2032 | intf_data["ospf6"][intf_attribute], | |
2033 | intf_data["ospf6"][intf_attribute], | |
2034 | ) | |
2035 | return errormsg | |
2036 | else: | |
2037 | errormsg = "[DUT: {}] OSPF6 interface {}: {} is {}, \ | |
2038 | Expected is {}".format( | |
2039 | router, | |
2040 | intf, | |
2041 | intf_attribute, | |
2042 | intf_data["ospf6"][intf_attribute], | |
2043 | intf_data["ospf6"][intf_attribute], | |
2044 | ) | |
2045 | return errormsg | |
2046 | result = True | |
2047 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
2048 | return result | |
2049 | ||
2050 | ||
2051 | @retry(retry_timeout=20) | |
2052 | def verify_ospf6_database(tgen, topo, dut, input_dict): | |
2053 | """ | |
2054 | This API is to verify ospf lsa's by running | |
2055 | show ip ospf database command. | |
2056 | ||
2057 | Parameters | |
2058 | ---------- | |
2059 | * `tgen` : Topogen object | |
2060 | * `dut`: device under test | |
2061 | * `input_dict` : Input dict data, required when configuring from testcase | |
2062 | * `topo` : next to be verified | |
2063 | ||
2064 | Usage | |
2065 | ----- | |
2066 | input_dict = { | |
2067 | "areas": { | |
2068 | "0.0.0.0": { | |
2069 | "routerLinkStates": { | |
2070 | "100.1.1.0-100.1.1.0": { | |
2071 | "LSID": "100.1.1.0", | |
2072 | "Advertised router": "100.1.1.0", | |
2073 | "LSA Age": 130, | |
2074 | "Sequence Number": "80000006", | |
2075 | "Checksum": "a703", | |
2076 | "Router links": 3 | |
2077 | } | |
2078 | }, | |
2079 | "networkLinkStates": { | |
2080 | "10.0.0.2-100.1.1.1": { | |
2081 | "LSID": "10.0.0.2", | |
2082 | "Advertised router": "100.1.1.1", | |
2083 | "LSA Age": 137, | |
2084 | "Sequence Number": "80000001", | |
2085 | "Checksum": "9583" | |
2086 | } | |
2087 | }, | |
2088 | }, | |
2089 | } | |
2090 | } | |
2091 | result = verify_ospf_database(tgen, topo, dut, input_dict) | |
2092 | ||
2093 | Returns | |
2094 | ------- | |
2095 | True or False (Error Message) | |
2096 | """ | |
2097 | ||
2098 | result = False | |
2099 | router = dut | |
2100 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
2101 | ||
2102 | if "ospf" not in topo["routers"][dut]: | |
2103 | errormsg = "[DUT: {}] OSPF is not configured on the router.".format(dut) | |
2104 | return errormsg | |
2105 | ||
2106 | rnode = tgen.routers()[dut] | |
2107 | ||
2108 | logger.info("Verifying OSPF interface on router %s:", dut) | |
2109 | show_ospf_json = run_frr_cmd(rnode, "show ip ospf database json", isjson=True) | |
2110 | # Verifying output dictionary show_ospf_json is empty or not | |
2111 | if not bool(show_ospf_json): | |
2112 | errormsg = "OSPF is not running" | |
2113 | return errormsg | |
2114 | ||
2115 | # for inter and inter lsa's | |
2116 | ospf_db_data = input_dict.setdefault("areas", None) | |
2117 | ospf_external_lsa = input_dict.setdefault("asExternalLinkStates", None) | |
2118 | ||
2119 | if ospf_db_data: | |
2120 | for ospf_area, area_lsa in ospf_db_data.items(): | |
2121 | if ospf_area in show_ospf_json["areas"]: | |
2122 | if "routerLinkStates" in area_lsa: | |
2123 | for lsa in area_lsa["routerLinkStates"]: | |
2124 | for rtrlsa in show_ospf_json["areas"][ospf_area][ | |
2125 | "routerLinkStates" | |
2126 | ]: | |
2127 | if ( | |
2128 | lsa["lsaId"] == rtrlsa["lsaId"] | |
2129 | and lsa["advertisedRouter"] | |
2130 | == rtrlsa["advertisedRouter"] | |
2131 | ): | |
2132 | result = True | |
2133 | break | |
2134 | if result: | |
2135 | logger.info( | |
2136 | "[DUT: %s] OSPF LSDB area %s:Router " "LSA %s", | |
2137 | router, | |
2138 | ospf_area, | |
2139 | lsa, | |
2140 | ) | |
2141 | break | |
2142 | else: | |
2143 | errormsg = ( | |
2144 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2145 | " Router LSA is {}".format(router, ospf_area, lsa) | |
2146 | ) | |
2147 | return errormsg | |
2148 | ||
2149 | if "networkLinkStates" in area_lsa: | |
2150 | for lsa in area_lsa["networkLinkStates"]: | |
2151 | for netlsa in show_ospf_json["areas"][ospf_area][ | |
2152 | "networkLinkStates" | |
2153 | ]: | |
2154 | if ( | |
2155 | lsa | |
2156 | in show_ospf_json["areas"][ospf_area][ | |
2157 | "networkLinkStates" | |
2158 | ] | |
2159 | ): | |
2160 | if ( | |
2161 | lsa["lsaId"] == netlsa["lsaId"] | |
2162 | and lsa["advertisedRouter"] | |
2163 | == netlsa["advertisedRouter"] | |
2164 | ): | |
2165 | result = True | |
2166 | break | |
2167 | if result: | |
2168 | logger.info( | |
2169 | "[DUT: %s] OSPF LSDB area %s:Network " "LSA %s", | |
2170 | router, | |
2171 | ospf_area, | |
2172 | lsa, | |
2173 | ) | |
2174 | break | |
2175 | else: | |
2176 | errormsg = ( | |
2177 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2178 | " Network LSA is {}".format(router, ospf_area, lsa) | |
2179 | ) | |
2180 | return errormsg | |
2181 | ||
2182 | if "summaryLinkStates" in area_lsa: | |
2183 | for lsa in area_lsa["summaryLinkStates"]: | |
2184 | for t3lsa in show_ospf_json["areas"][ospf_area][ | |
2185 | "summaryLinkStates" | |
2186 | ]: | |
2187 | if ( | |
2188 | lsa["lsaId"] == t3lsa["lsaId"] | |
2189 | and lsa["advertisedRouter"] == t3lsa["advertisedRouter"] | |
2190 | ): | |
2191 | result = True | |
2192 | break | |
2193 | if result: | |
2194 | logger.info( | |
2195 | "[DUT: %s] OSPF LSDB area %s:Summary " "LSA %s", | |
2196 | router, | |
2197 | ospf_area, | |
2198 | lsa, | |
2199 | ) | |
2200 | break | |
2201 | else: | |
2202 | errormsg = ( | |
2203 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2204 | " Summary LSA is {}".format(router, ospf_area, lsa) | |
2205 | ) | |
2206 | return errormsg | |
2207 | ||
2208 | if "nssaExternalLinkStates" in area_lsa: | |
2209 | for lsa in area_lsa["nssaExternalLinkStates"]: | |
2210 | for t7lsa in show_ospf_json["areas"][ospf_area][ | |
2211 | "nssaExternalLinkStates" | |
2212 | ]: | |
2213 | if ( | |
2214 | lsa["lsaId"] == t7lsa["lsaId"] | |
2215 | and lsa["advertisedRouter"] == t7lsa["advertisedRouter"] | |
2216 | ): | |
2217 | result = True | |
2218 | break | |
2219 | if result: | |
2220 | logger.info( | |
2221 | "[DUT: %s] OSPF LSDB area %s:Type7 " "LSA %s", | |
2222 | router, | |
2223 | ospf_area, | |
2224 | lsa, | |
2225 | ) | |
2226 | break | |
2227 | else: | |
2228 | errormsg = ( | |
2229 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2230 | " Type7 LSA is {}".format(router, ospf_area, lsa) | |
2231 | ) | |
2232 | return errormsg | |
2233 | ||
2234 | if "asbrSummaryLinkStates" in area_lsa: | |
2235 | for lsa in area_lsa["asbrSummaryLinkStates"]: | |
2236 | for t4lsa in show_ospf_json["areas"][ospf_area][ | |
2237 | "asbrSummaryLinkStates" | |
2238 | ]: | |
2239 | if ( | |
2240 | lsa["lsaId"] == t4lsa["lsaId"] | |
2241 | and lsa["advertisedRouter"] == t4lsa["advertisedRouter"] | |
2242 | ): | |
2243 | result = True | |
2244 | break | |
2245 | if result: | |
2246 | logger.info( | |
2247 | "[DUT: %s] OSPF LSDB area %s:ASBR Summary " "LSA %s", | |
2248 | router, | |
2249 | ospf_area, | |
2250 | lsa, | |
2251 | ) | |
2252 | result = True | |
2253 | else: | |
2254 | errormsg = ( | |
2255 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2256 | " ASBR Summary LSA is {}".format(router, ospf_area, lsa) | |
2257 | ) | |
2258 | return errormsg | |
2259 | ||
2260 | if "linkLocalOpaqueLsa" in area_lsa: | |
2261 | for lsa in area_lsa["linkLocalOpaqueLsa"]: | |
2262 | try: | |
2263 | for lnklsa in show_ospf_json["areas"][ospf_area][ | |
2264 | "linkLocalOpaqueLsa" | |
2265 | ]: | |
2266 | if ( | |
2267 | lsa["lsaId"] in lnklsa["lsaId"] | |
2268 | and "linkLocalOpaqueLsa" | |
2269 | in show_ospf_json["areas"][ospf_area] | |
2270 | ): | |
2271 | logger.info( | |
2272 | ( | |
2273 | "[DUT: FRR] OSPF LSDB area %s:Opaque-LSA" | |
2274 | "%s", | |
2275 | ospf_area, | |
2276 | lsa, | |
2277 | ) | |
2278 | ) | |
2279 | result = True | |
2280 | else: | |
2281 | errormsg = ( | |
2282 | "[DUT: FRR] OSPF LSDB area: {} " | |
2283 | "expected Opaque-LSA is {}, Found is {}".format( | |
2284 | ospf_area, lsa, show_ospf_json | |
2285 | ) | |
2286 | ) | |
2287 | raise ValueError(errormsg) | |
2288 | return errormsg | |
2289 | except KeyError: | |
2290 | errormsg = "[DUT: FRR] linkLocalOpaqueLsa Not " "present" | |
2291 | return errormsg | |
2292 | ||
2293 | if ospf_external_lsa: | |
2294 | for lsa in ospf_external_lsa: | |
2295 | try: | |
2296 | for t5lsa in show_ospf_json["asExternalLinkStates"]: | |
2297 | if ( | |
2298 | lsa["lsaId"] == t5lsa["lsaId"] | |
2299 | and lsa["advertisedRouter"] == t5lsa["advertisedRouter"] | |
2300 | ): | |
2301 | result = True | |
2302 | break | |
2303 | except KeyError: | |
2304 | result = False | |
2305 | if result: | |
2306 | logger.info("[DUT: %s] OSPF LSDB:External LSA %s", router, lsa) | |
2307 | result = True | |
2308 | else: | |
2309 | errormsg = ( | |
2310 | "[DUT: {}] OSPF LSDB : expected" | |
2311 | " External LSA is {}".format(router, lsa) | |
2312 | ) | |
2313 | return errormsg | |
2314 | ||
2315 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
2316 | return result | |
2317 | ||
2318 | ||
2319 | def config_ospf6_interface( | |
2320 | tgen, topo=None, input_dict=None, build=False, load_config=True | |
2321 | ): | |
2322 | """ | |
2323 | API to configure ospf on router. | |
2324 | ||
2325 | Parameters | |
2326 | ---------- | |
2327 | * `tgen` : Topogen object | |
2328 | * `topo` : json file data | |
2329 | * `input_dict` : Input dict data, required when configuring from testcase | |
2330 | * `build` : Only for initial setup phase this is set as True. | |
2331 | * `load_config` : Loading the config to router this is set as True. | |
2332 | ||
2333 | Usage | |
2334 | ----- | |
2335 | r1_ospf_auth = { | |
2336 | "r1": { | |
2337 | "links": { | |
2338 | "r2": { | |
2339 | "ospf": { | |
2340 | "authentication": 'message-digest', | |
2341 | "authentication-key": "ospf", | |
2342 | "message-digest-key": "10" | |
2343 | } | |
2344 | } | |
2345 | } | |
2346 | } | |
2347 | } | |
2348 | result = config_ospf6_interface(tgen, topo, r1_ospf_auth) | |
2349 | ||
2350 | Returns | |
2351 | ------- | |
2352 | True or False | |
2353 | """ | |
2354 | ||
2355 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
2356 | result = False | |
2357 | if topo is None: | |
2358 | topo = tgen.json_topo | |
2359 | ||
2360 | if not input_dict: | |
2361 | input_dict = deepcopy(topo) | |
2362 | else: | |
2363 | input_dict = deepcopy(input_dict) | |
2364 | ||
2365 | config_data_dict = {} | |
2366 | ||
2367 | for router in input_dict.keys(): | |
2368 | config_data = [] | |
2369 | for lnk in input_dict[router]["links"].keys(): | |
2370 | if "ospf6" not in input_dict[router]["links"][lnk]: | |
2371 | logger.debug( | |
2372 | "Router %s: ospf6 config is not present in" | |
2373 | "input_dict, passed input_dict %s", | |
2374 | router, | |
2375 | str(input_dict), | |
2376 | ) | |
2377 | continue | |
2378 | ospf_data = input_dict[router]["links"][lnk]["ospf6"] | |
2379 | data_ospf_area = ospf_data.setdefault("area", None) | |
2380 | data_ospf_auth = ospf_data.setdefault("hash-algo", None) | |
2381 | data_ospf_keychain = ospf_data.setdefault("keychain", None) | |
2382 | data_ospf_dr_priority = ospf_data.setdefault("priority", None) | |
2383 | data_ospf_cost = ospf_data.setdefault("cost", None) | |
2384 | data_ospf_mtu = ospf_data.setdefault("mtu_ignore", None) | |
2385 | ||
2386 | try: | |
2387 | intf = topo["routers"][router]["links"][lnk]["interface"] | |
2388 | except KeyError: | |
2389 | intf = topo["switches"][router]["links"][lnk]["interface"] | |
2390 | ||
2391 | # interface | |
2392 | cmd = "interface {}".format(intf) | |
2393 | ||
2394 | config_data.append(cmd) | |
2395 | # interface area config | |
2396 | if data_ospf_area: | |
2397 | cmd = "ipv6 ospf area {}".format(data_ospf_area) | |
2398 | config_data.append(cmd) | |
2399 | ||
2400 | # interface ospf auth | |
2401 | if data_ospf_auth: | |
2402 | cmd = "ipv6 ospf6 authentication" | |
2403 | ||
2404 | if "del_action" in ospf_data: | |
2405 | cmd = "no {}".format(cmd) | |
2406 | ||
2407 | if "hash-algo" in ospf_data: | |
2408 | cmd = "{} key-id {} hash-algo {} key {}".format( | |
2409 | cmd, | |
2410 | ospf_data["key-id"], | |
2411 | ospf_data["hash-algo"], | |
2412 | ospf_data["key"], | |
2413 | ) | |
2414 | config_data.append(cmd) | |
2415 | ||
2416 | # interface ospf auth with keychain | |
2417 | if data_ospf_keychain: | |
2418 | cmd = "ipv6 ospf6 authentication" | |
2419 | ||
2420 | if "del_action" in ospf_data: | |
2421 | cmd = "no {}".format(cmd) | |
2422 | ||
2423 | if "keychain" in ospf_data: | |
2424 | cmd = "{} keychain {}".format(cmd, ospf_data["keychain"]) | |
2425 | config_data.append(cmd) | |
2426 | ||
2427 | # interface ospf dr priority | |
2428 | if data_ospf_dr_priority: | |
2429 | cmd = "ipv6 ospf priority {}".format(ospf_data["priority"]) | |
2430 | if "del_action" in ospf_data: | |
2431 | cmd = "no {}".format(cmd) | |
2432 | config_data.append(cmd) | |
2433 | ||
2434 | # interface ospf cost | |
2435 | if data_ospf_cost: | |
2436 | cmd = "ipv6 ospf cost {}".format(ospf_data["cost"]) | |
2437 | if "del_action" in ospf_data: | |
2438 | cmd = "no {}".format(cmd) | |
2439 | config_data.append(cmd) | |
2440 | ||
2441 | # interface ospf mtu | |
2442 | if data_ospf_mtu: | |
2443 | cmd = "ipv6 ospf mtu-ignore" | |
2444 | if "del_action" in ospf_data: | |
2445 | cmd = "no {}".format(cmd) | |
2446 | config_data.append(cmd) | |
2447 | ||
2448 | if build: | |
2449 | return config_data | |
2450 | ||
2451 | if config_data: | |
2452 | config_data_dict[router] = config_data | |
2453 | ||
2454 | result = create_common_configurations( | |
2455 | tgen, config_data_dict, "interface_config", build=build | |
2456 | ) | |
2457 | ||
2458 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
2459 | return result | |
2460 | ||
2461 | ||
2462 | @retry(retry_timeout=20) | |
2463 | def verify_ospf_gr_helper(tgen, topo, dut, input_dict=None): | |
2464 | """ | |
2465 | This API is used to vreify gr helper using command | |
2466 | show ip ospf graceful-restart helper | |
2467 | ||
2468 | Parameters | |
2469 | ---------- | |
2470 | * `tgen` : Topogen object | |
2471 | * `topo` : topology descriptions | |
2472 | * 'dut' : router | |
2473 | * 'input_dict' - values to be verified | |
2474 | ||
2475 | Usage: | |
2476 | ------- | |
2477 | input_dict = { | |
2478 | "helperSupport":"Disabled", | |
2479 | "strictLsaCheck":"Enabled", | |
2480 | "restartSupoort":"Planned and Unplanned Restarts", | |
2481 | "supportedGracePeriod":1800 | |
2482 | } | |
2483 | result = verify_ospf_gr_helper(tgen, topo, dut, input_dict) | |
2484 | ||
2485 | """ | |
2486 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
2487 | result = False | |
2488 | ||
2489 | if "ospf" not in topo["routers"][dut]: | |
2490 | errormsg = "[DUT: {}] OSPF is not configured on the router.".format(dut) | |
2491 | return errormsg | |
2492 | ||
2493 | rnode = tgen.routers()[dut] | |
2494 | logger.info("Verifying OSPF GR details on router %s:", dut) | |
2495 | show_ospf_json = run_frr_cmd( | |
2496 | rnode, "show ip ospf graceful-restart helper json", isjson=True | |
2497 | ) | |
2498 | ||
2499 | # Verifying output dictionary show_ospf_json is empty or not | |
2500 | if not bool(show_ospf_json): | |
2501 | errormsg = "OSPF is not running" | |
2502 | raise ValueError(errormsg) | |
2503 | return errormsg | |
2504 | ||
2505 | for ospf_gr, gr_data in input_dict.items(): | |
2506 | try: | |
2507 | if input_dict[ospf_gr] == show_ospf_json[ospf_gr]: | |
2508 | logger.info( | |
2509 | "[DUT: FRR] OSPF GR Helper: %s is %s", | |
2510 | ospf_gr, | |
2511 | show_ospf_json[ospf_gr], | |
2512 | ) | |
2513 | result = True | |
2514 | else: | |
2515 | errormsg = ( | |
2516 | "[DUT: FRR] OSPF GR Helper: {} expected is {}, Found " | |
2517 | "is {}".format( | |
2518 | ospf_gr, input_dict[ospf_gr], show_ospf_json[ospf_gr] | |
2519 | ) | |
2520 | ) | |
2521 | raise ValueError(errormsg) | |
2522 | return errormsg | |
2523 | ||
2524 | except KeyError: | |
2525 | errormsg = "[DUT: FRR] OSPF GR Helper: {}".format(ospf_gr) | |
2526 | return errormsg | |
2527 | ||
2528 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
2529 | return result | |
2530 | ||
2531 | ||
2532 | def get_ospf_database(tgen, topo, dut, input_dict, vrf=None, lsatype=None, rid=None): | |
2533 | """ | |
2534 | This API is to return ospf lsa's by running | |
2535 | show ip ospf database command. | |
2536 | ||
2537 | Parameters | |
2538 | ---------- | |
2539 | * `tgen` : Topogen object | |
2540 | * `dut`: device under test | |
2541 | * `input_dict` : Input dict data, required when configuring from testcase | |
2542 | * `topo` : next to be verified | |
2543 | * `vrf` : vrf to be checked | |
2544 | * `lsatype` : type of lsa to be checked | |
2545 | * `rid` : router id for lsa to be checked | |
2546 | Usage | |
2547 | ----- | |
2548 | input_dict = { | |
2549 | "areas": { | |
2550 | "0.0.0.0": { | |
2551 | "routerLinkStates": { | |
2552 | "100.1.1.0-100.1.1.0": { | |
2553 | "LSID": "100.1.1.0", | |
2554 | "Advertised router": "100.1.1.0", | |
2555 | "LSA Age": 130, | |
2556 | "Sequence Number": "80000006", | |
2557 | "Checksum": "a703", | |
2558 | "Router links": 3 | |
2559 | } | |
2560 | }, | |
2561 | "networkLinkStates": { | |
2562 | "10.0.0.2-100.1.1.1": { | |
2563 | "LSID": "10.0.0.2", | |
2564 | "Advertised router": "100.1.1.1", | |
2565 | "LSA Age": 137, | |
2566 | "Sequence Number": "80000001", | |
2567 | "Checksum": "9583" | |
2568 | } | |
2569 | }, | |
2570 | }, | |
2571 | } | |
2572 | } | |
2573 | result = get_ospf_database(tgen, topo, dut, input_dict) | |
2574 | ||
2575 | Returns | |
2576 | ------- | |
2577 | True or False (Error Message) | |
2578 | """ | |
2579 | ||
2580 | result = False | |
2581 | router = dut | |
2582 | logger.debug("Entering lib API: {}".format(sys._getframe().f_code.co_name)) | |
2583 | sleep(10) | |
2584 | if "ospf" not in topo["routers"][dut]: | |
2585 | errormsg = "[DUT: {}] OSPF is not configured on the router.".format(dut) | |
2586 | return errormsg | |
2587 | ||
2588 | rnode = tgen.routers()[dut] | |
2589 | ||
2590 | logger.info("Verifying OSPF interface on router %s:", dut) | |
2591 | if not rid: | |
2592 | rid = "self-originate" | |
2593 | if lsatype: | |
2594 | if vrf is None: | |
2595 | command = "show ip ospf database {} {} json".format(lsatype, rid) | |
2596 | else: | |
2597 | command = "show ip ospf database {} {} vrf {} json".format( | |
2598 | lsatype, rid, vrf | |
2599 | ) | |
2600 | else: | |
2601 | if vrf is None: | |
2602 | command = "show ip ospf database json" | |
2603 | else: | |
2604 | command = "show ip ospf database vrf {} json".format(vrf) | |
2605 | ||
2606 | show_ospf_json = run_frr_cmd(rnode, command, isjson=True) | |
2607 | # Verifying output dictionary show_ospf_json is empty or not | |
2608 | if not bool(show_ospf_json): | |
2609 | errormsg = "OSPF is not running" | |
2610 | return errormsg | |
2611 | ||
2612 | # for inter and inter lsa's | |
2613 | ospf_db_data = input_dict.setdefault("areas", None) | |
2614 | ospf_external_lsa = input_dict.setdefault("asExternalLinkStates", None) | |
2615 | ||
2616 | if ospf_db_data: | |
2617 | for ospf_area, area_lsa in ospf_db_data.items(): | |
2618 | if "areas" in show_ospf_json and ospf_area in show_ospf_json["areas"]: | |
2619 | if "routerLinkStates" in area_lsa: | |
2620 | for lsa in area_lsa["routerLinkStates"]: | |
2621 | for rtrlsa in show_ospf_json["areas"][ospf_area][ | |
2622 | "routerLinkStates" | |
2623 | ]: | |
2624 | _advrtr = lsa.setdefault("advertisedRouter", None) | |
2625 | _options = lsa.setdefault("options", None) | |
2626 | if ( | |
2627 | _advrtr | |
2628 | and lsa["lsaId"] == rtrlsa["lsaId"] | |
2629 | and lsa["advertisedRouter"] | |
2630 | == rtrlsa["advertisedRouter"] | |
2631 | ): | |
2632 | result = True | |
2633 | break | |
2634 | if ( | |
2635 | _options | |
2636 | and lsa["lsaId"] == rtrlsa["lsaId"] | |
2637 | and lsa["options"] == rtrlsa["options"] | |
2638 | ): | |
2639 | result = True | |
2640 | break | |
2641 | ||
2642 | if result: | |
2643 | logger.info( | |
2644 | "[DUT: %s] OSPF LSDB area %s:Router " "LSA %s", | |
2645 | router, | |
2646 | ospf_area, | |
2647 | lsa, | |
2648 | ) | |
2649 | break | |
2650 | else: | |
2651 | errormsg = ( | |
2652 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2653 | " Router LSA is {}\n found Router LSA: {}".format( | |
2654 | router, ospf_area, lsa, rtrlsa | |
2655 | ) | |
2656 | ) | |
2657 | return errormsg | |
2658 | ||
2659 | if "networkLinkStates" in area_lsa: | |
2660 | for lsa in area_lsa["networkLinkStates"]: | |
2661 | for netlsa in show_ospf_json["areas"][ospf_area][ | |
2662 | "networkLinkStates" | |
2663 | ]: | |
2664 | if ( | |
2665 | lsa | |
2666 | in show_ospf_json["areas"][ospf_area][ | |
2667 | "networkLinkStates" | |
2668 | ] | |
2669 | ): | |
2670 | if ( | |
2671 | lsa["lsaId"] == netlsa["lsaId"] | |
2672 | and lsa["advertisedRouter"] | |
2673 | == netlsa["advertisedRouter"] | |
2674 | ): | |
2675 | result = True | |
2676 | break | |
2677 | if result: | |
2678 | logger.info( | |
2679 | "[DUT: %s] OSPF LSDB area %s:Network " "LSA %s", | |
2680 | router, | |
2681 | ospf_area, | |
2682 | lsa, | |
2683 | ) | |
2684 | break | |
2685 | else: | |
2686 | errormsg = ( | |
2687 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2688 | " Network LSA is {}".format(router, ospf_area, lsa) | |
2689 | ) | |
2690 | return errormsg | |
2691 | ||
2692 | if "summaryLinkStates" in area_lsa: | |
2693 | for lsa in area_lsa["summaryLinkStates"]: | |
2694 | for t3lsa in show_ospf_json["areas"][ospf_area][ | |
2695 | "summaryLinkStates" | |
2696 | ]: | |
2697 | if ( | |
2698 | lsa["lsaId"] == t3lsa["lsaId"] | |
2699 | and lsa["advertisedRouter"] == t3lsa["advertisedRouter"] | |
2700 | ): | |
2701 | result = True | |
2702 | break | |
2703 | if result: | |
2704 | logger.info( | |
2705 | "[DUT: %s] OSPF LSDB area %s:Summary " "LSA %s", | |
2706 | router, | |
2707 | ospf_area, | |
2708 | lsa, | |
2709 | ) | |
2710 | break | |
2711 | else: | |
2712 | errormsg = ( | |
2713 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2714 | " Summary LSA is {}".format(router, ospf_area, lsa) | |
2715 | ) | |
2716 | return errormsg | |
2717 | ||
2718 | if "nssaExternalLinkStates" in area_lsa: | |
2719 | for lsa in area_lsa["nssaExternalLinkStates"]: | |
2720 | for t7lsa in show_ospf_json["areas"][ospf_area][ | |
2721 | "nssaExternalLinkStates" | |
2722 | ]: | |
2723 | if ( | |
2724 | lsa["lsaId"] == t7lsa["lsaId"] | |
2725 | and lsa["advertisedRouter"] == t7lsa["advertisedRouter"] | |
2726 | ): | |
2727 | result = True | |
2728 | break | |
2729 | if result: | |
2730 | logger.info( | |
2731 | "[DUT: %s] OSPF LSDB area %s:Type7 " "LSA %s", | |
2732 | router, | |
2733 | ospf_area, | |
2734 | lsa, | |
2735 | ) | |
2736 | break | |
2737 | else: | |
2738 | errormsg = ( | |
2739 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2740 | " Type7 LSA is {}".format(router, ospf_area, lsa) | |
2741 | ) | |
2742 | return errormsg | |
2743 | ||
2744 | if "asbrSummaryLinkStates" in area_lsa: | |
2745 | for lsa in area_lsa["asbrSummaryLinkStates"]: | |
2746 | for t4lsa in show_ospf_json["areas"][ospf_area][ | |
2747 | "asbrSummaryLinkStates" | |
2748 | ]: | |
2749 | if ( | |
2750 | lsa["lsaId"] == t4lsa["lsaId"] | |
2751 | and lsa["advertisedRouter"] == t4lsa["advertisedRouter"] | |
2752 | ): | |
2753 | result = True | |
2754 | break | |
2755 | if result: | |
2756 | logger.info( | |
2757 | "[DUT: %s] OSPF LSDB area %s:ASBR Summary " "LSA %s", | |
2758 | router, | |
2759 | ospf_area, | |
2760 | lsa, | |
2761 | ) | |
2762 | result = True | |
2763 | else: | |
2764 | errormsg = ( | |
2765 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2766 | " ASBR Summary LSA is {}".format(router, ospf_area, lsa) | |
2767 | ) | |
2768 | return errormsg | |
2769 | ||
2770 | if "linkLocalOpaqueLsa" in area_lsa: | |
2771 | for lsa in area_lsa["linkLocalOpaqueLsa"]: | |
2772 | try: | |
2773 | for lnklsa in show_ospf_json["areas"][ospf_area][ | |
2774 | "linkLocalOpaqueLsa" | |
2775 | ]: | |
2776 | if ( | |
2777 | lsa["lsaId"] in lnklsa["lsaId"] | |
2778 | and "linkLocalOpaqueLsa" | |
2779 | in show_ospf_json["areas"][ospf_area] | |
2780 | ): | |
2781 | logger.info( | |
2782 | ( | |
2783 | "[DUT: FRR] OSPF LSDB area %s:Opaque-LSA" | |
2784 | "%s", | |
2785 | ospf_area, | |
2786 | lsa, | |
2787 | ) | |
2788 | ) | |
2789 | result = True | |
2790 | else: | |
2791 | errormsg = ( | |
2792 | "[DUT: FRR] OSPF LSDB area: {} " | |
2793 | "expected Opaque-LSA is {}, Found is {}".format( | |
2794 | ospf_area, lsa, show_ospf_json | |
2795 | ) | |
2796 | ) | |
2797 | raise ValueError(errormsg) | |
2798 | return errormsg | |
2799 | except KeyError: | |
2800 | errormsg = "[DUT: FRR] linkLocalOpaqueLsa Not " "present" | |
2801 | return errormsg | |
2802 | else: | |
2803 | if "routerLinkStates" in area_lsa: | |
2804 | for lsa in area_lsa["routerLinkStates"]: | |
2805 | for rtrlsa in show_ospf_json["routerLinkStates"]: | |
2806 | _advrtr = lsa.setdefault("advertisedRouter", None) | |
2807 | _options = lsa.setdefault("options", None) | |
2808 | _age = lsa.setdefault("lsaAge", None) | |
2809 | if ( | |
2810 | _options | |
2811 | and lsa["options"] | |
2812 | == show_ospf_json["routerLinkStates"][rtrlsa][ | |
2813 | ospf_area | |
2814 | ][0]["options"] | |
2815 | ): | |
2816 | result = True | |
2817 | break | |
2818 | if ( | |
2819 | _age is not "get" | |
2820 | and lsa["lsaAge"] | |
2821 | == show_ospf_json["routerLinkStates"][rtrlsa][ | |
2822 | ospf_area | |
2823 | ][0]["lsaAge"] | |
2824 | ): | |
2825 | result = True | |
2826 | break | |
2827 | ||
2828 | if _age == "get": | |
2829 | return "{}".format( | |
2830 | show_ospf_json["routerLinkStates"][rtrlsa][ | |
2831 | ospf_area | |
2832 | ][0]["lsaAge"] | |
2833 | ) | |
2834 | if result: | |
2835 | logger.info( | |
2836 | "[DUT: %s] OSPF LSDB area %s:Router " "LSA %s", | |
2837 | router, | |
2838 | ospf_area, | |
2839 | lsa, | |
2840 | ) | |
2841 | break | |
2842 | else: | |
2843 | errormsg = ( | |
2844 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2845 | " Router LSA is {}\n found Router LSA: {}".format( | |
2846 | router, | |
2847 | ospf_area, | |
2848 | lsa, | |
2849 | show_ospf_json["routerLinkStates"], | |
2850 | ) | |
2851 | ) | |
2852 | return errormsg | |
2853 | ||
2854 | if "networkLinkStates" in area_lsa: | |
2855 | for lsa in area_lsa["networkLinkStates"]: | |
2856 | for netlsa in show_ospf_json["areas"][ospf_area][ | |
2857 | "networkLinkStates" | |
2858 | ]: | |
2859 | if ( | |
2860 | lsa | |
2861 | in show_ospf_json["areas"][ospf_area][ | |
2862 | "networkLinkStates" | |
2863 | ] | |
2864 | ): | |
2865 | if ( | |
2866 | lsa["lsaId"] == netlsa["lsaId"] | |
2867 | and lsa["advertisedRouter"] | |
2868 | == netlsa["advertisedRouter"] | |
2869 | ): | |
2870 | result = True | |
2871 | break | |
2872 | if result: | |
2873 | logger.info( | |
2874 | "[DUT: %s] OSPF LSDB area %s:Network " "LSA %s", | |
2875 | router, | |
2876 | ospf_area, | |
2877 | lsa, | |
2878 | ) | |
2879 | break | |
2880 | else: | |
2881 | errormsg = ( | |
2882 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2883 | " Network LSA is {}".format(router, ospf_area, lsa) | |
2884 | ) | |
2885 | return errormsg | |
2886 | ||
2887 | if "summaryLinkStates" in area_lsa: | |
2888 | for lsa in area_lsa["summaryLinkStates"]: | |
2889 | for t3lsa in show_ospf_json["areas"][ospf_area][ | |
2890 | "summaryLinkStates" | |
2891 | ]: | |
2892 | if ( | |
2893 | lsa["lsaId"] == t3lsa["lsaId"] | |
2894 | and lsa["advertisedRouter"] == t3lsa["advertisedRouter"] | |
2895 | ): | |
2896 | result = True | |
2897 | break | |
2898 | if result: | |
2899 | logger.info( | |
2900 | "[DUT: %s] OSPF LSDB area %s:Summary " "LSA %s", | |
2901 | router, | |
2902 | ospf_area, | |
2903 | lsa, | |
2904 | ) | |
2905 | break | |
2906 | else: | |
2907 | errormsg = ( | |
2908 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2909 | " Summary LSA is {}".format(router, ospf_area, lsa) | |
2910 | ) | |
2911 | return errormsg | |
2912 | ||
2913 | if "nssaExternalLinkStates" in area_lsa: | |
2914 | for lsa in area_lsa["nssaExternalLinkStates"]: | |
2915 | for t7lsa in show_ospf_json["areas"][ospf_area][ | |
2916 | "nssaExternalLinkStates" | |
2917 | ]: | |
2918 | if ( | |
2919 | lsa["lsaId"] == t7lsa["lsaId"] | |
2920 | and lsa["advertisedRouter"] == t7lsa["advertisedRouter"] | |
2921 | ): | |
2922 | result = True | |
2923 | break | |
2924 | if result: | |
2925 | logger.info( | |
2926 | "[DUT: %s] OSPF LSDB area %s:Type7 " "LSA %s", | |
2927 | router, | |
2928 | ospf_area, | |
2929 | lsa, | |
2930 | ) | |
2931 | break | |
2932 | else: | |
2933 | errormsg = ( | |
2934 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2935 | " Type7 LSA is {}".format(router, ospf_area, lsa) | |
2936 | ) | |
2937 | return errormsg | |
2938 | ||
2939 | if "asbrSummaryLinkStates" in area_lsa: | |
2940 | for lsa in area_lsa["asbrSummaryLinkStates"]: | |
2941 | for t4lsa in show_ospf_json["areas"][ospf_area][ | |
2942 | "asbrSummaryLinkStates" | |
2943 | ]: | |
2944 | if ( | |
2945 | lsa["lsaId"] == t4lsa["lsaId"] | |
2946 | and lsa["advertisedRouter"] == t4lsa["advertisedRouter"] | |
2947 | ): | |
2948 | result = True | |
2949 | break | |
2950 | if result: | |
2951 | logger.info( | |
2952 | "[DUT: %s] OSPF LSDB area %s:ASBR Summary " "LSA %s", | |
2953 | router, | |
2954 | ospf_area, | |
2955 | lsa, | |
2956 | ) | |
2957 | result = True | |
2958 | else: | |
2959 | errormsg = ( | |
2960 | "[DUT: {}] OSPF LSDB area {}: expected" | |
2961 | " ASBR Summary LSA is {}".format(router, ospf_area, lsa) | |
2962 | ) | |
2963 | return errormsg | |
2964 | ||
2965 | if "linkLocalOpaqueLsa" in area_lsa: | |
2966 | for lsa in area_lsa["linkLocalOpaqueLsa"]: | |
2967 | try: | |
2968 | for lnklsa in show_ospf_json["areas"][ospf_area][ | |
2969 | "linkLocalOpaqueLsa" | |
2970 | ]: | |
2971 | if ( | |
2972 | lsa["lsaId"] in lnklsa["lsaId"] | |
2973 | and "linkLocalOpaqueLsa" | |
2974 | in show_ospf_json["areas"][ospf_area] | |
2975 | ): | |
2976 | logger.info( | |
2977 | ( | |
2978 | "[DUT: FRR] OSPF LSDB area %s:Opaque-LSA" | |
2979 | "%s", | |
2980 | ospf_area, | |
2981 | lsa, | |
2982 | ) | |
2983 | ) | |
2984 | result = True | |
2985 | else: | |
2986 | errormsg = ( | |
2987 | "[DUT: FRR] OSPF LSDB area: {} " | |
2988 | "expected Opaque-LSA is {}, Found is {}".format( | |
2989 | ospf_area, lsa, show_ospf_json | |
2990 | ) | |
2991 | ) | |
2992 | raise ValueError(errormsg) | |
2993 | return errormsg | |
2994 | except KeyError: | |
2995 | errormsg = "[DUT: FRR] linkLocalOpaqueLsa Not " "present" | |
2996 | return errormsg | |
2997 | ||
2998 | if ospf_external_lsa: | |
2999 | for lsa in ospf_external_lsa: | |
3000 | try: | |
3001 | for t5lsa in show_ospf_json["asExternalLinkStates"]: | |
3002 | if ( | |
3003 | lsa["lsaId"] == t5lsa["lsaId"] | |
3004 | and lsa["advertisedRouter"] == t5lsa["advertisedRouter"] | |
3005 | ): | |
3006 | result = True | |
3007 | break | |
3008 | except KeyError: | |
3009 | result = False | |
3010 | if result: | |
3011 | logger.info("[DUT: %s] OSPF LSDB:External LSA %s", router, lsa) | |
3012 | result = True | |
3013 | else: | |
3014 | errormsg = ( | |
3015 | "[DUT: {}] OSPF LSDB : expected" | |
3016 | " External LSA is {}".format(router, lsa) | |
3017 | ) | |
3018 | return errormsg | |
3019 | ||
3020 | logger.debug("Exiting lib API: {}".format(sys._getframe().f_code.co_name)) | |
3021 | return result |