summaryrefslogtreecommitdiffstats
path: root/mgmtd/mgmt_vty.c.safe
blob: c43485c92b00bc0d724f52c75c874cc40162315a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * MGMTD VTY Interface
 * Copyright (C) 2021  Vmware, Inc.
 *		       Pushpasis Sarkar <spushpasis@vmware.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include "command.h"
#include "json.h"
#include "mgmtd/mgmt.h"
#include "mgmtd/mgmt_be_server.h"
#include "mgmtd/mgmt_be_adapter.h"
#include "mgmtd/mgmt_fe_server.h"
#include "mgmtd/mgmt_fe_adapter.h"
#include "mgmtd/mgmt_ds.h"
#include "mgmtd/mgmt_history.h"

#include "mgmtd/mgmt_vty_clippy.c"

DEFPY(show_mgmt_be_adapter,
      show_mgmt_be_adapter_cmd,
      "show mgmt backend-adapter all",
      SHOW_STR
      MGMTD_STR
      MGMTD_BE_ADAPTER_STR
      "Display all Backend Adapters\n")
{
	mgmt_be_adapter_status_write(vty);

	return CMD_SUCCESS;
}

DEFPY(show_mgmt_be_xpath_reg,
      show_mgmt_be_xpath_reg_cmd,
      "show mgmt backend-yang-xpath-registry",
      SHOW_STR
      MGMTD_STR
      "Backend Adapter YANG Xpath Registry\n")
{
	mgmt_be_xpath_register_write(vty);

	return CMD_SUCCESS;
}

DEFPY(show_mgmt_fe_adapter,
      show_mgmt_fe_adapter_cmd,
      "show mgmt frontend-adapter all",
      SHOW_STR MGMTD_STR MGMTD_FE_ADAPTER_STR "Display all Frontend Adapters\n")
{
	mgmt_fe_adapter_status_write(vty, false);

	return CMD_SUCCESS;
}

DEFPY(show_mgmt_fe_adapter_detail, show_mgmt_fe_adapter_detail_cmd,
      "show mgmt frontend-adapter all detail",
      SHOW_STR MGMTD_STR MGMTD_FE_ADAPTER_STR
      "Display all Frontend Adapters\n"
      "Details of commit stats\n")
{
	mgmt_fe_adapter_status_write(vty, true);

	return CMD_SUCCESS;
}

DEFPY_HIDDEN(mgmt_performance_measurement,
	     mgmt_performance_measurement_cmd,
	     "[no] mgmt performance-measurement",
	     NO_STR
	     MGMTD_STR
	     "Enable performance measurement\n")
{
	if (no)
		mgmt_fe_adapter_perf_measurement(vty, false);
	else
		mgmt_fe_adapter_perf_measurement(vty, true);

	return CMD_SUCCESS;
}

DEFPY(mgmt_reset_performance_stats,
      mgmt_reset_performance_stats_cmd,
      "mgmt reset-statistics",
      MGMTD_STR
      "Reset the Performance measurement statistics\n")
{
	mgmt_fe_adapter_reset_perf_stats(vty);

	return CMD_SUCCESS;
}

DEFPY(show_mgmt_txn,
      show_mgmt_txn_cmd,
      "show mgmt transaction all",
      SHOW_STR
      MGMTD_STR
      MGMTD_TXN_STR
      "Display all Transactions\n")
{
	mgmt_txn_status_write(vty);

	return CMD_SUCCESS;
}

DEFPY(show_mgmt_ds,
      show_mgmt_ds_cmd,
      "show mgmt datastore [all|candidate|operational|running]$dsname",
      SHOW_STR
      MGMTD_STR
      MGMTD_DS_STR
      "All datastores (default)\n"
      "Candidate datastore\n"
      "Operational datastore\n"
      "Running datastore\n")
{
	struct mgmt_ds_ctx *ds_ctx;

	if (!dsname || dsname[0] == 'a') {
		mgmt_ds_status_write(vty);
		return CMD_SUCCESS;
	}
	ds_ctx = mgmt_ds_get_ctx_by_id(mm, mgmt_ds_name2id(dsname));
	if (!ds_ctx) {
		vty_out(vty, "ERROR: Could not access %s datastore!\n", dsname);
		return CMD_ERR_NO_MATCH;
	}
	mgmt_ds_status_write_one(vty, ds_ctx);

	return CMD_SUCCESS;
}

