ofp-actions: Fix use-after-free in decode_NOTE.
[cascardo/ovs.git] / tests / test-ovsdb.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 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 <assert.h>
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "byte-order.h"
27 #include "command-line.h"
28 #include "openvswitch/dynamic-string.h"
29 #include "json.h"
30 #include "jsonrpc.h"
31 #include "ovsdb-data.h"
32 #include "ovsdb-error.h"
33 #include "ovsdb-idl.h"
34 #include "ovsdb-types.h"
35 #include "ovsdb/column.h"
36 #include "ovsdb/condition.h"
37 #include "ovsdb/file.h"
38 #include "ovsdb/log.h"
39 #include "ovsdb/mutation.h"
40 #include "ovsdb/ovsdb.h"
41 #include "ovsdb/query.h"
42 #include "ovsdb/row.h"
43 #include "ovsdb/server.h"
44 #include "ovsdb/table.h"
45 #include "ovsdb/transaction.h"
46 #include "ovsdb/trigger.h"
47 #include "poll-loop.h"
48 #include "stream.h"
49 #include "svec.h"
50 #include "tests/idltest.h"
51 #include "timeval.h"
52 #include "util.h"
53 #include "openvswitch/vlog.h"
54
55 struct test_ovsdb_pvt_context {
56     bool track;
57 };
58
59 OVS_NO_RETURN static void usage(void);
60 static void parse_options(int argc, char *argv[],
61     struct test_ovsdb_pvt_context *pvt);
62 static struct ovs_cmdl_command *get_all_commands(void);
63
64 int
65 main(int argc, char *argv[])
66 {
67     struct test_ovsdb_pvt_context pvt = {.track = false};
68     struct ovs_cmdl_context ctx = { .argc = 0, .pvt = &pvt};
69     set_program_name(argv[0]);
70     parse_options(argc, argv, &pvt);
71     ctx.argc = argc - optind;
72     ctx.argv = argv + optind;
73     ovs_cmdl_run_command(&ctx, get_all_commands());
74     return 0;
75 }
76
77 static void
78 parse_options(int argc, char *argv[], struct test_ovsdb_pvt_context *pvt)
79 {
80     static const struct option long_options[] = {
81         {"timeout", required_argument, NULL, 't'},
82         {"verbose", optional_argument, NULL, 'v'},
83         {"change-track", optional_argument, NULL, 'c'},
84         {"help", no_argument, NULL, 'h'},
85         {NULL, 0, NULL, 0},
86     };
87     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
88
89     for (;;) {
90         unsigned long int timeout;
91         int c;
92
93         c = getopt_long(argc, argv, short_options, long_options, NULL);
94         if (c == -1) {
95             break;
96         }
97
98         switch (c) {
99         case 't':
100             timeout = strtoul(optarg, NULL, 10);
101             if (timeout <= 0) {
102                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
103                           optarg);
104             } else {
105                 time_alarm(timeout);
106             }
107             break;
108
109         case 'h':
110             usage();
111
112         case 'v':
113             vlog_set_verbosity(optarg);
114             break;
115
116         case 'c':
117             pvt->track = true;
118             break;
119
120         case '?':
121             exit(EXIT_FAILURE);
122
123         default:
124             abort();
125         }
126     }
127     free(short_options);
128 }
129
130 static void
131 usage(void)
132 {
133     printf("%s: Open vSwitch database test utility\n"
134            "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
135            "  log-io FILE FLAGS COMMAND...\n"
136            "    open FILE with FLAGS, run COMMANDs\n"
137            "  default-atoms\n"
138            "    test ovsdb_atom_default()\n"
139            "  default-data\n"
140            "    test ovsdb_datum_default()\n"
141            "  parse-atomic-type TYPE\n"
142            "    parse TYPE as OVSDB atomic type, and re-serialize\n"
143            "  parse-base-type TYPE\n"
144            "    parse TYPE as OVSDB base type, and re-serialize\n"
145            "  parse-type JSON\n"
146            "    parse JSON as OVSDB type, and re-serialize\n"
147            "  parse-atoms TYPE ATOM...\n"
148            "    parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
149            "  parse-atom-strings TYPE ATOM...\n"
150            "    parse string ATOMs as atoms of given TYPE, and re-serialize\n"
151            "  sort-atoms TYPE ATOM...\n"
152            "    print JSON ATOMs in sorted order\n"
153            "  parse-data TYPE DATUM...\n"
154            "    parse JSON DATUMs as data of given TYPE, and re-serialize\n"
155            "  parse-data-strings TYPE DATUM...\n"
156            "    parse string DATUMs as data of given TYPE, and re-serialize\n"
157            "  parse-column NAME OBJECT\n"
158            "    parse column NAME with info OBJECT, and re-serialize\n"
159            "  parse-table NAME OBJECT [DEFAULT-IS-ROOT]\n"
160            "    parse table NAME with info OBJECT\n"
161            "  parse-row TABLE ROW..., and re-serialize\n"
162            "    parse each ROW of defined TABLE\n"
163            "  compare-row TABLE ROW...\n"
164            "    mutually compare all of the ROWs, print those that are equal\n"
165            "  parse-conditions TABLE CONDITION...\n"
166            "    parse each CONDITION on TABLE, and re-serialize\n"
167            "  evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
168            "    test CONDITIONS on TABLE against each ROW, print results\n"
169            "  parse-mutations TABLE MUTATION...\n"
170            "    parse each MUTATION on TABLE, and re-serialize\n"
171            "  execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
172            "    execute MUTATIONS on TABLE on each ROW, print results\n"
173            "  query TABLE [ROW,...] [CONDITION,...]\n"
174            "    add each ROW to TABLE, then query and print the rows that\n"
175            "    satisfy each CONDITION.\n"
176            "  query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
177            "    add each ROW to TABLE, then query and print the rows that\n"
178            "    satisfy each CONDITION and have distinct COLUMNS.\n"
179            "  parse-schema JSON\n"
180            "    parse JSON as an OVSDB schema, and re-serialize\n"
181            "  transact COMMAND\n"
182            "    execute each specified transactional COMMAND:\n"
183            "      commit\n"
184            "      abort\n"
185            "      insert UUID I J\n"
186            "      delete UUID\n"
187            "      modify UUID I J\n"
188            "      print\n"
189            "  execute SCHEMA TRANSACTION...\n"
190            "    executes each TRANSACTION on an initially empty database\n"
191            "    the specified SCHEMA\n"
192            "  trigger SCHEMA TRANSACTION...\n"
193            "    executes each TRANSACTION on an initially empty database\n"
194            "    the specified SCHEMA.   A TRANSACTION of the form\n"
195            "    [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
196            "    simulated time, for causing triggers to time out.\n"
197            "  idl SERVER [TRANSACTION...]\n"
198            "    connect to SERVER and dump the contents of the database\n"
199            "    as seen initially by the IDL implementation and after\n"
200            "    executing each TRANSACTION.  (Each TRANSACTION must modify\n"
201            "    the database or this command will hang.)\n",
202            program_name, program_name);
203     vlog_usage();
204     printf("\nOther options:\n"
205            "  -t, --timeout=SECS          give up after SECS seconds\n"
206            "  -h, --help                  display this help message\n"
207            "  -c, --change-track          used with the 'idl' command to\n"
208            "                              enable tracking of IDL changes\n");
209     exit(EXIT_SUCCESS);
210 }
211 \f
212 /* Command helper functions. */
213
214 static struct json *
215 parse_json(const char *s)
216 {
217     struct json *json = json_from_string(s);
218     if (json->type == JSON_STRING) {
219         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
220     }
221     return json;
222 }
223
224 static struct json *
225 unbox_json(struct json *json)
226 {
227     if (json->type == JSON_ARRAY && json->u.array.n == 1) {
228         struct json *inner = json->u.array.elems[0];
229         json->u.array.elems[0] = NULL;
230         json_destroy(json);
231         return inner;
232     } else {
233         return json;
234     }
235 }
236
237 static void
238 print_and_free_json(struct json *json)
239 {
240     char *string = json_to_string(json, JSSF_SORT);
241     json_destroy(json);
242     puts(string);
243     free(string);
244 }
245
246 static void
247 print_and_free_ovsdb_error(struct ovsdb_error *error)
248 {
249     char *string = ovsdb_error_to_string(error);
250     ovsdb_error_destroy(error);
251     puts(string);
252     free(string);
253 }
254
255 static void
256 check_ovsdb_error(struct ovsdb_error *error)
257 {
258     if (error) {
259         char *s = ovsdb_error_to_string(error);
260         ovsdb_error_destroy(error);
261         ovs_fatal(0, "%s", s);
262     }
263 }
264
265 static void
266 die_if_error(char *error)
267 {
268     if (error) {
269         ovs_fatal(0, "%s", error);
270     }
271 }
272 \f
273 /* Command implementations. */
274
275 static void
276 do_log_io(struct ovs_cmdl_context *ctx)
277 {
278     const char *name = ctx->argv[1];
279     char *mode_string = ctx->argv[2];
280
281     struct ovsdb_error *error;
282     enum ovsdb_log_open_mode mode;
283     struct ovsdb_log *log;
284     int i;
285
286     if (!strcmp(mode_string, "read-only")) {
287         mode = OVSDB_LOG_READ_ONLY;
288     } else if (!strcmp(mode_string, "read/write")) {
289         mode = OVSDB_LOG_READ_WRITE;
290     } else if (!strcmp(mode_string, "create")) {
291         mode = OVSDB_LOG_CREATE;
292     } else {
293         ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
294     }
295
296     check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
297     printf("%s: open successful\n", name);
298
299     for (i = 3; i < ctx->argc; i++) {
300         const char *command = ctx->argv[i];
301         if (!strcmp(command, "read")) {
302             struct json *json;
303
304             error = ovsdb_log_read(log, &json);
305             if (!error) {
306                 printf("%s: read: ", name);
307                 if (json) {
308                     print_and_free_json(json);
309                 } else {
310                     printf("end of log\n");
311                 }
312                 continue;
313             }
314         } else if (!strncmp(command, "write:", 6)) {
315             struct json *json = parse_json(command + 6);
316             error = ovsdb_log_write(log, json);
317             json_destroy(json);
318         } else if (!strcmp(command, "commit")) {
319             error = ovsdb_log_commit(log);
320         } else {
321             ovs_fatal(0, "unknown log-io command \"%s\"", command);
322         }
323         if (error) {
324             char *s = ovsdb_error_to_string(error);
325             printf("%s: %s failed: %s\n", name, command, s);
326             free(s);
327             ovsdb_error_destroy(error);
328         } else {
329             printf("%s: %s successful\n", name, command);
330         }
331     }
332
333     ovsdb_log_close(log);
334 }
335
336 static void
337 do_default_atoms(struct ovs_cmdl_context *ctx OVS_UNUSED)
338 {
339     int type;
340
341     for (type = 0; type < OVSDB_N_TYPES; type++) {
342         union ovsdb_atom atom;
343
344         if (type == OVSDB_TYPE_VOID) {
345             continue;
346         }
347
348         printf("%s: ", ovsdb_atomic_type_to_string(type));
349
350         ovsdb_atom_init_default(&atom, type);
351         if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
352             printf("wrong\n");
353             exit(1);
354         }
355         ovsdb_atom_destroy(&atom, type);
356
357         printf("OK\n");
358     }
359 }
360
361 static void
362 do_default_data(struct ovs_cmdl_context *ctx OVS_UNUSED)
363 {
364     unsigned int n_min;
365     int key, value;
366
367     for (n_min = 0; n_min <= 1; n_min++) {
368         for (key = 0; key < OVSDB_N_TYPES; key++) {
369             if (key == OVSDB_TYPE_VOID) {
370                 continue;
371             }
372             for (value = 0; value < OVSDB_N_TYPES; value++) {
373                 struct ovsdb_datum datum;
374                 struct ovsdb_type type;
375
376                 ovsdb_base_type_init(&type.key, key);
377                 ovsdb_base_type_init(&type.value, value);
378                 type.n_min = n_min;
379                 type.n_max = 1;
380                 assert(ovsdb_type_is_valid(&type));
381
382                 printf("key %s, value %s, n_min %u: ",
383                        ovsdb_atomic_type_to_string(key),
384                        ovsdb_atomic_type_to_string(value), n_min);
385
386                 ovsdb_datum_init_default(&datum, &type);
387                 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
388                                         &type)) {
389                     printf("wrong\n");
390                     exit(1);
391                 }
392                 ovsdb_datum_destroy(&datum, &type);
393                 ovsdb_type_destroy(&type);
394
395                 printf("OK\n");
396             }
397         }
398     }
399 }
400
401 static void
402 do_diff_data(struct ovs_cmdl_context *ctx)
403 {
404     struct ovsdb_type type;
405     struct json *json;
406     struct ovsdb_datum new, old, diff, reincarnation;
407
408     json = unbox_json(parse_json(ctx->argv[1]));
409     check_ovsdb_error(ovsdb_type_from_json(&type, json));
410     json_destroy(json);
411
412     /* Arguments in pairs of 'old' and 'new'. */
413     for (int i = 2; i < ctx->argc - 1; i+=2) {
414         struct ovsdb_error *error;
415
416         json = unbox_json(parse_json(ctx->argv[i]));
417         check_ovsdb_error(ovsdb_datum_from_json(&old, &type, json, NULL));
418         json_destroy(json);
419
420         json = unbox_json(parse_json(ctx->argv[i+1]));
421         check_ovsdb_error(ovsdb_transient_datum_from_json(&new, &type, json));
422         json_destroy(json);
423
424         /* Generate the diff.  */
425         ovsdb_datum_diff(&diff, &old, &new, &type);
426
427         /* Apply diff to 'old' to create'reincarnation'. */
428         error = ovsdb_datum_apply_diff(&reincarnation, &old, &diff, &type);
429         if (error) {
430             char *string = ovsdb_error_to_string(error);
431             ovsdb_error_destroy(error);
432             ovs_fatal(0, "%s", string);
433         }
434
435         /* Test to make sure 'new' equals 'reincarnation'.  */
436         if (!ovsdb_datum_equals(&new, &reincarnation, &type)) {
437             ovs_fatal(0, "failed to apply diff");
438         }
439
440         /* Print diff */
441         json = ovsdb_datum_to_json(&diff, &type);
442         printf ("diff: ");
443         print_and_free_json(json);
444
445         /* Print reincarnation */
446         json = ovsdb_datum_to_json(&reincarnation, &type);
447         printf ("apply diff: ");
448         print_and_free_json(json);
449
450         ovsdb_datum_destroy(&new, &type);
451         ovsdb_datum_destroy(&old, &type);
452         ovsdb_datum_destroy(&diff, &type);
453         ovsdb_datum_destroy(&reincarnation, &type);
454
455         printf("OK\n");
456     }
457
458     ovsdb_type_destroy(&type);
459 }
460
461 static void
462 do_parse_atomic_type(struct ovs_cmdl_context *ctx)
463 {
464     enum ovsdb_atomic_type type;
465     struct json *json;
466
467     json = unbox_json(parse_json(ctx->argv[1]));
468     check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
469     json_destroy(json);
470     print_and_free_json(ovsdb_atomic_type_to_json(type));
471 }
472
473 static void
474 do_parse_base_type(struct ovs_cmdl_context *ctx)
475 {
476     struct ovsdb_base_type base;
477     struct json *json;
478
479     json = unbox_json(parse_json(ctx->argv[1]));
480     check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
481     json_destroy(json);
482     print_and_free_json(ovsdb_base_type_to_json(&base));
483     ovsdb_base_type_destroy(&base);
484 }
485
486 static void
487 do_parse_type(struct ovs_cmdl_context *ctx)
488 {
489     struct ovsdb_type type;
490     struct json *json;
491
492     json = unbox_json(parse_json(ctx->argv[1]));
493     check_ovsdb_error(ovsdb_type_from_json(&type, json));
494     json_destroy(json);
495     print_and_free_json(ovsdb_type_to_json(&type));
496     ovsdb_type_destroy(&type);
497 }
498
499 static void
500 do_parse_atoms(struct ovs_cmdl_context *ctx)
501 {
502     struct ovsdb_base_type base;
503     struct json *json;
504     int i;
505
506     json = unbox_json(parse_json(ctx->argv[1]));
507     check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
508     json_destroy(json);
509
510     for (i = 2; i < ctx->argc; i++) {
511         struct ovsdb_error *error;
512         union ovsdb_atom atom;
513
514         json = unbox_json(parse_json(ctx->argv[i]));
515         error = ovsdb_atom_from_json(&atom, &base, json, NULL);
516         json_destroy(json);
517
518         if (error) {
519             print_and_free_ovsdb_error(error);
520         } else {
521             print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
522             ovsdb_atom_destroy(&atom, base.type);
523         }
524     }
525     ovsdb_base_type_destroy(&base);
526 }
527
528 static void
529 do_parse_atom_strings(struct ovs_cmdl_context *ctx)
530 {
531     struct ovsdb_base_type base;
532     struct json *json;
533     int i;
534
535     json = unbox_json(parse_json(ctx->argv[1]));
536     check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
537     json_destroy(json);
538
539     for (i = 2; i < ctx->argc; i++) {
540         union ovsdb_atom atom;
541         struct ds out;
542
543         die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL));
544
545         ds_init(&out);
546         ovsdb_atom_to_string(&atom, base.type, &out);
547         puts(ds_cstr(&out));
548         ds_destroy(&out);
549
550         ovsdb_atom_destroy(&atom, base.type);
551     }
552     ovsdb_base_type_destroy(&base);
553 }
554
555 static void
556 do_parse_data__(int argc, char *argv[],
557                 struct ovsdb_error *
558                 (*parse)(struct ovsdb_datum *datum,
559                          const struct ovsdb_type *type,
560                          const struct json *json,
561                          struct ovsdb_symbol_table *symtab))
562 {
563     struct ovsdb_type type;
564     struct json *json;
565     int i;
566
567     json = unbox_json(parse_json(argv[1]));
568     check_ovsdb_error(ovsdb_type_from_json(&type, json));
569     json_destroy(json);
570
571     for (i = 2; i < argc; i++) {
572         struct ovsdb_datum datum;
573
574         json = unbox_json(parse_json(argv[i]));
575         check_ovsdb_error(parse(&datum, &type, json, NULL));
576         json_destroy(json);
577
578         print_and_free_json(ovsdb_datum_to_json(&datum, &type));
579
580         ovsdb_datum_destroy(&datum, &type);
581     }
582     ovsdb_type_destroy(&type);
583 }
584
585 static void
586 do_parse_data(struct ovs_cmdl_context *ctx)
587 {
588     do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json);
589 }
590
591 static void
592 do_parse_data_strings(struct ovs_cmdl_context *ctx)
593 {
594     struct ovsdb_type type;
595     struct json *json;
596     int i;
597
598     json = unbox_json(parse_json(ctx->argv[1]));
599     check_ovsdb_error(ovsdb_type_from_json(&type, json));
600     json_destroy(json);
601
602     for (i = 2; i < ctx->argc; i++) {
603         struct ovsdb_datum datum;
604         struct ds out;
605
606         die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL));
607
608         ds_init(&out);
609         ovsdb_datum_to_string(&datum, &type, &out);
610         puts(ds_cstr(&out));
611         ds_destroy(&out);
612
613         ovsdb_datum_destroy(&datum, &type);
614     }
615     ovsdb_type_destroy(&type);
616 }
617
618 static enum ovsdb_atomic_type compare_atoms_atomic_type;
619
620 static int
621 compare_atoms(const void *a_, const void *b_)
622 {
623     const union ovsdb_atom *a = a_;
624     const union ovsdb_atom *b = b_;
625
626     return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
627 }
628
629 static void
630 do_sort_atoms(struct ovs_cmdl_context *ctx)
631 {
632     struct ovsdb_base_type base;
633     union ovsdb_atom *atoms;
634     struct json *json, **json_atoms;
635     size_t n_atoms;
636     int i;
637
638     json = unbox_json(parse_json(ctx->argv[1]));
639     check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
640     json_destroy(json);
641
642     json = unbox_json(parse_json(ctx->argv[2]));
643     if (json->type != JSON_ARRAY) {
644         ovs_fatal(0, "second argument must be array");
645     }
646
647     /* Convert JSON atoms to internal representation. */
648     n_atoms = json->u.array.n;
649     atoms = xmalloc(n_atoms * sizeof *atoms);
650     for (i = 0; i < n_atoms; i++) {
651         check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
652                                                json->u.array.elems[i], NULL));
653     }
654     json_destroy(json);
655
656     /* Sort atoms. */
657     compare_atoms_atomic_type = base.type;
658     qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
659
660     /* Convert internal representation back to JSON. */
661     json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
662     for (i = 0; i < n_atoms; i++) {
663         json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
664         ovsdb_atom_destroy(&atoms[i], base.type);
665     }
666     print_and_free_json(json_array_create(json_atoms, n_atoms));
667     free(atoms);
668     ovsdb_base_type_destroy(&base);
669 }
670
671 static void
672 do_parse_column(struct ovs_cmdl_context *ctx)
673 {
674     struct ovsdb_column *column;
675     struct json *json;
676
677     json = parse_json(ctx->argv[2]);
678     check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column));
679     json_destroy(json);
680     print_and_free_json(ovsdb_column_to_json(column));
681     ovsdb_column_destroy(column);
682 }
683
684 static void
685 do_parse_table(struct ovs_cmdl_context *ctx)
686 {
687     struct ovsdb_table_schema *ts;
688     bool default_is_root;
689     struct json *json;
690
691     default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true");
692
693     json = parse_json(ctx->argv[2]);
694     check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts));
695     json_destroy(json);
696     print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
697     ovsdb_table_schema_destroy(ts);
698 }
699
700 static void
701 do_parse_rows(struct ovs_cmdl_context *ctx)
702 {
703     struct ovsdb_column_set all_columns;
704     struct ovsdb_table_schema *ts;
705     struct ovsdb_table *table;
706     struct json *json;
707     int i;
708
709     json = unbox_json(parse_json(ctx->argv[1]));
710     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
711     json_destroy(json);
712
713     table = ovsdb_table_create(ts);
714     ovsdb_column_set_init(&all_columns);
715     ovsdb_column_set_add_all(&all_columns, table);
716
717     for (i = 2; i < ctx->argc; i++) {
718         struct ovsdb_column_set columns;
719         struct ovsdb_row *row;
720
721         ovsdb_column_set_init(&columns);
722         row = ovsdb_row_create(table);
723
724         json = unbox_json(parse_json(ctx->argv[i]));
725         check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
726         json_destroy(json);
727
728         print_and_free_json(ovsdb_row_to_json(row, &all_columns));
729
730         if (columns.n_columns) {
731             struct svec names;
732             size_t j;
733             char *s;
734
735             svec_init(&names);
736             for (j = 0; j < columns.n_columns; j++) {
737                 svec_add(&names, columns.columns[j]->name);
738             }
739             svec_sort(&names);
740             s = svec_join(&names, ", ", "");
741             puts(s);
742             free(s);
743             svec_destroy(&names);
744         } else {
745             printf("<none>\n");
746         }
747
748         ovsdb_column_set_destroy(&columns);
749         ovsdb_row_destroy(row);
750     }
751
752     ovsdb_column_set_destroy(&all_columns);
753     ovsdb_table_destroy(table); /* Also destroys 'ts'. */
754 }
755
756 static void
757 do_compare_rows(struct ovs_cmdl_context *ctx)
758 {
759     struct ovsdb_column_set all_columns;
760     struct ovsdb_table_schema *ts;
761     struct ovsdb_table *table;
762     struct ovsdb_row **rows;
763     struct json *json;
764     char **names;
765     int n_rows;
766     int i, j;
767
768     json = unbox_json(parse_json(ctx->argv[1]));
769     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
770     json_destroy(json);
771
772     table = ovsdb_table_create(ts);
773     ovsdb_column_set_init(&all_columns);
774     ovsdb_column_set_add_all(&all_columns, table);
775
776     n_rows = ctx->argc - 2;
777     rows = xmalloc(sizeof *rows * n_rows);
778     names = xmalloc(sizeof *names * n_rows);
779     for (i = 0; i < n_rows; i++) {
780         rows[i] = ovsdb_row_create(table);
781
782         json = parse_json(ctx->argv[i + 2]);
783         if (json->type != JSON_ARRAY || json->u.array.n != 2
784             || json->u.array.elems[0]->type != JSON_STRING) {
785             ovs_fatal(0, "\"%s\" does not have expected form "
786                       "[\"name\", {data}]", ctx->argv[i]);
787         }
788         names[i] = xstrdup(json->u.array.elems[0]->u.string);
789         check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
790                                               NULL, NULL));
791         json_destroy(json);
792     }
793     for (i = 0; i < n_rows; i++) {
794         uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
795         for (j = i + 1; j < n_rows; j++) {
796             uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
797             if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
798                 printf("%s == %s\n", names[i], names[j]);
799                 if (i_hash != j_hash) {
800                     printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
801                     abort();
802                 }
803             } else if (i_hash == j_hash) {
804                 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
805             }
806         }
807     }
808     for (i = 0; i < n_rows; i++) {
809         ovsdb_row_destroy(rows[i]);
810         free(names[i]);
811     }
812     free(rows);
813     free(names);
814
815     ovsdb_column_set_destroy(&all_columns);
816     ovsdb_table_destroy(table); /* Also destroys 'ts'. */
817 }
818
819 static void
820 do_parse_conditions(struct ovs_cmdl_context *ctx)
821 {
822     struct ovsdb_table_schema *ts;
823     struct json *json;
824     int exit_code = 0;
825     int i;
826
827     json = unbox_json(parse_json(ctx->argv[1]));
828     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
829     json_destroy(json);
830
831     for (i = 2; i < ctx->argc; i++) {
832         struct ovsdb_condition cnd;
833         struct ovsdb_error *error;
834
835         json = parse_json(ctx->argv[i]);
836         error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
837         if (!error) {
838             print_and_free_json(ovsdb_condition_to_json(&cnd));
839         } else {
840             char *s = ovsdb_error_to_string(error);
841             ovs_error(0, "%s", s);
842             free(s);
843             ovsdb_error_destroy(error);
844             exit_code = 1;
845         }
846         json_destroy(json);
847
848         ovsdb_condition_destroy(&cnd);
849     }
850     ovsdb_table_schema_destroy(ts);
851
852     exit(exit_code);
853 }
854
855 static void
856 do_evaluate_conditions(struct ovs_cmdl_context *ctx)
857 {
858     struct ovsdb_table_schema *ts;
859     struct ovsdb_table *table;
860     struct ovsdb_condition *conditions;
861     size_t n_conditions;
862     struct ovsdb_row **rows;
863     size_t n_rows;
864     struct json *json;
865     size_t i, j;
866
867     /* Parse table schema, create table. */
868     json = unbox_json(parse_json(ctx->argv[1]));
869     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
870     json_destroy(json);
871
872     table = ovsdb_table_create(ts);
873
874     /* Parse conditions. */
875     json = parse_json(ctx->argv[2]);
876     if (json->type != JSON_ARRAY) {
877         ovs_fatal(0, "CONDITION argument is not JSON array");
878     }
879     n_conditions = json->u.array.n;
880     conditions = xmalloc(n_conditions * sizeof *conditions);
881     for (i = 0; i < n_conditions; i++) {
882         check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
883                                                     NULL, &conditions[i]));
884     }
885     json_destroy(json);
886
887     /* Parse rows. */
888     json = parse_json(ctx->argv[3]);
889     if (json->type != JSON_ARRAY) {
890         ovs_fatal(0, "ROW argument is not JSON array");
891     }
892     n_rows = json->u.array.n;
893     rows = xmalloc(n_rows * sizeof *rows);
894     for (i = 0; i < n_rows; i++) {
895         rows[i] = ovsdb_row_create(table);
896         check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
897                                               NULL, NULL));
898     }
899     json_destroy(json);
900
901     for (i = 0; i < n_conditions; i++) {
902         printf("condition %2"PRIuSIZE":", i);
903         for (j = 0; j < n_rows; j++) {
904             bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
905             if (j % 5 == 0) {
906                 putchar(' ');
907             }
908             putchar(result ? 'T' : '-');
909         }
910         printf("\n");
911     }
912
913     for (i = 0; i < n_conditions; i++) {
914         ovsdb_condition_destroy(&conditions[i]);
915     }
916     free(conditions);
917     for (i = 0; i < n_rows; i++) {
918         ovsdb_row_destroy(rows[i]);
919     }
920     free(rows);
921     ovsdb_table_destroy(table); /* Also destroys 'ts'. */
922 }
923
924 static void
925 do_parse_mutations(struct ovs_cmdl_context *ctx)
926 {
927     struct ovsdb_table_schema *ts;
928     struct json *json;
929     int exit_code = 0;
930     int i;
931
932     json = unbox_json(parse_json(ctx->argv[1]));
933     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
934     json_destroy(json);
935
936     for (i = 2; i < ctx->argc; i++) {
937         struct ovsdb_mutation_set set;
938         struct ovsdb_error *error;
939
940         json = parse_json(ctx->argv[i]);
941         error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
942         if (!error) {
943             print_and_free_json(ovsdb_mutation_set_to_json(&set));
944         } else {
945             char *s = ovsdb_error_to_string(error);
946             ovs_error(0, "%s", s);
947             free(s);
948             ovsdb_error_destroy(error);
949             exit_code = 1;
950         }
951         json_destroy(json);
952
953         ovsdb_mutation_set_destroy(&set);
954     }
955     ovsdb_table_schema_destroy(ts);
956
957     exit(exit_code);
958 }
959
960 static void
961 do_execute_mutations(struct ovs_cmdl_context *ctx)
962 {
963     struct ovsdb_table_schema *ts;
964     struct ovsdb_table *table;
965     struct ovsdb_mutation_set *sets;
966     size_t n_sets;
967     struct ovsdb_row **rows;
968     size_t n_rows;
969     struct json *json;
970     size_t i, j;
971
972     /* Parse table schema, create table. */
973     json = unbox_json(parse_json(ctx->argv[1]));
974     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
975     json_destroy(json);
976
977     table = ovsdb_table_create(ts);
978
979     /* Parse mutations. */
980     json = parse_json(ctx->argv[2]);
981     if (json->type != JSON_ARRAY) {
982         ovs_fatal(0, "MUTATION argument is not JSON array");
983     }
984     n_sets = json->u.array.n;
985     sets = xmalloc(n_sets * sizeof *sets);
986     for (i = 0; i < n_sets; i++) {
987         check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
988                                                        json->u.array.elems[i],
989                                                        NULL, &sets[i]));
990     }
991     json_destroy(json);
992
993     /* Parse rows. */
994     json = parse_json(ctx->argv[3]);
995     if (json->type != JSON_ARRAY) {
996         ovs_fatal(0, "ROW argument is not JSON array");
997     }
998     n_rows = json->u.array.n;
999     rows = xmalloc(n_rows * sizeof *rows);
1000     for (i = 0; i < n_rows; i++) {
1001         rows[i] = ovsdb_row_create(table);
1002         check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
1003                                               NULL, NULL));
1004     }
1005     json_destroy(json);
1006
1007     for (i = 0; i < n_sets; i++) {
1008         printf("mutation %2"PRIuSIZE":\n", i);
1009         for (j = 0; j < n_rows; j++) {
1010             struct ovsdb_error *error;
1011             struct ovsdb_row *row;
1012
1013             row = ovsdb_row_clone(rows[j]);
1014             error = ovsdb_mutation_set_execute(row, &sets[i]);
1015
1016             printf("row %"PRIuSIZE": ", j);
1017             if (error) {
1018                 print_and_free_ovsdb_error(error);
1019             } else {
1020                 struct ovsdb_column_set columns;
1021                 struct shash_node *node;
1022
1023                 ovsdb_column_set_init(&columns);
1024                 SHASH_FOR_EACH (node, &ts->columns) {
1025                     struct ovsdb_column *c = node->data;
1026                     if (!ovsdb_datum_equals(&row->fields[c->index],
1027                                             &rows[j]->fields[c->index],
1028                                             &c->type)) {
1029                         ovsdb_column_set_add(&columns, c);
1030                     }
1031                 }
1032                 if (columns.n_columns) {
1033                     print_and_free_json(ovsdb_row_to_json(row, &columns));
1034                 } else {
1035                     printf("no change\n");
1036                 }
1037                 ovsdb_column_set_destroy(&columns);
1038             }
1039             ovsdb_row_destroy(row);
1040         }
1041         printf("\n");
1042     }
1043
1044     for (i = 0; i < n_sets; i++) {
1045         ovsdb_mutation_set_destroy(&sets[i]);
1046     }
1047     free(sets);
1048     for (i = 0; i < n_rows; i++) {
1049         ovsdb_row_destroy(rows[i]);
1050     }
1051     free(rows);
1052     ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1053 }
1054
1055 /* Inserts a row, without bothering to update metadata such as refcounts. */
1056 static void
1057 put_row(struct ovsdb_table *table, struct ovsdb_row *row)
1058 {
1059     const struct uuid *uuid = ovsdb_row_get_uuid(row);
1060     if (!ovsdb_table_get_row(table, uuid)) {
1061         hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1062     }
1063 }
1064
1065 struct do_query_cbdata {
1066     struct uuid *row_uuids;
1067     int *counts;
1068     size_t n_rows;
1069 };
1070
1071 static bool
1072 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
1073 {
1074     struct do_query_cbdata *cbdata = cbdata_;
1075     size_t i;
1076
1077     for (i = 0; i < cbdata->n_rows; i++) {
1078         if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1079             cbdata->counts[i]++;
1080         }
1081     }
1082
1083     return true;
1084 }
1085
1086 static void
1087 do_query(struct ovs_cmdl_context *ctx)
1088 {
1089     struct do_query_cbdata cbdata;
1090     struct ovsdb_table_schema *ts;
1091     struct ovsdb_table *table;
1092     struct json *json;
1093     int exit_code = 0;
1094     size_t i;
1095
1096     /* Parse table schema, create table. */
1097     json = unbox_json(parse_json(ctx->argv[1]));
1098     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1099     json_destroy(json);
1100
1101     table = ovsdb_table_create(ts);
1102
1103     /* Parse rows, add to table. */
1104     json = parse_json(ctx->argv[2]);
1105     if (json->type != JSON_ARRAY) {
1106         ovs_fatal(0, "ROW argument is not JSON array");
1107     }
1108     cbdata.n_rows = json->u.array.n;
1109     cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1110     cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1111     for (i = 0; i < cbdata.n_rows; i++) {
1112         struct ovsdb_row *row = ovsdb_row_create(table);
1113         uuid_generate(ovsdb_row_get_uuid_rw(row));
1114         check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1115                                               NULL, NULL));
1116         if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1117             ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1118                       UUID_ARGS(ovsdb_row_get_uuid(row)));
1119         }
1120         cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1121         put_row(table, row);
1122     }
1123     json_destroy(json);
1124
1125     /* Parse conditions and execute queries. */
1126     json = parse_json(ctx->argv[3]);
1127     if (json->type != JSON_ARRAY) {
1128         ovs_fatal(0, "CONDITION argument is not JSON array");
1129     }
1130     for (i = 0; i < json->u.array.n; i++) {
1131         struct ovsdb_condition cnd;
1132         size_t j;
1133
1134         check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1135                                                     NULL, &cnd));
1136
1137         memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1138         ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1139
1140         printf("query %2"PRIuSIZE":", i);
1141         for (j = 0; j < cbdata.n_rows; j++) {
1142             if (j % 5 == 0) {
1143                 putchar(' ');
1144             }
1145             if (cbdata.counts[j]) {
1146                 printf("%d", cbdata.counts[j]);
1147                 if (cbdata.counts[j] > 1) {
1148                     /* Dup! */
1149                     exit_code = 1;
1150                 }
1151             } else {
1152                 putchar('-');
1153             }
1154         }
1155         putchar('\n');
1156
1157         ovsdb_condition_destroy(&cnd);
1158     }
1159     json_destroy(json);
1160
1161     ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1162
1163     exit(exit_code);
1164 }
1165
1166 struct do_query_distinct_class {
1167     struct ovsdb_row *example;
1168     int count;
1169 };
1170
1171 struct do_query_distinct_row {
1172     struct uuid uuid;
1173     struct do_query_distinct_class *class;
1174 };
1175
1176 static void
1177 do_query_distinct(struct ovs_cmdl_context *ctx)
1178 {
1179     struct ovsdb_column_set columns;
1180     struct ovsdb_table_schema *ts;
1181     struct ovsdb_table *table;
1182     struct do_query_distinct_row *rows;
1183     size_t n_rows;
1184     struct do_query_distinct_class *classes;
1185     size_t n_classes;
1186     struct json *json;
1187     int exit_code = 0;
1188     size_t i;
1189
1190     /* Parse table schema, create table. */
1191     json = unbox_json(parse_json(ctx->argv[1]));
1192     check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1193     json_destroy(json);
1194
1195     table = ovsdb_table_create(ts);
1196
1197     /* Parse column set. */
1198     json = parse_json(ctx->argv[4]);
1199     check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1200                                                  &columns));
1201     json_destroy(json);
1202
1203     /* Parse rows, add to table. */
1204     json = parse_json(ctx->argv[2]);
1205     if (json->type != JSON_ARRAY) {
1206         ovs_fatal(0, "ROW argument is not JSON array");
1207     }
1208     n_rows = json->u.array.n;
1209     rows = xmalloc(n_rows * sizeof *rows);
1210     classes = xmalloc(n_rows * sizeof *classes);
1211     n_classes = 0;
1212     for (i = 0; i < n_rows; i++) {
1213         struct ovsdb_row *row;
1214         size_t j;
1215
1216         /* Parse row. */
1217         row = ovsdb_row_create(table);
1218         uuid_generate(ovsdb_row_get_uuid_rw(row));
1219         check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1220                                               NULL, NULL));
1221
1222         /* Initialize row and find equivalence class. */
1223         rows[i].uuid = *ovsdb_row_get_uuid(row);
1224         rows[i].class = NULL;
1225         for (j = 0; j < n_classes; j++) {
1226             if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1227                 rows[i].class = &classes[j];
1228                 break;
1229             }
1230         }
1231         if (!rows[i].class) {
1232             rows[i].class = &classes[n_classes];
1233             classes[n_classes].example = ovsdb_row_clone(row);
1234             n_classes++;
1235         }
1236
1237         /* Add row to table. */
1238         if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1239             ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1240                       UUID_ARGS(ovsdb_row_get_uuid(row)));
1241         }
1242         put_row(table, row);
1243
1244     }
1245     json_destroy(json);
1246
1247     /* Parse conditions and execute queries. */
1248     json = parse_json(ctx->argv[3]);
1249     if (json->type != JSON_ARRAY) {
1250         ovs_fatal(0, "CONDITION argument is not JSON array");
1251     }
1252     for (i = 0; i < json->u.array.n; i++) {
1253         struct ovsdb_row_set results;
1254         struct ovsdb_condition cnd;
1255         size_t j;
1256
1257         check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1258                                                     NULL, &cnd));
1259
1260         for (j = 0; j < n_classes; j++) {
1261             classes[j].count = 0;
1262         }
1263         ovsdb_row_set_init(&results);
1264         ovsdb_query_distinct(table, &cnd, &columns, &results);
1265         for (j = 0; j < results.n_rows; j++) {
1266             size_t k;
1267
1268             for (k = 0; k < n_rows; k++) {
1269                 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1270                                 &rows[k].uuid)) {
1271                     rows[k].class->count++;
1272                 }
1273             }
1274         }
1275         ovsdb_row_set_destroy(&results);
1276
1277         printf("query %2"PRIuSIZE":", i);
1278         for (j = 0; j < n_rows; j++) {
1279             int count = rows[j].class->count;
1280
1281             if (j % 5 == 0) {
1282                 putchar(' ');
1283             }
1284             if (count > 1) {
1285                 /* Dup! */
1286                 printf("%d", count);
1287                 exit_code = 1;
1288             } else if (count == 1) {
1289                 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1290             } else {
1291                 putchar('-');
1292             }
1293         }
1294         putchar('\n');
1295
1296         ovsdb_condition_destroy(&cnd);
1297     }
1298     json_destroy(json);
1299
1300     for (i = 0; i < n_classes; i++) {
1301         ovsdb_row_destroy(classes[i].example);
1302     }
1303
1304     ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1305
1306     free(rows);
1307     free(classes);
1308     exit(exit_code);
1309 }
1310
1311 static void
1312 do_parse_schema(struct ovs_cmdl_context *ctx)
1313 {
1314     struct ovsdb_schema *schema;
1315     struct json *json;
1316
1317     json = parse_json(ctx->argv[1]);
1318     check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1319     json_destroy(json);
1320     print_and_free_json(ovsdb_schema_to_json(schema));
1321     ovsdb_schema_destroy(schema);
1322 }
1323
1324 static void
1325 do_execute(struct ovs_cmdl_context *ctx)
1326 {
1327     struct ovsdb_schema *schema;
1328     struct json *json;
1329     struct ovsdb *db;
1330     int i;
1331
1332     /* Create database. */
1333     json = parse_json(ctx->argv[1]);
1334     check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1335     json_destroy(json);
1336     db = ovsdb_create(schema);
1337
1338     for (i = 2; i < ctx->argc; i++) {
1339         struct json *params, *result;
1340         char *s;
1341
1342         params = parse_json(ctx->argv[i]);
1343         result = ovsdb_execute(db, NULL, params, 0, NULL);
1344         s = json_to_string(result, JSSF_SORT);
1345         printf("%s\n", s);
1346         free(s);
1347         json_destroy(params);
1348         json_destroy(result);
1349     }
1350
1351     ovsdb_destroy(db);
1352 }
1353
1354 struct test_trigger {
1355     struct ovsdb_trigger trigger;
1356     int number;
1357 };
1358
1359 static void
1360 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1361 {
1362     struct json *result;
1363     char *s;
1364
1365     result = ovsdb_trigger_steal_result(&t->trigger);
1366     s = json_to_string(result, JSSF_SORT);
1367     printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1368     free(s);
1369     json_destroy(result);
1370     ovsdb_trigger_destroy(&t->trigger);
1371     free(t);
1372 }
1373
1374 static void
1375 do_trigger(struct ovs_cmdl_context *ctx)
1376 {
1377     struct ovsdb_schema *schema;
1378     struct ovsdb_session session;
1379     struct ovsdb_server server;
1380     struct json *json;
1381     struct ovsdb *db;
1382     long long int now;
1383     int number;
1384     int i;
1385
1386     /* Create database. */
1387     json = parse_json(ctx->argv[1]);
1388     check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1389     json_destroy(json);
1390     db = ovsdb_create(schema);
1391
1392     ovsdb_server_init(&server);
1393     ovsdb_server_add_db(&server, db);
1394     ovsdb_session_init(&session, &server);
1395
1396     now = 0;
1397     number = 0;
1398     for (i = 2; i < ctx->argc; i++) {
1399         struct json *params = parse_json(ctx->argv[i]);
1400         if (params->type == JSON_ARRAY
1401             && json_array(params)->n == 2
1402             && json_array(params)->elems[0]->type == JSON_STRING
1403             && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1404             && json_array(params)->elems[1]->type == JSON_INTEGER) {
1405             now += json_integer(json_array(params)->elems[1]);
1406             json_destroy(params);
1407         } else {
1408             struct test_trigger *t = xmalloc(sizeof *t);
1409             ovsdb_trigger_init(&session, db, &t->trigger, params, now);
1410             t->number = number++;
1411             if (ovsdb_trigger_is_complete(&t->trigger)) {
1412                 do_trigger_dump(t, now, "immediate");
1413             } else {
1414                 printf("t=%lld: new trigger %d\n", now, t->number);
1415             }
1416         }
1417
1418         ovsdb_trigger_run(db, now);
1419         while (!ovs_list_is_empty(&session.completions)) {
1420             do_trigger_dump(CONTAINER_OF(ovs_list_pop_front(&session.completions),
1421                                          struct test_trigger, trigger.node),
1422                             now, "delayed");
1423         }
1424
1425         ovsdb_trigger_wait(db, now);
1426         poll_immediate_wake();
1427         poll_block();
1428     }
1429
1430     ovsdb_server_destroy(&server);
1431     ovsdb_destroy(db);
1432 }
1433
1434 static void
1435 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
1436 {
1437     usage();
1438 }
1439 \f
1440 /* "transact" command. */
1441
1442 static struct ovsdb *do_transact_db;
1443 static struct ovsdb_txn *do_transact_txn;
1444 static struct ovsdb_table *do_transact_table;
1445
1446 static void
1447 do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED)
1448 {
1449     ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
1450     do_transact_txn = NULL;
1451 }
1452
1453 static void
1454 do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED)
1455 {
1456     ovsdb_txn_abort(do_transact_txn);
1457     do_transact_txn = NULL;
1458 }
1459
1460 static void
1461 uuid_from_integer(int integer, struct uuid *uuid)
1462 {
1463     uuid_zero(uuid);
1464     uuid->parts[3] = integer;
1465 }
1466
1467 static const struct ovsdb_row *
1468 do_transact_find_row(const char *uuid_string)
1469 {
1470     const struct ovsdb_row *row;
1471     struct uuid uuid;
1472
1473     uuid_from_integer(atoi(uuid_string), &uuid);
1474     row = ovsdb_table_get_row(do_transact_table, &uuid);
1475     if (!row) {
1476         ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1477                   UUID_ARGS(&uuid));
1478     }
1479     return row;
1480 }
1481
1482 static void
1483 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1484                         int integer)
1485 {
1486     if (integer != -1) {
1487         const struct ovsdb_column *column;
1488
1489         column = ovsdb_table_schema_get_column(do_transact_table->schema,
1490                                                column_name);
1491         row->fields[column->index].keys[0].integer = integer;
1492     }
1493 }
1494
1495 static int
1496 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1497 {
1498     const struct ovsdb_column *column;
1499
1500     column = ovsdb_table_schema_get_column(do_transact_table->schema,
1501                                            column_name);
1502     return row->fields[column->index].keys[0].integer;
1503 }
1504
1505 static void
1506 do_transact_set_i_j(struct ovsdb_row *row,
1507                     const char *i_string, const char *j_string)
1508 {
1509     do_transact_set_integer(row, "i", atoi(i_string));
1510     do_transact_set_integer(row, "j", atoi(j_string));
1511 }
1512
1513 static void
1514 do_transact_insert(struct ovs_cmdl_context *ctx)
1515 {
1516     struct ovsdb_row *row;
1517     struct uuid *uuid;
1518
1519     row = ovsdb_row_create(do_transact_table);
1520
1521     /* Set UUID. */
1522     uuid = ovsdb_row_get_uuid_rw(row);
1523     uuid_from_integer(atoi(ctx->argv[1]), uuid);
1524     if (ovsdb_table_get_row(do_transact_table, uuid)) {
1525         ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1526                   UUID_ARGS(uuid));
1527     }
1528
1529     do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]);
1530
1531     /* Insert row. */
1532     ovsdb_txn_row_insert(do_transact_txn, row);
1533 }
1534
1535 static void
1536 do_transact_delete(struct ovs_cmdl_context *ctx)
1537 {
1538     const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]);
1539     ovsdb_txn_row_delete(do_transact_txn, row);
1540 }
1541
1542 static void
1543 do_transact_modify(struct ovs_cmdl_context *ctx)
1544 {
1545     const struct ovsdb_row *row_ro;
1546     struct ovsdb_row *row_rw;
1547
1548     row_ro = do_transact_find_row(ctx->argv[1]);
1549     row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1550     do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]);
1551 }
1552
1553 static int
1554 compare_rows_by_uuid(const void *a_, const void *b_)
1555 {
1556     struct ovsdb_row *const *ap = a_;
1557     struct ovsdb_row *const *bp = b_;
1558
1559     return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1560 }
1561
1562 static void
1563 do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED)
1564 {
1565     const struct ovsdb_row **rows;
1566     const struct ovsdb_row *row;
1567     size_t n_rows;
1568     size_t i;
1569
1570     n_rows = hmap_count(&do_transact_table->rows);
1571     rows = xmalloc(n_rows * sizeof *rows);
1572     i = 0;
1573     HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
1574         rows[i++] = row;
1575     }
1576     assert(i == n_rows);
1577
1578     qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1579
1580     for (i = 0; i < n_rows; i++) {
1581         printf("\n%"PRId32": i=%d, j=%d",
1582                ovsdb_row_get_uuid(rows[i])->parts[3],
1583                do_transact_get_integer(rows[i], "i"),
1584                do_transact_get_integer(rows[i], "j"));
1585     }
1586
1587     free(rows);
1588 }
1589
1590 static void
1591 do_transact(struct ovs_cmdl_context *ctx)
1592 {
1593     static const struct ovs_cmdl_command do_transact_commands[] = {
1594         { "commit", NULL, 0, 0, do_transact_commit },
1595         { "abort", NULL, 0, 0, do_transact_abort },
1596         { "insert", NULL, 2, 3, do_transact_insert },
1597         { "delete", NULL, 1, 1, do_transact_delete },
1598         { "modify", NULL, 2, 3, do_transact_modify },
1599         { "print", NULL, 0, 0, do_transact_print },
1600         { NULL, NULL, 0, 0, NULL },
1601     };
1602
1603     struct ovsdb_schema *schema;
1604     struct json *json;
1605     int i;
1606
1607     /* Create table. */
1608     json = parse_json("{\"name\": \"testdb\", "
1609                       " \"tables\": "
1610                       "  {\"mytable\": "
1611                       "    {\"columns\": "
1612                       "      {\"i\": {\"type\": \"integer\"}, "
1613                       "       \"j\": {\"type\": \"integer\"}}}}}");
1614     check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1615     json_destroy(json);
1616     do_transact_db = ovsdb_create(schema);
1617     do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1618     assert(do_transact_table != NULL);
1619
1620     for (i = 1; i < ctx->argc; i++) {
1621         struct json *command;
1622         size_t n_args;
1623         char **args;
1624         int j;
1625         struct ovs_cmdl_context transact_ctx = { .argc = 0, };
1626
1627         command = parse_json(ctx->argv[i]);
1628         if (command->type != JSON_ARRAY) {
1629             ovs_fatal(0, "transaction %d must be JSON array "
1630                       "with at least 1 element", i);
1631         }
1632
1633         n_args = command->u.array.n;
1634         args = xmalloc((n_args + 1) * sizeof *args);
1635         for (j = 0; j < n_args; j++) {
1636             struct json *s = command->u.array.elems[j];
1637             if (s->type != JSON_STRING) {
1638                 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1639                           i, j);
1640             }
1641             args[j] = xstrdup(json_string(s));
1642         }
1643         args[n_args] = NULL;
1644
1645         if (!do_transact_txn) {
1646             do_transact_txn = ovsdb_txn_create(do_transact_db);
1647         }
1648
1649         for (j = 0; j < n_args; j++) {
1650             if (j) {
1651                 putchar(' ');
1652             }
1653             fputs(args[j], stdout);
1654         }
1655         fputs(":", stdout);
1656         transact_ctx.argc = n_args;
1657         transact_ctx.argv = args;
1658         ovs_cmdl_run_command(&transact_ctx, do_transact_commands);
1659         putchar('\n');
1660
1661         for (j = 0; j < n_args; j++) {
1662             free(args[j]);
1663         }
1664         free(args);
1665         json_destroy(command);
1666     }
1667     ovsdb_txn_abort(do_transact_txn);
1668     ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1669 }
1670
1671 static int
1672 compare_link1(const void *a_, const void *b_)
1673 {
1674     const struct idltest_link1 *const *ap = a_;
1675     const struct idltest_link1 *const *bp = b_;
1676     const struct idltest_link1 *a = *ap;
1677     const struct idltest_link1 *b = *bp;
1678
1679     return a->i < b->i ? -1 : a->i > b->i;
1680 }
1681
1682 static void
1683 print_idl_row_updated_simple(const struct idltest_simple *s, int step)
1684 {
1685     size_t i;
1686     bool updated = false;
1687
1688     for (i = 0; i < IDLTEST_SIMPLE_N_COLUMNS; i++) {
1689         if (idltest_simple_is_updated(s, i)) {
1690             if (!updated) {
1691                 printf("%03d: updated columns:", step);
1692                 updated = true;
1693             }
1694             printf(" %s", idltest_simple_columns[i].name);
1695         }
1696     }
1697     if (updated) {
1698         printf("\n");
1699     }
1700 }
1701
1702 static void
1703 print_idl_row_updated_link1(const struct idltest_link1 *l1, int step)
1704 {
1705     size_t i;
1706     bool updated = false;
1707
1708     for (i = 0; i < IDLTEST_LINK1_N_COLUMNS; i++) {
1709         if (idltest_link1_is_updated(l1, i)) {
1710             if (!updated) {
1711                 printf("%03d: updated columns:", step);
1712                 updated = true;
1713             }
1714             printf(" %s", idltest_link1_columns[i].name);
1715         }
1716     }
1717     if (updated) {
1718         printf("\n");
1719     }
1720 }
1721
1722 static void
1723 print_idl_row_updated_link2(const struct idltest_link2 *l2, int step)
1724 {
1725     size_t i;
1726     bool updated = false;
1727
1728     for (i = 0; i < IDLTEST_LINK2_N_COLUMNS; i++) {
1729         if (idltest_link2_is_updated(l2, i)) {
1730             if (!updated) {
1731                 printf("%03d: updated columns:", step);
1732                 updated = true;
1733             }
1734             printf(" %s", idltest_link2_columns[i].name);
1735         }
1736     }
1737     if (updated) {
1738         printf("\n");
1739     }
1740 }
1741
1742 static void
1743 print_idl_row_simple(const struct idltest_simple *s, int step)
1744 {
1745     size_t i;
1746
1747     printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1748            step, s->i, s->r, s->b ? "true" : "false",
1749            s->s, UUID_ARGS(&s->u));
1750     for (i = 0; i < s->n_ia; i++) {
1751         printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1752     }
1753     printf("] ra=[");
1754     for (i = 0; i < s->n_ra; i++) {
1755         printf("%s%g", i ? " " : "", s->ra[i]);
1756     }
1757     printf("] ba=[");
1758     for (i = 0; i < s->n_ba; i++) {
1759         printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1760     }
1761     printf("] sa=[");
1762     for (i = 0; i < s->n_sa; i++) {
1763         printf("%s%s", i ? " " : "", s->sa[i]);
1764     }
1765     printf("] ua=[");
1766     for (i = 0; i < s->n_ua; i++) {
1767         printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1768     }
1769     printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1770     print_idl_row_updated_simple(s, step);
1771 }
1772
1773 static void
1774 print_idl_row_link1(const struct idltest_link1 *l1, int step)
1775 {
1776     struct idltest_link1 **links;
1777     size_t i;
1778
1779     printf("%03d: i=%"PRId64" k=", step, l1->i);
1780     if (l1->k) {
1781         printf("%"PRId64, l1->k->i);
1782     }
1783     printf(" ka=[");
1784     links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1785     qsort(links, l1->n_ka, sizeof *links, compare_link1);
1786     for (i = 0; i < l1->n_ka; i++) {
1787         printf("%s%"PRId64, i ? " " : "", links[i]->i);
1788     }
1789     free(links);
1790     printf("] l2=");
1791     if (l1->l2) {
1792         printf("%"PRId64, l1->l2->i);
1793     }
1794     printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1795     print_idl_row_updated_link1(l1, step);
1796 }
1797
1798 static void
1799 print_idl_row_link2(const struct idltest_link2 *l2, int step)
1800 {
1801     printf("%03d: i=%"PRId64" l1=", step, l2->i);
1802     if (l2->l1) {
1803         printf("%"PRId64, l2->l1->i);
1804     }
1805     printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1806     print_idl_row_updated_link2(l2, step);
1807 }
1808
1809 static void
1810 print_idl(struct ovsdb_idl *idl, int step)
1811 {
1812     const struct idltest_simple *s;
1813     const struct idltest_link1 *l1;
1814     const struct idltest_link2 *l2;
1815     int n = 0;
1816
1817     IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1818         print_idl_row_simple(s, step);
1819         n++;
1820     }
1821     IDLTEST_LINK1_FOR_EACH (l1, idl) {
1822         print_idl_row_link1(l1, step);
1823         n++;
1824     }
1825     IDLTEST_LINK2_FOR_EACH (l2, idl) {
1826         print_idl_row_link2(l2, step);
1827         n++;
1828     }
1829     if (!n) {
1830         printf("%03d: empty\n", step);
1831     }
1832 }
1833
1834 static void
1835 print_idl_track(struct ovsdb_idl *idl, int step, unsigned int seqno)
1836 {
1837     const struct idltest_simple *s;
1838     const struct idltest_link1 *l1;
1839     const struct idltest_link2 *l2;
1840     int n = 0;
1841
1842     IDLTEST_SIMPLE_FOR_EACH_TRACKED (s, idl) {
1843         if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1844             printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1845         } else {
1846             print_idl_row_simple(s, step);
1847         }
1848         n++;
1849     }
1850     IDLTEST_LINK1_FOR_EACH_TRACKED (l1, idl) {
1851         if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1852             printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1853         } else {
1854             print_idl_row_link1(l1, step);
1855         }
1856         n++;
1857     }
1858     IDLTEST_LINK2_FOR_EACH_TRACKED (l2, idl) {
1859         if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1860             printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1861         } else {
1862             print_idl_row_link2(l2, step);
1863         }
1864         n++;
1865     }
1866     if (!n) {
1867         printf("%03d: empty\n", step);
1868     }
1869 }
1870
1871 static void
1872 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1873             size_t *n)
1874 {
1875     struct uuid uuid;
1876
1877     if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1878         char *name = xasprintf("#%"PRIuSIZE"#", *n);
1879         fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1880         ovsdb_symbol_table_put(symtab, name, &uuid, false);
1881         free(name);
1882         *n += 1;
1883     } else if (json->type == JSON_ARRAY) {
1884         size_t i;
1885
1886         for (i = 0; i < json->u.array.n; i++) {
1887             parse_uuids(json->u.array.elems[i], symtab, n);
1888         }
1889     } else if (json->type == JSON_OBJECT) {
1890         const struct shash_node *node;
1891
1892         SHASH_FOR_EACH (node, json_object(json)) {
1893             parse_uuids(node->data, symtab, n);
1894         }
1895     }
1896 }
1897
1898 static void
1899 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1900 {
1901     if (json->type == JSON_STRING) {
1902         const struct ovsdb_symbol *symbol;
1903
1904         symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1905         if (symbol) {
1906             free(json->u.string);
1907             json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1908         }
1909     } else if (json->type == JSON_ARRAY) {
1910         size_t i;
1911
1912         for (i = 0; i < json->u.array.n; i++) {
1913             substitute_uuids(json->u.array.elems[i], symtab);
1914         }
1915     } else if (json->type == JSON_OBJECT) {
1916         const struct shash_node *node;
1917
1918         SHASH_FOR_EACH (node, json_object(json)) {
1919             substitute_uuids(node->data, symtab);
1920         }
1921     }
1922 }
1923
1924 static const struct idltest_simple *
1925 idltest_find_simple(struct ovsdb_idl *idl, int i)
1926 {
1927     const struct idltest_simple *s;
1928
1929     IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1930         if (s->i == i) {
1931             return s;
1932         }
1933     }
1934     return NULL;
1935 }
1936
1937 static void
1938 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1939 {
1940     char *cmd, *save_ptr1 = NULL;
1941     struct ovsdb_idl_txn *txn;
1942     enum ovsdb_idl_txn_status status;
1943     bool increment = false;
1944
1945     txn = ovsdb_idl_txn_create(idl);
1946     for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1947          cmd = strtok_r(NULL, ",", &save_ptr1)) {
1948         char *save_ptr2 = NULL;
1949         char *name, *arg1, *arg2, *arg3;
1950
1951         name = strtok_r(cmd, " ", &save_ptr2);
1952         arg1 = strtok_r(NULL, " ", &save_ptr2);
1953         arg2 = strtok_r(NULL, " ", &save_ptr2);
1954         arg3 = strtok_r(NULL, " ", &save_ptr2);
1955
1956         if (!strcmp(name, "set")) {
1957             const struct idltest_simple *s;
1958
1959             if (!arg3) {
1960                 ovs_fatal(0, "\"set\" command requires 3 arguments");
1961             }
1962
1963             s = idltest_find_simple(idl, atoi(arg1));
1964             if (!s) {
1965                 ovs_fatal(0, "\"set\" command asks for nonexistent "
1966                           "i=%d", atoi(arg1));
1967             }
1968
1969             if (!strcmp(arg2, "b")) {
1970                 idltest_simple_set_b(s, atoi(arg3));
1971             } else if (!strcmp(arg2, "s")) {
1972                 idltest_simple_set_s(s, arg3);
1973             } else if (!strcmp(arg2, "u")) {
1974                 struct uuid uuid;
1975                 if (!uuid_from_string(&uuid, arg3)) {
1976                     ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
1977                 }
1978                 idltest_simple_set_u(s, uuid);
1979             } else if (!strcmp(arg2, "r")) {
1980                 idltest_simple_set_r(s, atof(arg3));
1981             } else {
1982                 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1983                           arg2);
1984             }
1985         } else if (!strcmp(name, "insert")) {
1986             struct idltest_simple *s;
1987
1988             if (!arg1 || arg2) {
1989                 ovs_fatal(0, "\"insert\" command requires 1 argument");
1990             }
1991
1992             s = idltest_simple_insert(txn);
1993             idltest_simple_set_i(s, atoi(arg1));
1994         } else if (!strcmp(name, "delete")) {
1995             const struct idltest_simple *s;
1996
1997             if (!arg1 || arg2) {
1998                 ovs_fatal(0, "\"delete\" command requires 1 argument");
1999             }
2000
2001             s = idltest_find_simple(idl, atoi(arg1));
2002             if (!s) {
2003                 ovs_fatal(0, "\"delete\" command asks for nonexistent "
2004                           "i=%d", atoi(arg1));
2005             }
2006             idltest_simple_delete(s);
2007         } else if (!strcmp(name, "verify")) {
2008             const struct idltest_simple *s;
2009
2010             if (!arg2 || arg3) {
2011                 ovs_fatal(0, "\"verify\" command requires 2 arguments");
2012             }
2013
2014             s = idltest_find_simple(idl, atoi(arg1));
2015             if (!s) {
2016                 ovs_fatal(0, "\"verify\" command asks for nonexistent "
2017                           "i=%d", atoi(arg1));
2018             }
2019
2020             if (!strcmp(arg2, "i")) {
2021                 idltest_simple_verify_i(s);
2022             } else if (!strcmp(arg2, "b")) {
2023                 idltest_simple_verify_b(s);
2024             } else if (!strcmp(arg2, "s")) {
2025                 idltest_simple_verify_s(s);
2026             } else if (!strcmp(arg2, "u")) {
2027                 idltest_simple_verify_s(s);
2028             } else if (!strcmp(arg2, "r")) {
2029                 idltest_simple_verify_r(s);
2030             } else {
2031                 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
2032                           arg2);
2033             }
2034         } else if (!strcmp(name, "increment")) {
2035             const struct idltest_simple *s;
2036
2037             if (!arg1 || arg2) {
2038                 ovs_fatal(0, "\"increment\" command requires 1 argument");
2039             }
2040
2041             s = idltest_find_simple(idl, atoi(arg1));
2042             if (!s) {
2043                 ovs_fatal(0, "\"set\" command asks for nonexistent "
2044                           "i=%d", atoi(arg1));
2045             }
2046
2047             ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i);
2048             increment = true;
2049         } else if (!strcmp(name, "abort")) {
2050             ovsdb_idl_txn_abort(txn);
2051             break;
2052         } else if (!strcmp(name, "destroy")) {
2053             printf("%03d: destroy\n", step);
2054             ovsdb_idl_txn_destroy(txn);
2055             return;
2056         } else {
2057             ovs_fatal(0, "unknown command %s", name);
2058         }
2059     }
2060
2061     status = ovsdb_idl_txn_commit_block(txn);
2062     printf("%03d: commit, status=%s",
2063            step, ovsdb_idl_txn_status_to_string(status));
2064     if (increment) {
2065         printf(", increment=%"PRId64,
2066                ovsdb_idl_txn_get_increment_new_value(txn));
2067     }
2068     putchar('\n');
2069     ovsdb_idl_txn_destroy(txn);
2070 }
2071
2072 static void
2073 do_idl(struct ovs_cmdl_context *ctx)
2074 {
2075     struct jsonrpc *rpc;
2076     struct ovsdb_idl *idl;
2077     unsigned int seqno = 0;
2078     struct ovsdb_symbol_table *symtab;
2079     size_t n_uuids = 0;
2080     int step = 0;
2081     int error;
2082     int i;
2083     bool track;
2084
2085     idltest_init();
2086
2087     track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
2088
2089     idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
2090     if (ctx->argc > 2) {
2091         struct stream *stream;
2092
2093         error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
2094                                   DSCP_DEFAULT), &stream);
2095         if (error) {
2096             ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
2097         }
2098         rpc = jsonrpc_open(stream);
2099     } else {
2100         rpc = NULL;
2101     }
2102
2103     if (track) {
2104         ovsdb_idl_track_add_all(idl);
2105     }
2106
2107     setvbuf(stdout, NULL, _IONBF, 0);
2108
2109     symtab = ovsdb_symbol_table_create();
2110     for (i = 2; i < ctx->argc; i++) {
2111         char *arg = ctx->argv[i];
2112         struct jsonrpc_msg *request, *reply;
2113
2114         if (*arg == '+') {
2115             /* The previous transaction didn't change anything. */
2116             arg++;
2117         } else {
2118             /* Wait for update. */
2119             for (;;) {
2120                 ovsdb_idl_run(idl);
2121                 if (ovsdb_idl_get_seqno(idl) != seqno) {
2122                     break;
2123                 }
2124                 jsonrpc_run(rpc);
2125
2126                 ovsdb_idl_wait(idl);
2127                 jsonrpc_wait(rpc);
2128                 poll_block();
2129             }
2130
2131             /* Print update. */
2132             if (track) {
2133                 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2134                 ovsdb_idl_track_clear(idl);
2135             } else {
2136                 print_idl(idl, step++);
2137             }
2138         }
2139         seqno = ovsdb_idl_get_seqno(idl);
2140
2141         if (!strcmp(arg, "reconnect")) {
2142             printf("%03d: reconnect\n", step++);
2143             ovsdb_idl_force_reconnect(idl);
2144         } else if (arg[0] != '[') {
2145             idl_set(idl, arg, step++);
2146         } else {
2147             struct json *json = parse_json(arg);
2148             substitute_uuids(json, symtab);
2149             request = jsonrpc_create_request("transact", json, NULL);
2150             error = jsonrpc_transact_block(rpc, request, &reply);
2151             if (error || reply->error) {
2152                 ovs_fatal(error, "jsonrpc transaction failed");
2153             }
2154             printf("%03d: ", step++);
2155             if (reply->result) {
2156                 parse_uuids(reply->result, symtab, &n_uuids);
2157             }
2158             json_destroy(reply->id);
2159             reply->id = NULL;
2160             print_and_free_json(jsonrpc_msg_to_json(reply));
2161         }
2162     }
2163     ovsdb_symbol_table_destroy(symtab);
2164
2165     if (rpc) {
2166         jsonrpc_close(rpc);
2167     }
2168     for (;;) {
2169         ovsdb_idl_run(idl);
2170         if (ovsdb_idl_get_seqno(idl) != seqno) {
2171             break;
2172         }
2173         ovsdb_idl_wait(idl);
2174         poll_block();
2175     }
2176     print_idl(idl, step++);
2177     ovsdb_idl_track_clear(idl);
2178     ovsdb_idl_destroy(idl);
2179     printf("%03d: done\n", step);
2180 }
2181
2182 static struct ovs_cmdl_command all_commands[] = {
2183     { "log-io", NULL, 2, INT_MAX, do_log_io },
2184     { "default-atoms", NULL, 0, 0, do_default_atoms },
2185     { "default-data", NULL, 0, 0, do_default_data },
2186     { "diff-data", NULL, 3, INT_MAX, do_diff_data},
2187     { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
2188     { "parse-base-type", NULL, 1, 1, do_parse_base_type },
2189     { "parse-type", NULL, 1, 1, do_parse_type },
2190     { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
2191     { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
2192     { "parse-data", NULL, 2, INT_MAX, do_parse_data },
2193     { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
2194     { "sort-atoms", NULL, 2, 2, do_sort_atoms },
2195     { "parse-column", NULL, 2, 2, do_parse_column },
2196     { "parse-table", NULL, 2, 3, do_parse_table },
2197     { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
2198     { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
2199     { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
2200     { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
2201     { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
2202     { "execute-mutations", NULL, 3, 3, do_execute_mutations },
2203     { "query", NULL, 3, 3, do_query },
2204     { "query-distinct", NULL, 4, 4, do_query_distinct },
2205     { "transact", NULL, 1, INT_MAX, do_transact },
2206     { "parse-schema", NULL, 1, 1, do_parse_schema },
2207     { "execute", NULL, 2, INT_MAX, do_execute },
2208     { "trigger", NULL, 2, INT_MAX, do_trigger },
2209     { "idl", NULL, 1, INT_MAX, do_idl },
2210     { "help", NULL, 0, INT_MAX, do_help },
2211     { NULL, NULL, 0, 0, NULL },
2212 };
2213
2214 static struct ovs_cmdl_command *
2215 get_all_commands(void)
2216 {
2217     return all_commands;
2218 }