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