1 /* Copyright (c) 2009 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "jsonrpc-server.h"
25 #include "ovsdb-error.h"
26 #include "ovsdb-parser.h"
28 #include "reconnect.h"
33 #include "transaction.h"
36 #define THIS_MODULE VLM_ovsdb_jsonrpc_server
39 struct ovsdb_jsonrpc_session;
42 static void ovsdb_jsonrpc_session_create_active(struct ovsdb_jsonrpc_server *,
44 static void ovsdb_jsonrpc_session_create_passive(struct ovsdb_jsonrpc_server *,
46 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_server *);
47 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_server *);
50 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
51 struct json *id, struct json *params);
52 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
53 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
54 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
55 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
56 static void ovsdb_jsonrpc_trigger_complete_done(
57 struct ovsdb_jsonrpc_session *);
60 static struct json *ovsdb_jsonrpc_monitor_create(
61 struct ovsdb_jsonrpc_session *, struct json *params);
62 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
63 struct ovsdb_jsonrpc_session *,
64 struct json_array *params,
65 const struct json *request_id);
66 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
68 /* JSON-RPC database server. */
70 struct ovsdb_jsonrpc_server {
73 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
74 unsigned int n_sessions, max_sessions;
75 unsigned int max_triggers;
77 struct pstream **listeners;
78 size_t n_listeners, allocated_listeners;
81 struct ovsdb_jsonrpc_server *
82 ovsdb_jsonrpc_server_create(struct ovsdb *db)
84 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
86 server->max_sessions = 64;
87 server->max_triggers = 64;
88 list_init(&server->sessions);
93 ovsdb_jsonrpc_server_listen(struct ovsdb_jsonrpc_server *svr, const char *name)
95 struct pstream *pstream;
98 error = pstream_open(name, &pstream);
103 if (svr->n_listeners >= svr->allocated_listeners) {
104 svr->listeners = x2nrealloc(svr->listeners, &svr->allocated_listeners,
105 sizeof *svr->listeners);
107 svr->listeners[svr->n_listeners++] = pstream;
112 ovsdb_jsonrpc_server_connect(struct ovsdb_jsonrpc_server *svr,
115 ovsdb_jsonrpc_session_create_active(svr, name);
119 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
123 /* Accept new connections. */
124 for (i = 0; i < svr->n_listeners && svr->n_sessions < svr->max_sessions;) {
125 struct pstream *listener = svr->listeners[i];
126 struct stream *stream;
129 error = pstream_accept(listener, &stream);
131 ovsdb_jsonrpc_session_create_passive(svr, stream);
132 } else if (error == EAGAIN) {
135 VLOG_WARN("%s: accept failed: %s",
136 pstream_get_name(listener), strerror(error));
137 pstream_close(listener);
138 svr->listeners[i] = svr->listeners[--svr->n_listeners];
142 /* Handle each session. */
143 ovsdb_jsonrpc_session_run_all(svr);
147 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
149 if (svr->n_sessions < svr->max_sessions) {
152 for (i = 0; i < svr->n_listeners; i++) {
153 pstream_wait(svr->listeners[i]);
157 ovsdb_jsonrpc_session_wait_all(svr);
160 /* JSON-RPC database server session. */
162 struct ovsdb_jsonrpc_session {
163 struct ovsdb_jsonrpc_server *server;
164 struct list node; /* Element in server's sessions list. */
167 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
168 struct list completions; /* Completed triggers. */
171 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
173 /* Connecting and reconnecting. */
174 struct reconnect *reconnect; /* For back-off. */
175 bool active; /* Active or passive connection? */
177 struct stream *stream; /* Only if active == false and rpc == NULL. */
180 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
181 static void ovsdb_jsonrpc_session_disconnect(struct ovsdb_jsonrpc_session *s);
182 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
183 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
184 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
185 struct jsonrpc_msg *);
186 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
187 struct jsonrpc_msg *);
189 static struct ovsdb_jsonrpc_session *
190 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_server *svr,
191 const char *name, bool active)
193 struct ovsdb_jsonrpc_session *s;
195 s = xzalloc(sizeof *s);
197 list_push_back(&svr->sessions, &s->node);
198 hmap_init(&s->triggers);
199 hmap_init(&s->monitors);
200 list_init(&s->completions);
201 s->reconnect = reconnect_create(time_msec());
202 reconnect_set_name(s->reconnect, name);
203 reconnect_enable(s->reconnect, time_msec());
212 ovsdb_jsonrpc_session_create_active(struct ovsdb_jsonrpc_server *svr,
215 ovsdb_jsonrpc_session_create(svr, name, true);
219 ovsdb_jsonrpc_session_create_passive(struct ovsdb_jsonrpc_server *svr,
220 struct stream *stream)
222 struct ovsdb_jsonrpc_session *s;
224 s = ovsdb_jsonrpc_session_create(svr, stream_get_name(stream), false);
225 reconnect_connected(s->reconnect, time_msec());
226 s->rpc = jsonrpc_open(stream);
230 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
232 ovsdb_jsonrpc_session_disconnect(s);
233 list_remove(&s->node);
234 s->server->n_sessions--;
238 ovsdb_jsonrpc_session_disconnect(struct ovsdb_jsonrpc_session *s)
240 reconnect_disconnected(s->reconnect, time_msec(), 0);
242 jsonrpc_error(s->rpc, EOF);
243 ovsdb_jsonrpc_trigger_complete_all(s);
244 ovsdb_jsonrpc_monitor_remove_all(s);
245 jsonrpc_close(s->rpc);
247 } else if (s->stream) {
248 stream_close(s->stream);
254 ovsdb_jsonrpc_session_connect(struct ovsdb_jsonrpc_session *s)
256 ovsdb_jsonrpc_session_disconnect(s);
258 int error = stream_open(reconnect_get_name(s->reconnect), &s->stream);
260 reconnect_connect_failed(s->reconnect, time_msec(), error);
262 reconnect_connecting(s->reconnect, time_msec());
268 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
271 struct jsonrpc_msg *msg;
276 ovsdb_jsonrpc_trigger_complete_done(s);
278 if (!jsonrpc_get_backlog(s->rpc) && !jsonrpc_recv(s->rpc, &msg)) {
279 reconnect_received(s->reconnect, time_msec());
280 if (msg->type == JSONRPC_REQUEST) {
281 ovsdb_jsonrpc_session_got_request(s, msg);
282 } else if (msg->type == JSONRPC_NOTIFY) {
283 ovsdb_jsonrpc_session_got_notify(s, msg);
284 } else if (msg->type == JSONRPC_REPLY
285 && msg->id && msg->id->type == JSON_STRING
286 && !strcmp(msg->id->u.string, "echo")) {
287 /* It's a reply to our echo request. Ignore it. */
289 VLOG_WARN("%s: received unexpected %s message",
290 jsonrpc_get_name(s->rpc),
291 jsonrpc_msg_type_to_string(msg->type));
292 jsonrpc_error(s->rpc, EPROTO);
293 jsonrpc_msg_destroy(msg);
297 error = jsonrpc_get_status(s->rpc);
300 ovsdb_jsonrpc_session_disconnect(s);
305 } else if (s->stream) {
306 int error = stream_connect(s->stream);
308 reconnect_connected(s->reconnect, time_msec());
309 s->rpc = jsonrpc_open(s->stream);
311 } else if (error != EAGAIN) {
312 reconnect_connect_failed(s->reconnect, time_msec(), error);
313 stream_close(s->stream);
318 switch (reconnect_run(s->reconnect, time_msec())) {
319 case RECONNECT_CONNECT:
320 ovsdb_jsonrpc_session_connect(s);
323 case RECONNECT_DISCONNECT:
324 ovsdb_jsonrpc_session_disconnect(s);
327 case RECONNECT_PROBE:
330 struct jsonrpc_msg *request;
332 params = json_array_create_empty();
333 request = jsonrpc_create_request("echo", params);
334 json_destroy(request->id);
335 request->id = json_string_create("echo");
336 jsonrpc_send(s->rpc, request);
340 return s->active || s->rpc ? 0 : ETIMEDOUT;
344 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_server *svr)
346 struct ovsdb_jsonrpc_session *s, *next;
348 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
350 int error = ovsdb_jsonrpc_session_run(s);
352 ovsdb_jsonrpc_session_close(s);
358 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
361 jsonrpc_wait(s->rpc);
362 if (!jsonrpc_get_backlog(s->rpc)) {
363 jsonrpc_recv_wait(s->rpc);
365 } else if (s->stream) {
366 stream_connect_wait(s->stream);
368 reconnect_wait(s->reconnect, time_msec());
372 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_server *svr)
374 struct ovsdb_jsonrpc_session *s;
376 LIST_FOR_EACH (s, struct ovsdb_jsonrpc_session, node, &svr->sessions) {
377 ovsdb_jsonrpc_session_wait(s);
381 static struct jsonrpc_msg *
382 execute_transaction(struct ovsdb_jsonrpc_session *s,
383 struct jsonrpc_msg *request)
385 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
387 request->params = NULL;
392 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
393 struct jsonrpc_msg *request)
395 struct jsonrpc_msg *reply;
397 if (!strcmp(request->method, "transact")) {
398 reply = execute_transaction(s, request);
399 } else if (!strcmp(request->method, "monitor")) {
400 reply = jsonrpc_create_reply(
401 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
402 } else if (!strcmp(request->method, "monitor_cancel")) {
403 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
405 } else if (!strcmp(request->method, "get_schema")) {
406 reply = jsonrpc_create_reply(
407 ovsdb_schema_to_json(s->server->db->schema), request->id);
408 } else if (!strcmp(request->method, "echo")) {
409 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
411 reply = jsonrpc_create_error(json_string_create("unknown method"),
416 jsonrpc_msg_destroy(request);
417 jsonrpc_send(s->rpc, reply);
422 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
424 if (json_array(request->params)->n == 1) {
425 struct ovsdb_jsonrpc_trigger *t;
428 id = request->params->u.array.elems[0];
429 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
431 ovsdb_jsonrpc_trigger_complete(t);
437 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
438 struct jsonrpc_msg *request)
440 if (!strcmp(request->method, "cancel")) {
441 execute_cancel(s, request);
443 jsonrpc_msg_destroy(request);
446 /* JSON-RPC database server triggers.
448 * (Every transaction is treated as a trigger even if it doesn't actually have
449 * any "wait" operations.) */
451 struct ovsdb_jsonrpc_trigger {
452 struct ovsdb_trigger trigger;
453 struct ovsdb_jsonrpc_session *session;
454 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
459 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
460 struct json *id, struct json *params)
462 struct ovsdb_jsonrpc_trigger *t;
465 /* Check for duplicate ID. */
466 hash = json_hash(id, 0);
467 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
469 jsonrpc_send(s->rpc, jsonrpc_create_error(
470 json_string_create("duplicate request ID"), id));
472 json_destroy(params);
476 /* Insert into trigger table. */
477 t = xmalloc(sizeof *t);
478 ovsdb_trigger_init(s->server->db,
479 &t->trigger, params, &s->completions,
483 hmap_insert(&s->triggers, &t->hmap_node, hash);
485 /* Complete early if possible. */
486 if (ovsdb_trigger_is_complete(&t->trigger)) {
487 ovsdb_jsonrpc_trigger_complete(t);
491 static struct ovsdb_jsonrpc_trigger *
492 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
493 const struct json *id, size_t hash)
495 struct ovsdb_jsonrpc_trigger *t;
497 HMAP_FOR_EACH_WITH_HASH (t, struct ovsdb_jsonrpc_trigger, hmap_node, hash,
499 if (json_equal(t->id, id)) {
508 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
510 struct ovsdb_jsonrpc_session *s = t->session;
512 if (s->rpc && !jsonrpc_get_status(s->rpc)) {
513 struct jsonrpc_msg *reply;
516 result = ovsdb_trigger_steal_result(&t->trigger);
518 reply = jsonrpc_create_reply(result, t->id);
520 reply = jsonrpc_create_error(json_string_create("canceled"),
523 jsonrpc_send(s->rpc, reply);
527 ovsdb_trigger_destroy(&t->trigger);
528 hmap_remove(&s->triggers, &t->hmap_node);
533 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
535 struct ovsdb_jsonrpc_trigger *t, *next;
536 HMAP_FOR_EACH_SAFE (t, next, struct ovsdb_jsonrpc_trigger, hmap_node,
538 ovsdb_jsonrpc_trigger_complete(t);
543 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
545 while (!list_is_empty(&s->completions)) {
546 struct ovsdb_jsonrpc_trigger *t
547 = CONTAINER_OF(s->completions.next,
548 struct ovsdb_jsonrpc_trigger, trigger.node);
549 ovsdb_jsonrpc_trigger_complete(t);
553 /* JSON-RPC database table monitors. */
555 enum ovsdb_jsonrpc_monitor_selection {
556 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
557 OJMS_INSERT = 1 << 1, /* New rows. */
558 OJMS_DELETE = 1 << 2, /* Deleted rows. */
559 OJMS_MODIFY = 1 << 3 /* Modified rows. */
562 struct ovsdb_jsonrpc_monitor_table {
563 const struct ovsdb_table *table;
564 enum ovsdb_jsonrpc_monitor_selection select;
565 struct ovsdb_column_set columns;
568 struct ovsdb_jsonrpc_monitor {
569 struct ovsdb_replica replica;
570 struct ovsdb_jsonrpc_session *session;
571 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
573 struct json *monitor_id;
574 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
577 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
579 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
580 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
581 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
582 static struct json *ovsdb_jsonrpc_monitor_get_initial(
583 const struct ovsdb_jsonrpc_monitor *);
586 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
588 const struct json *json;
590 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
591 return json ? json_boolean(json) : default_value;
594 struct ovsdb_jsonrpc_monitor *
595 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
596 const struct json *monitor_id)
598 struct ovsdb_jsonrpc_monitor *m;
600 HMAP_FOR_EACH_WITH_HASH (m, struct ovsdb_jsonrpc_monitor, node,
601 json_hash(monitor_id, 0), &s->monitors) {
602 if (json_equal(m->monitor_id, monitor_id)) {
611 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
614 struct ovsdb_jsonrpc_monitor *m = NULL;
615 struct json *monitor_id, *monitor_requests;
616 struct ovsdb_error *error = NULL;
617 struct shash_node *node;
620 if (json_array(params)->n != 2) {
621 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
624 monitor_id = params->u.array.elems[0];
625 monitor_requests = params->u.array.elems[1];
626 if (monitor_requests->type != JSON_OBJECT) {
627 error = ovsdb_syntax_error(monitor_requests, NULL,
628 "monitor-requests must be object");
632 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
633 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
637 m = xzalloc(sizeof *m);
638 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
639 ovsdb_add_replica(s->server->db, &m->replica);
641 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
642 m->monitor_id = json_clone(monitor_id);
643 shash_init(&m->tables);
645 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
646 const struct ovsdb_table *table;
647 struct ovsdb_jsonrpc_monitor_table *mt;
648 const struct json *columns_json, *select_json;
649 struct ovsdb_parser parser;
651 table = ovsdb_get_table(s->server->db, node->name);
653 error = ovsdb_syntax_error(NULL, NULL,
654 "no table named %s", node->name);
658 mt = xzalloc(sizeof *mt);
660 mt->select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
661 ovsdb_column_set_init(&mt->columns);
662 shash_add(&m->tables, table->schema->name, mt);
664 ovsdb_parser_init(&parser, node->data, "table %s", node->name);
665 columns_json = ovsdb_parser_member(&parser, "columns",
666 OP_ARRAY | OP_OPTIONAL);
667 select_json = ovsdb_parser_member(&parser, "select",
668 OP_OBJECT | OP_OPTIONAL);
669 error = ovsdb_parser_finish(&parser);
675 error = ovsdb_column_set_from_json(columns_json, table,
681 struct shash_node *node;
683 SHASH_FOR_EACH (node, &table->schema->columns) {
684 const struct ovsdb_column *column = node->data;
685 if (column->index != OVSDB_COL_UUID) {
686 ovsdb_column_set_add(&mt->columns, column);
693 ovsdb_parser_init(&parser, select_json, "table %s select",
694 table->schema->name);
695 if (parse_bool(&parser, "initial", true)) {
696 mt->select |= OJMS_INITIAL;
698 if (parse_bool(&parser, "insert", true)) {
699 mt->select |= OJMS_INSERT;
701 if (parse_bool(&parser, "delete", true)) {
702 mt->select |= OJMS_DELETE;
704 if (parse_bool(&parser, "modify", true)) {
705 mt->select |= OJMS_MODIFY;
707 error = ovsdb_parser_finish(&parser);
714 return ovsdb_jsonrpc_monitor_get_initial(m);
718 ovsdb_remove_replica(s->server->db, &m->replica);
721 json = ovsdb_error_to_json(error);
722 ovsdb_error_destroy(error);
726 static struct jsonrpc_msg *
727 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
728 struct json_array *params,
729 const struct json *request_id)
731 if (params->n != 1) {
732 return jsonrpc_create_error(json_string_create("invalid parameters"),
735 struct ovsdb_jsonrpc_monitor *m;
737 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
739 return jsonrpc_create_error(json_string_create("unknown monitor"),
742 ovsdb_remove_replica(s->server->db, &m->replica);
743 return jsonrpc_create_reply(json_object_create(), request_id);
749 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
751 struct ovsdb_jsonrpc_monitor *m, *next;
753 HMAP_FOR_EACH_SAFE (m, next,
754 struct ovsdb_jsonrpc_monitor, node, &s->monitors) {
755 ovsdb_remove_replica(s->server->db, &m->replica);
759 static struct ovsdb_jsonrpc_monitor *
760 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
762 assert(replica->class == &ovsdb_jsonrpc_replica_class);
763 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
766 struct ovsdb_jsonrpc_monitor_aux {
767 bool initial; /* Sending initial contents of table? */
768 const struct ovsdb_jsonrpc_monitor *monitor;
769 struct json *json; /* JSON for the whole transaction. */
772 struct ovsdb_jsonrpc_monitor_table *mt;
773 struct json *table_json; /* JSON for table's transaction. */
777 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
778 const struct ovsdb_row *new,
781 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
782 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
783 struct ovsdb_table *table = new ? new->table : old->table;
784 enum ovsdb_jsonrpc_monitor_selection type;
785 struct json *old_json, *new_json;
786 struct json *row_json;
787 char uuid[UUID_LEN + 1];
791 if (!aux->mt || table != aux->mt->table) {
792 aux->mt = shash_find_data(&m->tables, table->schema->name);
793 aux->table_json = NULL;
795 /* We don't care about rows in this table at all. Tell the caller
801 type = (aux->initial ? OJMS_INITIAL
805 if (!(aux->mt->select & type)) {
806 /* We don't care about this type of change (but do want to be called
807 * back for changes to other rows in the same table). */
811 old_json = new_json = NULL;
813 for (i = 0; i < aux->mt->columns.n_columns; i++) {
814 const struct ovsdb_column *column = aux->mt->columns.columns[i];
815 unsigned int idx = column->index;
816 bool changed = false;
818 if (type == OJMS_MODIFY) {
819 changed = !ovsdb_datum_equals(&old->fields[idx],
820 &new->fields[idx], &column->type);
821 n_changed += changed;
823 if (changed || type == OJMS_DELETE) {
825 old_json = json_object_create();
827 json_object_put(old_json, column->name,
828 ovsdb_datum_to_json(&old->fields[idx],
831 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
833 new_json = json_object_create();
835 json_object_put(new_json, column->name,
836 ovsdb_datum_to_json(&new->fields[idx],
840 if ((type == OJMS_MODIFY && !n_changed) || (!old_json && !new_json)) {
841 /* No reportable changes. */
842 json_destroy(old_json);
843 json_destroy(new_json);
847 /* Create JSON object for transaction overall. */
849 aux->json = json_object_create();
852 /* Create JSON object for transaction on this table. */
853 if (!aux->table_json) {
854 aux->table_json = json_object_create();
855 json_object_put(aux->json, aux->mt->table->schema->name,
859 /* Create JSON object for transaction on this row. */
860 row_json = json_object_create();
862 json_object_put(row_json, "old", old_json);
865 json_object_put(row_json, "new", new_json);
868 /* Add JSON row to JSON table. */
869 snprintf(uuid, sizeof uuid,
870 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
871 json_object_put(aux->table_json, uuid, row_json);
877 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
878 const struct ovsdb_jsonrpc_monitor *m,
881 aux->initial = initial;
885 aux->table_json = NULL;
888 static struct ovsdb_error *
889 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
890 const struct ovsdb_txn *txn, bool durable UNUSED)
892 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
893 struct ovsdb_jsonrpc_monitor_aux aux;
895 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
896 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
898 struct jsonrpc_msg *msg;
901 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
903 msg = jsonrpc_create_notify("update", params);
904 jsonrpc_send(aux.monitor->session->rpc, msg);
911 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
913 struct ovsdb_jsonrpc_monitor_aux aux;
914 struct shash_node *node;
916 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
917 SHASH_FOR_EACH (node, &m->tables) {
918 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
920 if (mt->select & OJMS_INITIAL) {
921 struct ovsdb_row *row;
923 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
925 ovsdb_jsonrpc_monitor_change_cb(NULL, row, &aux);
929 return aux.json ? aux.json : json_object_create();
933 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
935 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
936 struct shash_node *node;
938 json_destroy(m->monitor_id);
939 SHASH_FOR_EACH (node, &m->tables) {
940 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
941 ovsdb_column_set_destroy(&mt->columns);
944 shash_destroy(&m->tables);
945 hmap_remove(&m->session->monitors, &m->node);
949 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
950 ovsdb_jsonrpc_monitor_commit,
951 ovsdb_jsonrpc_monitor_destroy