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