1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2021 Vmware, Inc.
6 * Pushpasis Sarkar <spushpasis@vmware.com>
13 #include "mgmtd/mgmt.h"
14 #include "mgmtd/mgmt_be_server.h"
15 #include "mgmtd/mgmt_be_adapter.h"
16 #include "mgmtd/mgmt_fe_server.h"
17 #include "mgmtd/mgmt_fe_adapter.h"
18 #include "mgmtd/mgmt_ds.h"
19 #include "mgmtd/mgmt_history.h"
21 #include "mgmtd/mgmt_vty_clippy.c"
23 DEFPY(show_mgmt_be_adapter
,
24 show_mgmt_be_adapter_cmd
,
25 "show mgmt backend-adapter all",
29 "Display all Backend Adapters\n")
31 mgmt_be_adapter_status_write(vty
);
36 DEFPY(show_mgmt_be_xpath_reg
,
37 show_mgmt_be_xpath_reg_cmd
,
38 "show mgmt backend-yang-xpath-registry",
41 "Backend Adapter YANG Xpath Registry\n")
43 mgmt_be_xpath_register_write(vty
);
48 DEFPY(show_mgmt_fe_adapter
, show_mgmt_fe_adapter_cmd
,
49 "show mgmt frontend-adapter all [detail$detail]",
53 "Display all Frontend Adapters\n"
54 "Display more details\n")
56 mgmt_fe_adapter_status_write(vty
, !!detail
);
61 DEFPY_HIDDEN(mgmt_performance_measurement
,
62 mgmt_performance_measurement_cmd
,
63 "[no] mgmt performance-measurement",
66 "Enable performance measurement\n")
69 mgmt_fe_adapter_perf_measurement(vty
, false);
71 mgmt_fe_adapter_perf_measurement(vty
, true);
76 DEFPY(mgmt_reset_performance_stats
,
77 mgmt_reset_performance_stats_cmd
,
78 "mgmt reset-statistics",
80 "Reset the Performance measurement statistics\n")
82 mgmt_fe_adapter_reset_perf_stats(vty
);
89 "show mgmt transaction all",
93 "Display all Transactions\n")
95 mgmt_txn_status_write(vty
);
102 "show mgmt datastore [all|candidate|operational|running]$dsname",
106 "All datastores (default)\n"
107 "Candidate datastore\n"
108 "Operational datastore\n"
109 "Running datastore\n")
111 struct mgmt_ds_ctx
*ds_ctx
;
113 if (!dsname
|| dsname
[0] == 'a') {
114 mgmt_ds_status_write(vty
);
117 ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, mgmt_ds_name2id(dsname
));
119 vty_out(vty
, "ERROR: Could not access %s datastore!\n", dsname
);
120 return CMD_ERR_NO_MATCH
;
122 mgmt_ds_status_write_one(vty
, ds_ctx
);
129 "mgmt commit <check|apply|abort>$type",
132 "Validate the set of config commands\n"
133 "Validate and apply the set of config commands\n"
134 "Abort and drop the set of config commands recently added\n")
136 bool validate_only
= type
[0] == 'c';
137 bool abort
= type
[1] == 'b';
139 if (vty_mgmt_send_commit_config(vty
, validate_only
, abort
) != 0)
140 return CMD_WARNING_CONFIG_FAILED
;
144 DEFPY(mgmt_set_config_data
, mgmt_set_config_data_cmd
,
145 "mgmt set-config WORD$path VALUE",
147 "Set configuration data\n"
148 "XPath expression specifying the YANG data path\n"
149 "Value of the data to set\n")
151 strlcpy(vty
->cfg_changes
[0].xpath
, path
,
152 sizeof(vty
->cfg_changes
[0].xpath
));
153 vty
->cfg_changes
[0].value
= value
;
154 vty
->cfg_changes
[0].operation
= NB_OP_CREATE
;
155 vty
->num_cfg_changes
= 1;
157 vty
->no_implicit_commit
= true;
158 vty_mgmt_send_config_data(vty
);
159 vty
->no_implicit_commit
= false;
163 DEFPY(mgmt_delete_config_data
, mgmt_delete_config_data_cmd
,
164 "mgmt delete-config WORD$path",
166 "Delete configuration data\n"
167 "XPath expression specifying the YANG data path\n")
170 strlcpy(vty
->cfg_changes
[0].xpath
, path
,
171 sizeof(vty
->cfg_changes
[0].xpath
));
172 vty
->cfg_changes
[0].value
= NULL
;
173 vty
->cfg_changes
[0].operation
= NB_OP_DESTROY
;
174 vty
->num_cfg_changes
= 1;
176 vty
->no_implicit_commit
= true;
177 vty_mgmt_send_config_data(vty
);
178 vty
->no_implicit_commit
= false;
182 DEFPY(show_mgmt_get_config
, show_mgmt_get_config_cmd
,
183 "show mgmt get-config [candidate|operational|running]$dsname WORD$path",
185 "Get configuration data from a specific configuration datastore\n"
186 "Candidate datastore (default)\n"
187 "Operational datastore\n"
188 "Running datastore\n"
189 "XPath expression specifying the YANG data path\n")
191 const char *xpath_list
[VTY_MAXCFGCHANGES
] = {0};
192 Mgmtd__DatastoreId datastore
= MGMTD_DS_CANDIDATE
;
195 datastore
= mgmt_ds_name2id(dsname
);
197 xpath_list
[0] = path
;
198 vty_mgmt_send_get_config(vty
, datastore
, xpath_list
, 1);
202 DEFPY(show_mgmt_get_data
, show_mgmt_get_data_cmd
,
203 "show mgmt get-data [candidate|operational|running]$dsname WORD$path",
205 "Get data from a specific datastore\n"
206 "Candidate datastore\n"
207 "Operational datastore (default)\n"
208 "Running datastore\n"
209 "XPath expression specifying the YANG data path\n")
211 const char *xpath_list
[VTY_MAXCFGCHANGES
] = {0};
212 Mgmtd__DatastoreId datastore
= MGMTD_DS_OPERATIONAL
;
215 datastore
= mgmt_ds_name2id(dsname
);
217 xpath_list
[0] = path
;
218 vty_mgmt_send_get_data(vty
, datastore
, xpath_list
, 1);
222 DEFPY(show_mgmt_dump_data
,
223 show_mgmt_dump_data_cmd
,
224 "show mgmt datastore-contents [candidate|operational|running]$dsname [xpath WORD$path] [file WORD$filepath] <json|xml>$fmt",
227 "Get Datastore contents from a specific datastore\n"
228 "Candidate datastore (default)\n"
229 "Operational datastore\n"
230 "Running datastore\n"
231 "XPath expression specifying the YANG data path\n"
233 "Dump the contents to a file\n"
234 "Full path of the file\n"
238 struct mgmt_ds_ctx
*ds_ctx
;
239 Mgmtd__DatastoreId datastore
= MGMTD_DS_CANDIDATE
;
240 LYD_FORMAT format
= fmt
[0] == 'j' ? LYD_JSON
: LYD_XML
;
244 datastore
= mgmt_ds_name2id(dsname
);
246 ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, datastore
);
248 vty_out(vty
, "ERROR: Could not access datastore!\n");
249 return CMD_ERR_NO_MATCH
;
253 f
= fopen(filepath
, "w");
256 "Could not open file pointed by filepath %s\n",
262 mgmt_ds_dump_tree(vty
, ds_ctx
, path
, f
, format
);
269 DEFPY(show_mgmt_map_xpath
,
270 show_mgmt_map_xpath_cmd
,
271 "show mgmt yang-xpath-subscription WORD$path",
274 "Get YANG Backend Subscription\n"
275 "XPath expression specifying the YANG data path\n")
277 mgmt_be_xpath_subscr_info_write(vty
, path
);
281 DEFPY(mgmt_load_config
,
282 mgmt_load_config_cmd
,
283 "mgmt load-config WORD$filepath <merge|replace>$type",
285 "Load configuration onto Candidate Datastore\n"
286 "Full path of the file\n"
287 "Merge configuration with contents of Candidate Datastore\n"
288 "Replace the existing contents of Candidate datastore\n")
290 bool merge
= type
[0] == 'm' ? true : false;
291 struct mgmt_ds_ctx
*ds_ctx
;
294 if (access(filepath
, F_OK
) == -1) {
295 vty_out(vty
, "ERROR: File %s : %s\n", filepath
,
297 return CMD_ERR_NO_FILE
;
300 ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, MGMTD_DS_CANDIDATE
);
302 vty_out(vty
, "ERROR: Could not access Candidate datastore!\n");
303 return CMD_ERR_NO_MATCH
;
306 ret
= mgmt_ds_load_config_from_file(ds_ctx
, filepath
, merge
);
308 vty_out(vty
, "Error with parsing the file with error code %d\n",
313 DEFPY(mgmt_save_config
,
314 mgmt_save_config_cmd
,
315 "mgmt save-config <candidate|running>$dsname WORD$filepath",
317 "Save configuration from datastore\n"
318 "Candidate datastore\n"
319 "Running datastore\n"
320 "Full path of the file\n")
322 Mgmtd__DatastoreId datastore
= mgmt_ds_name2id(dsname
);
323 struct mgmt_ds_ctx
*ds_ctx
;
326 ds_ctx
= mgmt_ds_get_ctx_by_id(mm
, datastore
);
328 vty_out(vty
, "ERROR: Could not access the '%s' datastore!\n",
330 return CMD_ERR_NO_MATCH
;
334 vty_out(vty
, "ERROR: No file path mentioned!\n");
335 return CMD_ERR_NO_MATCH
;
338 f
= fopen(filepath
, "w");
340 vty_out(vty
, "Could not open file pointed by filepath %s\n",
345 mgmt_ds_dump_tree(vty
, ds_ctx
, "/", f
, LYD_JSON
);
352 DEFPY(show_mgmt_cmt_hist
,
353 show_mgmt_cmt_hist_cmd
,
354 "show mgmt commit-history",
357 "Show commit history\n")
359 show_mgmt_cmt_history(vty
);
365 "mgmt rollback <commit-id WORD$commit | last [(1-10)]$last>",
368 "Rollback to commit ID\n"
370 "Rollbak n commits\n"
371 "Number of commits\n")
374 mgmt_history_rollback_by_id(vty
, commit
);
376 mgmt_history_rollback_n(vty
, last
);
381 static int config_write_mgmt_debug(struct vty
*vty
);
382 static struct cmd_node debug_node
= {
386 .config_write
= config_write_mgmt_debug
,
389 static int config_write_mgmt_debug_helper(struct vty
*vty
, bool config
)
391 int n
= mgmt_debug_be
+ mgmt_debug_fe
+ mgmt_debug_ds
+ mgmt_debug_txn
;
395 if (config
&& mgmt_debug_be
&& mgmt_debug_fe
&& mgmt_debug_ds
&&
397 vty_out(vty
, "debug mgmt all\n");
401 vty_out(vty
, "debug mgmt");
403 vty_out(vty
, " backend");
405 vty_out(vty
, " datastore");
407 vty_out(vty
, " frontend");
409 vty_out(vty
, " transaction");
416 static int config_write_mgmt_debug(struct vty
*vty
)
418 return config_write_mgmt_debug_helper(vty
, true);
421 DEFUN_NOSH(show_debugging_mgmt
, show_debugging_mgmt_cmd
,
422 "show debugging [mgmt]", SHOW_STR DEBUG_STR
"MGMT Information\n")
424 vty_out(vty
, "MGMT debugging status:\n");
426 config_write_mgmt_debug_helper(vty
, false);
428 cmd_show_lib_debugs(vty
);
433 DEFPY(debug_mgmt
, debug_mgmt_cmd
,
434 "[no$no] debug mgmt <all$all|{backend$be|datastore$ds|frontend$fe|transaction$txn}>",
435 NO_STR DEBUG_STR MGMTD_STR
440 "Transaction debug\n")
444 be
= fe
= ds
= txn
= set
? all
: NULL
;
453 mgmt_debug_txn
= set
;
458 void mgmt_vty_init(void)
461 * Initialize command handling from VTYSH connection.
462 * Call command initialization routines defined by
463 * backend components that are moved to new MGMTD infra
467 extern void static_vty_init(void);
471 install_node(&debug_node
);
473 install_element(VIEW_NODE
, &show_mgmt_be_adapter_cmd
);
474 install_element(VIEW_NODE
, &show_mgmt_be_xpath_reg_cmd
);
475 install_element(VIEW_NODE
, &show_mgmt_fe_adapter_cmd
);
476 install_element(VIEW_NODE
, &show_mgmt_txn_cmd
);
477 install_element(VIEW_NODE
, &show_mgmt_ds_cmd
);
478 install_element(VIEW_NODE
, &show_mgmt_get_config_cmd
);
479 install_element(VIEW_NODE
, &show_mgmt_get_data_cmd
);
480 install_element(VIEW_NODE
, &show_mgmt_dump_data_cmd
);
481 install_element(VIEW_NODE
, &show_mgmt_map_xpath_cmd
);
482 install_element(VIEW_NODE
, &show_mgmt_cmt_hist_cmd
);
484 install_element(CONFIG_NODE
, &mgmt_commit_cmd
);
485 install_element(CONFIG_NODE
, &mgmt_set_config_data_cmd
);
486 install_element(CONFIG_NODE
, &mgmt_delete_config_data_cmd
);
487 install_element(CONFIG_NODE
, &mgmt_load_config_cmd
);
488 install_element(CONFIG_NODE
, &mgmt_save_config_cmd
);
489 install_element(CONFIG_NODE
, &mgmt_rollback_cmd
);
491 install_element(VIEW_NODE
, &debug_mgmt_cmd
);
492 install_element(CONFIG_NODE
, &debug_mgmt_cmd
);
495 install_element(ENABLE_NODE
, &mgmt_performance_measurement_cmd
);
496 install_element(ENABLE_NODE
, &mgmt_reset_performance_stats_cmd
);
498 install_element(ENABLE_NODE
, &show_debugging_mgmt_cmd
);
501 * TODO: Register and handlers for auto-completion here.