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)
31 struct ovsdb_schema *schema;
33 schema = xzalloc(sizeof *schema);
34 schema->name = xstrdup(name);
35 shash_init(&schema->tables);
41 ovsdb_schema_clone(const struct ovsdb_schema *old)
43 struct ovsdb_schema *new;
44 struct shash_node *node;
46 new = ovsdb_schema_create(old->name);
47 SHASH_FOR_EACH (node, &old->tables) {
48 const struct ovsdb_table_schema *ts = node->data;
50 shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
57 ovsdb_schema_destroy(struct ovsdb_schema *schema)
59 struct shash_node *node;
65 SHASH_FOR_EACH (node, &schema->tables) {
66 ovsdb_table_schema_destroy(node->data);
68 shash_destroy(&schema->tables);
74 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
76 struct ovsdb_schema *schema;
77 struct ovsdb_error *error;
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));
90 error = ovsdb_schema_from_json(json, &schema);
93 return ovsdb_wrap_error(error,
94 "failed to parse \"%s\" as ovsdb schema",
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)
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);
120 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
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;
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);
138 schema = ovsdb_schema_create(json_string(name));
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);
194 tables = json_object_create();
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));
201 json_object_put(json, "tables", tables);
207 ovsdb_set_ref_table(const struct shash *tables,
208 struct ovsdb_base_type *base)
210 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
211 struct ovsdb_table *table;
213 table = shash_find_data(tables, base->u.uuid.refTableName);
214 base->u.uuid.refTable = table;
219 ovsdb_create(struct ovsdb_schema *schema)
221 struct shash_node *node;
224 db = xmalloc(sizeof *db);
226 list_init(&db->replicas);
227 list_init(&db->triggers);
228 db->run_triggers = false;
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));
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;
241 SHASH_FOR_EACH (node2, &table->columns) {
242 struct ovsdb_column *column = node2->data;
244 ovsdb_set_ref_table(&db->tables, &column->type.key);
245 ovsdb_set_ref_table(&db->tables, &column->type.value);
253 ovsdb_destroy(struct ovsdb *db)
256 struct shash_node *node;
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);
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);
271 shash_destroy(&db->tables);
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);
278 ovsdb_schema_destroy(db->schema);
284 ovsdb_get_table(const struct ovsdb *db, const char *name)
286 return shash_find_data(&db->tables, name);
290 ovsdb_replica_init(struct ovsdb_replica *r,
291 const struct ovsdb_replica_class *class)
297 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
299 list_push_back(&db->replicas, &r->node);
303 ovsdb_remove_replica(struct ovsdb *db OVS_UNUSED, struct ovsdb_replica *r)
305 list_remove(&r->node);
306 (r->class->destroy)(r);