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"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
33 #include "transaction.h"
35 struct ovsdb_execution {
37 struct ovsdb_txn *txn;
38 struct ovsdb_symbol_table *symtab;
42 long long int elapsed_msec;
43 long long int timeout_msec;
46 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
47 struct ovsdb_parser *,
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;
59 static ovsdb_operation_executor *
60 lookup_executor(const char *name)
62 struct ovsdb_operation {
64 ovsdb_operation_executor *executor;
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 },
80 for (i = 0; i < ARRAY_SIZE(operations); i++) {
81 const struct ovsdb_operation *c = &operations[i];
82 if (!strcmp(c->name, name)) {
90 ovsdb_execute(struct ovsdb *db, const struct json *params,
91 long long int elapsed_msec, long long int *timeout_msec)
93 struct ovsdb_execution x;
94 struct ovsdb_error *error;
99 if (params->type != JSON_ARRAY) {
100 struct ovsdb_error *error;
102 error = ovsdb_syntax_error(params, NULL, "array expected");
103 results = ovsdb_error_to_json(error);
104 ovsdb_error_destroy(error);
109 x.txn = ovsdb_txn_create(db);
110 x.symtab = ovsdb_symbol_table_create();
112 x.elapsed_msec = elapsed_msec;
113 x.timeout_msec = LLONG_MAX;
116 results = json_array_create_empty();
117 n_operations = params->u.array.n;
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;
124 const struct json *op;
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();
132 const char *op_name = json_string(op);
133 ovsdb_operation_executor *executor = lookup_executor(op_name);
135 error = executor(&x, &parser, result);
137 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
141 assert(ovsdb_parser_has_error(&parser));
144 /* A parse error overrides any other error.
145 * An error overrides any other result. */
146 parse_error = ovsdb_parser_finish(&parser);
148 ovsdb_error_destroy(error);
152 json_destroy(result);
153 result = ovsdb_error_to_json(error);
155 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
157 ovsdb_txn_abort(x.txn);
158 *timeout_msec = x.timeout_msec;
159 ovsdb_error_destroy(error);
160 json_destroy(results);
164 /* Add result to array. */
165 json_array_add(results, result);
172 error = ovsdb_txn_commit(x.txn, x.durable);
174 json_array_add(results, ovsdb_error_to_json(error));
177 ovsdb_txn_abort(x.txn);
180 while (json_array(results)->n < n_operations) {
181 json_array_add(results, json_null_create());
184 ovsdb_error_destroy(error);
185 ovsdb_symbol_table_destroy(x.symtab);
191 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
192 struct json *result UNUSED)
194 const struct json *durable;
196 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
197 if (durable && json_boolean(durable)) {
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)
208 return ovsdb_error("aborted", "aborted by request");
211 static struct ovsdb_table *
212 parse_table(struct ovsdb_execution *x,
213 struct ovsdb_parser *parser, const char *member)
215 struct ovsdb_table *table;
216 const char *table_name;
217 const struct json *json;
219 json = ovsdb_parser_member(parser, member, OP_ID);
223 table_name = json_string(json);
225 table = shash_find_data(&x->db->tables, table_name);
227 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
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)
238 struct ovsdb_error *error;
239 const struct json *json;
240 struct ovsdb_row *row;
245 return OVSDB_BUG("null table");
247 json = ovsdb_parser_member(parser, member, OP_OBJECT);
249 return OVSDB_BUG("null row member");
252 row = ovsdb_row_create(table);
253 error = ovsdb_row_from_json(row, json, symtab, columns);
255 ovsdb_row_destroy(row);
264 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
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);
278 struct ovsdb_symbol *symbol;
280 symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name));
283 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
284 "This \"uuid-name\" appeared on an "
285 "earlier \"insert\" operation.");
287 row_uuid = symbol->uuid;
290 uuid_generate(&row_uuid);
291 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
295 uuid_generate(&row_uuid);
299 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
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],
313 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
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);
329 error = ovsdb_parser_get_error(parser);
331 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
335 error = ovsdb_column_set_from_json(columns_json, table, &columns);
338 error = ovsdb_column_set_from_json(sort_json, table, &sort);
341 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
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));
348 ovsdb_row_set_destroy(&rows);
351 ovsdb_column_set_destroy(&columns);
352 ovsdb_column_set_destroy(&sort);
353 ovsdb_condition_destroy(&condition);
358 struct update_row_cbdata {
360 struct ovsdb_txn *txn;
361 const struct ovsdb_row *row;
362 const struct ovsdb_column_set *columns;
366 update_row_cb(const struct ovsdb_row *row, void *ur_)
368 struct update_row_cbdata *ur = ur_;
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);
380 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
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;
391 table = parse_table(x, parser, "table");
392 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
393 error = ovsdb_parser_get_error(parser);
395 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
398 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
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));
410 ovsdb_row_destroy(row);
411 ovsdb_column_set_destroy(&columns);
412 ovsdb_condition_destroy(&condition);
417 struct delete_row_cbdata {
419 const struct ovsdb_table *table;
420 struct ovsdb_txn *txn;
424 delete_row_cb(const struct ovsdb_row *row, void *dr_)
426 struct delete_row_cbdata *dr = dr_;
429 ovsdb_txn_row_delete(dr->txn, row);
435 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
438 struct ovsdb_table *table;
439 const struct json *where;
440 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
441 struct ovsdb_error *error;
443 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
444 table = parse_table(x, parser, "table");
445 error = ovsdb_parser_get_error(parser);
447 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
451 struct delete_row_cbdata dr;
456 ovsdb_query(table, &condition, delete_row_cb, &dr);
458 json_object_put(result, "count", json_integer_create(dr.n_matches));
461 ovsdb_condition_destroy(&condition);
466 struct wait_auxdata {
467 struct ovsdb_row_hash *actual;
468 struct ovsdb_row_hash *expected;
473 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
475 struct wait_auxdata *aux = aux_;
477 if (ovsdb_row_hash_contains(aux->expected, row)) {
478 ovsdb_row_hash_insert(aux->actual, row);
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. */
489 static struct ovsdb_error *
490 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
491 struct json *result UNUSED)
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;
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);
513 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
517 error = ovsdb_column_set_from_json(columns_json, table, &columns);
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;
529 timeout_msec = LLONG_MAX;
531 if (strcmp(json_string(until), "==")
532 && strcmp(json_string(until), "!=")) {
533 error = ovsdb_syntax_error(until, NULL,
534 "\"until\" must be \"==\" or \"!=\"");
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;
544 row = ovsdb_row_create(table);
545 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
551 if (!ovsdb_row_hash_insert(&expected, row)) {
552 /* XXX Perhaps we should abort with an error or log a
554 ovsdb_row_destroy(row);
561 ovsdb_row_hash_init(&actual, &columns);
562 aux.actual = &actual;
563 aux.expected = &expected;
565 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
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;
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",
583 error = ovsdb_error("timed out", "\"wait\" timed out");
586 /* ovsdb_execute() will change this, if triggers really are
588 error = ovsdb_error("not supported", "triggers not supported");
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);
602 static struct ovsdb_error *
603 ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser,
606 const struct json *uuid_name;
609 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID);
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\" "
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))));