cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
188
Views
0
Helpful
0
Replies

CodexServiceProvider encode to JSON but keep YFilter.replace

hyber
Level 1
Level 1

Hi Experts.

Im trying to use YDK-GEN to convert JSON "config" into NetConf XML format to send to my router and switches. My plan is to use a combination of "code" and some jinja2 templating depending on the complexity of the config (and how standardized it is between my devices). Anyways, I would like to describe some of my code as jinja in json format, but I would really like to be able to have some parts use the "operation=merge" and some "operation=replace" option. 

here is the code I am using. 

from ydk.filters import YFilter
from ydk.services import CodecService
from ydk.providers import CodecServiceProvider
import logging

if __name__ == "__main__":

    logger = logging.getLogger("ydk")
    logger.setLevel(logging.WARNING)
    handler = logging.StreamHandler()
    formatter = logging.Formatter(("%(asctime)s - %(name)s - "
                                    "%(levelname)s - %(message)s"))
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    json_codec_provider = CodecServiceProvider(type='json')
    xml_codec_provider = CodecServiceProvider(type='xml')
    codec = CodecService()

    with open("test_test.json", "r") as f:
        json_data = f.read()

    blah = codec.decode(json_codec_provider, json_data)

    blah.ip.access_list.extended["TEST_TEST"].yfilter = YFilter.replace

    print(codec.encode(json_codec_provider, blah, subtree=True))
    print(codec.encode(xml_codec_provider, blah, subtree=True))

The output I am getting looks like this:

{
  "Cisco-IOS-XE-native:native": {
    "ip": {
      "access-list": {
        "Cisco-IOS-XE-acl:extended": [
          {
            "access-list-seq-rule": [
              {
                "ace-rule": {
                  "action": "permit",
                  "any": null,
                  "dst-any": null,
                  "protocol": "ospf"
                },
                "sequence": 5
              }
            ],
            "name": "TEST_TEST"
          },
          {
            "access-list-seq-rule": [
              {
                "ace-rule": {
                  "action": "deny",
                  "any": null,
                  "dst-any": null,
                  "protocol": "udp"
                },
                "sequence": 10
              }
            ],
            "name": "TEST_TEST2"
          }
        ]
      }
    }
  }
}

<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
  <ip>
    <access-list>
      <extended xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-acl" operation="replace">
        <name>TEST_TEST</name>
        <access-list-seq-rule>
          <sequence>5</sequence>
          <ace-rule>
            <action>permit</action>
            <protocol>ospf</protocol>
            <any/>
            <dst-any/>
          </ace-rule>
        </access-list-seq-rule>
      </extended>
      <extended xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-acl">
        <name>TEST_TEST2</name>
        <access-list-seq-rule>
          <sequence>10</sequence>
          <ace-rule>
            <action>deny</action>
            <protocol>udp</protocol>
            <any/>
            <dst-any/>
          </ace-rule>
        </access-list-seq-rule>
      </extended>
    </access-list>
  </ip>
</native>

as you can see the JSON does not have any notation of the NetConf operation, but the XML output has on the TEST_TEST access-list but not on the TEST_TEST2 ACL..

I guess this is "sorta" by design, but I was hoping that someone had input on how to put the operation into the JSON data or if there was some other cleaver way of marking some of the json data as operation replace and some as merge?

I hope it makes sense..

Thanks a lot for your inpurt

Esben

0 Replies 0
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: