ovsdb: Add new "mutation" operation to transactions.
[sliver-openvswitch.git] / ovsdb / execution.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 <assert.h>
19 #include <limits.h>
20
21 #include "column.h"
22 #include "condition.h"
23 #include "file.h"
24 #include "json.h"
25 #include "mutation.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
29 #include "ovsdb.h"
30 #include "query.h"
31 #include "row.h"
32 #include "table.h"
33 #include "timeval.h"
34 #include "transaction.h"
35
36 struct ovsdb_execution {
37     struct ovsdb *db;
38     struct ovsdb_txn *txn;
39     struct ovsdb_symbol_table *symtab;
40     bool durable;
41
42     /* Triggers. */
43     long long int elapsed_msec;
44     long long int timeout_msec;
45 };
46
47 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
48                                                      struct ovsdb_parser *,
49                                                      struct json *result);
50
51 static ovsdb_operation_executor ovsdb_execute_insert;
52 static ovsdb_operation_executor ovsdb_execute_select;
53 static ovsdb_operation_executor ovsdb_execute_update;
54 static ovsdb_operation_executor ovsdb_execute_mutate;
55 static ovsdb_operation_executor ovsdb_execute_delete;
56 static ovsdb_operation_executor ovsdb_execute_wait;
57 static ovsdb_operation_executor ovsdb_execute_commit;
58 static ovsdb_operation_executor ovsdb_execute_abort;
59 static ovsdb_operation_executor ovsdb_execute_declare;
60
61 static ovsdb_operation_executor *
62 lookup_executor(const char *name)
63 {
64     struct ovsdb_operation {
65         const char *name;
66         ovsdb_operation_executor *executor;
67     };
68
69     static const struct ovsdb_operation operations[] = {
70         { "insert", ovsdb_execute_insert },
71         { "select", ovsdb_execute_select },
72         { "update", ovsdb_execute_update },
73         { "mutate", ovsdb_execute_mutate },
74         { "delete", ovsdb_execute_delete },
75         { "wait", ovsdb_execute_wait },
76         { "commit", ovsdb_execute_commit },
77         { "abort", ovsdb_execute_abort },
78         { "declare", ovsdb_execute_declare },
79     };
80
81     size_t i;
82
83     for (i = 0; i < ARRAY_SIZE(operations); i++) {
84         const struct ovsdb_operation *c = &operations[i];
85         if (!strcmp(c->name, name)) {
86             return c->executor;
87         }
88     }
89     return NULL;
90 }
91
92 struct json *
93 ovsdb_execute(struct ovsdb *db, const struct json *params,
94               long long int elapsed_msec, long long int *timeout_msec)
95 {
96     struct ovsdb_execution x;
97     struct ovsdb_error *error;
98     struct json *results;
99     size_t n_operations;
100     size_t i;
101
102     if (params->type != JSON_ARRAY) {
103         struct ovsdb_error *error;
104
105         error = ovsdb_syntax_error(params, NULL, "array expected");
106         results = ovsdb_error_to_json(error);
107         ovsdb_error_destroy(error);
108         return results;
109     }
110
111     x.db = db;
112     x.txn = ovsdb_txn_create(db);
113     x.symtab = ovsdb_symbol_table_create();
114     x.durable = false;
115     x.elapsed_msec = elapsed_msec;
116     x.timeout_msec = LLONG_MAX;
117     results = NULL;
118
119     results = json_array_create_empty();
120     n_operations = params->u.array.n;
121     error = NULL;
122     for (i = 0; i < n_operations; i++) {
123         struct json *operation = params->u.array.elems[i];
124         struct ovsdb_error *parse_error;
125         struct ovsdb_parser parser;
126         struct json *result;
127         const struct json *op;
128
129         /* Parse and execute operation. */
130         ovsdb_parser_init(&parser, operation,
131                           "ovsdb operation %zu of %zu", i + 1, n_operations);
132         op = ovsdb_parser_member(&parser, "op", OP_ID);
133         result = json_object_create();
134         if (op) {
135             const char *op_name = json_string(op);
136             ovsdb_operation_executor *executor = lookup_executor(op_name);
137             if (executor) {
138                 error = executor(&x, &parser, result);
139             } else {
140                 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
141                                          op_name);
142             }
143         } else {
144             assert(ovsdb_parser_has_error(&parser));
145         }
146
147         /* A parse error overrides any other error.
148          * An error overrides any other result. */
149         parse_error = ovsdb_parser_finish(&parser);
150         if (parse_error) {
151             ovsdb_error_destroy(error);
152             error = parse_error;
153         }
154         if (error) {
155             json_destroy(result);
156             result = ovsdb_error_to_json(error);
157         }
158         if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
159             && timeout_msec) {
160             ovsdb_txn_abort(x.txn);
161             *timeout_msec = x.timeout_msec;
162             ovsdb_error_destroy(error);
163             json_destroy(results);
164             return NULL;
165         }
166
167         /* Add result to array. */
168         json_array_add(results, result);
169         if (error) {
170             break;
171         }
172     }
173
174     if (!error) {
175         error = ovsdb_txn_commit(x.txn, x.durable);
176         if (error) {
177             json_array_add(results, ovsdb_error_to_json(error));
178         }
179     } else {
180         ovsdb_txn_abort(x.txn);
181     }
182
183     while (json_array(results)->n < n_operations) {
184         json_array_add(results, json_null_create());
185     }
186
187     ovsdb_error_destroy(error);
188     ovsdb_symbol_table_destroy(x.symtab);
189
190     return results;
191 }
192
193 struct ovsdb_error *
194 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
195                      struct json *result UNUSED)
196 {
197     const struct json *durable;
198
199     durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
200     if (durable && json_boolean(durable)) {
201         x->durable = true;
202     }
203     return NULL;
204 }
205
206 static struct ovsdb_error *
207 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
208                     struct ovsdb_parser *parser UNUSED,
209                     struct json *result UNUSED)
210 {
211     return ovsdb_error("aborted", "aborted by request");
212 }
213
214 static struct ovsdb_table *
215 parse_table(struct ovsdb_execution *x,
216             struct ovsdb_parser *parser, const char *member)
217 {
218     struct ovsdb_table *table;
219     const char *table_name;
220     const struct json *json;
221
222     json = ovsdb_parser_member(parser, member, OP_ID);
223     if (!json) {
224         return NULL;
225     }
226     table_name = json_string(json);
227
228     table = shash_find_data(&x->db->tables, table_name);
229     if (!table) {
230         ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
231     }
232     return table;
233 }
234
235 static WARN_UNUSED_RESULT struct ovsdb_error *
236 parse_row(struct ovsdb_parser *parser, const char *member,
237           const struct ovsdb_table *table,
238           const struct ovsdb_symbol_table *symtab,
239           struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
240 {
241     struct ovsdb_error *error;
242     const struct json *json;
243     struct ovsdb_row *row;
244
245     *rowp = NULL;
246
247     if (!table) {
248         return OVSDB_BUG("null table");
249     }
250     json = ovsdb_parser_member(parser, member, OP_OBJECT);
251     if (!json) {
252         return OVSDB_BUG("null row member");
253     }
254
255     row = ovsdb_row_create(table);
256     error = ovsdb_row_from_json(row, json, symtab, columns);
257     if (error) {
258         ovsdb_row_destroy(row);
259         return error;
260     } else {
261         *rowp = row;
262         return NULL;
263     }
264 }
265
266 struct ovsdb_error *
267 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
268                      struct json *result)
269 {
270     struct ovsdb_table *table;
271     struct ovsdb_row *row = NULL;
272     const struct json *uuid_name;
273     struct ovsdb_error *error;
274     struct uuid row_uuid;
275
276     table = parse_table(x, parser, "table");
277     uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
278     error = ovsdb_parser_get_error(parser);
279
280     if (uuid_name) {
281         struct ovsdb_symbol *symbol;
282
283         symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name));
284         if (symbol) {
285             if (symbol->used) {
286                 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
287                                           "This \"uuid-name\" appeared on an "
288                                           "earlier \"insert\" operation.");
289             }
290             row_uuid = symbol->uuid;
291             symbol->used = true;
292         } else {
293             uuid_generate(&row_uuid);
294             ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
295                                    &row_uuid, true);
296         }
297     } else {
298         uuid_generate(&row_uuid);
299     }
300
301     if (!error) {
302         error = parse_row(parser, "row", table, x->symtab, &row, NULL);
303     }
304     if (!error) {
305         *ovsdb_row_get_uuid_rw(row) = row_uuid;
306         ovsdb_txn_row_insert(x->txn, row);
307         json_object_put(result, "uuid",
308                         ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
309                                             &ovsdb_type_uuid));
310         row = NULL;
311     }
312     return error;
313 }
314
315 struct ovsdb_error *
316 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
317                      struct json *result)
318 {
319     struct ovsdb_table *table;
320     const struct json *where, *columns_json, *sort_json;
321     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
322     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
323     struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
324     struct ovsdb_error *error;
325
326     table = parse_table(x, parser, "table");
327     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
328     columns_json = ovsdb_parser_member(parser, "columns",
329                                        OP_ARRAY | OP_OPTIONAL);
330     sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
331
332     error = ovsdb_parser_get_error(parser);
333     if (!error) {
334         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
335                                           &condition);
336     }
337     if (!error) {
338         error = ovsdb_column_set_from_json(columns_json, table, &columns);
339     }
340     if (!error) {
341         error = ovsdb_column_set_from_json(sort_json, table, &sort);
342     }
343     if (!error) {
344         struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
345
346         ovsdb_query_distinct(table, &condition, &columns, &rows);
347         ovsdb_row_set_sort(&rows, &sort);
348         json_object_put(result, "rows",
349                         ovsdb_row_set_to_json(&rows, &columns));
350
351         ovsdb_row_set_destroy(&rows);
352     }
353
354     ovsdb_column_set_destroy(&columns);
355     ovsdb_column_set_destroy(&sort);
356     ovsdb_condition_destroy(&condition);
357
358     return error;
359 }
360
361 struct update_row_cbdata {
362     size_t n_matches;
363     struct ovsdb_txn *txn;
364     const struct ovsdb_row *row;
365     const struct ovsdb_column_set *columns;
366 };
367
368 static bool
369 update_row_cb(const struct ovsdb_row *row, void *ur_)
370 {
371     struct update_row_cbdata *ur = ur_;
372
373     ur->n_matches++;
374     if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
375         ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
376                                  ur->row, ur->columns);
377     }
378
379     return true;
380 }
381
382 struct ovsdb_error *
383 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
384                      struct json *result)
385 {
386     struct ovsdb_table *table;
387     const struct json *where;
388     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
389     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
390     struct ovsdb_row *row = NULL;
391     struct update_row_cbdata ur;
392     struct ovsdb_error *error;
393
394     table = parse_table(x, parser, "table");
395     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
396     error = ovsdb_parser_get_error(parser);
397     if (!error) {
398         error = parse_row(parser, "row", table, x->symtab, &row, &columns);
399     }
400     if (!error) {
401         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
402                                           &condition);
403     }
404     if (!error) {
405         ur.n_matches = 0;
406         ur.txn = x->txn;
407         ur.row = row;
408         ur.columns = &columns;
409         ovsdb_query(table, &condition, update_row_cb, &ur);
410         json_object_put(result, "count", json_integer_create(ur.n_matches));
411     }
412
413     ovsdb_row_destroy(row);
414     ovsdb_column_set_destroy(&columns);
415     ovsdb_condition_destroy(&condition);
416
417     return error;
418 }
419
420 struct mutate_row_cbdata {
421     size_t n_matches;
422     struct ovsdb_txn *txn;
423     const struct ovsdb_mutation_set *mutations;
424 };
425
426 static bool
427 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
428 {
429     struct mutate_row_cbdata *mr = mr_;
430
431     mr->n_matches++;
432     ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
433                                mr->mutations);
434
435     return true;
436 }
437
438 struct ovsdb_error *
439 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
440                      struct json *result)
441 {
442     struct ovsdb_table *table;
443     const struct json *where;
444     const struct json *mutations_json;
445     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
446     struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
447     struct ovsdb_row *row = NULL;
448     struct mutate_row_cbdata mr;
449     struct ovsdb_error *error;
450
451     table = parse_table(x, parser, "table");
452     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
453     mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
454     error = ovsdb_parser_get_error(parser);
455     if (!error) {
456         error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
457                                              x->symtab, &mutations);
458     }
459     if (!error) {
460         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
461                                           &condition);
462     }
463     if (!error) {
464         mr.n_matches = 0;
465         mr.txn = x->txn;
466         mr.mutations = &mutations;
467         ovsdb_query(table, &condition, mutate_row_cb, &mr);
468         json_object_put(result, "count", json_integer_create(mr.n_matches));
469     }
470
471     ovsdb_row_destroy(row);
472     ovsdb_mutation_set_destroy(&mutations);
473     ovsdb_condition_destroy(&condition);
474
475     return error;
476 }
477
478 struct delete_row_cbdata {
479     size_t n_matches;
480     const struct ovsdb_table *table;
481     struct ovsdb_txn *txn;
482 };
483
484 static bool
485 delete_row_cb(const struct ovsdb_row *row, void *dr_)
486 {
487     struct delete_row_cbdata *dr = dr_;
488
489     dr->n_matches++;
490     ovsdb_txn_row_delete(dr->txn, row);
491
492     return true;
493 }
494
495 struct ovsdb_error *
496 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
497                      struct json *result)
498 {
499     struct ovsdb_table *table;
500     const struct json *where;
501     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
502     struct ovsdb_error *error;
503
504     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
505     table = parse_table(x, parser, "table");
506     error = ovsdb_parser_get_error(parser);
507     if (!error) {
508         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
509                                           &condition);
510     }
511     if (!error) {
512         struct delete_row_cbdata dr;
513
514         dr.n_matches = 0;
515         dr.table = table;
516         dr.txn = x->txn;
517         ovsdb_query(table, &condition, delete_row_cb, &dr);
518
519         json_object_put(result, "count", json_integer_create(dr.n_matches));
520     }
521
522     ovsdb_condition_destroy(&condition);
523
524     return error;
525 }
526
527 struct wait_auxdata {
528     struct ovsdb_row_hash *actual;
529     struct ovsdb_row_hash *expected;
530     bool *equal;
531 };
532
533 static bool
534 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
535 {
536     struct wait_auxdata *aux = aux_;
537
538     if (ovsdb_row_hash_contains(aux->expected, row)) {
539         ovsdb_row_hash_insert(aux->actual, row);
540         return true;
541     } else {
542         /* The query row isn't in the expected result set, so the actual and
543          * expected results sets definitely differ and we can short-circuit the
544          * rest of the query. */
545         *aux->equal = false;
546         return false;
547     }
548 }
549
550 static struct ovsdb_error *
551 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
552                    struct json *result UNUSED)
553 {
554     struct ovsdb_table *table;
555     const struct json *timeout, *where, *columns_json, *until, *rows;
556     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
557     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
558     struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
559     struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
560     struct ovsdb_error *error;
561     struct wait_auxdata aux;
562     long long int timeout_msec = 0;
563     size_t i;
564
565     timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
566     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
567     columns_json = ovsdb_parser_member(parser, "columns",
568                                        OP_ARRAY | OP_OPTIONAL);
569     until = ovsdb_parser_member(parser, "until", OP_STRING);
570     rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
571     table = parse_table(x, parser, "table");
572     error = ovsdb_parser_get_error(parser);
573     if (!error) {
574         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
575                                           &condition);
576     }
577     if (!error) {
578         error = ovsdb_column_set_from_json(columns_json, table, &columns);
579     }
580     if (!error) {
581         if (timeout) {
582             timeout_msec = MIN(LLONG_MAX, json_real(timeout));
583             if (timeout_msec < 0) {
584                 error = ovsdb_syntax_error(timeout, NULL,
585                                            "timeout must be nonnegative");
586             } else if (timeout_msec < x->timeout_msec) {
587                 x->timeout_msec = timeout_msec;
588             }
589         } else {
590             timeout_msec = LLONG_MAX;
591         }
592         if (strcmp(json_string(until), "==")
593             && strcmp(json_string(until), "!=")) {
594             error = ovsdb_syntax_error(until, NULL,
595                                        "\"until\" must be \"==\" or \"!=\"");
596         }
597     }
598     if (!error) {
599         /* Parse "rows" into 'expected'. */
600         ovsdb_row_hash_init(&expected, &columns);
601         for (i = 0; i < rows->u.array.n; i++) {
602             struct ovsdb_error *error;
603             struct ovsdb_row *row;
604
605             row = ovsdb_row_create(table);
606             error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
607                                         NULL);
608             if (error) {
609                 break;
610             }
611
612             if (!ovsdb_row_hash_insert(&expected, row)) {
613                 /* XXX Perhaps we should abort with an error or log a
614                  * warning. */
615                 ovsdb_row_destroy(row);
616             }
617         }
618     }
619     if (!error) {
620         /* Execute query. */
621         bool equal = true;
622         ovsdb_row_hash_init(&actual, &columns);
623         aux.actual = &actual;
624         aux.expected = &expected;
625         aux.equal = &equal;
626         ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
627         if (equal) {
628             /* We know that every row in 'actual' is also in 'expected'.  We
629              * also know that all of the rows in 'actual' are distinct and that
630              * all of the rows in 'expected' are distinct.  Therefore, if
631              * 'actual' and 'expected' have the same number of rows, then they
632              * have the same content. */
633             size_t n_actual = ovsdb_row_hash_count(&actual);
634             size_t n_expected = ovsdb_row_hash_count(&expected);
635             equal = n_actual == n_expected;
636         }
637         if (!strcmp(json_string(until), "==") != equal) {
638             if (timeout && x->elapsed_msec >= timeout_msec) {
639                 if (x->elapsed_msec) {
640                     error = ovsdb_error("timed out",
641                                         "\"wait\" timed out after %lld ms",
642                                         x->elapsed_msec);
643                 } else {
644                     error = ovsdb_error("timed out", "\"wait\" timed out");
645                 }
646             } else {
647                 /* ovsdb_execute() will change this, if triggers really are
648                  * supported. */
649                 error = ovsdb_error("not supported", "triggers not supported");
650             }
651         }
652     }
653
654
655     ovsdb_row_hash_destroy(&expected, true);
656     ovsdb_row_hash_destroy(&actual, false);
657     ovsdb_column_set_destroy(&columns);
658     ovsdb_condition_destroy(&condition);
659
660     return error;
661 }
662
663 static struct ovsdb_error *
664 ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser,
665                       struct json *result)
666 {
667     const struct json *uuid_name;
668     struct uuid uuid;
669
670     uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID);
671     if (!uuid_name) {
672         return NULL;
673     }
674
675     if (ovsdb_symbol_table_get(x->symtab, json_string(uuid_name))) {
676         return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
677                                   "This \"uuid-name\" appeared on an "
678                                   "earlier \"declare\" or \"insert\" "
679                                   "operation.");
680     }
681
682     uuid_generate(&uuid);
683     ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &uuid, false);
684     json_object_put(result, "uuid", json_string_create_nocopy(
685                         xasprintf(UUID_FMT, UUID_ARGS(&uuid))));
686     return NULL;
687 }