ovsdb-tool: Remove reference to non-implemented "extract-schema" command
[sliver-openvswitch.git] / ovsdb / ovsdb-tool.c
1 /*
2  * Copyright (c) 2009, 2010 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 "command-line.h"
26 #include "compiler.h"
27 #include "file.h"
28 #include "lockfile.h"
29 #include "log.h"
30 #include "json.h"
31 #include "ovsdb.h"
32 #include "ovsdb-error.h"
33 #include "socket-util.h"
34 #include "table.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(ovsdb_tool)
40
41 /* -m, --more: Verbosity level for "show-log" command output. */
42 static int show_log_verbosity;
43
44 static const struct command all_commands[];
45
46 static void usage(void) NO_RETURN;
47 static void parse_options(int argc, char *argv[]);
48
49 int
50 main(int argc, char *argv[])
51 {
52     set_program_name(argv[0]);
53     parse_options(argc, argv);
54     signal(SIGPIPE, SIG_IGN);
55     run_command(argc - optind, argv + optind, all_commands);
56     return 0;
57 }
58
59 static void
60 parse_options(int argc, char *argv[])
61 {
62     static struct option long_options[] = {
63         {"more", no_argument, 0, 'm'},
64         {"verbose", optional_argument, 0, 'v'},
65         {"help", no_argument, 0, 'h'},
66         {"version", no_argument, 0, 'V'},
67         {0, 0, 0, 0},
68     };
69     char *short_options = long_options_to_short_options(long_options);
70
71     for (;;) {
72         int c;
73
74         c = getopt_long(argc, argv, short_options, long_options, NULL);
75         if (c == -1) {
76             break;
77         }
78
79         switch (c) {
80         case 'm':
81             show_log_verbosity++;
82             break;
83
84         case 'h':
85             usage();
86
87         case 'V':
88             OVS_PRINT_VERSION(0, 0);
89             exit(EXIT_SUCCESS);
90
91         case 'v':
92             vlog_set_verbosity(optarg);
93             break;
94
95         case '?':
96             exit(EXIT_FAILURE);
97
98         default:
99             abort();
100         }
101     }
102     free(short_options);
103 }
104
105 static void
106 usage(void)
107 {
108     printf("%s: Open vSwitch database management utility\n"
109            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
110            "  create DB SCHEMA   create DB with the given SCHEMA\n"
111            "  compact DB [DST]   compact DB in-place (or to DST)\n"
112            "  convert DB SCHEMA [DST]   convert DB to SCHEMA (to DST)\n"
113            "  query DB TRNS      execute read-only transaction on DB\n"
114            "  transact DB TRNS   execute read/write transaction on DB\n"
115            "  show-log DB        prints information about DB's log entries\n",
116            program_name, program_name);
117     vlog_usage();
118     printf("\nOther options:\n"
119            "  -m, --more                  increase show-log verbosity\n"
120            "  -h, --help                  display this help message\n"
121            "  -V, --version               display version information\n");
122     exit(EXIT_SUCCESS);
123 }
124 \f
125 static struct json *
126 parse_json(const char *s)
127 {
128     struct json *json = json_from_string(s);
129     if (json->type == JSON_STRING) {
130         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
131     }
132     return json;
133 }
134
135 static void
136 print_and_free_json(struct json *json)
137 {
138     char *string = json_to_string(json, JSSF_SORT);
139     json_destroy(json);
140     puts(string);
141     free(string);
142 }
143
144 static void
145 check_ovsdb_error(struct ovsdb_error *error)
146 {
147     if (error) {
148         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
149     }
150 }
151 \f
152 static void
153 do_create(int argc OVS_UNUSED, char *argv[])
154 {
155     const char *db_file_name = argv[1];
156     const char *schema_file_name = argv[2];
157     struct ovsdb_schema *schema;
158     struct ovsdb_log *log;
159     struct json *json;
160
161     /* Read schema from file and convert to JSON. */
162     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
163     json = ovsdb_schema_to_json(schema);
164     ovsdb_schema_destroy(schema);
165
166     /* Create database file. */
167     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
168                                      -1, &log));
169     check_ovsdb_error(ovsdb_log_write(log, json));
170     check_ovsdb_error(ovsdb_log_commit(log));
171     ovsdb_log_close(log);
172
173     json_destroy(json);
174 }
175
176 static void
177 compact_or_convert(const char *src_name, const char *dst_name,
178                    const struct ovsdb_schema *new_schema,
179                    const char *comment)
180 {
181     struct lockfile *src_lock;
182     struct lockfile *dst_lock;
183     bool in_place = dst_name == NULL;
184     struct ovsdb *db;
185     int retval;
186
187     /* Lock the source, if we will be replacing it. */
188     if (in_place) {
189         retval = lockfile_lock(src_name, 0, &src_lock);
190         if (retval) {
191             ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
192         }
193     }
194
195     /* Get (temporary) destination and lock it. */
196     if (in_place) {
197         dst_name = xasprintf("%s.tmp", src_name);
198     }
199     retval = lockfile_lock(dst_name, 0, &dst_lock);
200     if (retval) {
201         ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
202     }
203
204     /* Save a copy. */
205     check_ovsdb_error(new_schema
206                       ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
207                       : ovsdb_file_open(src_name, true, &db, NULL));
208     check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
209     ovsdb_destroy(db);
210
211     /* Replace source. */
212     if (in_place) {
213         if (rename(dst_name, src_name)) {
214             ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
215                       dst_name, src_name);
216         }
217         fsync_parent_dir(dst_name);
218         lockfile_unlock(src_lock);
219     }
220
221     lockfile_unlock(dst_lock);
222 }
223
224 static void
225 do_compact(int argc OVS_UNUSED, char *argv[])
226 {
227     compact_or_convert(argv[1], argv[2], NULL, "compacted by ovsdb-tool");
228 }
229
230 static void
231 do_convert(int argc OVS_UNUSED, char *argv[])
232 {
233     const char *schema_file_name = argv[2];
234     struct ovsdb_schema *new_schema;
235
236     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
237     compact_or_convert(argv[1], argv[3], new_schema,
238                        "converted by ovsdb-tool");
239     ovsdb_schema_destroy(new_schema);
240 }
241
242 static void
243 transact(bool read_only, const char *db_file_name, const char *transaction)
244 {
245     struct json *request, *result;
246     struct ovsdb *db;
247
248     check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
249
250     request = parse_json(transaction);
251     result = ovsdb_execute(db, request, 0, NULL);
252     json_destroy(request);
253
254     print_and_free_json(result);
255     ovsdb_destroy(db);
256 }
257
258 static void
259 do_query(int argc OVS_UNUSED, char *argv[])
260 {
261     transact(true, argv[1], argv[2]);
262 }
263
264 static void
265 do_transact(int argc OVS_UNUSED, char *argv[])
266 {
267     transact(false, argv[1], argv[2]);
268 }
269
270 static void
271 print_db_changes(struct shash *tables, struct shash *names)
272 {
273     struct shash_node *n1;
274
275     SHASH_FOR_EACH (n1, tables) {
276         const char *table = n1->name;
277         struct json *rows = n1->data;
278         struct shash_node *n2;
279
280         if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
281             continue;
282         }
283
284         SHASH_FOR_EACH (n2, json_object(rows)) {
285             const char *row_uuid = n2->name;
286             struct json *columns = n2->data;
287             struct shash_node *n3;
288             char *old_name, *new_name;
289             bool free_new_name = false;
290
291             old_name = new_name = shash_find_data(names, row_uuid);
292             if (columns->type == JSON_OBJECT) {
293                 struct json *new_name_json;
294
295                 new_name_json = shash_find_data(json_object(columns), "name");
296                 if (new_name_json) {
297                     new_name = json_to_string(new_name_json, JSSF_SORT);
298                     free_new_name = true;
299                 }
300             }
301
302             printf("\ttable %s", table);
303
304             if (!old_name) {
305                 if (new_name) {
306                     printf(" insert row %s:\n", new_name);
307                 } else {
308                     printf(" insert row %.8s:\n", row_uuid);
309                 }
310             } else {
311                 printf(" row %s:\n", old_name);
312             }
313
314             if (columns->type == JSON_OBJECT) {
315                 if (show_log_verbosity > 1) {
316                     SHASH_FOR_EACH (n3, json_object(columns)) {
317                         const char *column = n3->name;
318                         struct json *value = n3->data;
319                         char *value_string;
320
321                         value_string = json_to_string(value, JSSF_SORT);
322                         printf("\t\t%s=%s\n", column, value_string);
323                         free(value_string);
324                     }
325                 }
326                 if (!old_name
327                     || (new_name != old_name && strcmp(old_name, new_name))) {
328                     if (old_name) {
329                         shash_delete(names, shash_find(names, row_uuid));
330                         free(old_name);
331                     }
332                     shash_add(names, row_uuid, (new_name
333                                                 ? xstrdup(new_name)
334                                                 : xmemdup0(row_uuid, 8)));
335                 }
336             } else if (columns->type == JSON_NULL) {
337                 struct shash_node *node;
338
339                 printf("\t\tdelete row\n");
340                 node = shash_find(names, row_uuid);
341                 if (node) {
342                     shash_delete(names, node);
343                 }
344                 free(old_name);
345             }
346
347             if (free_new_name) {
348                 free(new_name);
349             }
350         }
351     }
352 }
353
354 static void
355 do_show_log(int argc OVS_UNUSED, char *argv[])
356 {
357     const char *db_file_name = argv[1];
358     struct shash names;
359     struct ovsdb_log *log;
360     unsigned int i;
361
362     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
363                                      -1, &log));
364     shash_init(&names);
365     for (i = 0; ; i++) {
366         struct json *json;
367
368         check_ovsdb_error(ovsdb_log_read(log, &json));
369         if (!json) {
370             break;
371         }
372
373         printf("record %u:", i);
374         if (json->type == JSON_OBJECT) {
375             struct json *date, *comment;
376
377             date = shash_find_data(json_object(json), "_date");
378             if (date && date->type == JSON_INTEGER) {
379                 time_t t = json_integer(date);
380                 char s[128];
381
382                 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
383                 printf(" %s", s);
384             }
385
386             comment = shash_find_data(json_object(json), "_comment");
387             if (comment && comment->type == JSON_STRING) {
388                 printf(" \"%s\"", json_string(comment));
389             }
390
391             if (i > 0 && show_log_verbosity > 0) {
392                 putchar('\n');
393                 print_db_changes(json_object(json), &names);
394             }
395         }
396         json_destroy(json);
397         putchar('\n');
398     }
399
400     /* XXX free 'names'. */
401 }
402
403 static void
404 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
405 {
406     usage();
407 }
408
409 static const struct command all_commands[] = {
410     { "create", 2, 2, do_create },
411     { "compact", 1, 2, do_compact },
412     { "convert", 2, 3, do_convert },
413     { "query", 2, 2, do_query },
414     { "transact", 2, 2, do_transact },
415     { "show-log", 1, 1, do_show_log },
416     { "help", 0, INT_MAX, do_help },
417     { NULL, 0, 0, NULL },
418 };