2013-11-25 23:38:48 -08:00
|
|
|
/* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
|
2009-11-04 15:11:44 -08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "column.h"
|
|
|
|
#include "condition.h"
|
2009-11-13 13:37:55 -08:00
|
|
|
#include "file.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
#include "json.h"
|
2009-12-16 10:49:31 -08:00
|
|
|
#include "mutation.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
#include "ovsdb-data.h"
|
|
|
|
#include "ovsdb-error.h"
|
|
|
|
#include "ovsdb-parser.h"
|
|
|
|
#include "ovsdb.h"
|
|
|
|
#include "query.h"
|
|
|
|
#include "row.h"
|
2011-07-26 10:24:17 -07:00
|
|
|
#include "server.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
#include "table.h"
|
|
|
|
#include "timeval.h"
|
|
|
|
#include "transaction.h"
|
|
|
|
|
|
|
|
struct ovsdb_execution {
|
|
|
|
struct ovsdb *db;
|
2011-07-26 10:24:17 -07:00
|
|
|
const struct ovsdb_session *session;
|
2009-11-04 15:11:44 -08:00
|
|
|
struct ovsdb_txn *txn;
|
|
|
|
struct ovsdb_symbol_table *symtab;
|
|
|
|
bool durable;
|
|
|
|
|
|
|
|
/* Triggers. */
|
|
|
|
long long int elapsed_msec;
|
|
|
|
long long int timeout_msec;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
|
|
|
|
struct ovsdb_parser *,
|
|
|
|
struct json *result);
|
|
|
|
|
|
|
|
static ovsdb_operation_executor ovsdb_execute_insert;
|
|
|
|
static ovsdb_operation_executor ovsdb_execute_select;
|
|
|
|
static ovsdb_operation_executor ovsdb_execute_update;
|
2009-12-16 10:49:31 -08:00
|
|
|
static ovsdb_operation_executor ovsdb_execute_mutate;
|
2009-11-04 15:11:44 -08:00
|
|
|
static ovsdb_operation_executor ovsdb_execute_delete;
|
|
|
|
static ovsdb_operation_executor ovsdb_execute_wait;
|
|
|
|
static ovsdb_operation_executor ovsdb_execute_commit;
|
|
|
|
static ovsdb_operation_executor ovsdb_execute_abort;
|
2009-12-16 13:30:53 -08:00
|
|
|
static ovsdb_operation_executor ovsdb_execute_comment;
|
2011-07-26 10:24:17 -07:00
|
|
|
static ovsdb_operation_executor ovsdb_execute_assert;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
|
|
static ovsdb_operation_executor *
|
|
|
|
lookup_executor(const char *name)
|
|
|
|
{
|
|
|
|
struct ovsdb_operation {
|
|
|
|
const char *name;
|
|
|
|
ovsdb_operation_executor *executor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ovsdb_operation operations[] = {
|
|
|
|
{ "insert", ovsdb_execute_insert },
|
|
|
|
{ "select", ovsdb_execute_select },
|
|
|
|
{ "update", ovsdb_execute_update },
|
2009-12-16 10:49:31 -08:00
|
|
|
{ "mutate", ovsdb_execute_mutate },
|
2009-11-04 15:11:44 -08:00
|
|
|
{ "delete", ovsdb_execute_delete },
|
|
|
|
{ "wait", ovsdb_execute_wait },
|
|
|
|
{ "commit", ovsdb_execute_commit },
|
|
|
|
{ "abort", ovsdb_execute_abort },
|
2009-12-16 13:30:53 -08:00
|
|
|
{ "comment", ovsdb_execute_comment },
|
2011-07-26 10:24:17 -07:00
|
|
|
{ "assert", ovsdb_execute_assert },
|
2009-11-04 15:11:44 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(operations); i++) {
|
|
|
|
const struct ovsdb_operation *c = &operations[i];
|
|
|
|
if (!strcmp(c->name, name)) {
|
|
|
|
return c->executor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct json *
|
2011-07-26 10:24:17 -07:00
|
|
|
ovsdb_execute(struct ovsdb *db, const struct ovsdb_session *session,
|
|
|
|
const struct json *params,
|
2009-11-04 15:11:44 -08:00
|
|
|
long long int elapsed_msec, long long int *timeout_msec)
|
|
|
|
{
|
|
|
|
struct ovsdb_execution x;
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
struct json *results;
|
|
|
|
size_t n_operations;
|
|
|
|
size_t i;
|
|
|
|
|
2010-02-09 10:17:58 -08:00
|
|
|
if (params->type != JSON_ARRAY
|
|
|
|
|| !params->u.array.n
|
|
|
|
|| params->u.array.elems[0]->type != JSON_STRING
|
|
|
|
|| strcmp(params->u.array.elems[0]->u.string, db->schema->name)) {
|
|
|
|
if (params->type != JSON_ARRAY) {
|
|
|
|
error = ovsdb_syntax_error(params, NULL, "array expected");
|
|
|
|
} else {
|
|
|
|
error = ovsdb_syntax_error(params, NULL, "database name expected "
|
|
|
|
"as first parameter");
|
|
|
|
}
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
results = ovsdb_error_to_json(error);
|
|
|
|
ovsdb_error_destroy(error);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
x.db = db;
|
2011-07-26 10:24:17 -07:00
|
|
|
x.session = session;
|
2009-11-04 15:11:44 -08:00
|
|
|
x.txn = ovsdb_txn_create(db);
|
|
|
|
x.symtab = ovsdb_symbol_table_create();
|
|
|
|
x.durable = false;
|
|
|
|
x.elapsed_msec = elapsed_msec;
|
|
|
|
x.timeout_msec = LLONG_MAX;
|
|
|
|
results = NULL;
|
|
|
|
|
|
|
|
results = json_array_create_empty();
|
2010-02-09 10:17:58 -08:00
|
|
|
n_operations = params->u.array.n - 1;
|
2009-11-04 15:11:44 -08:00
|
|
|
error = NULL;
|
2010-02-09 10:17:58 -08:00
|
|
|
for (i = 1; i <= n_operations; i++) {
|
2009-11-04 15:11:44 -08:00
|
|
|
struct json *operation = params->u.array.elems[i];
|
|
|
|
struct ovsdb_error *parse_error;
|
|
|
|
struct ovsdb_parser parser;
|
|
|
|
struct json *result;
|
|
|
|
const struct json *op;
|
|
|
|
|
|
|
|
/* Parse and execute operation. */
|
|
|
|
ovsdb_parser_init(&parser, operation,
|
2013-11-25 23:38:48 -08:00
|
|
|
"ovsdb operation %"PRIuSIZE" of %"PRIuSIZE, i, n_operations);
|
2009-11-04 15:11:44 -08:00
|
|
|
op = ovsdb_parser_member(&parser, "op", OP_ID);
|
|
|
|
result = json_object_create();
|
|
|
|
if (op) {
|
|
|
|
const char *op_name = json_string(op);
|
|
|
|
ovsdb_operation_executor *executor = lookup_executor(op_name);
|
|
|
|
if (executor) {
|
|
|
|
error = executor(&x, &parser, result);
|
|
|
|
} else {
|
2009-11-06 15:33:25 -08:00
|
|
|
ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
|
|
|
|
op_name);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
} else {
|
2012-11-06 13:14:55 -08:00
|
|
|
ovs_assert(ovsdb_parser_has_error(&parser));
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A parse error overrides any other error.
|
|
|
|
* An error overrides any other result. */
|
|
|
|
parse_error = ovsdb_parser_finish(&parser);
|
|
|
|
if (parse_error) {
|
|
|
|
ovsdb_error_destroy(error);
|
|
|
|
error = parse_error;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
json_destroy(result);
|
|
|
|
result = ovsdb_error_to_json(error);
|
|
|
|
}
|
|
|
|
if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
|
|
|
|
&& timeout_msec) {
|
|
|
|
ovsdb_txn_abort(x.txn);
|
|
|
|
*timeout_msec = x.timeout_msec;
|
2010-02-02 14:40:25 -08:00
|
|
|
|
|
|
|
json_destroy(result);
|
2009-11-04 15:11:44 -08:00
|
|
|
json_destroy(results);
|
2010-02-02 14:40:25 -08:00
|
|
|
results = NULL;
|
|
|
|
goto exit;
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add result to array. */
|
|
|
|
json_array_add(results, result);
|
|
|
|
if (error) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error) {
|
2009-11-13 13:37:55 -08:00
|
|
|
error = ovsdb_txn_commit(x.txn, x.durable);
|
2009-11-04 15:11:44 -08:00
|
|
|
if (error) {
|
|
|
|
json_array_add(results, ovsdb_error_to_json(error));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ovsdb_txn_abort(x.txn);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (json_array(results)->n < n_operations) {
|
|
|
|
json_array_add(results, json_null_create());
|
|
|
|
}
|
|
|
|
|
2010-02-02 14:40:25 -08:00
|
|
|
exit:
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_error_destroy(error);
|
|
|
|
ovsdb_symbol_table_destroy(x.symtab);
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:58:10 -07:00
|
|
|
static struct ovsdb_error *
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
2010-02-11 11:11:23 -08:00
|
|
|
struct json *result OVS_UNUSED)
|
2009-11-04 15:11:44 -08:00
|
|
|
{
|
|
|
|
const struct json *durable;
|
|
|
|
|
|
|
|
durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
|
|
|
|
if (durable && json_boolean(durable)) {
|
|
|
|
x->durable = true;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ovsdb_error *
|
2010-02-11 11:11:23 -08:00
|
|
|
ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
|
|
|
|
struct ovsdb_parser *parser OVS_UNUSED,
|
|
|
|
struct json *result OVS_UNUSED)
|
2009-11-04 15:11:44 -08:00
|
|
|
{
|
|
|
|
return ovsdb_error("aborted", "aborted by request");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ovsdb_table *
|
|
|
|
parse_table(struct ovsdb_execution *x,
|
|
|
|
struct ovsdb_parser *parser, const char *member)
|
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
|
|
|
const char *table_name;
|
|
|
|
const struct json *json;
|
|
|
|
|
|
|
|
json = ovsdb_parser_member(parser, member, OP_ID);
|
|
|
|
if (!json) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
table_name = json_string(json);
|
|
|
|
|
|
|
|
table = shash_find_data(&x->db->tables, table_name);
|
|
|
|
if (!table) {
|
|
|
|
ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WARN_UNUSED_RESULT struct ovsdb_error *
|
2011-03-09 12:42:46 -08:00
|
|
|
parse_row(const struct json *json, const struct ovsdb_table *table,
|
2010-02-08 16:03:21 -08:00
|
|
|
struct ovsdb_symbol_table *symtab,
|
2009-11-04 15:11:44 -08:00
|
|
|
struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
|
|
|
|
{
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
struct ovsdb_row *row;
|
|
|
|
|
|
|
|
*rowp = NULL;
|
|
|
|
|
|
|
|
if (!table) {
|
|
|
|
return OVSDB_BUG("null table");
|
|
|
|
}
|
|
|
|
if (!json) {
|
2011-03-09 12:42:46 -08:00
|
|
|
return OVSDB_BUG("null row");
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
row = ovsdb_row_create(table);
|
|
|
|
error = ovsdb_row_from_json(row, json, symtab, columns);
|
|
|
|
if (error) {
|
|
|
|
ovsdb_row_destroy(row);
|
|
|
|
return error;
|
|
|
|
} else {
|
|
|
|
*rowp = row;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:58:10 -07:00
|
|
|
static struct ovsdb_error *
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
|
|
|
struct json *result)
|
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
|
|
|
struct ovsdb_row *row = NULL;
|
2011-03-09 12:42:46 -08:00
|
|
|
const struct json *uuid_name, *row_json;
|
2009-11-04 15:11:44 -08:00
|
|
|
struct ovsdb_error *error;
|
2009-12-01 16:35:33 -08:00
|
|
|
struct uuid row_uuid;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
|
|
table = parse_table(x, parser, "table");
|
|
|
|
uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
|
2011-03-09 12:42:46 -08:00
|
|
|
row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
|
2009-11-04 15:11:44 -08:00
|
|
|
error = ovsdb_parser_get_error(parser);
|
2011-03-09 12:42:46 -08:00
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
2009-12-01 16:35:33 -08:00
|
|
|
|
|
|
|
if (uuid_name) {
|
2009-12-07 11:47:48 -08:00
|
|
|
struct ovsdb_symbol *symbol;
|
|
|
|
|
2010-02-08 16:03:21 -08:00
|
|
|
symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
|
2011-02-28 12:43:15 -08:00
|
|
|
if (symbol->created) {
|
2010-02-08 16:03:21 -08:00
|
|
|
return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
|
|
|
|
"This \"uuid-name\" appeared on an "
|
|
|
|
"earlier \"insert\" operation.");
|
2009-12-07 11:47:48 -08:00
|
|
|
}
|
2010-02-08 16:03:21 -08:00
|
|
|
row_uuid = symbol->uuid;
|
2011-02-28 12:43:15 -08:00
|
|
|
symbol->created = true;
|
2009-12-07 11:47:48 -08:00
|
|
|
} else {
|
|
|
|
uuid_generate(&row_uuid);
|
2009-12-01 16:35:33 -08:00
|
|
|
}
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
if (!error) {
|
2011-03-09 12:42:46 -08:00
|
|
|
error = parse_row(row_json, table, x->symtab, &row, NULL);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
2010-02-08 14:09:36 -08:00
|
|
|
if (!error) {
|
|
|
|
/* Check constraints for columns not included in "row", in case the
|
|
|
|
* default values do not satisfy the constraints. We could check only
|
|
|
|
* the columns that have their default values by supplying an
|
|
|
|
* ovsdb_column_set to parse_row() above, but I suspect that this is
|
|
|
|
* cheaper. */
|
|
|
|
const struct shash_node *node;
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &table->schema->columns) {
|
|
|
|
const struct ovsdb_column *column = node->data;
|
|
|
|
const struct ovsdb_datum *datum = &row->fields[column->index];
|
|
|
|
|
|
|
|
/* If there are 0 keys or pairs, there's nothing to check.
|
|
|
|
* If there is 1, it might be a default value.
|
|
|
|
* If there are more, it can't be a default value, so the value has
|
|
|
|
* already been checked. */
|
|
|
|
if (datum->n == 1) {
|
|
|
|
error = ovsdb_datum_check_constraints(datum, &column->type);
|
|
|
|
if (error) {
|
|
|
|
ovsdb_row_destroy(row);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
if (!error) {
|
2009-12-01 16:35:33 -08:00
|
|
|
*ovsdb_row_get_uuid_rw(row) = row_uuid;
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_txn_row_insert(x->txn, row);
|
|
|
|
json_object_put(result, "uuid",
|
|
|
|
ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
|
|
|
|
&ovsdb_type_uuid));
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:58:10 -07:00
|
|
|
static struct ovsdb_error *
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
|
|
|
struct json *result)
|
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
|
|
|
const struct json *where, *columns_json, *sort_json;
|
|
|
|
struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
|
|
|
|
struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
|
|
|
|
struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
|
|
|
table = parse_table(x, parser, "table");
|
|
|
|
where = ovsdb_parser_member(parser, "where", OP_ARRAY);
|
|
|
|
columns_json = ovsdb_parser_member(parser, "columns",
|
|
|
|
OP_ARRAY | OP_OPTIONAL);
|
|
|
|
sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
|
|
|
|
|
|
|
|
error = ovsdb_parser_get_error(parser);
|
|
|
|
if (!error) {
|
|
|
|
error = ovsdb_condition_from_json(table->schema, where, x->symtab,
|
|
|
|
&condition);
|
|
|
|
}
|
|
|
|
if (!error) {
|
2011-06-01 16:14:46 -07:00
|
|
|
error = ovsdb_column_set_from_json(columns_json, table->schema,
|
|
|
|
&columns);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
if (!error) {
|
2011-06-01 16:14:46 -07:00
|
|
|
error = ovsdb_column_set_from_json(sort_json, table->schema, &sort);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
|
|
|
|
|
|
|
|
ovsdb_query_distinct(table, &condition, &columns, &rows);
|
|
|
|
ovsdb_row_set_sort(&rows, &sort);
|
|
|
|
json_object_put(result, "rows",
|
|
|
|
ovsdb_row_set_to_json(&rows, &columns));
|
|
|
|
|
|
|
|
ovsdb_row_set_destroy(&rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
ovsdb_column_set_destroy(&columns);
|
|
|
|
ovsdb_column_set_destroy(&sort);
|
|
|
|
ovsdb_condition_destroy(&condition);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct update_row_cbdata {
|
|
|
|
size_t n_matches;
|
|
|
|
struct ovsdb_txn *txn;
|
|
|
|
const struct ovsdb_row *row;
|
|
|
|
const struct ovsdb_column_set *columns;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
update_row_cb(const struct ovsdb_row *row, void *ur_)
|
|
|
|
{
|
|
|
|
struct update_row_cbdata *ur = ur_;
|
|
|
|
|
|
|
|
ur->n_matches++;
|
|
|
|
if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
|
|
|
|
ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
|
|
|
|
ur->row, ur->columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:58:10 -07:00
|
|
|
static struct ovsdb_error *
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
|
|
|
struct json *result)
|
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
2011-03-09 12:42:46 -08:00
|
|
|
const struct json *where, *row_json;
|
2009-11-04 15:11:44 -08:00
|
|
|
struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
|
|
|
|
struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
|
|
|
|
struct ovsdb_row *row = NULL;
|
|
|
|
struct update_row_cbdata ur;
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
|
|
|
table = parse_table(x, parser, "table");
|
|
|
|
where = ovsdb_parser_member(parser, "where", OP_ARRAY);
|
2011-03-09 12:42:46 -08:00
|
|
|
row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
|
2009-11-04 15:11:44 -08:00
|
|
|
error = ovsdb_parser_get_error(parser);
|
|
|
|
if (!error) {
|
2011-03-09 12:42:46 -08:00
|
|
|
error = parse_row(row_json, table, x->symtab, &row, &columns);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
2012-09-05 10:35:20 -07:00
|
|
|
if (!error) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < columns.n_columns; i++) {
|
|
|
|
const struct ovsdb_column *column = columns.columns[i];
|
|
|
|
|
|
|
|
if (!column->mutable) {
|
|
|
|
error = ovsdb_syntax_error(parser->json,
|
|
|
|
"constraint violation",
|
|
|
|
"Cannot update immutable column %s "
|
|
|
|
"in table %s.",
|
|
|
|
column->name, table->schema->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
if (!error) {
|
|
|
|
error = ovsdb_condition_from_json(table->schema, where, x->symtab,
|
|
|
|
&condition);
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
ur.n_matches = 0;
|
|
|
|
ur.txn = x->txn;
|
|
|
|
ur.row = row;
|
|
|
|
ur.columns = &columns;
|
|
|
|
ovsdb_query(table, &condition, update_row_cb, &ur);
|
|
|
|
json_object_put(result, "count", json_integer_create(ur.n_matches));
|
|
|
|
}
|
|
|
|
|
|
|
|
ovsdb_row_destroy(row);
|
|
|
|
ovsdb_column_set_destroy(&columns);
|
|
|
|
ovsdb_condition_destroy(&condition);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-12-16 10:49:31 -08:00
|
|
|
struct mutate_row_cbdata {
|
|
|
|
size_t n_matches;
|
|
|
|
struct ovsdb_txn *txn;
|
|
|
|
const struct ovsdb_mutation_set *mutations;
|
2011-05-31 12:50:57 -07:00
|
|
|
struct ovsdb_error **error;
|
2009-12-16 10:49:31 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
mutate_row_cb(const struct ovsdb_row *row, void *mr_)
|
|
|
|
{
|
|
|
|
struct mutate_row_cbdata *mr = mr_;
|
|
|
|
|
|
|
|
mr->n_matches++;
|
2011-05-31 12:50:57 -07:00
|
|
|
*mr->error = ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
|
|
|
|
mr->mutations);
|
|
|
|
return *mr->error == NULL;
|
2009-12-16 10:49:31 -08:00
|
|
|
}
|
|
|
|
|
2011-05-04 13:58:10 -07:00
|
|
|
static struct ovsdb_error *
|
2009-12-16 10:49:31 -08:00
|
|
|
ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
|
|
|
struct json *result)
|
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
|
|
|
const struct json *where;
|
|
|
|
const struct json *mutations_json;
|
|
|
|
struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
|
|
|
|
struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
|
|
|
|
struct ovsdb_row *row = NULL;
|
|
|
|
struct mutate_row_cbdata mr;
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
|
|
|
table = parse_table(x, parser, "table");
|
|
|
|
where = ovsdb_parser_member(parser, "where", OP_ARRAY);
|
|
|
|
mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
|
|
|
|
error = ovsdb_parser_get_error(parser);
|
|
|
|
if (!error) {
|
|
|
|
error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
|
|
|
|
x->symtab, &mutations);
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
error = ovsdb_condition_from_json(table->schema, where, x->symtab,
|
|
|
|
&condition);
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
mr.n_matches = 0;
|
|
|
|
mr.txn = x->txn;
|
|
|
|
mr.mutations = &mutations;
|
2011-05-31 12:50:57 -07:00
|
|
|
mr.error = &error;
|
2009-12-16 10:49:31 -08:00
|
|
|
ovsdb_query(table, &condition, mutate_row_cb, &mr);
|
|
|
|
json_object_put(result, "count", json_integer_create(mr.n_matches));
|
|
|
|
}
|
|
|
|
|
|
|
|
ovsdb_row_destroy(row);
|
|
|
|
ovsdb_mutation_set_destroy(&mutations);
|
|
|
|
ovsdb_condition_destroy(&condition);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
struct delete_row_cbdata {
|
|
|
|
size_t n_matches;
|
|
|
|
const struct ovsdb_table *table;
|
|
|
|
struct ovsdb_txn *txn;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
delete_row_cb(const struct ovsdb_row *row, void *dr_)
|
|
|
|
{
|
|
|
|
struct delete_row_cbdata *dr = dr_;
|
|
|
|
|
|
|
|
dr->n_matches++;
|
|
|
|
ovsdb_txn_row_delete(dr->txn, row);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:58:10 -07:00
|
|
|
static struct ovsdb_error *
|
2009-11-04 15:11:44 -08:00
|
|
|
ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
|
|
|
struct json *result)
|
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
|
|
|
const struct json *where;
|
|
|
|
struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
|
|
|
where = ovsdb_parser_member(parser, "where", OP_ARRAY);
|
|
|
|
table = parse_table(x, parser, "table");
|
|
|
|
error = ovsdb_parser_get_error(parser);
|
|
|
|
if (!error) {
|
|
|
|
error = ovsdb_condition_from_json(table->schema, where, x->symtab,
|
|
|
|
&condition);
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
struct delete_row_cbdata dr;
|
|
|
|
|
|
|
|
dr.n_matches = 0;
|
|
|
|
dr.table = table;
|
|
|
|
dr.txn = x->txn;
|
|
|
|
ovsdb_query(table, &condition, delete_row_cb, &dr);
|
|
|
|
|
|
|
|
json_object_put(result, "count", json_integer_create(dr.n_matches));
|
|
|
|
}
|
|
|
|
|
|
|
|
ovsdb_condition_destroy(&condition);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wait_auxdata {
|
|
|
|
struct ovsdb_row_hash *actual;
|
|
|
|
struct ovsdb_row_hash *expected;
|
|
|
|
bool *equal;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
|
|
|
|
{
|
|
|
|
struct wait_auxdata *aux = aux_;
|
|
|
|
|
|
|
|
if (ovsdb_row_hash_contains(aux->expected, row)) {
|
|
|
|
ovsdb_row_hash_insert(aux->actual, row);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
/* The query row isn't in the expected result set, so the actual and
|
|
|
|
* expected results sets definitely differ and we can short-circuit the
|
|
|
|
* rest of the query. */
|
|
|
|
*aux->equal = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ovsdb_error *
|
|
|
|
ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
2010-02-11 11:11:23 -08:00
|
|
|
struct json *result OVS_UNUSED)
|
2009-11-04 15:11:44 -08:00
|
|
|
{
|
|
|
|
struct ovsdb_table *table;
|
|
|
|
const struct json *timeout, *where, *columns_json, *until, *rows;
|
|
|
|
struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
|
|
|
|
struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
|
|
|
|
struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
|
|
|
|
struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
struct wait_auxdata aux;
|
|
|
|
long long int timeout_msec = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
|
|
|
|
where = ovsdb_parser_member(parser, "where", OP_ARRAY);
|
|
|
|
columns_json = ovsdb_parser_member(parser, "columns",
|
|
|
|
OP_ARRAY | OP_OPTIONAL);
|
|
|
|
until = ovsdb_parser_member(parser, "until", OP_STRING);
|
|
|
|
rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
|
|
|
|
table = parse_table(x, parser, "table");
|
|
|
|
error = ovsdb_parser_get_error(parser);
|
|
|
|
if (!error) {
|
|
|
|
error = ovsdb_condition_from_json(table->schema, where, x->symtab,
|
|
|
|
&condition);
|
|
|
|
}
|
|
|
|
if (!error) {
|
2011-06-01 16:14:46 -07:00
|
|
|
error = ovsdb_column_set_from_json(columns_json, table->schema,
|
|
|
|
&columns);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
if (timeout) {
|
|
|
|
timeout_msec = MIN(LLONG_MAX, json_real(timeout));
|
|
|
|
if (timeout_msec < 0) {
|
|
|
|
error = ovsdb_syntax_error(timeout, NULL,
|
|
|
|
"timeout must be nonnegative");
|
|
|
|
} else if (timeout_msec < x->timeout_msec) {
|
|
|
|
x->timeout_msec = timeout_msec;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timeout_msec = LLONG_MAX;
|
|
|
|
}
|
2014-08-28 14:40:50 +02:00
|
|
|
}
|
|
|
|
if (!error) {
|
2009-11-04 15:11:44 -08:00
|
|
|
if (strcmp(json_string(until), "==")
|
|
|
|
&& strcmp(json_string(until), "!=")) {
|
|
|
|
error = ovsdb_syntax_error(until, NULL,
|
|
|
|
"\"until\" must be \"==\" or \"!=\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
/* Parse "rows" into 'expected'. */
|
|
|
|
ovsdb_row_hash_init(&expected, &columns);
|
|
|
|
for (i = 0; i < rows->u.array.n; i++) {
|
|
|
|
struct ovsdb_row *row;
|
|
|
|
|
|
|
|
row = ovsdb_row_create(table);
|
|
|
|
error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
|
|
|
|
NULL);
|
|
|
|
if (error) {
|
2014-08-27 09:52:54 +08:00
|
|
|
ovsdb_row_destroy(row);
|
2009-11-04 15:11:44 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ovsdb_row_hash_insert(&expected, row)) {
|
|
|
|
/* XXX Perhaps we should abort with an error or log a
|
|
|
|
* warning. */
|
|
|
|
ovsdb_row_destroy(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
/* Execute query. */
|
|
|
|
bool equal = true;
|
|
|
|
ovsdb_row_hash_init(&actual, &columns);
|
|
|
|
aux.actual = &actual;
|
|
|
|
aux.expected = &expected;
|
|
|
|
aux.equal = &equal;
|
|
|
|
ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
|
|
|
|
if (equal) {
|
|
|
|
/* We know that every row in 'actual' is also in 'expected'. We
|
|
|
|
* also know that all of the rows in 'actual' are distinct and that
|
|
|
|
* all of the rows in 'expected' are distinct. Therefore, if
|
|
|
|
* 'actual' and 'expected' have the same number of rows, then they
|
|
|
|
* have the same content. */
|
|
|
|
size_t n_actual = ovsdb_row_hash_count(&actual);
|
|
|
|
size_t n_expected = ovsdb_row_hash_count(&expected);
|
|
|
|
equal = n_actual == n_expected;
|
|
|
|
}
|
|
|
|
if (!strcmp(json_string(until), "==") != equal) {
|
|
|
|
if (timeout && x->elapsed_msec >= timeout_msec) {
|
|
|
|
if (x->elapsed_msec) {
|
|
|
|
error = ovsdb_error("timed out",
|
|
|
|
"\"wait\" timed out after %lld ms",
|
|
|
|
x->elapsed_msec);
|
|
|
|
} else {
|
|
|
|
error = ovsdb_error("timed out", "\"wait\" timed out");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* ovsdb_execute() will change this, if triggers really are
|
|
|
|
* supported. */
|
|
|
|
error = ovsdb_error("not supported", "triggers not supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ovsdb_row_hash_destroy(&expected, true);
|
|
|
|
ovsdb_row_hash_destroy(&actual, false);
|
|
|
|
ovsdb_column_set_destroy(&columns);
|
|
|
|
ovsdb_condition_destroy(&condition);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2009-12-07 11:47:48 -08:00
|
|
|
|
2009-12-16 13:30:53 -08:00
|
|
|
static struct ovsdb_error *
|
|
|
|
ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
2010-02-11 11:11:23 -08:00
|
|
|
struct json *result OVS_UNUSED)
|
2009-12-16 13:30:53 -08:00
|
|
|
{
|
|
|
|
const struct json *comment;
|
|
|
|
|
|
|
|
comment = ovsdb_parser_member(parser, "comment", OP_STRING);
|
|
|
|
if (!comment) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ovsdb_txn_add_comment(x->txn, json_string(comment));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-07-26 10:24:17 -07:00
|
|
|
|
|
|
|
static struct ovsdb_error *
|
|
|
|
ovsdb_execute_assert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
|
|
|
|
struct json *result OVS_UNUSED)
|
|
|
|
{
|
|
|
|
const struct json *lock_name;
|
|
|
|
|
2011-08-05 15:56:05 -07:00
|
|
|
lock_name = ovsdb_parser_member(parser, "lock", OP_ID);
|
2011-07-26 10:24:17 -07:00
|
|
|
if (!lock_name) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x->session) {
|
|
|
|
const struct ovsdb_lock_waiter *waiter;
|
|
|
|
|
|
|
|
waiter = ovsdb_session_get_lock_waiter(x->session,
|
|
|
|
json_string(lock_name));
|
|
|
|
if (waiter && ovsdb_lock_waiter_is_owner(waiter)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ovsdb_error("not owner", "Asserted lock %s not held.",
|
|
|
|
json_string(lock_name));
|
|
|
|
}
|