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_destroy(struct ovsdb_schema *schema)
44 struct shash_node *node;
46 SHASH_FOR_EACH (node, &schema->tables) {
47 ovsdb_table_schema_destroy(node->data);
49 shash_destroy(&schema->tables);
50 free(schema->comment);
56 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
58 struct ovsdb_schema *schema;
59 struct ovsdb_error *error;
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));
72 error = ovsdb_schema_from_json(json, &schema);
75 return ovsdb_wrap_error(error,
76 "failed to parse \"%s\" as ovsdb schema",
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)
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);
102 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
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;
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);
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;
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");
132 error = ovsdb_table_schema_from_json(node->data, node->name,
136 ovsdb_schema_destroy(schema);
140 shash_add(&schema->tables, table->name, table);
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;
148 SHASH_FOR_EACH (node2, &table->columns) {
149 struct ovsdb_column *column = node2->data;
151 error = ovsdb_schema_check_ref_table(column, &schema->tables,
152 &column->type.key, "key");
154 error = ovsdb_schema_check_ref_table(column, &schema->tables,
159 ovsdb_schema_destroy(schema);
170 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
172 struct json *json, *tables;
173 struct shash_node *node;
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);
181 tables = json_object_create();
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));
188 json_object_put(json, "tables", tables);
194 ovsdb_set_ref_table(const struct shash *tables,
195 struct ovsdb_base_type *base)
197 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
198 struct ovsdb_table *table;
200 table = shash_find_data(tables, base->u.uuid.refTableName);
201 base->u.uuid.refTable = table;
206 ovsdb_create(struct ovsdb_schema *schema)
208 struct shash_node *node;
211 db = xmalloc(sizeof *db);
213 list_init(&db->replicas);
214 list_init(&db->triggers);
215 db->run_triggers = false;
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));
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;
228 SHASH_FOR_EACH (node2, &table->columns) {
229 struct ovsdb_column *column = node2->data;
231 ovsdb_set_ref_table(&db->tables, &column->type.key);
232 ovsdb_set_ref_table(&db->tables, &column->type.value);
240 ovsdb_destroy(struct ovsdb *db)
243 struct shash_node *node;
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);
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);
258 shash_destroy(&db->tables);
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);
265 ovsdb_schema_destroy(db->schema);
271 ovsdb_get_table(const struct ovsdb *db, const char *name)
273 return shash_find_data(&db->tables, name);
277 ovsdb_replica_init(struct ovsdb_replica *r,
278 const struct ovsdb_replica_class *class)
284 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
286 list_push_back(&db->replicas, &r->node);
290 ovsdb_remove_replica(struct ovsdb *db UNUSED, struct ovsdb_replica *r)
292 list_remove(&r->node);
293 (r->class->destroy)(r);