summaryrefslogtreecommitdiff
path: root/lib
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
parent62705b81108ff6d011362331847cf0ba22494779 (diff)
downloadopenvswitch-fe0fb88551b4cc5b4bee6814f1027f78c451daa2.tar.gz
ovsdb-client: Add new "restore" command.
Signed-off-by: Ben Pfaff <blp@ovn.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/ovsdb-data.c76
-rw-r--r--lib/ovsdb-data.h5
-rw-r--r--lib/ovsdb-idl.c20
3 files changed, 70 insertions, 31 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;
+}
diff --git a/lib/ovsdb-data.h b/lib/ovsdb-data.h
index c7bb095cd..84639c4a3 100644
--- a/lib/ovsdb-data.h
+++ b/lib/ovsdb-data.h
@@ -244,6 +244,11 @@ void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
const struct ovsdb_type *,
const union ovsdb_atom *range_end_atom);
+/* Transactions with named-uuid row names. */
+struct json *ovsdb_datum_to_json_with_row_names(const struct ovsdb_datum *,
+ const struct ovsdb_type *);
+char *ovsdb_data_row_name(const struct uuid *);
+
/* Type checking. */
static inline bool
ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
diff --git a/lib/ovsdb-idl.c b/lib/ovsdb-idl.c
index 96e5c1f58..29f893116 100644
--- a/lib/ovsdb-idl.c
+++ b/lib/ovsdb-idl.c
@@ -3102,22 +3102,6 @@ where_uuid_equals(const struct uuid *uuid)
xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
}
-static char *
-uuid_name_from_uuid(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;
-}
-
static const struct ovsdb_idl_row *
ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
{
@@ -3152,7 +3136,7 @@ substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
return json_array_create_2(
json_string_create("named-uuid"),
- json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
+ json_string_create_nocopy(ovsdb_data_row_name(&uuid)));
}
}
@@ -3567,7 +3551,7 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
json_object_put(op, "uuid-name",
json_string_create_nocopy(
- uuid_name_from_uuid(&row->uuid)));
+ ovsdb_data_row_name(&row->uuid)));
insert = xmalloc(sizeof *insert);
insert->dummy = row->uuid;