Skip to content

Commit dadfd60

Browse files
committed
Add Set RPC
1 parent 049a512 commit dadfd60

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

src/cisco_gnmi/cli.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def main():
4242
logging.error("%s not in supported RPCs: %s!", args.rpc, ', '.join(rpc_map.keys()))
4343
parser.print_help()
4444
exit(1)
45-
rpc_map[args.rpc]()
45+
try:
46+
rpc_map[args.rpc]()
47+
except:
48+
logging.exception("Error during usage!")
49+
exit(1)
4650

4751
def gnmi_capabilities():
4852
parser = argparse.ArgumentParser(
@@ -124,6 +128,9 @@ def gnmi_subscribe():
124128
logging.exception("Stopping due to exception!")
125129

126130
def gnmi_get():
131+
"""Provides Get RPC usage. Assumes JSON or JSON_IETF style configurations.
132+
TODO: This is the least well understood/implemented. Need to validate if there is an OOO for update/replace/delete.
133+
"""
127134
parser = argparse.ArgumentParser(
128135
description="Performs Get RPC against network element."
129136
)
@@ -163,12 +170,71 @@ def gnmi_get():
163170
logging.info(formatted_message)
164171

165172
def gnmi_set():
173+
"""Provides Set RPC usage. Assumes JSON or JSON_IETF style configurations.
174+
Applies update/replace operations, and then delete operations.
175+
TODO: This is the least well understood/implemented. Need to validate if there is an OOO for update/replace/delete.
176+
"""
166177
parser = argparse.ArgumentParser(
167178
description="Performs Set RPC against network element."
168179
)
180+
parser.add_argument(
181+
"-update_json_config",
182+
description="JSON-modeled config to apply as an update."
183+
)
184+
parser.add_argument(
185+
"-replace_json_config",
186+
description="JSON-modeled config to apply as an update."
187+
)
188+
parser.add_argument(
189+
"-delete_xpath",
190+
help="XPaths to delete.",
191+
type=str,
192+
action="append"
193+
)
194+
parser.add_argument(
195+
"-no_ietf",
196+
help="JSON is not IETF conformant.",
197+
action="store_true"
198+
)
199+
parser.add_argument(
200+
"-dump_json",
201+
help="Dump as JSON instead of textual protos.",
202+
action="store_true"
203+
)
169204
args = __common_args_handler(parser)
205+
if not any([args.update_json_config, args.replace_json_config, args.delete_xpath]):
206+
raise Exception("Must specify update, replace, or delete parameters!")
207+
def load_json_file(filename):
208+
config = None
209+
with open(filename, "r") as config_fd:
210+
config = json.load(config_fd)
211+
return config
212+
kwargs = {}
213+
if args.update_json_config:
214+
kwargs["update_json_configs"] = load_json_file(args.update_json_config)
215+
if args.replace_json_config:
216+
kwargs["replace_json_configs"] = load_json_file(args.replace_json_config)
217+
if args.no_ietf:
218+
kwargs["ietf"] = False
170219
client = __gen_client(args)
171-
220+
set_response = client.set_json(**kwargs)
221+
formatted_message = None
222+
if args.dump_json:
223+
formatted_message = json_format.MessageToJson(set_response, sort_keys=True)
224+
else:
225+
formatted_message = text_format.MessageToString(set_response)
226+
logging.info(formatted_message)
227+
if args.delete_xpath:
228+
if getattr(client, "delete_xpaths", None) is not None:
229+
delete_response = client.delete_xpaths(args.xpath)
230+
formatted_message = None
231+
if args.dump_json:
232+
formatted_message = json_format.MessageToJson(delete_response, sort_keys=True)
233+
else:
234+
formatted_message = text_format.MessageToString(delete_response)
235+
logging.info(formatted_message)
236+
else:
237+
raise Exception("Convenience delete_xpaths is not supported in the client library!")
172238

173239
def __gen_client(args):
174240
builder = ClientBuilder(args.netloc)

0 commit comments

Comments
 (0)