1 /* Copyright (c) 2009, 2010 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "ovsdb-error.h"
23 #include "ovsdb-parser.h"
24 #include "ovsdb-types.h"
26 #include "transaction.h"
29 ovsdb_schema_create(const char *name, const char *comment)
31 struct ovsdb_schema *schema;
33 schema = xzalloc(sizeof *schema);
34 schema->name = xstrdup(name);
35 schema->comment = comment ? xstrdup(comment) : NULL;
36 shash_init(&schema->tables);
42 ovsdb_schema_clone(const struct ovsdb_schema *old)
44 struct ovsdb_schema *new;
45 struct shash_node *node;
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;
51 shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
58 ovsdb_schema_destroy(struct ovsdb_schema *schema)
60 struct shash_node *node;
62 SHASH_FOR_EACH (node, &schema->tables) {
63 ovsdb_table_schema_destroy(node->data);
65 shash_destroy(&schema->tables);
66 free(schema->comment);
72 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
74 struct ovsdb_schema *schema;
75 struct ovsdb_error *error;
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));
88 error = ovsdb_schema_from_json(json, &schema);
91 return ovsdb_wrap_error(error,
92 "failed to parse \"%s\" as ovsdb schema",
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)
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);
118 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
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;
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);
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;
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");
148 error = ovsdb_table_schema_from_json(node->data, node->name,
152 ovsdb_schema_destroy(schema);
156 shash_add(&schema->tables, table->name, table);
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;
164 SHASH_FOR_EACH (node2, &table->columns) {
165 struct ovsdb_column *column = node2->data;
167 error = ovsdb_schema_check_ref_table(column, &schema->tables,
168 &column->type.key, "key");
170 error = ovsdb_schema_check_ref_table(column, &schema->tables,
175 ovsdb_schema_destroy(schema);
186 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
188 struct json *json, *tables;
189 struct shash_node *node;
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);
197 tables = json_object_create();
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));
204 json_object_put(json, "tables", tables);
210 ovsdb_set_ref_table(const struct shash *tables,
211 struct ovsdb_base_type *base)
213 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
214 struct ovsdb_table *table;
216 table = shash_find_data(tables, base->u.uuid.refTableName);
217 base->u.uuid.refTable = table;
222 ovsdb_create(struct ovsdb_schema *schema)
224 struct shash_node *node;
227 db = xmalloc(sizeof *db);
229 list_init(&db->replicas);
230 list_init(&db->triggers);
231 db->run_triggers = false;
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));
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;
244 SHASH_FOR_EACH (node2, &table->columns) {
245 struct ovsdb_column *column = node2->data;
247 ovsdb_set_ref_table(&db->tables, &column->type.key);
248 ovsdb_set_ref_table(&db->tables, &column->type.value);
256 ovsdb_destroy(struct ovsdb *db)
259 struct shash_node *node;
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);
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);
274 shash_destroy(&db->tables);
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);
281 ovsdb_schema_destroy(db->schema);
287 ovsdb_get_table(const struct ovsdb *db, const char *name)
289 return shash_find_data(&db->tables, name);
293 ovsdb_replica_init(struct ovsdb_replica *r,
294 const struct ovsdb_replica_class *class)
300 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
302 list_push_back(&db->replicas, &r->node);
306 ovsdb_remove_replica(struct ovsdb *db OVS_UNUSED, struct ovsdb_replica *r)
308 list_remove(&r->node);
309 (r->class->destroy)(r);