08-11-2022 03:29 AM
Hello community,
When using YDK python decode and encode modules to convert XML Junos configurations into JSON, XML attributes do not get reflected in the JSON configuration. Whilst I understand this could be a limitation JSON not supporting extensive semantics I would like to understand if this is supported in YDK and or if there may be a way to not lose XML attributes.
Example of XML snipped where the XML attribute is inactive="inactive". This is used in Junos to declare a stanza present in the configuration as deactivated or not active.
<bgp>
<group inactive="inactive">
<name>EBGP-TEST</name>
<type>external</type>
<import>IMPORT-IPV4</import>
<export>EXPORT-IPV4</export>
<peer-as>1111</peer-as>
<neighbor>
<name>192.168.1.1</name>
</neighbor>
</group>
</bgp>
JSON translated code
"bgp": {
"group": [
{
"name": "EBGP-TEST",
"type": "external",
"import": [
"IMPORT-IPV4"
],
"export": [
"EXPORT-IPV4"
],
"peer-as": "1111",
"neighbor": [
{
"name": "192.168.1.1"
}
]
}
]
},
Python code used to decode/encode is the following
from ydk.providers import CodecServiceProvider
from ydk.services import CodecService
CODEC = CodecService()
JSON_PROVIDER = CodecServiceProvider(type='json')
XML_PROVIDER = CodecServiceProvider(type='xml')
def test_yang_xml_to_yml (xml_file):
mode = 'rb'
with open(xml_file, mode) as xml_in:
config_xml = xml_in.read()
decoded_xml = CODEC.decode(XML_PROVIDER, config_xml)
yang_json = CODEC.encode(JSON_PROVIDER, decoded_xml)
return yang_json
Thank you in advance
08-11-2022 08:04 AM
Could you please attach YANG models that are used to encode the payload.
08-11-2022 08:13 AM
YANG models are the Juniper models taken from https://github.com/Juniper/yang/tree/master/18.4/18.4R3/junos/conf and bundles generated from the models above. BGP is covered under junos-conf-protocols https://yangcatalog.org/yang-search/module_details/junos-conf-protocols
08-12-2022 09:03 AM - edited 08-12-2022 09:24 AM
The YDK does not process XML attributes other than xmlns, which defines element's namespace. It is not designed to do the translation from XML to JSON and backward. For this you would need to use some other tools, which are available in open source space.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide