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.
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
27 #include "transaction.h"
29 #define THIS_MODULE VLM_ovsdb
33 ovsdb_schema_create(const char *name, const char *comment)
35 struct ovsdb_schema *schema;
37 schema = xzalloc(sizeof *schema);
38 schema->name = xstrdup(name);
39 schema->comment = comment ? xstrdup(comment) : NULL;
40 shash_init(&schema->tables);
46 ovsdb_schema_destroy(struct ovsdb_schema *schema)
48 struct shash_node *node;
50 SHASH_FOR_EACH (node, &schema->tables) {
51 ovsdb_table_schema_destroy(node->data);
53 shash_destroy(&schema->tables);
54 free(schema->comment);
60 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
62 struct ovsdb_schema *schema;
63 struct ovsdb_error *error;
67 json = json_from_file(file_name);
68 if (json->type == JSON_STRING) {
69 error = ovsdb_error("failed to read schema",
70 "\"%s\" could not be read as JSON (%s)",
71 file_name, json_string(json));
76 error = ovsdb_schema_from_json(json, &schema);
79 return ovsdb_wrap_error(error,
80 "failed to parse \"%s\" as ovsdb schema",
89 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
91 struct ovsdb_schema *schema;
92 const struct json *name, *comment, *tables;
93 struct ovsdb_error *error;
94 struct shash_node *node;
95 struct ovsdb_parser parser;
99 ovsdb_parser_init(&parser, json, "database schema");
100 name = ovsdb_parser_member(&parser, "name", OP_ID);
101 comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
102 tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
103 error = ovsdb_parser_finish(&parser);
108 schema = ovsdb_schema_create(json_string(name),
109 comment ? json_string(comment) : NULL);
110 SHASH_FOR_EACH (node, json_object(tables)) {
111 struct ovsdb_table_schema *table;
113 if (node->name[0] == '_') {
114 error = ovsdb_syntax_error(json, NULL, "names beginning with "
115 "\"_\" are reserved");
117 error = ovsdb_table_schema_from_json(node->data, node->name,
121 ovsdb_schema_destroy(schema);
125 shash_add(&schema->tables, table->name, table);
132 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
134 struct json *json, *tables;
135 struct shash_node *node;
137 json = json_object_create();
138 json_object_put_string(json, "name", schema->name);
139 if (schema->comment) {
140 json_object_put_string(json, "comment", schema->comment);
143 tables = json_object_create();
145 SHASH_FOR_EACH (node, &schema->tables) {
146 struct ovsdb_table_schema *table = node->data;
147 json_object_put(tables, table->name,
148 ovsdb_table_schema_to_json(table));
150 json_object_put(json, "tables", tables);
156 ovsdb_create(struct ovsdb_file *file, struct ovsdb_schema *schema)
158 struct shash_node *node;
161 db = xmalloc(sizeof *db);
164 list_init(&db->triggers);
165 db->run_triggers = false;
167 shash_init(&db->tables);
168 SHASH_FOR_EACH (node, &schema->tables) {
169 struct ovsdb_table_schema *ts = node->data;
170 shash_add(&db->tables, node->name, ovsdb_table_create(ts));
177 ovsdb_open(const char *file_name, bool read_only, struct ovsdb **dbp)
179 struct ovsdb_schema *schema;
180 struct ovsdb_error *error;
181 struct ovsdb_file *file;
185 error = ovsdb_file_open(file_name, read_only ? O_RDONLY : O_RDWR, &file);
190 error = ovsdb_file_read(file, &json);
194 return ovsdb_io_error(EOF, "%s: database file contains no schema",
198 error = ovsdb_schema_from_json(json, &schema);
201 return ovsdb_wrap_error(error,
202 "failed to parse \"%s\" as ovsdb schema",
207 db = ovsdb_create(read_only ? file : NULL, schema);
208 while ((error = ovsdb_file_read(file, &json)) == NULL && json) {
209 struct ovsdb_txn *txn;
211 error = ovsdb_txn_from_json(db, json, &txn);
217 ovsdb_txn_commit(txn);
220 char *msg = ovsdb_error_to_string(error);
221 VLOG_WARN("%s", msg);
224 ovsdb_error_destroy(error);
228 ovsdb_file_close(file);
236 ovsdb_destroy(struct ovsdb *db)
239 struct shash_node *node;
241 /* Delete all the tables. This also deletes their schemas. */
242 SHASH_FOR_EACH (node, &db->tables) {
243 struct ovsdb_table *table = node->data;
244 ovsdb_table_destroy(table);
246 shash_destroy(&db->tables);
248 /* Clear the schema's hash of table schemas. The schemas, but not the
249 * table that points to them, were deleted in the previous step. */
250 shash_destroy(&db->schema->tables);
252 ovsdb_schema_destroy(db->schema);
253 ovsdb_file_close(db->file);
259 ovsdb_get_table(const struct ovsdb *db, const char *name)
261 return shash_find_data(&db->tables, name);