cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2227
Views
15
Helpful
5
Replies

How To: Edit YANG values before they are inserted into the device XML

Andrew Horrigan
Cisco Employee
Cisco Employee

I have the following YANG:

    leaf asa-outside-subent {

        tailf:info "ASA Outside Interface Subnet";

        type inet:ipv4-address {

        }

    }

Once the user inputs a subnet, I want to remove the .0 at the end, and use the first 3 octets to fill the XML template.

    <device>

      <name>{/asa}</name>

      <config>

                   <interface xmlns="http://cisco.com/ned/asa">

                     <TenGigabitEthernet>

                       <name>0/1</name>

                       <ip>

                         <address>

                           <ip>

                             <host-ip>{asa-outside-subnet}.2</host-ip>

                             <netmask>255.255.255.0</netmask>

                           </ip>

                         </address>

                       </ip>

What is the easiest way to modify these values after they have been inserted into the YANG model, but before they are used to generate the device XML?  I want to say Python ... but I am not entirely sure how to write or insert a script into this flow.

Thanks.

1 Accepted Solution

Accepted Solutions

Hi Andrew

glad you liked it

Best thing to do is:

a) use the package builder -- this will create the XML, YANG and Python components (and integrated structure)

and you only need to "fill in your configs".  In fact, after running the package builder, you can compile YANG and

do "packages reload" -- and you'll have a 100% functional (but doing nothing) service...

For your example i ran this (in the ./packages directory)

   ncs-make-package --service-skeleton python-and-template tinyTest

then

b) edit:

./tinyTest/src/yang/tinyTest.yang

./tinyTest/templates/tinyTest-template.xml

./tinyTest/python/tinyTest/main.py

be sure to compile YANG (from ./tinyTest/src just run "make clean all")

and "packages reload" in NSO

on your last note - in your YANG model you had used "hyphens" (dashes) in

the leaf names. e.g. asa-outside-subnet which is perfectly fine!

it's just that python thinks of the "-" symbol as "subtraction"...

so if you use those in YANG, just flip them to underscore _ in python.

(they are ok as dashes in XML)

cheers

gregg

View solution in original post

5 Replies 5

gschudel
Cisco Employee
Cisco Employee

Hi Andrew

assuming you're going down the "python-and-template" road... it's pretty easy to "split" a string and read the components in Python...

E.g.; http://www.pythonforbeginners.com/dictionary/python-split

I built a tiny model (used the ncs skeleton builder) to test this out using your inputs:

YANG:

  list tinyTest {

    description "This is an RFS skeleton service";

    key name;

    leaf name {

      tailf:info "Unique service id";

      tailf:cli-allow-range;

      type string;

    }

    uses ncs:service-data;

    ncs:servicepoint tinyTest-servicepoint;

    leaf device {

      type leafref {

        path "/ncs:devices/ncs:device/ncs:name";

      }

    }

    leaf asa-outside-subnet {

      tailf:info "ASA Outside Interface Subnet";

      type inet:ipv4-address;

    }

  }

}

XML:

<config-template xmlns="http://tail-f.com/ns/config/1.0">

  <devices xmlns="http://tail-f.com/ns/ncs">

    <device>

      <name>{/device}</name>

      <config>

        <interface xmlns="http://cisco.com/ned/asa">

          <TenGigabitEthernet>

            <name>0/1</name>

            <ip>

              <address>

                <ip>

                  <host-ip>{$SUBSTRING}.2</host-ip>

                  <netmask>255.255.255.0</netmask>

                </ip>

              </address>

            </ip>

          </TenGigabitEthernet>

        </interface>

      </config>

    </device>

  </devices>

</config-template>

PYTHON:

        readsubnet = service.asa_outside_subnet

        a,b,c,d  = readsubnet.split(".")

        writesubnet = '%s.%s.%s' % (a,b,c)

        self.log.info('writesubent = %s' % writesubnet )

        self.log.info('My Addr is: %s.%s.%s.2' % (a,b,c))

        vars = ncs.template.Variables()

        vars.add('SUBSTRING', writesubnet)

        template = ncs.template.Template(service)

        template.apply('tinyTest-template', vars)

