Merge "master" branch into "db".
[sliver-openvswitch.git] / ovsdb / file.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 "file.h"
19
20 #include <assert.h>
21 #include <fcntl.h>
22
23 #include "column.h"
24 #include "log.h"
25 #include "json.h"
26 #include "ovsdb.h"
27 #include "ovsdb-error.h"
28 #include "row.h"
29 #include "table.h"
30 #include "transaction.h"
31 #include "uuid.h"
32 #include "util.h"
33
34 #define THIS_MODULE VLM_ovsdb_file
35 #include "vlog.h"
36
37 static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
38                                                     const struct json *,
39                                                     struct ovsdb_txn **);
40 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
41
42 struct ovsdb_error *
43 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
44 {
45     struct ovsdb_schema *schema;
46     struct ovsdb_error *error;
47     struct ovsdb_log *log;
48     struct json *json;
49     struct ovsdb *db;
50
51     error = ovsdb_log_open(file_name, read_only ? O_RDONLY : O_RDWR, &log);
52     if (error) {
53         return error;
54     }
55
56     error = ovsdb_log_read(log, &json);
57     if (error) {
58         return error;
59     } else if (!json) {
60         return ovsdb_io_error(EOF, "%s: database file contains no schema",
61                               file_name);
62     }
63
64     error = ovsdb_schema_from_json(json, &schema);
65     if (error) {
66         json_destroy(json);
67         return ovsdb_wrap_error(error,
68                                 "failed to parse \"%s\" as ovsdb schema",
69                                 file_name);
70     }
71     json_destroy(json);
72
73     db = ovsdb_create(schema);
74     while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
75         struct ovsdb_txn *txn;
76
77         error = ovsdb_file_txn_from_json(db, json, &txn);
78         json_destroy(json);
79         if (error) {
80             break;
81         }
82
83         ovsdb_txn_commit(txn, false);
84     }
85     if (error) {
86         char *msg = ovsdb_error_to_string(error);
87         VLOG_WARN("%s", msg);
88         free(msg);
89
90         ovsdb_error_destroy(error);
91     }
92
93     if (!read_only) {
94         ovsdb_file_replica_create(db, log);
95     } else {
96         ovsdb_log_close(log);
97     }
98
99     *dbp = db;
100     return NULL;
101 }
102
103 static struct ovsdb_error *
104 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
105                              const struct uuid *row_uuid, struct json *json)
106 {
107     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
108     if (json->type == JSON_NULL) {
109         if (!row) {
110             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
111                                       "row "UUID_FMT" that does not exist",
112                                       UUID_ARGS(row_uuid));
113         }
114         ovsdb_txn_row_delete(txn, row);
115         return NULL;
116     } else if (row) {
117         return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
118                                    json, NULL, NULL);
119     } else {
120         struct ovsdb_error *error;
121         struct ovsdb_row *new;
122
123         new = ovsdb_row_create(table);
124         *ovsdb_row_get_uuid_rw(new) = *row_uuid;
125         error = ovsdb_row_from_json(new, json, NULL, NULL);
126         if (error) {
127             ovsdb_row_destroy(new);
128         }
129
130         ovsdb_txn_row_insert(txn, new);
131
132         return error;
133     }
134 }
135
136 static struct ovsdb_error *
137 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
138                                struct ovsdb_table *table, struct json *json)
139 {
140     struct shash_node *node;
141
142     if (json->type != JSON_OBJECT) {
143         return ovsdb_syntax_error(json, NULL, "object expected");
144     }
145
146     SHASH_FOR_EACH (node, json->u.object) {
147         const char *uuid_string = node->name;
148         struct json *txn_row_json = node->data;
149         struct ovsdb_error *error;
150         struct uuid row_uuid;
151
152         if (!uuid_from_string(&row_uuid, uuid_string)) {
153             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
154                                       uuid_string);
155         }
156
157         error = ovsdb_file_txn_row_from_json(txn, table, &row_uuid,
158                                              txn_row_json);
159         if (error) {
160             return error;
161         }
162     }
163
164     return NULL;
165 }
166
167 static struct ovsdb_error *
168 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
169                          struct ovsdb_txn **txnp)
170 {
171     struct ovsdb_error *error;
172     struct shash_node *node;
173     struct ovsdb_txn *txn;
174
175     *txnp = NULL;
176     if (json->type != JSON_OBJECT) {
177         return ovsdb_syntax_error(json, NULL, "object expected");
178     }
179
180     txn = ovsdb_txn_create(db);
181     SHASH_FOR_EACH (node, json->u.object) {
182         const char *table_name = node->name;
183         struct json *txn_table_json = node->data;
184         struct ovsdb_table *table;
185
186         table = shash_find_data(&db->tables, table_name);
187         if (!table) {
188             error = ovsdb_syntax_error(json, "unknown table",
189                                        "No table named %s.", table_name);
190             goto error;
191         }
192
193         error = ovsdb_file_txn_table_from_json(txn, table, txn_table_json);
194         if (error) {
195             goto error;
196         }
197     }
198     *txnp = txn;
199     return NULL;
200
201 error:
202     ovsdb_txn_abort(txn);
203     return error;
204 }
205 \f
206 /* Replica implementation. */
207
208 struct ovsdb_file_replica {
209     struct ovsdb_replica replica;
210     struct ovsdb_log *log;
211 };
212
213 static const struct ovsdb_replica_class ovsdb_file_replica_class;
214
215 static void
216 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
217 {
218     struct ovsdb_file_replica *r = xmalloc(sizeof *r);
219     ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
220     r->log = log;
221     ovsdb_add_replica(db, &r->replica);
222
223 }
224
225 static struct ovsdb_file_replica *
226 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
227 {
228     assert(replica->class == &ovsdb_file_replica_class);
229     return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
230 }
231
232 struct ovsdb_file_replica_aux {
233     struct json *json;          /* JSON for the whole transaction. */
234     struct json *table_json;    /* JSON for 'table''s transaction. */
235     struct ovsdb_table *table;  /* Table described in 'table_json'.  */
236 };
237
238 static bool
239 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
240                              const struct ovsdb_row *new,
241                              void *aux_)
242 {
243     struct ovsdb_file_replica_aux *aux = aux_;
244     struct json *row;
245
246     if (!new) {
247         row = json_null_create();
248     } else {
249         struct shash_node *node;
250
251         row = NULL;
252         SHASH_FOR_EACH (node, &new->table->schema->columns) {
253             const struct ovsdb_column *column = node->data;
254             const struct ovsdb_type *type = &column->type;
255             unsigned int idx = column->index;
256
257             if (idx != OVSDB_COL_UUID && column->persistent
258                 && (!old || !ovsdb_datum_equals(&old->fields[idx],
259                                                 &new->fields[idx], type)))
260             {
261                 if (!row) {
262                     row = json_object_create();
263                 }
264                 json_object_put(row, column->name,
265                                 ovsdb_datum_to_json(&new->fields[idx], type));
266             }
267         }
268     }
269
270     if (row) {
271         struct ovsdb_table *table = new ? new->table : old->table;
272         char uuid[UUID_LEN + 1];
273
274         if (table != aux->table) {
275             /* Create JSON object for transaction overall. */
276             if (!aux->json) {
277                 aux->json = json_object_create();
278             }
279
280             /* Create JSON object for transaction on this table. */
281             aux->table_json = json_object_create();
282             aux->table = table;
283             json_object_put(aux->json, table->schema->name, aux->table_json);
284         }
285
286         /* Add row to transaction for this table. */
287         snprintf(uuid, sizeof uuid,
288                  UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
289         json_object_put(aux->table_json, uuid, row);
290     }
291
292     return true;
293 }
294
295 static struct ovsdb_error *
296 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
297                           const struct ovsdb_txn *txn, bool durable)
298 {
299     struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
300     struct ovsdb_file_replica_aux aux;
301     struct ovsdb_error *error;
302
303     aux.json = NULL;
304     aux.table_json = NULL;
305     aux.table = NULL;
306     ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &aux);
307
308     if (!aux.json) {
309         /* Nothing to commit. */
310         return NULL;
311     }
312
313     error = ovsdb_log_write(r->log, aux.json);
314     json_destroy(aux.json);
315     if (error) {
316         return ovsdb_wrap_error(error, "writing transaction failed");
317     }
318
319     if (durable) {
320         error = ovsdb_log_commit(r->log);
321         if (error) {
322             return ovsdb_wrap_error(error, "committing transaction failed");
323         }
324     }
325
326     return NULL;
327 }
328
329 static void
330 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
331 {
332     struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
333
334     ovsdb_log_close(r->log);
335     free(r);
336 }
337
338 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
339     ovsdb_file_replica_commit,
340     ovsdb_file_replica_destroy
341 };