Closed ldacol closed 1 year ago
from ydk.services import CodecService
from ydk.providers import CodecServiceProvider
import logging
def enable_logging(level):
log = logging.getLogger('ydk')
log.setLevel(level)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)
return log
xml_payload = """
<network-instances xmlns="http://openconfig.net/yang/network-instance">
<network-instance>
<name>default</name>
<protocols>
<protocol>
<identifier xmlns:idx="http://openconfig.net/yang/policy-types">idx:BGP</identifier>
<name>default</name>
<config>
<identifier xmlns:idx="http://openconfig.net/yang/policy-types">idx:BGP</identifier>
<name>default</name>
</config>
</protocol>
</protocols>
<config>
<name>default</name>
</config>
</network-instance>
</network-instances>
"""
def xml_to_json(xml: str) -> str:
codec = CodecService()
provider = CodecServiceProvider(type='xml')
json_provider = CodecServiceProvider(type='json')
try:
model = codec.decode(provider, xml)
json = codec.encode(json_provider, model)
return json
except Exception as err:
print(f"Exception: {err}")
return None
if __name__ == "__main__":
enable_logging(logging.INFO)
json_payload = xml_to_json(xml_payload)
if json_payload:
print(json_payload)
else:
print("Failed to transform XML payload to JSON")
/home/ubuntu/venv/bin/python3 /home/ubuntu/ydk-gen/scripts/issues/1071/xml_to_json.py
{
"openconfig-network-instance:network-instances": {
"network-instance": [
{
"name": "default",
"config": {
"name": "default"
},
"protocols": {
"protocol": [
{
"identifier": "openconfig-policy-types:BGP",
"name": "default",
"config": {
"identifier": "openconfig-policy-types:BGP",
"name": "default"
}
}
]
}
}
]
}
}
Process finished with exit code 0
Issue tracker is ONLY used for reporting bugs. Please use the YDK Community for any support issues.
Expected Behavior
Current Behavior
CodecService.decode fails to decode XML code in python when using idx BGP prefix in the names space
Steps to Reproduce
Attempt to decode below XML code via CodecService
Your Script
Logs
Enable logging and post the logs below
System Information
MacOS 12.6.1 Python 3.10.8
ydk @ file:///Users/ldacol/Code/GNSA/ydk-gen/gen-api/python/ydk/dist/ydk-0.8.6.3.tar.gz ydk-models-cisco-ios-xe-1651 @ file:///Users/ldacol/Code/GNSA/ydk-gen/gen-api/python/cisco_ios_xe_1651-bundle/dist/ydk-models-cisco-ios-xe-1651-16.5.1.tar.gz ydk-models-cisco-ios-xr-762 @ file:///Users/ldacol/Code/GNSA/ydk-gen/gen-api/python/cisco_ios_xr_762-bundle/dist/ydk-models-cisco-ios-xr-762-7.6.2.tar.gz ydk-models-ietf==0.1.6 ydk-models-juniper-junos-18-4R3 @ file:///Users/ldacol/Code/GNSA/ydk-gen/gen-api/python/juniper_junos_18_4R3-bundle/dist/ydk-models-juniper-junos-18-4R3-18.4.post3.tar.gz ydk-models-juniper-qfx-20-2R1 @ file:///Users/ldacol/Code/GNSA/ydk-gen/gen-api/python/juniper_qfx_20_2R1-bundle/dist/ydk-models-juniper-qfx-20-2R1-20.2.post1.tar.gz