ovsdb: Fix bug in "wait" command implementation.
[sliver-openvswitch.git] / ovsdb / execution.c
index 7cf45f6..7ce9a3f 100644 (file)
@@ -56,7 +56,6 @@ 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;
-static ovsdb_operation_executor ovsdb_execute_declare;
 static ovsdb_operation_executor ovsdb_execute_comment;
 
 static ovsdb_operation_executor *
@@ -76,7 +75,6 @@ lookup_executor(const char *name)
         { "wait", ovsdb_execute_wait },
         { "commit", ovsdb_execute_commit },
         { "abort", ovsdb_execute_abort },
-        { "declare", ovsdb_execute_declare },
         { "comment", ovsdb_execute_comment },
     };
 
@@ -101,10 +99,19 @@ ovsdb_execute(struct ovsdb *db, const struct json *params,
     size_t n_operations;
     size_t i;
 
-    if (params->type != JSON_ARRAY) {
+    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)) {
         struct ovsdb_error *error;
 
-        error = ovsdb_syntax_error(params, NULL, "array expected");
+        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");
+        }
+
         results = ovsdb_error_to_json(error);
         ovsdb_error_destroy(error);
         return results;
@@ -119,9 +126,9 @@ ovsdb_execute(struct ovsdb *db, const struct json *params,
     results = NULL;
 
     results = json_array_create_empty();
-    n_operations = params->u.array.n;
+    n_operations = params->u.array.n - 1;
     error = NULL;
-    for (i = 0; i < n_operations; i++) {
+    for (i = 1; i <= n_operations; i++) {
         struct json *operation = params->u.array.elems[i];
         struct ovsdb_error *parse_error;
         struct ovsdb_parser parser;
@@ -130,7 +137,7 @@ ovsdb_execute(struct ovsdb *db, const struct json *params,
 
         /* Parse and execute operation. */
         ovsdb_parser_init(&parser, operation,
-                          "ovsdb operation %zu of %zu", i + 1, n_operations);
+                          "ovsdb operation %zu of %zu", i, n_operations);
         op = ovsdb_parser_member(&parser, "op", OP_ID);
         result = json_object_create();
         if (op) {
@@ -197,7 +204,7 @@ exit:
 
 struct ovsdb_error *
 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
-                     struct json *result UNUSED)
+                     struct json *result OVS_UNUSED)
 {
     const struct json *durable;
 
@@ -209,9 +216,9 @@ ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
 }
 
 static struct ovsdb_error *
-ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
-                    struct ovsdb_parser *parser UNUSED,
-                    struct json *result UNUSED)
+ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
+                    struct ovsdb_parser *parser OVS_UNUSED,
+                    struct json *result OVS_UNUSED)
 {
     return ovsdb_error("aborted", "aborted by request");
 }
@@ -240,7 +247,7 @@ parse_table(struct ovsdb_execution *x,
 static WARN_UNUSED_RESULT struct ovsdb_error *
 parse_row(struct ovsdb_parser *parser, const char *member,
           const struct ovsdb_table *table,
-          const struct ovsdb_symbol_table *symtab,
+          struct ovsdb_symbol_table *symtab,
           struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
 {
     struct ovsdb_error *error;
@@ -285,20 +292,14 @@ ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
     if (uuid_name) {
         struct ovsdb_symbol *symbol;
 
-        symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name));
-        if (symbol) {
-            if (symbol->used) {
-                return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
-                                          "This \"uuid-name\" appeared on an "
-                                          "earlier \"insert\" operation.");
-            }
-            row_uuid = symbol->uuid;
-            symbol->used = true;
-        } else {
-            uuid_generate(&row_uuid);
-            ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
-                                   &row_uuid, true);
+        symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
+        if (symbol->used) {
+            return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
+                                      "This \"uuid-name\" appeared on an "
+                                      "earlier \"insert\" operation.");
         }
+        row_uuid = symbol->uuid;
+        symbol->used = true;
     } else {
         uuid_generate(&row_uuid);
     }
@@ -578,7 +579,7 @@ ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
 
 static struct ovsdb_error *
 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
-                   struct json *result UNUSED)
+                   struct json *result OVS_UNUSED)
 {
     struct ovsdb_table *table;
     const struct json *timeout, *where, *columns_json, *until, *rows;
@@ -628,7 +629,6 @@ ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
         /* Parse "rows" into 'expected'. */
         ovsdb_row_hash_init(&expected, &columns);
         for (i = 0; i < rows->u.array.n; i++) {
-            struct ovsdb_error *error;
             struct ovsdb_row *row;
 
             row = ovsdb_row_create(table);
@@ -689,38 +689,9 @@ ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
     return error;
 }
 
-static struct ovsdb_error *
-ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser,
-                      struct json *result)
-{
-    const struct json *uuid_name;
-    struct uuid uuid;
-
-    uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID);
-    if (!uuid_name) {
-        return NULL;
-    }
-
-    if (ovsdb_symbol_table_get(x->symtab, json_string(uuid_name))) {
-        return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
-                                  "This \"uuid-name\" appeared on an "
-                                  "earlier \"declare\" or \"insert\" "
-                                  "operation.");
-    }
-
-    uuid_generate(&uuid);
-    ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &uuid, false);
-    json_object_put(result, "uuid",
-                    json_array_create_2(
-                        json_string_create("uuid"),
-                        json_string_create_nocopy(
-                            xasprintf(UUID_FMT, UUID_ARGS(&uuid)))));
-    return NULL;
-}
-
 static struct ovsdb_error *
 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
-                      struct json *result UNUSED)
+                      struct json *result OVS_UNUSED)
 {
     const struct json *comment;