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