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