Merge "master" into "next".
[cascardo/ovs.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 "log.h"
29 #include "json.h"
30 #include "ovsdb.h"
31 #include "ovsdb-error.h"
32 #include "table.h"
33 #include "timeval.h"
34 #include "util.h"
35
36 #include "vlog.h"
37 #define THIS_MODULE VLM_ovsdb_tool
38
39 /* -m, --more: Verbosity level for "show-log" command output. */
40 static int show_log_verbosity;
41
42 static const struct command all_commands[];
43
44 static void usage(void) NO_RETURN;
45 static void parse_options(int argc, char *argv[]);
46
47 int
48 main(int argc, char *argv[])
49 {
50     set_program_name(argv[0]);
51     time_init();
52     vlog_init();
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            "  extract-schema DB  print DB's schema on stdout\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, O_RDWR | O_CREAT | O_EXCL,
168                                      &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 transact(bool read_only, const char *db_file_name, const char *transaction)
178 {
179     struct json *request, *result;
180     struct ovsdb *db;
181
182     check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db));
183
184     request = parse_json(transaction);
185     result = ovsdb_execute(db, request, 0, NULL);
186     json_destroy(request);
187
188     print_and_free_json(result);
189     ovsdb_destroy(db);
190 }
191
192 static void
193 do_query(int argc OVS_UNUSED, char *argv[])
194 {
195     transact(true, argv[1], argv[2]);
196 }
197
198 static void
199 do_transact(int argc OVS_UNUSED, char *argv[])
200 {
201     transact(false, argv[1], argv[2]);
202 }
203
204 static void
205 print_db_changes(struct shash *tables, struct shash *names)
206 {
207     struct shash_node *n1;
208
209     SHASH_FOR_EACH (n1, tables) {
210         const char *table = n1->name;
211         struct json *rows = n1->data;
212         struct shash_node *n2;
213
214         if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
215             continue;
216         }
217
218         SHASH_FOR_EACH (n2, json_object(rows)) {
219             const char *row_uuid = n2->name;
220             struct json *columns = n2->data;
221             struct shash_node *n3;
222             char *old_name, *new_name;
223             bool free_new_name = false;
224
225             old_name = new_name = shash_find_data(names, row_uuid);
226             if (columns->type == JSON_OBJECT) {
227                 struct json *new_name_json;
228
229                 new_name_json = shash_find_data(json_object(columns), "name");
230                 if (new_name_json) {
231                     new_name = json_to_string(new_name_json, JSSF_SORT);
232                     free_new_name = true;
233                 }
234             }
235
236             printf("\ttable %s", table);
237
238             if (!old_name) {
239                 if (new_name) {
240                     printf(" insert row %s:\n", new_name);
241                 } else {
242                     printf(" insert row %.8s:\n", row_uuid);
243                 }
244             } else {
245                 printf(" row %s:\n", old_name);
246             }
247
248             if (columns->type == JSON_OBJECT) {
249                 if (show_log_verbosity > 1) {
250                     SHASH_FOR_EACH (n3, json_object(columns)) {
251                         const char *column = n3->name;
252                         struct json *value = n3->data;
253                         char *value_string;
254
255                         value_string = json_to_string(value, JSSF_SORT);
256                         printf("\t\t%s=%s\n", column, value_string);
257                         free(value_string);
258                     }
259                 }
260                 if (!old_name
261                     || (new_name != old_name && strcmp(old_name, new_name))) {
262                     if (old_name) {
263                         shash_delete(names, shash_find(names, row_uuid));
264                         free(old_name);
265                     }
266                     shash_add(names, row_uuid, (new_name
267                                                 ? xstrdup(new_name)
268                                                 : xmemdup0(row_uuid, 8)));
269                 }
270             } else if (columns->type == JSON_NULL) {
271                 printf("\t\tdelete row\n");
272                 shash_delete(names, shash_find(names, row_uuid));
273                 free(old_name);
274             }
275
276             if (free_new_name) {
277                 free(new_name);
278             }
279         }
280     }
281 }
282
283 static void
284 do_show_log(int argc OVS_UNUSED, char *argv[])
285 {
286     const char *db_file_name = argv[1];
287     struct shash names;
288     struct ovsdb_log *log;
289     unsigned int i;
290
291     check_ovsdb_error(ovsdb_log_open(db_file_name, O_RDONLY, &log));
292     shash_init(&names);
293     for (i = 0; ; i++) {
294         struct json *json;
295
296         check_ovsdb_error(ovsdb_log_read(log, &json));
297         if (!json) {
298             break;
299         }
300
301         printf("record %u:", i);
302         if (json->type == JSON_OBJECT) {
303             struct json *date, *comment;
304
305             date = shash_find_data(json_object(json), "_date");
306             if (date && date->type == JSON_INTEGER) {
307                 time_t t = json_integer(date);
308                 char s[128];
309
310                 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
311                 printf(" %s", s);
312             }
313
314             comment = shash_find_data(json_object(json), "_comment");
315             if (comment && comment->type == JSON_STRING) {
316                 printf(" \"%s\"", json_string(comment));
317             }
318
319             if (i > 0 && show_log_verbosity > 0) {
320                 putchar('\n');
321                 print_db_changes(json_object(json), &names);
322             }
323         }
324         json_destroy(json);
325         putchar('\n');
326     }
327
328     /* XXX free 'names'. */
329 }
330
331 static void
332 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
333 {
334     usage();
335 }
336
337 static const struct command all_commands[] = {
338     { "create", 2, 2, do_create },
339     { "query", 2, 2, do_query },
340     { "transact", 2, 2, do_transact },
341     { "show-log", 1, 1, do_show_log },
342     { "help", 0, INT_MAX, do_help },
343     { NULL, 0, 0, NULL },
344 };