<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Creating a loopback interface using OpenConfig in Tools</title>
    <link>https://community.cisco.com/t5/tools/creating-a-loopback-interface-using-openconfig/m-p/4031936#M2036</link>
    <description>&lt;P&gt;You may wonder how you can create a loopback interface using the OpenConfig Interface model.&amp;nbsp; Here's how:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;#!/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
&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;$ ./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 =============
&amp;lt;rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"&amp;gt;&amp;lt;edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"&amp;gt;
  &amp;lt;target&amp;gt;
    &amp;lt;candidate/&amp;gt;
  &amp;lt;/target&amp;gt;
  &amp;lt;config&amp;gt;&amp;lt;interfaces xmlns="http://openconfig.net/yang/interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="merge"&amp;gt;
  &amp;lt;interface&amp;gt;
    &amp;lt;name&amp;gt;Loopback0&amp;lt;/name&amp;gt;
    &amp;lt;config&amp;gt;
      &amp;lt;type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type"&amp;gt;ianaift:softwareLoopback&amp;lt;/type&amp;gt;
      &amp;lt;name&amp;gt;Loopback0&amp;lt;/name&amp;gt;
      &amp;lt;description&amp;gt;My new loopback interface :-)&amp;lt;/description&amp;gt;
      &amp;lt;enabled&amp;gt;true&amp;lt;/enabled&amp;gt;
    &amp;lt;/config&amp;gt;
  &amp;lt;/interface&amp;gt;
&amp;lt;/interfaces&amp;gt;
&amp;lt;/config&amp;gt;
&amp;lt;/edit-config&amp;gt;
&amp;lt;/rpc&amp;gt;
2020-02-18 19:33:24,823 - ydk - INFO - ============= Received RPC from device =============
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"&amp;gt;
  &amp;lt;ok/&amp;gt;
&amp;lt;/rpc-reply&amp;gt;

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 =============
&amp;lt;rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"&amp;gt;&amp;lt;commit xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"/&amp;gt;
&amp;lt;/rpc&amp;gt;
2020-02-18 19:33:25,822 - ydk - INFO - ============= Received RPC from device =============
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2"&amp;gt;
  &amp;lt;ok/&amp;gt;
&amp;lt;/rpc-reply&amp;gt;

2020-02-18 19:33:25,822 - ydk - INFO - Operation succeeded
2020-02-18 19:33:25,832 - ydk - INFO - Disconnected from device
$&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Feb 2020 03:37:36 GMT</pubDate>
    <dc:creator>saalvare</dc:creator>
    <dc:date>2020-02-19T03:37:36Z</dc:date>
    <item>
      <title>Creating a loopback interface using OpenConfig</title>
      <link>https://community.cisco.com/t5/tools/creating-a-loopback-interface-using-openconfig/m-p/4031936#M2036</link>
      <description>&lt;P&gt;You may wonder how you can create a loopback interface using the OpenConfig Interface model.&amp;nbsp; Here's how:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;#!/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
&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;$ ./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 =============
&amp;lt;rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"&amp;gt;&amp;lt;edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"&amp;gt;
  &amp;lt;target&amp;gt;
    &amp;lt;candidate/&amp;gt;
  &amp;lt;/target&amp;gt;
  &amp;lt;config&amp;gt;&amp;lt;interfaces xmlns="http://openconfig.net/yang/interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="merge"&amp;gt;
  &amp;lt;interface&amp;gt;
    &amp;lt;name&amp;gt;Loopback0&amp;lt;/name&amp;gt;
    &amp;lt;config&amp;gt;
      &amp;lt;type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type"&amp;gt;ianaift:softwareLoopback&amp;lt;/type&amp;gt;
      &amp;lt;name&amp;gt;Loopback0&amp;lt;/name&amp;gt;
      &amp;lt;description&amp;gt;My new loopback interface :-)&amp;lt;/description&amp;gt;
      &amp;lt;enabled&amp;gt;true&amp;lt;/enabled&amp;gt;
    &amp;lt;/config&amp;gt;
  &amp;lt;/interface&amp;gt;
&amp;lt;/interfaces&amp;gt;
&amp;lt;/config&amp;gt;
&amp;lt;/edit-config&amp;gt;
&amp;lt;/rpc&amp;gt;
2020-02-18 19:33:24,823 - ydk - INFO - ============= Received RPC from device =============
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"&amp;gt;
  &amp;lt;ok/&amp;gt;
&amp;lt;/rpc-reply&amp;gt;

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 =============
&amp;lt;rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"&amp;gt;&amp;lt;commit xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"/&amp;gt;
&amp;lt;/rpc&amp;gt;
2020-02-18 19:33:25,822 - ydk - INFO - ============= Received RPC from device =============
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2"&amp;gt;
  &amp;lt;ok/&amp;gt;
&amp;lt;/rpc-reply&amp;gt;

2020-02-18 19:33:25,822 - ydk - INFO - Operation succeeded
2020-02-18 19:33:25,832 - ydk - INFO - Disconnected from device
$&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Feb 2020 03:37:36 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/creating-a-loopback-interface-using-openconfig/m-p/4031936#M2036</guid>
      <dc:creator>saalvare</dc:creator>
      <dc:date>2020-02-19T03:37:36Z</dc:date>
    </item>
  </channel>
</rss>

