These functions will be needed when we'll need to load/save
configuration of each OVSDB remote separately.

The parsing function is written in a way that it updates the
provided options and doesn't create a new structure.  This
is done in order for different callers to have their own
default values and only update them with what is provided
by the user explicitly.  For example, replication and relay
have different default probe intervals.

Signed-off-by: Ilya Maximets <[email protected]>
---
 ovsdb/jsonrpc-server.c | 66 ++++++++++++++++++++++++++++++++++++++++++
 ovsdb/jsonrpc-server.h |  6 ++++
 2 files changed, 72 insertions(+)

diff --git a/ovsdb/jsonrpc-server.c b/ovsdb/jsonrpc-server.c
index 4ea4c7a4b..51b7db886 100644
--- a/ovsdb/jsonrpc-server.c
+++ b/ovsdb/jsonrpc-server.c
@@ -219,6 +219,72 @@ ovsdb_jsonrpc_default_options(const char *target)
     return options;
 }
 
+struct json *
+ovsdb_jsonrpc_options_to_json(const struct ovsdb_jsonrpc_options *options)
+{
+    struct json *json = json_object_create();
+
+    json_object_put(json, "max-backoff",
+                    json_integer_create(options->max_backoff));
+    json_object_put(json, "inactivity-probe",
+                    json_integer_create(options->probe_interval));
+    json_object_put(json, "read-only",
+                    json_boolean_create(options->read_only));
+    json_object_put(json, "dscp", json_integer_create(options->dscp));
+    if (options->role) {
+        json_object_put(json, "role", json_string_create(options->role));
+    }
+
+    return json;
+}
+
+void
+ovsdb_jsonrpc_options_update_from_json(struct ovsdb_jsonrpc_options *options,
+                                       const struct json *json)
+{
+    const struct json *max_backoff, *probe_interval, *read_only, *dscp, *role;
+    struct ovsdb_parser parser;
+    struct ovsdb_error *error;
+
+    ovsdb_parser_init(&parser, json, "JSON-RPC options");
+
+    max_backoff = ovsdb_parser_member(&parser, "max-backoff",
+                                      OP_INTEGER | OP_OPTIONAL);
+    if (max_backoff) {
+        options->max_backoff = json_integer(max_backoff);
+    }
+
+    probe_interval = ovsdb_parser_member(&parser, "inactivity-probe",
+                                         OP_INTEGER | OP_OPTIONAL);
+    if (probe_interval) {
+        options->probe_interval = json_integer(probe_interval);
+    }
+
+    read_only = ovsdb_parser_member(&parser, "read-only",
+                                    OP_BOOLEAN | OP_OPTIONAL);
+    if (read_only) {
+        options->read_only = json_boolean(read_only);
+    }
+
+    dscp = ovsdb_parser_member(&parser, "dscp", OP_INTEGER | OP_OPTIONAL);
+    if (dscp) {
+        options->dscp = json_integer(dscp);
+    }
+
+    role = ovsdb_parser_member(&parser, "role", OP_STRING | OP_OPTIONAL);
+    if (role) {
+        options->role = nullable_xstrdup(json_string(role));
+    }
+
+    error = ovsdb_parser_finish(&parser);
+    if (error) {
+        char *s = ovsdb_error_to_string_free(error);
+
+        VLOG_WARN("%s", s);
+        free(s);
+    }
+}
+
 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
  * options in the struct ovsdb_jsonrpc_options supplied as the data values.
  *
diff --git a/ovsdb/jsonrpc-server.h b/ovsdb/jsonrpc-server.h
index e0653aa39..9c49966c1 100644
--- a/ovsdb/jsonrpc-server.h
+++ b/ovsdb/jsonrpc-server.h
@@ -42,6 +42,12 @@ struct ovsdb_jsonrpc_options {
 struct ovsdb_jsonrpc_options *
 ovsdb_jsonrpc_default_options(const char *target);
 
+struct json *ovsdb_jsonrpc_options_to_json(
+                                const struct ovsdb_jsonrpc_options *)
+    OVS_WARN_UNUSED_RESULT;
+void ovsdb_jsonrpc_options_update_from_json(struct ovsdb_jsonrpc_options *,
+                                            const struct json *);
+
 void ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *,
                                       const struct shash *);
 
-- 
2.43.0

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to