Skip to content

Commit 049a512

Browse files
committed
Add Get RPC
1 parent 6bfc312 commit 049a512

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

src/cisco_gnmi/cli.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env python
22
"""
3+
Wraps gNMI RPCs with a reasonably useful CLI for interacting with network elements.
4+
5+
36
Command parsing sourced from this wonderful blog by Chase Seibert
47
https://chase-seibert.github.io/blog/2014/03/21/python-multilevel-argparse.html
58
"""
@@ -8,7 +11,8 @@
811
import argparse
912
from getpass import getpass
1013
from google.protobuf import json_format, text_format
11-
from . import ClientBuilder
14+
from . import ClientBuilder, proto
15+
from google.protobuf.internal import enum_type_wrapper
1216
import sys
1317

1418

@@ -69,7 +73,7 @@ def gnmi_subscribe():
6973
)
7074
parser.add_argument(
7175
"-dump_file",
72-
help="Filename to dump to.",
76+
help="Filename to dump to. Defaults to stdout.",
7377
type=str,
7478
default="stdout"
7579
)
@@ -82,24 +86,28 @@ def gnmi_subscribe():
8286
"-sync_stop", help="Stop on sync_response.", action="store_true"
8387
)
8488
parser.add_argument(
85-
"-encoding", help="gNMI subscription encoding.", type=str, nargs="?"
89+
"-encoding", help="gNMI subscription encoding.", type=str, nargs="?", choices=list(proto.gnmi_pb2.Encoding.keys())
8690
)
8791
args = __common_args_handler(parser)
88-
# Set default XPath outside of argparse
92+
# Set default XPath outside of argparse due to default being persistent in argparse.
8993
if not args.xpath:
9094
args.xpath = ["/interfaces/interface/state/counters"]
9195
client = __gen_client(args)
96+
# Take care not to override options unnecessarily.
9297
kwargs = {}
9398
if args.encoding:
9499
kwargs["encoding"] = args.encoding
95100
if args.sample_interval:
96101
kwargs["sample_interval"] = args.sample_interval
97102
try:
98-
logging.info("Subscribing to %s ...", args.xpath)
103+
logging.info("Dumping responses to %s as %s ...", args.dump_file, "JSON" if args.dump_json else "textual proto")
104+
logging.info("Subscribing to:\n%s", '\n'.join(args.xpath))
99105
for subscribe_response in client.subscribe_xpaths(args.xpath, **kwargs):
100-
if subscribe_response.sync_response and args.sync_stop:
101-
logging.warning("Stopping on sync_response.")
102-
break
106+
if subscribe_response.sync_response:
107+
logging.debug("sync_response received.")
108+
if args.sync_stop:
109+
logging.warning("Stopping on sync_response.")
110+
break
103111
formatted_message = None
104112
if args.dump_json:
105113
formatted_message = json_format.MessageToJson(subscribe_response, sort_keys=True)
@@ -119,8 +127,40 @@ def gnmi_get():
119127
parser = argparse.ArgumentParser(
120128
description="Performs Get RPC against network element."
121129
)
130+
parser.add_argument(
131+
"-xpath",
132+
help="XPaths to Get.",
133+
type=str,
134+
action="append"
135+
)
136+
parser.add_argument(
137+
"-encoding", help="gNMI subscription encoding.", type=str, nargs="?", choices=list(proto.gnmi_pb2.Encoding.keys())
138+
)
139+
parser.add_argument(
140+
"-data_type", help="gNMI GetRequest DataType", type=str, nargs="?", choices=list(enum_type_wrapper.EnumTypeWrapper(proto.gnmi_pb2._GETREQUEST_DATATYPE).keys())
141+
)
142+
parser.add_argument(
143+
"-dump_json",
144+
help="Dump as JSON instead of textual protos.",
145+
action="store_true"
146+
)
122147
args = __common_args_handler(parser)
148+
# Set default XPath outside of argparse due to default being persistent in argparse.
149+
if not args.xpath:
150+
args.xpath = ["/interfaces/interface/state/counters"]
123151
client = __gen_client(args)
152+
kwargs = {}
153+
if args.encoding:
154+
kwargs["encoding"] = args.encoding
155+
if args.data_type:
156+
kwargs["data_type"] = args.data_type
157+
get_response = client.get_xpaths(args.xpath, **kwargs)
158+
formatted_message = None
159+
if args.dump_json:
160+
formatted_message = json_format.MessageToJson(get_response, sort_keys=True)
161+
else:
162+
formatted_message = text_format.MessageToString(get_response)
163+
logging.info(formatted_message)
124164

125165
def gnmi_set():
126166
parser = argparse.ArgumentParser(

0 commit comments

Comments
 (0)