DEFPY(mgmt_commit,
      mgmt_commit_cmd,
      "mgmt commit <check|apply|abort>$type",
      MGMTD_STR
      "Commit action\n"
      "Validate the set of config commands\n"
      "Validate and apply the set of config commands\n"
      "Abort and drop the set of config commands recently added\n")
{
	bool validate_only = type[0] == 'c';
	bool abort = type[1] == 'b';

	if (vty_mgmt_send_commit_config(vty, validate_only, abort) != 0)
		return CMD_WARNING_CONFIG_FAILED;
	return CMD_SUCCESS;
}

DEFPY(mgmt_set_config_data, mgmt_set_config_data_cmd,
      "mgmt set-config WORD$path VALUE",
      MGMTD_STR
      "Set configuration data\n"
      "XPath expression specifying the YANG data path\n"
      "Value of the data to set\n")
{
	strlcpy(vty->cfg_changes[0].xpath, path,
		sizeof(vty->cfg_changes[0].xpath));
	vty->cfg_changes[0].value = value;
	vty->cfg_changes[0].operation = NB_OP_CREATE;
	vty->num_cfg_changes = 1;

	vty->no_implicit_commit = true;
	vty_mgmt_send_config_data(vty);
	vty->no_implicit_commit = false;
	return CMD_SUCCESS;
}

DEFPY(mgmt_delete_config_data, mgmt_delete_config_data_cmd,
      "mgmt delete-config WORD$path",
      MGMTD_STR
      "Delete configuration data\n"
      "XPath expression specifying the YANG data path\n")
{

	strlcpy(vty->cfg_changes[0].xpath, path,
		sizeof(vty->cfg_changes[0].xpath));
	vty->cfg_changes[0].value = NULL;
	vty->cfg_changes[0].operation = NB_OP_DESTROY;
	vty->num_cfg_changes = 1;

	vty->no_implicit_commit = true;
	vty_mgmt_send_config_data(vty);
	vty->no_implicit_commit = false;
	return CMD_SUCCESS;
}

DEFPY(show_mgmt_get_config, show_mgmt_get_config_cmd,
      "show mgmt get-config [candidate|operational|running]$dsname WORD$path",
      SHOW_STR MGMTD_STR
      "Get configuration data from a specific configuration datastore\n"
      "Candidate datastore (default)\n"
      "Operational datastore\n"
      "Running datastore\n"
      "XPath expression specifying the YANG data path\n")
{
	const char *xpath_list[VTY_MAXCFGCHANGES] = {0};
	Mgmtd__DatastoreId datastore = MGMTD_DS_CANDIDATE;

	if (dsname)
		datastore = mgmt_ds_name2id(dsname);

	xpath_list[0] = path;
	vty_mgmt_send_get_config(vty, datastore, xpath_list, 1);
	return CMD_SUCCESS;
}

DEFPY(show_mgmt_get_data, show_mgmt_get_data_cmd,
      "show mgmt get-data [candidate|operational|running]$dsname WORD$path",
      SHOW_STR MGMTD_STR
      "Get data from a specific datastore\n"
      "Candidate datastore\n"
      "Operational datastore (default)\n"
      "Running datastore\n"
      "XPath expression specifying the YANG data path\n")
{
	const char *xpath_list[VTY_MAXCFGCHANGES] = {0};
	Mgmtd__DatastoreId datastore = MGMTD_DS_OPERATIONAL;

	if (dsname)
		datastore = mgmt_ds_name2id(dsname);

	xpath_list[0] = path;
	vty_mgmt_send_get_data(vty, datastore, xpath_list, 1);
	return CMD_SUCCESS;
}

