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