2 * Copyright (c) 2009 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"
29 #include "ovsdb-data.h"
30 #include "ovsdb-error.h"
31 #include "ovsdb-idl.h"
32 #include "ovsdb-types.h"
33 #include "ovsdb/column.h"
34 #include "ovsdb/condition.h"
35 #include "ovsdb/file.h"
36 #include "ovsdb/log.h"
37 #include "ovsdb/mutation.h"
38 #include "ovsdb/ovsdb.h"
39 #include "ovsdb/query.h"
40 #include "ovsdb/row.h"
41 #include "ovsdb/table.h"
42 #include "ovsdb/transaction.h"
43 #include "ovsdb/trigger.h"
44 #include "poll-loop.h"
47 #include "tests/idltest.h"
52 static struct command all_commands[];
54 static void usage(void) NO_RETURN;
55 static void parse_options(int argc, char *argv[]);
58 main(int argc, char *argv[])
60 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"
123 " parse-atomic-type TYPE\n"
124 " parse TYPE as OVSDB atomic type, and re-serialize\n"
126 " parse JSON as OVSDB type, and re-serialize\n"
127 " parse-atoms TYPE ATOM...\n"
128 " parse ATOMs as atoms of given TYPE, and re-serialize\n"
129 " sort-atoms TYPE ATOM...\n"
130 " print ATOMs in sorted order, and re-serialize\n"
131 " parse-data TYPE DATUM...\n"
132 " parse DATUMs as data of given TYPE, and re-serialize\n"
133 " parse-column NAME OBJECT\n"
134 " parse column NAME with info OBJECT, and re-serialize\n"
135 " parse-table NAME OBJECT\n"
136 " parse table NAME with info OBJECT\n"
137 " parse-row TABLE ROW..., and re-serialize\n"
138 " parse each ROW of defined TABLE\n"
139 " compare-row TABLE ROW...\n"
140 " mutually compare all of the ROWs, print those that are equal\n"
141 " parse-conditions TABLE CONDITION...\n"
142 " parse each CONDITION on TABLE, and re-serialize\n"
143 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
144 " test CONDITIONS on TABLE against each ROW, print results\n"
145 " parse-mutations TABLE MUTATION...\n"
146 " parse each MUTATION on TABLE, and re-serialize\n"
147 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
148 " execute MUTATIONS on TABLE on each ROW, print results\n"
149 " query TABLE [ROW,...] [CONDITION,...]\n"
150 " add each ROW to TABLE, then query and print the rows that\n"
151 " satisfy each CONDITION.\n"
152 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
153 " add each ROW to TABLE, then query and print the rows that\n"
154 " satisfy each CONDITION and have distinct COLUMNS.\n"
155 " transact COMMAND\n"
156 " execute each specified transactional COMMAND:\n"
163 " execute SCHEMA TRANSACTION...\n"
164 " executes each TRANSACTION on an initially empty database\n"
165 " the specified SCHEMA\n"
166 " trigger SCHEMA TRANSACTION...\n"
167 " executes each TRANSACTION on an initially empty database\n"
168 " the specified SCHEMA. A TRANSACTION of the form\n"
169 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
170 " simulated time, for causing triggers to time out.\n"
171 " idl SERVER [TRANSACTION...]\n"
172 " connect to SERVER and dump the contents of the database\n"
173 " as seen initially by the IDL implementation and after\n"
174 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
175 " the database or this command will hang.)\n",
176 program_name, program_name);
178 printf("\nOther options:\n"
179 " -t, --timeout=SECS give up after SECS seconds\n"
180 " -h, --help display this help message\n");
184 /* Command helper functions. */
187 parse_json(const char *s)
189 struct json *json = json_from_string(s);
190 if (json->type == JSON_STRING) {
191 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
197 unbox_json(struct json *json)
199 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
200 struct json *inner = json->u.array.elems[0];
201 json->u.array.elems[0] = NULL;
210 print_and_free_json(struct json *json)
212 char *string = json_to_string(json, JSSF_SORT);
219 print_and_free_ovsdb_error(struct ovsdb_error *error)
221 char *string = ovsdb_error_to_string(error);
222 ovsdb_error_destroy(error);
228 check_ovsdb_error(struct ovsdb_error *error)
231 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
235 /* Command implementations. */
238 do_log_io(int argc, char *argv[])
240 const char *name = argv[1];
241 char *mode = argv[2];
243 struct ovsdb_error *error;
244 struct ovsdb_log *log;
245 char *save_ptr = NULL;
250 for (flags = 0, token = strtok_r(mode, " |", &save_ptr); token != NULL;
251 token = strtok_r(NULL, " |", &save_ptr))
253 if (!strcmp(token, "O_RDONLY")) {
255 } else if (!strcmp(token, "O_RDWR")) {
257 } else if (!strcmp(token, "O_TRUNC")) {
259 } else if (!strcmp(token, "O_CREAT")) {
261 } else if (!strcmp(token, "O_EXCL")) {
263 } else if (!strcmp(token, "O_TRUNC")) {
268 check_ovsdb_error(ovsdb_log_open(name, flags, &log));
269 printf("%s: open successful\n", name);
271 for (i = 3; i < argc; i++) {
272 const char *command = argv[i];
273 if (!strcmp(command, "read")) {
276 error = ovsdb_log_read(log, &json);
278 printf("%s: read: ", name);
280 print_and_free_json(json);
282 printf("end of log\n");
286 } else if (!strncmp(command, "write:", 6)) {
287 struct json *json = parse_json(command + 6);
288 error = ovsdb_log_write(log, json);
290 } else if (!strcmp(command, "commit")) {
291 error = ovsdb_log_commit(log);
293 ovs_fatal(0, "unknown log-io command \"%s\"", command);
296 char *s = ovsdb_error_to_string(error);
297 printf("%s: %s failed: %s\n", name, command, s);
300 printf("%s: %s successful\n", name, command);
304 ovsdb_log_close(log);
308 do_parse_atomic_type(int argc UNUSED, char *argv[])
310 enum ovsdb_atomic_type type;
313 json = unbox_json(parse_json(argv[1]));
314 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
316 print_and_free_json(ovsdb_atomic_type_to_json(type));
320 do_parse_type(int argc UNUSED, char *argv[])
322 struct ovsdb_type type;
325 json = unbox_json(parse_json(argv[1]));
326 check_ovsdb_error(ovsdb_type_from_json(&type, json));
328 print_and_free_json(ovsdb_type_to_json(&type));
332 do_parse_atoms(int argc, char *argv[])
334 enum ovsdb_atomic_type type;
338 json = unbox_json(parse_json(argv[1]));
339 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
342 for (i = 2; i < argc; i++) {
343 union ovsdb_atom atom;
345 json = unbox_json(parse_json(argv[i]));
346 check_ovsdb_error(ovsdb_atom_from_json(&atom, type, json, NULL));
349 print_and_free_json(ovsdb_atom_to_json(&atom, type));
351 ovsdb_atom_destroy(&atom, type);
356 do_parse_data(int argc, char *argv[])
358 struct ovsdb_type type;
362 json = unbox_json(parse_json(argv[1]));
363 check_ovsdb_error(ovsdb_type_from_json(&type, json));
366 for (i = 2; i < argc; i++) {
367 struct ovsdb_datum datum;
369 json = unbox_json(parse_json(argv[i]));
370 check_ovsdb_error(ovsdb_datum_from_json(&datum, &type, json, NULL));
373 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
375 ovsdb_datum_destroy(&datum, &type);
379 static enum ovsdb_atomic_type compare_atoms_atomic_type;
382 compare_atoms(const void *a_, const void *b_)
384 const union ovsdb_atom *a = a_;
385 const union ovsdb_atom *b = b_;
387 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
391 do_sort_atoms(int argc UNUSED, char *argv[])
393 enum ovsdb_atomic_type type;
394 union ovsdb_atom *atoms;
395 struct json *json, **json_atoms;
399 json = unbox_json(parse_json(argv[1]));
400 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
403 json = unbox_json(parse_json(argv[2]));
404 if (json->type != JSON_ARRAY) {
405 ovs_fatal(0, "second argument must be array");
408 /* Convert JSON atoms to internal representation. */
409 n_atoms = json->u.array.n;
410 atoms = xmalloc(n_atoms * sizeof *atoms);
411 for (i = 0; i < n_atoms; i++) {
412 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], type,
413 json->u.array.elems[i], NULL));
418 compare_atoms_atomic_type = type;
419 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
421 /* Convert internal representation back to JSON. */
422 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
423 for (i = 0; i < n_atoms; i++) {
424 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], type);
425 ovsdb_atom_destroy(&atoms[i], type);
427 print_and_free_json(json_array_create(json_atoms, n_atoms));
431 do_parse_column(int argc UNUSED, char *argv[])
433 struct ovsdb_column *column;
436 json = parse_json(argv[2]);
437 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
439 print_and_free_json(ovsdb_column_to_json(column));
440 ovsdb_column_destroy(column);
444 do_parse_table(int argc UNUSED, char *argv[])
446 struct ovsdb_table_schema *ts;
449 json = parse_json(argv[2]);
450 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
452 print_and_free_json(ovsdb_table_schema_to_json(ts));
453 ovsdb_table_schema_destroy(ts);
457 do_parse_rows(int argc, char *argv[])
459 struct ovsdb_column_set all_columns;
460 struct ovsdb_table_schema *ts;
461 struct ovsdb_table *table;
465 json = unbox_json(parse_json(argv[1]));
466 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
469 table = ovsdb_table_create(ts);
470 ovsdb_column_set_init(&all_columns);
471 ovsdb_column_set_add_all(&all_columns, table);
473 for (i = 2; i < argc; i++) {
474 struct ovsdb_column_set columns;
475 struct ovsdb_row *row;
477 ovsdb_column_set_init(&columns);
478 row = ovsdb_row_create(table);
480 json = unbox_json(parse_json(argv[i]));
481 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
484 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
486 if (columns.n_columns) {
492 for (j = 0; j < columns.n_columns; j++) {
493 svec_add(&names, columns.columns[j]->name);
496 s = svec_join(&names, ", ", "");
499 svec_destroy(&names);
504 ovsdb_column_set_destroy(&columns);
505 ovsdb_row_destroy(row);
508 ovsdb_column_set_destroy(&all_columns);
509 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
513 do_compare_rows(int argc, char *argv[])
515 struct ovsdb_column_set all_columns;
516 struct ovsdb_table_schema *ts;
517 struct ovsdb_table *table;
518 struct ovsdb_row **rows;
524 json = unbox_json(parse_json(argv[1]));
525 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
528 table = ovsdb_table_create(ts);
529 ovsdb_column_set_init(&all_columns);
530 ovsdb_column_set_add_all(&all_columns, table);
533 rows = xmalloc(sizeof *rows * n_rows);
534 names = xmalloc(sizeof *names * n_rows);
535 for (i = 0; i < n_rows; i++) {
536 rows[i] = ovsdb_row_create(table);
538 json = parse_json(argv[i + 2]);
539 if (json->type != JSON_ARRAY || json->u.array.n != 2
540 || json->u.array.elems[0]->type != JSON_STRING) {
541 ovs_fatal(0, "\"%s\" does not have expected form "
542 "[\"name\", {data}]", argv[i]);
544 names[i] = xstrdup(json->u.array.elems[0]->u.string);
545 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
549 for (i = 0; i < n_rows; i++) {
550 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
551 for (j = i + 1; j < n_rows; j++) {
552 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
553 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
554 printf("%s == %s\n", names[i], names[j]);
555 if (i_hash != j_hash) {
556 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
559 } else if (i_hash == j_hash) {
560 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
567 ovsdb_column_set_destroy(&all_columns);
568 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
572 do_parse_conditions(int argc, char *argv[])
574 struct ovsdb_table_schema *ts;
579 json = unbox_json(parse_json(argv[1]));
580 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
583 for (i = 2; i < argc; i++) {
584 struct ovsdb_condition cnd;
585 struct ovsdb_error *error;
587 json = parse_json(argv[i]);
588 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
590 print_and_free_json(ovsdb_condition_to_json(&cnd));
592 char *s = ovsdb_error_to_string(error);
593 ovs_error(0, "%s", s);
595 ovsdb_error_destroy(error);
600 ovsdb_condition_destroy(&cnd);
602 ovsdb_table_schema_destroy(ts);
608 do_evaluate_conditions(int argc UNUSED, char *argv[])
610 struct ovsdb_table_schema *ts;
611 struct ovsdb_table *table;
612 struct ovsdb_condition *conditions;
614 struct ovsdb_row **rows;
619 /* Parse table schema, create table. */
620 json = unbox_json(parse_json(argv[1]));
621 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
624 table = ovsdb_table_create(ts);
626 /* Parse conditions. */
627 json = parse_json(argv[2]);
628 if (json->type != JSON_ARRAY) {
629 ovs_fatal(0, "CONDITION argument is not JSON array");
631 n_conditions = json->u.array.n;
632 conditions = xmalloc(n_conditions * sizeof *conditions);
633 for (i = 0; i < n_conditions; i++) {
634 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
635 NULL, &conditions[i]));
640 json = parse_json(argv[3]);
641 if (json->type != JSON_ARRAY) {
642 ovs_fatal(0, "ROW argument is not JSON array");
644 n_rows = json->u.array.n;
645 rows = xmalloc(n_rows * sizeof *rows);
646 for (i = 0; i < n_rows; i++) {
647 rows[i] = ovsdb_row_create(table);
648 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
653 for (i = 0; i < n_conditions; i++) {
654 printf("condition %2d:", i);
655 for (j = 0; j < n_rows; j++) {
656 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
660 putchar(result ? 'T' : '-');
665 for (i = 0; i < n_conditions; i++) {
666 ovsdb_condition_destroy(&conditions[i]);
668 for (i = 0; i < n_rows; i++) {
669 ovsdb_row_destroy(rows[i]);
671 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
675 do_parse_mutations(int argc, char *argv[])
677 struct ovsdb_table_schema *ts;
682 json = unbox_json(parse_json(argv[1]));
683 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
686 for (i = 2; i < argc; i++) {
687 struct ovsdb_mutation_set set;
688 struct ovsdb_error *error;
690 json = parse_json(argv[i]);
691 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
693 print_and_free_json(ovsdb_mutation_set_to_json(&set));
695 char *s = ovsdb_error_to_string(error);
696 ovs_error(0, "%s", s);
698 ovsdb_error_destroy(error);
703 ovsdb_mutation_set_destroy(&set);
705 ovsdb_table_schema_destroy(ts);
711 do_execute_mutations(int argc UNUSED, char *argv[])
713 struct ovsdb_table_schema *ts;
714 struct ovsdb_table *table;
715 struct ovsdb_mutation_set *sets;
717 struct ovsdb_row **rows;
722 /* Parse table schema, create table. */
723 json = unbox_json(parse_json(argv[1]));
724 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
727 table = ovsdb_table_create(ts);
729 /* Parse mutations. */
730 json = parse_json(argv[2]);
731 if (json->type != JSON_ARRAY) {
732 ovs_fatal(0, "MUTATION argument is not JSON array");
734 n_sets = json->u.array.n;
735 sets = xmalloc(n_sets * sizeof *sets);
736 for (i = 0; i < n_sets; i++) {
737 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
738 json->u.array.elems[i],
744 json = parse_json(argv[3]);
745 if (json->type != JSON_ARRAY) {
746 ovs_fatal(0, "ROW argument is not JSON array");
748 n_rows = json->u.array.n;
749 rows = xmalloc(n_rows * sizeof *rows);
750 for (i = 0; i < n_rows; i++) {
751 rows[i] = ovsdb_row_create(table);
752 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
757 for (i = 0; i < n_sets; i++) {
758 printf("mutation %2d:\n", i);
759 for (j = 0; j < n_rows; j++) {
760 struct ovsdb_error *error;
761 struct ovsdb_row *row;
763 row = ovsdb_row_clone(rows[j]);
764 error = ovsdb_mutation_set_execute(row, &sets[i]);
766 printf("row %zu: ", j);
768 print_and_free_ovsdb_error(error);
770 struct ovsdb_column_set columns;
771 struct shash_node *node;
773 ovsdb_column_set_init(&columns);
774 SHASH_FOR_EACH (node, &ts->columns) {
775 struct ovsdb_column *c = node->data;
776 if (!ovsdb_datum_equals(&row->fields[c->index],
777 &rows[j]->fields[c->index],
779 ovsdb_column_set_add(&columns, c);
782 if (columns.n_columns) {
783 print_and_free_json(ovsdb_row_to_json(row, &columns));
785 printf("no change\n");
787 ovsdb_column_set_destroy(&columns);
789 ovsdb_row_destroy(row);
794 for (i = 0; i < n_sets; i++) {
795 ovsdb_mutation_set_destroy(&sets[i]);
797 for (i = 0; i < n_rows; i++) {
798 ovsdb_row_destroy(rows[i]);
800 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
803 struct do_query_cbdata {
804 struct uuid *row_uuids;
810 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
812 struct do_query_cbdata *cbdata = cbdata_;
815 for (i = 0; i < cbdata->n_rows; i++) {
816 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
825 do_query(int argc UNUSED, char *argv[])
827 struct do_query_cbdata cbdata;
828 struct ovsdb_table_schema *ts;
829 struct ovsdb_table *table;
834 /* Parse table schema, create table. */
835 json = unbox_json(parse_json(argv[1]));
836 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
839 table = ovsdb_table_create(ts);
841 /* Parse rows, add to table. */
842 json = parse_json(argv[2]);
843 if (json->type != JSON_ARRAY) {
844 ovs_fatal(0, "ROW argument is not JSON array");
846 cbdata.n_rows = json->u.array.n;
847 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
848 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
849 for (i = 0; i < cbdata.n_rows; i++) {
850 struct ovsdb_row *row = ovsdb_row_create(table);
851 uuid_generate(ovsdb_row_get_uuid_rw(row));
852 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
854 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
855 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
856 UUID_ARGS(ovsdb_row_get_uuid(row)));
858 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
859 ovsdb_table_put_row(table, row);
863 /* Parse conditions and execute queries. */
864 json = parse_json(argv[3]);
865 if (json->type != JSON_ARRAY) {
866 ovs_fatal(0, "CONDITION argument is not JSON array");
868 for (i = 0; i < json->u.array.n; i++) {
869 struct ovsdb_condition cnd;
872 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
875 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
876 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
878 printf("query %2d:", i);
879 for (j = 0; j < cbdata.n_rows; j++) {
883 if (cbdata.counts[j]) {
884 printf("%d", cbdata.counts[j]);
885 if (cbdata.counts[j] > 1) {
895 ovsdb_condition_destroy(&cnd);
899 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
904 struct do_query_distinct_class {
905 struct ovsdb_row *example;
909 struct do_query_distinct_row {
911 struct do_query_distinct_class *class;
915 do_query_distinct(int argc UNUSED, char *argv[])
917 struct ovsdb_column_set columns;
918 struct ovsdb_table_schema *ts;
919 struct ovsdb_table *table;
920 struct do_query_distinct_row *rows;
922 struct do_query_distinct_class *classes;
928 /* Parse table schema, create table. */
929 json = unbox_json(parse_json(argv[1]));
930 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
933 table = ovsdb_table_create(ts);
935 /* Parse column set. */
936 json = parse_json(argv[4]);
937 check_ovsdb_error(ovsdb_column_set_from_json(json, table, &columns));
940 /* Parse rows, add to table. */
941 json = parse_json(argv[2]);
942 if (json->type != JSON_ARRAY) {
943 ovs_fatal(0, "ROW argument is not JSON array");
945 n_rows = json->u.array.n;
946 rows = xmalloc(n_rows * sizeof *rows);
947 classes = xmalloc(n_rows * sizeof *classes);
949 for (i = 0; i < n_rows; i++) {
950 struct ovsdb_row *row;
954 row = ovsdb_row_create(table);
955 uuid_generate(ovsdb_row_get_uuid_rw(row));
956 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
959 /* Initialize row and find equivalence class. */
960 rows[i].uuid = *ovsdb_row_get_uuid(row);
961 rows[i].class = NULL;
962 for (j = 0; j < n_classes; j++) {
963 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
964 rows[i].class = &classes[j];
968 if (!rows[i].class) {
969 rows[i].class = &classes[n_classes];
970 classes[n_classes].example = ovsdb_row_clone(row);
974 /* Add row to table. */
975 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
976 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
977 UUID_ARGS(ovsdb_row_get_uuid(row)));
979 ovsdb_table_put_row(table, row);
984 /* Parse conditions and execute queries. */
985 json = parse_json(argv[3]);
986 if (json->type != JSON_ARRAY) {
987 ovs_fatal(0, "CONDITION argument is not JSON array");
989 for (i = 0; i < json->u.array.n; i++) {
990 struct ovsdb_row_set results;
991 struct ovsdb_condition cnd;
993 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
996 for (j = 0; j < n_classes; j++) {
997 classes[j].count = 0;
999 ovsdb_row_set_init(&results);
1000 ovsdb_query_distinct(table, &cnd, &columns, &results);
1001 for (j = 0; j < results.n_rows; j++) {
1002 for (k = 0; k < n_rows; k++) {
1003 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1005 rows[k].class->count++;
1009 ovsdb_row_set_destroy(&results);
1011 printf("query %2d:", i);
1012 for (j = 0; j < n_rows; j++) {
1013 int count = rows[j].class->count;
1020 printf("%d", count);
1022 } else if (count == 1) {
1023 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1030 ovsdb_condition_destroy(&cnd);
1034 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1040 do_execute(int argc UNUSED, char *argv[])
1042 struct ovsdb_schema *schema;
1047 /* Create database. */
1048 json = parse_json(argv[1]);
1049 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1051 db = ovsdb_create(schema);
1053 for (i = 2; i < argc; i++) {
1054 struct json *params, *result;
1057 params = parse_json(argv[i]);
1058 result = ovsdb_execute(db, params, 0, NULL);
1059 s = json_to_string(result, JSSF_SORT);
1061 json_destroy(params);
1062 json_destroy(result);
1068 struct test_trigger {
1069 struct ovsdb_trigger trigger;
1074 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1076 struct json *result;
1079 result = ovsdb_trigger_steal_result(&t->trigger);
1080 s = json_to_string(result, JSSF_SORT);
1081 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1082 json_destroy(result);
1083 ovsdb_trigger_destroy(&t->trigger);
1088 do_trigger(int argc UNUSED, char *argv[])
1090 struct ovsdb_schema *schema;
1091 struct list completions;
1098 /* Create database. */
1099 json = parse_json(argv[1]);
1100 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1102 db = ovsdb_create(schema);
1104 list_init(&completions);
1107 for (i = 2; i < argc; i++) {
1108 struct json *params = parse_json(argv[i]);
1109 if (params->type == JSON_ARRAY
1110 && json_array(params)->n == 2
1111 && json_array(params)->elems[0]->type == JSON_STRING
1112 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1113 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1114 now += json_integer(json_array(params)->elems[1]);
1115 json_destroy(params);
1117 struct test_trigger *t = xmalloc(sizeof *t);
1118 ovsdb_trigger_init(db, &t->trigger, params, &completions, now);
1119 t->number = number++;
1120 if (ovsdb_trigger_is_complete(&t->trigger)) {
1121 do_trigger_dump(t, now, "immediate");
1123 printf("t=%lld: new trigger %d\n", now, t->number);
1127 ovsdb_trigger_run(db, now);
1128 while (!list_is_empty(&completions)) {
1129 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions),
1130 struct test_trigger, trigger.node),
1134 ovsdb_trigger_wait(db, now);
1135 poll_immediate_wake();
1143 do_help(int argc UNUSED, char *argv[] UNUSED)
1148 /* "transact" command. */
1150 static struct ovsdb *do_transact_db;
1151 static struct ovsdb_txn *do_transact_txn;
1152 static struct ovsdb_table *do_transact_table;
1155 do_transact_commit(int argc UNUSED, char *argv[] UNUSED)
1157 ovsdb_txn_commit(do_transact_txn, false);
1158 do_transact_txn = NULL;
1162 do_transact_abort(int argc UNUSED, char *argv[] UNUSED)
1164 ovsdb_txn_abort(do_transact_txn);
1165 do_transact_txn = NULL;
1169 uuid_from_integer(int integer, struct uuid *uuid)
1172 uuid->parts[3] = integer;
1175 static const struct ovsdb_row *
1176 do_transact_find_row(const char *uuid_string)
1178 const struct ovsdb_row *row;
1181 uuid_from_integer(atoi(uuid_string), &uuid);
1182 row = ovsdb_table_get_row(do_transact_table, &uuid);
1184 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1191 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1194 if (integer != -1) {
1195 const struct ovsdb_column *column;
1197 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1199 row->fields[column->index].keys[0].integer = integer;
1204 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1206 const struct ovsdb_column *column;
1208 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1210 return row->fields[column->index].keys[0].integer;
1214 do_transact_set_i_j(struct ovsdb_row *row,
1215 const char *i_string, const char *j_string)
1217 do_transact_set_integer(row, "i", atoi(i_string));
1218 do_transact_set_integer(row, "j", atoi(j_string));
1222 do_transact_insert(int argc UNUSED, char *argv[] UNUSED)
1224 struct ovsdb_row *row;
1227 row = ovsdb_row_create(do_transact_table);
1230 uuid = ovsdb_row_get_uuid_rw(row);
1231 uuid_from_integer(atoi(argv[1]), uuid);
1232 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1233 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1237 do_transact_set_i_j(row, argv[2], argv[3]);
1240 ovsdb_txn_row_insert(do_transact_txn, row);
1244 do_transact_delete(int argc UNUSED, char *argv[] UNUSED)
1246 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1247 ovsdb_txn_row_delete(do_transact_txn, row);
1251 do_transact_modify(int argc UNUSED, char *argv[] UNUSED)
1253 const struct ovsdb_row *row_ro;
1254 struct ovsdb_row *row_rw;
1256 row_ro = do_transact_find_row(argv[1]);
1257 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1258 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1262 compare_rows_by_uuid(const void *a_, const void *b_)
1264 struct ovsdb_row *const *ap = a_;
1265 struct ovsdb_row *const *bp = b_;
1267 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1271 do_transact_print(int argc UNUSED, char *argv[] UNUSED)
1273 const struct ovsdb_row **rows;
1274 const struct ovsdb_row *row;
1278 n_rows = hmap_count(&do_transact_table->rows);
1279 rows = xmalloc(n_rows * sizeof *rows);
1281 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1282 &do_transact_table->rows) {
1285 assert(i == n_rows);
1287 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1289 for (i = 0; i < n_rows; i++) {
1290 printf("\n%"PRId32": i=%d, j=%d",
1291 ovsdb_row_get_uuid(rows[i])->parts[3],
1292 do_transact_get_integer(rows[i], "i"),
1293 do_transact_get_integer(rows[i], "j"));
1300 do_transact(int argc, char *argv[])
1302 static const struct command do_transact_commands[] = {
1303 { "commit", 0, 0, do_transact_commit },
1304 { "abort", 0, 0, do_transact_abort },
1305 { "insert", 2, 3, do_transact_insert },
1306 { "delete", 1, 1, do_transact_delete },
1307 { "modify", 2, 3, do_transact_modify },
1308 { "print", 0, 0, do_transact_print },
1309 { NULL, 0, 0, NULL },
1312 struct ovsdb_schema *schema;
1317 json = parse_json("{\"name\": \"testdb\", "
1321 " {\"i\": {\"type\": \"integer\"}, "
1322 " \"j\": {\"type\": \"integer\"}}}}}");
1323 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1325 do_transact_db = ovsdb_create(schema);
1326 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1327 assert(do_transact_table != NULL);
1329 for (i = 1; i < argc; i++) {
1330 struct json *command;
1335 command = parse_json(argv[i]);
1336 if (command->type != JSON_ARRAY) {
1337 ovs_fatal(0, "transaction %d must be JSON array "
1338 "with at least 1 element", i);
1341 n_args = command->u.array.n;
1342 args = xmalloc((n_args + 1) * sizeof *args);
1343 for (j = 0; j < n_args; j++) {
1344 struct json *s = command->u.array.elems[j];
1345 if (s->type != JSON_STRING) {
1346 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1349 args[j] = xstrdup(json_string(s));
1351 args[n_args] = NULL;
1353 if (!do_transact_txn) {
1354 do_transact_txn = ovsdb_txn_create(do_transact_db);
1357 for (j = 0; j < n_args; j++) {
1361 fputs(args[j], stdout);
1364 run_command(n_args, args, do_transact_commands);
1367 for (j = 0; j < n_args; j++) {
1371 json_destroy(command);
1373 ovsdb_txn_abort(do_transact_txn);
1374 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1378 compare_link1(const void *a_, const void *b_)
1380 const struct idltest_link1 *const *ap = a_;
1381 const struct idltest_link1 *const *bp = b_;
1382 const struct idltest_link1 *a = *ap;
1383 const struct idltest_link1 *b = *bp;
1385 return a->i < b->i ? -1 : a->i > b->i;
1389 print_idl(struct ovsdb_idl *idl, int step)
1391 const struct idltest_simple *s;
1392 const struct idltest_link1 *l1;
1393 const struct idltest_link2 *l2;
1396 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1399 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1400 step, s->i, s->r, s->b ? "true" : "false",
1401 s->s, UUID_ARGS(&s->u));
1402 for (i = 0; i < s->n_ia; i++) {
1403 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1406 for (i = 0; i < s->n_ra; i++) {
1407 printf("%s%g", i ? " " : "", s->ra[i]);
1410 for (i = 0; i < s->n_ba; i++) {
1411 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1414 for (i = 0; i < s->n_sa; i++) {
1415 printf("%s%s", i ? " " : "", s->sa[i]);
1418 for (i = 0; i < s->n_ua; i++) {
1419 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1421 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1424 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1425 struct idltest_link1 **links;
1428 printf("%03d: i=%"PRId64" k=", step, l1->i);
1430 printf("%"PRId64, l1->k->i);
1433 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1434 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1435 for (i = 0; i < l1->n_ka; i++) {
1436 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1441 printf("%"PRId64, l1->l2->i);
1443 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1446 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1447 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1449 printf("%"PRId64, l2->l1->i);
1451 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1455 printf("%03d: empty\n", step);
1460 print_updated_idl(struct ovsdb_idl *idl, struct jsonrpc *rpc,
1461 int step, unsigned int seqno)
1464 unsigned int new_seqno;
1470 new_seqno = ovsdb_idl_get_seqno(idl);
1471 if (new_seqno != seqno) {
1472 print_idl(idl, step);
1479 ovsdb_idl_wait(idl);
1485 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1490 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1491 char *name = xasprintf("#%d#", *n);
1492 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1493 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1496 } else if (json->type == JSON_ARRAY) {
1499 for (i = 0; i < json->u.array.n; i++) {
1500 parse_uuids(json->u.array.elems[i], symtab, n);
1502 } else if (json->type == JSON_OBJECT) {
1503 const struct shash_node *node;
1505 SHASH_FOR_EACH (node, json_object(json)) {
1506 parse_uuids(node->data, symtab, n);
1512 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1514 if (json->type == JSON_STRING) {
1515 const struct ovsdb_symbol *symbol;
1517 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1519 free(json->u.string);
1520 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1522 } else if (json->type == JSON_ARRAY) {
1525 for (i = 0; i < json->u.array.n; i++) {
1526 substitute_uuids(json->u.array.elems[i], symtab);
1528 } else if (json->type == JSON_OBJECT) {
1529 const struct shash_node *node;
1531 SHASH_FOR_EACH (node, json_object(json)) {
1532 substitute_uuids(node->data, symtab);
1537 static const struct idltest_simple *
1538 idltest_find_simple(struct ovsdb_idl *idl, int i)
1540 const struct idltest_simple *s;
1542 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1551 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1553 char *cmd, *save_ptr1 = NULL;
1554 struct ovsdb_idl_txn *txn;
1555 enum ovsdb_idl_txn_status status;
1557 txn = ovsdb_idl_txn_create(idl);
1558 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1559 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1560 char *save_ptr2 = NULL;
1561 char *name, *arg1, *arg2, *arg3;
1563 name = strtok_r(cmd, " ", &save_ptr2);
1564 arg1 = strtok_r(NULL, " ", &save_ptr2);
1565 arg2 = strtok_r(NULL, " ", &save_ptr2);
1566 arg3 = strtok_r(NULL, " ", &save_ptr2);
1568 if (!strcmp(name, "set")) {
1569 const struct idltest_simple *s;
1572 ovs_fatal(0, "\"set\" command requires 3 arguments");
1575 s = idltest_find_simple(idl, atoi(arg1));
1577 ovs_fatal(0, "\"set\" command asks for nonexistent "
1578 "i=%d", atoi(arg1));
1581 if (!strcmp(arg2, "b")) {
1582 idltest_simple_set_b(s, atoi(arg3));
1583 } else if (!strcmp(arg2, "s")) {
1584 idltest_simple_set_s(s, arg3);
1585 } else if (!strcmp(arg2, "u")) {
1587 uuid_from_string(&uuid, arg3);
1588 idltest_simple_set_u(s, uuid);
1589 } else if (!strcmp(arg2, "r")) {
1590 idltest_simple_set_r(s, atof(arg3));
1592 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1595 } else if (!strcmp(name, "insert")) {
1596 struct idltest_simple *s;
1598 if (!arg1 || arg2) {
1599 ovs_fatal(0, "\"set\" command requires 1 argument");
1602 s = idltest_simple_insert(txn);
1603 idltest_simple_set_i(s, atoi(arg1));
1604 } else if (!strcmp(name, "delete")) {
1605 const struct idltest_simple *s;
1607 if (!arg1 || arg2) {
1608 ovs_fatal(0, "\"set\" command requires 1 argument");
1611 s = idltest_find_simple(idl, atoi(arg1));
1613 ovs_fatal(0, "\"set\" command asks for nonexistent "
1614 "i=%d", atoi(arg1));
1616 idltest_simple_delete(s);
1618 ovs_fatal(0, "unknown command %s", name);
1622 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1624 ovsdb_idl_wait(idl);
1625 ovsdb_idl_txn_wait(txn);
1628 printf("%03d: commit, status=%s\n",
1629 step, ovsdb_idl_txn_status_to_string(status));
1630 ovsdb_idl_txn_destroy(txn);
1634 do_idl(int argc, char *argv[])
1636 struct jsonrpc *rpc;
1637 struct ovsdb_idl *idl;
1638 unsigned int seqno = 0;
1639 struct ovsdb_symbol_table *symtab;
1645 idl = ovsdb_idl_create(argv[1], &idltest_idl_class);
1647 struct stream *stream;
1649 error = stream_open_block(argv[1], &stream);
1651 ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
1653 rpc = jsonrpc_open(stream);
1658 symtab = ovsdb_symbol_table_create();
1659 for (i = 2; i < argc; i++) {
1660 struct jsonrpc_msg *request, *reply;
1663 seqno = print_updated_idl(idl, rpc, step++, seqno);
1665 if (!strcmp(argv[i], "reconnect")) {
1666 printf("%03d: reconnect\n", step++);
1667 ovsdb_idl_force_reconnect(idl);
1668 } else if (argv[i][0] != '[') {
1669 idl_set(idl, argv[i], step++);
1671 struct json *json = parse_json(argv[i]);
1672 substitute_uuids(json, symtab);
1673 request = jsonrpc_create_request("transact", json, NULL);
1674 error = jsonrpc_transact_block(rpc, request, &reply);
1676 ovs_fatal(error, "jsonrpc transaction failed");
1678 printf("%03d: ", step++);
1679 if (reply->result) {
1680 parse_uuids(reply->result, symtab, &n_uuids);
1682 json_destroy(reply->id);
1684 print_and_free_json(jsonrpc_msg_to_json(reply));
1687 ovsdb_symbol_table_destroy(symtab);
1692 print_updated_idl(idl, NULL, step++, seqno);
1693 ovsdb_idl_destroy(idl);
1694 printf("%03d: done\n", step);
1697 static struct command all_commands[] = {
1698 { "log-io", 2, INT_MAX, do_log_io },
1699 { "parse-atomic-type", 1, 1, do_parse_atomic_type },
1700 { "parse-type", 1, 1, do_parse_type },
1701 { "parse-atoms", 2, INT_MAX, do_parse_atoms },
1702 { "parse-data", 2, INT_MAX, do_parse_data },
1703 { "sort-atoms", 2, 2, do_sort_atoms },
1704 { "parse-column", 2, 2, do_parse_column },
1705 { "parse-table", 2, 2, do_parse_table },
1706 { "parse-rows", 2, INT_MAX, do_parse_rows },
1707 { "compare-rows", 2, INT_MAX, do_compare_rows },
1708 { "parse-conditions", 2, INT_MAX, do_parse_conditions },
1709 { "evaluate-conditions", 3, 3, do_evaluate_conditions },
1710 { "parse-mutations", 2, INT_MAX, do_parse_mutations },
1711 { "execute-mutations", 3, 3, do_execute_mutations },
1712 { "query", 3, 3, do_query },
1713 { "query-distinct", 4, 4, do_query_distinct },
1714 { "transact", 1, INT_MAX, do_transact },
1715 { "execute", 2, INT_MAX, do_execute },
1716 { "trigger", 2, INT_MAX, do_trigger },
1717 { "idl", 1, INT_MAX, do_idl },
1718 { "help", 0, INT_MAX, do_help },
1719 { NULL, 0, 0, NULL },