DEFPY(show_mgmt_dump_data,
      show_mgmt_dump_data_cmd,
      "show mgmt datastore-contents [candidate|operational|running]$dsname [xpath WORD$path] [file WORD$filepath] <json|xml>$fmt",
      SHOW_STR
      MGMTD_STR
      "Get Datastore contents from a specific datastore\n"
      "Candidate datastore (default)\n"
      "Operational datastore\n"
      "Running datastore\n"
      "XPath expression specifying the YANG data path\n"
      "XPath string\n"
      "Dump the contents to a file\n"
      "Full path of the file\n"
      "json output\n"
      "xml output\n")
{
	struct mgmt_ds_ctx *ds_ctx;
	Mgmtd__DatastoreId datastore = MGMTD_DS_CANDIDATE;
	LYD_FORMAT format = fmt[0] == 'j' ? LYD_JSON : LYD_XML;
	FILE *f = NULL;

	if (datastore)
		datastore = mgmt_ds_name2id(dsname);

	ds_ctx = mgmt_ds_get_ctx_by_id(mm, datastore);
	if (!ds_ctx) {
		vty_out(vty, "ERROR: Could not access datastore!\n");
		return CMD_ERR_NO_MATCH;
	}

	if (filepath) {
		f = fopen(filepath, "w");
		if (!f) {
			vty_out(vty,
				"Could not open file pointed by filepath %s\n",
				filepath);
			return CMD_SUCCESS;
		}
	}

	mgmt_ds_dump_tree(vty, ds_ctx, path, f, format);

	if (f)
		fclose(f);
	return CMD_SUCCESS;
}

DEFPY(show_mgmt_map_xpath,
      show_mgmt_map_xpath_cmd,
      "show mgmt yang-xpath-subscription WORD$path",
      SHOW_STR
      MGMTD_STR
      "Get YANG Backend Subscription\n"
      "XPath expression specifying the YANG data path\n")
{
	mgmt_be_xpath_subscr_info_write(vty, path);
	return CMD_SUCCESS;
}

DEFPY(mgmt_load_config,
      mgmt_load_config_cmd,
      "mgmt load-config WORD$filepath <merge|replace>$type",
      MGMTD_STR
      "Load configuration onto Candidate Datastore\n"
      "Full path of the file\n"
      "Merge configuration with contents of Candidate Datastore\n"
      "Replace the existing contents of Candidate datastore\n")
{
	bool merge = type[0] == 'm' ? true : false;
	struct mgmt_ds_ctx *ds_ctx;
	int ret;

	if (access(filepath, F_OK) == -1) {
		vty_out(vty, "ERROR: File %s : %s\n", filepath,
			strerror(errno));
		return CMD_ERR_NO_FILE;
	}

	ds_ctx = mgmt_ds_get_ctx_by_id(mm, MGMTD_DS_CANDIDATE);
	if (!ds_ctx) {
		vty_out(vty, "ERROR: Could not access Candidate datastore!\n");
		return CMD_ERR_NO_MATCH;
	}

	ret = mgmt_ds_load_config_from_file(ds_ctx, filepath, merge);
	if (ret != 0)
		vty_out(vty, "Error with parsing the file with error code %d\n",
			ret);
	return CMD_SUCCESS;
}

DEFPY(mgmt_save_config,
      mgmt_save_config_cmd,
      "mgmt save-config <candidate|running>$dsname WORD$filepath",
      MGMTD_STR
      "Save configuration from datastore\n"
      "Candidate datastore\n"
      "Running datastore\n"
      "Full path of the file\n")
{
	Mgmtd__DatastoreId datastore = mgmt_ds_name2id(dsname);
	struct mgmt_ds_ctx *ds_ctx;
	FILE *f;

	ds_ctx = mgmt_ds_get_ctx_by_id(mm, datastore);
	if (!ds_ctx) {
		vty_out(vty, "ERROR: Could not access the '%s' datastore!\n",
			dsname);
		return CMD_ERR_NO_MATCH;
	}

	if (!filepath) {
		vty_out(vty, "ERROR: No file path mentioned!\n");
		return CMD_ERR_NO_MATCH;
	}

	f = fopen(filepath, "w");
	if (!f) {
		vty_out(vty, "Could not open file pointed by filepath %s\n",
			filepath);
		return CMD_SUCCESS;
	}

	mgmt_ds_dump_tree(vty, ds_ctx, "/", f, LYD_JSON);

	fclose(f);

	return CMD_SUCCESS;
}

DEFPY(show_mgmt_cmt_hist,
      show_mgmt_cmt_hist_cmd,
      "show mgmt commit-history",
      SHOW_STR
      MGMTD_STR
      "Show commit history\n")
{
	show_mgmt_cmt_history(vty);
	return CMD_SUCCESS;
}

