ovsdb: Rename ovsdb_file to ovsdb_log.
[cascardo/ovs.git] / ovsdb / ovsdb-tool.c
1 /*
2  * Copyright (c) 2009 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 "log.h"
28 #include "json.h"
29 #include "ovsdb.h"
30 #include "ovsdb-error.h"
31 #include "table.h"
32 #include "timeval.h"
33 #include "util.h"
34
35 #include "vlog.h"
36 #define THIS_MODULE VLM_ovsdb_tool
37
38 static const struct command all_commands[];
39
40 static void usage(void) NO_RETURN;
41 static void parse_options(int argc, char *argv[]);
42
43 int
44 main(int argc, char *argv[])
45 {
46     set_program_name(argv[0]);
47     time_init();
48     vlog_init();
49     parse_options(argc, argv);
50     signal(SIGPIPE, SIG_IGN);
51     run_command(argc - optind, argv + optind, all_commands);
52     return 0;
53 }
54
55 static void
56 parse_options(int argc, char *argv[])
57 {
58     static struct option long_options[] = {
59         {"verbose", optional_argument, 0, 'v'},
60         {"help", no_argument, 0, 'h'},
61         {"version", no_argument, 0, 'V'},
62         {0, 0, 0, 0},
63     };
64     char *short_options = long_options_to_short_options(long_options);
65
66     for (;;) {
67         int c;
68
69         c = getopt_long(argc, argv, short_options, long_options, NULL);
70         if (c == -1) {
71             break;
72         }
73
74         switch (c) {
75         case 'h':
76             usage();
77
78         case 'V':
79             OVS_PRINT_VERSION(0, 0);
80             exit(EXIT_SUCCESS);
81
82         case 'v':
83             vlog_set_verbosity(optarg);
84             break;
85
86         case '?':
87             exit(EXIT_FAILURE);
88
89         default:
90             abort();
91         }
92     }
93     free(short_options);
94 }
95
96 static void
97 usage(void)
98 {
99     printf("%s: Open vSwitch database management utility\n"
100            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
101            "  create DB SCHEMA   create DB with the given SCHEMA\n"
102            "  compact DB [DST]   compact DB in-place (or to DST)\n"
103            "  extract-schema DB  print DB's schema on stdout\n"
104            "  query DB TRNS      execute read-only transaction on DB\n"
105            "  transact DB TRNS   execute read/write transaction on DB\n",
106            program_name, program_name);
107     vlog_usage();
108     printf("\nOther options:\n"
109            "  -h, --help                  display this help message\n"
110            "  -V, --version               display version information\n");
111     exit(EXIT_SUCCESS);
112 }
113 \f
114 static struct json *
115 parse_json(const char *s)
116 {
117     struct json *json = json_from_string(s);
118     if (json->type == JSON_STRING) {
119         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
120     }
121     return json;
122 }
123
124 static void
125 print_and_free_json(struct json *json)
126 {
127     char *string = json_to_string(json, JSSF_SORT);
128     json_destroy(json);
129     puts(string);
130     free(string);
131 }
132
133 static void
134 check_ovsdb_error(struct ovsdb_error *error)
135 {
136     if (error) {
137         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
138     }
139 }
140 \f
141 static void
142 do_create(int argc UNUSED, char *argv[])
143 {
144     const char *db_file_name = argv[1];
145     const char *schema_file_name = argv[2];
146     struct ovsdb_schema *schema;
147     struct ovsdb_log *log;
148     struct json *json;
149
150     /* Read schema from file and convert to JSON. */
151     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
152     json = ovsdb_schema_to_json(schema);
153
154     /* Create database file. */
155     check_ovsdb_error(ovsdb_log_open(db_file_name, O_RDWR | O_CREAT | O_EXCL,
156                                      &log));
157     check_ovsdb_error(ovsdb_log_write(log, json));
158     check_ovsdb_error(ovsdb_log_commit(log));
159     ovsdb_log_close(log);
160
161     json_destroy(json);
162 }
163
164 static void
165 transact(bool read_only, const char *db_file_name, const char *transaction)
166 {
167     struct json *request, *result;
168     struct ovsdb *db;
169
170     check_ovsdb_error(ovsdb_open(db_file_name, read_only, &db));
171
172     request = parse_json(transaction);
173     result = ovsdb_execute(db, request, 0, NULL);
174     json_destroy(request);
175
176     print_and_free_json(result);
177     ovsdb_destroy(db);
178 }
179
180 static void
181 do_query(int argc UNUSED, char *argv[])
182 {
183     transact(true, argv[1], argv[2]);
184 }
185
186 static void
187 do_transact(int argc UNUSED, char *argv[])
188 {
189     transact(false, argv[1], argv[2]);
190 }
191
192 static void
193 do_help(int argc UNUSED, char *argv[] UNUSED)
194 {
195     usage();
196 }
197
198 static const struct command all_commands[] = {
199     { "create", 2, 2, do_create },
200     { "query", 2, 2, do_query },
201     { "transact", 2, 2, do_transact },
202     { "help", 0, INT_MAX, do_help },
203     { NULL, 0, 0, NULL },
204 };