ovsdb: Add replication support and refactor files in terms of replication.
[cascardo/ovs.git] / ovsdb / ovsdb.c
1 /* Copyright (c) 2009 Nicira Networks
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include "json.h"
21 #include "ovsdb-error.h"
22 #include "ovsdb-parser.h"
23 #include "table.h"
24 #include "transaction.h"
25
26 struct ovsdb_schema *
27 ovsdb_schema_create(const char *name, const char *comment)
28 {
29     struct ovsdb_schema *schema;
30
31     schema = xzalloc(sizeof *schema);
32     schema->name = xstrdup(name);
33     schema->comment = comment ? xstrdup(comment) : NULL;
34     shash_init(&schema->tables);
35
36     return schema;
37 }
38
39 void
40 ovsdb_schema_destroy(struct ovsdb_schema *schema)
41 {
42     struct shash_node *node;
43
44     SHASH_FOR_EACH (node, &schema->tables) {
45         ovsdb_table_schema_destroy(node->data);
46     }
47     shash_destroy(&schema->tables);
48     free(schema->comment);
49     free(schema->name);
50     free(schema);
51 }
52
53 struct ovsdb_error *
54 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
55 {
56     struct ovsdb_schema *schema;
57     struct ovsdb_error *error;
58     struct json *json;
59
60     *schemap = NULL;
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));
66         json_destroy(json);
67         return error;
68     }
69
70     error = ovsdb_schema_from_json(json, &schema);
71     if (error) {
72         json_destroy(json);
73         return ovsdb_wrap_error(error,
74                                 "failed to parse \"%s\" as ovsdb schema",
75                                 file_name);
76     }
77
78     *schemap = schema;
79     return NULL;
80 }
81
82 struct ovsdb_error *
83 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
84 {
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;
90
91     *schemap = NULL;
92
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);
98     if (error) {
99         return error;
100     }
101
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;
106
107         if (node->name[0] == '_') {
108             error = ovsdb_syntax_error(json, NULL, "names beginning with "
109                                        "\"_\" are reserved");
110         } else {
111             error = ovsdb_table_schema_from_json(node->data, node->name,
112                                                  &table);
113         }
114         if (error) {
115             ovsdb_schema_destroy(schema);
116             return error;
117         }
118
119         shash_add(&schema->tables, table->name, table);
120     }
121     *schemap = schema;
122     return 0;
123 }
124
125 struct json *
126 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
127 {
128     struct json *json, *tables;
129     struct shash_node *node;
130
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);
135     }
136
137     tables = json_object_create();
138
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));
143     }
144     json_object_put(json, "tables", tables);
145
146     return json;
147 }
148 \f
149 struct ovsdb *
150 ovsdb_create(struct ovsdb_schema *schema)
151 {
152     struct shash_node *node;
153     struct ovsdb *db;
154
155     db = xmalloc(sizeof *db);
156     db->schema = schema;
157     list_init(&db->replicas);
158     list_init(&db->triggers);
159     db->run_triggers = false;
160
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));
165     }
166
167     return db;
168 }
169
170 void
171 ovsdb_destroy(struct ovsdb *db)
172 {
173     if (db) {
174         struct shash_node *node;
175
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);
182         }
183
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);
188         }
189         shash_destroy(&db->tables);
190
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);
195
196         ovsdb_schema_destroy(db->schema);
197         free(db);
198     }
199 }
200
201 struct ovsdb_table *
202 ovsdb_get_table(const struct ovsdb *db, const char *name)
203 {
204     return shash_find_data(&db->tables, name);
205 }
206 \f
207 void
208 ovsdb_replica_init(struct ovsdb_replica *r,
209                    const struct ovsdb_replica_class *class)
210 {
211     r->class = class;
212 }
213
214 void
215 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
216 {
217     list_push_back(&db->replicas, &r->node);
218 }
219
220 void
221 ovsdb_remove_replica(struct ovsdb *db UNUSED, struct ovsdb_replica *r)
222 {
223     list_remove(&r->node);
224     (r->class->destroy)(r);
225 }