Configuring Cisco IOS XRv router with NETCONF

In this scenario I use Python ncclient library to configure and verify an interface on a Cisco IOS XRv running on GNS3. I use Cisco YANG Explorer to generate NETCONF XML formatted code.

GNS3 Topology:

Enabling NECTCONF on Cisco IOS XRv router:

username automation
 group root-system
 password 7 00343315174C5B140B
!
aaa authorization exec default local
aaa authentication login default local
!
interface MgmtEth0/0/CPU0/0
 ipv4 address 192.168.240.201 255.255.255.0
!
netconf agent tty
!
netconf-yang agent
 ssh
!
ssh server v2
ssh server vrf default
ssh server netconf vrf default

# Also run “crypto key generate rsa” in the exec mode after configuring SSH

I have created an inventory file for the devices in this scenario and include it in my Python script:

iosxrv_1 = {
    "ip" : "192.168.240.201",
    "netconf_port" : "830",
    "username" : "automation",
    "password" : "P@ssw0rd",
    "device_params" : {'name':'csr'}
}

iosxrv_2 = {
    "ip" : "192.168.240.202",
    "netconf_port" : "830",
    "username" : "automation",
    "password" : "P@ssw0rd",
    "device_params": {'name': 'csr'}
}

This is a simple template file for interface configuration using NETCONF. I will include this file in my Python script as well:

int_filter = '''
<filter>
   <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg" xmlns:ipv4-io-cfg="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-io-cfg">
     <interface-configuration>
       <link-status/>
       <description/>
       <active/>
       <interface-name>{INTERFACE}</interface-name>
       <ipv4-io-cfg:ipv4-network>
         <ipv4-io-cfg:addresses>
           <ipv4-io-cfg:primary>
             <ipv4-io-cfg:address/>
             <ipv4-io-cfg:netmask/>
           </ipv4-io-cfg:primary>
         </ipv4-io-cfg:addresses>
       </ipv4-io-cfg:ipv4-network>
     </interface-configuration>
   </interface-configurations>
</filter>
'''

int_config = '''
<config>
      <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg" xmlns:ipv4-io-cfg="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-io-cfg">
        <interface-configuration>
          <description>{DESCRIPTION}</description>
          <active>act</active>
          <interface-name>{INTERFACE}</interface-name>
          <ipv4-io-cfg:ipv4-network>
            <ipv4-io-cfg:addresses>
              <ipv4-io-cfg:primary>
                <ipv4-io-cfg:address>{IP}</ipv4-io-cfg:address>
                <ipv4-io-cfg:netmask>{SUBNET_MASK}</ipv4-io-cfg:netmask>
              </ipv4-io-cfg:primary>
            </ipv4-io-cfg:addresses>
          </ipv4-io-cfg:ipv4-network>
        </interface-configuration>
      </interface-configurations>
</config>
'''

And here is the script:

from inventory import iosxrv_1
from ncclient import manager
import nc_templates as templates

interface = "GigabitEthernet0/0/0/1"
ip = "172.16.0.1"
subnet_mask = "255.255.255.0"
description = "Configured by NETCONG"

nc_filter = templates.int_filter.format(INTERFACE=interface)
nc_config = templates.int_config.format(INTERFACE=interface, IP=ip, SUBNET_MASK=subnet_mask, DESCRIPTION=description)

with manager.connect(host=iosxrv_1["ip"],
                     port=iosxrv_1["netconf_port"],
                     username=iosxrv_1["username"],
                     password=iosxrv_1["password"],
                     device_params=iosxrv_1["device_params"],
                     hostkey_verify=False) as device_h:

    nc_reply = device_h.edit_config(target="candidate", config=nc_config)
    nc_reply = device_h.commit()

    nc_reply = device_h.get_config("running", nc_filter)
    print(nc_reply.xml)

The only magic is how to create the XML formatted NETCONF template file. I used Cisco YANG explorer server with Cisco-IOS-XR-ifmgr-cfg.YANG model to create the XML code and turned the XML code to a simple template by substituting the values with template parameters.   

For learning how to install and use cisco YANG explorer server visit: https://github.com/CiscoDevNet/yang-explorer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.