cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
125
Views
0
Helpful
0
Replies
Cisco Employee

Creating a loopback interface using OpenConfig

You may wonder how you can create a loopback interface using the OpenConfig Interface model.  Here's how:

 

#!/usr/bin/env python
#
# Copyright 2016 Cisco Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
Create configuration for model openconfig-interfaces.

usage: nc-create-oc-interfaces-20-ydk.py [-h] [-v] device

positional arguments:
  device         NETCONF device (ssh://user:password@host:port)

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  print debugging messages
"""

from argparse import ArgumentParser
from urlparse import urlparse

from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.openconfig import openconfig_interfaces \
    as oc_interfaces
from ydk.models.ietf import iana_if_type
import logging


def config_interfaces(interfaces):
    """Add config data to interfaces object."""
    # configure loopback interface
    interface = interfaces.Interface()
    interface.name = "Loopback0"
    interface.config.name = "Loopback0"

    interface.config.type = iana_if_type.SoftwareLoopback()
    interface.config.description = "My new loopback interface :-)"
    interface.config.enabled = True

    interfaces.interface.append(interface)



if __name__ == "__main__":
    """Execute main program."""
    parser = ArgumentParser()
    parser.add_argument("-v", "--verbose", help="print debugging messages",
                        action="store_true")
    parser.add_argument("device",
                        help="NETCONF device (ssh://user:password@host:port)")
    args = parser.parse_args()
    device = urlparse(args.device)

    # log debug messages if verbose argument specified
    if args.verbose:
        logger = logging.getLogger("ydk")
        logger.setLevel(logging.INFO)
        handler = logging.StreamHandler()
        formatter = logging.Formatter(("%(asctime)s - %(name)s - "
                                      "%(levelname)s - %(message)s"))
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    # create NETCONF provider
    provider = NetconfServiceProvider(address=device.hostname,
                                      port=device.port,
                                      username=device.username,
                                      password=device.password,
                                      protocol=device.scheme)
    # create CRUD service
    crud = CRUDService()

    interfaces = oc_interfaces.Interfaces()
    config_interfaces(interfaces)  # add object configuration

    # create configuration on NETCONF device
    crud.create(provider, interfaces)

    exit()
# End of script

Log:

 

$ ./nc-create-oc-interfaces-20-ydk.py ssh://user:password@host -v
2020-02-18 19:33:24,486 - ydk - INFO - Path where models are to be downloaded: /home/saalvare/.ydk/pavarotti
2020-02-18 19:33:24,494 - ydk - INFO - Connected to pavarotti on port 830 using ssh with timeout of -1
2020-02-18 19:33:24,506 - ydk - INFO - Executing CRUD create operation on [openconfig-interfaces:interfaces]
2020-02-18 19:33:24,506 - ydk - INFO - Executing 'edit-config' RPC on [openconfig-interfaces:interfaces]
2020-02-18 19:33:24,518 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <target>
    <candidate/>
  </target>
  <config><interfaces xmlns="http://openconfig.net/yang/interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="merge">
  <interface>
    <name>Loopback0</name>
    <config>
      <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
      <name>Loopback0</name>
      <description>My new loopback interface :-)</description>
      <enabled>true</enabled>
    </config>
  </interface>
</interfaces>
</config>
</edit-config>
</rpc>
2020-02-18 19:33:24,823 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
  <ok/>
</rpc-reply>

2020-02-18 19:33:24,823 - ydk - INFO - Executing 'commit' RPC
2020-02-18 19:33:24,823 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><commit xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"/>
</rpc>
2020-02-18 19:33:25,822 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
  <ok/>
</rpc-reply>

2020-02-18 19:33:25,822 - ydk - INFO - Operation succeeded
2020-02-18 19:33:25,832 - ydk - INFO - Disconnected from device
$