ovs-openflowd: Standardize on OpenFlow description option
[openvswitch] / lib / ovsdb-data.c
index e79f84997e023290d21f786914a166a604688f9c..08d623a51a967ec176b59e87baf6a006d92d72a0 100644 (file)
@@ -229,14 +229,9 @@ parse_json_pair(const struct json *json,
     return NULL;
 }
 
-static struct ovsdb_error *
-ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
-                      const struct ovsdb_symbol_table *symtab)
-    WARN_UNUSED_RESULT;
-
-static struct ovsdb_error *
+static struct ovsdb_error * WARN_UNUSED_RESULT
 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
-                      const struct ovsdb_symbol_table *symtab)
+                      struct ovsdb_symbol_table *symtab)
 {
     struct ovsdb_error *error0;
     const struct json *value;
@@ -254,18 +249,10 @@ ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
         error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
         if (!error1) {
             const char *name = json_string(value);
-            const struct ovsdb_symbol *symbol;
 
             ovsdb_error_destroy(error0);
-
-            symbol = ovsdb_symbol_table_get(symtab, name);
-            if (symbol) {
-                *uuid = symbol->uuid;
-                return NULL;
-            } else {
-                return ovsdb_syntax_error(json, NULL,
-                                          "unknown named-uuid \"%s\"", name);
-            }
+            *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
+            return NULL;
         }
         ovsdb_error_destroy(error1);
     }
@@ -276,7 +263,7 @@ ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
 static struct ovsdb_error * WARN_UNUSED_RESULT
 ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
                        const struct json *json,
-                       const struct ovsdb_symbol_table *symtab)
+                       struct ovsdb_symbol_table *symtab)
 {
     switch (type) {
     case OVSDB_TYPE_VOID:
@@ -332,7 +319,7 @@ struct ovsdb_error *
 ovsdb_atom_from_json(union ovsdb_atom *atom,
                      const struct ovsdb_base_type *base,
                      const struct json *json,
-                     const struct ovsdb_symbol_table *symtab)
+                     struct ovsdb_symbol_table *symtab)
 {
     struct ovsdb_error *error;
 
@@ -619,7 +606,10 @@ check_string_constraints(const char *s,
 
 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
  * (base->type must specify 'atom''s type.)  Returns a null pointer if the
- * constraints are met, otherwise an error that explains the violation. */
+ * constraints are met, otherwise an error that explains the violation.
+ *
+ * Checking UUID constraints is deferred to transaction commit time, so this
+ * function does nothing for UUID constraints. */
 struct ovsdb_error *
 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
                              const struct ovsdb_base_type *base)
@@ -903,30 +893,21 @@ struct ovsdb_error *
 ovsdb_datum_from_json(struct ovsdb_datum *datum,
                       const struct ovsdb_type *type,
                       const struct json *json,
-                      const struct ovsdb_symbol_table *symtab)
+                      struct ovsdb_symbol_table *symtab)
 {
     struct ovsdb_error *error;
 
-    if (ovsdb_type_is_scalar(type)) {
-        datum->n = 1;
-        datum->keys = xmalloc(sizeof *datum->keys);
-        datum->values = NULL;
-
-        error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
-                                     json, symtab);
-        if (error) {
-            free(datum->keys);
-        }
-        return error;
-    } else {
+    if (ovsdb_type_is_map(type)
+        || (json->type == JSON_ARRAY
+            && json->u.array.n > 0
+            && json->u.array.elems[0]->type == JSON_STRING
+            && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
         bool is_map = ovsdb_type_is_map(type);
         const char *class = is_map ? "map" : "set";
         const struct json *inner;
         unsigned int i;
         size_t n;
 
-        assert(is_map || ovsdb_type_is_set(type));
-
         error = unwrap_json(json, class, JSON_ARRAY, &inner);
         if (error) {
             return error;
@@ -984,6 +965,17 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum,
     error:
         ovsdb_datum_destroy(datum, type);
         return error;
+    } else {
+        datum->n = 1;
+        datum->keys = xmalloc(sizeof *datum->keys);
+        datum->values = NULL;
+
+        error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
+                                     json, symtab);
+        if (error) {
+            free(datum->keys);
+        }
+        return error;
     }
 }
 
@@ -993,7 +985,7 @@ ovsdb_datum_to_json(const struct ovsdb_datum *datum,
 {
     /* These tests somewhat tolerate a 'datum' that does not exactly match
      * 'type', in particular a datum with 'n' not in the allowed range. */
-    if (datum->n == 1 && ovsdb_type_is_scalar(type)) {
+    if (datum->n == 1 && !ovsdb_type_is_map(type)) {
         return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
     } else if (type->value.type == OVSDB_TYPE_VOID) {
         struct json **elems;
@@ -1524,7 +1516,7 @@ ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
     return shash_find_data(&symtab->sh, name);
 }
 
-void
+struct ovsdb_symbol *
 ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
                        const struct uuid *uuid, bool used)
 {
@@ -1535,6 +1527,23 @@ ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
     symbol->uuid = *uuid;
     symbol->used = used;
     shash_add(&symtab->sh, name, symbol);
+    return symbol;
+}
+
+struct ovsdb_symbol *
+ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
+                          const char *name)
+{
+    struct ovsdb_symbol *symbol;
+
+    symbol = ovsdb_symbol_table_get(symtab, name);
+    if (!symbol) {
+        struct uuid uuid;
+
+        uuid_generate(&uuid);
+        symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
+    }
+    return symbol;
 }
 \f
 /* Extracts a token from the beginning of 's' and returns a pointer just after