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