And then complied, and ran the tiny model:

On NSO CLI:

admin@ncs% set tinyTest u01 device asa1 asa-outside-subnet 10.1.1.0

[ok][2017-12-21 13:39:32]

[edit]

admin@ncs% commit dry-run

cli {

    local-node {

        data +tinyTest u01 {

             +    device asa1;

             +    asa-outside-subnet 10.1.1.0;

             +}

              devices {

                  device asa1 {

                      config {

                          asa:interface {

             +                TenGigabitEthernet 0/1 {

             +                    ip {

             +                        address {

             +                            ip {

             +                                host-ip 10.1.1.2;

             +                                netmask 255.255.255.0;

             +                            }

             +                        }

             +                    }

             +                }

                          }

                      }

                  }

              }

    }

}

[ok][2017-12-21 13:39:33]

[edit]

admin@ncs% commit

Commit complete.

[ok][2017-12-21 13:39:38]

[edit]

admin@ncs%

IN DEBUG LOGS:

(tail -f ./logs/ncs-python-vm-tinyTest.log)

----------------------------------------------

2017-12-21 13:39:33 - tinyTest - INFO - Service create(service=/tinyTest:tinyTest{u01})

2017-12-21 13:39:33 - tinyTest - INFO - writesubent = 10.1.1

2017-12-21 13:39:33 - tinyTest - INFO - My Addr is: 10.1.1.2

2017-12-21 13:39:37 - tinyTest - INFO - Service create(service=/tinyTest:tinyTest{u01})

2017-12-21 13:39:37 - tinyTest - INFO - writesubent = 10.1.1

2017-12-21 13:39:37 - tinyTest - INFO - My Addr is: 10.1.1.2

ON ASA NETSIM:

(before)

asa1# show running-config

no names

no snmp-server contact

no snmp-server location

no arp permit-nonconnected

no loopback-proxy server

no loopback-proxy client syslog

no call-home reporting anonymous

asa1#

(after)

asa1#

System message at 2017-12-21 13:39:38...

Commit performed by admin via ssh using cli.

asa1#

System message at 2017-12-21 13:39:38...

Commit performed by admin via ssh using cli.

asa1# show running-config

no names

interface TenGigabitEthernet0/1

ip address 10.1.1.2 255.255.255.0

no nameif

no shutdown

no security-level

exit

no snmp-server contact

no snmp-server location

no arp permit-nonconnected

no loopback-proxy server

no loopback-proxy client syslog

no call-home reporting anonymous

asa1#




That seems to work


hope that helps

- Gregg

Wow, thanks Gregg!  This is really helpful.

The last part that is confusing to me is what is tying the YANG and the XML to the Python?  I am assuming the Python script is saved in the /python folder (SUBSTRING.py), but what happens in NSO to run/call that script when the service starts?

Also, and this is a small thing, but something I noticed before - why is is that variable in the YANG uses dashes ( - ) but then the Python uses underscores ( _ ) ?  This is something I've seen before ...

Best,

Andrew

Hi Andrew

glad you liked it

Best thing to do is:

a) use the package builder -- this will create the XML, YANG and Python components (and integrated structure)

and you only need to "fill in your configs".  In fact, after running the package builder, you can compile YANG and

do "packages reload" -- and you'll have a 100% functional (but doing nothing) service...

For your example i ran this (in the ./packages directory)

   ncs-make-package --service-skeleton python-and-template tinyTest

then

b) edit:

./tinyTest/src/yang/tinyTest.yang

./tinyTest/templates/tinyTest-template.xml

./tinyTest/python/tinyTest/main.py

be sure to compile YANG (from ./tinyTest/src just run "make clean all")

and "packages reload" in NSO

on your last note - in your YANG model you had used "hyphens" (dashes) in

the leaf names. e.g. asa-outside-subnet which is perfectly fine!

it's just that python thinks of the "-" symbol as "subtraction"...

so if you use those in YANG, just flip them to underscore _ in python.

(they are ok as dashes in XML)

cheers

gregg

Anyone have an example on how to reference nested yang objects ? For example:

container a

  container b

    leaf c

service.???

Start a new thread for this question.