cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1040
Views
5
Helpful
3
Replies

YDK Python decode/encode and XML attributes

ldacol
Level 1
Level 1

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

3 Replies 3

ygorelik
Cisco Employee
Cisco Employee

Could you please attach YANG models that are used to encode the payload.

ldacol
Level 1
Level 1

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

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.