1 /* Copyright (c) 2009, 2010 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;
60 static ovsdb_operation_executor ovsdb_execute_comment;
62 static ovsdb_operation_executor *
63 lookup_executor(const char *name)
65 struct ovsdb_operation {
67 ovsdb_operation_executor *executor;
70 static const struct ovsdb_operation operations[] = {
71 { "insert", ovsdb_execute_insert },
72 { "select", ovsdb_execute_select },
73 { "update", ovsdb_execute_update },
74 { "mutate", ovsdb_execute_mutate },
75 { "delete", ovsdb_execute_delete },
76 { "wait", ovsdb_execute_wait },
77 { "commit", ovsdb_execute_commit },
78 { "abort", ovsdb_execute_abort },
79 { "declare", ovsdb_execute_declare },
80 { "comment", ovsdb_execute_comment },
85 for (i = 0; i < ARRAY_SIZE(operations); i++) {
86 const struct ovsdb_operation *c = &operations[i];
87 if (!strcmp(c->name, name)) {
95 ovsdb_execute(struct ovsdb *db, const struct json *params,
96 long long int elapsed_msec, long long int *timeout_msec)
98 struct ovsdb_execution x;
99 struct ovsdb_error *error;
100 struct json *results;
104 if (params->type != JSON_ARRAY) {
105 struct ovsdb_error *error;
107 error = ovsdb_syntax_error(params, NULL, "array expected");
108 results = ovsdb_error_to_json(error);
109 ovsdb_error_destroy(error);
114 x.txn = ovsdb_txn_create(db);
115 x.symtab = ovsdb_symbol_table_create();
117 x.elapsed_msec = elapsed_msec;
118 x.timeout_msec = LLONG_MAX;
121 results = json_array_create_empty();
122 n_operations = params->u.array.n;
124 for (i = 0; i < n_operations; i++) {
125 struct json *operation = params->u.array.elems[i];
126 struct ovsdb_error *parse_error;
127 struct ovsdb_parser parser;
129 const struct json *op;
131 /* Parse and execute operation. */
132 ovsdb_parser_init(&parser, operation,
133 "ovsdb operation %zu of %zu", i + 1, n_operations);
134 op = ovsdb_parser_member(&parser, "op", OP_ID);
135 result = json_object_create();
137 const char *op_name = json_string(op);
138 ovsdb_operation_executor *executor = lookup_executor(op_name);
140 error = executor(&x, &parser, result);
142 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
146 assert(ovsdb_parser_has_error(&parser));
149 /* A parse error overrides any other error.
150 * An error overrides any other result. */
151 parse_error = ovsdb_parser_finish(&parser);
153 ovsdb_error_destroy(error);
157 json_destroy(result);
158 result = ovsdb_error_to_json(error);
160 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
162 ovsdb_txn_abort(x.txn);
163 *timeout_msec = x.timeout_msec;
165 json_destroy(result);
166 json_destroy(results);
171 /* Add result to array. */
172 json_array_add(results, result);
179 error = ovsdb_txn_commit(x.txn, x.durable);
181 json_array_add(results, ovsdb_error_to_json(error));
184 ovsdb_txn_abort(x.txn);
187 while (json_array(results)->n < n_operations) {
188 json_array_add(results, json_null_create());
192 ovsdb_error_destroy(error);
193 ovsdb_symbol_table_destroy(x.symtab);
199 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
200 struct json *result UNUSED)
202 const struct json *durable;
204 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
205 if (durable && json_boolean(durable)) {
211 static struct ovsdb_error *
212 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
213 struct ovsdb_parser *parser UNUSED,
214 struct json *result UNUSED)
216 return ovsdb_error("aborted", "aborted by request");
219 static struct ovsdb_table *
220 parse_table(struct ovsdb_execution *x,
221 struct ovsdb_parser *parser, const char *member)
223 struct ovsdb_table *table;
224 const char *table_name;
225 const struct json *json;
227 json = ovsdb_parser_member(parser, member, OP_ID);
231 table_name = json_string(json);
233 table = shash_find_data(&x->db->tables, table_name);
235 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
240 static WARN_UNUSED_RESULT struct ovsdb_error *
241 parse_row(struct ovsdb_parser *parser, const char *member,
242 const struct ovsdb_table *table,
243 const struct ovsdb_symbol_table *symtab,
244 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
246 struct ovsdb_error *error;
247 const struct json *json;
248 struct ovsdb_row *row;
253 return OVSDB_BUG("null table");
255 json = ovsdb_parser_member(parser, member, OP_OBJECT);
257 return OVSDB_BUG("null row member");
260 row = ovsdb_row_create(table);
261 error = ovsdb_row_from_json(row, json, symtab, columns);
263 ovsdb_row_destroy(row);
272 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
275 struct ovsdb_table *table;
276 struct ovsdb_row *row = NULL;
277 const struct json *uuid_name;
278 struct ovsdb_error *error;
279 struct uuid row_uuid;
281 table = parse_table(x, parser, "table");
282 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
283 error = ovsdb_parser_get_error(parser);
286 struct ovsdb_symbol *symbol;
288 symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name));
291 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
292 "This \"uuid-name\" appeared on an "
293 "earlier \"insert\" operation.");
295 row_uuid = symbol->uuid;
298 uuid_generate(&row_uuid);
299 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
303 uuid_generate(&row_uuid);
307 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
310 *ovsdb_row_get_uuid_rw(row) = row_uuid;
311 ovsdb_txn_row_insert(x->txn, row);
312 json_object_put(result, "uuid",
313 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
321 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
324 struct ovsdb_table *table;
325 const struct json *where, *columns_json, *sort_json;
326 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
327 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
328 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
329 struct ovsdb_error *error;
331 table = parse_table(x, parser, "table");
332 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
333 columns_json = ovsdb_parser_member(parser, "columns",
334 OP_ARRAY | OP_OPTIONAL);
335 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
337 error = ovsdb_parser_get_error(parser);
339 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
343 error = ovsdb_column_set_from_json(columns_json, table, &columns);
346 error = ovsdb_column_set_from_json(sort_json, table, &sort);
349 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
351 ovsdb_query_distinct(table, &condition, &columns, &rows);
352 ovsdb_row_set_sort(&rows, &sort);
353 json_object_put(result, "rows",
354 ovsdb_row_set_to_json(&rows, &columns));
356 ovsdb_row_set_destroy(&rows);
359 ovsdb_column_set_destroy(&columns);
360 ovsdb_column_set_destroy(&sort);
361 ovsdb_condition_destroy(&condition);
366 struct update_row_cbdata {
368 struct ovsdb_txn *txn;
369 const struct ovsdb_row *row;
370 const struct ovsdb_column_set *columns;
374 update_row_cb(const struct ovsdb_row *row, void *ur_)
376 struct update_row_cbdata *ur = ur_;
379 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
380 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
381 ur->row, ur->columns);
388 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
391 struct ovsdb_table *table;
392 const struct json *where;
393 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
394 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
395 struct ovsdb_row *row = NULL;
396 struct update_row_cbdata ur;
397 struct ovsdb_error *error;
399 table = parse_table(x, parser, "table");
400 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
401 error = ovsdb_parser_get_error(parser);
403 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
406 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
413 ur.columns = &columns;
414 ovsdb_query(table, &condition, update_row_cb, &ur);
415 json_object_put(result, "count", json_integer_create(ur.n_matches));
418 ovsdb_row_destroy(row);
419 ovsdb_column_set_destroy(&columns);
420 ovsdb_condition_destroy(&condition);
425 struct mutate_row_cbdata {
427 struct ovsdb_txn *txn;
428 const struct ovsdb_mutation_set *mutations;
432 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
434 struct mutate_row_cbdata *mr = mr_;
437 ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
444 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
447 struct ovsdb_table *table;
448 const struct json *where;
449 const struct json *mutations_json;
450 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
451 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
452 struct ovsdb_row *row = NULL;
453 struct mutate_row_cbdata mr;
454 struct ovsdb_error *error;
456 table = parse_table(x, parser, "table");
457 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
458 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
459 error = ovsdb_parser_get_error(parser);
461 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
462 x->symtab, &mutations);
465 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
471 mr.mutations = &mutations;
472 ovsdb_query(table, &condition, mutate_row_cb, &mr);
473 json_object_put(result, "count", json_integer_create(mr.n_matches));
476 ovsdb_row_destroy(row);
477 ovsdb_mutation_set_destroy(&mutations);
478 ovsdb_condition_destroy(&condition);
483 struct delete_row_cbdata {
485 const struct ovsdb_table *table;
486 struct ovsdb_txn *txn;
490 delete_row_cb(const struct ovsdb_row *row, void *dr_)
492 struct delete_row_cbdata *dr = dr_;
495 ovsdb_txn_row_delete(dr->txn, row);
501 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
504 struct ovsdb_table *table;
505 const struct json *where;
506 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
507 struct ovsdb_error *error;
509 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
510 table = parse_table(x, parser, "table");
511 error = ovsdb_parser_get_error(parser);
513 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
517 struct delete_row_cbdata dr;
522 ovsdb_query(table, &condition, delete_row_cb, &dr);
524 json_object_put(result, "count", json_integer_create(dr.n_matches));
527 ovsdb_condition_destroy(&condition);
532 struct wait_auxdata {
533 struct ovsdb_row_hash *actual;
534 struct ovsdb_row_hash *expected;
539 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
541 struct wait_auxdata *aux = aux_;
543 if (ovsdb_row_hash_contains(aux->expected, row)) {
544 ovsdb_row_hash_insert(aux->actual, row);
547 /* The query row isn't in the expected result set, so the actual and
548 * expected results sets definitely differ and we can short-circuit the
549 * rest of the query. */
555 static struct ovsdb_error *
556 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
557 struct json *result UNUSED)
559 struct ovsdb_table *table;
560 const struct json *timeout, *where, *columns_json, *until, *rows;
561 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
562 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
563 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
564 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
565 struct ovsdb_error *error;
566 struct wait_auxdata aux;
567 long long int timeout_msec = 0;
570 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
571 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
572 columns_json = ovsdb_parser_member(parser, "columns",
573 OP_ARRAY | OP_OPTIONAL);
574 until = ovsdb_parser_member(parser, "until", OP_STRING);
575 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
576 table = parse_table(x, parser, "table");
577 error = ovsdb_parser_get_error(parser);
579 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
583 error = ovsdb_column_set_from_json(columns_json, table, &columns);
587 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
588 if (timeout_msec < 0) {
589 error = ovsdb_syntax_error(timeout, NULL,
590 "timeout must be nonnegative");
591 } else if (timeout_msec < x->timeout_msec) {
592 x->timeout_msec = timeout_msec;
595 timeout_msec = LLONG_MAX;
597 if (strcmp(json_string(until), "==")
598 && strcmp(json_string(until), "!=")) {
599 error = ovsdb_syntax_error(until, NULL,
600 "\"until\" must be \"==\" or \"!=\"");
604 /* Parse "rows" into 'expected'. */
605 ovsdb_row_hash_init(&expected, &columns);
606 for (i = 0; i < rows->u.array.n; i++) {
607 struct ovsdb_error *error;
608 struct ovsdb_row *row;
610 row = ovsdb_row_create(table);
611 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
617 if (!ovsdb_row_hash_insert(&expected, row)) {
618 /* XXX Perhaps we should abort with an error or log a
620 ovsdb_row_destroy(row);
627 ovsdb_row_hash_init(&actual, &columns);
628 aux.actual = &actual;
629 aux.expected = &expected;
631 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
633 /* We know that every row in 'actual' is also in 'expected'. We
634 * also know that all of the rows in 'actual' are distinct and that
635 * all of the rows in 'expected' are distinct. Therefore, if
636 * 'actual' and 'expected' have the same number of rows, then they
637 * have the same content. */
638 size_t n_actual = ovsdb_row_hash_count(&actual);
639 size_t n_expected = ovsdb_row_hash_count(&expected);
640 equal = n_actual == n_expected;
642 if (!strcmp(json_string(until), "==") != equal) {
643 if (timeout && x->elapsed_msec >= timeout_msec) {
644 if (x->elapsed_msec) {
645 error = ovsdb_error("timed out",
646 "\"wait\" timed out after %lld ms",
649 error = ovsdb_error("timed out", "\"wait\" timed out");
652 /* ovsdb_execute() will change this, if triggers really are
654 error = ovsdb_error("not supported", "triggers not supported");
660 ovsdb_row_hash_destroy(&expected, true);
661 ovsdb_row_hash_destroy(&actual, false);
662 ovsdb_column_set_destroy(&columns);
663 ovsdb_condition_destroy(&condition);
668 static struct ovsdb_error *
669 ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser,
672 const struct json *uuid_name;
675 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID);
680 if (ovsdb_symbol_table_get(x->symtab, json_string(uuid_name))) {
681 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
682 "This \"uuid-name\" appeared on an "
683 "earlier \"declare\" or \"insert\" "
687 uuid_generate(&uuid);
688 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &uuid, false);
689 json_object_put(result, "uuid", json_string_create_nocopy(
690 xasprintf(UUID_FMT, UUID_ARGS(&uuid))));
694 static struct ovsdb_error *
695 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
696 struct json *result UNUSED)
698 const struct json *comment;
700 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
704 ovsdb_txn_add_comment(x->txn, json_string(comment));