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