1
1
#!/usr/bin/env python
2
2
"""
3
+ Wraps gNMI RPCs with a reasonably useful CLI for interacting with network elements.
4
+
5
+
3
6
Command parsing sourced from this wonderful blog by Chase Seibert
4
7
https://chase-seibert.github.io/blog/2014/03/21/python-multilevel-argparse.html
5
8
"""
8
11
import argparse
9
12
from getpass import getpass
10
13
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
12
16
import sys
13
17
14
18
@@ -69,7 +73,7 @@ def gnmi_subscribe():
69
73
)
70
74
parser .add_argument (
71
75
"-dump_file" ,
72
- help = "Filename to dump to." ,
76
+ help = "Filename to dump to. Defaults to stdout. " ,
73
77
type = str ,
74
78
default = "stdout"
75
79
)
@@ -82,24 +86,28 @@ def gnmi_subscribe():
82
86
"-sync_stop" , help = "Stop on sync_response." , action = "store_true"
83
87
)
84
88
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 ())
86
90
)
87
91
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.
89
93
if not args .xpath :
90
94
args .xpath = ["/interfaces/interface/state/counters" ]
91
95
client = __gen_client (args )
96
+ # Take care not to override options unnecessarily.
92
97
kwargs = {}
93
98
if args .encoding :
94
99
kwargs ["encoding" ] = args .encoding
95
100
if args .sample_interval :
96
101
kwargs ["sample_interval" ] = args .sample_interval
97
102
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 ))
99
105
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
103
111
formatted_message = None
104
112
if args .dump_json :
105
113
formatted_message = json_format .MessageToJson (subscribe_response , sort_keys = True )
@@ -119,8 +127,40 @@ def gnmi_get():
119
127
parser = argparse .ArgumentParser (
120
128
description = "Performs Get RPC against network element."
121
129
)
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
+ )
122
147
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" ]
123
151
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 )
124
164
125
165
def gnmi_set ():
126
166
parser = argparse .ArgumentParser (
0 commit comments