05-21-2020 07:14 AM
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.
Solved! Go to Solution.
05-22-2020 03:25 PM
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.
05-22-2020 03:25 PM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide