+/* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
+ * 'options'. */
+static void
+ovsdb_jsonrpc_session_set_all_options(
+ struct ovsdb_jsonrpc_remote *remote,
+ const struct ovsdb_jsonrpc_options *options)
+{
+ struct ovsdb_jsonrpc_session *s;
+
+ LIST_FOR_EACH (s, node, &remote->sessions) {
+ ovsdb_jsonrpc_session_set_options(s, options);
+ }
+}
+
+static void
+ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
+ struct shash *shash)
+{
+ const struct ovsdb_jsonrpc_session *s;
+ const struct jsonrpc_session *js;
+ const char *name;
+ struct ovsdb_jsonrpc_remote_status *status;
+ struct reconnect_stats rstats;
+
+ /* We only look at the first session in the list. There should be only one
+ * node in the list for outbound connections. We don't track status for
+ * each individual inbound connection if someone configures the DB that
+ * way. Since outbound connections are the norm, this is fine. */
+ if (list_is_empty(&remote->sessions)) {
+ return;
+ }
+ s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
+ js = s->js;
+ if (!js) {
+ return;
+ }
+ name = jsonrpc_session_get_name(js);
+
+ status = xzalloc(sizeof *status);
+ shash_add(shash, name, status);
+
+ status->is_connected = jsonrpc_session_is_connected(js);
+ status->last_error = jsonrpc_session_get_status(js);
+
+ jsonrpc_session_get_reconnect_stats(js, &rstats);
+ status->state = rstats.state;
+ status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
+ ? UINT_MAX : rstats.msec_since_connect / 1000;
+ status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
+ ? UINT_MAX : rstats.msec_since_disconnect / 1000;
+}
+
+static const char *
+get_db_name(const struct ovsdb_jsonrpc_session *s)
+{
+ return s->remote->server->db->schema->name;
+}
+
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
+ const struct jsonrpc_msg *request)
+{
+ struct json_array *params;
+ const char *want_db_name;
+ const char *have_db_name;
+ struct ovsdb_error *error;
+ struct jsonrpc_msg *reply;
+
+ params = json_array(request->params);
+ if (!params->n || params->elems[0]->type != JSON_STRING) {
+ error = ovsdb_syntax_error(
+ request->params, NULL,
+ "%s request params must begin with <db-name>", request->method);
+ goto error;
+ }
+
+ want_db_name = params->elems[0]->u.string;
+ have_db_name = get_db_name(s);
+ if (strcmp(want_db_name, have_db_name)) {
+ error = ovsdb_syntax_error(
+ request->params, "unknown database",
+ "%s request specifies unknown database %s",
+ request->method, want_db_name);
+ goto error;
+ }
+
+ return NULL;
+
+error:
+ reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+ ovsdb_error_destroy(error);
+ return reply;
+}
+
+static struct jsonrpc_msg *
+execute_transaction(struct ovsdb_jsonrpc_session *s,
+ struct jsonrpc_msg *request)
+{
+ ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
+ request->id = NULL;
+ request->params = NULL;
+ jsonrpc_msg_destroy(request);
+ return NULL;
+}
+
+static void
+ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
+ struct jsonrpc_msg *request)
+{
+ struct jsonrpc_msg *reply;
+
+ if (!strcmp(request->method, "transact")) {
+ reply = ovsdb_jsonrpc_check_db_name(s, request);
+ if (!reply) {
+ reply = execute_transaction(s, request);
+ }
+ } else if (!strcmp(request->method, "monitor")) {
+ reply = ovsdb_jsonrpc_check_db_name(s, request);
+ if (!reply) {
+ reply = jsonrpc_create_reply(
+ ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
+ }
+ } else if (!strcmp(request->method, "monitor_cancel")) {
+ reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
+ request->id);
+ } else if (!strcmp(request->method, "get_schema")) {
+ reply = ovsdb_jsonrpc_check_db_name(s, request);
+ if (!reply) {
+ reply = jsonrpc_create_reply(
+ ovsdb_schema_to_json(s->remote->server->db->schema),
+ request->id);
+ }
+ } else if (!strcmp(request->method, "list_dbs")) {
+ reply = jsonrpc_create_reply(
+ json_array_create_1(json_string_create(get_db_name(s))),
+ request->id);
+ } else if (!strcmp(request->method, "echo")) {
+ reply = jsonrpc_create_reply(json_clone(request->params), request->id);
+ } else {
+ reply = jsonrpc_create_error(json_string_create("unknown method"),
+ request->id);
+ }
+
+ if (reply) {
+ jsonrpc_msg_destroy(request);
+ jsonrpc_session_send(s->js, reply);
+ }
+}
+
+static void
+execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
+{
+ if (json_array(request->params)->n == 1) {
+ struct ovsdb_jsonrpc_trigger *t;
+ struct json *id;
+
+ id = request->params->u.array.elems[0];
+ t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
+ if (t) {
+ ovsdb_jsonrpc_trigger_complete(t);