2 * Copyright (c) 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 #include "command-line.h"
30 #include "dynamic-string.h"
34 #include "ovsdb-error.h"
41 #define THIS_MODULE VLM_ovsdb_client
43 /* --format: Output formatting. */
45 FMT_TABLE, /* Textual table. */
46 FMT_HTML, /* HTML table. */
47 FMT_CSV /* Comma-separated lines. */
50 /* --wide: For --format=table, the maximum output width. */
51 static int output_width;
53 /* --no-headings: Whether table output should include headings. */
54 static int output_headings = true;
56 static const struct command all_commands[];
58 static void usage(void) NO_RETURN;
59 static void parse_options(int argc, char *argv[]);
62 main(int argc, char *argv[])
64 set_program_name(argv[0]);
67 parse_options(argc, argv);
68 signal(SIGPIPE, SIG_IGN);
69 run_command(argc - optind, argv + optind, all_commands);
74 parse_options(int argc, char *argv[])
76 static struct option long_options[] = {
77 {"wide", no_argument, &output_width, INT_MAX},
78 {"format", required_argument, 0, 'f'},
79 {"no-headings", no_argument, &output_headings, 0},
80 {"verbose", optional_argument, 0, 'v'},
81 {"help", no_argument, 0, 'h'},
82 {"version", no_argument, 0, 'V'},
85 char *short_options = long_options_to_short_options(long_options);
87 output_width = isatty(fileno(stdout)) ? 79 : INT_MAX;
91 c = getopt_long(argc, argv, short_options, long_options, NULL);
98 if (!strcmp(optarg, "table")) {
99 output_format = FMT_TABLE;
100 } else if (!strcmp(optarg, "html")) {
101 output_format = FMT_HTML;
102 } else if (!strcmp(optarg, "csv")) {
103 output_format = FMT_CSV;
105 ovs_fatal(0, "unknown output format \"%s\"", optarg);
110 output_width = INT_MAX;
117 OVS_PRINT_VERSION(0, 0);
121 vlog_set_verbosity(optarg);
128 /* getopt_long() already set the value for us. */
141 printf("%s: Open vSwitch database JSON-RPC client\n"
142 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
143 "\nValid commands are:\n"
144 "\n get-schema SERVER\n"
145 " retrieve schema from SERVER\n"
146 "\n list-tables SERVER\n"
147 " list SERVER's tables\n"
148 "\n list-columns SERVER [TABLE]\n"
149 " list columns in TABLE (or all tables) on SERVER\n"
150 "\n transact SERVER TRANSACTION\n"
151 " run TRANSACTION (a JSON array of operations) on SERVER\n"
152 " and print the results as JSON on stdout\n",
153 program_name, program_name);
154 stream_usage("SERVER", true, true);
155 printf("\nOutput formatting options:\n"
156 " -f, --format=FORMAT set output formatting to FORMAT\n"
157 " (\"table\", \"html\", or \"csv\"\n"
158 " --wide don't limit TTY lines to 79 bytes\n"
159 " --no-headings omit table heading row\n");
161 printf("\nOther options:\n"
162 " -h, --help display this help message\n"
163 " -V, --version display version information\n");
168 parse_json(const char *s)
170 struct json *json = json_from_string(s);
171 if (json->type == JSON_STRING) {
172 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
177 static struct jsonrpc *
178 open_jsonrpc(const char *server)
180 struct stream *stream;
183 error = stream_open_block(server, &stream);
184 if (error == EAFNOSUPPORT) {
185 struct pstream *pstream;
187 error = pstream_open(server, &pstream);
189 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
192 VLOG_INFO("%s: waiting for connection...", server);
193 error = pstream_accept_block(pstream, &stream);
195 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
198 pstream_close(pstream);
200 ovs_fatal(error, "failed to connect to \"%s\"", server);
203 return jsonrpc_open(stream);
207 print_json(struct json *json)
209 char *string = json_to_string(json, JSSF_SORT);
210 fputs(string, stdout);
215 print_and_free_json(struct json *json)
222 check_ovsdb_error(struct ovsdb_error *error)
225 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
229 static struct ovsdb_schema *
230 fetch_schema(const char *server)
232 struct jsonrpc_msg *request, *reply;
233 struct ovsdb_schema *schema;
237 rpc = open_jsonrpc(server);
238 request = jsonrpc_create_request("get_schema", json_array_create_empty());
239 error = jsonrpc_transact_block(rpc, request, &reply);
241 ovs_fatal(error, "transaction failed");
243 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
244 jsonrpc_msg_destroy(reply);
257 struct column *columns;
258 size_t n_columns, allocated_columns;
259 size_t n_rows, allocated_rows;
260 size_t current_column;
264 table_init(struct table *table)
266 memset(table, 0, sizeof *table);
270 table_add_column(struct table *table, const char *heading, ...)
274 table_add_column(struct table *table, const char *heading, ...)
276 struct column *column;
279 assert(!table->n_rows);
280 if (table->n_columns >= table->allocated_columns) {
281 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
282 sizeof *table->columns);
284 column = &table->columns[table->n_columns++];
286 va_start(args, heading);
287 column->heading = xvasprintf(heading, args);
288 column->width = strlen(column->heading);
293 table_cell__(const struct table *table, size_t row, size_t column)
295 return &table->cells[column + row * table->n_columns];
299 table_add_row(struct table *table)
303 if (table->n_rows >= table->allocated_rows) {
304 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
305 table->n_columns * sizeof *table->cells);
309 table->current_column = 0;
310 for (x = 0; x < table->n_columns; x++) {
311 *table_cell__(table, y, x) = NULL;
316 table_add_cell_nocopy(struct table *table, char *s)
321 assert(table->n_rows > 0);
322 assert(table->current_column < table->n_columns);
324 x = table->current_column++;
325 y = table->n_rows - 1;
326 *table_cell__(table, y, x) = s;
329 if (length > table->columns[x].width) {
330 table->columns[x].width = length;
335 table_add_cell(struct table *table, const char *format, ...)
339 va_start(args, format);
340 table_add_cell_nocopy(table, xvasprintf(format, args));
345 table_print_table_line__(struct ds *line, size_t max_width)
347 ds_truncate(line, max_width);
353 table_print_table__(const struct table *table)
355 struct ds line = DS_EMPTY_INITIALIZER;
358 if (output_headings) {
359 for (x = 0; x < table->n_columns; x++) {
360 const struct column *column = &table->columns[x];
362 ds_put_char(&line, ' ');
364 ds_put_format(&line, "%-*s", column->width, column->heading);
366 table_print_table_line__(&line, output_width);
368 for (x = 0; x < table->n_columns; x++) {
369 const struct column *column = &table->columns[x];
373 ds_put_char(&line, ' ');
375 for (i = 0; i < column->width; i++) {
376 ds_put_char(&line, '-');
379 table_print_table_line__(&line, output_width);
382 for (y = 0; y < table->n_rows; y++) {
383 for (x = 0; x < table->n_columns; x++) {
384 const char *cell = *table_cell__(table, y, x);
386 ds_put_char(&line, ' ');
388 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
390 table_print_table_line__(&line, output_width);
397 table_print_html_cell__(const char *element, const char *content)
401 printf(" <%s>", element);
402 for (p = content; *p != '\0'; p++) {
405 fputs("&", stdout);
408 fputs("<", stdout);
411 fputs(">", stdout);
418 printf("</%s>\n", element);
422 table_print_html__(const struct table *table)
426 fputs("<table>\n", stdout);
428 if (output_headings) {
429 fputs(" <tr>\n", stdout);
430 for (x = 0; x < table->n_columns; x++) {
431 const struct column *column = &table->columns[x];
432 table_print_html_cell__("th", column->heading);
434 fputs(" </tr>\n", stdout);
437 for (y = 0; y < table->n_rows; y++) {
438 fputs(" <tr>\n", stdout);
439 for (x = 0; x < table->n_columns; x++) {
440 table_print_html_cell__("td", *table_cell__(table, y, x));
442 fputs(" </tr>\n", stdout);
445 fputs("</table>\n", stdout);
449 table_print_csv_cell__(const char *content)
453 if (!strpbrk(content, "\n\",")) {
454 fputs(content, stdout);
457 for (p = content; *p != '\0'; p++) {
460 fputs("\"\"", stdout);
472 table_print_csv__(const struct table *table)
476 if (output_headings) {
477 for (x = 0; x < table->n_columns; x++) {
478 const struct column *column = &table->columns[x];
482 table_print_csv_cell__(column->heading);
487 for (y = 0; y < table->n_rows; y++) {
488 for (x = 0; x < table->n_columns; x++) {
492 table_print_csv_cell__(*table_cell__(table, y, x));
499 table_print(const struct table *table)
501 switch (output_format) {
503 table_print_table__(table);
507 table_print_html__(table);
511 table_print_csv__(table);
517 do_get_schema(int argc UNUSED, char *argv[])
519 struct ovsdb_schema *schema = fetch_schema(argv[1]);
520 print_and_free_json(ovsdb_schema_to_json(schema));
521 ovsdb_schema_destroy(schema);
525 do_list_tables(int argc UNUSED, char *argv[])
527 struct ovsdb_schema *schema;
528 struct shash_node *node;
531 schema = fetch_schema(argv[1]);
533 table_add_column(&t, "Table");
534 table_add_column(&t, "Comment");
535 SHASH_FOR_EACH (node, &schema->tables) {
536 struct ovsdb_table_schema *ts = node->data;
539 table_add_cell(&t, ts->name);
541 table_add_cell(&t, ts->comment);
544 ovsdb_schema_destroy(schema);
549 do_list_columns(int argc UNUSED, char *argv[])
551 const char *table_name = argv[2];
552 struct ovsdb_schema *schema;
553 struct shash_node *table_node;
556 schema = fetch_schema(argv[1]);
559 table_add_column(&t, "Table");
561 table_add_column(&t, "Column");
562 table_add_column(&t, "Type");
563 table_add_column(&t, "Comment");
564 SHASH_FOR_EACH (table_node, &schema->tables) {
565 struct ovsdb_table_schema *ts = table_node->data;
567 if (!table_name || !strcmp(table_name, ts->name)) {
568 struct shash_node *column_node;
570 SHASH_FOR_EACH (column_node, &ts->columns) {
571 struct ovsdb_column *column = column_node->data;
572 struct json *type = ovsdb_type_to_json(&column->type);
576 table_add_cell(&t, ts->name);
578 table_add_cell(&t, column->name);
579 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
580 if (column->comment) {
581 table_add_cell(&t, column->comment);
588 ovsdb_schema_destroy(schema);
593 do_transact(int argc UNUSED, char *argv[] UNUSED)
595 struct jsonrpc_msg *request, *reply;
596 struct json *transaction;
600 transaction = parse_json(argv[2]);
602 rpc = open_jsonrpc(argv[1]);
603 request = jsonrpc_create_request("transact", transaction);
604 error = jsonrpc_transact_block(rpc, request, &reply);
606 ovs_fatal(error, "transaction failed");
609 ovs_fatal(error, "transaction returned error: %s",
610 json_to_string(reply->error, JSSF_SORT));
612 print_json(reply->result);
614 jsonrpc_msg_destroy(reply);
619 do_help(int argc UNUSED, char *argv[] UNUSED)
624 static const struct command all_commands[] = {
625 { "get-schema", 1, 1, do_get_schema },
626 { "list-tables", 1, 1, do_list_tables },
627 { "list-columns", 1, 2, do_list_columns },
628 { "transact", 2, 2, do_transact },
629 { "help", 0, INT_MAX, do_help },
630 { NULL, 0, 0, NULL },