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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* MGMTD VTY Interface
*
* Copyright (C) 2021 Vmware, Inc.
* Pushpasis Sarkar <spushpasis@vmware.com>
*/
#include <zebra.h>
#include "command.h"
#include "json.h"
#include "mgmtd/mgmt.h"
#include "mgmtd/mgmt_ds.h"
#ifndef VTYSH_EXTRACT_PL
#include "mgmtd/mgmt_vty_clippy.c"
#endif
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(show_mgmt_dump_data,
show_mgmt_dump_data_cmd,
"show mgmt datastore-contents WORD$dsname [xpath WORD$path] [file WORD$filepath] <json|xml>$fmt",
SHOW_STR
MGMTD_STR
"Get Datastore contents from a specific datastore\n"
"<candidate | running | operational>\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|xml\n")
{
enum mgmt_datastore_id datastore = MGMTD_DS_CANDIDATE;
struct mgmt_ds_ctx *ds_ctx;
LYD_FORMAT format = fmt[0] == 'j' ? LYD_JSON : LYD_XML;
FILE *f = NULL;
datastore = mgmt_ds_name2id(dsname);
if (datastore == MGMTD_DS_NONE) {
vty_out(vty,
"DS Name %s does not matches any existing datastore\n",
dsname);
return CMD_SUCCESS;
}
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(mgmt_load_config,
mgmt_load_config_cmd,
"mgmt load-config file WORD$filepath <merge|replace>",
MGMTD_STR
"Load configuration onto Candidate Datastore\n"
"Read the configuration from a file\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 = false;
int idx_merge = 4;
int ret;
struct mgmt_ds_ctx *ds_ctx;
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;
}
if (strncmp(argv[idx_merge]->arg, "merge", sizeof("merge")) == 0)
merge = true;
else if (strncmp(argv[idx_merge]->arg, "replace", sizeof("replace"))
== 0)
merge = false;
else {
vty_out(vty, "Chosen option: %s not valid\n",
argv[idx_merge]->arg);
return CMD_SUCCESS;
}
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 datastore WORD$dsname file WORD$filepath",
MGMTD_STR
"Save configuration from datastore\n"
"Datastore keyword\n"
"<candidate|running>\n"
"Write the configuration to a file\n"
"Full path of the file\n")
{
struct mgmt_ds_ctx *ds_ctx;
enum mgmt_datastore_id datastore;
FILE *f;
datastore = mgmt_ds_name2id(dsname);
if (datastore == MGMTD_DS_NONE) {
vty_out(vty,
"DS Name %s does not matches any existing datastore\n",
dsname);
return CMD_SUCCESS;
}
if (datastore != MGMTD_DS_CANDIDATE && datastore != MGMTD_DS_RUNNING) {
vty_out(vty, "DS Name %s is not a configuration datastore\n",
dsname);
return CMD_SUCCESS;
}
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;
}
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;
}
static struct cmd_node debug_node = {
.name = "debug",
.node = DEBUG_NODE,
.prompt = "",
.config_write = config_write_mgmt_debug,
};
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)
{
install_node(&debug_node);
install_element(VIEW_NODE, &show_mgmt_ds_cmd);
install_element(VIEW_NODE, &show_mgmt_dump_data_cmd);
install_element(CONFIG_NODE, &mgmt_load_config_cmd);
install_element(CONFIG_NODE, &mgmt_save_config_cmd);
install_element(VIEW_NODE, &debug_mgmt_cmd);
install_element(CONFIG_NODE, &debug_mgmt_cmd);
/*
* TODO: Register and handlers for auto-completion here (if any).
*/
}
|