1 /* Copyright (c) 2009 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.
21 #include "ovsdb-error.h"
22 #include "ovsdb-parser.h"
24 #include "transaction.h"
27 ovsdb_schema_create(const char *name, const char *comment)
29 struct ovsdb_schema *schema;
31 schema = xzalloc(sizeof *schema);
32 schema->name = xstrdup(name);
33 schema->comment = comment ? xstrdup(comment) : NULL;
34 shash_init(&schema->tables);
40 ovsdb_schema_destroy(struct ovsdb_schema *schema)
42 struct shash_node *node;
44 SHASH_FOR_EACH (node, &schema->tables) {
45 ovsdb_table_schema_destroy(node->data);
47 shash_destroy(&schema->tables);
48 free(schema->comment);
54 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
56 struct ovsdb_schema *schema;
57 struct ovsdb_error *error;
61 json = json_from_file(file_name);
62 if (json->type == JSON_STRING) {
63 error = ovsdb_error("failed to read schema",
64 "\"%s\" could not be read as JSON (%s)",
65 file_name, json_string(json));
70 error = ovsdb_schema_from_json(json, &schema);
73 return ovsdb_wrap_error(error,
74 "failed to parse \"%s\" as ovsdb schema",
83 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
85 struct ovsdb_schema *schema;
86 const struct json *name, *comment, *tables;
87 struct ovsdb_error *error;
88 struct shash_node *node;
89 struct ovsdb_parser parser;
93 ovsdb_parser_init(&parser, json, "database schema");
94 name = ovsdb_parser_member(&parser, "name", OP_ID);
95 comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
96 tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
97 error = ovsdb_parser_finish(&parser);
102 schema = ovsdb_schema_create(json_string(name),
103 comment ? json_string(comment) : NULL);
104 SHASH_FOR_EACH (node, json_object(tables)) {
105 struct ovsdb_table_schema *table;
107 if (node->name[0] == '_') {
108 error = ovsdb_syntax_error(json, NULL, "names beginning with "
109 "\"_\" are reserved");
111 error = ovsdb_table_schema_from_json(node->data, node->name,
115 ovsdb_schema_destroy(schema);
119 shash_add(&schema->tables, table->name, table);
126 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
128 struct json *json, *tables;
129 struct shash_node *node;
131 json = json_object_create();
132 json_object_put_string(json, "name", schema->name);
133 if (schema->comment) {
134 json_object_put_string(json, "comment", schema->comment);
137 tables = json_object_create();
139 SHASH_FOR_EACH (node, &schema->tables) {
140 struct ovsdb_table_schema *table = node->data;
141 json_object_put(tables, table->name,
142 ovsdb_table_schema_to_json(table));
144 json_object_put(json, "tables", tables);
150 ovsdb_create(struct ovsdb_schema *schema)
152 struct shash_node *node;
155 db = xmalloc(sizeof *db);
157 list_init(&db->replicas);
158 list_init(&db->triggers);
159 db->run_triggers = false;
161 shash_init(&db->tables);
162 SHASH_FOR_EACH (node, &schema->tables) {
163 struct ovsdb_table_schema *ts = node->data;
164 shash_add(&db->tables, node->name, ovsdb_table_create(ts));
171 ovsdb_destroy(struct ovsdb *db)
174 struct shash_node *node;
176 /* Remove all the replicas. */
177 while (!list_is_empty(&db->replicas)) {
178 struct ovsdb_replica *r
179 = CONTAINER_OF(list_pop_back(&db->replicas),
180 struct ovsdb_replica, node);
181 ovsdb_remove_replica(db, r);
184 /* Delete all the tables. This also deletes their schemas. */
185 SHASH_FOR_EACH (node, &db->tables) {
186 struct ovsdb_table *table = node->data;
187 ovsdb_table_destroy(table);
189 shash_destroy(&db->tables);
191 /* The schemas, but not the table that points to them, were deleted in
192 * the previous step, so we need to clear out the table. We can't
193 * destroy the table, because ovsdb_schema_destroy() will do that. */
194 shash_clear(&db->schema->tables);
196 ovsdb_schema_destroy(db->schema);
202 ovsdb_get_table(const struct ovsdb *db, const char *name)
204 return shash_find_data(&db->tables, name);
208 ovsdb_replica_init(struct ovsdb_replica *r,
209 const struct ovsdb_replica_class *class)
215 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
217 list_push_back(&db->replicas, &r->node);
221 ovsdb_remove_replica(struct ovsdb *db UNUSED, struct ovsdb_replica *r)
223 list_remove(&r->node);
224 (r->class->destroy)(r);