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;
61 SHASH_FOR_EACH (node, &schema->tables) {
62 ovsdb_table_schema_destroy(node->data);
64 shash_destroy(&schema->tables);
70 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
72 struct ovsdb_schema *schema;
73 struct ovsdb_error *error;
77 json = json_from_file(file_name);
78 if (json->type == JSON_STRING) {
79 error = ovsdb_error("failed to read schema",
80 "\"%s\" could not be read as JSON (%s)",
81 file_name, json_string(json));
86 error = ovsdb_schema_from_json(json, &schema);
89 return ovsdb_wrap_error(error,
90 "failed to parse \"%s\" as ovsdb schema",
98 static struct ovsdb_error * WARN_UNUSED_RESULT
99 ovsdb_schema_check_ref_table(const struct ovsdb_column *column,
100 const struct shash *tables,
101 const struct ovsdb_base_type *base,
102 const char *base_name)
104 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName
105 && !shash_find(tables, base->u.uuid.refTableName)) {
106 return ovsdb_syntax_error(NULL, NULL,
107 "column %s %s refers to undefined table %s",
108 column->name, base_name,
109 base->u.uuid.refTableName);
116 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
118 struct ovsdb_schema *schema;
119 const struct json *name, *tables;
120 struct ovsdb_error *error;
121 struct shash_node *node;
122 struct ovsdb_parser parser;
126 ovsdb_parser_init(&parser, json, "database schema");
127 name = ovsdb_parser_member(&parser, "name", OP_ID);
128 tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
129 error = ovsdb_parser_finish(&parser);
134 schema = ovsdb_schema_create(json_string(name));
135 SHASH_FOR_EACH (node, json_object(tables)) {
136 struct ovsdb_table_schema *table;
138 if (node->name[0] == '_') {
139 error = ovsdb_syntax_error(json, NULL, "names beginning with "
140 "\"_\" are reserved");
141 } else if (!ovsdb_parser_is_id(node->name)) {
142 error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
144 error = ovsdb_table_schema_from_json(node->data, node->name,
148 ovsdb_schema_destroy(schema);
152 shash_add(&schema->tables, table->name, table);
155 /* Validate that all refTables refer to the names of tables that exist. */
156 SHASH_FOR_EACH (node, &schema->tables) {
157 struct ovsdb_table_schema *table = node->data;
158 struct shash_node *node2;
160 SHASH_FOR_EACH (node2, &table->columns) {
161 struct ovsdb_column *column = node2->data;
163 error = ovsdb_schema_check_ref_table(column, &schema->tables,
164 &column->type.key, "key");
166 error = ovsdb_schema_check_ref_table(column, &schema->tables,
171 ovsdb_schema_destroy(schema);
182 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
184 struct json *json, *tables;
185 struct shash_node *node;
187 json = json_object_create();
188 json_object_put_string(json, "name", schema->name);
190 tables = json_object_create();
192 SHASH_FOR_EACH (node, &schema->tables) {
193 struct ovsdb_table_schema *table = node->data;
194 json_object_put(tables, table->name,
195 ovsdb_table_schema_to_json(table));
197 json_object_put(json, "tables", tables);
203 ovsdb_set_ref_table(const struct shash *tables,
204 struct ovsdb_base_type *base)
206 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
207 struct ovsdb_table *table;
209 table = shash_find_data(tables, base->u.uuid.refTableName);
210 base->u.uuid.refTable = table;
215 ovsdb_create(struct ovsdb_schema *schema)
217 struct shash_node *node;
220 db = xmalloc(sizeof *db);
222 list_init(&db->replicas);
223 list_init(&db->triggers);
224 db->run_triggers = false;
226 shash_init(&db->tables);
227 SHASH_FOR_EACH (node, &schema->tables) {
228 struct ovsdb_table_schema *ts = node->data;
229 shash_add(&db->tables, node->name, ovsdb_table_create(ts));
232 /* Set all the refTables. */
233 SHASH_FOR_EACH (node, &schema->tables) {
234 struct ovsdb_table_schema *table = node->data;
235 struct shash_node *node2;
237 SHASH_FOR_EACH (node2, &table->columns) {
238 struct ovsdb_column *column = node2->data;
240 ovsdb_set_ref_table(&db->tables, &column->type.key);
241 ovsdb_set_ref_table(&db->tables, &column->type.value);
249 ovsdb_destroy(struct ovsdb *db)
252 struct shash_node *node;
254 /* Remove all the replicas. */
255 while (!list_is_empty(&db->replicas)) {
256 struct ovsdb_replica *r
257 = CONTAINER_OF(list_pop_back(&db->replicas),
258 struct ovsdb_replica, node);
259 ovsdb_remove_replica(db, r);
262 /* Delete all the tables. This also deletes their schemas. */
263 SHASH_FOR_EACH (node, &db->tables) {
264 struct ovsdb_table *table = node->data;
265 ovsdb_table_destroy(table);
267 shash_destroy(&db->tables);
269 /* The schemas, but not the table that points to them, were deleted in
270 * the previous step, so we need to clear out the table. We can't
271 * destroy the table, because ovsdb_schema_destroy() will do that. */
272 shash_clear(&db->schema->tables);
274 ovsdb_schema_destroy(db->schema);
280 ovsdb_get_table(const struct ovsdb *db, const char *name)
282 return shash_find_data(&db->tables, name);
286 ovsdb_replica_init(struct ovsdb_replica *r,
287 const struct ovsdb_replica_class *class)
293 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
295 list_push_back(&db->replicas, &r->node);
299 ovsdb_remove_replica(struct ovsdb *db OVS_UNUSED, struct ovsdb_replica *r)
301 list_remove(&r->node);
302 (r->class->destroy)(r);