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");
110 } else if (!ovsdb_parser_is_id(node->name)) {
111 error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
113 error = ovsdb_table_schema_from_json(node->data, node->name,
117 ovsdb_schema_destroy(schema);
121 shash_add(&schema->tables, table->name, table);
128 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
130 struct json *json, *tables;
131 struct shash_node *node;
133 json = json_object_create();
134 json_object_put_string(json, "name", schema->name);
135 if (schema->comment) {
136 json_object_put_string(json, "comment", schema->comment);
139 tables = json_object_create();
141 SHASH_FOR_EACH (node, &schema->tables) {
142 struct ovsdb_table_schema *table = node->data;
143 json_object_put(tables, table->name,
144 ovsdb_table_schema_to_json(table));
146 json_object_put(json, "tables", tables);
152 ovsdb_create(struct ovsdb_schema *schema)
154 struct shash_node *node;
157 db = xmalloc(sizeof *db);
159 list_init(&db->replicas);
160 list_init(&db->triggers);
161 db->run_triggers = false;
163 shash_init(&db->tables);
164 SHASH_FOR_EACH (node, &schema->tables) {
165 struct ovsdb_table_schema *ts = node->data;
166 shash_add(&db->tables, node->name, ovsdb_table_create(ts));
173 ovsdb_destroy(struct ovsdb *db)
176 struct shash_node *node;
178 /* Remove all the replicas. */
179 while (!list_is_empty(&db->replicas)) {
180 struct ovsdb_replica *r
181 = CONTAINER_OF(list_pop_back(&db->replicas),
182 struct ovsdb_replica, node);
183 ovsdb_remove_replica(db, r);
186 /* Delete all the tables. This also deletes their schemas. */
187 SHASH_FOR_EACH (node, &db->tables) {
188 struct ovsdb_table *table = node->data;
189 ovsdb_table_destroy(table);
191 shash_destroy(&db->tables);
193 /* The schemas, but not the table that points to them, were deleted in
194 * the previous step, so we need to clear out the table. We can't
195 * destroy the table, because ovsdb_schema_destroy() will do that. */
196 shash_clear(&db->schema->tables);
198 ovsdb_schema_destroy(db->schema);
204 ovsdb_get_table(const struct ovsdb *db, const char *name)
206 return shash_find_data(&db->tables, name);
210 ovsdb_replica_init(struct ovsdb_replica *r,
211 const struct ovsdb_replica_class *class)
217 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
219 list_push_back(&db->replicas, &r->node);
223 ovsdb_remove_replica(struct ovsdb *db UNUSED, struct ovsdb_replica *r)
225 list_remove(&r->node);
226 (r->class->destroy)(r);