Debian Codegraph @

lib/jnpr/junos/ofacts/routing_engines.py

raw
1 import re as RE
2
3
4 def _get_vc_status(dev, facts):
5 try:
6 rsp = dev.rpc.get_virtual_chassis_information()
7 # MX issue where command returns, but without content
8 if rsp is not True:
9 facts["vc_capable"] = True
10 return rsp
11 else:
12 facts["vc_capable"] = False
13 return None
14 except:
15 facts["vc_capable"] = False
16 return None
17
18
19 def facts_routing_engines(junos, facts):
20 re_facts = ["mastership-state", "status", "model", "up-time", "last-reboot-reason"]
21
22 master = []
23
24 vc_info = _get_vc_status(junos, facts)
25
26 if vc_info is not None:
27 facts["vc_mode"] = vc_info.findtext(".//virtual-chassis-mode")
28 if (
29 len(vc_info.xpath(".//virtual-chassis-id-information" "[@style='fabric']"))
30 > 0
31 ):
32 facts["vc_fabric"] = True
33 vc_list = vc_info.xpath(
34 ".//member-role[starts-with(.,'Master') " "or starts-with(.,'Backup')]"
35 )
36 if len(vc_list) > 1:
37 facts["2RE"] = True
38 for member_id in vc_info.xpath(
39 ".//member-role[starts-with(.,'Master')]" "/preceding-sibling::member-id"
40 ):
41 master.append("RE{}".format(member_id.text))
42
43 try:
44 re_info = junos.rpc.get_route_engine_information()
45 except:
46 # this means that the RPC failed. this should "never"
47 # happen, but we will trap it cleanly for now
48 return
49
50 re_list = re_info.xpath(".//route-engine")
51
52 if len(re_list) > 1:
53 facts["2RE"] = True
54
55 for re in re_list:
56 x_re_name = re.xpath("ancestor::multi-routing-engine-item/re-name")
57
58 if not x_re_name:
59 # not a multi-instance routing engine platform, but could
60 # have multiple RE slots
61 re_name = "RE"
62 x_slot = re.find("slot")
63 slot_id = x_slot.text if x_slot is not None else "0"
64 re_name = re_name + slot_id
65 else:
66 # multi-instance routing platform
67 m = RE.search(r"(\d)", x_re_name[0].text)
68 if vc_info is not None:
69 # => RE0-RE0 | RE0-RE1
70 re_name = "RE{}-RE{}".format(m.group(0), re.find("slot").text)
71 else:
72 re_name = "RE" + m.group(0) # => RE0 | RE1
73
74 re_fd = {}
75 facts[re_name] = re_fd
76 for factoid in re_facts:
77 x_f = re.find(factoid)
78 if x_f is not None:
79 re_fd[factoid.replace("-", "_")] = x_f.text
80
81 if vc_info is None and "mastership_state" in re_fd:
82 if facts[re_name]["mastership_state"] == "master":
83 master.append(re_name)
84
85 # --[ end for-each 're' ]-------------------------------------------------
86
87 len_master = len(master)
88 if len_master > 1:
89 facts["master"] = master
90 elif len_master == 1:
91 facts["master"] = master[0]