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

gNMI provider requires `port` argument #880

Closed 111pontes closed 5 years ago

111pontes commented 5 years ago

gNMI provider documents the port argument as optional (http://ydk.cisco.com/py/docs/api/providers/gnmi_provider.html).

* port – (int) Port on which the gNMI interface can be accessed on the device. If not specified, the default value of 57400 is assigned.

However, it is treated as mandatory in ydk-py:

    provider = gNMIServiceProvider(repo=repo,
                                   address=address,
                                   username=username,
                                   password=password)

results in:

TypeError: __init__() missing 1 required positional argument: 'port'

The port argument can be set to None as a workaround:

    provider = gNMIServiceProvider(repo=repo,
                                   address=address,
                                   port=None,
                                   username=username,
                                   password=password)

Verified with:

$ pip list | grep ydk
ydk                     0.8.1.post1
ydk-models-cisco-ios-xr 6.5.1.post1
ydk-models-ietf         0.1.5.post2
ydk-models-openconfig   0.1.6.post1
ydk-service-gnmi        0.4.0.post1
$
ygorelik commented 5 years ago

Test script

#!/usr/bin/env python
# =======================================================================
# Copyright 2018 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.
# ========================================================================
"""
hello_ydk_gnmi.py
Read all data for model Cisco-IOS-XR-shellutil-oper.yang and print system name and uptime.

Usage: hello_ydk_gnmi.py [-h] [-v] device repo
Positional arguments:
  device      gNMI enabled device (ssh://user:password@host:port)
  repo        yang model repository location - full path to repository directory
Optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  print debugging messages
Example:
  hello_ydk_gnmi.py -v ssh://root:Cisco123!@172.27.150.154:57800 /home/yan/.ydk/172.27.150.154_830
"""
import logging
from datetime import timedelta
from argparse import ArgumentParser
import sys
if sys.version_info > (3,):
    from urllib.parse import urlparse
else:
    from urlparse import urlparse

from test_utils import enable_logging

# import providers, services and models
from ydk.path import Repository
from ydk.services import CRUDService
from ydk.providers import NetconfServiceProvider
from ydk.gnmi.providers import gNMIServiceProvider

from ydk.models.cisco_ios_xr import Cisco_IOS_XR_shellutil_oper as xr_shellutil_oper

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="gNMI device (ssh://user:password@host:port)")
    parser.add_argument("repo",
                        help="yang model repository location - full path to repository directory")

    args = parser.parse_args()
    device = urlparse(args.device)
    repo_path = args.repo

    # log debug messages if verbose argument specified
    if args.verbose:
        enable_logging(logging.INFO)

    # create gNMI session
    repo = Repository(repo_path)
    provider = gNMIServiceProvider(repo,
                                   address=device.hostname,
                                   username=device.username,
                                   password=device.password)
    # create CRUD service
    crud = CRUDService()

    # create system time object
    system_time = xr_shellutil_oper.SystemTime()

    # read system time from device
    system_time = crud.read(provider, system_time)

    # print system name and uptime
    print("System '%s' uptime is "%system_time.uptime.host_name +
          str(timedelta(seconds=system_time.uptime.uptime)))

Script execute command (note, port is not specified)

./test_880.py -v ssh://admin:admin@192.168.122.169 /home/ygorelik/.ydk/172.27.150.154_830/

Run results

2019-03-06 08:58:27,288 - ydk - INFO - gNMIServiceProvider Connected to 192.168.122.169 via Insecure Channel
2019-03-06 08:58:27,289 - ydk - INFO - Executing CRUD read operation on [Cisco-IOS-XR-shellutil-oper:system-time]
2019-03-06 08:58:27,289 - ydk - INFO -
=============== Get Request Sent ================
path {
  origin: "Cisco-IOS-XR-shellutil-oper"
  elem {
    name: "system-time"
  }
}
encoding: JSON_IETF

2019-03-06 08:58:27,315 - ydk - INFO -
============= Get Response Received =============
notification {
  timestamp: 1551891506343917025
  update {
    path {
      origin: "Cisco-IOS-XR-shellutil-oper"
      elem {
        name: "system-time"
      }
    }
    val {
      json_ietf_val: "{\"clock\":{\"year\":2019,\"month\":3,\"day\":6,\"hour\":16,\"minute\":58,\"second\":26,\"millisecond\":332,\"wday\":3,\"time-zone\":\"UTC\",\"time-source\":\"calendar\"},\"uptime\":{\"host-name\":\"gnmi\",\"uptime\":3568}}"
    }
  }
}
error {
}

2019-03-06 08:58:27,316 - ydk - INFO - Get Operation Succeeded
System 'gnmi' uptime is 0:59:28
2019-03-06 08:58:27,318 - ydk - INFO - Disconnected from device