1 /* Copyright (c) 2009, 2010 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"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
29 #include "reconnect.h"
34 #include "transaction.h"
37 #define THIS_MODULE VLM_ovsdb_jsonrpc_server
40 struct ovsdb_jsonrpc_remote;
41 struct ovsdb_jsonrpc_session;
43 /* Message rate-limiting. */
44 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
47 static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
48 struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
49 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
50 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
51 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
54 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
55 struct json *id, struct json *params);
56 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
57 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
58 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
59 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
60 static void ovsdb_jsonrpc_trigger_complete_done(
61 struct ovsdb_jsonrpc_session *);
64 static struct json *ovsdb_jsonrpc_monitor_create(
65 struct ovsdb_jsonrpc_session *, struct json *params);
66 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
67 struct ovsdb_jsonrpc_session *,
68 struct json_array *params,
69 const struct json *request_id);
70 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
72 /* JSON-RPC database server. */
74 struct ovsdb_jsonrpc_server {
76 unsigned int n_sessions, max_sessions;
77 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
80 /* A configured remote. This is either a passive stream listener plus a list
81 * of the currently connected sessions, or a list of exactly one active
83 struct ovsdb_jsonrpc_remote {
84 struct ovsdb_jsonrpc_server *server;
85 struct pstream *listener; /* Listener, if passive. */
86 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
89 static void ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *,
91 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
93 struct ovsdb_jsonrpc_server *
94 ovsdb_jsonrpc_server_create(struct ovsdb *db)
96 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
98 server->max_sessions = 64;
99 shash_init(&server->remotes);
104 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
106 struct shash_node *node, *next;
108 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
109 ovsdb_jsonrpc_server_del_remote(node);
111 shash_destroy(&svr->remotes);
115 /* Sets 'svr''s current set of remotes to the names in 'new_remotes'. The data
116 * values in 'new_remotes' are ignored.
118 * A remote is an active or passive stream connection method, e.g. "pssl:" or
121 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
122 const struct shash *new_remotes)
124 struct shash_node *node, *next;
126 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
127 if (!shash_find(new_remotes, node->name)) {
128 ovsdb_jsonrpc_server_del_remote(node);
131 SHASH_FOR_EACH (node, new_remotes) {
132 if (!shash_find(&svr->remotes, node->name)) {
133 ovsdb_jsonrpc_server_add_remote(svr, node->name);
139 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
142 struct ovsdb_jsonrpc_remote *remote;
143 struct pstream *listener;
146 error = pstream_open(name, &listener);
147 if (error && error != EAFNOSUPPORT) {
148 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
152 remote = xmalloc(sizeof *remote);
153 remote->server = svr;
154 remote->listener = listener;
155 list_init(&remote->sessions);
156 shash_add(&svr->remotes, name, remote);
159 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
164 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
166 struct ovsdb_jsonrpc_remote *remote = node->data;
168 ovsdb_jsonrpc_session_close_all(remote);
169 pstream_close(remote->listener);
170 shash_delete(&remote->server->remotes, node);
175 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
177 struct shash_node *node;
179 SHASH_FOR_EACH (node, &svr->remotes) {
180 struct ovsdb_jsonrpc_remote *remote = node->data;
182 if (remote->listener && svr->n_sessions < svr->max_sessions) {
183 struct stream *stream;
186 error = pstream_accept(remote->listener, &stream);
188 struct jsonrpc_session *js;
189 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
190 ovsdb_jsonrpc_session_create(remote, js);
191 } else if (error != EAGAIN) {
192 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
193 pstream_get_name(remote->listener),
198 ovsdb_jsonrpc_session_run_all(remote);
203 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
205 struct shash_node *node;
207 SHASH_FOR_EACH (node, &svr->remotes) {
208 struct ovsdb_jsonrpc_remote *remote = node->data;
210 if (remote->listener && svr->n_sessions < svr->max_sessions) {
211 pstream_wait(remote->listener);
214 ovsdb_jsonrpc_session_wait_all(remote);
218 /* JSON-RPC database server session. */
220 struct ovsdb_jsonrpc_session {
221 struct ovsdb_jsonrpc_remote *remote;
222 struct list node; /* Element in remote's sessions list. */
225 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
226 struct list completions; /* Completed triggers. */
229 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
231 /* Network connectivity. */
232 struct jsonrpc_session *js; /* JSON-RPC session. */
233 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
236 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
237 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
238 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
239 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
240 struct jsonrpc_msg *);
241 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
242 struct jsonrpc_msg *);
244 static struct ovsdb_jsonrpc_session *
245 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
246 struct jsonrpc_session *js)
248 struct ovsdb_jsonrpc_session *s;
250 s = xzalloc(sizeof *s);
252 list_push_back(&remote->sessions, &s->node);
253 hmap_init(&s->triggers);
254 hmap_init(&s->monitors);
255 list_init(&s->completions);
257 s->js_seqno = jsonrpc_session_get_seqno(js);
259 remote->server->n_sessions++;
265 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
267 ovsdb_jsonrpc_monitor_remove_all(s);
268 jsonrpc_session_close(s->js);
269 list_remove(&s->node);
270 s->remote->server->n_sessions--;
275 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
277 jsonrpc_session_run(s->js);
278 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
279 s->js_seqno = jsonrpc_session_get_seqno(s->js);
280 ovsdb_jsonrpc_trigger_complete_all(s);
281 ovsdb_jsonrpc_monitor_remove_all(s);
284 ovsdb_jsonrpc_trigger_complete_done(s);
286 if (!jsonrpc_session_get_backlog(s->js)) {
287 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
289 if (msg->type == JSONRPC_REQUEST) {
290 ovsdb_jsonrpc_session_got_request(s, msg);
291 } else if (msg->type == JSONRPC_NOTIFY) {
292 ovsdb_jsonrpc_session_got_notify(s, msg);
294 VLOG_WARN("%s: received unexpected %s message",
295 jsonrpc_session_get_name(s->js),
296 jsonrpc_msg_type_to_string(msg->type));
297 jsonrpc_session_force_reconnect(s->js);
298 jsonrpc_msg_destroy(msg);
302 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
306 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
308 struct ovsdb_jsonrpc_session *s, *next;
310 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
312 int error = ovsdb_jsonrpc_session_run(s);
314 ovsdb_jsonrpc_session_close(s);
320 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
322 jsonrpc_session_wait(s->js);
323 if (!jsonrpc_session_get_backlog(s->js)) {
324 jsonrpc_session_recv_wait(s->js);
329 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
331 struct ovsdb_jsonrpc_session *s;
333 LIST_FOR_EACH (s, struct ovsdb_jsonrpc_session, node, &remote->sessions) {
334 ovsdb_jsonrpc_session_wait(s);
339 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
341 struct ovsdb_jsonrpc_session *s, *next;
343 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
345 ovsdb_jsonrpc_session_close(s);
349 static struct jsonrpc_msg *
350 execute_transaction(struct ovsdb_jsonrpc_session *s,
351 struct jsonrpc_msg *request)
353 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
355 request->params = NULL;
356 jsonrpc_msg_destroy(request);
361 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
362 struct jsonrpc_msg *request)
364 struct jsonrpc_msg *reply;
366 if (!strcmp(request->method, "transact")) {
367 reply = execute_transaction(s, request);
368 } else if (!strcmp(request->method, "monitor")) {
369 reply = jsonrpc_create_reply(
370 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
371 } else if (!strcmp(request->method, "monitor_cancel")) {
372 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
374 } else if (!strcmp(request->method, "get_schema")) {
375 reply = jsonrpc_create_reply(
376 ovsdb_schema_to_json(s->remote->server->db->schema), request->id);
377 } else if (!strcmp(request->method, "echo")) {
378 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
380 reply = jsonrpc_create_error(json_string_create("unknown method"),
385 jsonrpc_msg_destroy(request);
386 jsonrpc_session_send(s->js, reply);
391 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
393 if (json_array(request->params)->n == 1) {
394 struct ovsdb_jsonrpc_trigger *t;
397 id = request->params->u.array.elems[0];
398 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
400 ovsdb_jsonrpc_trigger_complete(t);
406 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
407 struct jsonrpc_msg *request)
409 if (!strcmp(request->method, "cancel")) {
410 execute_cancel(s, request);
412 jsonrpc_msg_destroy(request);
415 /* JSON-RPC database server triggers.
417 * (Every transaction is treated as a trigger even if it doesn't actually have
418 * any "wait" operations.) */
420 struct ovsdb_jsonrpc_trigger {
421 struct ovsdb_trigger trigger;
422 struct ovsdb_jsonrpc_session *session;
423 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
428 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
429 struct json *id, struct json *params)
431 struct ovsdb_jsonrpc_trigger *t;
434 /* Check for duplicate ID. */
435 hash = json_hash(id, 0);
436 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
438 struct jsonrpc_msg *msg;
440 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
442 jsonrpc_session_send(s->js, msg);
444 json_destroy(params);
448 /* Insert into trigger table. */
449 t = xmalloc(sizeof *t);
450 ovsdb_trigger_init(s->remote->server->db,
451 &t->trigger, params, &s->completions,
455 hmap_insert(&s->triggers, &t->hmap_node, hash);
457 /* Complete early if possible. */
458 if (ovsdb_trigger_is_complete(&t->trigger)) {
459 ovsdb_jsonrpc_trigger_complete(t);
463 static struct ovsdb_jsonrpc_trigger *
464 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
465 const struct json *id, size_t hash)
467 struct ovsdb_jsonrpc_trigger *t;
469 HMAP_FOR_EACH_WITH_HASH (t, struct ovsdb_jsonrpc_trigger, hmap_node, hash,
471 if (json_equal(t->id, id)) {
480 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
482 struct ovsdb_jsonrpc_session *s = t->session;
484 if (jsonrpc_session_is_connected(s->js)) {
485 struct jsonrpc_msg *reply;
488 result = ovsdb_trigger_steal_result(&t->trigger);
490 reply = jsonrpc_create_reply(result, t->id);
492 reply = jsonrpc_create_error(json_string_create("canceled"),
495 jsonrpc_session_send(s->js, reply);
499 ovsdb_trigger_destroy(&t->trigger);
500 hmap_remove(&s->triggers, &t->hmap_node);
505 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
507 struct ovsdb_jsonrpc_trigger *t, *next;
508 HMAP_FOR_EACH_SAFE (t, next, struct ovsdb_jsonrpc_trigger, hmap_node,
510 ovsdb_jsonrpc_trigger_complete(t);
515 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
517 while (!list_is_empty(&s->completions)) {
518 struct ovsdb_jsonrpc_trigger *t
519 = CONTAINER_OF(s->completions.next,
520 struct ovsdb_jsonrpc_trigger, trigger.node);
521 ovsdb_jsonrpc_trigger_complete(t);
525 /* JSON-RPC database table monitors. */
527 enum ovsdb_jsonrpc_monitor_selection {
528 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
529 OJMS_INSERT = 1 << 1, /* New rows. */
530 OJMS_DELETE = 1 << 2, /* Deleted rows. */
531 OJMS_MODIFY = 1 << 3 /* Modified rows. */
534 struct ovsdb_jsonrpc_monitor_table {
535 const struct ovsdb_table *table;
536 enum ovsdb_jsonrpc_monitor_selection select;
537 struct ovsdb_column_set columns;
540 struct ovsdb_jsonrpc_monitor {
541 struct ovsdb_replica replica;
542 struct ovsdb_jsonrpc_session *session;
543 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
545 struct json *monitor_id;
546 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
549 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
551 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
552 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
553 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
554 static struct json *ovsdb_jsonrpc_monitor_get_initial(
555 const struct ovsdb_jsonrpc_monitor *);
558 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
560 const struct json *json;
562 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
563 return json ? json_boolean(json) : default_value;
566 struct ovsdb_jsonrpc_monitor *
567 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
568 const struct json *monitor_id)
570 struct ovsdb_jsonrpc_monitor *m;
572 HMAP_FOR_EACH_WITH_HASH (m, struct ovsdb_jsonrpc_monitor, node,
573 json_hash(monitor_id, 0), &s->monitors) {
574 if (json_equal(m->monitor_id, monitor_id)) {
583 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
586 struct ovsdb_jsonrpc_monitor *m = NULL;
587 struct json *monitor_id, *monitor_requests;
588 struct ovsdb_error *error = NULL;
589 struct shash_node *node;
592 if (json_array(params)->n != 2) {
593 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
596 monitor_id = params->u.array.elems[0];
597 monitor_requests = params->u.array.elems[1];
598 if (monitor_requests->type != JSON_OBJECT) {
599 error = ovsdb_syntax_error(monitor_requests, NULL,
600 "monitor-requests must be object");
604 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
605 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
609 m = xzalloc(sizeof *m);
610 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
611 ovsdb_add_replica(s->remote->server->db, &m->replica);
613 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
614 m->monitor_id = json_clone(monitor_id);
615 shash_init(&m->tables);
617 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
618 const struct ovsdb_table *table;
619 struct ovsdb_jsonrpc_monitor_table *mt;
620 const struct json *columns_json, *select_json;
621 struct ovsdb_parser parser;
623 table = ovsdb_get_table(s->remote->server->db, node->name);
625 error = ovsdb_syntax_error(NULL, NULL,
626 "no table named %s", node->name);
630 mt = xzalloc(sizeof *mt);
632 mt->select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
633 ovsdb_column_set_init(&mt->columns);
634 shash_add(&m->tables, table->schema->name, mt);
636 ovsdb_parser_init(&parser, node->data, "table %s", node->name);
637 columns_json = ovsdb_parser_member(&parser, "columns",
638 OP_ARRAY | OP_OPTIONAL);
639 select_json = ovsdb_parser_member(&parser, "select",
640 OP_OBJECT | OP_OPTIONAL);
641 error = ovsdb_parser_finish(&parser);
647 error = ovsdb_column_set_from_json(columns_json, table,
653 struct shash_node *node;
655 SHASH_FOR_EACH (node, &table->schema->columns) {
656 const struct ovsdb_column *column = node->data;
657 if (column->index != OVSDB_COL_UUID) {
658 ovsdb_column_set_add(&mt->columns, column);
665 ovsdb_parser_init(&parser, select_json, "table %s select",
666 table->schema->name);
667 if (parse_bool(&parser, "initial", true)) {
668 mt->select |= OJMS_INITIAL;
670 if (parse_bool(&parser, "insert", true)) {
671 mt->select |= OJMS_INSERT;
673 if (parse_bool(&parser, "delete", true)) {
674 mt->select |= OJMS_DELETE;
676 if (parse_bool(&parser, "modify", true)) {
677 mt->select |= OJMS_MODIFY;
679 error = ovsdb_parser_finish(&parser);
686 return ovsdb_jsonrpc_monitor_get_initial(m);
690 ovsdb_remove_replica(s->remote->server->db, &m->replica);
693 json = ovsdb_error_to_json(error);
694 ovsdb_error_destroy(error);
698 static struct jsonrpc_msg *
699 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
700 struct json_array *params,
701 const struct json *request_id)
703 if (params->n != 1) {
704 return jsonrpc_create_error(json_string_create("invalid parameters"),
707 struct ovsdb_jsonrpc_monitor *m;
709 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
711 return jsonrpc_create_error(json_string_create("unknown monitor"),
714 ovsdb_remove_replica(s->remote->server->db, &m->replica);
715 return jsonrpc_create_reply(json_object_create(), request_id);
721 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
723 struct ovsdb_jsonrpc_monitor *m, *next;
725 HMAP_FOR_EACH_SAFE (m, next,
726 struct ovsdb_jsonrpc_monitor, node, &s->monitors) {
727 ovsdb_remove_replica(s->remote->server->db, &m->replica);
731 static struct ovsdb_jsonrpc_monitor *
732 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
734 assert(replica->class == &ovsdb_jsonrpc_replica_class);
735 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
738 struct ovsdb_jsonrpc_monitor_aux {
739 bool initial; /* Sending initial contents of table? */
740 const struct ovsdb_jsonrpc_monitor *monitor;
741 struct json *json; /* JSON for the whole transaction. */
744 struct ovsdb_jsonrpc_monitor_table *mt;
745 struct json *table_json; /* JSON for table's transaction. */
749 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
750 const struct ovsdb_row *new,
753 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
754 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
755 struct ovsdb_table *table = new ? new->table : old->table;
756 enum ovsdb_jsonrpc_monitor_selection type;
757 struct json *old_json, *new_json;
758 struct json *row_json;
759 char uuid[UUID_LEN + 1];
763 if (!aux->mt || table != aux->mt->table) {
764 aux->mt = shash_find_data(&m->tables, table->schema->name);
765 aux->table_json = NULL;
767 /* We don't care about rows in this table at all. Tell the caller
773 type = (aux->initial ? OJMS_INITIAL
777 if (!(aux->mt->select & type)) {
778 /* We don't care about this type of change (but do want to be called
779 * back for changes to other rows in the same table). */
783 old_json = new_json = NULL;
785 for (i = 0; i < aux->mt->columns.n_columns; i++) {
786 const struct ovsdb_column *column = aux->mt->columns.columns[i];
787 unsigned int idx = column->index;
788 bool changed = false;
790 if (type == OJMS_MODIFY) {
791 changed = !ovsdb_datum_equals(&old->fields[idx],
792 &new->fields[idx], &column->type);
793 n_changed += changed;
795 if (changed || type == OJMS_DELETE) {
797 old_json = json_object_create();
799 json_object_put(old_json, column->name,
800 ovsdb_datum_to_json(&old->fields[idx],
803 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
805 new_json = json_object_create();
807 json_object_put(new_json, column->name,
808 ovsdb_datum_to_json(&new->fields[idx],
812 if ((type == OJMS_MODIFY && !n_changed) || (!old_json && !new_json)) {
813 /* No reportable changes. */
814 json_destroy(old_json);
815 json_destroy(new_json);
819 /* Create JSON object for transaction overall. */
821 aux->json = json_object_create();
824 /* Create JSON object for transaction on this table. */
825 if (!aux->table_json) {
826 aux->table_json = json_object_create();
827 json_object_put(aux->json, aux->mt->table->schema->name,
831 /* Create JSON object for transaction on this row. */
832 row_json = json_object_create();
834 json_object_put(row_json, "old", old_json);
837 json_object_put(row_json, "new", new_json);
840 /* Add JSON row to JSON table. */
841 snprintf(uuid, sizeof uuid,
842 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
843 json_object_put(aux->table_json, uuid, row_json);
849 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
850 const struct ovsdb_jsonrpc_monitor *m,
853 aux->initial = initial;
857 aux->table_json = NULL;
860 static struct ovsdb_error *
861 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
862 const struct ovsdb_txn *txn, bool durable UNUSED)
864 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
865 struct ovsdb_jsonrpc_monitor_aux aux;
867 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
868 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
870 struct jsonrpc_msg *msg;
873 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
875 msg = jsonrpc_create_notify("update", params);
876 jsonrpc_session_send(aux.monitor->session->js, msg);
883 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
885 struct ovsdb_jsonrpc_monitor_aux aux;
886 struct shash_node *node;
888 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
889 SHASH_FOR_EACH (node, &m->tables) {
890 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
892 if (mt->select & OJMS_INITIAL) {
893 struct ovsdb_row *row;
895 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
897 ovsdb_jsonrpc_monitor_change_cb(NULL, row, &aux);
901 return aux.json ? aux.json : json_object_create();
905 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
907 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
908 struct shash_node *node;
910 json_destroy(m->monitor_id);
911 SHASH_FOR_EACH (node, &m->tables) {
912 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
913 ovsdb_column_set_destroy(&mt->columns);
916 shash_destroy(&m->tables);
917 hmap_remove(&m->session->monitors, &m->node);
921 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
922 ovsdb_jsonrpc_monitor_commit,
923 ovsdb_jsonrpc_monitor_destroy