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]);
64 parse_options(argc, argv);
65 run_command(argc - optind, argv + optind, all_commands);
70 parse_options(int argc, char *argv[])
72 static struct option long_options[] = {
73 {"timeout", required_argument, 0, 't'},
74 {"verbose", optional_argument, 0, 'v'},
75 {"help", no_argument, 0, 'h'},
78 char *short_options = long_options_to_short_options(long_options);
81 unsigned long int timeout;
84 c = getopt_long(argc, argv, short_options, long_options, NULL);
91 timeout = strtoul(optarg, NULL, 10);
93 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
104 vlog_set_verbosity(optarg);
120 printf("%s: Open vSwitch database test utility\n"
121 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
122 " log-io FILE FLAGS COMMAND...\n"
123 " open FILE with FLAGS, run COMMANDs\n"
125 " test ovsdb_atom_default()\n"
127 " test ovsdb_datum_default()\n"
128 " parse-atomic-type TYPE\n"
129 " parse TYPE as OVSDB atomic type, and re-serialize\n"
130 " parse-base-type TYPE\n"
131 " parse TYPE as OVSDB base type, and re-serialize\n"
133 " parse JSON as OVSDB type, and re-serialize\n"
134 " parse-atoms TYPE ATOM...\n"
135 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
136 " parse-atom-strings TYPE ATOM...\n"
137 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
138 " sort-atoms TYPE ATOM...\n"
139 " print JSON ATOMs in sorted order\n"
140 " parse-data TYPE DATUM...\n"
141 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
142 " parse-data-unique TYPE DATUM...\n"
143 " parse JSON DATUMs as data of given TYPE, eliminating\n"
144 " duplicate keys, and re-serialize\n"
145 " parse-data-strings TYPE DATUM...\n"
146 " parse string DATUMs as data of given TYPE, and re-serialize\n"
147 " parse-column NAME OBJECT\n"
148 " parse column NAME with info OBJECT, and re-serialize\n"
149 " parse-table NAME OBJECT\n"
150 " parse table NAME with info OBJECT\n"
151 " parse-row TABLE ROW..., and re-serialize\n"
152 " parse each ROW of defined TABLE\n"
153 " compare-row TABLE ROW...\n"
154 " mutually compare all of the ROWs, print those that are equal\n"
155 " parse-conditions TABLE CONDITION...\n"
156 " parse each CONDITION on TABLE, and re-serialize\n"
157 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
158 " test CONDITIONS on TABLE against each ROW, print results\n"
159 " parse-mutations TABLE MUTATION...\n"
160 " parse each MUTATION on TABLE, and re-serialize\n"
161 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
162 " execute MUTATIONS on TABLE on each ROW, print results\n"
163 " query TABLE [ROW,...] [CONDITION,...]\n"
164 " add each ROW to TABLE, then query and print the rows that\n"
165 " satisfy each CONDITION.\n"
166 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
167 " add each ROW to TABLE, then query and print the rows that\n"
168 " satisfy each CONDITION and have distinct COLUMNS.\n"
169 " parse-schema JSON\n"
170 " parse JSON as an OVSDB schema, and re-serialize\n"
171 " transact COMMAND\n"
172 " execute each specified transactional COMMAND:\n"
179 " execute SCHEMA TRANSACTION...\n"
180 " executes each TRANSACTION on an initially empty database\n"
181 " the specified SCHEMA\n"
182 " trigger SCHEMA TRANSACTION...\n"
183 " executes each TRANSACTION on an initially empty database\n"
184 " the specified SCHEMA. A TRANSACTION of the form\n"
185 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
186 " simulated time, for causing triggers to time out.\n"
187 " idl SERVER [TRANSACTION...]\n"
188 " connect to SERVER and dump the contents of the database\n"
189 " as seen initially by the IDL implementation and after\n"
190 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
191 " the database or this command will hang.)\n",
192 program_name, program_name);
194 printf("\nOther options:\n"
195 " -t, --timeout=SECS give up after SECS seconds\n"
196 " -h, --help display this help message\n");
200 /* Command helper functions. */
203 parse_json(const char *s)
205 struct json *json = json_from_string(s);
206 if (json->type == JSON_STRING) {
207 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
213 unbox_json(struct json *json)
215 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
216 struct json *inner = json->u.array.elems[0];
217 json->u.array.elems[0] = NULL;
226 print_and_free_json(struct json *json)
228 char *string = json_to_string(json, JSSF_SORT);
235 print_and_free_ovsdb_error(struct ovsdb_error *error)
237 char *string = ovsdb_error_to_string(error);
238 ovsdb_error_destroy(error);
244 check_ovsdb_error(struct ovsdb_error *error)
247 char *s = ovsdb_error_to_string(error);
248 ovsdb_error_destroy(error);
249 ovs_fatal(0, "%s", s);
254 die_if_error(char *error)
257 ovs_fatal(0, "%s", error);
261 /* Command implementations. */
264 do_log_io(int argc, char *argv[])
266 const char *name = argv[1];
267 char *mode_string = argv[2];
269 struct ovsdb_error *error;
270 enum ovsdb_log_open_mode mode;
271 struct ovsdb_log *log;
274 if (!strcmp(mode_string, "read-only")) {
275 mode = OVSDB_LOG_READ_ONLY;
276 } else if (!strcmp(mode_string, "read/write")) {
277 mode = OVSDB_LOG_READ_WRITE;
278 } else if (!strcmp(mode_string, "create")) {
279 mode = OVSDB_LOG_CREATE;
281 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
284 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
285 printf("%s: open successful\n", name);
287 for (i = 3; i < argc; i++) {
288 const char *command = argv[i];
289 if (!strcmp(command, "read")) {
292 error = ovsdb_log_read(log, &json);
294 printf("%s: read: ", name);
296 print_and_free_json(json);
298 printf("end of log\n");
302 } else if (!strncmp(command, "write:", 6)) {
303 struct json *json = parse_json(command + 6);
304 error = ovsdb_log_write(log, json);
306 } else if (!strcmp(command, "commit")) {
307 error = ovsdb_log_commit(log);
309 ovs_fatal(0, "unknown log-io command \"%s\"", command);
312 char *s = ovsdb_error_to_string(error);
313 printf("%s: %s failed: %s\n", name, command, s);
315 ovsdb_error_destroy(error);
317 printf("%s: %s successful\n", name, command);
321 ovsdb_log_close(log);
325 do_default_atoms(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
329 for (type = 0; type < OVSDB_N_TYPES; type++) {
330 union ovsdb_atom atom;
332 if (type == OVSDB_TYPE_VOID) {
336 printf("%s: ", ovsdb_atomic_type_to_string(type));
338 ovsdb_atom_init_default(&atom, type);
339 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
343 ovsdb_atom_destroy(&atom, type);
350 do_default_data(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
355 for (n_min = 0; n_min <= 1; n_min++) {
356 for (key = 0; key < OVSDB_N_TYPES; key++) {
357 if (key == OVSDB_TYPE_VOID) {
360 for (value = 0; value < OVSDB_N_TYPES; value++) {
361 struct ovsdb_datum datum;
362 struct ovsdb_type type;
364 ovsdb_base_type_init(&type.key, key);
365 ovsdb_base_type_init(&type.value, value);
368 assert(ovsdb_type_is_valid(&type));
370 printf("key %s, value %s, n_min %u: ",
371 ovsdb_atomic_type_to_string(key),
372 ovsdb_atomic_type_to_string(value), n_min);
374 ovsdb_datum_init_default(&datum, &type);
375 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
380 ovsdb_datum_destroy(&datum, &type);
381 ovsdb_type_destroy(&type);
390 do_parse_atomic_type(int argc OVS_UNUSED, char *argv[])
392 enum ovsdb_atomic_type type;
395 json = unbox_json(parse_json(argv[1]));
396 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
398 print_and_free_json(ovsdb_atomic_type_to_json(type));
402 do_parse_base_type(int argc OVS_UNUSED, char *argv[])
404 struct ovsdb_base_type base;
407 json = unbox_json(parse_json(argv[1]));
408 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
410 print_and_free_json(ovsdb_base_type_to_json(&base));
411 ovsdb_base_type_destroy(&base);
415 do_parse_type(int argc OVS_UNUSED, char *argv[])
417 struct ovsdb_type type;
420 json = unbox_json(parse_json(argv[1]));
421 check_ovsdb_error(ovsdb_type_from_json(&type, json));
423 print_and_free_json(ovsdb_type_to_json(&type));
424 ovsdb_type_destroy(&type);
428 do_parse_atoms(int argc, char *argv[])
430 struct ovsdb_base_type base;
434 json = unbox_json(parse_json(argv[1]));
435 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
438 for (i = 2; i < argc; i++) {
439 struct ovsdb_error *error;
440 union ovsdb_atom atom;
442 json = unbox_json(parse_json(argv[i]));
443 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
447 print_and_free_ovsdb_error(error);
449 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
450 ovsdb_atom_destroy(&atom, base.type);
453 ovsdb_base_type_destroy(&base);
457 do_parse_atom_strings(int argc, char *argv[])
459 struct ovsdb_base_type base;
463 json = unbox_json(parse_json(argv[1]));
464 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
467 for (i = 2; i < argc; i++) {
468 union ovsdb_atom atom;
471 die_if_error(ovsdb_atom_from_string(&atom, &base, argv[i], NULL));
474 ovsdb_atom_to_string(&atom, base.type, &out);
478 ovsdb_atom_destroy(&atom, base.type);
480 ovsdb_base_type_destroy(&base);
484 do_parse_data__(int argc, char *argv[],
486 (*parse)(struct ovsdb_datum *datum,
487 const struct ovsdb_type *type,
488 const struct json *json,
489 struct ovsdb_symbol_table *symtab))
491 struct ovsdb_type type;
495 json = unbox_json(parse_json(argv[1]));
496 check_ovsdb_error(ovsdb_type_from_json(&type, json));
499 for (i = 2; i < argc; i++) {
500 struct ovsdb_datum datum;
502 json = unbox_json(parse_json(argv[i]));
503 check_ovsdb_error(parse(&datum, &type, json, NULL));
506 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
508 ovsdb_datum_destroy(&datum, &type);
510 ovsdb_type_destroy(&type);
514 do_parse_data(int argc, char *argv[])
516 do_parse_data__(argc, argv, ovsdb_datum_from_json);
520 do_parse_data_unique(int argc, char *argv[])
522 do_parse_data__(argc, argv, ovsdb_datum_from_json_unique);
526 do_parse_data_strings(int argc, char *argv[])
528 struct ovsdb_type type;
532 json = unbox_json(parse_json(argv[1]));
533 check_ovsdb_error(ovsdb_type_from_json(&type, json));
536 for (i = 2; i < argc; i++) {
537 struct ovsdb_datum datum;
540 die_if_error(ovsdb_datum_from_string(&datum, &type, argv[i], NULL));
543 ovsdb_datum_to_string(&datum, &type, &out);
547 ovsdb_datum_destroy(&datum, &type);
549 ovsdb_type_destroy(&type);
552 static enum ovsdb_atomic_type compare_atoms_atomic_type;
555 compare_atoms(const void *a_, const void *b_)
557 const union ovsdb_atom *a = a_;
558 const union ovsdb_atom *b = b_;
560 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
564 do_sort_atoms(int argc OVS_UNUSED, char *argv[])
566 struct ovsdb_base_type base;
567 union ovsdb_atom *atoms;
568 struct json *json, **json_atoms;
572 json = unbox_json(parse_json(argv[1]));
573 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
576 json = unbox_json(parse_json(argv[2]));
577 if (json->type != JSON_ARRAY) {
578 ovs_fatal(0, "second argument must be array");
581 /* Convert JSON atoms to internal representation. */
582 n_atoms = json->u.array.n;
583 atoms = xmalloc(n_atoms * sizeof *atoms);
584 for (i = 0; i < n_atoms; i++) {
585 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
586 json->u.array.elems[i], NULL));
591 compare_atoms_atomic_type = base.type;
592 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
594 /* Convert internal representation back to JSON. */
595 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
596 for (i = 0; i < n_atoms; i++) {
597 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
598 ovsdb_atom_destroy(&atoms[i], base.type);
600 print_and_free_json(json_array_create(json_atoms, n_atoms));
602 ovsdb_base_type_destroy(&base);
606 do_parse_column(int argc OVS_UNUSED, char *argv[])
608 struct ovsdb_column *column;
611 json = parse_json(argv[2]);
612 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
614 print_and_free_json(ovsdb_column_to_json(column));
615 ovsdb_column_destroy(column);
619 do_parse_table(int argc OVS_UNUSED, char *argv[])
621 struct ovsdb_table_schema *ts;
624 json = parse_json(argv[2]);
625 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
627 print_and_free_json(ovsdb_table_schema_to_json(ts));
628 ovsdb_table_schema_destroy(ts);
632 do_parse_rows(int argc, char *argv[])
634 struct ovsdb_column_set all_columns;
635 struct ovsdb_table_schema *ts;
636 struct ovsdb_table *table;
640 json = unbox_json(parse_json(argv[1]));
641 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
644 table = ovsdb_table_create(ts);
645 ovsdb_column_set_init(&all_columns);
646 ovsdb_column_set_add_all(&all_columns, table);
648 for (i = 2; i < argc; i++) {
649 struct ovsdb_column_set columns;
650 struct ovsdb_row *row;
652 ovsdb_column_set_init(&columns);
653 row = ovsdb_row_create(table);
655 json = unbox_json(parse_json(argv[i]));
656 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
659 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
661 if (columns.n_columns) {
667 for (j = 0; j < columns.n_columns; j++) {
668 svec_add(&names, columns.columns[j]->name);
671 s = svec_join(&names, ", ", "");
674 svec_destroy(&names);
679 ovsdb_column_set_destroy(&columns);
680 ovsdb_row_destroy(row);
683 ovsdb_column_set_destroy(&all_columns);
684 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
688 do_compare_rows(int argc, char *argv[])
690 struct ovsdb_column_set all_columns;
691 struct ovsdb_table_schema *ts;
692 struct ovsdb_table *table;
693 struct ovsdb_row **rows;
699 json = unbox_json(parse_json(argv[1]));
700 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
703 table = ovsdb_table_create(ts);
704 ovsdb_column_set_init(&all_columns);
705 ovsdb_column_set_add_all(&all_columns, table);
708 rows = xmalloc(sizeof *rows * n_rows);
709 names = xmalloc(sizeof *names * n_rows);
710 for (i = 0; i < n_rows; i++) {
711 rows[i] = ovsdb_row_create(table);
713 json = parse_json(argv[i + 2]);
714 if (json->type != JSON_ARRAY || json->u.array.n != 2
715 || json->u.array.elems[0]->type != JSON_STRING) {
716 ovs_fatal(0, "\"%s\" does not have expected form "
717 "[\"name\", {data}]", argv[i]);
719 names[i] = xstrdup(json->u.array.elems[0]->u.string);
720 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
724 for (i = 0; i < n_rows; i++) {
725 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
726 for (j = i + 1; j < n_rows; j++) {
727 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
728 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
729 printf("%s == %s\n", names[i], names[j]);
730 if (i_hash != j_hash) {
731 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
734 } else if (i_hash == j_hash) {
735 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
739 for (i = 0; i < n_rows; i++) {
740 ovsdb_row_destroy(rows[i]);
746 ovsdb_column_set_destroy(&all_columns);
747 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
751 do_parse_conditions(int argc, char *argv[])
753 struct ovsdb_table_schema *ts;
758 json = unbox_json(parse_json(argv[1]));
759 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
762 for (i = 2; i < argc; i++) {
763 struct ovsdb_condition cnd;
764 struct ovsdb_error *error;
766 json = parse_json(argv[i]);
767 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
769 print_and_free_json(ovsdb_condition_to_json(&cnd));
771 char *s = ovsdb_error_to_string(error);
772 ovs_error(0, "%s", s);
774 ovsdb_error_destroy(error);
779 ovsdb_condition_destroy(&cnd);
781 ovsdb_table_schema_destroy(ts);
787 do_evaluate_conditions(int argc OVS_UNUSED, char *argv[])
789 struct ovsdb_table_schema *ts;
790 struct ovsdb_table *table;
791 struct ovsdb_condition *conditions;
793 struct ovsdb_row **rows;
798 /* Parse table schema, create table. */
799 json = unbox_json(parse_json(argv[1]));
800 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
803 table = ovsdb_table_create(ts);
805 /* Parse conditions. */
806 json = parse_json(argv[2]);
807 if (json->type != JSON_ARRAY) {
808 ovs_fatal(0, "CONDITION argument is not JSON array");
810 n_conditions = json->u.array.n;
811 conditions = xmalloc(n_conditions * sizeof *conditions);
812 for (i = 0; i < n_conditions; i++) {
813 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
814 NULL, &conditions[i]));
819 json = parse_json(argv[3]);
820 if (json->type != JSON_ARRAY) {
821 ovs_fatal(0, "ROW argument is not JSON array");
823 n_rows = json->u.array.n;
824 rows = xmalloc(n_rows * sizeof *rows);
825 for (i = 0; i < n_rows; i++) {
826 rows[i] = ovsdb_row_create(table);
827 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
832 for (i = 0; i < n_conditions; i++) {
833 printf("condition %2zu:", i);
834 for (j = 0; j < n_rows; j++) {
835 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
839 putchar(result ? 'T' : '-');
844 for (i = 0; i < n_conditions; i++) {
845 ovsdb_condition_destroy(&conditions[i]);
848 for (i = 0; i < n_rows; i++) {
849 ovsdb_row_destroy(rows[i]);
852 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
856 do_parse_mutations(int argc, char *argv[])
858 struct ovsdb_table_schema *ts;
863 json = unbox_json(parse_json(argv[1]));
864 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
867 for (i = 2; i < argc; i++) {
868 struct ovsdb_mutation_set set;
869 struct ovsdb_error *error;
871 json = parse_json(argv[i]);
872 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
874 print_and_free_json(ovsdb_mutation_set_to_json(&set));
876 char *s = ovsdb_error_to_string(error);
877 ovs_error(0, "%s", s);
879 ovsdb_error_destroy(error);
884 ovsdb_mutation_set_destroy(&set);
886 ovsdb_table_schema_destroy(ts);
892 do_execute_mutations(int argc OVS_UNUSED, char *argv[])
894 struct ovsdb_table_schema *ts;
895 struct ovsdb_table *table;
896 struct ovsdb_mutation_set *sets;
898 struct ovsdb_row **rows;
903 /* Parse table schema, create table. */
904 json = unbox_json(parse_json(argv[1]));
905 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
908 table = ovsdb_table_create(ts);
910 /* Parse mutations. */
911 json = parse_json(argv[2]);
912 if (json->type != JSON_ARRAY) {
913 ovs_fatal(0, "MUTATION argument is not JSON array");
915 n_sets = json->u.array.n;
916 sets = xmalloc(n_sets * sizeof *sets);
917 for (i = 0; i < n_sets; i++) {
918 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
919 json->u.array.elems[i],
925 json = parse_json(argv[3]);
926 if (json->type != JSON_ARRAY) {
927 ovs_fatal(0, "ROW argument is not JSON array");
929 n_rows = json->u.array.n;
930 rows = xmalloc(n_rows * sizeof *rows);
931 for (i = 0; i < n_rows; i++) {
932 rows[i] = ovsdb_row_create(table);
933 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
938 for (i = 0; i < n_sets; i++) {
939 printf("mutation %2zu:\n", i);
940 for (j = 0; j < n_rows; j++) {
941 struct ovsdb_error *error;
942 struct ovsdb_row *row;
944 row = ovsdb_row_clone(rows[j]);
945 error = ovsdb_mutation_set_execute(row, &sets[i]);
947 printf("row %zu: ", j);
949 print_and_free_ovsdb_error(error);
951 struct ovsdb_column_set columns;
952 struct shash_node *node;
954 ovsdb_column_set_init(&columns);
955 SHASH_FOR_EACH (node, &ts->columns) {
956 struct ovsdb_column *c = node->data;
957 if (!ovsdb_datum_equals(&row->fields[c->index],
958 &rows[j]->fields[c->index],
960 ovsdb_column_set_add(&columns, c);
963 if (columns.n_columns) {
964 print_and_free_json(ovsdb_row_to_json(row, &columns));
966 printf("no change\n");
968 ovsdb_column_set_destroy(&columns);
970 ovsdb_row_destroy(row);
975 for (i = 0; i < n_sets; i++) {
976 ovsdb_mutation_set_destroy(&sets[i]);
979 for (i = 0; i < n_rows; i++) {
980 ovsdb_row_destroy(rows[i]);
983 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
986 struct do_query_cbdata {
987 struct uuid *row_uuids;
993 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
995 struct do_query_cbdata *cbdata = cbdata_;
998 for (i = 0; i < cbdata->n_rows; i++) {
999 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1000 cbdata->counts[i]++;
1008 do_query(int argc OVS_UNUSED, char *argv[])
1010 struct do_query_cbdata cbdata;
1011 struct ovsdb_table_schema *ts;
1012 struct ovsdb_table *table;
1017 /* Parse table schema, create table. */
1018 json = unbox_json(parse_json(argv[1]));
1019 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1022 table = ovsdb_table_create(ts);
1024 /* Parse rows, add to table. */
1025 json = parse_json(argv[2]);
1026 if (json->type != JSON_ARRAY) {
1027 ovs_fatal(0, "ROW argument is not JSON array");
1029 cbdata.n_rows = json->u.array.n;
1030 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1031 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1032 for (i = 0; i < cbdata.n_rows; i++) {
1033 struct ovsdb_row *row = ovsdb_row_create(table);
1034 uuid_generate(ovsdb_row_get_uuid_rw(row));
1035 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1037 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1038 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1039 UUID_ARGS(ovsdb_row_get_uuid(row)));
1041 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1042 ovsdb_table_put_row(table, row);
1046 /* Parse conditions and execute queries. */
1047 json = parse_json(argv[3]);
1048 if (json->type != JSON_ARRAY) {
1049 ovs_fatal(0, "CONDITION argument is not JSON array");
1051 for (i = 0; i < json->u.array.n; i++) {
1052 struct ovsdb_condition cnd;
1055 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1058 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1059 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1061 printf("query %2zu:", i);
1062 for (j = 0; j < cbdata.n_rows; j++) {
1066 if (cbdata.counts[j]) {
1067 printf("%d", cbdata.counts[j]);
1068 if (cbdata.counts[j] > 1) {
1078 ovsdb_condition_destroy(&cnd);
1082 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1087 struct do_query_distinct_class {
1088 struct ovsdb_row *example;
1092 struct do_query_distinct_row {
1094 struct do_query_distinct_class *class;
1098 do_query_distinct(int argc OVS_UNUSED, char *argv[])
1100 struct ovsdb_column_set columns;
1101 struct ovsdb_table_schema *ts;
1102 struct ovsdb_table *table;
1103 struct do_query_distinct_row *rows;
1105 struct do_query_distinct_class *classes;
1111 /* Parse table schema, create table. */
1112 json = unbox_json(parse_json(argv[1]));
1113 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1116 table = ovsdb_table_create(ts);
1118 /* Parse column set. */
1119 json = parse_json(argv[4]);
1120 check_ovsdb_error(ovsdb_column_set_from_json(json, table, &columns));
1123 /* Parse rows, add to table. */
1124 json = parse_json(argv[2]);
1125 if (json->type != JSON_ARRAY) {
1126 ovs_fatal(0, "ROW argument is not JSON array");
1128 n_rows = json->u.array.n;
1129 rows = xmalloc(n_rows * sizeof *rows);
1130 classes = xmalloc(n_rows * sizeof *classes);
1132 for (i = 0; i < n_rows; i++) {
1133 struct ovsdb_row *row;
1137 row = ovsdb_row_create(table);
1138 uuid_generate(ovsdb_row_get_uuid_rw(row));
1139 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1142 /* Initialize row and find equivalence class. */
1143 rows[i].uuid = *ovsdb_row_get_uuid(row);
1144 rows[i].class = NULL;
1145 for (j = 0; j < n_classes; j++) {
1146 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1147 rows[i].class = &classes[j];
1151 if (!rows[i].class) {
1152 rows[i].class = &classes[n_classes];
1153 classes[n_classes].example = ovsdb_row_clone(row);
1157 /* Add row to table. */
1158 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1159 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1160 UUID_ARGS(ovsdb_row_get_uuid(row)));
1162 ovsdb_table_put_row(table, row);
1167 /* Parse conditions and execute queries. */
1168 json = parse_json(argv[3]);
1169 if (json->type != JSON_ARRAY) {
1170 ovs_fatal(0, "CONDITION argument is not JSON array");
1172 for (i = 0; i < json->u.array.n; i++) {
1173 struct ovsdb_row_set results;
1174 struct ovsdb_condition cnd;
1176 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1179 for (j = 0; j < n_classes; j++) {
1180 classes[j].count = 0;
1182 ovsdb_row_set_init(&results);
1183 ovsdb_query_distinct(table, &cnd, &columns, &results);
1184 for (j = 0; j < results.n_rows; j++) {
1185 for (k = 0; k < n_rows; k++) {
1186 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1188 rows[k].class->count++;
1192 ovsdb_row_set_destroy(&results);
1194 printf("query %2zu:", i);
1195 for (j = 0; j < n_rows; j++) {
1196 int count = rows[j].class->count;
1203 printf("%d", count);
1205 } else if (count == 1) {
1206 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1213 ovsdb_condition_destroy(&cnd);
1217 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1223 do_parse_schema(int argc OVS_UNUSED, char *argv[])
1225 struct ovsdb_schema *schema;
1228 json = parse_json(argv[1]);
1229 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1231 print_and_free_json(ovsdb_schema_to_json(schema));
1232 ovsdb_schema_destroy(schema);
1236 do_execute(int argc OVS_UNUSED, char *argv[])
1238 struct ovsdb_schema *schema;
1243 /* Create database. */
1244 json = parse_json(argv[1]);
1245 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1247 db = ovsdb_create(schema);
1249 for (i = 2; i < argc; i++) {
1250 struct json *params, *result;
1253 params = parse_json(argv[i]);
1254 result = ovsdb_execute(db, params, 0, NULL);
1255 s = json_to_string(result, JSSF_SORT);
1258 json_destroy(params);
1259 json_destroy(result);
1265 struct test_trigger {
1266 struct ovsdb_trigger trigger;
1271 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1273 struct json *result;
1276 result = ovsdb_trigger_steal_result(&t->trigger);
1277 s = json_to_string(result, JSSF_SORT);
1278 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1280 json_destroy(result);
1281 ovsdb_trigger_destroy(&t->trigger);
1286 do_trigger(int argc OVS_UNUSED, char *argv[])
1288 struct ovsdb_schema *schema;
1289 struct list completions;
1296 /* Create database. */
1297 json = parse_json(argv[1]);
1298 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1300 db = ovsdb_create(schema);
1302 list_init(&completions);
1305 for (i = 2; i < argc; i++) {
1306 struct json *params = parse_json(argv[i]);
1307 if (params->type == JSON_ARRAY
1308 && json_array(params)->n == 2
1309 && json_array(params)->elems[0]->type == JSON_STRING
1310 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1311 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1312 now += json_integer(json_array(params)->elems[1]);
1313 json_destroy(params);
1315 struct test_trigger *t = xmalloc(sizeof *t);
1316 ovsdb_trigger_init(db, &t->trigger, params, &completions, now);
1317 t->number = number++;
1318 if (ovsdb_trigger_is_complete(&t->trigger)) {
1319 do_trigger_dump(t, now, "immediate");
1321 printf("t=%lld: new trigger %d\n", now, t->number);
1325 ovsdb_trigger_run(db, now);
1326 while (!list_is_empty(&completions)) {
1327 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions),
1328 struct test_trigger, trigger.node),
1332 ovsdb_trigger_wait(db, now);
1333 poll_immediate_wake();
1341 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1346 /* "transact" command. */
1348 static struct ovsdb *do_transact_db;
1349 static struct ovsdb_txn *do_transact_txn;
1350 static struct ovsdb_table *do_transact_table;
1353 do_transact_commit(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1355 ovsdb_txn_commit(do_transact_txn, false);
1356 do_transact_txn = NULL;
1360 do_transact_abort(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1362 ovsdb_txn_abort(do_transact_txn);
1363 do_transact_txn = NULL;
1367 uuid_from_integer(int integer, struct uuid *uuid)
1370 uuid->parts[3] = integer;
1373 static const struct ovsdb_row *
1374 do_transact_find_row(const char *uuid_string)
1376 const struct ovsdb_row *row;
1379 uuid_from_integer(atoi(uuid_string), &uuid);
1380 row = ovsdb_table_get_row(do_transact_table, &uuid);
1382 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1389 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1392 if (integer != -1) {
1393 const struct ovsdb_column *column;
1395 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1397 row->fields[column->index].keys[0].integer = integer;
1402 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1404 const struct ovsdb_column *column;
1406 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1408 return row->fields[column->index].keys[0].integer;
1412 do_transact_set_i_j(struct ovsdb_row *row,
1413 const char *i_string, const char *j_string)
1415 do_transact_set_integer(row, "i", atoi(i_string));
1416 do_transact_set_integer(row, "j", atoi(j_string));
1420 do_transact_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1422 struct ovsdb_row *row;
1425 row = ovsdb_row_create(do_transact_table);
1428 uuid = ovsdb_row_get_uuid_rw(row);
1429 uuid_from_integer(atoi(argv[1]), uuid);
1430 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1431 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1435 do_transact_set_i_j(row, argv[2], argv[3]);
1438 ovsdb_txn_row_insert(do_transact_txn, row);
1442 do_transact_delete(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1444 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1445 ovsdb_txn_row_delete(do_transact_txn, row);
1449 do_transact_modify(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1451 const struct ovsdb_row *row_ro;
1452 struct ovsdb_row *row_rw;
1454 row_ro = do_transact_find_row(argv[1]);
1455 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1456 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1460 compare_rows_by_uuid(const void *a_, const void *b_)
1462 struct ovsdb_row *const *ap = a_;
1463 struct ovsdb_row *const *bp = b_;
1465 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1469 do_transact_print(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1471 const struct ovsdb_row **rows;
1472 const struct ovsdb_row *row;
1476 n_rows = hmap_count(&do_transact_table->rows);
1477 rows = xmalloc(n_rows * sizeof *rows);
1479 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1480 &do_transact_table->rows) {
1483 assert(i == n_rows);
1485 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1487 for (i = 0; i < n_rows; i++) {
1488 printf("\n%"PRId32": i=%d, j=%d",
1489 ovsdb_row_get_uuid(rows[i])->parts[3],
1490 do_transact_get_integer(rows[i], "i"),
1491 do_transact_get_integer(rows[i], "j"));
1498 do_transact(int argc, char *argv[])
1500 static const struct command do_transact_commands[] = {
1501 { "commit", 0, 0, do_transact_commit },
1502 { "abort", 0, 0, do_transact_abort },
1503 { "insert", 2, 3, do_transact_insert },
1504 { "delete", 1, 1, do_transact_delete },
1505 { "modify", 2, 3, do_transact_modify },
1506 { "print", 0, 0, do_transact_print },
1507 { NULL, 0, 0, NULL },
1510 struct ovsdb_schema *schema;
1515 json = parse_json("{\"name\": \"testdb\", "
1519 " {\"i\": {\"type\": \"integer\"}, "
1520 " \"j\": {\"type\": \"integer\"}}}}}");
1521 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1523 do_transact_db = ovsdb_create(schema);
1524 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1525 assert(do_transact_table != NULL);
1527 for (i = 1; i < argc; i++) {
1528 struct json *command;
1533 command = parse_json(argv[i]);
1534 if (command->type != JSON_ARRAY) {
1535 ovs_fatal(0, "transaction %d must be JSON array "
1536 "with at least 1 element", i);
1539 n_args = command->u.array.n;
1540 args = xmalloc((n_args + 1) * sizeof *args);
1541 for (j = 0; j < n_args; j++) {
1542 struct json *s = command->u.array.elems[j];
1543 if (s->type != JSON_STRING) {
1544 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1547 args[j] = xstrdup(json_string(s));
1549 args[n_args] = NULL;
1551 if (!do_transact_txn) {
1552 do_transact_txn = ovsdb_txn_create(do_transact_db);
1555 for (j = 0; j < n_args; j++) {
1559 fputs(args[j], stdout);
1562 run_command(n_args, args, do_transact_commands);
1565 for (j = 0; j < n_args; j++) {
1569 json_destroy(command);
1571 ovsdb_txn_abort(do_transact_txn);
1572 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1576 compare_link1(const void *a_, const void *b_)
1578 const struct idltest_link1 *const *ap = a_;
1579 const struct idltest_link1 *const *bp = b_;
1580 const struct idltest_link1 *a = *ap;
1581 const struct idltest_link1 *b = *bp;
1583 return a->i < b->i ? -1 : a->i > b->i;
1587 print_idl(struct ovsdb_idl *idl, int step)
1589 const struct idltest_simple *s;
1590 const struct idltest_link1 *l1;
1591 const struct idltest_link2 *l2;
1594 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1597 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1598 step, s->i, s->r, s->b ? "true" : "false",
1599 s->s, UUID_ARGS(&s->u));
1600 for (i = 0; i < s->n_ia; i++) {
1601 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1604 for (i = 0; i < s->n_ra; i++) {
1605 printf("%s%g", i ? " " : "", s->ra[i]);
1608 for (i = 0; i < s->n_ba; i++) {
1609 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1612 for (i = 0; i < s->n_sa; i++) {
1613 printf("%s%s", i ? " " : "", s->sa[i]);
1616 for (i = 0; i < s->n_ua; i++) {
1617 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1619 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1622 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1623 struct idltest_link1 **links;
1626 printf("%03d: i=%"PRId64" k=", step, l1->i);
1628 printf("%"PRId64, l1->k->i);
1631 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1632 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1633 for (i = 0; i < l1->n_ka; i++) {
1634 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1639 printf("%"PRId64, l1->l2->i);
1641 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1644 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1645 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1647 printf("%"PRId64, l2->l1->i);
1649 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1653 printf("%03d: empty\n", step);
1658 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1663 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1664 char *name = xasprintf("#%zu#", *n);
1665 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1666 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1669 } else if (json->type == JSON_ARRAY) {
1672 for (i = 0; i < json->u.array.n; i++) {
1673 parse_uuids(json->u.array.elems[i], symtab, n);
1675 } else if (json->type == JSON_OBJECT) {
1676 const struct shash_node *node;
1678 SHASH_FOR_EACH (node, json_object(json)) {
1679 parse_uuids(node->data, symtab, n);
1685 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1687 if (json->type == JSON_STRING) {
1688 const struct ovsdb_symbol *symbol;
1690 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1692 free(json->u.string);
1693 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1695 } else if (json->type == JSON_ARRAY) {
1698 for (i = 0; i < json->u.array.n; i++) {
1699 substitute_uuids(json->u.array.elems[i], symtab);
1701 } else if (json->type == JSON_OBJECT) {
1702 const struct shash_node *node;
1704 SHASH_FOR_EACH (node, json_object(json)) {
1705 substitute_uuids(node->data, symtab);
1710 static const struct idltest_simple *
1711 idltest_find_simple(struct ovsdb_idl *idl, int i)
1713 const struct idltest_simple *s;
1715 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1724 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1726 char *cmd, *save_ptr1 = NULL;
1727 struct ovsdb_idl_txn *txn;
1728 enum ovsdb_idl_txn_status status;
1729 bool increment = false;
1731 txn = ovsdb_idl_txn_create(idl);
1732 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1733 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1734 char *save_ptr2 = NULL;
1735 char *name, *arg1, *arg2, *arg3;
1737 name = strtok_r(cmd, " ", &save_ptr2);
1738 arg1 = strtok_r(NULL, " ", &save_ptr2);
1739 arg2 = strtok_r(NULL, " ", &save_ptr2);
1740 arg3 = strtok_r(NULL, " ", &save_ptr2);
1742 if (!strcmp(name, "set")) {
1743 const struct idltest_simple *s;
1746 ovs_fatal(0, "\"set\" command requires 3 arguments");
1749 s = idltest_find_simple(idl, atoi(arg1));
1751 ovs_fatal(0, "\"set\" command asks for nonexistent "
1752 "i=%d", atoi(arg1));
1755 if (!strcmp(arg2, "b")) {
1756 idltest_simple_set_b(s, atoi(arg3));
1757 } else if (!strcmp(arg2, "s")) {
1758 idltest_simple_set_s(s, arg3);
1759 } else if (!strcmp(arg2, "u")) {
1761 uuid_from_string(&uuid, arg3);
1762 idltest_simple_set_u(s, uuid);
1763 } else if (!strcmp(arg2, "r")) {
1764 idltest_simple_set_r(s, atof(arg3));
1766 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1769 } else if (!strcmp(name, "insert")) {
1770 struct idltest_simple *s;
1772 if (!arg1 || arg2) {
1773 ovs_fatal(0, "\"set\" command requires 1 argument");
1776 s = idltest_simple_insert(txn);
1777 idltest_simple_set_i(s, atoi(arg1));
1778 } else if (!strcmp(name, "delete")) {
1779 const struct idltest_simple *s;
1781 if (!arg1 || arg2) {
1782 ovs_fatal(0, "\"set\" command requires 1 argument");
1785 s = idltest_find_simple(idl, atoi(arg1));
1787 ovs_fatal(0, "\"set\" command asks for nonexistent "
1788 "i=%d", atoi(arg1));
1790 idltest_simple_delete(s);
1791 } else if (!strcmp(name, "increment")) {
1792 if (!arg2 || arg3) {
1793 ovs_fatal(0, "\"set\" command requires 2 arguments");
1795 ovsdb_idl_txn_increment(txn, arg1, arg2, NULL);
1798 ovs_fatal(0, "unknown command %s", name);
1802 status = ovsdb_idl_txn_commit_block(txn);
1803 printf("%03d: commit, status=%s",
1804 step, ovsdb_idl_txn_status_to_string(status));
1806 printf(", increment=%"PRId64,
1807 ovsdb_idl_txn_get_increment_new_value(txn));
1810 ovsdb_idl_txn_destroy(txn);
1814 do_idl(int argc, char *argv[])
1816 struct jsonrpc *rpc;
1817 struct ovsdb_idl *idl;
1818 unsigned int seqno = 0;
1819 struct ovsdb_symbol_table *symtab;
1827 idl = ovsdb_idl_create(argv[1], &idltest_idl_class);
1829 struct stream *stream;
1831 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream),
1834 ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
1836 rpc = jsonrpc_open(stream);
1841 setvbuf(stdout, NULL, _IOLBF, 0);
1843 symtab = ovsdb_symbol_table_create();
1844 for (i = 2; i < argc; i++) {
1845 char *arg = argv[i];
1846 struct jsonrpc_msg *request, *reply;
1850 /* The previous transaction didn't change anything. */
1853 /* Wait for update. */
1854 while (ovsdb_idl_get_seqno(idl) == seqno && !ovsdb_idl_run(idl)) {
1857 ovsdb_idl_wait(idl);
1863 print_idl(idl, step++);
1865 seqno = ovsdb_idl_get_seqno(idl);
1867 if (!strcmp(arg, "reconnect")) {
1868 printf("%03d: reconnect\n", step++);
1869 ovsdb_idl_force_reconnect(idl);
1870 } else if (arg[0] != '[') {
1871 idl_set(idl, arg, step++);
1873 struct json *json = parse_json(arg);
1874 substitute_uuids(json, symtab);
1875 request = jsonrpc_create_request("transact", json, NULL);
1876 error = jsonrpc_transact_block(rpc, request, &reply);
1878 ovs_fatal(error, "jsonrpc transaction failed");
1880 printf("%03d: ", step++);
1881 if (reply->result) {
1882 parse_uuids(reply->result, symtab, &n_uuids);
1884 json_destroy(reply->id);
1886 print_and_free_json(jsonrpc_msg_to_json(reply));
1889 ovsdb_symbol_table_destroy(symtab);
1894 while (ovsdb_idl_get_seqno(idl) == seqno && !ovsdb_idl_run(idl)) {
1895 ovsdb_idl_wait(idl);
1898 print_idl(idl, step++);
1899 ovsdb_idl_destroy(idl);
1900 printf("%03d: done\n", step);
1903 static struct command all_commands[] = {
1904 { "log-io", 2, INT_MAX, do_log_io },
1905 { "default-atoms", 0, 0, do_default_atoms },
1906 { "default-data", 0, 0, do_default_data },
1907 { "parse-atomic-type", 1, 1, do_parse_atomic_type },
1908 { "parse-base-type", 1, 1, do_parse_base_type },
1909 { "parse-type", 1, 1, do_parse_type },
1910 { "parse-atoms", 2, INT_MAX, do_parse_atoms },
1911 { "parse-atom-strings", 2, INT_MAX, do_parse_atom_strings },
1912 { "parse-data", 2, INT_MAX, do_parse_data },
1913 { "parse-data-unique", 2, INT_MAX, do_parse_data_unique },
1914 { "parse-data-strings", 2, INT_MAX, do_parse_data_strings },
1915 { "sort-atoms", 2, 2, do_sort_atoms },
1916 { "parse-column", 2, 2, do_parse_column },
1917 { "parse-table", 2, 2, do_parse_table },
1918 { "parse-rows", 2, INT_MAX, do_parse_rows },
1919 { "compare-rows", 2, INT_MAX, do_compare_rows },
1920 { "parse-conditions", 2, INT_MAX, do_parse_conditions },
1921 { "evaluate-conditions", 3, 3, do_evaluate_conditions },
1922 { "parse-mutations", 2, INT_MAX, do_parse_mutations },
1923 { "execute-mutations", 3, 3, do_execute_mutations },
1924 { "query", 3, 3, do_query },
1925 { "query-distinct", 4, 4, do_query_distinct },
1926 { "transact", 1, INT_MAX, do_transact },
1927 { "parse-schema", 1, 1, do_parse_schema },
1928 { "execute", 2, INT_MAX, do_execute },
1929 { "trigger", 2, INT_MAX, do_trigger },
1930 { "idl", 1, INT_MAX, do_idl },
1931 { "help", 0, INT_MAX, do_help },
1932 { NULL, 0, 0, NULL },