ovsdb-tool: Make "show-log" convert raw JSON to easier-to-read syntax.
[cascardo/ovs.git] / ovsdb / ovsdb-tool.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "compiler.h"
28 #include "dynamic-string.h"
29 #include "file.h"
30 #include "lockfile.h"
31 #include "log.h"
32 #include "json.h"
33 #include "ovsdb.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-error.h"
36 #include "socket-util.h"
37 #include "table.h"
38 #include "timeval.h"
39 #include "util.h"
40 #include "vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(ovsdb_tool);
43
44 /* -m, --more: Verbosity level for "show-log" command output. */
45 static int show_log_verbosity;
46
47 static const struct command all_commands[];
48
49 static void usage(void) NO_RETURN;
50 static void parse_options(int argc, char *argv[]);
51
52 int
53 main(int argc, char *argv[])
54 {
55     set_program_name(argv[0]);
56     parse_options(argc, argv);
57     signal(SIGPIPE, SIG_IGN);
58     run_command(argc - optind, argv + optind, all_commands);
59     return 0;
60 }
61
62 static void
63 parse_options(int argc, char *argv[])
64 {
65     static struct option long_options[] = {
66         {"more", no_argument, NULL, 'm'},
67         {"verbose", optional_argument, NULL, 'v'},
68         {"help", no_argument, NULL, 'h'},
69         {"version", no_argument, NULL, 'V'},
70         {NULL, 0, NULL, 0},
71     };
72     char *short_options = long_options_to_short_options(long_options);
73
74     for (;;) {
75         int c;
76
77         c = getopt_long(argc, argv, short_options, long_options, NULL);
78         if (c == -1) {
79             break;
80         }
81
82         switch (c) {
83         case 'm':
84             show_log_verbosity++;
85             break;
86
87         case 'h':
88             usage();
89
90         case 'V':
91             ovs_print_version(0, 0);
92             exit(EXIT_SUCCESS);
93
94         case 'v':
95             vlog_set_verbosity(optarg);
96             break;
97
98         case '?':
99             exit(EXIT_FAILURE);
100
101         default:
102             abort();
103         }
104     }
105     free(short_options);
106 }
107
108 static void
109 usage(void)
110 {
111     printf("%s: Open vSwitch database management utility\n"
112            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
113            "  create DB SCHEMA   create DB with the given SCHEMA\n"
114            "  compact DB [DST]   compact DB in-place (or to DST)\n"
115            "  convert DB SCHEMA [DST]   convert DB to SCHEMA (to DST)\n"
116            "  db-version DB      report version of schema used by DB\n"
117            "  db-cksum DB        report checksum of schema used by DB\n"
118            "  schema-version SCHEMA  report SCHEMA's schema version\n"
119            "  schema-cksum SCHEMA  report SCHEMA's checksum\n"
120            "  query DB TRNS      execute read-only transaction on DB\n"
121            "  transact DB TRNS   execute read/write transaction on DB\n"
122            "  show-log DB        prints information about DB's log entries\n",
123            program_name, program_name);
124     vlog_usage();
125     printf("\nOther options:\n"
126            "  -m, --more                  increase show-log verbosity\n"
127            "  -h, --help                  display this help message\n"
128            "  -V, --version               display version information\n");
129     exit(EXIT_SUCCESS);
130 }
131 \f
132 static struct json *
133 parse_json(const char *s)
134 {
135     struct json *json = json_from_string(s);
136     if (json->type == JSON_STRING) {
137         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
138     }
139     return json;
140 }
141
142 static void
143 print_and_free_json(struct json *json)
144 {
145     char *string = json_to_string(json, JSSF_SORT);
146     json_destroy(json);
147     puts(string);
148     free(string);
149 }
150
151 static void
152 check_ovsdb_error(struct ovsdb_error *error)
153 {
154     if (error) {
155         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
156     }
157 }
158 \f
159 static void
160 do_create(int argc OVS_UNUSED, char *argv[])
161 {
162     const char *db_file_name = argv[1];
163     const char *schema_file_name = argv[2];
164     struct ovsdb_schema *schema;
165     struct ovsdb_log *log;
166     struct json *json;
167
168     /* Read schema from file and convert to JSON. */
169     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
170     json = ovsdb_schema_to_json(schema);
171     ovsdb_schema_destroy(schema);
172
173     /* Create database file. */
174     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
175                                      -1, &log));
176     check_ovsdb_error(ovsdb_log_write(log, json));
177     check_ovsdb_error(ovsdb_log_commit(log));
178     ovsdb_log_close(log);
179
180     json_destroy(json);
181 }
182
183 static void
184 compact_or_convert(const char *src_name, const char *dst_name,
185                    const struct ovsdb_schema *new_schema,
186                    const char *comment)
187 {
188     struct lockfile *src_lock;
189     struct lockfile *dst_lock;
190     bool in_place = dst_name == NULL;
191     struct ovsdb *db;
192     int retval;
193
194     /* Lock the source, if we will be replacing it. */
195     if (in_place) {
196         retval = lockfile_lock(src_name, 0, &src_lock);
197         if (retval) {
198             ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
199         }
200     }
201
202     /* Get (temporary) destination and lock it. */
203     if (in_place) {
204         dst_name = xasprintf("%s.tmp", src_name);
205     }
206     retval = lockfile_lock(dst_name, 0, &dst_lock);
207     if (retval) {
208         ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
209     }
210
211     /* Save a copy. */
212     check_ovsdb_error(new_schema
213                       ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
214                       : ovsdb_file_open(src_name, true, &db, NULL));
215     check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
216     ovsdb_destroy(db);
217
218     /* Replace source. */
219     if (in_place) {
220         if (rename(dst_name, src_name)) {
221             ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
222                       dst_name, src_name);
223         }
224         fsync_parent_dir(dst_name);
225         lockfile_unlock(src_lock);
226     }
227
228     lockfile_unlock(dst_lock);
229 }
230
231 static void
232 do_compact(int argc OVS_UNUSED, char *argv[])
233 {
234     compact_or_convert(argv[1], argv[2], NULL,
235                        "compacted by ovsdb-tool "VERSION BUILDNR);
236 }
237
238 static void
239 do_convert(int argc OVS_UNUSED, char *argv[])
240 {
241     const char *schema_file_name = argv[2];
242     struct ovsdb_schema *new_schema;
243
244     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
245     compact_or_convert(argv[1], argv[3], new_schema,
246                        "converted by ovsdb-tool "VERSION BUILDNR);
247     ovsdb_schema_destroy(new_schema);
248 }
249
250 static void
251 do_needs_conversion(int argc OVS_UNUSED, char *argv[])
252 {
253     const char *db_file_name = argv[1];
254     const char *schema_file_name = argv[2];
255     struct ovsdb_schema *schema1, *schema2;
256
257     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
258     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
259     puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
260     ovsdb_schema_destroy(schema1);
261     ovsdb_schema_destroy(schema2);
262 }
263
264 static void
265 do_db_version(int argc OVS_UNUSED, char *argv[])
266 {
267     const char *db_file_name = argv[1];
268     struct ovsdb_schema *schema;
269
270     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
271     puts(schema->version);
272     ovsdb_schema_destroy(schema);
273 }
274
275 static void
276 do_db_cksum(int argc OVS_UNUSED, char *argv[])
277 {
278     const char *db_file_name = argv[1];
279     struct ovsdb_schema *schema;
280
281     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
282     puts(schema->cksum);
283     ovsdb_schema_destroy(schema);
284 }
285
286 static void
287 do_schema_version(int argc OVS_UNUSED, char *argv[])
288 {
289     const char *schema_file_name = argv[1];
290     struct ovsdb_schema *schema;
291
292     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
293     puts(schema->version);
294     ovsdb_schema_destroy(schema);
295 }
296
297 static void
298 do_schema_cksum(int argc OVS_UNUSED, char *argv[])
299 {
300     const char *schema_file_name = argv[1];
301     struct ovsdb_schema *schema;
302
303     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
304     puts(schema->cksum);
305     ovsdb_schema_destroy(schema);
306 }
307
308 static void
309 transact(bool read_only, const char *db_file_name, const char *transaction)
310 {
311     struct json *request, *result;
312     struct ovsdb *db;
313
314     check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
315
316     request = parse_json(transaction);
317     result = ovsdb_execute(db, NULL, request, 0, NULL);
318     json_destroy(request);
319
320     print_and_free_json(result);
321     ovsdb_destroy(db);
322 }
323
324 static void
325 do_query(int argc OVS_UNUSED, char *argv[])
326 {
327     transact(true, argv[1], argv[2]);
328 }
329
330 static void
331 do_transact(int argc OVS_UNUSED, char *argv[])
332 {
333     transact(false, argv[1], argv[2]);
334 }
335
336 static void
337 print_db_changes(struct shash *tables, struct shash *names,
338                  const struct ovsdb_schema *schema)
339 {
340     struct shash_node *n1;
341
342     SHASH_FOR_EACH (n1, tables) {
343         const char *table = n1->name;
344         struct ovsdb_table_schema *table_schema;
345         struct json *rows = n1->data;
346         struct shash_node *n2;
347
348         if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
349             continue;
350         }
351
352         table_schema = shash_find_data(&schema->tables, table);
353         SHASH_FOR_EACH (n2, json_object(rows)) {
354             const char *row_uuid = n2->name;
355             struct json *columns = n2->data;
356             struct shash_node *n3;
357             char *old_name, *new_name;
358             bool free_new_name = false;
359
360             old_name = new_name = shash_find_data(names, row_uuid);
361             if (columns->type == JSON_OBJECT) {
362                 struct json *new_name_json;
363
364                 new_name_json = shash_find_data(json_object(columns), "name");
365                 if (new_name_json) {
366                     new_name = json_to_string(new_name_json, JSSF_SORT);
367                     free_new_name = true;
368                 }
369             }
370
371             printf("\ttable %s", table);
372
373             if (!old_name) {
374                 if (new_name) {
375                     printf(" insert row %s (%.8s):\n", new_name, row_uuid);
376                 } else {
377                     printf(" insert row %.8s:\n", row_uuid);
378                 }
379             } else {
380                 printf(" row %s (%.8s):\n", old_name, row_uuid);
381             }
382
383             if (columns->type == JSON_OBJECT) {
384                 if (show_log_verbosity > 1) {
385                     SHASH_FOR_EACH (n3, json_object(columns)) {
386                         const char *column = n3->name;
387                         const struct ovsdb_column *column_schema;
388                         struct json *value = n3->data;
389                         char *value_string = NULL;
390
391                         column_schema =
392                             (table_schema
393                              ? shash_find_data(&table_schema->columns, column)
394                              : NULL);
395                         if (column_schema) {
396                             const struct ovsdb_error *error;
397                             const struct ovsdb_type *type;
398                             struct ovsdb_datum datum;
399
400                             type = &column_schema->type;
401                             error = ovsdb_datum_from_json(&datum, type,
402                                                           value, NULL);
403                             if (!error) {
404                                 struct ds s;
405
406                                 ds_init(&s);
407                                 ovsdb_datum_to_string(&datum, type, &s);
408                                 value_string = ds_steal_cstr(&s);
409                             }
410                         }
411                         if (!value_string) {
412                             value_string = json_to_string(value, JSSF_SORT);
413                         }
414                         printf("\t\t%s=%s\n", column, value_string);
415                         free(value_string);
416                     }
417                 }
418                 if (!old_name
419                     || (new_name != old_name && strcmp(old_name, new_name))) {
420                     if (old_name) {
421                         shash_delete(names, shash_find(names, row_uuid));
422                         free(old_name);
423                     }
424                     shash_add(names, row_uuid, (new_name
425                                                 ? xstrdup(new_name)
426                                                 : xmemdup0(row_uuid, 8)));
427                 }
428             } else if (columns->type == JSON_NULL) {
429                 struct shash_node *node;
430
431                 printf("\t\tdelete row\n");
432                 node = shash_find(names, row_uuid);
433                 if (node) {
434                     shash_delete(names, node);
435                 }
436                 free(old_name);
437             }
438
439             if (free_new_name) {
440                 free(new_name);
441             }
442         }
443     }
444 }
445
446 static void
447 do_show_log(int argc OVS_UNUSED, char *argv[])
448 {
449     const char *db_file_name = argv[1];
450     struct shash names;
451     struct ovsdb_log *log;
452     struct ovsdb_schema *schema;
453     unsigned int i;
454
455     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
456                                      -1, &log));
457     shash_init(&names);
458     schema = NULL;
459     for (i = 0; ; i++) {
460         struct json *json;
461
462         check_ovsdb_error(ovsdb_log_read(log, &json));
463         if (!json) {
464             break;
465         }
466
467         printf("record %u:", i);
468         if (i == 0) {
469             check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
470             printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
471                    schema->name, schema->version, schema->cksum);
472         } else if (json->type == JSON_OBJECT) {
473             struct json *date, *comment;
474
475             date = shash_find_data(json_object(json), "_date");
476             if (date && date->type == JSON_INTEGER) {
477                 time_t t = json_integer(date);
478                 char s[128];
479
480                 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
481                 printf(" %s", s);
482             }
483
484             comment = shash_find_data(json_object(json), "_comment");
485             if (comment && comment->type == JSON_STRING) {
486                 printf(" \"%s\"", json_string(comment));
487             }
488
489             if (i > 0 && show_log_verbosity > 0) {
490                 putchar('\n');
491                 print_db_changes(json_object(json), &names, schema);
492             }
493         }
494         json_destroy(json);
495         putchar('\n');
496     }
497
498     ovsdb_log_close(log);
499     ovsdb_schema_destroy(schema);
500     /* XXX free 'names'. */
501 }
502
503 static void
504 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
505 {
506     usage();
507 }
508
509 static const struct command all_commands[] = {
510     { "create", 2, 2, do_create },
511     { "compact", 1, 2, do_compact },
512     { "convert", 2, 3, do_convert },
513     { "needs-conversion", 2, 2, do_needs_conversion },
514     { "db-version", 1, 1, do_db_version },
515     { "db-cksum", 1, 1, do_db_cksum },
516     { "schema-version", 1, 1, do_schema_version },
517     { "schema-cksum", 1, 1, do_schema_cksum },
518     { "query", 2, 2, do_query },
519     { "transact", 2, 2, do_transact },
520     { "show-log", 1, 1, do_show_log },
521     { "help", 0, INT_MAX, do_help },
522     { NULL, 0, 0, NULL },
523 };