ovsdb: Fix ovsdb-server crash when specifying nonexistent file.
[cascardo/ovs.git] / ovsdb / ovsdb.c
1 /* Copyright (c) 2009, 2010 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)
30 {
31     struct ovsdb_schema *schema;
32
33     schema = xzalloc(sizeof *schema);
34     schema->name = xstrdup(name);
35     shash_init(&schema->tables);
36
37     return schema;
38 }
39
40 struct ovsdb_schema *
41 ovsdb_schema_clone(const struct ovsdb_schema *old)
42 {
43     struct ovsdb_schema *new;
44     struct shash_node *node;
45
46     new = ovsdb_schema_create(old->name);
47     SHASH_FOR_EACH (node, &old->tables) {
48         const struct ovsdb_table_schema *ts = node->data;
49
50         shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
51     }
52     return new;
53 }
54
55
56 void
57 ovsdb_schema_destroy(struct ovsdb_schema *schema)
58 {
59     struct shash_node *node;
60
61     if (!schema) {
62         return;
63     }
64
65     SHASH_FOR_EACH (node, &schema->tables) {
66         ovsdb_table_schema_destroy(node->data);
67     }
68     shash_destroy(&schema->tables);
69     free(schema->name);
70     free(schema);
71 }
72
73 struct ovsdb_error *
74 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
75 {
76     struct ovsdb_schema *schema;
77     struct ovsdb_error *error;
78     struct json *json;
79
80     *schemap = NULL;
81     json = json_from_file(file_name);
82     if (json->type == JSON_STRING) {
83         error = ovsdb_error("failed to read schema",
84                            "\"%s\" could not be read as JSON (%s)",
85                            file_name, json_string(json));
86         json_destroy(json);
87         return error;
88     }
89
90     error = ovsdb_schema_from_json(json, &schema);
91     json_destroy(json);
92     if (error) {
93         return ovsdb_wrap_error(error,
94                                 "failed to parse \"%s\" as ovsdb schema",
95                                 file_name);
96     }
97
98     *schemap = schema;
99     return NULL;
100 }
101
102 static struct ovsdb_error * WARN_UNUSED_RESULT
103 ovsdb_schema_check_ref_table(const struct ovsdb_column *column,
104                              const struct shash *tables,
105                              const struct ovsdb_base_type *base,
106                              const char *base_name)
107 {
108     if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName
109         && !shash_find(tables, base->u.uuid.refTableName)) {
110         return ovsdb_syntax_error(NULL, NULL,
111                                   "column %s %s refers to undefined table %s",
112                                   column->name, base_name,
113                                   base->u.uuid.refTableName);
114     } else {
115         return NULL;
116     }
117 }
118
119 struct ovsdb_error *
120 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
121 {
122     struct ovsdb_schema *schema;
123     const struct json *name, *tables;
124     struct ovsdb_error *error;
125     struct shash_node *node;
126     struct ovsdb_parser parser;
127
128     *schemap = NULL;
129
130     ovsdb_parser_init(&parser, json, "database schema");
131     name = ovsdb_parser_member(&parser, "name", OP_ID);
132     tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
133     error = ovsdb_parser_finish(&parser);
134     if (error) {
135         return error;
136     }
137
138     schema = ovsdb_schema_create(json_string(name));
139     SHASH_FOR_EACH (node, json_object(tables)) {
140         struct ovsdb_table_schema *table;
141
142         if (node->name[0] == '_') {
143             error = ovsdb_syntax_error(json, NULL, "names beginning with "
144                                        "\"_\" are reserved");
145         } else if (!ovsdb_parser_is_id(node->name)) {
146             error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
147         } else {
148             error = ovsdb_table_schema_from_json(node->data, node->name,
149                                                  &table);
150         }
151         if (error) {
152             ovsdb_schema_destroy(schema);
153             return error;
154         }
155
156         shash_add(&schema->tables, table->name, table);
157     }
158
159     /* Validate that all refTables refer to the names of tables that exist. */
160     SHASH_FOR_EACH (node, &schema->tables) {
161         struct ovsdb_table_schema *table = node->data;
162         struct shash_node *node2;
163
164         SHASH_FOR_EACH (node2, &table->columns) {
165             struct ovsdb_column *column = node2->data;
166
167             error = ovsdb_schema_check_ref_table(column, &schema->tables,
168                                                  &column->type.key, "key");
169             if (!error) {
170                 error = ovsdb_schema_check_ref_table(column, &schema->tables,
171                                                      &column->type.value,
172                                                      "value");
173             }
174             if (error) {
175                 ovsdb_schema_destroy(schema);
176                 return error;
177             }
178         }
179     }
180
181     *schemap = schema;
182     return 0;
183 }
184
185 struct json *
186 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
187 {
188     struct json *json, *tables;
189     struct shash_node *node;
190
191     json = json_object_create();
192     json_object_put_string(json, "name", schema->name);
193
194     tables = json_object_create();
195
196     SHASH_FOR_EACH (node, &schema->tables) {
197         struct ovsdb_table_schema *table = node->data;
198         json_object_put(tables, table->name,
199                         ovsdb_table_schema_to_json(table));
200     }
201     json_object_put(json, "tables", tables);
202
203     return json;
204 }
205 \f
206 static void
207 ovsdb_set_ref_table(const struct shash *tables,
208                     struct ovsdb_base_type *base)
209 {
210     if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
211         struct ovsdb_table *table;
212
213         table = shash_find_data(tables, base->u.uuid.refTableName);
214         base->u.uuid.refTable = table;
215     }
216 }
217
218 struct ovsdb *
219 ovsdb_create(struct ovsdb_schema *schema)
220 {
221     struct shash_node *node;
222     struct ovsdb *db;
223
224     db = xmalloc(sizeof *db);
225     db->schema = schema;
226     list_init(&db->replicas);
227     list_init(&db->triggers);
228     db->run_triggers = false;
229
230     shash_init(&db->tables);
231     SHASH_FOR_EACH (node, &schema->tables) {
232         struct ovsdb_table_schema *ts = node->data;
233         shash_add(&db->tables, node->name, ovsdb_table_create(ts));
234     }
235
236     /* Set all the refTables. */
237     SHASH_FOR_EACH (node, &schema->tables) {
238         struct ovsdb_table_schema *table = node->data;
239         struct shash_node *node2;
240
241         SHASH_FOR_EACH (node2, &table->columns) {
242             struct ovsdb_column *column = node2->data;
243
244             ovsdb_set_ref_table(&db->tables, &column->type.key);
245             ovsdb_set_ref_table(&db->tables, &column->type.value);
246         }
247     }
248
249     return db;
250 }
251
252 void
253 ovsdb_destroy(struct ovsdb *db)
254 {
255     if (db) {
256         struct shash_node *node;
257
258         /* Remove all the replicas. */
259         while (!list_is_empty(&db->replicas)) {
260             struct ovsdb_replica *r
261                 = CONTAINER_OF(list_pop_back(&db->replicas),
262                                struct ovsdb_replica, node);
263             ovsdb_remove_replica(db, r);
264         }
265
266         /* Delete all the tables.  This also deletes their schemas. */
267         SHASH_FOR_EACH (node, &db->tables) {
268             struct ovsdb_table *table = node->data;
269             ovsdb_table_destroy(table);
270         }
271         shash_destroy(&db->tables);
272
273         /* The schemas, but not the table that points to them, were deleted in
274          * the previous step, so we need to clear out the table.  We can't
275          * destroy the table, because ovsdb_schema_destroy() will do that. */
276         shash_clear(&db->schema->tables);
277
278         ovsdb_schema_destroy(db->schema);
279         free(db);
280     }
281 }
282
283 struct ovsdb_table *
284 ovsdb_get_table(const struct ovsdb *db, const char *name)
285 {
286     return shash_find_data(&db->tables, name);
287 }
288 \f
289 void
290 ovsdb_replica_init(struct ovsdb_replica *r,
291                    const struct ovsdb_replica_class *class)
292 {
293     r->class = class;
294 }
295
296 void
297 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
298 {
299     list_push_back(&db->replicas, &r->node);
300 }
301
302 void
303 ovsdb_remove_replica(struct ovsdb *db OVS_UNUSED, struct ovsdb_replica *r)
304 {
305     list_remove(&r->node);
306     (r->class->destroy)(r);
307 }