Table of Contents
YDK consists of two main components: core library, which consists of services and providers, and python model API, which are APIs generated based on YANG models and packaged as bundles.
Core library consists of the below:
- Service: Provides simple API interface to be used with the bindings and providers
- ServiceProvider: Provides concrete implementation that abstracts underlying protocol details (e.g.
NetconfServiceProvider, which is based on the NETCONF protocol)
Applications can be written using the python model API in conjunction with a service and a provider.
In this example, we set some BGP configuration using the OpenConfig model, the CRUD (Create/Read/Update/Delete) service and the NETCONF service provider. The example in this document is a simplified version of the more complete sample that is available in
samples/bgp.py. The more complete sample can be run with the below steps:
(ydk-py)ydk-py$ cd core/samples (ydk-py)samples$ ./bgp.py -h Usage: bgp.py [-h | --help] [options] Options: -h, --help show this help message and exit -v VERSION, --version=VERSION force NETCONF version 1.0 or 1.1 -u USERNAME, --user=USERNAME -p PASSWORD, --password=PASSWORD password --proto=PROTO Which transport protocol to use, one of ssh or tcp --host=HOST NETCONF agent hostname --port=PORT NETCONF agent SSH port (ydk-py)samples$ ./bgp.py --host <ip-address-of-netconf-server> -u <username> -p <password> --port <port-number>
YDK performs the below actions when running this application:
- Establish a session with the device
- Encode python data objects to the protocol format (e.g. netconf XML payload)
- Perform transport operation with the device and collect the response (e.g. netconf reply)
- Decode response as python object and return the result to app
- Raise python exceptions for any errors that occurred
The first step in any application is to create a service provider instance. In this case, the NETCONF service provider (defined in
NetconfServiceProvider) is responsible for mapping between the CRUD service API and the underlying manageability protocol (NETCONF RPCs).
We instantiate an instance of the service provider that creates a NETCONF session to the machine with address 10.0.0.1:
1 2 3 4 5 6 7
from ydk.providers import NetconfServiceProvider sp_instance = NetconfServiceProvider(address='10.0.0.1', port=830, username='test', password='test', protocol='ssh')
After establishing the connection, we instantiate the entities and set some data. First, we import the types from the OpenConfig BGP module:
from ydk.models.openconfig import openconfig_bgp from ydk.models.openconfig import openconfig_bgp_types
Next, create a
Bgp configuration object and set the attributes:
10 11 12 13 14 15 16 17 18 19 20 21 22 23
# create BGP object bgp_cfg = openconfig_bgp.Bgp() # set the Global AS bgp_cfg.global_.config.as_ = 65001 # Create an AFI SAFI config ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi() ipv4_afsf.afi_safi_name = openconfig_bgp_types.Ipv4Unicast() ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv4Unicast() ipv4_afsf.config.enabled = True # Add the AFI SAFI config to the global AFI SAFI list bgp_cfg.global_.afi_safis.afi_safi.append(ipv4_afsf)
The CRUD service provides methods to create, read, update and delete entities on a device making use of the session provided by a service provider (NETCONF in this case). In order to use the CRUD service, we need to import the
from ydk.services import CRUDService
Next, we instantiate the CRUD service:
crud_service = CRUDService()
Finally, we invoke the create method of the in this case). In order to use the CRUD service, we need to import the
CRUDService class passing in the
service provider instance and our entity (
26 27 28
try: crud_service.create(sp_instance, bgp_cfg) except YError:
Note if there were any errors the above API will raise a YError exception.
YDK uses common Python logging. All modules are based on the
ydk log. The below code snippet shows how to enable basic logging with the
INFO level, which is useful for most users of YDK. Using the
DEBUG level will produces a lot more detailed logs, which may be useful for developers working on YDK.
1 2 3 4 5
import logging log = logging.getLogger('ydk') log.setLevel(logging.INFO) handler = logging.StreamHandler() log.addHandler(handler)
To see time stamps and logging levels, please see the below code snippet.
1 2 3 4 5 6 7
import logging log = logging.getLogger('ydk') log.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter(("%(asctime)s - %(name)s - %(levelname)s - %(message)s")) handler.setFormatter(formatter) log.addHandler(handler)