ovsdb: Get rid of "declare" operation.
[cascardo/ovs.git] / ovsdb / execution.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 <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_comment;
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         { "comment", ovsdb_execute_comment },
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
163             json_destroy(result);
164             json_destroy(results);
165             results = NULL;
166             goto exit;
167         }
168
169         /* Add result to array. */
170         json_array_add(results, result);
171         if (error) {
172             break;
173         }
174     }
175
176     if (!error) {
177         error = ovsdb_txn_commit(x.txn, x.durable);
178         if (error) {
179             json_array_add(results, ovsdb_error_to_json(error));
180         }
181     } else {
182         ovsdb_txn_abort(x.txn);
183     }
184
185     while (json_array(results)->n < n_operations) {
186         json_array_add(results, json_null_create());
187     }
188
189 exit:
190     ovsdb_error_destroy(error);
191     ovsdb_symbol_table_destroy(x.symtab);
192
193     return results;
194 }
195
196 struct ovsdb_error *
197 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
198                      struct json *result UNUSED)
199 {
200     const struct json *durable;
201
202     durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
203     if (durable && json_boolean(durable)) {
204         x->durable = true;
205     }
206     return NULL;
207 }
208
209 static struct ovsdb_error *
210 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
211                     struct ovsdb_parser *parser UNUSED,
212                     struct json *result UNUSED)
213 {
214     return ovsdb_error("aborted", "aborted by request");
215 }
216
217 static struct ovsdb_table *
218 parse_table(struct ovsdb_execution *x,
219             struct ovsdb_parser *parser, const char *member)
220 {
221     struct ovsdb_table *table;
222     const char *table_name;
223     const struct json *json;
224
225     json = ovsdb_parser_member(parser, member, OP_ID);
226     if (!json) {
227         return NULL;
228     }
229     table_name = json_string(json);
230
231     table = shash_find_data(&x->db->tables, table_name);
232     if (!table) {
233         ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
234     }
235     return table;
236 }
237
238 static WARN_UNUSED_RESULT struct ovsdb_error *
239 parse_row(struct ovsdb_parser *parser, const char *member,
240           const struct ovsdb_table *table,
241           struct ovsdb_symbol_table *symtab,
242           struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
243 {
244     struct ovsdb_error *error;
245     const struct json *json;
246     struct ovsdb_row *row;
247
248     *rowp = NULL;
249
250     if (!table) {
251         return OVSDB_BUG("null table");
252     }
253     json = ovsdb_parser_member(parser, member, OP_OBJECT);
254     if (!json) {
255         return OVSDB_BUG("null row member");
256     }
257
258     row = ovsdb_row_create(table);
259     error = ovsdb_row_from_json(row, json, symtab, columns);
260     if (error) {
261         ovsdb_row_destroy(row);
262         return error;
263     } else {
264         *rowp = row;
265         return NULL;
266     }
267 }
268
269 struct ovsdb_error *
270 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
271                      struct json *result)
272 {
273     struct ovsdb_table *table;
274     struct ovsdb_row *row = NULL;
275     const struct json *uuid_name;
276     struct ovsdb_error *error;
277     struct uuid row_uuid;
278
279     table = parse_table(x, parser, "table");
280     uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
281     error = ovsdb_parser_get_error(parser);
282
283     if (uuid_name) {
284         struct ovsdb_symbol *symbol;
285
286         symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
287         if (symbol->used) {
288             return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
289                                       "This \"uuid-name\" appeared on an "
290                                       "earlier \"insert\" operation.");
291         }
292         row_uuid = symbol->uuid;
293         symbol->used = true;
294     } else {
295         uuid_generate(&row_uuid);
296     }
297
298     if (!error) {
299         error = parse_row(parser, "row", table, x->symtab, &row, NULL);
300     }
301     if (!error) {
302         /* Check constraints for columns not included in "row", in case the
303          * default values do not satisfy the constraints.  We could check only
304          * the columns that have their default values by supplying an
305          * ovsdb_column_set to parse_row() above, but I suspect that this is
306          * cheaper.  */
307         const struct shash_node *node;
308
309         SHASH_FOR_EACH (node, &table->schema->columns) {
310             const struct ovsdb_column *column = node->data;
311             const struct ovsdb_datum *datum = &row->fields[column->index];
312
313             /* If there are 0 keys or pairs, there's nothing to check.
314              * If there is 1, it might be a default value.
315              * If there are more, it can't be a default value, so the value has
316              * already been checked. */
317             if (datum->n == 1) {
318                 error = ovsdb_datum_check_constraints(datum, &column->type);
319                 if (error) {
320                     ovsdb_row_destroy(row);
321                     break;
322                 }
323             }
324         }
325     }
326     if (!error) {
327         *ovsdb_row_get_uuid_rw(row) = row_uuid;
328         ovsdb_txn_row_insert(x->txn, row);
329         json_object_put(result, "uuid",
330                         ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
331                                             &ovsdb_type_uuid));
332     }
333     return error;
334 }
335
336 struct ovsdb_error *
337 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
338                      struct json *result)
339 {
340     struct ovsdb_table *table;
341     const struct json *where, *columns_json, *sort_json;
342     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
343     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
344     struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
345     struct ovsdb_error *error;
346
347     table = parse_table(x, parser, "table");
348     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
349     columns_json = ovsdb_parser_member(parser, "columns",
350                                        OP_ARRAY | OP_OPTIONAL);
351     sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
352
353     error = ovsdb_parser_get_error(parser);
354     if (!error) {
355         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
356                                           &condition);
357     }
358     if (!error) {
359         error = ovsdb_column_set_from_json(columns_json, table, &columns);
360     }
361     if (!error) {
362         error = ovsdb_column_set_from_json(sort_json, table, &sort);
363     }
364     if (!error) {
365         struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
366
367         ovsdb_query_distinct(table, &condition, &columns, &rows);
368         ovsdb_row_set_sort(&rows, &sort);
369         json_object_put(result, "rows",
370                         ovsdb_row_set_to_json(&rows, &columns));
371
372         ovsdb_row_set_destroy(&rows);
373     }
374
375     ovsdb_column_set_destroy(&columns);
376     ovsdb_column_set_destroy(&sort);
377     ovsdb_condition_destroy(&condition);
378
379     return error;
380 }
381
382 struct update_row_cbdata {
383     size_t n_matches;
384     struct ovsdb_txn *txn;
385     const struct ovsdb_row *row;
386     const struct ovsdb_column_set *columns;
387 };
388
389 static bool
390 update_row_cb(const struct ovsdb_row *row, void *ur_)
391 {
392     struct update_row_cbdata *ur = ur_;
393
394     ur->n_matches++;
395     if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
396         ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
397                                  ur->row, ur->columns);
398     }
399
400     return true;
401 }
402
403 struct ovsdb_error *
404 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
405                      struct json *result)
406 {
407     struct ovsdb_table *table;
408     const struct json *where;
409     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
410     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
411     struct ovsdb_row *row = NULL;
412     struct update_row_cbdata ur;
413     struct ovsdb_error *error;
414
415     table = parse_table(x, parser, "table");
416     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
417     error = ovsdb_parser_get_error(parser);
418     if (!error) {
419         error = parse_row(parser, "row", table, x->symtab, &row, &columns);
420     }
421     if (!error) {
422         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
423                                           &condition);
424     }
425     if (!error) {
426         ur.n_matches = 0;
427         ur.txn = x->txn;
428         ur.row = row;
429         ur.columns = &columns;
430         ovsdb_query(table, &condition, update_row_cb, &ur);
431         json_object_put(result, "count", json_integer_create(ur.n_matches));
432     }
433
434     ovsdb_row_destroy(row);
435     ovsdb_column_set_destroy(&columns);
436     ovsdb_condition_destroy(&condition);
437
438     return error;
439 }
440
441 struct mutate_row_cbdata {
442     size_t n_matches;
443     struct ovsdb_txn *txn;
444     const struct ovsdb_mutation_set *mutations;
445 };
446
447 static bool
448 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
449 {
450     struct mutate_row_cbdata *mr = mr_;
451
452     mr->n_matches++;
453     ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
454                                mr->mutations);
455
456     return true;
457 }
458
459 struct ovsdb_error *
460 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
461                      struct json *result)
462 {
463     struct ovsdb_table *table;
464     const struct json *where;
465     const struct json *mutations_json;
466     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
467     struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
468     struct ovsdb_row *row = NULL;
469     struct mutate_row_cbdata mr;
470     struct ovsdb_error *error;
471
472     table = parse_table(x, parser, "table");
473     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
474     mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
475     error = ovsdb_parser_get_error(parser);
476     if (!error) {
477         error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
478                                              x->symtab, &mutations);
479     }
480     if (!error) {
481         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
482                                           &condition);
483     }
484     if (!error) {
485         mr.n_matches = 0;
486         mr.txn = x->txn;
487         mr.mutations = &mutations;
488         ovsdb_query(table, &condition, mutate_row_cb, &mr);
489         json_object_put(result, "count", json_integer_create(mr.n_matches));
490     }
491
492     ovsdb_row_destroy(row);
493     ovsdb_mutation_set_destroy(&mutations);
494     ovsdb_condition_destroy(&condition);
495
496     return error;
497 }
498
499 struct delete_row_cbdata {
500     size_t n_matches;
501     const struct ovsdb_table *table;
502     struct ovsdb_txn *txn;
503 };
504
505 static bool
506 delete_row_cb(const struct ovsdb_row *row, void *dr_)
507 {
508     struct delete_row_cbdata *dr = dr_;
509
510     dr->n_matches++;
511     ovsdb_txn_row_delete(dr->txn, row);
512
513     return true;
514 }
515
516 struct ovsdb_error *
517 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
518                      struct json *result)
519 {
520     struct ovsdb_table *table;
521     const struct json *where;
522     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
523     struct ovsdb_error *error;
524
525     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
526     table = parse_table(x, parser, "table");
527     error = ovsdb_parser_get_error(parser);
528     if (!error) {
529         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
530                                           &condition);
531     }
532     if (!error) {
533         struct delete_row_cbdata dr;
534
535         dr.n_matches = 0;
536         dr.table = table;
537         dr.txn = x->txn;
538         ovsdb_query(table, &condition, delete_row_cb, &dr);
539
540         json_object_put(result, "count", json_integer_create(dr.n_matches));
541     }
542
543     ovsdb_condition_destroy(&condition);
544
545     return error;
546 }
547
548 struct wait_auxdata {
549     struct ovsdb_row_hash *actual;
550     struct ovsdb_row_hash *expected;
551     bool *equal;
552 };
553
554 static bool
555 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
556 {
557     struct wait_auxdata *aux = aux_;
558
559     if (ovsdb_row_hash_contains(aux->expected, row)) {
560         ovsdb_row_hash_insert(aux->actual, row);
561         return true;
562     } else {
563         /* The query row isn't in the expected result set, so the actual and
564          * expected results sets definitely differ and we can short-circuit the
565          * rest of the query. */
566         *aux->equal = false;
567         return false;
568     }
569 }
570
571 static struct ovsdb_error *
572 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
573                    struct json *result UNUSED)
574 {
575     struct ovsdb_table *table;
576     const struct json *timeout, *where, *columns_json, *until, *rows;
577     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
578     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
579     struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
580     struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
581     struct ovsdb_error *error;
582     struct wait_auxdata aux;
583     long long int timeout_msec = 0;
584     size_t i;
585
586     timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
587     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
588     columns_json = ovsdb_parser_member(parser, "columns",
589                                        OP_ARRAY | OP_OPTIONAL);
590     until = ovsdb_parser_member(parser, "until", OP_STRING);
591     rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
592     table = parse_table(x, parser, "table");
593     error = ovsdb_parser_get_error(parser);
594     if (!error) {
595         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
596                                           &condition);
597     }
598     if (!error) {
599         error = ovsdb_column_set_from_json(columns_json, table, &columns);
600     }
601     if (!error) {
602         if (timeout) {
603             timeout_msec = MIN(LLONG_MAX, json_real(timeout));
604             if (timeout_msec < 0) {
605                 error = ovsdb_syntax_error(timeout, NULL,
606                                            "timeout must be nonnegative");
607             } else if (timeout_msec < x->timeout_msec) {
608                 x->timeout_msec = timeout_msec;
609             }
610         } else {
611             timeout_msec = LLONG_MAX;
612         }
613         if (strcmp(json_string(until), "==")
614             && strcmp(json_string(until), "!=")) {
615             error = ovsdb_syntax_error(until, NULL,
616                                        "\"until\" must be \"==\" or \"!=\"");
617         }
618     }
619     if (!error) {
620         /* Parse "rows" into 'expected'. */
621         ovsdb_row_hash_init(&expected, &columns);
622         for (i = 0; i < rows->u.array.n; i++) {
623             struct ovsdb_error *error;
624             struct ovsdb_row *row;
625
626             row = ovsdb_row_create(table);
627             error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
628                                         NULL);
629             if (error) {
630                 break;
631             }
632
633             if (!ovsdb_row_hash_insert(&expected, row)) {
634                 /* XXX Perhaps we should abort with an error or log a
635                  * warning. */
636                 ovsdb_row_destroy(row);
637             }
638         }
639     }
640     if (!error) {
641         /* Execute query. */
642         bool equal = true;
643         ovsdb_row_hash_init(&actual, &columns);
644         aux.actual = &actual;
645         aux.expected = &expected;
646         aux.equal = &equal;
647         ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
648         if (equal) {
649             /* We know that every row in 'actual' is also in 'expected'.  We
650              * also know that all of the rows in 'actual' are distinct and that
651              * all of the rows in 'expected' are distinct.  Therefore, if
652              * 'actual' and 'expected' have the same number of rows, then they
653              * have the same content. */
654             size_t n_actual = ovsdb_row_hash_count(&actual);
655             size_t n_expected = ovsdb_row_hash_count(&expected);
656             equal = n_actual == n_expected;
657         }
658         if (!strcmp(json_string(until), "==") != equal) {
659             if (timeout && x->elapsed_msec >= timeout_msec) {
660                 if (x->elapsed_msec) {
661                     error = ovsdb_error("timed out",
662                                         "\"wait\" timed out after %lld ms",
663                                         x->elapsed_msec);
664                 } else {
665                     error = ovsdb_error("timed out", "\"wait\" timed out");
666                 }
667             } else {
668                 /* ovsdb_execute() will change this, if triggers really are
669                  * supported. */
670                 error = ovsdb_error("not supported", "triggers not supported");
671             }
672         }
673     }
674
675
676     ovsdb_row_hash_destroy(&expected, true);
677     ovsdb_row_hash_destroy(&actual, false);
678     ovsdb_column_set_destroy(&columns);
679     ovsdb_condition_destroy(&condition);
680
681     return error;
682 }
683
684 static struct ovsdb_error *
685 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
686                       struct json *result UNUSED)
687 {
688     const struct json *comment;
689
690     comment = ovsdb_parser_member(parser, "comment", OP_STRING);
691     if (!comment) {
692         return NULL;
693     }
694     ovsdb_txn_add_comment(x->txn, json_string(comment));
695
696     return NULL;
697 }