cancel
Showing results for 
Search instead for 
Did you mean: 
cancel

How to retrieve the interface configuration

yitung
Cisco Employee
Cisco Employee

How to retrieve the interface configuration??

I saw the "nc-read-xr-ifmgr-cfg-11-ydk.py", the process is just "pass"

def process_interface_configurations(interface_configurations):

    """Process data in interface_configurations object."""

    pass

I tried to modified a bit but I got the response "None".

def process_interface_configurations(interface_configurations):

    """Process data in interface_configurations object."""

    int=interface_configurations.InterfaceConfiguration()

    print ('interface name: ',int.interface_name)

I believe it should be a list of configuration... but don't know how it should be...

any sample can be reference???

Thanks a lot!!

18 REPLIES 18

saalvare
Cisco Employee
Cisco Employee

All basic sample apps in the range of 10-19 are boilerplates (templates).  Those boilerplates do no processing and provide no output.  The convention should be described in the README:

ydk-py-samples/samples/basic at master · CiscoDevNet/ydk-py-samples · GitHub

Now, for a sample implementation of processing interface config, here's a very simple function to print the interface names:

def process_interface_configurations(interface_configurations):

    """Process data in interface_configurations object."""

    for interface_configuration in interface_configurations.interface_configuration:

      print(interface_configuration.interface_name)

The entire configuration hierarchy is documented at:

http://ydk.cisco.com/py/docs/gen_doc_6bcc931a236e23d2877d3e742c4a01f5adc17782.html

The function above produces the following output when the script is run against one of my routers:

Loopback0

MgmtEth0/RP0/CPU0/0

GigabitEthernet0/0/0/0

GigabitEthernet0/0/0/1

Hope that helps!

Million Thanks!! IT works like a charm! I now start understand the idea to learn ydk.

One more question, is there a way to read all configurations under a specific interface??

Sure thing.  Note that the object passed to crud.read really acts as a filter.  So, you need to construct an object with an interface with the name you're interested in.  Interface name is the key of the list of interfaces defined in the model:

# create read filter for mgmt interface

interface_configurations = xr_ifmgr_cfg.InterfaceConfigurations()

interface_configuration = interface_configurations.InterfaceConfiguration()

interface_configuration.interface_name ="MgmtEth0/RP0/CPU0/0"

interface_configurations.interface_configuration.append(interface_configuration)

# read data from NETCONF device

interface_configurations = crud.read(provider, interface_configurations)

Actually, you can be more clever and just instantiate the class that defines an interface.  YDK will do the right thing and build the proper filter with the full hierarchy even though you are reading a more specific object:

# create read filter for mgmt interface

interface_configuration = xr_ifmgr_cfg.InterfaceConfigurations.InterfaceConfiguration()

interface_configuration.interface_name ="MgmtEth0/RP0/CPU0/0"


# read data from NETCONF device

interface_configuration = crud.read(provider, interface_configuration)

Let's say we just want to print the interface description once read:

print(interface_configuration.description)

Here's a sample output on a mgmt interface with an obvious description:

$ ./nc-read-xr-ifmgr-cfg-99-ydk.py ssh://admin:admin@router

*** MANAGEMENT INTERFACE ***

$

Here's the full app log:

$ ./nc-read-xr-ifmgr-cfg-99-ydk.py ssh://admin:admin@router -v

2016-12-13 14:44:10,148 - ydk.providers.netconf_provider - INFO - NetconfServiceProvider connected to router:None using ssh

2016-12-13 14:44:10,211 - ydk.services.crud_service - INFO - READ operation initiated

2016-12-13 14:44:10,213 - ydk.providers._provider_plugin - DEBUG -

<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:63dbbab9-2cce-48e8-8437-832206be1989">

  <get>

    <filter type="subtree">

      <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg">

        <interface-configuration>

          <interface-name>MgmtEth0/RP0/CPU0/0</interface-name>

        </interface-configuration>

      </interface-configurations>

    </filter>

  </get>

</rpc>

2016-12-13 14:44:10,277 - ydk.providers._provider_plugin - DEBUG -

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:63dbbab9-2cce-48e8-8437-832206be1989">

  <data>

    <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg">

      <interface-configuration>

        <active>act</active>

        <interface-name>MgmtEth0/RP0/CPU0/0</interface-name>

        <description>*** MANAGEMENT INTERFACE ***</description>

        <ipv4-network xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-io-cfg">

          <addresses>

            <primary>

              <address>10.30.110.82</address>

              <netmask>255.255.254.0</netmask>

            </primary>

          </addresses>

        </ipv4-network>

      </interface-configuration>

    </interface-configurations>

  </data>

</rpc-reply>

2016-12-13 14:44:10,278 - ydk.services.crud_service - INFO - READ operation completed

*** MANAGEMENT INTERFACE ***

2016-12-13 14:44:10,500 - ydk.providers.netconf_provider - INFO - NetconfServiceProvider disconnected from router using ssh

$

Hope that helps!

How do you print the whole XML interface in a single print statement?  Similar to what is in the rpc-reply data group?