Skip to content

Commit 6bfc312

Browse files
committed
Add Subscribe RPC
1 parent 45b5a60 commit 6bfc312

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

src/cisco_gnmi/cli.py

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,102 @@ def gnmi_capabilities():
4545
description="Performs Capabilities RPC against network element."
4646
)
4747
args = __common_args_handler(parser)
48+
client = __gen_client(args)
49+
logging.info(client.capabilities())
4850

4951
def gnmi_subscribe():
52+
"""Performs a sampled Subscribe against network element.
53+
TODO: ON_CHANGE
54+
"""
5055
parser = argparse.ArgumentParser(
5156
description="Performs Subscribe RPC against network element."
5257
)
58+
parser.add_argument(
59+
"-xpath",
60+
help="XPath to subscribe to.",
61+
type=str,
62+
action="append"
63+
)
64+
parser.add_argument(
65+
"-interval",
66+
help="Sample interval in seconds for Subscription.",
67+
type=int,
68+
default=10*int(1e9)
69+
)
70+
parser.add_argument(
71+
"-dump_file",
72+
help="Filename to dump to.",
73+
type=str,
74+
default="stdout"
75+
)
76+
parser.add_argument(
77+
"-dump_json",
78+
help="Dump as JSON instead of textual protos.",
79+
action="store_true"
80+
)
81+
parser.add_argument(
82+
"-sync_stop", help="Stop on sync_response.", action="store_true"
83+
)
84+
parser.add_argument(
85+
"-encoding", help="gNMI subscription encoding.", type=str, nargs="?"
86+
)
5387
args = __common_args_handler(parser)
54-
pass
88+
# Set default XPath outside of argparse
89+
if not args.xpath:
90+
args.xpath = ["/interfaces/interface/state/counters"]
91+
client = __gen_client(args)
92+
kwargs = {}
93+
if args.encoding:
94+
kwargs["encoding"] = args.encoding
95+
if args.sample_interval:
96+
kwargs["sample_interval"] = args.sample_interval
97+
try:
98+
logging.info("Subscribing to %s ...", args.xpath)
99+
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
103+
formatted_message = None
104+
if args.dump_json:
105+
formatted_message = json_format.MessageToJson(subscribe_response, sort_keys=True)
106+
else:
107+
formatted_message = text_format.MessageToString(subscribe_response)
108+
if args.dump_file == "stdout":
109+
logging.info(formatted_message)
110+
else:
111+
with open(args.dump_file, "a") as dump_fd:
112+
dump_fd.write(formatted_message)
113+
except KeyboardInterrupt:
114+
logging.warning("Stopping on interrupt.")
115+
except Exception:
116+
logging.exception("Stopping due to exception!")
55117

56118
def gnmi_get():
57119
parser = argparse.ArgumentParser(
58120
description="Performs Get RPC against network element."
59121
)
60122
args = __common_args_handler(parser)
123+
client = __gen_client(args)
61124

62125
def gnmi_set():
63126
parser = argparse.ArgumentParser(
64127
description="Performs Set RPC against network element."
65128
)
66129
args = __common_args_handler(parser)
130+
client = __gen_client(args)
131+
67132

68-
def __gen_client(netloc, os_name, username, password, root_certificates=None, private_key=None, certificate_chain=None, ssl_target_override=None, auto_ssl_target_override=False):
69-
builder = ClientBuilder(netloc)
70-
builder.set_os(os_name)
71-
builder.set_call_authentication(username, password)
72-
if not any([root_certificates, private_key, certificate_chain]):
133+
def __gen_client(args):
134+
builder = ClientBuilder(args.netloc)
135+
builder.set_os(args.os)
136+
builder.set_call_authentication(args.username, args.password)
137+
if not any([args.root_certificates, args.private_key, args.certificate_chain]):
73138
builder.set_secure_from_target()
74139
else:
75-
builder.set_secure_from_file(root_certificates, private_key, certificate_chain)
76-
if ssl_target_override:
77-
builder.set_ssl_target_override(ssl_target_override)
78-
elif auto_ssl_target_override:
140+
builder.set_secure_from_file(args.root_certificates, args.private_key, args.certificate_chain)
141+
if args.ssl_target_override:
142+
builder.set_ssl_target_override(args.ssl_target_override)
143+
elif args.auto_ssl_target_override:
79144
builder.set_ssl_target_override()
80145
return builder.construct()
81146

0 commit comments

Comments
 (0)