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