1 /* Copyright (c) 2009, 2010, 2011 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_comment;
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 { "comment", ovsdb_execute_comment },
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 || !params->u.array.n
104 || params->u.array.elems[0]->type != JSON_STRING
105 || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) {
106 if (params->type != JSON_ARRAY) {
107 error = ovsdb_syntax_error(params, NULL, "array expected");
109 error = ovsdb_syntax_error(params, NULL, "database name expected "
110 "as first parameter");
113 results = ovsdb_error_to_json(error);
114 ovsdb_error_destroy(error);
119 x.txn = ovsdb_txn_create(db);
120 x.symtab = ovsdb_symbol_table_create();
122 x.elapsed_msec = elapsed_msec;
123 x.timeout_msec = LLONG_MAX;
126 results = json_array_create_empty();
127 n_operations = params->u.array.n - 1;
129 for (i = 1; i <= n_operations; i++) {
130 struct json *operation = params->u.array.elems[i];
131 struct ovsdb_error *parse_error;
132 struct ovsdb_parser parser;
134 const struct json *op;
136 /* Parse and execute operation. */
137 ovsdb_parser_init(&parser, operation,
138 "ovsdb operation %zu of %zu", i, n_operations);
139 op = ovsdb_parser_member(&parser, "op", OP_ID);
140 result = json_object_create();
142 const char *op_name = json_string(op);
143 ovsdb_operation_executor *executor = lookup_executor(op_name);
145 error = executor(&x, &parser, result);
147 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
151 assert(ovsdb_parser_has_error(&parser));
154 /* A parse error overrides any other error.
155 * An error overrides any other result. */
156 parse_error = ovsdb_parser_finish(&parser);
158 ovsdb_error_destroy(error);
162 json_destroy(result);
163 result = ovsdb_error_to_json(error);
165 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
167 ovsdb_txn_abort(x.txn);
168 *timeout_msec = x.timeout_msec;
170 json_destroy(result);
171 json_destroy(results);
176 /* Add result to array. */
177 json_array_add(results, result);
184 error = ovsdb_txn_commit(x.txn, x.durable);
186 json_array_add(results, ovsdb_error_to_json(error));
189 ovsdb_txn_abort(x.txn);
192 while (json_array(results)->n < n_operations) {
193 json_array_add(results, json_null_create());
197 ovsdb_error_destroy(error);
198 ovsdb_symbol_table_destroy(x.symtab);
203 static struct ovsdb_error *
204 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
205 struct json *result OVS_UNUSED)
207 const struct json *durable;
209 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
210 if (durable && json_boolean(durable)) {
216 static struct ovsdb_error *
217 ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
218 struct ovsdb_parser *parser OVS_UNUSED,
219 struct json *result OVS_UNUSED)
221 return ovsdb_error("aborted", "aborted by request");
224 static struct ovsdb_table *
225 parse_table(struct ovsdb_execution *x,
226 struct ovsdb_parser *parser, const char *member)
228 struct ovsdb_table *table;
229 const char *table_name;
230 const struct json *json;
232 json = ovsdb_parser_member(parser, member, OP_ID);
236 table_name = json_string(json);
238 table = shash_find_data(&x->db->tables, table_name);
240 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
245 static WARN_UNUSED_RESULT struct ovsdb_error *
246 parse_row(const struct json *json, const struct ovsdb_table *table,
247 struct ovsdb_symbol_table *symtab,
248 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
250 struct ovsdb_error *error;
251 struct ovsdb_row *row;
256 return OVSDB_BUG("null table");
259 return OVSDB_BUG("null row");
262 row = ovsdb_row_create(table);
263 error = ovsdb_row_from_json(row, json, symtab, columns);
265 ovsdb_row_destroy(row);
273 static struct ovsdb_error *
274 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
277 struct ovsdb_table *table;
278 struct ovsdb_row *row = NULL;
279 const struct json *uuid_name, *row_json;
280 struct ovsdb_error *error;
281 struct uuid row_uuid;
283 table = parse_table(x, parser, "table");
284 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
285 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
286 error = ovsdb_parser_get_error(parser);
292 struct ovsdb_symbol *symbol;
294 symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
295 if (symbol->created) {
296 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
297 "This \"uuid-name\" appeared on an "
298 "earlier \"insert\" operation.");
300 row_uuid = symbol->uuid;
301 symbol->created = true;
303 uuid_generate(&row_uuid);
307 error = parse_row(row_json, table, x->symtab, &row, NULL);
310 /* Check constraints for columns not included in "row", in case the
311 * default values do not satisfy the constraints. We could check only
312 * the columns that have their default values by supplying an
313 * ovsdb_column_set to parse_row() above, but I suspect that this is
315 const struct shash_node *node;
317 SHASH_FOR_EACH (node, &table->schema->columns) {
318 const struct ovsdb_column *column = node->data;
319 const struct ovsdb_datum *datum = &row->fields[column->index];
321 /* If there are 0 keys or pairs, there's nothing to check.
322 * If there is 1, it might be a default value.
323 * If there are more, it can't be a default value, so the value has
324 * already been checked. */
326 error = ovsdb_datum_check_constraints(datum, &column->type);
328 ovsdb_row_destroy(row);
335 *ovsdb_row_get_uuid_rw(row) = row_uuid;
336 ovsdb_txn_row_insert(x->txn, row);
337 json_object_put(result, "uuid",
338 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
344 static struct ovsdb_error *
345 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
348 struct ovsdb_table *table;
349 const struct json *where, *columns_json, *sort_json;
350 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
351 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
352 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
353 struct ovsdb_error *error;
355 table = parse_table(x, parser, "table");
356 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
357 columns_json = ovsdb_parser_member(parser, "columns",
358 OP_ARRAY | OP_OPTIONAL);
359 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
361 error = ovsdb_parser_get_error(parser);
363 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
367 error = ovsdb_column_set_from_json(columns_json, table->schema,
371 error = ovsdb_column_set_from_json(sort_json, table->schema, &sort);
374 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
376 ovsdb_query_distinct(table, &condition, &columns, &rows);
377 ovsdb_row_set_sort(&rows, &sort);
378 json_object_put(result, "rows",
379 ovsdb_row_set_to_json(&rows, &columns));
381 ovsdb_row_set_destroy(&rows);
384 ovsdb_column_set_destroy(&columns);
385 ovsdb_column_set_destroy(&sort);
386 ovsdb_condition_destroy(&condition);
391 struct update_row_cbdata {
393 struct ovsdb_txn *txn;
394 const struct ovsdb_row *row;
395 const struct ovsdb_column_set *columns;
399 update_row_cb(const struct ovsdb_row *row, void *ur_)
401 struct update_row_cbdata *ur = ur_;
404 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
405 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
406 ur->row, ur->columns);
412 static struct ovsdb_error *
413 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
416 struct ovsdb_table *table;
417 const struct json *where, *row_json;
418 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
419 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
420 struct ovsdb_row *row = NULL;
421 struct update_row_cbdata ur;
422 struct ovsdb_error *error;
424 table = parse_table(x, parser, "table");
425 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
426 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
427 error = ovsdb_parser_get_error(parser);
429 error = parse_row(row_json, table, x->symtab, &row, &columns);
432 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
439 ur.columns = &columns;
440 ovsdb_query(table, &condition, update_row_cb, &ur);
441 json_object_put(result, "count", json_integer_create(ur.n_matches));
444 ovsdb_row_destroy(row);
445 ovsdb_column_set_destroy(&columns);
446 ovsdb_condition_destroy(&condition);
451 struct mutate_row_cbdata {
453 struct ovsdb_txn *txn;
454 const struct ovsdb_mutation_set *mutations;
455 struct ovsdb_error **error;
459 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
461 struct mutate_row_cbdata *mr = mr_;
464 *mr->error = ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
466 return *mr->error == NULL;
469 static struct ovsdb_error *
470 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
473 struct ovsdb_table *table;
474 const struct json *where;
475 const struct json *mutations_json;
476 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
477 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
478 struct ovsdb_row *row = NULL;
479 struct mutate_row_cbdata mr;
480 struct ovsdb_error *error;
482 table = parse_table(x, parser, "table");
483 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
484 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
485 error = ovsdb_parser_get_error(parser);
487 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
488 x->symtab, &mutations);
491 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
497 mr.mutations = &mutations;
499 ovsdb_query(table, &condition, mutate_row_cb, &mr);
500 json_object_put(result, "count", json_integer_create(mr.n_matches));
503 ovsdb_row_destroy(row);
504 ovsdb_mutation_set_destroy(&mutations);
505 ovsdb_condition_destroy(&condition);
510 struct delete_row_cbdata {
512 const struct ovsdb_table *table;
513 struct ovsdb_txn *txn;
517 delete_row_cb(const struct ovsdb_row *row, void *dr_)
519 struct delete_row_cbdata *dr = dr_;
522 ovsdb_txn_row_delete(dr->txn, row);
527 static struct ovsdb_error *
528 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
531 struct ovsdb_table *table;
532 const struct json *where;
533 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
534 struct ovsdb_error *error;
536 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
537 table = parse_table(x, parser, "table");
538 error = ovsdb_parser_get_error(parser);
540 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
544 struct delete_row_cbdata dr;
549 ovsdb_query(table, &condition, delete_row_cb, &dr);
551 json_object_put(result, "count", json_integer_create(dr.n_matches));
554 ovsdb_condition_destroy(&condition);
559 struct wait_auxdata {
560 struct ovsdb_row_hash *actual;
561 struct ovsdb_row_hash *expected;
566 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
568 struct wait_auxdata *aux = aux_;
570 if (ovsdb_row_hash_contains(aux->expected, row)) {
571 ovsdb_row_hash_insert(aux->actual, row);
574 /* The query row isn't in the expected result set, so the actual and
575 * expected results sets definitely differ and we can short-circuit the
576 * rest of the query. */
582 static struct ovsdb_error *
583 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
584 struct json *result OVS_UNUSED)
586 struct ovsdb_table *table;
587 const struct json *timeout, *where, *columns_json, *until, *rows;
588 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
589 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
590 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
591 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
592 struct ovsdb_error *error;
593 struct wait_auxdata aux;
594 long long int timeout_msec = 0;
597 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
598 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
599 columns_json = ovsdb_parser_member(parser, "columns",
600 OP_ARRAY | OP_OPTIONAL);
601 until = ovsdb_parser_member(parser, "until", OP_STRING);
602 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
603 table = parse_table(x, parser, "table");
604 error = ovsdb_parser_get_error(parser);
606 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
610 error = ovsdb_column_set_from_json(columns_json, table->schema,
615 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
616 if (timeout_msec < 0) {
617 error = ovsdb_syntax_error(timeout, NULL,
618 "timeout must be nonnegative");
619 } else if (timeout_msec < x->timeout_msec) {
620 x->timeout_msec = timeout_msec;
623 timeout_msec = LLONG_MAX;
625 if (strcmp(json_string(until), "==")
626 && strcmp(json_string(until), "!=")) {
627 error = ovsdb_syntax_error(until, NULL,
628 "\"until\" must be \"==\" or \"!=\"");
632 /* Parse "rows" into 'expected'. */
633 ovsdb_row_hash_init(&expected, &columns);
634 for (i = 0; i < rows->u.array.n; i++) {
635 struct ovsdb_row *row;
637 row = ovsdb_row_create(table);
638 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
644 if (!ovsdb_row_hash_insert(&expected, row)) {
645 /* XXX Perhaps we should abort with an error or log a
647 ovsdb_row_destroy(row);
654 ovsdb_row_hash_init(&actual, &columns);
655 aux.actual = &actual;
656 aux.expected = &expected;
658 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
660 /* We know that every row in 'actual' is also in 'expected'. We
661 * also know that all of the rows in 'actual' are distinct and that
662 * all of the rows in 'expected' are distinct. Therefore, if
663 * 'actual' and 'expected' have the same number of rows, then they
664 * have the same content. */
665 size_t n_actual = ovsdb_row_hash_count(&actual);
666 size_t n_expected = ovsdb_row_hash_count(&expected);
667 equal = n_actual == n_expected;
669 if (!strcmp(json_string(until), "==") != equal) {
670 if (timeout && x->elapsed_msec >= timeout_msec) {
671 if (x->elapsed_msec) {
672 error = ovsdb_error("timed out",
673 "\"wait\" timed out after %lld ms",
676 error = ovsdb_error("timed out", "\"wait\" timed out");
679 /* ovsdb_execute() will change this, if triggers really are
681 error = ovsdb_error("not supported", "triggers not supported");
687 ovsdb_row_hash_destroy(&expected, true);
688 ovsdb_row_hash_destroy(&actual, false);
689 ovsdb_column_set_destroy(&columns);
690 ovsdb_condition_destroy(&condition);
695 static struct ovsdb_error *
696 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
697 struct json *result OVS_UNUSED)
699 const struct json *comment;
701 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
705 ovsdb_txn_add_comment(x->txn, json_string(comment));