Skip to content

Commit

Permalink
Merge pull request #1667 from napalm-automation/xr_getfacts
Browse files Browse the repository at this point in the history
Improving iosxr get_facts behavior to handle XML changes in newer OS versions
  • Loading branch information
mirceaulinic committed Jul 11, 2022
2 parents d2f9fc5 + 9f41701 commit ef8326c
Show file tree
Hide file tree
Showing 8 changed files with 598 additions and 11 deletions.
61 changes: 53 additions & 8 deletions napalm/iosxr/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from napalm.pyIOSXR.exceptions import ConnectError
from napalm.pyIOSXR.exceptions import TimeoutError
from napalm.pyIOSXR.exceptions import InvalidInputError
from napalm.pyIOSXR.exceptions import XMLCLIError

# import NAPALM base
import napalm.base.helpers
Expand Down Expand Up @@ -165,16 +166,60 @@ def get_facts(self):
"interface_list": [],
}

facts_rpc_request = (
"<Get><Operational><SystemTime/><PlatformInventory><RackTable>"
"<Rack><Naming><Name>0</Name></Naming>"
"<Attributes><BasicInfo/></Attributes>"
"</Rack></RackTable></PlatformInventory></Operational></Get>"
)
facts_rpc_request = """
<Get>
<Operational>
<SystemTime/>
<PlatformInventory>
<RackTable>
<Rack>
<Naming>
<Name>0</Name>
</Naming>
<Attributes>
<BasicInfo/>
</Attributes>
</Rack>
</RackTable>
</PlatformInventory>
</Operational>
</Get>
"""

# IOS-XR 7.3.3 and possibly other 7.X versions have this located in
# different location in the XML tree
facts_rpc_request_alt = """
<Get>
<Operational>
<SystemTime/>
<Inventory>
<Entities>
<Entity>
<Naming>
<Name>Rack 0</Name>
</Naming>
<Attributes>
<InvBasicBag></InvBasicBag>
</Attributes>
</Entity>
</Entities>
</Inventory>
</Operational>
</Get>
"""

facts_rpc_reply = ETREE.fromstring(self.device.make_rpc_call(facts_rpc_request))
system_time_xpath = ".//SystemTime/Uptime"
platform_attr_xpath = ".//RackTable/Rack/Attributes/BasicInfo"
try:
facts_rpc_reply = ETREE.fromstring(
self.device.make_rpc_call(facts_rpc_request)
)
platform_attr_xpath = ".//RackTable/Rack/Attributes/BasicInfo"
except XMLCLIError:
facts_rpc_reply = ETREE.fromstring(
self.device.make_rpc_call(facts_rpc_request_alt)
)
platform_attr_xpath = ".//Entities/Entity/Attributes/InvBasicBag"

system_time_tree = facts_rpc_reply.xpath(system_time_xpath)[0]
try:
platform_attr_tree = facts_rpc_reply.xpath(platform_attr_xpath)[0]
Expand Down
13 changes: 10 additions & 3 deletions test/iosxr/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

import pytest
from napalm.base.test import conftest as parent_conftest

from napalm.base.test.double import BaseTestDouble

from napalm.iosxr import iosxr
from napalm.pyIOSXR.exceptions import XMLCLIError


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

def make_rpc_call(self, rpc_call, encoded=True):
filename = "{}.txt".format(self.sanitize_text(rpc_call))
full_path = self.find_file(filename)
try:
full_path = self.find_file(filename)
except OSError:
# Some versions of IOSXR require different form of get_facts call
# so replicate the failure here (to ultimately force the other form)
if "PlatformInventory" in rpc_call:
raise XMLCLIError
else:
raise
result = self.read_txt_file(full_path)
if encoded:
return str.encode(result)
Expand Down

0 comments on commit ef8326c

Please sign in to comment.