2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "command-line.h"
27 #include "dynamic-string.h"
30 #include "ovsdb-data.h"
31 #include "ovsdb-error.h"
32 #include "ovsdb-idl.h"
33 #include "ovsdb-types.h"
34 #include "ovsdb/column.h"
35 #include "ovsdb/condition.h"
36 #include "ovsdb/file.h"
37 #include "ovsdb/log.h"
38 #include "ovsdb/mutation.h"
39 #include "ovsdb/ovsdb.h"
40 #include "ovsdb/query.h"
41 #include "ovsdb/row.h"
42 #include "ovsdb/table.h"
43 #include "ovsdb/transaction.h"
44 #include "ovsdb/trigger.h"
45 #include "poll-loop.h"
48 #include "tests/idltest.h"
53 static struct command all_commands[];
55 static void usage(void) NO_RETURN;
56 static void parse_options(int argc, char *argv[]);
59 main(int argc, char *argv[])
61 set_program_name(argv[0]);
63 parse_options(argc, argv);
64 run_command(argc - optind, argv + optind, all_commands);
69 parse_options(int argc, char *argv[])
71 static struct option long_options[] = {
72 {"timeout", required_argument, 0, 't'},
73 {"verbose", optional_argument, 0, 'v'},
74 {"help", no_argument, 0, 'h'},
77 char *short_options = long_options_to_short_options(long_options);
80 unsigned long int timeout;
83 c = getopt_long(argc, argv, short_options, long_options, NULL);
90 timeout = strtoul(optarg, NULL, 10);
92 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
103 vlog_set_verbosity(optarg);
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"
124 " test ovsdb_atom_default()\n"
126 " test ovsdb_datum_default()\n"
127 " parse-atomic-type TYPE\n"
128 " parse TYPE as OVSDB atomic type, and re-serialize\n"
129 " parse-base-type TYPE\n"
130 " parse TYPE as OVSDB base type, and re-serialize\n"
132 " parse JSON as OVSDB type, and re-serialize\n"
133 " parse-atoms TYPE ATOM...\n"
134 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
135 " parse-atom-strings TYPE ATOM...\n"
136 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
137 " sort-atoms TYPE ATOM...\n"
138 " print JSON ATOMs in sorted order\n"
139 " parse-data TYPE DATUM...\n"
140 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
141 " parse-data-unique TYPE DATUM...\n"
142 " parse JSON DATUMs as data of given TYPE, eliminating\n"
143 " duplicate keys, and re-serialize\n"
144 " parse-data-strings TYPE DATUM...\n"
145 " parse string DATUMs as data of given TYPE, and re-serialize\n"
146 " parse-column NAME OBJECT\n"
147 " parse column NAME with info OBJECT, and re-serialize\n"
148 " parse-table NAME OBJECT\n"
149 " parse table NAME with info OBJECT\n"
150 " parse-row TABLE ROW..., and re-serialize\n"
151 " parse each ROW of defined TABLE\n"
152 " compare-row TABLE ROW...\n"
153 " mutually compare all of the ROWs, print those that are equal\n"
154 " parse-conditions TABLE CONDITION...\n"
155 " parse each CONDITION on TABLE, and re-serialize\n"
156 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
157 " test CONDITIONS on TABLE against each ROW, print results\n"
158 " parse-mutations TABLE MUTATION...\n"
159 " parse each MUTATION on TABLE, and re-serialize\n"
160 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
161 " execute MUTATIONS on TABLE on each ROW, print results\n"
162 " query TABLE [ROW,...] [CONDITION,...]\n"
163 " add each ROW to TABLE, then query and print the rows that\n"
164 " satisfy each CONDITION.\n"
165 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
166 " add each ROW to TABLE, then query and print the rows that\n"
167 " satisfy each CONDITION and have distinct COLUMNS.\n"
168 " parse-schema JSON\n"
169 " parse JSON as an OVSDB schema, and re-serialize\n"
170 " transact COMMAND\n"
171 " execute each specified transactional COMMAND:\n"
178 " execute SCHEMA TRANSACTION...\n"
179 " executes each TRANSACTION on an initially empty database\n"
180 " the specified SCHEMA\n"
181 " trigger SCHEMA TRANSACTION...\n"
182 " executes each TRANSACTION on an initially empty database\n"
183 " the specified SCHEMA. A TRANSACTION of the form\n"
184 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
185 " simulated time, for causing triggers to time out.\n"
186 " idl SERVER [TRANSACTION...]\n"
187 " connect to SERVER and dump the contents of the database\n"
188 " as seen initially by the IDL implementation and after\n"
189 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
190 " the database or this command will hang.)\n",
191 program_name, program_name);
193 printf("\nOther options:\n"
194 " -t, --timeout=SECS give up after SECS seconds\n"
195 " -h, --help display this help message\n");
199 /* Command helper functions. */
202 parse_json(const char *s)
204 struct json *json = json_from_string(s);
205 if (json->type == JSON_STRING) {
206 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
212 unbox_json(struct json *json)
214 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
215 struct json *inner = json->u.array.elems[0];
216 json->u.array.elems[0] = NULL;
225 print_and_free_json(struct json *json)
227 char *string = json_to_string(json, JSSF_SORT);
234 print_and_free_ovsdb_error(struct ovsdb_error *error)
236 char *string = ovsdb_error_to_string(error);
237 ovsdb_error_destroy(error);
243 check_ovsdb_error(struct ovsdb_error *error)
246 char *s = ovsdb_error_to_string(error);
247 ovsdb_error_destroy(error);
248 ovs_fatal(0, "%s", s);
253 die_if_error(char *error)
256 ovs_fatal(0, "%s", error);
260 /* Command implementations. */
263 do_log_io(int argc, char *argv[])
265 const char *name = argv[1];
266 char *mode_string = argv[2];
268 struct ovsdb_error *error;
269 enum ovsdb_log_open_mode mode;
270 struct ovsdb_log *log;
273 if (!strcmp(mode_string, "read-only")) {
274 mode = OVSDB_LOG_READ_ONLY;
275 } else if (!strcmp(mode_string, "read/write")) {
276 mode = OVSDB_LOG_READ_WRITE;
277 } else if (!strcmp(mode_string, "create")) {
278 mode = OVSDB_LOG_CREATE;
280 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
283 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
284 printf("%s: open successful\n", name);
286 for (i = 3; i < argc; i++) {
287 const char *command = argv[i];
288 if (!strcmp(command, "read")) {
291 error = ovsdb_log_read(log, &json);
293 printf("%s: read: ", name);
295 print_and_free_json(json);
297 printf("end of log\n");
301 } else if (!strncmp(command, "write:", 6)) {
302 struct json *json = parse_json(command + 6);
303 error = ovsdb_log_write(log, json);
305 } else if (!strcmp(command, "commit")) {
306 error = ovsdb_log_commit(log);
308 ovs_fatal(0, "unknown log-io command \"%s\"", command);
311 char *s = ovsdb_error_to_string(error);
312 printf("%s: %s failed: %s\n", name, command, s);
314 ovsdb_error_destroy(error);
316 printf("%s: %s successful\n", name, command);
320 ovsdb_log_close(log);
324 do_default_atoms(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
328 for (type = 0; type < OVSDB_N_TYPES; type++) {
329 union ovsdb_atom atom;
331 if (type == OVSDB_TYPE_VOID) {
335 printf("%s: ", ovsdb_atomic_type_to_string(type));
337 ovsdb_atom_init_default(&atom, type);
338 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
342 ovsdb_atom_destroy(&atom, type);
349 do_default_data(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
354 for (n_min = 0; n_min <= 1; n_min++) {
355 for (key = 0; key < OVSDB_N_TYPES; key++) {
356 if (key == OVSDB_TYPE_VOID) {
359 for (value = 0; value < OVSDB_N_TYPES; value++) {
360 struct ovsdb_datum datum;
361 struct ovsdb_type type;
363 ovsdb_base_type_init(&type.key, key);
364 ovsdb_base_type_init(&type.value, value);
367 assert(ovsdb_type_is_valid(&type));
369 printf("key %s, value %s, n_min %u: ",
370 ovsdb_atomic_type_to_string(key),
371 ovsdb_atomic_type_to_string(value), n_min);
373 ovsdb_datum_init_default(&datum, &type);
374 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
379 ovsdb_datum_destroy(&datum, &type);
380 ovsdb_type_destroy(&type);
389 do_parse_atomic_type(int argc OVS_UNUSED, char *argv[])
391 enum ovsdb_atomic_type type;
394 json = unbox_json(parse_json(argv[1]));
395 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
397 print_and_free_json(ovsdb_atomic_type_to_json(type));
401 do_parse_base_type(int argc OVS_UNUSED, char *argv[])
403 struct ovsdb_base_type base;
406 json = unbox_json(parse_json(argv[1]));
407 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
409 print_and_free_json(ovsdb_base_type_to_json(&base));
410 ovsdb_base_type_destroy(&base);
414 do_parse_type(int argc OVS_UNUSED, char *argv[])
416 struct ovsdb_type type;
419 json = unbox_json(parse_json(argv[1]));
420 check_ovsdb_error(ovsdb_type_from_json(&type, json));
422 print_and_free_json(ovsdb_type_to_json(&type));
423 ovsdb_type_destroy(&type);
427 do_parse_atoms(int argc, char *argv[])
429 struct ovsdb_base_type base;
433 json = unbox_json(parse_json(argv[1]));
434 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
437 for (i = 2; i < argc; i++) {
438 struct ovsdb_error *error;
439 union ovsdb_atom atom;
441 json = unbox_json(parse_json(argv[i]));
442 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
446 print_and_free_ovsdb_error(error);
448 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
449 ovsdb_atom_destroy(&atom, base.type);
452 ovsdb_base_type_destroy(&base);
456 do_parse_atom_strings(int argc, char *argv[])
458 struct ovsdb_base_type base;
462 json = unbox_json(parse_json(argv[1]));
463 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
466 for (i = 2; i < argc; i++) {
467 union ovsdb_atom atom;
470 die_if_error(ovsdb_atom_from_string(&atom, &base, argv[i], NULL));
473 ovsdb_atom_to_string(&atom, base.type, &out);
477 ovsdb_atom_destroy(&atom, base.type);
479 ovsdb_base_type_destroy(&base);
483 do_parse_data__(int argc, char *argv[],
485 (*parse)(struct ovsdb_datum *datum,
486 const struct ovsdb_type *type,
487 const struct json *json,
488 struct ovsdb_symbol_table *symtab))
490 struct ovsdb_type type;
494 json = unbox_json(parse_json(argv[1]));
495 check_ovsdb_error(ovsdb_type_from_json(&type, json));
498 for (i = 2; i < argc; i++) {
499 struct ovsdb_datum datum;
501 json = unbox_json(parse_json(argv[i]));
502 check_ovsdb_error(parse(&datum, &type, json, NULL));
505 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
507 ovsdb_datum_destroy(&datum, &type);
509 ovsdb_type_destroy(&type);
513 do_parse_data(int argc, char *argv[])
515 do_parse_data__(argc, argv, ovsdb_datum_from_json);
519 do_parse_data_unique(int argc, char *argv[])
521 do_parse_data__(argc, argv, ovsdb_datum_from_json_unique);
525 do_parse_data_strings(int argc, char *argv[])
527 struct ovsdb_type type;
531 json = unbox_json(parse_json(argv[1]));
532 check_ovsdb_error(ovsdb_type_from_json(&type, json));
535 for (i = 2; i < argc; i++) {
536 struct ovsdb_datum datum;
539 die_if_error(ovsdb_datum_from_string(&datum, &type, argv[i], NULL));
542 ovsdb_datum_to_string(&datum, &type, &out);
546 ovsdb_datum_destroy(&datum, &type);
548 ovsdb_type_destroy(&type);
551 static enum ovsdb_atomic_type compare_atoms_atomic_type;
554 compare_atoms(const void *a_, const void *b_)
556 const union ovsdb_atom *a = a_;
557 const union ovsdb_atom *b = b_;
559 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
563 do_sort_atoms(int argc OVS_UNUSED, char *argv[])
565 struct ovsdb_base_type base;
566 union ovsdb_atom *atoms;
567 struct json *json, **json_atoms;
571 json = unbox_json(parse_json(argv[1]));
572 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
575 json = unbox_json(parse_json(argv[2]));
576 if (json->type != JSON_ARRAY) {
577 ovs_fatal(0, "second argument must be array");
580 /* Convert JSON atoms to internal representation. */
581 n_atoms = json->u.array.n;
582 atoms = xmalloc(n_atoms * sizeof *atoms);
583 for (i = 0; i < n_atoms; i++) {
584 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
585 json->u.array.elems[i], NULL));
590 compare_atoms_atomic_type = base.type;
591 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
593 /* Convert internal representation back to JSON. */
594 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
595 for (i = 0; i < n_atoms; i++) {
596 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
597 ovsdb_atom_destroy(&atoms[i], base.type);
599 print_and_free_json(json_array_create(json_atoms, n_atoms));
601 ovsdb_base_type_destroy(&base);
605 do_parse_column(int argc OVS_UNUSED, char *argv[])
607 struct ovsdb_column *column;
610 json = parse_json(argv[2]);
611 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
613 print_and_free_json(ovsdb_column_to_json(column));
614 ovsdb_column_destroy(column);
618 do_parse_table(int argc OVS_UNUSED, char *argv[])
620 struct ovsdb_table_schema *ts;
623 json = parse_json(argv[2]);
624 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
626 print_and_free_json(ovsdb_table_schema_to_json(ts));
627 ovsdb_table_schema_destroy(ts);
631 do_parse_rows(int argc, char *argv[])
633 struct ovsdb_column_set all_columns;
634 struct ovsdb_table_schema *ts;
635 struct ovsdb_table *table;
639 json = unbox_json(parse_json(argv[1]));
640 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
643 table = ovsdb_table_create(ts);
644 ovsdb_column_set_init(&all_columns);
645 ovsdb_column_set_add_all(&all_columns, table);
647 for (i = 2; i < argc; i++) {
648 struct ovsdb_column_set columns;
649 struct ovsdb_row *row;
651 ovsdb_column_set_init(&columns);
652 row = ovsdb_row_create(table);
654 json = unbox_json(parse_json(argv[i]));
655 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
658 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
660 if (columns.n_columns) {
666 for (j = 0; j < columns.n_columns; j++) {
667 svec_add(&names, columns.columns[j]->name);
670 s = svec_join(&names, ", ", "");
673 svec_destroy(&names);
678 ovsdb_column_set_destroy(&columns);
679 ovsdb_row_destroy(row);
682 ovsdb_column_set_destroy(&all_columns);
683 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
687 do_compare_rows(int argc, char *argv[])
689 struct ovsdb_column_set all_columns;
690 struct ovsdb_table_schema *ts;
691 struct ovsdb_table *table;
692 struct ovsdb_row **rows;
698 json = unbox_json(parse_json(argv[1]));
699 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
702 table = ovsdb_table_create(ts);
703 ovsdb_column_set_init(&all_columns);
704 ovsdb_column_set_add_all(&all_columns, table);
707 rows = xmalloc(sizeof *rows * n_rows);
708 names = xmalloc(sizeof *names * n_rows);
709 for (i = 0; i < n_rows; i++) {
710 rows[i] = ovsdb_row_create(table);
712 json = parse_json(argv[i + 2]);
713 if (json->type != JSON_ARRAY || json->u.array.n != 2
714 || json->u.array.elems[0]->type != JSON_STRING) {
715 ovs_fatal(0, "\"%s\" does not have expected form "
716 "[\"name\", {data}]", argv[i]);
718 names[i] = xstrdup(json->u.array.elems[0]->u.string);
719 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
723 for (i = 0; i < n_rows; i++) {
724 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
725 for (j = i + 1; j < n_rows; j++) {
726 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
727 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
728 printf("%s == %s\n", names[i], names[j]);
729 if (i_hash != j_hash) {
730 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
733 } else if (i_hash == j_hash) {
734 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
738 for (i = 0; i < n_rows; i++) {
739 ovsdb_row_destroy(rows[i]);
745 ovsdb_column_set_destroy(&all_columns);
746 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
750 do_parse_conditions(int argc, char *argv[])
752 struct ovsdb_table_schema *ts;
757 json = unbox_json(parse_json(argv[1]));
758 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
761 for (i = 2; i < argc; i++) {
762 struct ovsdb_condition cnd;
763 struct ovsdb_error *error;
765 json = parse_json(argv[i]);
766 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
768 print_and_free_json(ovsdb_condition_to_json(&cnd));
770 char *s = ovsdb_error_to_string(error);
771 ovs_error(0, "%s", s);
773 ovsdb_error_destroy(error);
778 ovsdb_condition_destroy(&cnd);
780 ovsdb_table_schema_destroy(ts);
786 do_evaluate_conditions(int argc OVS_UNUSED, char *argv[])
788 struct ovsdb_table_schema *ts;
789 struct ovsdb_table *table;
790 struct ovsdb_condition *conditions;
792 struct ovsdb_row **rows;
797 /* Parse table schema, create table. */
798 json = unbox_json(parse_json(argv[1]));
799 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
802 table = ovsdb_table_create(ts);
804 /* Parse conditions. */
805 json = parse_json(argv[2]);
806 if (json->type != JSON_ARRAY) {
807 ovs_fatal(0, "CONDITION argument is not JSON array");
809 n_conditions = json->u.array.n;
810 conditions = xmalloc(n_conditions * sizeof *conditions);
811 for (i = 0; i < n_conditions; i++) {
812 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
813 NULL, &conditions[i]));
818 json = parse_json(argv[3]);
819 if (json->type != JSON_ARRAY) {
820 ovs_fatal(0, "ROW argument is not JSON array");
822 n_rows = json->u.array.n;
823 rows = xmalloc(n_rows * sizeof *rows);
824 for (i = 0; i < n_rows; i++) {
825 rows[i] = ovsdb_row_create(table);
826 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
831 for (i = 0; i < n_conditions; i++) {
832 printf("condition %2zu:", i);
833 for (j = 0; j < n_rows; j++) {
834 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
838 putchar(result ? 'T' : '-');
843 for (i = 0; i < n_conditions; i++) {
844 ovsdb_condition_destroy(&conditions[i]);
847 for (i = 0; i < n_rows; i++) {
848 ovsdb_row_destroy(rows[i]);
851 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
855 do_parse_mutations(int argc, char *argv[])
857 struct ovsdb_table_schema *ts;
862 json = unbox_json(parse_json(argv[1]));
863 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
866 for (i = 2; i < argc; i++) {
867 struct ovsdb_mutation_set set;
868 struct ovsdb_error *error;
870 json = parse_json(argv[i]);
871 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
873 print_and_free_json(ovsdb_mutation_set_to_json(&set));
875 char *s = ovsdb_error_to_string(error);
876 ovs_error(0, "%s", s);
878 ovsdb_error_destroy(error);
883 ovsdb_mutation_set_destroy(&set);
885 ovsdb_table_schema_destroy(ts);
891 do_execute_mutations(int argc OVS_UNUSED, char *argv[])
893 struct ovsdb_table_schema *ts;
894 struct ovsdb_table *table;
895 struct ovsdb_mutation_set *sets;
897 struct ovsdb_row **rows;
902 /* Parse table schema, create table. */
903 json = unbox_json(parse_json(argv[1]));
904 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
907 table = ovsdb_table_create(ts);
909 /* Parse mutations. */
910 json = parse_json(argv[2]);
911 if (json->type != JSON_ARRAY) {
912 ovs_fatal(0, "MUTATION argument is not JSON array");
914 n_sets = json->u.array.n;
915 sets = xmalloc(n_sets * sizeof *sets);
916 for (i = 0; i < n_sets; i++) {
917 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
918 json->u.array.elems[i],
924 json = parse_json(argv[3]);
925 if (json->type != JSON_ARRAY) {
926 ovs_fatal(0, "ROW argument is not JSON array");
928 n_rows = json->u.array.n;
929 rows = xmalloc(n_rows * sizeof *rows);
930 for (i = 0; i < n_rows; i++) {
931 rows[i] = ovsdb_row_create(table);
932 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
937 for (i = 0; i < n_sets; i++) {
938 printf("mutation %2zu:\n", i);
939 for (j = 0; j < n_rows; j++) {
940 struct ovsdb_error *error;
941 struct ovsdb_row *row;
943 row = ovsdb_row_clone(rows[j]);
944 error = ovsdb_mutation_set_execute(row, &sets[i]);
946 printf("row %zu: ", j);
948 print_and_free_ovsdb_error(error);
950 struct ovsdb_column_set columns;
951 struct shash_node *node;
953 ovsdb_column_set_init(&columns);
954 SHASH_FOR_EACH (node, &ts->columns) {
955 struct ovsdb_column *c = node->data;
956 if (!ovsdb_datum_equals(&row->fields[c->index],
957 &rows[j]->fields[c->index],
959 ovsdb_column_set_add(&columns, c);
962 if (columns.n_columns) {
963 print_and_free_json(ovsdb_row_to_json(row, &columns));
965 printf("no change\n");
967 ovsdb_column_set_destroy(&columns);
969 ovsdb_row_destroy(row);
974 for (i = 0; i < n_sets; i++) {
975 ovsdb_mutation_set_destroy(&sets[i]);
978 for (i = 0; i < n_rows; i++) {
979 ovsdb_row_destroy(rows[i]);
982 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
985 struct do_query_cbdata {
986 struct uuid *row_uuids;
992 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
994 struct do_query_cbdata *cbdata = cbdata_;
997 for (i = 0; i < cbdata->n_rows; i++) {
998 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1007 do_query(int argc OVS_UNUSED, char *argv[])
1009 struct do_query_cbdata cbdata;
1010 struct ovsdb_table_schema *ts;
1011 struct ovsdb_table *table;
1016 /* Parse table schema, create table. */
1017 json = unbox_json(parse_json(argv[1]));
1018 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1021 table = ovsdb_table_create(ts);
1023 /* Parse rows, add to table. */
1024 json = parse_json(argv[2]);
1025 if (json->type != JSON_ARRAY) {
1026 ovs_fatal(0, "ROW argument is not JSON array");
1028 cbdata.n_rows = json->u.array.n;
1029 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1030 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1031 for (i = 0; i < cbdata.n_rows; i++) {
1032 struct ovsdb_row *row = ovsdb_row_create(table);
1033 uuid_generate(ovsdb_row_get_uuid_rw(row));
1034 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1036 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1037 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1038 UUID_ARGS(ovsdb_row_get_uuid(row)));
1040 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1041 ovsdb_table_put_row(table, row);
1045 /* Parse conditions and execute queries. */
1046 json = parse_json(argv[3]);
1047 if (json->type != JSON_ARRAY) {
1048 ovs_fatal(0, "CONDITION argument is not JSON array");
1050 for (i = 0; i < json->u.array.n; i++) {
1051 struct ovsdb_condition cnd;
1054 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1057 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1058 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1060 printf("query %2zu:", i);
1061 for (j = 0; j < cbdata.n_rows; j++) {
1065 if (cbdata.counts[j]) {
1066 printf("%d", cbdata.counts[j]);
1067 if (cbdata.counts[j] > 1) {
1077 ovsdb_condition_destroy(&cnd);
1081 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1086 struct do_query_distinct_class {
1087 struct ovsdb_row *example;
1091 struct do_query_distinct_row {
1093 struct do_query_distinct_class *class;
1097 do_query_distinct(int argc OVS_UNUSED, char *argv[])
1099 struct ovsdb_column_set columns;
1100 struct ovsdb_table_schema *ts;
1101 struct ovsdb_table *table;
1102 struct do_query_distinct_row *rows;
1104 struct do_query_distinct_class *classes;
1110 /* Parse table schema, create table. */
1111 json = unbox_json(parse_json(argv[1]));
1112 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1115 table = ovsdb_table_create(ts);
1117 /* Parse column set. */
1118 json = parse_json(argv[4]);
1119 check_ovsdb_error(ovsdb_column_set_from_json(json, table, &columns));
1122 /* Parse rows, add to table. */
1123 json = parse_json(argv[2]);
1124 if (json->type != JSON_ARRAY) {
1125 ovs_fatal(0, "ROW argument is not JSON array");
1127 n_rows = json->u.array.n;
1128 rows = xmalloc(n_rows * sizeof *rows);
1129 classes = xmalloc(n_rows * sizeof *classes);
1131 for (i = 0; i < n_rows; i++) {
1132 struct ovsdb_row *row;
1136 row = ovsdb_row_create(table);
1137 uuid_generate(ovsdb_row_get_uuid_rw(row));
1138 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1141 /* Initialize row and find equivalence class. */
1142 rows[i].uuid = *ovsdb_row_get_uuid(row);
1143 rows[i].class = NULL;
1144 for (j = 0; j < n_classes; j++) {
1145 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1146 rows[i].class = &classes[j];
1150 if (!rows[i].class) {
1151 rows[i].class = &classes[n_classes];
1152 classes[n_classes].example = ovsdb_row_clone(row);
1156 /* Add row to table. */
1157 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1158 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1159 UUID_ARGS(ovsdb_row_get_uuid(row)));
1161 ovsdb_table_put_row(table, row);
1166 /* Parse conditions and execute queries. */
1167 json = parse_json(argv[3]);
1168 if (json->type != JSON_ARRAY) {
1169 ovs_fatal(0, "CONDITION argument is not JSON array");
1171 for (i = 0; i < json->u.array.n; i++) {
1172 struct ovsdb_row_set results;
1173 struct ovsdb_condition cnd;
1175 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1178 for (j = 0; j < n_classes; j++) {
1179 classes[j].count = 0;
1181 ovsdb_row_set_init(&results);
1182 ovsdb_query_distinct(table, &cnd, &columns, &results);
1183 for (j = 0; j < results.n_rows; j++) {
1184 for (k = 0; k < n_rows; k++) {
1185 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1187 rows[k].class->count++;
1191 ovsdb_row_set_destroy(&results);
1193 printf("query %2zu:", i);
1194 for (j = 0; j < n_rows; j++) {
1195 int count = rows[j].class->count;
1202 printf("%d", count);
1204 } else if (count == 1) {
1205 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1212 ovsdb_condition_destroy(&cnd);
1216 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1222 do_parse_schema(int argc OVS_UNUSED, char *argv[])
1224 struct ovsdb_schema *schema;
1227 json = parse_json(argv[1]);
1228 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1230 print_and_free_json(ovsdb_schema_to_json(schema));
1231 ovsdb_schema_destroy(schema);
1235 do_execute(int argc OVS_UNUSED, char *argv[])
1237 struct ovsdb_schema *schema;
1242 /* Create database. */
1243 json = parse_json(argv[1]);
1244 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1246 db = ovsdb_create(schema);
1248 for (i = 2; i < argc; i++) {
1249 struct json *params, *result;
1252 params = parse_json(argv[i]);
1253 result = ovsdb_execute(db, params, 0, NULL);
1254 s = json_to_string(result, JSSF_SORT);
1257 json_destroy(params);
1258 json_destroy(result);
1264 struct test_trigger {
1265 struct ovsdb_trigger trigger;
1270 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1272 struct json *result;
1275 result = ovsdb_trigger_steal_result(&t->trigger);
1276 s = json_to_string(result, JSSF_SORT);
1277 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1279 json_destroy(result);
1280 ovsdb_trigger_destroy(&t->trigger);
1285 do_trigger(int argc OVS_UNUSED, char *argv[])
1287 struct ovsdb_schema *schema;
1288 struct list completions;
1295 /* Create database. */
1296 json = parse_json(argv[1]);
1297 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1299 db = ovsdb_create(schema);
1301 list_init(&completions);
1304 for (i = 2; i < argc; i++) {
1305 struct json *params = parse_json(argv[i]);
1306 if (params->type == JSON_ARRAY
1307 && json_array(params)->n == 2
1308 && json_array(params)->elems[0]->type == JSON_STRING
1309 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1310 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1311 now += json_integer(json_array(params)->elems[1]);
1312 json_destroy(params);
1314 struct test_trigger *t = xmalloc(sizeof *t);
1315 ovsdb_trigger_init(db, &t->trigger, params, &completions, now);
1316 t->number = number++;
1317 if (ovsdb_trigger_is_complete(&t->trigger)) {
1318 do_trigger_dump(t, now, "immediate");
1320 printf("t=%lld: new trigger %d\n", now, t->number);
1324 ovsdb_trigger_run(db, now);
1325 while (!list_is_empty(&completions)) {
1326 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions),
1327 struct test_trigger, trigger.node),
1331 ovsdb_trigger_wait(db, now);
1332 poll_immediate_wake();
1340 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1345 /* "transact" command. */
1347 static struct ovsdb *do_transact_db;
1348 static struct ovsdb_txn *do_transact_txn;
1349 static struct ovsdb_table *do_transact_table;
1352 do_transact_commit(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1354 ovsdb_txn_commit(do_transact_txn, false);
1355 do_transact_txn = NULL;
1359 do_transact_abort(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1361 ovsdb_txn_abort(do_transact_txn);
1362 do_transact_txn = NULL;
1366 uuid_from_integer(int integer, struct uuid *uuid)
1369 uuid->parts[3] = integer;
1372 static const struct ovsdb_row *
1373 do_transact_find_row(const char *uuid_string)
1375 const struct ovsdb_row *row;
1378 uuid_from_integer(atoi(uuid_string), &uuid);
1379 row = ovsdb_table_get_row(do_transact_table, &uuid);
1381 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1388 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1391 if (integer != -1) {
1392 const struct ovsdb_column *column;
1394 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1396 row->fields[column->index].keys[0].integer = integer;
1401 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1403 const struct ovsdb_column *column;
1405 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1407 return row->fields[column->index].keys[0].integer;
1411 do_transact_set_i_j(struct ovsdb_row *row,
1412 const char *i_string, const char *j_string)
1414 do_transact_set_integer(row, "i", atoi(i_string));
1415 do_transact_set_integer(row, "j", atoi(j_string));
1419 do_transact_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1421 struct ovsdb_row *row;
1424 row = ovsdb_row_create(do_transact_table);
1427 uuid = ovsdb_row_get_uuid_rw(row);
1428 uuid_from_integer(atoi(argv[1]), uuid);
1429 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1430 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1434 do_transact_set_i_j(row, argv[2], argv[3]);
1437 ovsdb_txn_row_insert(do_transact_txn, row);
1441 do_transact_delete(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1443 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1444 ovsdb_txn_row_delete(do_transact_txn, row);
1448 do_transact_modify(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1450 const struct ovsdb_row *row_ro;
1451 struct ovsdb_row *row_rw;
1453 row_ro = do_transact_find_row(argv[1]);
1454 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1455 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1459 compare_rows_by_uuid(const void *a_, const void *b_)
1461 struct ovsdb_row *const *ap = a_;
1462 struct ovsdb_row *const *bp = b_;
1464 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1468 do_transact_print(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1470 const struct ovsdb_row **rows;
1471 const struct ovsdb_row *row;
1475 n_rows = hmap_count(&do_transact_table->rows);
1476 rows = xmalloc(n_rows * sizeof *rows);
1478 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1479 &do_transact_table->rows) {
1482 assert(i == n_rows);
1484 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1486 for (i = 0; i < n_rows; i++) {
1487 printf("\n%"PRId32": i=%d, j=%d",
1488 ovsdb_row_get_uuid(rows[i])->parts[3],
1489 do_transact_get_integer(rows[i], "i"),
1490 do_transact_get_integer(rows[i], "j"));
1497 do_transact(int argc, char *argv[])
1499 static const struct command do_transact_commands[] = {
1500 { "commit", 0, 0, do_transact_commit },
1501 { "abort", 0, 0, do_transact_abort },
1502 { "insert", 2, 3, do_transact_insert },
1503 { "delete", 1, 1, do_transact_delete },
1504 { "modify", 2, 3, do_transact_modify },
1505 { "print", 0, 0, do_transact_print },
1506 { NULL, 0, 0, NULL },
1509 struct ovsdb_schema *schema;
1514 json = parse_json("{\"name\": \"testdb\", "
1518 " {\"i\": {\"type\": \"integer\"}, "
1519 " \"j\": {\"type\": \"integer\"}}}}}");
1520 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1522 do_transact_db = ovsdb_create(schema);
1523 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1524 assert(do_transact_table != NULL);
1526 for (i = 1; i < argc; i++) {
1527 struct json *command;
1532 command = parse_json(argv[i]);
1533 if (command->type != JSON_ARRAY) {
1534 ovs_fatal(0, "transaction %d must be JSON array "
1535 "with at least 1 element", i);
1538 n_args = command->u.array.n;
1539 args = xmalloc((n_args + 1) * sizeof *args);
1540 for (j = 0; j < n_args; j++) {
1541 struct json *s = command->u.array.elems[j];
1542 if (s->type != JSON_STRING) {
1543 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1546 args[j] = xstrdup(json_string(s));
1548 args[n_args] = NULL;
1550 if (!do_transact_txn) {
1551 do_transact_txn = ovsdb_txn_create(do_transact_db);
1554 for (j = 0; j < n_args; j++) {
1558 fputs(args[j], stdout);
1561 run_command(n_args, args, do_transact_commands);
1564 for (j = 0; j < n_args; j++) {
1568 json_destroy(command);
1570 ovsdb_txn_abort(do_transact_txn);
1571 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1575 compare_link1(const void *a_, const void *b_)
1577 const struct idltest_link1 *const *ap = a_;
1578 const struct idltest_link1 *const *bp = b_;
1579 const struct idltest_link1 *a = *ap;
1580 const struct idltest_link1 *b = *bp;
1582 return a->i < b->i ? -1 : a->i > b->i;
1586 print_idl(struct ovsdb_idl *idl, int step)
1588 const struct idltest_simple *s;
1589 const struct idltest_link1 *l1;
1590 const struct idltest_link2 *l2;
1593 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1596 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1597 step, s->i, s->r, s->b ? "true" : "false",
1598 s->s, UUID_ARGS(&s->u));
1599 for (i = 0; i < s->n_ia; i++) {
1600 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1603 for (i = 0; i < s->n_ra; i++) {
1604 printf("%s%g", i ? " " : "", s->ra[i]);
1607 for (i = 0; i < s->n_ba; i++) {
1608 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1611 for (i = 0; i < s->n_sa; i++) {
1612 printf("%s%s", i ? " " : "", s->sa[i]);
1615 for (i = 0; i < s->n_ua; i++) {
1616 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1618 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1621 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1622 struct idltest_link1 **links;
1625 printf("%03d: i=%"PRId64" k=", step, l1->i);
1627 printf("%"PRId64, l1->k->i);
1630 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1631 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1632 for (i = 0; i < l1->n_ka; i++) {
1633 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1638 printf("%"PRId64, l1->l2->i);
1640 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1643 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1644 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1646 printf("%"PRId64, l2->l1->i);
1648 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1652 printf("%03d: empty\n", step);
1657 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1662 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1663 char *name = xasprintf("#%zu#", *n);
1664 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1665 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1668 } else if (json->type == JSON_ARRAY) {
1671 for (i = 0; i < json->u.array.n; i++) {
1672 parse_uuids(json->u.array.elems[i], symtab, n);
1674 } else if (json->type == JSON_OBJECT) {
1675 const struct shash_node *node;
1677 SHASH_FOR_EACH (node, json_object(json)) {
1678 parse_uuids(node->data, symtab, n);
1684 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1686 if (json->type == JSON_STRING) {
1687 const struct ovsdb_symbol *symbol;
1689 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1691 free(json->u.string);
1692 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1694 } else if (json->type == JSON_ARRAY) {
1697 for (i = 0; i < json->u.array.n; i++) {
1698 substitute_uuids(json->u.array.elems[i], symtab);
1700 } else if (json->type == JSON_OBJECT) {
1701 const struct shash_node *node;
1703 SHASH_FOR_EACH (node, json_object(json)) {
1704 substitute_uuids(node->data, symtab);
1709 static const struct idltest_simple *
1710 idltest_find_simple(struct ovsdb_idl *idl, int i)
1712 const struct idltest_simple *s;
1714 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1723 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1725 char *cmd, *save_ptr1 = NULL;
1726 struct ovsdb_idl_txn *txn;
1727 enum ovsdb_idl_txn_status status;
1728 bool increment = false;
1730 txn = ovsdb_idl_txn_create(idl);
1731 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1732 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1733 char *save_ptr2 = NULL;
1734 char *name, *arg1, *arg2, *arg3;
1736 name = strtok_r(cmd, " ", &save_ptr2);
1737 arg1 = strtok_r(NULL, " ", &save_ptr2);
1738 arg2 = strtok_r(NULL, " ", &save_ptr2);
1739 arg3 = strtok_r(NULL, " ", &save_ptr2);
1741 if (!strcmp(name, "set")) {
1742 const struct idltest_simple *s;
1745 ovs_fatal(0, "\"set\" command requires 3 arguments");
1748 s = idltest_find_simple(idl, atoi(arg1));
1750 ovs_fatal(0, "\"set\" command asks for nonexistent "
1751 "i=%d", atoi(arg1));
1754 if (!strcmp(arg2, "b")) {
1755 idltest_simple_set_b(s, atoi(arg3));
1756 } else if (!strcmp(arg2, "s")) {
1757 idltest_simple_set_s(s, arg3);
1758 } else if (!strcmp(arg2, "u")) {
1760 uuid_from_string(&uuid, arg3);
1761 idltest_simple_set_u(s, uuid);
1762 } else if (!strcmp(arg2, "r")) {
1763 idltest_simple_set_r(s, atof(arg3));
1765 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1768 } else if (!strcmp(name, "insert")) {
1769 struct idltest_simple *s;
1771 if (!arg1 || arg2) {
1772 ovs_fatal(0, "\"set\" command requires 1 argument");
1775 s = idltest_simple_insert(txn);
1776 idltest_simple_set_i(s, atoi(arg1));
1777 } else if (!strcmp(name, "delete")) {
1778 const struct idltest_simple *s;
1780 if (!arg1 || arg2) {
1781 ovs_fatal(0, "\"set\" command requires 1 argument");
1784 s = idltest_find_simple(idl, atoi(arg1));
1786 ovs_fatal(0, "\"set\" command asks for nonexistent "
1787 "i=%d", atoi(arg1));
1789 idltest_simple_delete(s);
1790 } else if (!strcmp(name, "increment")) {
1791 if (!arg2 || arg3) {
1792 ovs_fatal(0, "\"set\" command requires 2 arguments");
1794 ovsdb_idl_txn_increment(txn, arg1, arg2, NULL);
1797 ovs_fatal(0, "unknown command %s", name);
1801 status = ovsdb_idl_txn_commit_block(txn);
1802 printf("%03d: commit, status=%s",
1803 step, ovsdb_idl_txn_status_to_string(status));
1805 printf(", increment=%"PRId64,
1806 ovsdb_idl_txn_get_increment_new_value(txn));
1809 ovsdb_idl_txn_destroy(txn);
1813 do_idl(int argc, char *argv[])
1815 struct jsonrpc *rpc;
1816 struct ovsdb_idl *idl;
1817 unsigned int seqno = 0;
1818 struct ovsdb_symbol_table *symtab;
1826 idl = ovsdb_idl_create(argv[1], &idltest_idl_class);
1828 struct stream *stream;
1830 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream),
1833 ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
1835 rpc = jsonrpc_open(stream);
1840 setvbuf(stdout, NULL, _IOLBF, 0);
1842 symtab = ovsdb_symbol_table_create();
1843 for (i = 2; i < argc; i++) {
1844 char *arg = argv[i];
1845 struct jsonrpc_msg *request, *reply;
1849 /* The previous transaction didn't change anything. */
1852 /* Wait for update. */
1853 while (ovsdb_idl_get_seqno(idl) == seqno && !ovsdb_idl_run(idl)) {
1856 ovsdb_idl_wait(idl);
1862 print_idl(idl, step++);
1864 seqno = ovsdb_idl_get_seqno(idl);
1866 if (!strcmp(arg, "reconnect")) {
1867 printf("%03d: reconnect\n", step++);
1868 ovsdb_idl_force_reconnect(idl);
1869 } else if (arg[0] != '[') {
1870 idl_set(idl, arg, step++);
1872 struct json *json = parse_json(arg);
1873 substitute_uuids(json, symtab);
1874 request = jsonrpc_create_request("transact", json, NULL);
1875 error = jsonrpc_transact_block(rpc, request, &reply);
1877 ovs_fatal(error, "jsonrpc transaction failed");
1879 printf("%03d: ", step++);
1880 if (reply->result) {
1881 parse_uuids(reply->result, symtab, &n_uuids);
1883 json_destroy(reply->id);
1885 print_and_free_json(jsonrpc_msg_to_json(reply));
1888 ovsdb_symbol_table_destroy(symtab);
1893 while (ovsdb_idl_get_seqno(idl) == seqno && !ovsdb_idl_run(idl)) {
1894 ovsdb_idl_wait(idl);
1897 print_idl(idl, step++);
1898 ovsdb_idl_destroy(idl);
1899 printf("%03d: done\n", step);
1902 static struct command all_commands[] = {
1903 { "log-io", 2, INT_MAX, do_log_io },
1904 { "default-atoms", 0, 0, do_default_atoms },
1905 { "default-data", 0, 0, do_default_data },
1906 { "parse-atomic-type", 1, 1, do_parse_atomic_type },
1907 { "parse-base-type", 1, 1, do_parse_base_type },
1908 { "parse-type", 1, 1, do_parse_type },
1909 { "parse-atoms", 2, INT_MAX, do_parse_atoms },
1910 { "parse-atom-strings", 2, INT_MAX, do_parse_atom_strings },
1911 { "parse-data", 2, INT_MAX, do_parse_data },
1912 { "parse-data-unique", 2, INT_MAX, do_parse_data_unique },
1913 { "parse-data-strings", 2, INT_MAX, do_parse_data_strings },
1914 { "sort-atoms", 2, 2, do_sort_atoms },
1915 { "parse-column", 2, 2, do_parse_column },
1916 { "parse-table", 2, 2, do_parse_table },
1917 { "parse-rows", 2, INT_MAX, do_parse_rows },
1918 { "compare-rows", 2, INT_MAX, do_compare_rows },
1919 { "parse-conditions", 2, INT_MAX, do_parse_conditions },
1920 { "evaluate-conditions", 3, 3, do_evaluate_conditions },
1921 { "parse-mutations", 2, INT_MAX, do_parse_mutations },
1922 { "execute-mutations", 3, 3, do_execute_mutations },
1923 { "query", 3, 3, do_query },
1924 { "query-distinct", 4, 4, do_query_distinct },
1925 { "transact", 1, INT_MAX, do_transact },
1926 { "parse-schema", 1, 1, do_parse_schema },
1927 { "execute", 2, INT_MAX, do_execute },
1928 { "trigger", 2, INT_MAX, do_trigger },
1929 { "idl", 1, INT_MAX, do_idl },
1930 { "help", 0, INT_MAX, do_help },
1931 { NULL, 0, 0, NULL },