11-25-2020 10:31 AM
Problems executing python script in this specific lab of DEVASC training course, when you instantiate CRUDService() it fails with messages: "TypeError: ydk_.services.CRUDService.__init__() must be called when overriding __init__"
Ive tried to make changes inside of the crud_service, executor_service and netconf_service python files as mentioned here https://github.com/ygorelik/ydk-gen/commit/2c53f5dc736d73064c03dc1c404982d1c9011d64 actually the previous issue dissapear but now is showing issus related with the read method of the CRUDService object.
The python script executed is attached.
Someone knows which steps we have to take in account to make this script suscessfully work?
11-25-2020 11:25 AM
Hi Luis
When building model you must always start from the top level entity and keep in mind that container and list classes has already been instantiated in parent class. Your script should be:
from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.cisco_ios_xe import Cisco_IOS_XE_native as xe_model # you had error in this line
# CSR1kv1 Credentials
ip = '10.0.0.20'
port_n = 830
user = 'cisco'
paswd = 'cisco'
proto = 'ssh'
if __name__ == '__main__':
# open the connection with Netconf server
provider = NetconfServiceProvider(address=ip, port=port_n, username=user, password=paswd, protocol=proto)
# initialize CRUD Service
crud = CRUDService()
# create a new instance of Native object
xe_native = xe_model.Native() # always start from top level entity
xe_interfaces = xe_native.interfaces # object 'interfaces' has already been instantiated
# read the interfaces with the help of read function
interfaces_data = crud.read(provider, xe_interfaces)
# print the primary address of the fifth gigabitethernet interface
print(interfaces_data.gigabitethernet[4].ip.address.primary.address)
exit()
11-26-2020 03:20 PM
Have the same problem when initializing CRUDService:
Python 3.6.9 (default, Oct 8 2020, 12:12:24)
[GCC 8.4.0] on linux
>>> from ydk.services import CRUDService
>>> crud = CRUDService()
TypeError: ydk_.services.CRUDService.__init__() must be called when overriding __init__
Please your help to solve this problem.
11-27-2020 05:41 AM
Try this
https://github.com/ygorelik/ydk-gen/commit/2c53f5dc736d73064c03dc1c404982d1c9011d64
I made the changes inside of the crud_service.py file suggested and that solve the issue
01-20-2022 06:21 AM
it helps me.
I used CentOS7 in my environment.
Let's try below.
$ pip show ydk
please check which location have you installed your ydk.
my location is /home/cisco/.local/lib/python3.6/site-package
I edited below files.
/home/cisco/.local/lib/python3.6/site-package/ydk/services/crud_service.py
/home/cisco/.local/lib/python3.6/site-package/ydk/services/executor_service.py
/home/cisco/.local/lib/python3.6/site-package/ydk/services/netconf_service.py
I have referenced below URL. I know how do I change it.
https://github.com/ygorelik/ydk-gen/commit/2c53f5dc736d73064c03dc1c404982d1c9011d64
I hope this update for your help.
11-27-2020 05:43 AM
Thanks Yan i tried again only modifying the crud_service.py file as mentioned here https://github.com/ygorelik/ydk-gen/commit/2c53f5dc736d73064c03dc1c404982d1c9011d64 and without any change in the code the scritp works as expected
11-27-2020 09:35 AM - edited 11-27-2020 05:27 PM
Hello Luis and Jose
Since the 0.8.5 merge to upstream (still officially not released) I have fixed few bugs including the above one. Therefore I suggest you to use master branch from my fork. The installation is very simple:
git clone https://github.com/ygorelik/ydk-gen.git
cd ydk-gen
./install_ydk.sh --core
For YDK test drive I suggest to use docker:
docker run -it ydksolutions/ydk-gen
04-15-2021 05:58 PM - edited 04-15-2021 06:05 PM
Hi.
So I ran the docker container and i'm getting the same message:
root@b66bf1398402:/ydk-practice# python3 nc-read-and-write.py 'ssh://admin:XXXXXXXX@10.201.164.21:830'
Establishing NETCONF connection to 10.201.164.21...
Reading NETCONF configuration from 10.201.164.21...
Segmentation fault
Just wondering if when the container is built, is it running the 0.8.5 version of ydk?
I really want to run this from Docker.
Thanks.
Here's the file i'm trying to run.
#!/usr/bin/env python
#exi
# 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 Cisco-IOS-XE-native.
usage: nc-read-and-write.py [-h] [-v] ssh://cisco:cisco@10.62.149.173:22
python3 nc-read-and-write.py ssh://cisco:cisco@10.62.149.173:22
positional arguments:
device NETCONF device (ssh://user:password@host:port)
example device: ssh://developer:C1sco12345@ios-xe-mgmt.cisco.com:10000
optional arguments:
-h, --help show this help message and exit
-v, --verbose print debugging messages
"""
from argparse import ArgumentParser
from urllib.parse import urlparse
from ydk.services import CRUDService, CodecService
from ydk.providers import NetconfServiceProvider, CodecServiceProvider
from ydk.models.cisco_ios_xe import Cisco_IOS_XE_native \
as xe_native
import logging
def config_native(native):
"""
Add config data to native object.
Loop through interfaces and set their description to 'DESCRIPTION SET BY YDK'
"""
print("Loop through Loopback and GigabitEthernet interfaces in current configuration and change their interface descriptions to 'DESCRIPTION SET BY YDK'...\n")
for loopback in native.interface.loopback:
loopback.description = "DESCRIPTION SET BY YDK"
for gigabitethernet in native.interface.gigabitethernet:
gigabitethernet.description = "DESCRIPTION SET BY YDK"
# The following code can be uncommented and used to change the description on all interfaces
# regardless of interface type by looping through child entities of the native.interface entity.
# interface_children = native.interface.get_children()
# for entity_path in interface_children:
# interface = interface_children[entity_path]
# if( hasattr(interface, 'description')
# interface.description = "DESCRIPTION SET BY YDK"
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
print("Establishing NETCONF connection to " + device.hostname + "...\n")
nc_provider = NetconfServiceProvider(address=device.hostname,
port=device.port,
username=device.username,
password=device.password,
protocol=device.scheme)
# nc_provider = NetconfServiceProvider(address='10.201.164.21',
# port='830',
# username='admin',
# password='xxxxxxxx',
# protocol='')
# create CRUD service
crud = CRUDService()
native = xe_native.Native() # create object
# read data from NETCONF device
print("Reading NETCONF configuration from " + device.hostname + "...\n")
native = crud.read(nc_provider, native)
print("Configuration successfully captured!\n")
# create codec provider
codec_provider = CodecServiceProvider(type="xml")
# create codec service
codec = CodecService()
print("Printing current XML configuration for " + device.hostname + " and saving to " + device.hostname + "_before.xml ...\n")
print(codec.encode(codec_provider, native))
# save current configuration to hostname_before.xml
with open(device.hostname + "_before.xml", "w") as output_file:
output_file.write(codec.encode(codec_provider, native))
# change configuration in native object
config_native(native) # add object configuration
print("Printing updated XML configuration for " + device.hostname + " and saving to " + device.hostname + "_after.xml ...\n")
print(codec.encode(codec_provider, native))
# save current configuration to hostname_before.xml
with open(device.hostname + "_after.xml", "w") as output_file:
output_file.write(codec.encode(codec_provider, native))
# update configuration on NETCONF device
print("Writing updated NETCONF configuration to " + device.hostname + "...\n")
crud.update(nc_provider, native)
print("Configuration successfully updated!\n")
exit()
# End of script
04-16-2021 11:21 AM
Hi.
Since I couldn't get the docker install to work, i followed the instructions above to install this in a new ubuntu bionic vm.
I'm still getting the TypeError above unfortunately.
sclake@bionic_ydk:~/shares/ydk-practice$
e/nc-read-and-write.py 'ssh://admin:cisco!123@10.201.164.21:830'ares/ydk-practice
Establishing NETCONF connection to 10.201.164.21...
Traceback (most recent call last):
File "/home/sclake/shares/ydk-practice/nc-read-and-write.py", line 99, in <module>
crud = CRUDService()
TypeError: ydk_.services.CRUDService.__init__() must be called when overriding __init__
sclake@bionic_ydk:~/shares/ydk-practice$
e/nc-read-and-write.py 'ssh://admin:cisco!123@10.201.164.21:830'ares/ydk-practice
Establishing NETCONF connection to 10.201.164.21...
Traceback (most recent call last):
File "/home/sclake/shares/ydk-practice/nc-read-and-write.py", line 99, in <module>
crud = CRUDService()
TypeError: ydk_.services.CRUDService.__init__() must be called when overriding __init__
sclake@bionic_ydk:~/shares/ydk-practice$
I did get some error messages when i installed the models, which i am including below.
The read-and-write.py script is in my original comment below when i tried to get it working in Docker.
Thanks.
Scott
04-16-2021 03:17 PM
Hello guys
I followed above suggested YDK docker installation and ran your script on Cisco XE sandbox with no errors. To be exact, I only commented out CRUD update operation as it is not allowed to do changes on the sandbox. Please see attached console output that includes all the operations from YDK docker image installation to Python script run results.
04-16-2021 03:51 PM
So the only thing i'm noticing here is that I'm on docker for windows and you're running the docker run command under linux. Since I'm still getting the "segmentation fault" when executing the docker run command on windows (power shell prompt), I wonder if that is the cause? Even so, I still got the Typeerror issue running ydk directly on bionic under vmware workstation pro. So the only thing left here is for me to run the container in under linux i'm guessing, right? The thing is that with running ydk under docker or a vm, i can still get access to the host hard drive for my scripts. Not sure if i can do that if running docker under ubuntu under vmware workstation pro(?). Thanks for taking a look. Scott.
root@013b04f371be:/ydk-practice# python3 nc-read-and-write.py 'ssh://admin:cisco!123@10.201.164.21:830'
Establishing NETCONF connection to 10.201.164.21...
Reading NETCONF configuration from 10.201.164.21...
Segmentation fault
root@013b04f371be:/ydk-practice# ssh admin@10.201.164.21 -p 830
The authenticity of host '[10.201.164.21]:830 ([10.201.164.21]:830)' can't be established.
RSA key fingerprint is SHA256:xnlTMXQGFy8flo0UY/tdGsYXW7LkMd/l4yWtgqDQi68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.201.164.21]:830' (RSA) to the list of known hosts.
admin@10.201.164.21's password:
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
<capability>urn:ietf:params:netconf:base:1.1</capability>
<capability>urn:ietf:params:netconf:capability:writable-running:1.0</capability>
<capability>urn:ietf:params:netconf:capability:rollback-on-error:1.0</capability>
04-17-2021 09:26 AM
Hi Scott
Honestly, I've never tried to run YDK application under Windows platform. Currently it is not supported.
The docker use under Windows is limited and somewhat artificial, because the Docker was designed to run under Linux as it uses Linux kernel. Therefore the best way to work on Windows PC is to use Linux virtual machine. Nevertheless, I have to notice that Jacky managed to run YDK under Windows-10 WSL Ubuntu-18.04 (see this discussion).
Very important! Unfortunately the CiscoDevNet repository for YDK is now out of sync, therefore please clone YDK from my fork as suggested above. That is proven to work fine and I continue maintain that repository. Repeating here the installation procedure:
From source
git clone https://github.com/ygorelik/ydk-gen.git
cd ydk-gen
./install_ydk.sh --core
Docker (Ubuntu-18.04)
docker run -it ydksolutions/ydk-gen
04-19-2021 10:33 AM
Yan,
Decided to see if i could replicate Jacky's experience and install in wsl2 ubuntu 20.04 (which is what i originally installed on wsl2; if it is determined that 20.04 will not support ydk, then i will go ahead and reconfigure wsl2 to also support 18.04 and try that).
There were no installation issues as far as i could tell but this is where i'm at now below.
Thanks, Scott
(venv) sclake@SCLAKE-R4PQT:/mnt/c/Users/sclake/Dropbox/ipvzero/ydk-practice$ python3 /mnt/c/Users/sclake/Dropbox/ipvzero/ydk-practice/nc-read-and-write.py 'ssh://cisco:cisco!123@10.62.149.176:830' Establishing NETCONF connection to 10.62.149.176... Reading NETCONF configuration from 10.62.149.176... Traceback (most recent call last): File "/mnt/c/Users/sclake/Dropbox/ipvzero/ydk-practice/nc-read-and-write.py", line 106, in <module> native = crud.read(nc_provider, native) File "/home/sclake/venv/lib/python3.8/site-packages/ydk/services/crud_service.py", line 60, in read return _crud_read(provider, read_filter, False, self._crud.read) File "/home/sclake/venv/lib/python3.8/site-packages/ydk/services/crud_service.py", line 88, in _crud_read read_top_entity = crud_call(provider, top_filters) File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__ self.gen.throw(type, value, traceback) File "/home/sclake/venv/lib/python3.8/site-packages/ydk/errors/error_handler.py", line 82, in handle_runtime_error _raise(_exc) File "/home/sclake/venv/lib/python3.8/site-packages/ydk/errors/error_handler.py", line 54, in _raise exec("raise exc from None") File "<string>", line 1, in <module> ydk.errors.YInvalidArgumentError: Path is invalid: Cisco-IOS-XE-native:native (venv) sclake@SCLAKE-R4PQT:/mnt/c/Users/sclake/Dropbox/ipvzero/ydk-practice$ pip list Package Version ----------------------- ------------ alabaster 0.7.12 Babel 2.9.0 docutils 0.17.1 gitdb2 2.0.6 GitPython 2.1.15 imagesize 1.2.0 Jinja2 2.11.3 MarkupSafe 1.1.1 packaging 20.8 pip 20.0.2 pkg-resources 0.0.0 pyang 1.6 pybind11 2.2.2 Pygments 2.8.1 pyparsing 2.4.7 pytz 2021.1 rstr 2.2.6 setuptools 44.0.0 six 1.15.0 smmap 4.0.0 smmap2 3.0.1 snowballstemmer 2.1.0 Sphinx 1.4a1 sphinx-rtd-theme 0.1.9 wheel 0.34.2 ydk 0.8.5.post1 ydk-models-cisco-ios-xe 16.9.3.post1 ydk-models-ietf 0.1.5.post2 (venv) sclake@SCLAKE-R4PQT:/mnt/c/Users/sclake/Dropbox/ipvzero/ydk-practice$
04-19-2021 10:41 AM
04-23-2021 11:07 AM
Yan,
Ok let's continue to troubleshoot the installation of iosxe model and why ydk can't find it.
Here is a list of all installed packages in the venv:
sclake@SCLAKE-R4PQT:~/venv/bin$ source /home/sclake/venv/bin/activate
(venv) sclake@SCLAKE-R4PQT:~/venv/bin$ python3 -m pip show ydk-models-cisco-ios-xe
Name: ydk-models-cisco-ios-xe
Version: 16.9.3.post1
Summary: YDK bundle for Cisco IOS XE models
Home-page: https://github.com/CiscoDevNet/ydk-py
Author: Cisco Systems
Author-email: yang-dk@cisco.com
License: Apache 2.0
Location: /home/sclake/venv/lib/python3.8/site-packages
Requires: ydk-models-ietf, ydk
Required-by:
(venv) sclake@SCLAKE-R4PQT:~/venv/bin$ python3 -m pip show ydk-models-ietf
Name: ydk-models-ietf
Version: 0.1.5.post2
Summary: YDK bundle for IETF models
Home-page: https://github.com/CiscoDevNet/ydk-py
Author: Cisco Systems
Author-email: yang-dk@cisco.com
License: Apache 2.0
Location: /home/sclake/venv/lib/python3.8/site-packages
Requires: ydk
Required-by: ydk-models-cisco-ios-xe
(venv) sclake@SCLAKE-R4PQT:~/venv/bin$
(venv) sclake@SCLAKE-R4PQT:~/venv/lib/python3.8$ cd site-packages
(venv) sclake@SCLAKE-R4PQT:~/venv/lib/python3.8/site-packages$ ll
total 6228
drwxr-xr-x 53 sclake sclake 4096 Apr 19 10:11 ./
drwxr-xr-x 3 sclake sclake 4096 Apr 19 09:19 ../
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 Babel-2.9.0.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 GitPython-2.1.15.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 Jinja2-2.11.3.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 MarkupSafe-1.1.1.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 Pygments-2.8.1.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 Sphinx-1.4a1.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 __pycache__/
drwxr-xr-x 4 sclake sclake 4096 Apr 19 09:20 alabaster/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 alabaster-0.7.12.dist-info/
drwxr-xr-x 6 sclake sclake 4096 Apr 19 09:20 babel/
drwxr-xr-x 9 sclake sclake 4096 Apr 19 09:20 docutils/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 docutils-0.17.1.dist-info/
-rw-r--r-- 1 sclake sclake 126 Apr 19 09:20 easy_install.py
drwxr-xr-x 8 sclake sclake 4096 Apr 19 09:20 git/
drwxr-xr-x 6 sclake sclake 4096 Apr 19 09:20 gitdb/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 gitdb2-2.0.6.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 imagesize-1.2.0.dist-info/
-rw-r--r-- 1 sclake sclake 11464 Apr 19 09:20 imagesize.py
drwxr-xr-x 3 sclake sclake 4096 Apr 19 09:20 jinja2/
drwxr-xr-x 3 sclake sclake 4096 Apr 19 09:20 markupsafe/
drwxr-xr-x 3 sclake sclake 4096 Apr 19 09:20 packaging/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 packaging-20.8.dist-info/
drwxr-xr-x 5 sclake sclake 4096 Apr 19 09:20 pip/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 pip-20.0.2.dist-info/
drwxr-xr-x 5 sclake sclake 4096 Apr 19 09:20 pkg_resources/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 pkg_resources-0.0.0.dist-info/
drwxr-xr-x 5 sclake sclake 4096 Apr 19 09:20 pyang/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 pyang-1.6.dist-info/
drwxr-xr-x 3 sclake sclake 4096 Apr 19 09:20 pybind11/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 pybind11-2.2.2.dist-info/
drwxr-xr-x 7 sclake sclake 4096 Apr 19 09:20 pygments/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 pyparsing-2.4.7.dist-info/
-rw-r--r-- 1 sclake sclake 273365 Apr 19 09:20 pyparsing.py
drwxr-xr-x 4 sclake sclake 4096 Apr 19 09:20 pytz/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 pytz-2021.1.dist-info/
drwxr-xr-x 4 sclake sclake 4096 Apr 19 09:20 rstr/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 rstr-2.2.6.dist-info/
drwxr-xr-x 6 sclake sclake 4096 Apr 19 09:20 setuptools/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 setuptools-44.0.0.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 six-1.15.0.dist-info/
-rw-r--r-- 1 sclake sclake 34159 Apr 19 09:20 six.py
drwxr-xr-x 4 sclake sclake 4096 Apr 19 09:20 smmap/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 smmap-4.0.0.dist-info/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 smmap2-3.0.1.dist-info/
drwxr-xr-x 3 sclake sclake 4096 Apr 19 09:20 snowballstemmer/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 snowballstemmer-2.1.0.dist-info/
drwxr-xr-x 15 sclake sclake 4096 Apr 19 09:20 sphinx/
drwxr-xr-x 4 sclake sclake 4096 Apr 19 09:20 sphinx_rtd_theme/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 sphinx_rtd_theme-0.1.9.dist-info/
drwxr-xr-x 4 sclake sclake 4096 Apr 19 09:20 wheel/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:20 wheel-0.34.2.dist-info/
drwxr-xr-x 14 sclake sclake 4096 Apr 19 09:28 ydk/
drwxr-xr-x 2 sclake sclake 4096 Apr 19 09:28 ydk-0.8.5.post1.dist-info/
-rwxr-xr-x 1 sclake sclake 5822144 Apr 19 09:28 ydk_.so*
-rw-r--r-- 1 sclake sclake 1656 Apr 19 10:11 ydk_models_cisco_ios_xe-16.9.3.post1-nspkg.pth
drwxr-xr-x 2 sclake sclake 4096 Apr 19 10:11 ydk_models_cisco_ios_xe-16.9.3.post1.dist-info/
-rw-r--r-- 1 sclake sclake 1656 Apr 19 10:11 ydk_models_ietf-0.1.5.post2-nspkg.pth
drwxr-xr-x 2 sclake sclake 4096 Apr 19 10:11 ydk_models_ietf-0.1.5.post2.dist-info/
(venv) sclake@SCLAKE-R4PQT:~/venv/lib/python3.8/site-packages$
Here is the error repeated from above...
ydk.errors.YInvalidArgumentError: Path is invalid: Cisco-IOS-XE-native:native
So the question here is what is the expected path for the models? Seems to me that everything is in order here.
Thanks,
Scott.
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