def _record_response_netconf_1_0(self) -> None: """ Record response for netconf version 1.0 Args: N/A Returns: N/A # noqa: DAR202 Raises: N/A """ if not self.failed_when_contains: self.failed = False elif not any(err in self.raw_result for err in self.failed_when_contains): self.failed = False # remove the message end characters and xml document header see: # https://github.com/scrapli/scrapli_netconf/issues/1 self.xml_result = etree.fromstring( self.raw_result.replace(b"]]>]]>", b"").replace( b'<?xml version="1.0" encoding="UTF-8"?>', b""), parser=PARSER, ) if self.strip_namespaces: self.xml_result = remove_namespaces(self.xml_result) self.result = etree.tostring(self.xml_result, pretty_print=True).decode() else: self.result = etree.tostring(self.xml_result, pretty_print=True).decode()
def _record_response_netconf_1_1(self) -> None: """ Record response for netconf version 1.1 Args: N/A Returns: N/A # noqa: DAR202 Raises: N/A """ if not self.failed_when_contains: self.failed = False elif not any(err in self.raw_result for err in self.failed_when_contains): self.failed = False result_sections = re.findall(pattern=CHUNK_MATCH_1_1, string=self.raw_result) # validate all received data for result in result_sections: expected_len = int(result[0]) result_value = result[1] # account for trailing newline char actual_len = len(result_value) - 1 if expected_len == 1: # at least nokia tends to have itty bitty chunks of one element, deal w/ that actual_len = 1 if expected_len != actual_len: LOG.critical( f"Return element length invalid, expected {expected_len} got {actual_len} for " f"element: {repr(result_value)}") self.failed = True self.xml_result = etree.fromstring( b"\n".join([ # remove the message end characters and xml document header see: # https://github.com/scrapli/scrapli_netconf/issues/1 result[1].replace(b'<?xml version="1.0" encoding="UTF-8"?>', b"") for result in result_sections ]), parser=PARSER, ) if self.strip_namespaces: self.xml_result = remove_namespaces(self.xml_result) self.result = etree.tostring(self.xml_result, pretty_print=True).decode() else: self.result = etree.tostring(self.xml_result, pretty_print=True).decode()
def test_remove_namespaces(): xml_with_namespace = """<?xml version="1.0"?> <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <data> <components xmlns="http://openconfig.net/yang/platform"> <component> <name>0/0-Virtual-Motherboard</name> <state> <description>Cisco IOS-XRv 9000 Virtual LC Motherboard</description> <name>0/0-Virtual-Motherboard</name> <type xmlns:idx="http://openconfig.net/yang/platform-types">idx:MODULE</type> <id>65538</id> </state> </component> </components> </data> </rpc-reply>""" xml_without_namespace = """<rpc-reply message-id="101"> <data> <components> <component> <name>0/0-Virtual-Motherboard</name> <state> <description>Cisco IOS-XRv 9000 Virtual LC Motherboard</description> <name>0/0-Virtual-Motherboard</name> <type>idx:MODULE</type> <id>65538</id> </state> </component> </components> </data> </rpc-reply>""" etree_with_namespace = etree.fromstring(xml_with_namespace) etree_without_namespace = remove_namespaces(tree=etree_with_namespace) assert etree.tostring( etree_without_namespace).decode() == xml_without_namespace
def _record_response_netconf_1_1(self) -> None: """ Record response for netconf version 1.1 Args: N/A Returns: N/A # noqa: DAR202 Raises: N/A """ result_sections = re.findall(pattern=CHUNK_MATCH_1_1, string=self.raw_result) # validate all received data for result in result_sections: self._validate_chunk_size_netconf_1_1(result=result) self.xml_result = etree.fromstring( b"\n".join( [ # remove the message end characters and xml document header see: # https://github.com/scrapli/scrapli_netconf/issues/1 result[1].replace(b'<?xml version="1.0" encoding="UTF-8"?>', b"") for result in result_sections ] ), parser=PARSER, ) if self.strip_namespaces: self.xml_result = remove_namespaces(self.xml_result) self.result = etree.tostring(self.xml_result, pretty_print=True).decode() else: self.result = etree.tostring(self.xml_result, pretty_print=True).decode()