@@ -45,37 +45,102 @@ def gnmi_capabilities():
45
45
description = "Performs Capabilities RPC against network element."
46
46
)
47
47
args = __common_args_handler (parser )
48
+ client = __gen_client (args )
49
+ logging .info (client .capabilities ())
48
50
49
51
def gnmi_subscribe ():
52
+ """Performs a sampled Subscribe against network element.
53
+ TODO: ON_CHANGE
54
+ """
50
55
parser = argparse .ArgumentParser (
51
56
description = "Performs Subscribe RPC against network element."
52
57
)
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
+ )
53
87
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!" )
55
117
56
118
def gnmi_get ():
57
119
parser = argparse .ArgumentParser (
58
120
description = "Performs Get RPC against network element."
59
121
)
60
122
args = __common_args_handler (parser )
123
+ client = __gen_client (args )
61
124
62
125
def gnmi_set ():
63
126
parser = argparse .ArgumentParser (
64
127
description = "Performs Set RPC against network element."
65
128
)
66
129
args = __common_args_handler (parser )
130
+ client = __gen_client (args )
131
+
67
132
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 ]):
73
138
builder .set_secure_from_target ()
74
139
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 :
79
144
builder .set_ssl_target_override ()
80
145
return builder .construct ()
81
146
0 commit comments