CiscoDevNet / ydk-gen

Generate model-driven APIs from YANG models
http://ciscodevnet.github.io/ydk-gen/
Apache License 2.0
137 stars 74 forks source link

Delete operation fails on container #931

Closed dasomeom closed 5 years ago

dasomeom commented 5 years ago

Expected Behavior

crud.delete should allow you to delete a container in an element list.

Current Behavior

Execution fails with the following error:

2019-06-17 16:07:20,966 - ydk - INFO - Path where models are to be downloaded: /Users/deom/.ydk/domingo
2019-06-17 16:07:20,979 - ydk - INFO - Connected to domingo on port 830 using ssh with timeout of -1
2019-06-17 16:07:20,996 - ydk - INFO - Executing CRUD delete operation on [mpls]
2019-06-17 16:07:20,996 - ydk - INFO - Executing 'edit-config' RPC on [mpls]
Traceback (most recent call last):
  File "./nc-delete-oc-mpls-20-ydk.py", line 89, in <module>
    crud.delete(provider, network_instances.network_instance["default"].mpls)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/errors/error_handler.py", line 112, in helper
    return func(self, provider, entity, *args, **kwargs)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/services/crud_service.py", line 105, in delete
    return self._crud.delete(provider, entity)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/errors/error_handler.py", line 82, in handle_runtime_error
    _raise(_exc)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/errors/error_handler.py", line 56, in _raise
    raise exc
ydk.errors.YInvalidArgumentError:  ancestor for entity cannot be nullptr as one of the ancestors is a list. Path: mpls
2019-06-17 16:07:21,003 - ydk - INFO - Disconnected from device

Your Script

from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.openconfig import openconfig_network_instance \
    as oc_network_instance

def config_mpls(network_instances):
    """Add config data to bgp object."""
    # configure default network instance
    network_instance = network_instances.NetworkInstance()
    network_instance.name = "default"
    network_instance.config.name = "default"
    network_instances.network_instance.append(network_instance)

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)

    # 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()

    # BGP configuration
    network_instances = oc_network_instance.NetworkInstances()
    config_mpls(network_instances)  # add object configuration

    # create configuration on NETCONF device
    crud.delete(provider, network_instances.network_instance["default"].mpls)

Logs

Enable logging and post the logs below

2019-06-17 16:07:20,966 - ydk - INFO - Path where models are to be downloaded: /Users/deom/.ydk/domingo
2019-06-17 16:07:20,979 - ydk - INFO - Connected to domingo on port 830 using ssh with timeout of -1
2019-06-17 16:07:20,996 - ydk - INFO - Executing CRUD delete operation on [mpls]
2019-06-17 16:07:20,996 - ydk - INFO - Executing 'edit-config' RPC on [mpls]
Traceback (most recent call last):
  File "./nc-delete-oc-mpls-20-ydk.py", line 89, in <module>
    crud.delete(provider, network_instances.network_instance["default"].mpls)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/errors/error_handler.py", line 112, in helper
    return func(self, provider, entity, *args, **kwargs)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/services/crud_service.py", line 105, in delete
    return self._crud.delete(provider, entity)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/errors/error_handler.py", line 82, in handle_runtime_error
    _raise(_exc)
  File "/Users/deom/v/lib/python2.7/site-packages/ydk/errors/error_handler.py", line 56, in _raise
    raise exc
ydk.errors.YInvalidArgumentError:  ancestor for entity cannot be nullptr as one of the ancestors is a list. Path: mpls
2019-06-17 16:07:21,003 - ydk - INFO - Disconnected from device

System Information

ydk                     0.8.3      
ydk-models-cisco-ios-xr 6.6.2      
ydk-models-ietf         0.1.5.post2
ydk-models-openconfig   0.1.7      
ydk-service-gnmi        0.4.0.post2
ygorelik commented 5 years ago

The script sends to CRUD service non-top-level container entity on delete operation. The YDK fails due to two reasons:

  1. The YDK cannot traverse this entity to the top level node, which is required by the netconf RPC, - the 'mpls' entity does not have information about 'network-instance' key.
  2. The only currently implemented way to include a container without specifying its children or leaves to payload is to mark it with yfilter.

The corrected script for 'mpls' container delete operation should mark the mpls entity with YFiletr.delete and send to the CRUD the top-level entity, which is 'network-instance':

import argparse
import logging

from urllib.parse import urlparse

from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.models.ydktest_oc_nis import openconfig_network_instance as oc_network_instance
from ydk.filters import YFilter

from test_utils import enable_logging

def config_nis():
    """Build network instance configuration"""
    # configure default network instance
    nis = oc_network_instance.NetworkInstances()
    network_instance = oc_network_instance.NetworkInstances.NetworkInstance()
    network_instance.name = "default"
    network_instance.config.name = "default"
    nis.network_instance.append(network_instance)
    return nis

