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

Converting data model to valid yang output

denvle
Level 1
Level 1

Hello everyone,

 

I'm quite new to this and I'm wondering how you guys convert your own data model to a valid yang data model that you can send to the device?

 

My problem is that in your own data model "IAC" you have this file to configure an ACL:

acls:[
  {"ip": "10.1.0.190", "mask": ""},
  {"ip": "172.30.176.0", "mask": "0.0.15.255"}
]

Now you want to convert this to a valid YANG/json format.

The thing is the yang/json format doesn't accept empty keys

How do you convert this without writing a lot of code?

You can see that in my example I have an empty string for the mask.
But in the valid yang/json you can't send the key

 

{
    "Cisco-IOS-XE-acl:standard": {
        "name": 61,
        "access-list-seq-rule": [
            {
                "sequence": "10",
                "permit": {
                    "std-ace": {
                        "ipv4-prefix": "10.1.0.190"
                    }
                }
            },
            {
                "sequence": "20",
                "permit": {
                    "std-ace": {
                        "ipv4-prefix": "172.30.176.0",
                        "mask": "0.0.15.255"
                    }
                }
            },

Thanks in advance for setting me in the right direction!

1 Accepted Solution

Accepted Solutions

Hi Dennis

In my opinion you need to add verifications for IP address and mask values, before you are updating the yangacllist. This is critical, because the values must comply with the patterns defined in the YANG model. Obviously, the empty string does not comply with the pattern, so it will never gets into configuration.

Yan Gorelik
YDK Solutions

View solution in original post

4 Replies 4

yangorelik
Spotlight
Spotlight

Based on the YANG model

  grouping ipv4-std-ace-grouping {
    container std-ace {
      choice source-choice {
        case ipv4-prefix-case {
          leaf ipv4-prefix {
            type ios-types:ipv4-host;
          }
          leaf mask {
            type inet:ipv4-address;
          }
        }

        case any-case {
          leaf any {
            description
              "Any source prefix";
            type empty;
          }
        }

        case host-case {
          leaf host {
            description
              "A single source host";
            type ios-types:ipv4-host;
          }
        }
      }
      uses ace-log-grouping;
    }
  }

The 'mask' is not a mandatory leaf, so it is not necessary to include it to the configuration. But if you include it, you should follow its type format 'inet:ipv4-address':

  typedef ipv4-address {
    type string {
      pattern
        '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
      +  '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
      + '(%[\p{N}\p{L}]+)?';
    }
}

Based on that definition the mask cannot be an empty string!

Yan Gorelik
YDK Solutions

Hello Yan,

(I'm really new to this stuff first and foremost!)

 

Well that's basically my question, how do I leave this key out for just once?
I want to keep my IAC model consistent.

Is there some tool that can go from my data model and leave out these keys if they are empty?
Or should this all be programmatic logic?

Basically the question is how do I go from my data model to the correct yang/json output?

 

So far I came up with something like this:

class Yang_ios_acl:
    def __init__(self, aclnr):
        self.yangmodel = {
            "Cisco-IOS-XE-acl:standard": {
                "name": aclnr,
                "access-list-seq-rule": []
                }
            }
        self.yangacllist = {}
        self.action = ""


    def __str__(self):
        return json.dumps(self.yangmodel, indent=4)


    def add_list(self):
        self.yangmodel['Cisco-IOS-XE-acl:standard']['access-list-seq-rule'].append(self.yangacllist)
        self.yangacllist = {}


    def add_action(self, action):
        self.action = action
        self.yangacllist.update(
            {
                action:{"std-ace":{}}
            }
        )


    def add_ip(self, ip):
        try:
            self.yangacllist[self.action]["std-ace"].update(
                {
                    "ipv4-prefix": ip
                }
            )
        except KeyError:
            print("You first need to add an action before adding IP!")
            exit()


    def add_mask(self, mask):
        try:
            self.yangacllist[self.action]["std-ace"].update(
                {
                    "mask": mask
                }
            )
        except KeyError:
            print("You first need to add an action before adding IP!")
            exit()

    def add_remark(self, remark):
        self.yangacllist.update(
            {
                "remark":remark
            }
        )


    def add_seq(self, seq):
        self.yangacllist.update(
            {
                "sequence":seq
            }
        )

I'm using a class to fill this, but I'm just wondering if my approach is correct?

 

Kr,
Dennis van Leeuwen

Hi Dennis

In my opinion you need to add verifications for IP address and mask values, before you are updating the yangacllist. This is critical, because the values must comply with the patterns defined in the YANG model. Obviously, the empty string does not comply with the pattern, so it will never gets into configuration.

Yan Gorelik
YDK Solutions

denvle
Level 1
Level 1

Thanks for the help Yan!

I'm happy, except for the validation in the modules in my classes my basic idea is correct.

 

Kind regards,
Dennis van Leeuwen

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: