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;
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;
164 ovsdb_error_destroy(error);
165 json_destroy(results);
169 /* Add result to array. */
170 json_array_add(results, result);
177 error = ovsdb_txn_commit(x.txn, x.durable);
179 json_array_add(results, ovsdb_error_to_json(error));
182 ovsdb_txn_abort(x.txn);
185 while (json_array(results)->n < n_operations) {
186 json_array_add(results, json_null_create());
189 ovsdb_error_destroy(error);
190 ovsdb_symbol_table_destroy(x.symtab);
196 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
197 struct json *result UNUSED)
199 const struct json *durable;
201 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
202 if (durable && json_boolean(durable)) {
208 static struct ovsdb_error *
209 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
210 struct ovsdb_parser *parser UNUSED,
211 struct json *result UNUSED)
213 return ovsdb_error("aborted", "aborted by request");
216 static struct ovsdb_table *
217 parse_table(struct ovsdb_execution *x,
218 struct ovsdb_parser *parser, const char *member)
220 struct ovsdb_table *table;
221 const char *table_name;
222 const struct json *json;
224 json = ovsdb_parser_member(parser, member, OP_ID);
228 table_name = json_string(json);
230 table = shash_find_data(&x->db->tables, table_name);
232 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
237 static WARN_UNUSED_RESULT struct ovsdb_error *
238 parse_row(struct ovsdb_parser *parser, const char *member,
239 const struct ovsdb_table *table,
240 const struct ovsdb_symbol_table *symtab,
241 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
243 struct ovsdb_error *error;
244 const struct json *json;
245 struct ovsdb_row *row;
250 return OVSDB_BUG("null table");
252 json = ovsdb_parser_member(parser, member, OP_OBJECT);
254 return OVSDB_BUG("null row member");
257 row = ovsdb_row_create(table);
258 error = ovsdb_row_from_json(row, json, symtab, columns);
260 ovsdb_row_destroy(row);
269 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
272 struct ovsdb_table *table;
273 struct ovsdb_row *row = NULL;
274 const struct json *uuid_name;
275 struct ovsdb_error *error;
276 struct uuid row_uuid;
278 table = parse_table(x, parser, "table");
279 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
280 error = ovsdb_parser_get_error(parser);
283 struct ovsdb_symbol *symbol;
285 symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name));
288 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
289 "This \"uuid-name\" appeared on an "
290 "earlier \"insert\" operation.");
292 row_uuid = symbol->uuid;
295 uuid_generate(&row_uuid);
296 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
300 uuid_generate(&row_uuid);
304 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
307 *ovsdb_row_get_uuid_rw(row) = row_uuid;
308 ovsdb_txn_row_insert(x->txn, row);
309 json_object_put(result, "uuid",
310 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
318 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
321 struct ovsdb_table *table;
322 const struct json *where, *columns_json, *sort_json;
323 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
324 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
325 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
326 struct ovsdb_error *error;
328 table = parse_table(x, parser, "table");
329 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
330 columns_json = ovsdb_parser_member(parser, "columns",
331 OP_ARRAY | OP_OPTIONAL);
332 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
334 error = ovsdb_parser_get_error(parser);
336 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
340 error = ovsdb_column_set_from_json(columns_json, table, &columns);
343 error = ovsdb_column_set_from_json(sort_json, table, &sort);
346 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
348 ovsdb_query_distinct(table, &condition, &columns, &rows);
349 ovsdb_row_set_sort(&rows, &sort);
350 json_object_put(result, "rows",
351 ovsdb_row_set_to_json(&rows, &columns));
353 ovsdb_row_set_destroy(&rows);
356 ovsdb_column_set_destroy(&columns);
357 ovsdb_column_set_destroy(&sort);
358 ovsdb_condition_destroy(&condition);
363 struct update_row_cbdata {
365 struct ovsdb_txn *txn;
366 const struct ovsdb_row *row;
367 const struct ovsdb_column_set *columns;
371 update_row_cb(const struct ovsdb_row *row, void *ur_)
373 struct update_row_cbdata *ur = ur_;
376 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
377 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
378 ur->row, ur->columns);
385 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
388 struct ovsdb_table *table;
389 const struct json *where;
390 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
391 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
392 struct ovsdb_row *row = NULL;
393 struct update_row_cbdata ur;
394 struct ovsdb_error *error;
396 table = parse_table(x, parser, "table");
397 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
398 error = ovsdb_parser_get_error(parser);
400 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
403 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
410 ur.columns = &columns;
411 ovsdb_query(table, &condition, update_row_cb, &ur);
412 json_object_put(result, "count", json_integer_create(ur.n_matches));
415 ovsdb_row_destroy(row);
416 ovsdb_column_set_destroy(&columns);
417 ovsdb_condition_destroy(&condition);
422 struct mutate_row_cbdata {
424 struct ovsdb_txn *txn;
425 const struct ovsdb_mutation_set *mutations;
429 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
431 struct mutate_row_cbdata *mr = mr_;
434 ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
441 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
444 struct ovsdb_table *table;
445 const struct json *where;
446 const struct json *mutations_json;
447 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
448 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
449 struct ovsdb_row *row = NULL;
450 struct mutate_row_cbdata mr;
451 struct ovsdb_error *error;
453 table = parse_table(x, parser, "table");
454 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
455 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
456 error = ovsdb_parser_get_error(parser);
458 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
459 x->symtab, &mutations);
462 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
468 mr.mutations = &mutations;
469 ovsdb_query(table, &condition, mutate_row_cb, &mr);
470 json_object_put(result, "count", json_integer_create(mr.n_matches));
473 ovsdb_row_destroy(row);
474 ovsdb_mutation_set_destroy(&mutations);
475 ovsdb_condition_destroy(&condition);
480 struct delete_row_cbdata {
482 const struct ovsdb_table *table;
483 struct ovsdb_txn *txn;
487 delete_row_cb(const struct ovsdb_row *row, void *dr_)
489 struct delete_row_cbdata *dr = dr_;
492 ovsdb_txn_row_delete(dr->txn, row);
498 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
501 struct ovsdb_table *table;
502 const struct json *where;
503 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
504 struct ovsdb_error *error;
506 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
507 table = parse_table(x, parser, "table");
508 error = ovsdb_parser_get_error(parser);
510 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
514 struct delete_row_cbdata dr;
519 ovsdb_query(table, &condition, delete_row_cb, &dr);
521 json_object_put(result, "count", json_integer_create(dr.n_matches));
524 ovsdb_condition_destroy(&condition);
529 struct wait_auxdata {
530 struct ovsdb_row_hash *actual;
531 struct ovsdb_row_hash *expected;
536 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
538 struct wait_auxdata *aux = aux_;
540 if (ovsdb_row_hash_contains(aux->expected, row)) {
541 ovsdb_row_hash_insert(aux->actual, row);
544 /* The query row isn't in the expected result set, so the actual and
545 * expected results sets definitely differ and we can short-circuit the
546 * rest of the query. */
552 static struct ovsdb_error *
553 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
554 struct json *result UNUSED)
556 struct ovsdb_table *table;
557 const struct json *timeout, *where, *columns_json, *until, *rows;
558 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
559 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
560 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
561 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
562 struct ovsdb_error *error;
563 struct wait_auxdata aux;
564 long long int timeout_msec = 0;
567 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
568 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
569 columns_json = ovsdb_parser_member(parser, "columns",
570 OP_ARRAY | OP_OPTIONAL);
571 until = ovsdb_parser_member(parser, "until", OP_STRING);
572 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
573 table = parse_table(x, parser, "table");
574 error = ovsdb_parser_get_error(parser);
576 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
580 error = ovsdb_column_set_from_json(columns_json, table, &columns);
584 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
585 if (timeout_msec < 0) {
586 error = ovsdb_syntax_error(timeout, NULL,
587 "timeout must be nonnegative");
588 } else if (timeout_msec < x->timeout_msec) {
589 x->timeout_msec = timeout_msec;
592 timeout_msec = LLONG_MAX;
594 if (strcmp(json_string(until), "==")
595 && strcmp(json_string(until), "!=")) {
596 error = ovsdb_syntax_error(until, NULL,
597 "\"until\" must be \"==\" or \"!=\"");
601 /* Parse "rows" into 'expected'. */
602 ovsdb_row_hash_init(&expected, &columns);
603 for (i = 0; i < rows->u.array.n; i++) {
604 struct ovsdb_error *error;
605 struct ovsdb_row *row;
607 row = ovsdb_row_create(table);
608 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
614 if (!ovsdb_row_hash_insert(&expected, row)) {
615 /* XXX Perhaps we should abort with an error or log a
617 ovsdb_row_destroy(row);
624 ovsdb_row_hash_init(&actual, &columns);
625 aux.actual = &actual;
626 aux.expected = &expected;
628 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
630 /* We know that every row in 'actual' is also in 'expected'. We
631 * also know that all of the rows in 'actual' are distinct and that
632 * all of the rows in 'expected' are distinct. Therefore, if
633 * 'actual' and 'expected' have the same number of rows, then they
634 * have the same content. */
635 size_t n_actual = ovsdb_row_hash_count(&actual);
636 size_t n_expected = ovsdb_row_hash_count(&expected);
637 equal = n_actual == n_expected;
639 if (!strcmp(json_string(until), "==") != equal) {
640 if (timeout && x->elapsed_msec >= timeout_msec) {
641 if (x->elapsed_msec) {
642 error = ovsdb_error("timed out",
643 "\"wait\" timed out after %lld ms",
646 error = ovsdb_error("timed out", "\"wait\" timed out");
649 /* ovsdb_execute() will change this, if triggers really are
651 error = ovsdb_error("not supported", "triggers not supported");
657 ovsdb_row_hash_destroy(&expected, true);
658 ovsdb_row_hash_destroy(&actual, false);
659 ovsdb_column_set_destroy(&columns);
660 ovsdb_condition_destroy(&condition);
665 static struct ovsdb_error *
666 ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser,
669 const struct json *uuid_name;
672 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID);
677 if (ovsdb_symbol_table_get(x->symtab, json_string(uuid_name))) {
678 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
679 "This \"uuid-name\" appeared on an "
680 "earlier \"declare\" or \"insert\" "
684 uuid_generate(&uuid);
685 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &uuid, false);
686 json_object_put(result, "uuid", json_string_create_nocopy(
687 xasprintf(UUID_FMT, UUID_ARGS(&uuid))));
691 static struct ovsdb_error *
692 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
693 struct json *result UNUSED)
695 const struct json *comment;
697 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
701 ovsdb_txn_add_comment(x->txn, json_string(comment));