Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed pyc #8

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,48 @@ Python tools for gNMI
## Installation
Python tools for gNMI can be used with Python 2.7 and Python3.
Please follow gRPC Installation Guide from:
https://github.com/grpc/grpc/blob/master/INSTALL.md
https://grpc.io/docs/quickstart/python

```
$ pip install --upgrade pip
$ pip install grpcio
$ curl -O https://raw.githubusercontent.com/openconfig/gnmi/c5b444cd3ab8af669d0b8934f47a41ed6a985cdc/proto/gnmi/gnmi_pb2.py
$ python gNMI_Subscribe.py --help
$ pip install grpcio-tools googleapis-common-protos
$ curl -O https://raw.githubusercontent.com/openconfig/gnmi/master/proto/gnmi/gnmi_pb2.py
$ python pygnmi.py --help
```

## Usage Example:
## The default service is 'capabilities'

## Usage Example (CAPABILITIES):

```
$ pygnmi.py --service capabilities --username grpc --password nokia123 \
--server 192.168.5.10:57400

18/07/02 08:21:51,712 Create insecure channel to 192.168.5.10:57400 Username: grpc Password: nokia123
18/07/02 08:21:51,714 Obtaining capabilities from 192.168.5.10:57400
supported_models {
name: "nokia-conf"
organization: "Nokia"
version: "16.0.R1"
}
supported_models {
name: "nokia-state"
organization: "Nokia"
version: "16.0.R1"
}
supported_encodings: JSON
gNMI_version: "0.4.0"

```
$ python gNMI_Subscribe.py --server 192.168.33.2:57400 --username grpc --password Nokia4gnmi --cert CAcert.pem --ciphers AES128 /state/port[port-id=1/1/1]/ethernet/statistics/out-utilization

## Usage Example (SUBSCRIBE):

```
$ python pygnmi.py --server 192.168.33.2:57400 --username grpc --password Nokia4gnmi \
--service subscribe --cert CAcert.pem \
--ciphers AES128 /state/port[port-id=1/1/1]/ethernet/statistics/out-utilization

17/12/04 16:02:43,160 Sending SubscribeRequest
subscribe {
subscription {
Expand Down Expand Up @@ -81,5 +110,5 @@ update {


^C
17/12/04 16:03:04,511 gNMI_Subscribe stopped by user
17/12/04 16:03:04,511 pygnmi stopped by user
```
14 changes: 14 additions & 0 deletions gNMI_Capabilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def get_capabilities(channel, options, log):
try:
import grpc
import gnmi_pb2
except ImportError as err:
log.error(str(err))
quit()
stub = gnmi_pb2.gNMIStub(channel)
metadata = [('username',options.username), ('password',options.password)]
responses = gnmi_pb2.CapabilityResponse()
log.info("Obtaining capabilities from "+options.server)
responses = stub.Capabilities(gnmi_pb2.CapabilityRequest(),5,metadata=metadata)
return responses

127 changes: 127 additions & 0 deletions gNMI_Get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/python

##############################################################################
# #
# gNMI_Get.py #
# #
# History Change Log: #
# #
# 1.0 [JGC] 2018/06/26 first version #
# #
# Objective: #
# #
# gNMI Get (GRPC Network Management Interface) in Python #
# #
# License: #
# #
# Licensed under the MIT license #
# See LICENSE.md delivered with this project for more information. #
# #
# Author: #
# #
# James Cumming [JGC] #
# mail: james.cumming(at)nokia.com #
# #
##############################################################################

"""
gNMI Get in Python Version 1.0
Copyright (C) 2018 Nokia. All Rights Reserved.
"""

__title__ = "gNMI_Get"
__version__ = "1.0"
__status__ = "dev"
__author__ = "James Cumming"
__date__ = "2018 June 26th"

##############################################################################

import argparse
import re
import sys
import os
import time
import grpc_support

##############################################################################

def gen_request( opt, log ):
import gnmi_pb2
mypaths = []
for path in opt.xpaths:
mypath = grpc_support.path_from_string(path)
mypaths.append(mypath)

if opt.prefix:
myprefix = path_from_string(opt.prefix)
else:
myprefix = None

if opt.qos:
myqos = gnmi_pb2.QOSMarking(marking=opt.qos)
else:
myqos = None

return gnmi_pb2.GetRequest(path=mypaths)


def get(channel, options, log, prog):
try:
import grpc
import gnmi_pb2
except ImportError as err:
log.error(str(err))
quit()

log.debug("Create gNMI stub")
stub = gnmi_pb2.gNMIStub(channel)

req_iterator = gen_request( options, log )
metadata = [('username',options.username), ('password', options.password)]

msgs = 0
upds = 0
secs = 0
start = 0

try:
response = gnmi_pb2.GetResponse()
response = stub.Get(req_iterator, options.timeout, metadata=metadata)

# if response.HasField('notification'):
# log.debug('Sync Response received\n'+str(response))
# secs += time.time() - start
# start = 0
# if options.stats:
# log.info("%d updates and %d messages within %1.2f seconds", upds, msgs, secs)
# log.info("Statistics: %5.0f upd/sec, %5.0f msg/sec", upds/secs, msgs/secs)
# elif response.HasField('error'):
# log.error('gNMI Error '+str(response.error.code)+' received\n'+str(response.error.message))
# elif response.HasField('update'):
# if start==0:
# start=time.time()
# msgs += 1
# upds += len(response.update.update)
# if not options.stats:
# log.info('Update received\n'+str(response))
# else:
# log.error('Unknown response received:\n'+str(response))

except KeyboardInterrupt:
log.info("%s stopped by user", prog)

except grpc.RpcError as x:
log.error("grpc.RpcError received:\n%s", x.details)

except Exception as err:
log.error(err)

if (msgs>1):
log.info("%d update messages received", msgs)
return msgs

return response

# EOF

Loading