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);
103 /* Sets 'svr''s current set of remotes to the names in 'new_remotes'. The data
104 * values in 'new_remotes' are ignored.
106 * A remote is an active or passive stream connection method, e.g. "pssl:" or
109 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
110 const struct shash *new_remotes)
112 struct shash_node *node, *next;
114 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
115 if (!shash_find(new_remotes, node->name)) {
116 ovsdb_jsonrpc_server_del_remote(node);
119 SHASH_FOR_EACH (node, new_remotes) {
120 if (!shash_find(&svr->remotes, node->name)) {
121 ovsdb_jsonrpc_server_add_remote(svr, node->name);
127 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
130 struct ovsdb_jsonrpc_remote *remote;
131 struct pstream *listener;
134 error = pstream_open(name, &listener);
135 if (error && error != EAFNOSUPPORT) {
136 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
140 remote = xmalloc(sizeof *remote);
141 remote->server = svr;
142 remote->listener = listener;
143 list_init(&remote->sessions);
144 shash_add(&svr->remotes, name, remote);
147 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
152 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
154 struct ovsdb_jsonrpc_remote *remote = node->data;
156 ovsdb_jsonrpc_session_close_all(remote);
157 pstream_close(remote->listener);
158 shash_delete(&remote->server->remotes, node);
163 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
165 struct shash_node *node;
167 SHASH_FOR_EACH (node, &svr->remotes) {
168 struct ovsdb_jsonrpc_remote *remote = node->data;
170 if (remote->listener && svr->n_sessions < svr->max_sessions) {
171 struct stream *stream;
174 error = pstream_accept(remote->listener, &stream);
176 struct jsonrpc_session *js;
177 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
178 ovsdb_jsonrpc_session_create(remote, js);
179 } else if (error != EAGAIN) {
180 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
181 pstream_get_name(remote->listener),
186 ovsdb_jsonrpc_session_run_all(remote);
191 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
193 struct shash_node *node;
195 SHASH_FOR_EACH (node, &svr->remotes) {
196 struct ovsdb_jsonrpc_remote *remote = node->data;
198 if (remote->listener && svr->n_sessions < svr->max_sessions) {
199 pstream_wait(remote->listener);
202 ovsdb_jsonrpc_session_wait_all(remote);
206 /* JSON-RPC database server session. */
208 struct ovsdb_jsonrpc_session {
209 struct ovsdb_jsonrpc_remote *remote;
210 struct list node; /* Element in remote's sessions list. */
213 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
214 struct list completions; /* Completed triggers. */
217 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
219 /* Network connectivity. */
220 struct jsonrpc_session *js; /* JSON-RPC session. */
221 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
224 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
225 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
226 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
227 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
228 struct jsonrpc_msg *);
229 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
230 struct jsonrpc_msg *);
232 static struct ovsdb_jsonrpc_session *
233 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
234 struct jsonrpc_session *js)
236 struct ovsdb_jsonrpc_session *s;
238 s = xzalloc(sizeof *s);
240 list_push_back(&remote->sessions, &s->node);
241 hmap_init(&s->triggers);
242 hmap_init(&s->monitors);
243 list_init(&s->completions);
245 s->js_seqno = jsonrpc_session_get_seqno(js);
247 remote->server->n_sessions++;
253 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
255 jsonrpc_session_close(s->js);
256 list_remove(&s->node);
257 s->remote->server->n_sessions--;
261 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
263 jsonrpc_session_run(s->js);
264 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
265 s->js_seqno = jsonrpc_session_get_seqno(s->js);
266 ovsdb_jsonrpc_trigger_complete_all(s);
267 ovsdb_jsonrpc_monitor_remove_all(s);
270 ovsdb_jsonrpc_trigger_complete_done(s);
272 if (!jsonrpc_session_get_backlog(s->js)) {
273 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
275 if (msg->type == JSONRPC_REQUEST) {
276 ovsdb_jsonrpc_session_got_request(s, msg);
277 } else if (msg->type == JSONRPC_NOTIFY) {
278 ovsdb_jsonrpc_session_got_notify(s, msg);
280 VLOG_WARN("%s: received unexpected %s message",
281 jsonrpc_session_get_name(s->js),
282 jsonrpc_msg_type_to_string(msg->type));
283 jsonrpc_session_force_reconnect(s->js);
284 jsonrpc_msg_destroy(msg);
288 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
292 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
294 struct ovsdb_jsonrpc_session *s, *next;
296 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
298 int error = ovsdb_jsonrpc_session_run(s);
300 ovsdb_jsonrpc_session_close(s);
306 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
308 jsonrpc_session_wait(s->js);
309 if (!jsonrpc_session_get_backlog(s->js)) {
310 jsonrpc_session_recv_wait(s->js);
315 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
317 struct ovsdb_jsonrpc_session *s;
319 LIST_FOR_EACH (s, struct ovsdb_jsonrpc_session, node, &remote->sessions) {
320 ovsdb_jsonrpc_session_wait(s);
325 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
327 struct ovsdb_jsonrpc_session *s, *next;
329 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
331 ovsdb_jsonrpc_session_close(s);
335 static struct jsonrpc_msg *
336 execute_transaction(struct ovsdb_jsonrpc_session *s,
337 struct jsonrpc_msg *request)
339 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
341 request->params = NULL;
346 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
347 struct jsonrpc_msg *request)
349 struct jsonrpc_msg *reply;
351 if (!strcmp(request->method, "transact")) {
352 reply = execute_transaction(s, request);
353 } else if (!strcmp(request->method, "monitor")) {
354 reply = jsonrpc_create_reply(
355 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
356 } else if (!strcmp(request->method, "monitor_cancel")) {
357 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
359 } else if (!strcmp(request->method, "get_schema")) {
360 reply = jsonrpc_create_reply(
361 ovsdb_schema_to_json(s->remote->server->db->schema), request->id);
362 } else if (!strcmp(request->method, "echo")) {
363 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
365 reply = jsonrpc_create_error(json_string_create("unknown method"),
370 jsonrpc_msg_destroy(request);
371 jsonrpc_session_send(s->js, reply);
376 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
378 if (json_array(request->params)->n == 1) {
379 struct ovsdb_jsonrpc_trigger *t;
382 id = request->params->u.array.elems[0];
383 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
385 ovsdb_jsonrpc_trigger_complete(t);
391 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
392 struct jsonrpc_msg *request)
394 if (!strcmp(request->method, "cancel")) {
395 execute_cancel(s, request);
397 jsonrpc_msg_destroy(request);
400 /* JSON-RPC database server triggers.
402 * (Every transaction is treated as a trigger even if it doesn't actually have
403 * any "wait" operations.) */
405 struct ovsdb_jsonrpc_trigger {
406 struct ovsdb_trigger trigger;
407 struct ovsdb_jsonrpc_session *session;
408 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
413 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
414 struct json *id, struct json *params)
416 struct ovsdb_jsonrpc_trigger *t;
419 /* Check for duplicate ID. */
420 hash = json_hash(id, 0);
421 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
423 struct jsonrpc_msg *msg;
425 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
427 jsonrpc_session_send(s->js, msg);
429 json_destroy(params);
433 /* Insert into trigger table. */
434 t = xmalloc(sizeof *t);
435 ovsdb_trigger_init(s->remote->server->db,
436 &t->trigger, params, &s->completions,
440 hmap_insert(&s->triggers, &t->hmap_node, hash);
442 /* Complete early if possible. */
443 if (ovsdb_trigger_is_complete(&t->trigger)) {
444 ovsdb_jsonrpc_trigger_complete(t);
448 static struct ovsdb_jsonrpc_trigger *
449 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
450 const struct json *id, size_t hash)
452 struct ovsdb_jsonrpc_trigger *t;
454 HMAP_FOR_EACH_WITH_HASH (t, struct ovsdb_jsonrpc_trigger, hmap_node, hash,
456 if (json_equal(t->id, id)) {
465 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
467 struct ovsdb_jsonrpc_session *s = t->session;
469 if (jsonrpc_session_is_connected(s->js)) {
470 struct jsonrpc_msg *reply;
473 result = ovsdb_trigger_steal_result(&t->trigger);
475 reply = jsonrpc_create_reply(result, t->id);
477 reply = jsonrpc_create_error(json_string_create("canceled"),
480 jsonrpc_session_send(s->js, reply);
484 ovsdb_trigger_destroy(&t->trigger);
485 hmap_remove(&s->triggers, &t->hmap_node);
490 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
492 struct ovsdb_jsonrpc_trigger *t, *next;
493 HMAP_FOR_EACH_SAFE (t, next, struct ovsdb_jsonrpc_trigger, hmap_node,
495 ovsdb_jsonrpc_trigger_complete(t);
500 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
502 while (!list_is_empty(&s->completions)) {
503 struct ovsdb_jsonrpc_trigger *t
504 = CONTAINER_OF(s->completions.next,
505 struct ovsdb_jsonrpc_trigger, trigger.node);
506 ovsdb_jsonrpc_trigger_complete(t);
510 /* JSON-RPC database table monitors. */
512 enum ovsdb_jsonrpc_monitor_selection {
513 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
514 OJMS_INSERT = 1 << 1, /* New rows. */
515 OJMS_DELETE = 1 << 2, /* Deleted rows. */
516 OJMS_MODIFY = 1 << 3 /* Modified rows. */
519 struct ovsdb_jsonrpc_monitor_table {
520 const struct ovsdb_table *table;
521 enum ovsdb_jsonrpc_monitor_selection select;
522 struct ovsdb_column_set columns;
525 struct ovsdb_jsonrpc_monitor {
526 struct ovsdb_replica replica;
527 struct ovsdb_jsonrpc_session *session;
528 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
530 struct json *monitor_id;
531 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
534 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
536 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
537 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
538 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
539 static struct json *ovsdb_jsonrpc_monitor_get_initial(
540 const struct ovsdb_jsonrpc_monitor *);
543 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
545 const struct json *json;
547 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
548 return json ? json_boolean(json) : default_value;
551 struct ovsdb_jsonrpc_monitor *
552 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
553 const struct json *monitor_id)
555 struct ovsdb_jsonrpc_monitor *m;
557 HMAP_FOR_EACH_WITH_HASH (m, struct ovsdb_jsonrpc_monitor, node,
558 json_hash(monitor_id, 0), &s->monitors) {
559 if (json_equal(m->monitor_id, monitor_id)) {
568 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
571 struct ovsdb_jsonrpc_monitor *m = NULL;
572 struct json *monitor_id, *monitor_requests;
573 struct ovsdb_error *error = NULL;
574 struct shash_node *node;
577 if (json_array(params)->n != 2) {
578 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
581 monitor_id = params->u.array.elems[0];
582 monitor_requests = params->u.array.elems[1];
583 if (monitor_requests->type != JSON_OBJECT) {
584 error = ovsdb_syntax_error(monitor_requests, NULL,
585 "monitor-requests must be object");
589 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
590 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
594 m = xzalloc(sizeof *m);
595 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
596 ovsdb_add_replica(s->remote->server->db, &m->replica);
598 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
599 m->monitor_id = json_clone(monitor_id);
600 shash_init(&m->tables);
602 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
603 const struct ovsdb_table *table;
604 struct ovsdb_jsonrpc_monitor_table *mt;
605 const struct json *columns_json, *select_json;
606 struct ovsdb_parser parser;
608 table = ovsdb_get_table(s->remote->server->db, node->name);
610 error = ovsdb_syntax_error(NULL, NULL,
611 "no table named %s", node->name);
615 mt = xzalloc(sizeof *mt);
617 mt->select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
618 ovsdb_column_set_init(&mt->columns);
619 shash_add(&m->tables, table->schema->name, mt);
621 ovsdb_parser_init(&parser, node->data, "table %s", node->name);
622 columns_json = ovsdb_parser_member(&parser, "columns",
623 OP_ARRAY | OP_OPTIONAL);
624 select_json = ovsdb_parser_member(&parser, "select",
625 OP_OBJECT | OP_OPTIONAL);
626 error = ovsdb_parser_finish(&parser);
632 error = ovsdb_column_set_from_json(columns_json, table,
638 struct shash_node *node;
640 SHASH_FOR_EACH (node, &table->schema->columns) {
641 const struct ovsdb_column *column = node->data;
642 if (column->index != OVSDB_COL_UUID) {
643 ovsdb_column_set_add(&mt->columns, column);
650 ovsdb_parser_init(&parser, select_json, "table %s select",
651 table->schema->name);
652 if (parse_bool(&parser, "initial", true)) {
653 mt->select |= OJMS_INITIAL;
655 if (parse_bool(&parser, "insert", true)) {
656 mt->select |= OJMS_INSERT;
658 if (parse_bool(&parser, "delete", true)) {
659 mt->select |= OJMS_DELETE;
661 if (parse_bool(&parser, "modify", true)) {
662 mt->select |= OJMS_MODIFY;
664 error = ovsdb_parser_finish(&parser);
671 return ovsdb_jsonrpc_monitor_get_initial(m);
675 ovsdb_remove_replica(s->remote->server->db, &m->replica);
678 json = ovsdb_error_to_json(error);
679 ovsdb_error_destroy(error);
683 static struct jsonrpc_msg *
684 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
685 struct json_array *params,
686 const struct json *request_id)
688 if (params->n != 1) {
689 return jsonrpc_create_error(json_string_create("invalid parameters"),
692 struct ovsdb_jsonrpc_monitor *m;
694 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
696 return jsonrpc_create_error(json_string_create("unknown monitor"),
699 ovsdb_remove_replica(s->remote->server->db, &m->replica);
700 return jsonrpc_create_reply(json_object_create(), request_id);
706 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
708 struct ovsdb_jsonrpc_monitor *m, *next;
710 HMAP_FOR_EACH_SAFE (m, next,
711 struct ovsdb_jsonrpc_monitor, node, &s->monitors) {
712 ovsdb_remove_replica(s->remote->server->db, &m->replica);
716 static struct ovsdb_jsonrpc_monitor *
717 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
719 assert(replica->class == &ovsdb_jsonrpc_replica_class);
720 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
723 struct ovsdb_jsonrpc_monitor_aux {
724 bool initial; /* Sending initial contents of table? */
725 const struct ovsdb_jsonrpc_monitor *monitor;
726 struct json *json; /* JSON for the whole transaction. */
729 struct ovsdb_jsonrpc_monitor_table *mt;
730 struct json *table_json; /* JSON for table's transaction. */
734 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
735 const struct ovsdb_row *new,
738 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
739 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
740 struct ovsdb_table *table = new ? new->table : old->table;
741 enum ovsdb_jsonrpc_monitor_selection type;
742 struct json *old_json, *new_json;
743 struct json *row_json;
744 char uuid[UUID_LEN + 1];
748 if (!aux->mt || table != aux->mt->table) {
749 aux->mt = shash_find_data(&m->tables, table->schema->name);
750 aux->table_json = NULL;
752 /* We don't care about rows in this table at all. Tell the caller
758 type = (aux->initial ? OJMS_INITIAL
762 if (!(aux->mt->select & type)) {
763 /* We don't care about this type of change (but do want to be called
764 * back for changes to other rows in the same table). */
768 old_json = new_json = NULL;
770 for (i = 0; i < aux->mt->columns.n_columns; i++) {
771 const struct ovsdb_column *column = aux->mt->columns.columns[i];
772 unsigned int idx = column->index;
773 bool changed = false;
775 if (type == OJMS_MODIFY) {
776 changed = !ovsdb_datum_equals(&old->fields[idx],
777 &new->fields[idx], &column->type);
778 n_changed += changed;
780 if (changed || type == OJMS_DELETE) {
782 old_json = json_object_create();
784 json_object_put(old_json, column->name,
785 ovsdb_datum_to_json(&old->fields[idx],
788 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
790 new_json = json_object_create();
792 json_object_put(new_json, column->name,
793 ovsdb_datum_to_json(&new->fields[idx],
797 if ((type == OJMS_MODIFY && !n_changed) || (!old_json && !new_json)) {
798 /* No reportable changes. */
799 json_destroy(old_json);
800 json_destroy(new_json);
804 /* Create JSON object for transaction overall. */
806 aux->json = json_object_create();
809 /* Create JSON object for transaction on this table. */
810 if (!aux->table_json) {
811 aux->table_json = json_object_create();
812 json_object_put(aux->json, aux->mt->table->schema->name,
816 /* Create JSON object for transaction on this row. */
817 row_json = json_object_create();
819 json_object_put(row_json, "old", old_json);
822 json_object_put(row_json, "new", new_json);
825 /* Add JSON row to JSON table. */
826 snprintf(uuid, sizeof uuid,
827 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
828 json_object_put(aux->table_json, uuid, row_json);
834 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
835 const struct ovsdb_jsonrpc_monitor *m,
838 aux->initial = initial;
842 aux->table_json = NULL;
845 static struct ovsdb_error *
846 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
847 const struct ovsdb_txn *txn, bool durable UNUSED)
849 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
850 struct ovsdb_jsonrpc_monitor_aux aux;
852 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
853 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
855 struct jsonrpc_msg *msg;
858 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
860 msg = jsonrpc_create_notify("update", params);
861 jsonrpc_session_send(aux.monitor->session->js, msg);
868 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
870 struct ovsdb_jsonrpc_monitor_aux aux;
871 struct shash_node *node;
873 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
874 SHASH_FOR_EACH (node, &m->tables) {
875 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
877 if (mt->select & OJMS_INITIAL) {
878 struct ovsdb_row *row;
880 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
882 ovsdb_jsonrpc_monitor_change_cb(NULL, row, &aux);
886 return aux.json ? aux.json : json_object_create();
890 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
892 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
893 struct shash_node *node;
895 json_destroy(m->monitor_id);
896 SHASH_FOR_EACH (node, &m->tables) {
897 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
898 ovsdb_column_set_destroy(&mt->columns);
901 shash_destroy(&m->tables);
902 hmap_remove(&m->session->monitors, &m->node);
906 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
907 ovsdb_jsonrpc_monitor_commit,
908 ovsdb_jsonrpc_monitor_destroy