Skip to content

Commit ef8326c

Browse files
authored
Merge pull request #1667 from napalm-automation/xr_getfacts
Improving iosxr get_facts behavior to handle XML changes in newer OS versions
2 parents d2f9fc5 + 9f41701 commit ef8326c

8 files changed

+598
-11
lines changed

Diff for: napalm/iosxr/iosxr.py

+53-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from napalm.pyIOSXR.exceptions import ConnectError
3030
from napalm.pyIOSXR.exceptions import TimeoutError
3131
from napalm.pyIOSXR.exceptions import InvalidInputError
32+
from napalm.pyIOSXR.exceptions import XMLCLIError
3233

3334
# import NAPALM base
3435
import napalm.base.helpers
@@ -165,16 +166,60 @@ def get_facts(self):
165166
"interface_list": [],
166167
}
167168

168-
facts_rpc_request = (
169-
"<Get><Operational><SystemTime/><PlatformInventory><RackTable>"
170-
"<Rack><Naming><Name>0</Name></Naming>"
171-
"<Attributes><BasicInfo/></Attributes>"
172-
"</Rack></RackTable></PlatformInventory></Operational></Get>"
173-
)
169+
facts_rpc_request = """
170+
<Get>
171+
<Operational>
172+
<SystemTime/>
173+
<PlatformInventory>
174+
<RackTable>
175+
<Rack>
176+
<Naming>
177+
<Name>0</Name>
178+
</Naming>
179+
<Attributes>
180+
<BasicInfo/>
181+
</Attributes>
182+
</Rack>
183+
</RackTable>
184+
</PlatformInventory>
185+
</Operational>
186+
</Get>
187+
"""
188+
189+
# IOS-XR 7.3.3 and possibly other 7.X versions have this located in
190+
# different location in the XML tree
191+
facts_rpc_request_alt = """
192+
<Get>
193+
<Operational>
194+
<SystemTime/>
195+
<Inventory>
196+
<Entities>
197+
<Entity>
198+
<Naming>
199+
<Name>Rack 0</Name>
200+
</Naming>
201+
<Attributes>
202+
<InvBasicBag></InvBasicBag>
203+
</Attributes>
204+
</Entity>
205+
</Entities>
206+
</Inventory>
207+
</Operational>
208+
</Get>
209+
"""
174210

175-
facts_rpc_reply = ETREE.fromstring(self.device.make_rpc_call(facts_rpc_request))
176211
system_time_xpath = ".//SystemTime/Uptime"
177-
platform_attr_xpath = ".//RackTable/Rack/Attributes/BasicInfo"
212+
try:
213+
facts_rpc_reply = ETREE.fromstring(
214+
self.device.make_rpc_call(facts_rpc_request)
215+
)
216+
platform_attr_xpath = ".//RackTable/Rack/Attributes/BasicInfo"
217+
except XMLCLIError:
218+
facts_rpc_reply = ETREE.fromstring(
219+
self.device.make_rpc_call(facts_rpc_request_alt)
220+
)
221+
platform_attr_xpath = ".//Entities/Entity/Attributes/InvBasicBag"
222+
178223
system_time_tree = facts_rpc_reply.xpath(system_time_xpath)[0]
179224
try:
180225
platform_attr_tree = facts_rpc_reply.xpath(platform_attr_xpath)[0]

Diff for: test/iosxr/conftest.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
import pytest
55
from napalm.base.test import conftest as parent_conftest
6-
76
from napalm.base.test.double import BaseTestDouble
8-
97
from napalm.iosxr import iosxr
8+
from napalm.pyIOSXR.exceptions import XMLCLIError
109

1110

1211
@pytest.fixture(scope="class")
@@ -54,7 +53,15 @@ def close(self):
5453

5554
def make_rpc_call(self, rpc_call, encoded=True):
5655
filename = "{}.txt".format(self.sanitize_text(rpc_call))
57-
full_path = self.find_file(filename)
56+
try:
57+
full_path = self.find_file(filename)
58+
except OSError:
59+
# Some versions of IOSXR require different form of get_facts call
60+
# so replicate the failure here (to ultimately force the other form)
61+
if "PlatformInventory" in rpc_call:
62+
raise XMLCLIError
63+
else:
64+
raise
5865
result = self.read_txt_file(full_path)
5966
if encoded:
6067
return str.encode(result)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_Get__Operational__SystemTime___PlatformInventory__RackTable__Rack__Naming__Name_0__Name___Naming__Attributes__BasicInfo____Attributes___Rack___RackTa.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_Get__Operational__SystemTime___PlatformInventory__RackTable__Rack__Naming__Name_0__Name___Naming__Attributes__BasicInfo____Attributes___Rack___RackTa.txt

0 commit comments

Comments
 (0)