Skip to content
Open
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
25 changes: 21 additions & 4 deletions gnmi_cli_py/py_gnmicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import ssl
import sys
import string
import time
import six
import datetime
try:
Expand Down Expand Up @@ -153,6 +154,8 @@ def _create_parser():
required=False, action='store_true')
parser.add_argument('--interval', default=10000, type=int,
help='sample interval in millisecond (default: 10000ms)')
parser.add_argument('--polling_interval', default=10000, type=int,
help='polling interval in millisecond (default: 10000ms)')
parser.add_argument('--timeout', type=int, help='subscription'
'duration in seconds (default: none)')
parser.add_argument('--heartbeat', default=0, type=int, help='heartbeat interval (default: None)')
Expand All @@ -165,6 +168,7 @@ def _create_parser():
parser.add_argument('--encoding', default=0, type=int, help='[0=JSON, 1=BYTES, 2=PROTO, 3=ASCII, 4=JSON_IETF]')
parser.add_argument('--qos', default=0, type=int, help='')
parser.add_argument('--use_alias', action='store_true', help='use alias')
parser.add_argument('--updates_only', action='store_true', help='updates only')
parser.add_argument('--create_connections', type=int, nargs='?', const=1, default=1,
help='Creates specific number of TCP connections with gNMI server side. '
'Default number of TCP connections is 1 and use -1 to create '
Expand Down Expand Up @@ -284,7 +288,7 @@ def _create_stub(creds, target, port, host_override):
channel = gnmi_pb2_grpc.grpc.secure_channel(target + ':' + port, creds)
else:
channel = gnmi_pb2_grpc.grpc.insecure_channel(target + ':' + port)
return gnmi_pb2_grpc.gNMIStub(channel)
return gnmi_pb2_grpc.gNMIStub(channel), channel


def _format_type(json_value):
Expand Down Expand Up @@ -467,12 +471,19 @@ def gen_request(paths, opt, prefix):
myqos = None
mysblist = gnmi_pb2.SubscriptionList(prefix=myprefix, mode=opt['subscribe_mode'],
allow_aggregation=opt['aggregate'], encoding=opt['encoding'],
subscription=mysubs, use_aliases=opt['use_alias'], qos=myqos)
subscription=mysubs, use_aliases=opt['use_alias'], qos=myqos,
updates_only=opt['updates_only'])
mysubreq = gnmi_pb2.SubscribeRequest(subscribe=mysblist)

print('Sending SubscribeRequest\n'+str(mysubreq))
yield mysubreq

if opt["subscribe_mode"] == 2:
while True:
time.sleep(opt['polling_interval']/1000.0)
mysubreq = gnmi_pb2.SubscribeRequest(poll=gnmi_pb2.Poll())
print('Sending SubscribeRequest\n'+str(mysubreq))
yield mysubreq

def check_event_response(response, filter_event_regex):
resp = str(response)
Expand All @@ -496,9 +507,12 @@ def subscribe_start(stub, options, req_iterator):
try:
responses = stub.Subscribe(req_iterator, options['timeout'], metadata=metadata)
update_count = 0
synced = False
for response in responses:
print('{0} response received: '.format(datetime.datetime.now()))
if response.HasField('sync_response'):
synced = True
update_count = update_count+1
print(str(response))
elif response.HasField('error'):
print('gNMI Error '+str(response.error.code)+\
Expand All @@ -514,7 +528,8 @@ def subscribe_start(stub, options, req_iterator):
raise Exception("Filter event regex should not be empty")
else:
print(response)
update_count = update_count+1
if options["subscribe_mode"] != 2 and synced: # no polling
update_count = update_count+1
else:
print('Unknown response received:\n'+str(response))

Expand Down Expand Up @@ -583,7 +598,7 @@ def main():
break

try:
stub = _create_stub(creds, target, port, host_override)
stub, channel = _create_stub(creds, target, port, host_override)
if mode == 'get':
print('Performing GetRequest, encoding=JSON_IETF', 'to', target,
' with the following gNMI Path\n', '-'*25, '\n', paths)
Expand Down Expand Up @@ -637,6 +652,8 @@ def main():
else:
print("GRPC error\n {}".format(err.details()))
sys.exit(1)
finally:
channel.close()


if __name__ == '__main__':
Expand Down