1 /* Copyright (c) 2009 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "condition.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
34 #include "transaction.h"
36 struct ovsdb_execution {
38 struct ovsdb_txn *txn;
39 struct ovsdb_symbol_table *symtab;
43 long long int elapsed_msec;
44 long long int timeout_msec;
47 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
48 struct ovsdb_parser *,
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;
61 static ovsdb_operation_executor *
62 lookup_executor(const char *name)
64 struct ovsdb_operation {
66 ovsdb_operation_executor *executor;
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 },
83 for (i = 0; i < ARRAY_SIZE(operations); i++) {
84 const struct ovsdb_operation *c = &operations[i];
85 if (!strcmp(c->name, name)) {
93 ovsdb_execute(struct ovsdb *db, const struct json *params,
94 long long int elapsed_msec, long long int *timeout_msec)
96 struct ovsdb_execution x;
97 struct ovsdb_error *error;
102 if (params->type != JSON_ARRAY) {
103 struct ovsdb_error *error;
105 error = ovsdb_syntax_error(params, NULL, "array expected");
106 results = ovsdb_error_to_json(error);
107 ovsdb_error_destroy(error);
112 x.txn = ovsdb_txn_create(db);
113 x.symtab = ovsdb_symbol_table_create();
115 x.elapsed_msec = elapsed_msec;
116 x.timeout_msec = LLONG_MAX;
119 results = json_array_create_empty();
120 n_operations = params->u.array.n;
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;
127 const struct json *op;
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();
135 const char *op_name = json_string(op);
136 ovsdb_operation_executor *executor = lookup_executor(op_name);
138 error = executor(&x, &parser, result);
140 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
144 assert(ovsdb_parser_has_error(&parser));
147 /* A parse error overrides any other error.
148 * An error overrides any other result. */
149 parse_error = ovsdb_parser_finish(&parser);
151 ovsdb_error_destroy(error);
155 json_destroy(result);
156 result = ovsdb_error_to_json(error);
158 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
160 ovsdb_txn_abort(x.txn);
161 *timeout_msec = x.timeout_msec;
162 ovsdb_error_destroy(error);
163 json_destroy(results);
167 /* Add result to array. */
168 json_array_add(results, result);
175 error = ovsdb_txn_commit(x.txn, x.durable);
177 json_array_add(results, ovsdb_error_to_json(error));
180 ovsdb_txn_abort(x.txn);
183 while (json_array(results)->n < n_operations) {
184 json_array_add(results, json_null_create());
187 ovsdb_error_destroy(error);
188 ovsdb_symbol_table_destroy(x.symtab);
194 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
195 struct json *result UNUSED)
197 const struct json *durable;
199 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
200 if (durable && json_boolean(durable)) {
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)
211 return ovsdb_error("aborted", "aborted by request");
214 static struct ovsdb_table *
215 parse_table(struct ovsdb_execution *x,
216 struct ovsdb_parser *parser, const char *member)
218 struct ovsdb_table *table;
219 const char *table_name;
220 const struct json *json;
222 json = ovsdb_parser_member(parser, member, OP_ID);
226 table_name = json_string(json);
228 table = shash_find_data(&x->db->tables, table_name);
230 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
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)
241 struct ovsdb_error *error;
242 const struct json *json;
243 struct ovsdb_row *row;
248 return OVSDB_BUG("null table");
250 json = ovsdb_parser_member(parser, member, OP_OBJECT);
252 return OVSDB_BUG("null row member");
255 row = ovsdb_row_create(table);
256 error = ovsdb_row_from_json(row, json, symtab, columns);
258 ovsdb_row_destroy(row);
267 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
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);
281 struct ovsdb_symbol *symbol;
283 symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name));
286 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
287 "This \"uuid-name\" appeared on an "
288 "earlier \"insert\" operation.");
290 row_uuid = symbol->uuid;
293 uuid_generate(&row_uuid);
294 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
298 uuid_generate(&row_uuid);
302 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
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],
316 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
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);
332 error = ovsdb_parser_get_error(parser);
334 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
338 error = ovsdb_column_set_from_json(columns_json, table, &columns);
341 error = ovsdb_column_set_from_json(sort_json, table, &sort);
344 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
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));
351 ovsdb_row_set_destroy(&rows);
354 ovsdb_column_set_destroy(&columns);
355 ovsdb_column_set_destroy(&sort);
356 ovsdb_condition_destroy(&condition);
361 struct update_row_cbdata {
363 struct ovsdb_txn *txn;
364 const struct ovsdb_row *row;
365 const struct ovsdb_column_set *columns;
369 update_row_cb(const struct ovsdb_row *row, void *ur_)
371 struct update_row_cbdata *ur = ur_;
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);
383 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
394 table = parse_table(x, parser, "table");
395 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
396 error = ovsdb_parser_get_error(parser);
398 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
401 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
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));
413 ovsdb_row_destroy(row);
414 ovsdb_column_set_destroy(&columns);
415 ovsdb_condition_destroy(&condition);
420 struct mutate_row_cbdata {
422 struct ovsdb_txn *txn;
423 const struct ovsdb_mutation_set *mutations;
427 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
429 struct mutate_row_cbdata *mr = mr_;
432 ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
439 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
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);
456 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
457 x->symtab, &mutations);
460 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
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));
471 ovsdb_row_destroy(row);
472 ovsdb_mutation_set_destroy(&mutations);
473 ovsdb_condition_destroy(&condition);
478 struct delete_row_cbdata {
480 const struct ovsdb_table *table;
481 struct ovsdb_txn *txn;
485 delete_row_cb(const struct ovsdb_row *row, void *dr_)
487 struct delete_row_cbdata *dr = dr_;
490 ovsdb_txn_row_delete(dr->txn, row);
496 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
499 struct ovsdb_table *table;
500 const struct json *where;
501 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
502 struct ovsdb_error *error;
504 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
505 table = parse_table(x, parser, "table");
506 error = ovsdb_parser_get_error(parser);
508 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
512 struct delete_row_cbdata dr;
517 ovsdb_query(table, &condition, delete_row_cb, &dr);
519 json_object_put(result, "count", json_integer_create(dr.n_matches));
522 ovsdb_condition_destroy(&condition);
527 struct wait_auxdata {
528 struct ovsdb_row_hash *actual;
529 struct ovsdb_row_hash *expected;
534 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
536 struct wait_auxdata *aux = aux_;
538 if (ovsdb_row_hash_contains(aux->expected, row)) {
539 ovsdb_row_hash_insert(aux->actual, row);
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. */
550 static struct ovsdb_error *
551 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
552 struct json *result UNUSED)
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;
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);
574 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
578 error = ovsdb_column_set_from_json(columns_json, table, &columns);
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;
590 timeout_msec = LLONG_MAX;
592 if (strcmp(json_string(until), "==")
593 && strcmp(json_string(until), "!=")) {
594 error = ovsdb_syntax_error(until, NULL,
595 "\"until\" must be \"==\" or \"!=\"");
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;
605 row = ovsdb_row_create(table);
606 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
612 if (!ovsdb_row_hash_insert(&expected, row)) {
613 /* XXX Perhaps we should abort with an error or log a
615 ovsdb_row_destroy(row);
622 ovsdb_row_hash_init(&actual, &columns);
623 aux.actual = &actual;
624 aux.expected = &expected;
626 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
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;
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",
644 error = ovsdb_error("timed out", "\"wait\" timed out");
647 /* ovsdb_execute() will change this, if triggers really are
649 error = ovsdb_error("not supported", "triggers not supported");
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);
663 static struct ovsdb_error *
664 ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser,
667 const struct json *uuid_name;
670 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID);
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\" "
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))));