summaryrefslogtreecommitdiff
path: root/lib/ovsdb-data.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@ovn.org>2017-12-21 16:41:30 -0800
committerBen Pfaff <blp@ovn.org>2017-12-21 16:41:30 -0800
commitfe0fb88551b4cc5b4bee6814f1027f78c451daa2 (patch)
tree5cac6efc848ef3dba694a9219e2da8632bdcf23f /lib/ovsdb-data.c
parent62705b81108ff6d011362331847cf0ba22494779 (diff)
downloadopenvswitch-fe0fb88551b4cc5b4bee6814f1027f78c451daa2.tar.gz
ovsdb-client: Add new "restore" command.
Signed-off-by: Ben Pfaff <blp@ovn.org>
Diffstat (limited to 'lib/ovsdb-data.c')
-rw-r--r--lib/ovsdb-data.c76
1 files changed, 63 insertions, 13 deletions
diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c
index 3ddf5f5bd..cdd1bb653 100644
--- a/lib/ovsdb-data.c
+++ b/lib/ovsdb-data.c
@@ -1341,15 +1341,26 @@ ovsdb_transient_datum_from_json(struct ovsdb_datum *datum,
return ovsdb_datum_from_json(datum, &relaxed_type, json, NULL);
}
-/* Converts 'datum', of the specified 'type', to JSON format, and returns the
- * JSON. The caller is responsible for freeing the returned JSON.
- *
- * 'type' constraints on datum->n are ignored.
- *
- * Refer to RFC 7047 for the format of the JSON that this function produces. */
-struct json *
-ovsdb_datum_to_json(const struct ovsdb_datum *datum,
- const struct ovsdb_type *type)
+static struct json *
+ovsdb_base_to_json(const union ovsdb_atom *atom,
+ const struct ovsdb_base_type *base,
+ bool use_row_names)
+{
+ if (!use_row_names
+ || base->type != OVSDB_TYPE_UUID
+ || !base->u.uuid.refTableName) {
+ return ovsdb_atom_to_json(atom, base->type);
+ } else {
+ return json_array_create_2(
+ json_string_create("named-uuid"),
+ json_string_create_nocopy(ovsdb_data_row_name(&atom->uuid)));
+ }
+}
+
+static struct json *
+ovsdb_datum_to_json__(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type,
+ bool use_row_names)
{
if (ovsdb_type_is_map(type)) {
struct json **elems;
@@ -1358,26 +1369,49 @@ ovsdb_datum_to_json(const struct ovsdb_datum *datum,
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
elems[i] = json_array_create_2(
- ovsdb_atom_to_json(&datum->keys[i], type->key.type),
- ovsdb_atom_to_json(&datum->values[i], type->value.type));
+ ovsdb_base_to_json(&datum->keys[i], &type->key,
+ use_row_names),
+ ovsdb_base_to_json(&datum->values[i], &type->value,
+ use_row_names));
}
return wrap_json("map", json_array_create(elems, datum->n));
} else if (datum->n == 1) {
- return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
+ return ovsdb_base_to_json(&datum->keys[0], &type->key, use_row_names);
} else {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
+ elems[i] = ovsdb_base_to_json(&datum->keys[i], &type->key,
+ use_row_names);
}
return wrap_json("set", json_array_create(elems, datum->n));
}
}
+/* Converts 'datum', of the specified 'type', to JSON format, and returns the
+ * JSON. The caller is responsible for freeing the returned JSON.
+ *
+ * 'type' constraints on datum->n are ignored.
+ *
+ * Refer to RFC 7047 for the format of the JSON that this function produces. */
+struct json *
+ovsdb_datum_to_json(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type)
+{
+ return ovsdb_datum_to_json__(datum, type, false);
+}
+
+struct json *
+ovsdb_datum_to_json_with_row_names(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type)
+{
+ return ovsdb_datum_to_json__(datum, type, true);
+}
+
static const char *
skip_spaces(const char *p)
{
@@ -2150,3 +2184,19 @@ ovsdb_atom_range_check_size(int64_t range_start, int64_t range_end)
}
return NULL;
}
+
+char *
+ovsdb_data_row_name(const struct uuid *uuid)
+{
+ char *name;
+ char *p;
+
+ name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
+ for (p = name; *p != '\0'; p++) {
+ if (*p == '-') {
+ *p = '_';
+ }
+ }
+
+ return name;
+}