if __name__ == "__main__":
    parser = argparse.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)

    if args.verbose:
        enable_logging(logging.INFO)

    # 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()

    # NIS configuration
    network_instances = config_nis()  # configuration

    # execute CRUD operation
    network_instances.network_instance['default'].mpls.yfilter = YFilter.delete
    crud.update(provider, network_instances)

Runing results:

/home/yan/.virtualenvs/ydk81/bin/python /home/yan/ydk-workspace/ydk-gen/scripts/tests/issues/test_931.py -v ssh://admin:admin@10.30.110.84:830
2019-07-17 17:24:07,327 - ydk - INFO - Path where models are to be downloaded: /home/yan/.ydk/10.30.110.84
2019-07-17 17:24:07,338 - ydk - INFO - Connected to 10.30.110.84 on port 830 using ssh with timeout of -1
2019-07-17 17:24:07,356 - ydk - INFO - Executing CRUD update operation on [openconfig-network-instance:network-instances]
2019-07-17 17:24:07,356 - ydk - INFO - Executing 'edit-config' RPC on [openconfig-network-instance:network-instances]
2019-07-17 17:24:07,457 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Reached limit (65535) for storing typedefs.
2019-07-17 17:24:07,457 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Module "cisco-xr-openconfig-network-instance-deviations" parsing failed.
2019-07-17 17:24:07,474 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Reached limit (65535) for storing typedefs.
2019-07-17 17:24:07,474 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Module "cisco-xr-openconfig-network-instance-deviations" parsing failed.
2019-07-17 17:24:07,482 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <target>
    <candidate/>
  </target>
  <config><network-instances xmlns="http://openconfig.net/yang/network-instance" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="merge">
  <network-instance>
    <name>default</name>
    <config>
      <name>default</name>
    </config>
    <mpls nc:operation="delete"/>
  </network-instance>
</network-instances>
</config>
</edit-config>
</rpc>
2019-07-17 17:24:07,848 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
  <ok/>
</rpc-reply>

2019-07-17 17:24:07,848 - ydk - INFO - Executing 'commit' RPC
2019-07-17 17:24:07,848 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><commit xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"/>
</rpc>
2019-07-17 17:24:14,403 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
  <ok/>
</rpc-reply>

2019-07-17 17:24:14,403 - ydk - INFO - Operation succeeded
2019-07-17 17:24:14,403 - ydk - INFO - Disconnected from device

Process finished with exit code 0
ygorelik commented 5 years ago

Fixed in this commit to 0.8.4 branch.

/home/yan/.virtualenvs/ydk81/bin/python /home/yan/ydk-workspace/ydk-gen/scripts/issues/test_931.py -v ssh://admin:admin@10.30.110.84:830
2019-07-18 15:24:42,598 - ydk - INFO - Path where models are to be downloaded: /home/yan/.ydk/10.30.110.84
2019-07-18 15:24:42,610 - ydk - INFO - Connected to 10.30.110.84 on port 830 using ssh with timeout of -1
2019-07-18 15:24:42,629 - ydk - INFO - Executing CRUD delete operation on [mpls]
2019-07-18 15:24:42,629 - ydk - INFO - Executing 'edit-config' RPC on [mpls]
2019-07-18 15:24:42,734 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Reached limit (65535) for storing typedefs.
2019-07-18 15:24:42,734 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Module "cisco-xr-openconfig-network-instance-deviations" parsing failed.
2019-07-18 15:24:42,751 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Reached limit (65535) for storing typedefs.
2019-07-18 15:24:42,751 - ydk - ERROR - Data is invalid according to the yang model. Libyang error: Module "cisco-xr-openconfig-network-instance-deviations" parsing failed.
2019-07-18 15:24:42,759 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <target>
    <candidate/>
  </target>
  <config><network-instances xmlns="http://openconfig.net/yang/network-instance" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
  <network-instance>
    <name>default</name>
    <config>
      <name>default</name>
    </config>
    <mpls nc:operation="delete"/>
  </network-instance>
</network-instances>
</config>
</edit-config>
</rpc>
2019-07-18 15:24:43,099 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
  <ok/>
</rpc-reply>

2019-07-18 15:24:43,100 - ydk - INFO - Executing 'commit' RPC
2019-07-18 15:24:43,103 - ydk - INFO - ============= Sending RPC to device =============
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><commit xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"/>
</rpc>
2019-07-18 15:24:43,522 - ydk - INFO - ============= Received RPC from device =============
<?xml version="1.0"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
  <ok/>
</rpc-reply>

2019-07-18 15:24:43,523 - ydk - INFO - Operation succeeded
2019-07-18 15:24:43,524 - ydk - INFO - Disconnected from device

Process finished with exit code 0