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