cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1221
Views
0
Helpful
1
Replies

YDK server disconnection

vsoaresf
Cisco Employee
Cisco Employee

Does anybody knows a way to clean close the provider connection in YDK 0.8.4?

It complains about no attribute Disconnect() in provider or no __enter__ when used in a with clause in Python.

1 Accepted Solution

Accepted Solutions

yangorelik
Spotlight
Spotlight

Hello vsoaresf

Currently the YDK does not implement closing of NetconfSession. It is getting closed by destructor when class instance is out of scope. It is designed this way to avoid dangling NetconfSession class instances, which are not connected to the device. Correspondently you have to design your application. Do not use global variables for NetconfSession or NetconfServiceProvider, but rather use them inside functions, which are called by data processing application. When function is finished, the class instance will be destroyed automatically and connection will be dropped.

Here is simple example:

#!/usr/bin/env python
#
from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.openconfig import openconfig_interfaces

def nc_read(device, filter):
provider = NetconfServiceProvider(
address=device['address'],
port=device['port'],
username=device['username'],
password=device['password']
)
crud = CRUDService()
return crud.read(provider, filter)

if __name__ == '__main__':
device = {
'address': 'ios-xe-mgmt.cisco.com',
'port': 10000,
'username': 'developer',
'password': 'C1sco12345'
}
interfaces = openconfig_interfaces.Interfaces()
all_interfaces = nc_read(device, interfaces)
# Process all_interfaces entity

Inside main you can safely call nc_read function as many times as you want for any device and any filter. Each time it is called there will be established Netconf session with device and after operation it will be safely closed. 

Yan Gorelik
YDK Solutions

View solution in original post

1 Reply 1

yangorelik
Spotlight
Spotlight

Hello vsoaresf

Currently the YDK does not implement closing of NetconfSession. It is getting closed by destructor when class instance is out of scope. It is designed this way to avoid dangling NetconfSession class instances, which are not connected to the device. Correspondently you have to design your application. Do not use global variables for NetconfSession or NetconfServiceProvider, but rather use them inside functions, which are called by data processing application. When function is finished, the class instance will be destroyed automatically and connection will be dropped.

Here is simple example:

#!/usr/bin/env python
#
from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.openconfig import openconfig_interfaces

def nc_read(device, filter):
provider = NetconfServiceProvider(
address=device['address'],
port=device['port'],
username=device['username'],
password=device['password']
)
crud = CRUDService()
return crud.read(provider, filter)

if __name__ == '__main__':
device = {
'address': 'ios-xe-mgmt.cisco.com',
'port': 10000,
'username': 'developer',
'password': 'C1sco12345'
}
interfaces = openconfig_interfaces.Interfaces()
all_interfaces = nc_read(device, interfaces)
# Process all_interfaces entity

Inside main you can safely call nc_read function as many times as you want for any device and any filter. Each time it is called there will be established Netconf session with device and after operation it will be safely closed. 

Yan Gorelik
YDK Solutions