ovsdb-monitor: refactor ovsdb_monitor_create()
[cascardo/ovs.git] / ovsdb / monitor.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <errno.h>
20
21 #include "bitmap.h"
22 #include "column.h"
23 #include "dynamic-string.h"
24 #include "json.h"
25 #include "jsonrpc.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
28 #include "ovsdb.h"
29 #include "row.h"
30 #include "simap.h"
31 #include "hash.h"
32 #include "table.h"
33 #include "timeval.h"
34 #include "transaction.h"
35 #include "jsonrpc-server.h"
36 #include "monitor.h"
37 #include "openvswitch/vlog.h"
38
39
40 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
41
42 /*  Backend monitor.
43  *
44  *  ovsdb_monitor keep track of the ovsdb changes.
45  */
46
47 /* A collection of tables being monitored. */
48 struct ovsdb_monitor {
49     struct ovsdb_replica replica;
50     struct shash tables;     /* Holds "struct ovsdb_monitor_table"s. */
51     struct ovs_list jsonrpc_monitors;  /* Contains "jsonrpc_monitor_node"s. */
52     struct ovsdb *db;
53     uint64_t n_transactions;      /* Count number of committed transactions. */
54 };
55
56 struct jsonrpc_monitor_node {
57     struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
58     struct ovs_list node;
59 };
60
61 /* A particular column being monitored. */
62 struct ovsdb_monitor_column {
63     const struct ovsdb_column *column;
64     enum ovsdb_monitor_selection select;
65 };
66
67 /* A row that has changed in a monitored table. */
68 struct ovsdb_monitor_row {
69     struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
70     struct uuid uuid;           /* UUID of row that changed. */
71     struct ovsdb_datum *old;    /* Old data, NULL for an inserted row. */
72     struct ovsdb_datum *new;    /* New data, NULL for a deleted row. */
73 };
74
75 /* Contains 'struct ovsdb_monitor_row's for rows that have been
76  * updated but not yet flushed to all the jsonrpc connection.
77  *
78  * 'n_refs' represent the number of jsonrpc connections that have
79  * not received updates. Generate the update for the last jsonprc
80  * connection will also destroy the whole "struct ovsdb_monitor_changes"
81  * object.
82  *
83  * 'transaction' stores the first update's transaction id.
84  * */
85 struct ovsdb_monitor_changes {
86     struct ovsdb_monitor_table *mt;
87     struct hmap rows;
88     int n_refs;
89     uint64_t transaction;
90     struct hmap_node hmap_node;  /* Element in ovsdb_monitor_tables' changes
91                                     hmap.  */
92 };
93
94 /* A particular table being monitored. */
95 struct ovsdb_monitor_table {
96     const struct ovsdb_table *table;
97
98     /* This is the union (bitwise-OR) of the 'select' values in all of the
99      * members of 'columns' below. */
100     enum ovsdb_monitor_selection select;
101
102     /* Columns being monitored. */
103     struct ovsdb_monitor_column *columns;
104     size_t n_columns;
105
106     /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
107     struct hmap changes;
108 };
109
110 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
111 static void ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
112                                             uint64_t next_txn);
113 static struct ovsdb_monitor_changes *ovsdb_monitor_table_find_changes(
114     struct ovsdb_monitor_table *mt, uint64_t unflushed);
115 static void ovsdb_monitor_changes_destroy(
116                                   struct ovsdb_monitor_changes *changes);
117 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
118                                   uint64_t unflushed);
119
120 static int
121 compare_ovsdb_monitor_column(const void *a_, const void *b_)
122 {
123     const struct ovsdb_monitor_column *a = a_;
124     const struct ovsdb_monitor_column *b = b_;
125
126     return a->column < b->column ? -1 : a->column > b->column;
127 }
128
129 static struct ovsdb_monitor *
130 ovsdb_monitor_cast(struct ovsdb_replica *replica)
131 {
132     ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
133     return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
134 }
135
136 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
137  * given 'uuid', or NULL if there is no such row. */
138 static struct ovsdb_monitor_row *
139 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes *changes,
140                                const struct uuid *uuid)
141 {
142     struct ovsdb_monitor_row *row;
143
144     HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
145                              &changes->rows) {
146         if (uuid_equals(uuid, &row->uuid)) {
147             return row;
148         }
149     }
150     return NULL;
151 }
152
153 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
154  * copies of the data in 'row' drawn from the columns represented by
155  * mt->columns[].  Returns the array.
156  *
157  * If 'row' is NULL, returns NULL. */
158 static struct ovsdb_datum *
159 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
160                        const struct ovsdb_row *row)
161 {
162     struct ovsdb_datum *data;
163     size_t i;
164
165     if (!row) {
166         return NULL;
167     }
168
169     data = xmalloc(mt->n_columns * sizeof *data);
170     for (i = 0; i < mt->n_columns; i++) {
171         const struct ovsdb_column *c = mt->columns[i].column;
172         const struct ovsdb_datum *src = &row->fields[c->index];
173         struct ovsdb_datum *dst = &data[i];
174         const struct ovsdb_type *type = &c->type;
175
176         ovsdb_datum_clone(dst, src, type);
177     }
178     return data;
179 }
180
181 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
182  * in 'row' drawn from the columns represented by mt->columns[]. */
183 static void
184 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
185                         const struct ovsdb_row *row,
186                         struct ovsdb_datum *data)
187 {
188     size_t i;
189
190     for (i = 0; i < mt->n_columns; i++) {
191         const struct ovsdb_column *c = mt->columns[i].column;
192         const struct ovsdb_datum *src = &row->fields[c->index];
193         struct ovsdb_datum *dst = &data[i];
194         const struct ovsdb_type *type = &c->type;
195
196         if (!ovsdb_datum_equals(src, dst, type)) {
197             ovsdb_datum_destroy(dst, type);
198             ovsdb_datum_clone(dst, src, type);
199         }
200     }
201 }
202
203 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
204  * from mt->columns[], plus 'data' itself. */
205 static void
206 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
207                       struct ovsdb_datum *data)
208 {
209     if (data) {
210         size_t i;
211
212         for (i = 0; i < mt->n_columns; i++) {
213             const struct ovsdb_column *c = mt->columns[i].column;
214
215             ovsdb_datum_destroy(&data[i], &c->type);
216         }
217         free(data);
218     }
219 }
220
221 /* Frees 'row', which must have been created from 'mt'. */
222 static void
223 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
224                           struct ovsdb_monitor_row *row)
225 {
226     if (row) {
227         free_monitor_row_data(mt, row->old);
228         free_monitor_row_data(mt, row->new);
229         free(row);
230     }
231 }
232
233 static void
234 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
235                                   struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
236 {
237     struct jsonrpc_monitor_node *jm;
238
239     jm = xzalloc(sizeof *jm);
240     jm->jsonrpc_monitor = jsonrpc_monitor;
241     list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
242 }
243
244 struct ovsdb_monitor *
245 ovsdb_monitor_create(struct ovsdb *db,
246                      struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
247 {
248     struct ovsdb_monitor *dbmon;
249
250     dbmon = xzalloc(sizeof *dbmon);
251
252     ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
253     ovsdb_add_replica(db, &dbmon->replica);
254     list_init(&dbmon->jsonrpc_monitors);
255     dbmon->db = db;
256     dbmon->n_transactions = 0;
257     shash_init(&dbmon->tables);
258
259     ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
260     return dbmon;
261 }
262
263 void
264 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
265                         const struct ovsdb_table *table)
266 {
267     struct ovsdb_monitor_table *mt;
268
269     mt = xzalloc(sizeof *mt);
270     mt->table = table;
271     shash_add(&m->tables, table->schema->name, mt);
272     hmap_init(&mt->changes);
273 }
274
275 void
276 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
277                          const struct ovsdb_table *table,
278                          const struct ovsdb_column *column,
279                          enum ovsdb_monitor_selection select,
280                          size_t *allocated_columns)
281 {
282     struct ovsdb_monitor_table *mt;
283     struct ovsdb_monitor_column *c;
284
285     mt = shash_find_data(&dbmon->tables, table->schema->name);
286
287     if (mt->n_columns >= *allocated_columns) {
288         mt->columns = x2nrealloc(mt->columns, allocated_columns,
289                                  sizeof *mt->columns);
290     }
291
292     mt->select |= select;
293     c = &mt->columns[mt->n_columns++];
294     c->column = column;
295     c->select = select;
296 }
297
298 /* Check for duplicated column names. Return the first
299  * duplicated column's name if found. Otherwise return
300  * NULL.  */
301 const char * OVS_WARN_UNUSED_RESULT
302 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
303                                      const struct ovsdb_table *table)
304 {
305     struct ovsdb_monitor_table *mt;
306     int i;
307
308     mt = shash_find_data(&m->tables, table->schema->name);
309
310     if (mt) {
311         /* Check for duplicate columns. */
312         qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
313               compare_ovsdb_monitor_column);
314         for (i = 1; i < mt->n_columns; i++) {
315             if (mt->columns[i].column == mt->columns[i - 1].column) {
316                 return mt->columns[i].column->name;
317             }
318         }
319     }
320
321     return NULL;
322 }
323
324 static void
325 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
326                                 uint64_t next_txn)
327 {
328     struct ovsdb_monitor_changes *changes;
329
330     changes = xzalloc(sizeof *changes);
331
332     changes->transaction = next_txn;
333     changes->mt = mt;
334     changes->n_refs = 1;
335     hmap_init(&changes->rows);
336     hmap_insert(&mt->changes, &changes->hmap_node, hash_uint64(next_txn));
337 };
338
339 static struct ovsdb_monitor_changes *
340 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table *mt,
341                                  uint64_t transaction)
342 {
343     struct ovsdb_monitor_changes *changes;
344     size_t hash = hash_uint64(transaction);
345
346     HMAP_FOR_EACH_WITH_HASH(changes, hmap_node, hash, &mt->changes) {
347         if (changes->transaction == transaction) {
348             return changes;
349         }
350     }
351
352     return NULL;
353 }
354
355 /* Stop currently tracking changes to table 'mt' since 'transaction'.
356  *
357  * Return 'true' if the 'transaction' is being tracked. 'false' otherwise. */
358 static void
359 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table *mt,
360                                     uint64_t transaction)
361 {
362     struct ovsdb_monitor_changes *changes =
363                 ovsdb_monitor_table_find_changes(mt, transaction);
364     if (changes) {
365         ovs_assert(changes->transaction == transaction);
366         if (--changes->n_refs == 0) {
367             hmap_remove(&mt->changes, &changes->hmap_node);
368             ovsdb_monitor_changes_destroy(changes);
369         }
370     }
371 }
372
373 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
374  */
375 static void
376 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
377                                   uint64_t transaction)
378 {
379     struct ovsdb_monitor_changes *changes;
380
381     changes = ovsdb_monitor_table_find_changes(mt, transaction);
382     if (changes) {
383         changes->n_refs++;
384     } else {
385         ovsdb_monitor_table_add_changes(mt, transaction);
386     }
387 }
388
389 static void
390 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes *changes)
391 {
392     struct ovsdb_monitor_row *row, *next;
393
394     HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
395         hmap_remove(&changes->rows, &row->hmap_node);
396         ovsdb_monitor_row_destroy(changes->mt, row);
397     }
398     hmap_destroy(&changes->rows);
399     free(changes);
400 }
401
402 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
403  * 'mt', or NULL if no row update should be sent.
404  *
405  * The caller should specify 'initial' as true if the returned JSON is going to
406  * be used as part of the initial reply to a "monitor" request, false if it is
407  * going to be used as part of an "update" notification.
408  *
409  * 'changed' must be a scratch buffer for internal use that is at least
410  * bitmap_n_bytes(mt->n_columns) bytes long. */
411 static struct json *
412 ovsdb_monitor_compose_row_update(
413     const struct ovsdb_monitor_table *mt,
414     const struct ovsdb_monitor_row *row,
415     bool initial, unsigned long int *changed)
416 {
417     enum ovsdb_monitor_selection type;
418     struct json *old_json, *new_json;
419     struct json *row_json;
420     size_t i;
421
422     type = (initial ? OJMS_INITIAL
423             : !row->old ? OJMS_INSERT
424             : !row->new ? OJMS_DELETE
425             : OJMS_MODIFY);
426     if (!(mt->select & type)) {
427         return NULL;
428     }
429
430     if (type == OJMS_MODIFY) {
431         size_t n_changes;
432
433         n_changes = 0;
434         memset(changed, 0, bitmap_n_bytes(mt->n_columns));
435         for (i = 0; i < mt->n_columns; i++) {
436             const struct ovsdb_column *c = mt->columns[i].column;
437             if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
438                 bitmap_set1(changed, i);
439                 n_changes++;
440             }
441         }
442         if (!n_changes) {
443             /* No actual changes: presumably a row changed and then
444              * changed back later. */
445             return NULL;
446         }
447     }
448
449     row_json = json_object_create();
450     old_json = new_json = NULL;
451     if (type & (OJMS_DELETE | OJMS_MODIFY)) {
452         old_json = json_object_create();
453         json_object_put(row_json, "old", old_json);
454     }
455     if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
456         new_json = json_object_create();
457         json_object_put(row_json, "new", new_json);
458     }
459     for (i = 0; i < mt->n_columns; i++) {
460         const struct ovsdb_monitor_column *c = &mt->columns[i];
461
462         if (!(type & c->select)) {
463             /* We don't care about this type of change for this
464              * particular column (but we will care about it for some
465              * other column). */
466             continue;
467         }
468
469         if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
470             || type == OJMS_DELETE) {
471             json_object_put(old_json, c->column->name,
472                             ovsdb_datum_to_json(&row->old[i],
473                                                 &c->column->type));
474         }
475         if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
476             json_object_put(new_json, c->column->name,
477                             ovsdb_datum_to_json(&row->new[i],
478                                                 &c->column->type));
479         }
480     }
481
482     return row_json;
483 }
484
485 /* Constructs and returns JSON for a <table-updates> object (as described in
486  * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
487  * the outstanding changes from 'monitor'.  Returns NULL if no update needs to
488  * be sent.
489  *
490  * The caller should specify 'initial' as true if the returned JSON is going to
491  * be used as part of the initial reply to a "monitor" request, false if it is
492  * going to be used as part of an "update" notification.
493  *
494  * 'unflushed' should point to value that is the transaction ID that did
495  * was not updated. The update contains changes between
496  * ['unflushed, ovsdb->n_transcations]. Before the function returns, this
497  * value will be updated to ovsdb->n_transactions + 1, ready for the next
498  * update.  */
499 struct json *
500 ovsdb_monitor_compose_update(const struct ovsdb_monitor *dbmon,
501                              bool initial, uint64_t *unflushed)
502 {
503     struct shash_node *node;
504     unsigned long int *changed;
505     struct json *json;
506     size_t max_columns;
507     uint64_t prev_txn = *unflushed;
508     uint64_t next_txn = dbmon->n_transactions + 1;
509
510     max_columns = 0;
511     SHASH_FOR_EACH (node, &dbmon->tables) {
512         struct ovsdb_monitor_table *mt = node->data;
513
514         max_columns = MAX(max_columns, mt->n_columns);
515     }
516     changed = xmalloc(bitmap_n_bytes(max_columns));
517
518     json = NULL;
519     SHASH_FOR_EACH (node, &dbmon->tables) {
520         struct ovsdb_monitor_table *mt = node->data;
521         struct ovsdb_monitor_row *row, *next;
522         struct ovsdb_monitor_changes *changes;
523         struct json *table_json = NULL;
524
525         changes = ovsdb_monitor_table_find_changes(mt, prev_txn);
526         if (!changes) {
527             ovsdb_monitor_table_track_changes(mt, next_txn);
528             continue;
529         }
530
531         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
532             struct json *row_json;
533
534             row_json = ovsdb_monitor_compose_row_update(
535                 mt, row, initial, changed);
536             if (row_json) {
537                 char uuid[UUID_LEN + 1];
538
539                 /* Create JSON object for transaction overall. */
540                 if (!json) {
541                     json = json_object_create();
542                 }
543
544                 /* Create JSON object for transaction on this table. */
545                 if (!table_json) {
546                     table_json = json_object_create();
547                     json_object_put(json, mt->table->schema->name, table_json);
548                 }
549
550                 /* Add JSON row to JSON table. */
551                 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
552                 json_object_put(table_json, uuid, row_json);
553             }
554
555             hmap_remove(&changes->rows, &row->hmap_node);
556             ovsdb_monitor_row_destroy(mt, row);
557         }
558
559         ovsdb_monitor_table_untrack_changes(mt, prev_txn);
560         ovsdb_monitor_table_track_changes(mt, next_txn);
561     }
562
563     *unflushed = next_txn;
564     free(changed);
565     return json;
566 }
567
568 bool
569 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
570                           uint64_t next_transaction)
571 {
572     ovs_assert(next_transaction <= dbmon->n_transactions + 1);
573     return (next_transaction <= dbmon->n_transactions);
574 }
575
576 void
577 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
578                                const struct ovsdb_table *table,
579                                enum ovsdb_monitor_selection select)
580 {
581     struct ovsdb_monitor_table * mt;
582
583     mt = shash_find_data(&dbmon->tables, table->schema->name);
584     mt->select |= select;
585 }
586
587 struct ovsdb_monitor_aux {
588     const struct ovsdb_monitor *monitor;
589     struct ovsdb_monitor_table *mt;
590 };
591
592 static void
593 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
594                        const struct ovsdb_monitor *m)
595 {
596     aux->monitor = m;
597     aux->mt = NULL;
598 }
599
600 static void
601 ovsdb_monitor_changes_update(const struct ovsdb_row *old,
602                              const struct ovsdb_row *new,
603                              const struct ovsdb_monitor_table *mt,
604                              struct ovsdb_monitor_changes *changes)
605 {
606     const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
607     struct ovsdb_monitor_row *change;
608
609     change = ovsdb_monitor_changes_row_find(changes, uuid);
610     if (!change) {
611         change = xzalloc(sizeof *change);
612         hmap_insert(&changes->rows, &change->hmap_node, uuid_hash(uuid));
613         change->uuid = *uuid;
614         change->old = clone_monitor_row_data(mt, old);
615         change->new = clone_monitor_row_data(mt, new);
616     } else {
617         if (new) {
618             update_monitor_row_data(mt, new, change->new);
619         } else {
620             free_monitor_row_data(mt, change->new);
621             change->new = NULL;
622
623             if (!change->old) {
624                 /* This row was added then deleted.  Forget about it. */
625                 hmap_remove(&changes->rows, &change->hmap_node);
626                 free(change);
627             }
628         }
629     }
630 }
631
632 static bool
633 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
634                         const struct ovsdb_row *new,
635                         const unsigned long int *changed OVS_UNUSED,
636                         void *aux_)
637 {
638     struct ovsdb_monitor_aux *aux = aux_;
639     const struct ovsdb_monitor *m = aux->monitor;
640     struct ovsdb_table *table = new ? new->table : old->table;
641     struct ovsdb_monitor_table *mt;
642     struct ovsdb_monitor_changes *changes;
643
644     if (!aux->mt || table != aux->mt->table) {
645         aux->mt = shash_find_data(&m->tables, table->schema->name);
646         if (!aux->mt) {
647             /* We don't care about rows in this table at all.  Tell the caller
648              * to skip it.  */
649             return false;
650         }
651     }
652     mt = aux->mt;
653
654     HMAP_FOR_EACH(changes, hmap_node, &mt->changes) {
655         ovsdb_monitor_changes_update(old, new, mt, changes);
656     }
657     return true;
658 }
659
660 void
661 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
662 {
663     struct ovsdb_monitor_aux aux;
664     struct shash_node *node;
665
666     ovsdb_monitor_init_aux(&aux, dbmon);
667     SHASH_FOR_EACH (node, &dbmon->tables) {
668         struct ovsdb_monitor_table *mt = node->data;
669
670         if (mt->select & OJMS_INITIAL) {
671             struct ovsdb_row *row;
672
673             if (hmap_is_empty(&mt->changes)) {
674                 ovsdb_monitor_table_add_changes(mt, 0);
675             }
676
677             HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
678                 ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
679             }
680         }
681     }
682 }
683
684 void
685 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
686                    struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
687 {
688     struct jsonrpc_monitor_node *jm;
689
690     /* Find and remove the jsonrpc monitor from the list.  */
691     LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
692         if (jm->jsonrpc_monitor == jsonrpc_monitor) {
693             list_remove(&jm->node);
694             free(jm);
695
696             /* Destroy ovsdb monitor if this is the last user.  */
697             if (list_is_empty(&dbmon->jsonrpc_monitors)) {
698                 ovsdb_monitor_destroy(dbmon);
699             }
700
701             return;
702         };
703     }
704
705     /* Should never reach here. jsonrpc_monitor should be on the list.  */
706     OVS_NOT_REACHED();
707 }
708
709 static void
710 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
711 {
712     struct shash_node *node;
713
714     list_remove(&dbmon->replica.node);
715
716     SHASH_FOR_EACH (node, &dbmon->tables) {
717         struct ovsdb_monitor_table *mt = node->data;
718         struct ovsdb_monitor_changes *changes, *next;
719
720         HMAP_FOR_EACH_SAFE (changes, next, hmap_node, &mt->changes) {
721             hmap_remove(&mt->changes, &changes->hmap_node);
722             ovsdb_monitor_changes_destroy(changes);
723         }
724         free(mt->columns);
725         free(mt);
726     }
727     shash_destroy(&dbmon->tables);
728     free(dbmon);
729 }
730
731 static struct ovsdb_error *
732 ovsdb_monitor_commit(struct ovsdb_replica *replica,
733                      const struct ovsdb_txn *txn,
734                      bool durable OVS_UNUSED)
735 {
736     struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
737     struct ovsdb_monitor_aux aux;
738
739     ovsdb_monitor_init_aux(&aux, m);
740     ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
741     m->n_transactions++;
742
743     return NULL;
744 }
745
746 static void
747 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
748 {
749     struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
750     struct jsonrpc_monitor_node *jm, *next;
751
752     /* Delete all front end monitors. Removing the last front
753      * end monitor will also destroy the corresponding 'ovsdb_monitor'.
754      * ovsdb monitor will also be destroied.  */
755     LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
756         ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
757     }
758 }
759
760 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
761     ovsdb_monitor_commit,
762     ovsdb_monitor_destroy_callback,
763 };