Debian Codegraph @

lib/jnpr/junos/facts/get_route_engine_information.py

raw
1 def provides_facts():
2 """
3 Returns a dictionary keyed on the facts provided by this module. The value
4 of each key is the doc string describing the fact.
5 """
6 return {
7 "2RE": "A boolean indicating if the device has more than one "
8 "Routing Engine installed.",
9 "master": "On a single chassis/node system, a string value of "
10 "'RE0' or 'RE1' indicating which RE is master. On a "
11 "multi-chassis or multi-node system, the value is a "
12 "list of these strings indicating whether RE0 or RE1 "
13 "is master. There is one entry in the list for each "
14 "chassis/node in the system.",
15 "RE0": "A dictionary with information about RE0 (if present). The "
16 "keys of the dictionary are: mastership_state, status, "
17 "model, up_time, and last_reboot_reason.",
18 "RE1": "A dictionary with information about RE1 (if present). The "
19 "keys of the dictionary are: mastership_state, status, "
20 "model, up_time, and last_reboot_reason.",
21 "re_info": "A three-level dictionary with information about "
22 "the Routing Engines in the device. The first-level "
23 "key is the chassis or node name. The second-level key "
24 "is the slot number, the third-level keys are: "
25 "mastership_state, status, model, and "
26 "last_reboot_reason. A first-level key with a value "
27 "of 'default' will always be present and represents "
28 "the first chassis/node of the system (Note: the first "
29 "chasis/node of the system is not necessarily the "
30 "'master' node in a VC.) A second-level key with a "
31 "value of 'default' will always be present "
32 "for the default chassis/node and represents the "
33 "first Routing Engine on the first node/chassis. "
34 "(Note: the first RE of a chassis/node is not "
35 "necessarily the 'master' RE of the chassis/node. See "
36 "the RE_master fact for info on the 'master' RE of "
37 "each chassis/node.)",
38 "re_master": "A dictionary indicating which RE slot is master for "
39 "each chassis/node in the system. The dictionary key "
40 "is the chassis or node name. A key with a value "
41 "of 'default' will always be present and represents "
42 "the first node/chassis of the system. (Note: the "
43 "first chassis/node of the system is not necessarily "
44 "the 'master' node in a VC. See the vc_master fact "
45 "to determine which chassis/node is the master of "
46 "a VC.)",
47 }
48
49
50 def get_facts(device):
51 """
52 Gathers facts from the <get-route-engine-information/> RPC.
53 """
54 multi_re = None
55 master = None
56 RE0 = None
57 RE1 = None
58 re_info = None
59 re_master = None
60
61 rsp = device.rpc.get_route_engine_information(normalize=True)
62 re_list = rsp.findall(".//route-engine")
63 if len(re_list) > 1:
64 multi_re = True
65 else:
66 multi_re = False
67
68 first_node = None
69 first_slot = None
70 master_list = []
71 node_masters = {}
72 for current_re in re_list:
73 node = current_re.findtext("../../re-name", "default")
74 slot = current_re.findtext("slot", "0")
75 info = {
76 "mastership_state": current_re.findtext("mastership-state", "master"),
77 "status": current_re.findtext("status"),
78 "model": current_re.findtext("model"),
79 "last_reboot_reason": current_re.findtext("last-reboot-reason"),
80 # This key is only returned in the RE0 and RE1 facts in order
81 # to maintain backwards compatibility with the old fact
82 # gathering system. Since the up_time value changes, it's not
83 # really a "fact" and is therefore omitted from the new re_info
84 # fact.
85 "up_time": current_re.findtext("up-time"),
86 }
87 if first_node is None:
88 first_node = node
89 first_slot = slot
90 if node == first_node:
91 if slot == "0" and RE0 is None:
92 # Copy the dictionary
93 RE0 = dict(info)
94 if slot == "1" and RE1 is None:
95 # Copy the dictionary
96 RE1 = dict(info)
97 # Don't want the up_time key in the new re_info fact.
98 if "up_time" in info:
99 del info["up_time"]
100 if re_info is None:
101 re_info = {}
102 if node not in re_info:
103 re_info[node] = {}
104 # Save with second-level key as a string.
105 re_info[node][slot] = info
106 # If it's a master RE, then save in node_masters and master_list
107 if "mastership_state" in info and info["mastership_state"].lower().startswith(
108 "master"
109 ):
110 node_masters[node] = slot
111 master_list.append("RE" + slot)
112 # Fill in the 'default' first-level key if multi-chassis/node system
113 if first_node is not None and first_node != "default":
114 re_info["default"] = re_info[first_node]
115 if first_node in node_masters:
116 node_masters["default"] = node_masters[first_node]
117 # Fill in the 'default' second-level key if at least one RE was found.
118 if first_slot is not None:
119 re_info["default"]["default"] = re_info["default"][first_slot]
120
121 # Set the 'master' fact to a string or list based on the number of members.
122 master_list_len = len(master_list)
123 if master_list_len == 1:
124 master = master_list[0]
125 elif master_list_len > 1:
126 master = master_list
127
128 # If any info in the node_masters dict, then return it as fact 're_master'
129 if node_masters:
130 re_master = node_masters
131
132 return {
133 "2RE": multi_re,
134 "master": master,
135 "RE0": RE0,
136 "RE1": RE1,
137 "re_info": re_info,
138 "re_master": re_master,
139 }