ovsdb-tool: Add commands for printing the database checksum.
[cascardo/ovs.git] / ovsdb / ovsdb.c
1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include "column.h"
21 #include "json.h"
22 #include "ovsdb-error.h"
23 #include "ovsdb-parser.h"
24 #include "ovsdb-types.h"
25 #include "table.h"
26 #include "transaction.h"
27
28 struct ovsdb_schema *
29 ovsdb_schema_create(const char *name, const char *version, const char *cksum)
30 {
31     struct ovsdb_schema *schema;
32
33     schema = xzalloc(sizeof *schema);
34     schema->name = xstrdup(name);
35     schema->version = xstrdup(version);
36     schema->cksum = xstrdup(cksum);
37     shash_init(&schema->tables);
38
39     return schema;
40 }
41
42 struct ovsdb_schema *
43 ovsdb_schema_clone(const struct ovsdb_schema *old)
44 {
45     struct ovsdb_schema *new;
46     struct shash_node *node;
47
48     new = ovsdb_schema_create(old->name, old->version, old->cksum);
49     SHASH_FOR_EACH (node, &old->tables) {
50         const struct ovsdb_table_schema *ts = node->data;
51
52         shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
53     }
54     return new;
55 }
56
57 void
58 ovsdb_schema_destroy(struct ovsdb_schema *schema)
59 {
60     struct shash_node *node;
61
62     if (!schema) {
63         return;
64     }
65
66     SHASH_FOR_EACH (node, &schema->tables) {
67         ovsdb_table_schema_destroy(node->data);
68     }
69     shash_destroy(&schema->tables);
70     free(schema->name);
71     free(schema->version);
72     free(schema->cksum);
73     free(schema);
74 }
75
76 struct ovsdb_error *
77 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
78 {
79     struct ovsdb_schema *schema;
80     struct ovsdb_error *error;
81     struct json *json;
82
83     *schemap = NULL;
84     json = json_from_file(file_name);
85     if (json->type == JSON_STRING) {
86         error = ovsdb_error("failed to read schema",
87                            "\"%s\" could not be read as JSON (%s)",
88                            file_name, json_string(json));
89         json_destroy(json);
90         return error;
91     }
92
93     error = ovsdb_schema_from_json(json, &schema);
94     json_destroy(json);
95     if (error) {
96         return ovsdb_wrap_error(error,
97                                 "failed to parse \"%s\" as ovsdb schema",
98                                 file_name);
99     }
100
101     *schemap = schema;
102     return NULL;
103 }
104
105 static struct ovsdb_error * WARN_UNUSED_RESULT
106 ovsdb_schema_check_ref_table(const struct ovsdb_column *column,
107                              const struct shash *tables,
108                              const struct ovsdb_base_type *base,
109                              const char *base_name)
110 {
111     if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName
112         && !shash_find(tables, base->u.uuid.refTableName)) {
113         return ovsdb_syntax_error(NULL, NULL,
114                                   "column %s %s refers to undefined table %s",
115                                   column->name, base_name,
116                                   base->u.uuid.refTableName);
117     } else {
118         return NULL;
119     }
120 }
121
122 static bool
123 is_valid_version(const char *s)
124 {
125     int n = -1;
126     sscanf(s, "%*[0-9].%*[0-9].%*[0-9]%n", &n);
127     return n != -1 && s[n] == '\0';
128 }
129
130 struct ovsdb_error *
131 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
132 {
133     struct ovsdb_schema *schema;
134     const struct json *name, *tables, *version_json, *cksum;
135     struct ovsdb_error *error;
136     struct shash_node *node;
137     struct ovsdb_parser parser;
138     const char *version;
139
140     *schemap = NULL;
141
142     ovsdb_parser_init(&parser, json, "database schema");
143     name = ovsdb_parser_member(&parser, "name", OP_ID);
144     version_json = ovsdb_parser_member(&parser, "version",
145                                        OP_STRING | OP_OPTIONAL);
146     cksum = ovsdb_parser_member(&parser, "cksum", OP_STRING | OP_OPTIONAL);
147     tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
148     error = ovsdb_parser_finish(&parser);
149     if (error) {
150         return error;
151     }
152
153     if (version_json) {
154         version = json_string(version_json);
155         if (!is_valid_version(version)) {
156             return ovsdb_syntax_error(json, NULL, "schema version \"%s\" not "
157                                       "in format x.y.z", version);
158         }
159     } else {
160         /* Backward compatibility with old databases. */
161         version = "";
162     }
163
164     schema = ovsdb_schema_create(json_string(name), version,
165                                  cksum ? json_string(cksum) : "");
166     SHASH_FOR_EACH (node, json_object(tables)) {
167         struct ovsdb_table_schema *table;
168
169         if (node->name[0] == '_') {
170             error = ovsdb_syntax_error(json, NULL, "names beginning with "
171                                        "\"_\" are reserved");
172         } else if (!ovsdb_parser_is_id(node->name)) {
173             error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
174         } else {
175             error = ovsdb_table_schema_from_json(node->data, node->name,
176                                                  &table);
177         }
178         if (error) {
179             ovsdb_schema_destroy(schema);
180             return error;
181         }
182
183         shash_add(&schema->tables, table->name, table);
184     }
185
186     /* Validate that all refTables refer to the names of tables that exist. */
187     SHASH_FOR_EACH (node, &schema->tables) {
188         struct ovsdb_table_schema *table = node->data;
189         struct shash_node *node2;
190
191         SHASH_FOR_EACH (node2, &table->columns) {
192             struct ovsdb_column *column = node2->data;
193
194             error = ovsdb_schema_check_ref_table(column, &schema->tables,
195                                                  &column->type.key, "key");
196             if (!error) {
197                 error = ovsdb_schema_check_ref_table(column, &schema->tables,
198                                                      &column->type.value,
199                                                      "value");
200             }
201             if (error) {
202                 ovsdb_schema_destroy(schema);
203                 return error;
204             }
205         }
206     }
207
208     *schemap = schema;
209     return 0;
210 }
211
212 struct json *
213 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
214 {
215     struct json *json, *tables;
216     struct shash_node *node;
217
218     json = json_object_create();
219     json_object_put_string(json, "name", schema->name);
220     if (schema->version[0]) {
221         json_object_put_string(json, "version", schema->version);
222     }
223     if (schema->cksum[0]) {
224         json_object_put_string(json, "cksum", schema->cksum);
225     }
226
227     tables = json_object_create();
228
229     SHASH_FOR_EACH (node, &schema->tables) {
230         struct ovsdb_table_schema *table = node->data;
231         json_object_put(tables, table->name,
232                         ovsdb_table_schema_to_json(table));
233     }
234     json_object_put(json, "tables", tables);
235
236     return json;
237 }
238 \f
239 static void
240 ovsdb_set_ref_table(const struct shash *tables,
241                     struct ovsdb_base_type *base)
242 {
243     if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
244         struct ovsdb_table *table;
245
246         table = shash_find_data(tables, base->u.uuid.refTableName);
247         base->u.uuid.refTable = table;
248     }
249 }
250
251 struct ovsdb *
252 ovsdb_create(struct ovsdb_schema *schema)
253 {
254     struct shash_node *node;
255     struct ovsdb *db;
256
257     db = xmalloc(sizeof *db);
258     db->schema = schema;
259     list_init(&db->replicas);
260     list_init(&db->triggers);
261     db->run_triggers = false;
262
263     shash_init(&db->tables);
264     SHASH_FOR_EACH (node, &schema->tables) {
265         struct ovsdb_table_schema *ts = node->data;
266         shash_add(&db->tables, node->name, ovsdb_table_create(ts));
267     }
268
269     /* Set all the refTables. */
270     SHASH_FOR_EACH (node, &schema->tables) {
271         struct ovsdb_table_schema *table = node->data;
272         struct shash_node *node2;
273
274         SHASH_FOR_EACH (node2, &table->columns) {
275             struct ovsdb_column *column = node2->data;
276
277             ovsdb_set_ref_table(&db->tables, &column->type.key);
278             ovsdb_set_ref_table(&db->tables, &column->type.value);
279         }
280     }
281
282     return db;
283 }
284
285 void
286 ovsdb_destroy(struct ovsdb *db)
287 {
288     if (db) {
289         struct shash_node *node;
290
291         /* Remove all the replicas. */
292         while (!list_is_empty(&db->replicas)) {
293             struct ovsdb_replica *r
294                 = CONTAINER_OF(list_pop_back(&db->replicas),
295                                struct ovsdb_replica, node);
296             ovsdb_remove_replica(db, r);
297         }
298
299         /* Delete all the tables.  This also deletes their schemas. */
300         SHASH_FOR_EACH (node, &db->tables) {
301             struct ovsdb_table *table = node->data;
302             ovsdb_table_destroy(table);
303         }
304         shash_destroy(&db->tables);
305
306         /* The schemas, but not the table that points to them, were deleted in
307          * the previous step, so we need to clear out the table.  We can't
308          * destroy the table, because ovsdb_schema_destroy() will do that. */
309         shash_clear(&db->schema->tables);
310
311         ovsdb_schema_destroy(db->schema);
312         free(db);
313     }
314 }
315
316 struct ovsdb_table *
317 ovsdb_get_table(const struct ovsdb *db, const char *name)
318 {
319     return shash_find_data(&db->tables, name);
320 }
321 \f
322 void
323 ovsdb_replica_init(struct ovsdb_replica *r,
324                    const struct ovsdb_replica_class *class)
325 {
326     r->class = class;
327 }
328
329 void
330 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
331 {
332     list_push_back(&db->replicas, &r->node);
333 }
334
335 void
336 ovsdb_remove_replica(struct ovsdb *db OVS_UNUSED, struct ovsdb_replica *r)
337 {
338     list_remove(&r->node);
339     (r->class->destroy)(r);
340 }