diff mbox series

[ovs-dev,07/22] jsonrpc-server: Add functions to convert jsonrpc options to/from json.

Message ID 20231214010431.1664005-8-i.maximets@ovn.org
State Superseded
Delegated to: Ilya Maximets
Headers show
Series [ovs-dev,01/22] ovsdb-server.at: Enbale debug logs in active-backup tests. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation fail test: fail

Commit Message

Ilya Maximets Dec. 14, 2023, 1:04 a.m. UTC
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 <i.maximets@ovn.org>
---
 ovsdb/jsonrpc-server.c | 66 ++++++++++++++++++++++++++++++++++++++++++
 ovsdb/jsonrpc-server.h |  6 ++++
 2 files changed, 72 insertions(+)

Comments

Mike Pattrick Jan. 8, 2024, 6:51 p.m. UTC | #1
On Wed, Dec 13, 2023 at 8:05 PM Ilya Maximets <i.maximets@ovn.org> wrote:
>
> 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 <i.maximets@ovn.org>

Acked-by: Mike Pattrick <mkp@redhat.com>
diff mbox series

Patch

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 *);