DEFPY(mgmt_rollback,
      mgmt_rollback_cmd,
      "mgmt rollback <commit-id WORD$commit | last [(1-10)]$last>",
      MGMTD_STR
      "Rollback commits\n"
      "Rollback to commit ID\n"
      "Commit-ID\n"
      "Rollbak n commits\n"
      "Number of commits\n")
{
	if (commit)
		mgmt_history_rollback_by_id(vty, commit);
	else
		mgmt_history_rollback_n(vty, last);

	return CMD_SUCCESS;
}

static int config_write_mgmt_debug(struct vty *vty);
static struct cmd_node debug_node = {
	.name = "debug",
	.node = DEBUG_NODE,
	.prompt = "",
	.config_write = config_write_mgmt_debug,
};

static int config_write_mgmt_debug(struct vty *vty)
{
	int n = mgmt_debug_be + mgmt_debug_fe + mgmt_debug_ds + mgmt_debug_txn;
	if (!n)
		return 0;
	if (n == 4) {
		vty_out(vty, "debug mgmt all\n");
		return 0;
	}

	vty_out(vty, "debug mgmt");
	if (mgmt_debug_be)
		vty_out(vty, " backend");
	if (mgmt_debug_ds)
		vty_out(vty, " datastore");
	if (mgmt_debug_fe)
		vty_out(vty, " frontend");
	if (mgmt_debug_txn)
		vty_out(vty, " transaction");

	vty_out(vty, "\n");

	return 0;
}

DEFPY(debug_mgmt,
      debug_mgmt_cmd,
      "[no$no] debug mgmt <all$all|{backend$be|datastore$ds|frontend$fe|transaction$txn}>",
      NO_STR
      DEBUG_STR
      MGMTD_STR
      "All debug\n"
      "Back-end debug\n"
      "Datastore debug\n"
      "Front-end debug\n"
      "Transaction debug\n")
{
	bool set = !no;
	if (all)
		be = fe = ds = txn = set ? all : NULL;

	if (be)
		mgmt_debug_be = set;
	if (ds)
		mgmt_debug_ds = set;
	if (fe)
		mgmt_debug_fe = set;
	if (txn)
		mgmt_debug_txn = set;

	return CMD_SUCCESS;
}

void mgmt_vty_init(void)
{
	/*
	 * Initialize command handling from VTYSH connection.
	 * Call command initialization routines defined by
	 * backend components that are moved to new MGMTD infra
	 * here one by one.
	 */
#if HAVE_STATICD
	extern void static_vty_init(void);
	static_vty_init();
#endif

	install_node(&debug_node);

	install_element(VIEW_NODE, &show_mgmt_be_adapter_cmd);
	install_element(VIEW_NODE, &show_mgmt_be_xpath_reg_cmd);
	install_element(VIEW_NODE, &show_mgmt_fe_adapter_cmd);
	install_element(VIEW_NODE, &show_mgmt_fe_adapter_detail_cmd);
	install_element(VIEW_NODE, &show_mgmt_txn_cmd);
	install_element(VIEW_NODE, &show_mgmt_ds_cmd);
	install_element(VIEW_NODE, &show_mgmt_get_config_cmd);
	install_element(VIEW_NODE, &show_mgmt_get_data_cmd);
	install_element(VIEW_NODE, &show_mgmt_dump_data_cmd);
	install_element(VIEW_NODE, &show_mgmt_map_xpath_cmd);
	install_element(VIEW_NODE, &show_mgmt_cmt_hist_cmd);

	install_element(CONFIG_NODE, &mgmt_commit_cmd);
	install_element(CONFIG_NODE, &mgmt_set_config_data_cmd);
	install_element(CONFIG_NODE, &mgmt_delete_config_data_cmd);
	install_element(CONFIG_NODE, &mgmt_load_config_cmd);
	install_element(CONFIG_NODE, &mgmt_save_config_cmd);
	install_element(CONFIG_NODE, &mgmt_rollback_cmd);

	install_element(VIEW_NODE, &debug_mgmt_cmd);
	install_element(CONFIG_NODE, &debug_mgmt_cmd);

	/* Enable view */
	install_element(ENABLE_NODE, &mgmt_performance_measurement_cmd);
	install_element(ENABLE_NODE, &mgmt_reset_performance_stats_cmd);

	/*
	 * TODO: Register and handlers for auto-completion here.
	 */
}