2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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/server.h"
43 #include "ovsdb/table.h"
44 #include "ovsdb/transaction.h"
45 #include "ovsdb/trigger.h"
46 #include "poll-loop.h"
49 #include "tests/idltest.h"
52 #include "openvswitch/vlog.h"
54 struct test_ovsdb_pvt_context {
58 OVS_NO_RETURN static void usage(void);
59 static void parse_options(int argc, char *argv[],
60 struct test_ovsdb_pvt_context *pvt);
61 static struct ovs_cmdl_command *get_all_commands(void);
64 main(int argc, char *argv[])
66 struct test_ovsdb_pvt_context pvt = {.track = false};
67 struct ovs_cmdl_context ctx = { .argc = 0, .pvt = &pvt};
68 set_program_name(argv[0]);
69 parse_options(argc, argv, &pvt);
70 ctx.argc = argc - optind;
71 ctx.argv = argv + optind;
72 ovs_cmdl_run_command(&ctx, get_all_commands());
77 parse_options(int argc, char *argv[], struct test_ovsdb_pvt_context *pvt)
79 static const struct option long_options[] = {
80 {"timeout", required_argument, NULL, 't'},
81 {"verbose", optional_argument, NULL, 'v'},
82 {"change-track", optional_argument, NULL, 'c'},
83 {"help", no_argument, NULL, 'h'},
86 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
89 unsigned long int timeout;
92 c = getopt_long(argc, argv, short_options, long_options, NULL);
99 timeout = strtoul(optarg, NULL, 10);
101 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
112 vlog_set_verbosity(optarg);
132 printf("%s: Open vSwitch database test utility\n"
133 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
134 " log-io FILE FLAGS COMMAND...\n"
135 " open FILE with FLAGS, run COMMANDs\n"
137 " test ovsdb_atom_default()\n"
139 " test ovsdb_datum_default()\n"
140 " parse-atomic-type TYPE\n"
141 " parse TYPE as OVSDB atomic type, and re-serialize\n"
142 " parse-base-type TYPE\n"
143 " parse TYPE as OVSDB base type, and re-serialize\n"
145 " parse JSON as OVSDB type, and re-serialize\n"
146 " parse-atoms TYPE ATOM...\n"
147 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
148 " parse-atom-strings TYPE ATOM...\n"
149 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
150 " sort-atoms TYPE ATOM...\n"
151 " print JSON ATOMs in sorted order\n"
152 " parse-data TYPE DATUM...\n"
153 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
154 " parse-data-strings TYPE DATUM...\n"
155 " parse string DATUMs as data of given TYPE, and re-serialize\n"
156 " parse-column NAME OBJECT\n"
157 " parse column NAME with info OBJECT, and re-serialize\n"
158 " parse-table NAME OBJECT [DEFAULT-IS-ROOT]\n"
159 " parse table NAME with info OBJECT\n"
160 " parse-row TABLE ROW..., and re-serialize\n"
161 " parse each ROW of defined TABLE\n"
162 " compare-row TABLE ROW...\n"
163 " mutually compare all of the ROWs, print those that are equal\n"
164 " parse-conditions TABLE CONDITION...\n"
165 " parse each CONDITION on TABLE, and re-serialize\n"
166 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
167 " test CONDITIONS on TABLE against each ROW, print results\n"
168 " parse-mutations TABLE MUTATION...\n"
169 " parse each MUTATION on TABLE, and re-serialize\n"
170 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
171 " execute MUTATIONS on TABLE on each ROW, print results\n"
172 " query TABLE [ROW,...] [CONDITION,...]\n"
173 " add each ROW to TABLE, then query and print the rows that\n"
174 " satisfy each CONDITION.\n"
175 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
176 " add each ROW to TABLE, then query and print the rows that\n"
177 " satisfy each CONDITION and have distinct COLUMNS.\n"
178 " parse-schema JSON\n"
179 " parse JSON as an OVSDB schema, and re-serialize\n"
180 " transact COMMAND\n"
181 " execute each specified transactional COMMAND:\n"
188 " execute SCHEMA TRANSACTION...\n"
189 " executes each TRANSACTION on an initially empty database\n"
190 " the specified SCHEMA\n"
191 " trigger SCHEMA TRANSACTION...\n"
192 " executes each TRANSACTION on an initially empty database\n"
193 " the specified SCHEMA. A TRANSACTION of the form\n"
194 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
195 " simulated time, for causing triggers to time out.\n"
196 " idl SERVER [TRANSACTION...]\n"
197 " connect to SERVER and dump the contents of the database\n"
198 " as seen initially by the IDL implementation and after\n"
199 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
200 " the database or this command will hang.)\n",
201 program_name, program_name);
203 printf("\nOther options:\n"
204 " -t, --timeout=SECS give up after SECS seconds\n"
205 " -h, --help display this help message\n"
206 " -c, --change-track used with the 'idl' command to\n"
207 " enable tracking of IDL changes\n");
211 /* Command helper functions. */
214 parse_json(const char *s)
216 struct json *json = json_from_string(s);
217 if (json->type == JSON_STRING) {
218 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
224 unbox_json(struct json *json)
226 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
227 struct json *inner = json->u.array.elems[0];
228 json->u.array.elems[0] = NULL;
237 print_and_free_json(struct json *json)
239 char *string = json_to_string(json, JSSF_SORT);
246 print_and_free_ovsdb_error(struct ovsdb_error *error)
248 char *string = ovsdb_error_to_string(error);
249 ovsdb_error_destroy(error);
255 check_ovsdb_error(struct ovsdb_error *error)
258 char *s = ovsdb_error_to_string(error);
259 ovsdb_error_destroy(error);
260 ovs_fatal(0, "%s", s);
265 die_if_error(char *error)
268 ovs_fatal(0, "%s", error);
272 /* Command implementations. */
275 do_log_io(struct ovs_cmdl_context *ctx)
277 const char *name = ctx->argv[1];
278 char *mode_string = ctx->argv[2];
280 struct ovsdb_error *error;
281 enum ovsdb_log_open_mode mode;
282 struct ovsdb_log *log;
285 if (!strcmp(mode_string, "read-only")) {
286 mode = OVSDB_LOG_READ_ONLY;
287 } else if (!strcmp(mode_string, "read/write")) {
288 mode = OVSDB_LOG_READ_WRITE;
289 } else if (!strcmp(mode_string, "create")) {
290 mode = OVSDB_LOG_CREATE;
292 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
295 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
296 printf("%s: open successful\n", name);
298 for (i = 3; i < ctx->argc; i++) {
299 const char *command = ctx->argv[i];
300 if (!strcmp(command, "read")) {
303 error = ovsdb_log_read(log, &json);
305 printf("%s: read: ", name);
307 print_and_free_json(json);
309 printf("end of log\n");
313 } else if (!strncmp(command, "write:", 6)) {
314 struct json *json = parse_json(command + 6);
315 error = ovsdb_log_write(log, json);
317 } else if (!strcmp(command, "commit")) {
318 error = ovsdb_log_commit(log);
320 ovs_fatal(0, "unknown log-io command \"%s\"", command);
323 char *s = ovsdb_error_to_string(error);
324 printf("%s: %s failed: %s\n", name, command, s);
326 ovsdb_error_destroy(error);
328 printf("%s: %s successful\n", name, command);
332 ovsdb_log_close(log);
336 do_default_atoms(struct ovs_cmdl_context *ctx OVS_UNUSED)
340 for (type = 0; type < OVSDB_N_TYPES; type++) {
341 union ovsdb_atom atom;
343 if (type == OVSDB_TYPE_VOID) {
347 printf("%s: ", ovsdb_atomic_type_to_string(type));
349 ovsdb_atom_init_default(&atom, type);
350 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
354 ovsdb_atom_destroy(&atom, type);
361 do_default_data(struct ovs_cmdl_context *ctx OVS_UNUSED)
366 for (n_min = 0; n_min <= 1; n_min++) {
367 for (key = 0; key < OVSDB_N_TYPES; key++) {
368 if (key == OVSDB_TYPE_VOID) {
371 for (value = 0; value < OVSDB_N_TYPES; value++) {
372 struct ovsdb_datum datum;
373 struct ovsdb_type type;
375 ovsdb_base_type_init(&type.key, key);
376 ovsdb_base_type_init(&type.value, value);
379 assert(ovsdb_type_is_valid(&type));
381 printf("key %s, value %s, n_min %u: ",
382 ovsdb_atomic_type_to_string(key),
383 ovsdb_atomic_type_to_string(value), n_min);
385 ovsdb_datum_init_default(&datum, &type);
386 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
391 ovsdb_datum_destroy(&datum, &type);
392 ovsdb_type_destroy(&type);
401 do_parse_atomic_type(struct ovs_cmdl_context *ctx)
403 enum ovsdb_atomic_type type;
406 json = unbox_json(parse_json(ctx->argv[1]));
407 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
409 print_and_free_json(ovsdb_atomic_type_to_json(type));
413 do_parse_base_type(struct ovs_cmdl_context *ctx)
415 struct ovsdb_base_type base;
418 json = unbox_json(parse_json(ctx->argv[1]));
419 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
421 print_and_free_json(ovsdb_base_type_to_json(&base));
422 ovsdb_base_type_destroy(&base);
426 do_parse_type(struct ovs_cmdl_context *ctx)
428 struct ovsdb_type type;
431 json = unbox_json(parse_json(ctx->argv[1]));
432 check_ovsdb_error(ovsdb_type_from_json(&type, json));
434 print_and_free_json(ovsdb_type_to_json(&type));
435 ovsdb_type_destroy(&type);
439 do_parse_atoms(struct ovs_cmdl_context *ctx)
441 struct ovsdb_base_type base;
445 json = unbox_json(parse_json(ctx->argv[1]));
446 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
449 for (i = 2; i < ctx->argc; i++) {
450 struct ovsdb_error *error;
451 union ovsdb_atom atom;
453 json = unbox_json(parse_json(ctx->argv[i]));
454 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
458 print_and_free_ovsdb_error(error);
460 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
461 ovsdb_atom_destroy(&atom, base.type);
464 ovsdb_base_type_destroy(&base);
468 do_parse_atom_strings(struct ovs_cmdl_context *ctx)
470 struct ovsdb_base_type base;
474 json = unbox_json(parse_json(ctx->argv[1]));
475 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
478 for (i = 2; i < ctx->argc; i++) {
479 union ovsdb_atom atom;
482 die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL));
485 ovsdb_atom_to_string(&atom, base.type, &out);
489 ovsdb_atom_destroy(&atom, base.type);
491 ovsdb_base_type_destroy(&base);
495 do_parse_data__(int argc, char *argv[],
497 (*parse)(struct ovsdb_datum *datum,
498 const struct ovsdb_type *type,
499 const struct json *json,
500 struct ovsdb_symbol_table *symtab))
502 struct ovsdb_type type;
506 json = unbox_json(parse_json(argv[1]));
507 check_ovsdb_error(ovsdb_type_from_json(&type, json));
510 for (i = 2; i < argc; i++) {
511 struct ovsdb_datum datum;
513 json = unbox_json(parse_json(argv[i]));
514 check_ovsdb_error(parse(&datum, &type, json, NULL));
517 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
519 ovsdb_datum_destroy(&datum, &type);
521 ovsdb_type_destroy(&type);
525 do_parse_data(struct ovs_cmdl_context *ctx)
527 do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json);
531 do_parse_data_strings(struct ovs_cmdl_context *ctx)
533 struct ovsdb_type type;
537 json = unbox_json(parse_json(ctx->argv[1]));
538 check_ovsdb_error(ovsdb_type_from_json(&type, json));
541 for (i = 2; i < ctx->argc; i++) {
542 struct ovsdb_datum datum;
545 die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL));
548 ovsdb_datum_to_string(&datum, &type, &out);
552 ovsdb_datum_destroy(&datum, &type);
554 ovsdb_type_destroy(&type);
557 static enum ovsdb_atomic_type compare_atoms_atomic_type;
560 compare_atoms(const void *a_, const void *b_)
562 const union ovsdb_atom *a = a_;
563 const union ovsdb_atom *b = b_;
565 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
569 do_sort_atoms(struct ovs_cmdl_context *ctx)
571 struct ovsdb_base_type base;
572 union ovsdb_atom *atoms;
573 struct json *json, **json_atoms;
577 json = unbox_json(parse_json(ctx->argv[1]));
578 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
581 json = unbox_json(parse_json(ctx->argv[2]));
582 if (json->type != JSON_ARRAY) {
583 ovs_fatal(0, "second argument must be array");
586 /* Convert JSON atoms to internal representation. */
587 n_atoms = json->u.array.n;
588 atoms = xmalloc(n_atoms * sizeof *atoms);
589 for (i = 0; i < n_atoms; i++) {
590 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
591 json->u.array.elems[i], NULL));
596 compare_atoms_atomic_type = base.type;
597 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
599 /* Convert internal representation back to JSON. */
600 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
601 for (i = 0; i < n_atoms; i++) {
602 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
603 ovsdb_atom_destroy(&atoms[i], base.type);
605 print_and_free_json(json_array_create(json_atoms, n_atoms));
607 ovsdb_base_type_destroy(&base);
611 do_parse_column(struct ovs_cmdl_context *ctx)
613 struct ovsdb_column *column;
616 json = parse_json(ctx->argv[2]);
617 check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column));
619 print_and_free_json(ovsdb_column_to_json(column));
620 ovsdb_column_destroy(column);
624 do_parse_table(struct ovs_cmdl_context *ctx)
626 struct ovsdb_table_schema *ts;
627 bool default_is_root;
630 default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true");
632 json = parse_json(ctx->argv[2]);
633 check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts));
635 print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
636 ovsdb_table_schema_destroy(ts);
640 do_parse_rows(struct ovs_cmdl_context *ctx)
642 struct ovsdb_column_set all_columns;
643 struct ovsdb_table_schema *ts;
644 struct ovsdb_table *table;
648 json = unbox_json(parse_json(ctx->argv[1]));
649 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
652 table = ovsdb_table_create(ts);
653 ovsdb_column_set_init(&all_columns);
654 ovsdb_column_set_add_all(&all_columns, table);
656 for (i = 2; i < ctx->argc; i++) {
657 struct ovsdb_column_set columns;
658 struct ovsdb_row *row;
660 ovsdb_column_set_init(&columns);
661 row = ovsdb_row_create(table);
663 json = unbox_json(parse_json(ctx->argv[i]));
664 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
667 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
669 if (columns.n_columns) {
675 for (j = 0; j < columns.n_columns; j++) {
676 svec_add(&names, columns.columns[j]->name);
679 s = svec_join(&names, ", ", "");
682 svec_destroy(&names);
687 ovsdb_column_set_destroy(&columns);
688 ovsdb_row_destroy(row);
691 ovsdb_column_set_destroy(&all_columns);
692 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
696 do_compare_rows(struct ovs_cmdl_context *ctx)
698 struct ovsdb_column_set all_columns;
699 struct ovsdb_table_schema *ts;
700 struct ovsdb_table *table;
701 struct ovsdb_row **rows;
707 json = unbox_json(parse_json(ctx->argv[1]));
708 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
711 table = ovsdb_table_create(ts);
712 ovsdb_column_set_init(&all_columns);
713 ovsdb_column_set_add_all(&all_columns, table);
715 n_rows = ctx->argc - 2;
716 rows = xmalloc(sizeof *rows * n_rows);
717 names = xmalloc(sizeof *names * n_rows);
718 for (i = 0; i < n_rows; i++) {
719 rows[i] = ovsdb_row_create(table);
721 json = parse_json(ctx->argv[i + 2]);
722 if (json->type != JSON_ARRAY || json->u.array.n != 2
723 || json->u.array.elems[0]->type != JSON_STRING) {
724 ovs_fatal(0, "\"%s\" does not have expected form "
725 "[\"name\", {data}]", ctx->argv[i]);
727 names[i] = xstrdup(json->u.array.elems[0]->u.string);
728 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
732 for (i = 0; i < n_rows; i++) {
733 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
734 for (j = i + 1; j < n_rows; j++) {
735 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
736 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
737 printf("%s == %s\n", names[i], names[j]);
738 if (i_hash != j_hash) {
739 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
742 } else if (i_hash == j_hash) {
743 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
747 for (i = 0; i < n_rows; i++) {
748 ovsdb_row_destroy(rows[i]);
754 ovsdb_column_set_destroy(&all_columns);
755 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
759 do_parse_conditions(struct ovs_cmdl_context *ctx)
761 struct ovsdb_table_schema *ts;
766 json = unbox_json(parse_json(ctx->argv[1]));
767 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
770 for (i = 2; i < ctx->argc; i++) {
771 struct ovsdb_condition cnd;
772 struct ovsdb_error *error;
774 json = parse_json(ctx->argv[i]);
775 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
777 print_and_free_json(ovsdb_condition_to_json(&cnd));
779 char *s = ovsdb_error_to_string(error);
780 ovs_error(0, "%s", s);
782 ovsdb_error_destroy(error);
787 ovsdb_condition_destroy(&cnd);
789 ovsdb_table_schema_destroy(ts);
795 do_evaluate_conditions(struct ovs_cmdl_context *ctx)
797 struct ovsdb_table_schema *ts;
798 struct ovsdb_table *table;
799 struct ovsdb_condition *conditions;
801 struct ovsdb_row **rows;
806 /* Parse table schema, create table. */
807 json = unbox_json(parse_json(ctx->argv[1]));
808 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
811 table = ovsdb_table_create(ts);
813 /* Parse conditions. */
814 json = parse_json(ctx->argv[2]);
815 if (json->type != JSON_ARRAY) {
816 ovs_fatal(0, "CONDITION argument is not JSON array");
818 n_conditions = json->u.array.n;
819 conditions = xmalloc(n_conditions * sizeof *conditions);
820 for (i = 0; i < n_conditions; i++) {
821 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
822 NULL, &conditions[i]));
827 json = parse_json(ctx->argv[3]);
828 if (json->type != JSON_ARRAY) {
829 ovs_fatal(0, "ROW argument is not JSON array");
831 n_rows = json->u.array.n;
832 rows = xmalloc(n_rows * sizeof *rows);
833 for (i = 0; i < n_rows; i++) {
834 rows[i] = ovsdb_row_create(table);
835 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
840 for (i = 0; i < n_conditions; i++) {
841 printf("condition %2"PRIuSIZE":", i);
842 for (j = 0; j < n_rows; j++) {
843 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
847 putchar(result ? 'T' : '-');
852 for (i = 0; i < n_conditions; i++) {
853 ovsdb_condition_destroy(&conditions[i]);
856 for (i = 0; i < n_rows; i++) {
857 ovsdb_row_destroy(rows[i]);
860 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
864 do_parse_mutations(struct ovs_cmdl_context *ctx)
866 struct ovsdb_table_schema *ts;
871 json = unbox_json(parse_json(ctx->argv[1]));
872 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
875 for (i = 2; i < ctx->argc; i++) {
876 struct ovsdb_mutation_set set;
877 struct ovsdb_error *error;
879 json = parse_json(ctx->argv[i]);
880 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
882 print_and_free_json(ovsdb_mutation_set_to_json(&set));
884 char *s = ovsdb_error_to_string(error);
885 ovs_error(0, "%s", s);
887 ovsdb_error_destroy(error);
892 ovsdb_mutation_set_destroy(&set);
894 ovsdb_table_schema_destroy(ts);
900 do_execute_mutations(struct ovs_cmdl_context *ctx)
902 struct ovsdb_table_schema *ts;
903 struct ovsdb_table *table;
904 struct ovsdb_mutation_set *sets;
906 struct ovsdb_row **rows;
911 /* Parse table schema, create table. */
912 json = unbox_json(parse_json(ctx->argv[1]));
913 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
916 table = ovsdb_table_create(ts);
918 /* Parse mutations. */
919 json = parse_json(ctx->argv[2]);
920 if (json->type != JSON_ARRAY) {
921 ovs_fatal(0, "MUTATION argument is not JSON array");
923 n_sets = json->u.array.n;
924 sets = xmalloc(n_sets * sizeof *sets);
925 for (i = 0; i < n_sets; i++) {
926 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
927 json->u.array.elems[i],
933 json = parse_json(ctx->argv[3]);
934 if (json->type != JSON_ARRAY) {
935 ovs_fatal(0, "ROW argument is not JSON array");
937 n_rows = json->u.array.n;
938 rows = xmalloc(n_rows * sizeof *rows);
939 for (i = 0; i < n_rows; i++) {
940 rows[i] = ovsdb_row_create(table);
941 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
946 for (i = 0; i < n_sets; i++) {
947 printf("mutation %2"PRIuSIZE":\n", i);
948 for (j = 0; j < n_rows; j++) {
949 struct ovsdb_error *error;
950 struct ovsdb_row *row;
952 row = ovsdb_row_clone(rows[j]);
953 error = ovsdb_mutation_set_execute(row, &sets[i]);
955 printf("row %"PRIuSIZE": ", j);
957 print_and_free_ovsdb_error(error);
959 struct ovsdb_column_set columns;
960 struct shash_node *node;
962 ovsdb_column_set_init(&columns);
963 SHASH_FOR_EACH (node, &ts->columns) {
964 struct ovsdb_column *c = node->data;
965 if (!ovsdb_datum_equals(&row->fields[c->index],
966 &rows[j]->fields[c->index],
968 ovsdb_column_set_add(&columns, c);
971 if (columns.n_columns) {
972 print_and_free_json(ovsdb_row_to_json(row, &columns));
974 printf("no change\n");
976 ovsdb_column_set_destroy(&columns);
978 ovsdb_row_destroy(row);
983 for (i = 0; i < n_sets; i++) {
984 ovsdb_mutation_set_destroy(&sets[i]);
987 for (i = 0; i < n_rows; i++) {
988 ovsdb_row_destroy(rows[i]);
991 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
994 /* Inserts a row, without bothering to update metadata such as refcounts. */
996 put_row(struct ovsdb_table *table, struct ovsdb_row *row)
998 const struct uuid *uuid = ovsdb_row_get_uuid(row);
999 if (!ovsdb_table_get_row(table, uuid)) {
1000 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1004 struct do_query_cbdata {
1005 struct uuid *row_uuids;
1011 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
1013 struct do_query_cbdata *cbdata = cbdata_;
1016 for (i = 0; i < cbdata->n_rows; i++) {
1017 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1018 cbdata->counts[i]++;
1026 do_query(struct ovs_cmdl_context *ctx)
1028 struct do_query_cbdata cbdata;
1029 struct ovsdb_table_schema *ts;
1030 struct ovsdb_table *table;
1035 /* Parse table schema, create table. */
1036 json = unbox_json(parse_json(ctx->argv[1]));
1037 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1040 table = ovsdb_table_create(ts);
1042 /* Parse rows, add to table. */
1043 json = parse_json(ctx->argv[2]);
1044 if (json->type != JSON_ARRAY) {
1045 ovs_fatal(0, "ROW argument is not JSON array");
1047 cbdata.n_rows = json->u.array.n;
1048 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1049 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1050 for (i = 0; i < cbdata.n_rows; i++) {
1051 struct ovsdb_row *row = ovsdb_row_create(table);
1052 uuid_generate(ovsdb_row_get_uuid_rw(row));
1053 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1055 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1056 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1057 UUID_ARGS(ovsdb_row_get_uuid(row)));
1059 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1060 put_row(table, row);
1064 /* Parse conditions and execute queries. */
1065 json = parse_json(ctx->argv[3]);
1066 if (json->type != JSON_ARRAY) {
1067 ovs_fatal(0, "CONDITION argument is not JSON array");
1069 for (i = 0; i < json->u.array.n; i++) {
1070 struct ovsdb_condition cnd;
1073 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1076 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1077 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1079 printf("query %2"PRIuSIZE":", i);
1080 for (j = 0; j < cbdata.n_rows; j++) {
1084 if (cbdata.counts[j]) {
1085 printf("%d", cbdata.counts[j]);
1086 if (cbdata.counts[j] > 1) {
1096 ovsdb_condition_destroy(&cnd);
1100 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1105 struct do_query_distinct_class {
1106 struct ovsdb_row *example;
1110 struct do_query_distinct_row {
1112 struct do_query_distinct_class *class;
1116 do_query_distinct(struct ovs_cmdl_context *ctx)
1118 struct ovsdb_column_set columns;
1119 struct ovsdb_table_schema *ts;
1120 struct ovsdb_table *table;
1121 struct do_query_distinct_row *rows;
1123 struct do_query_distinct_class *classes;
1129 /* Parse table schema, create table. */
1130 json = unbox_json(parse_json(ctx->argv[1]));
1131 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1134 table = ovsdb_table_create(ts);
1136 /* Parse column set. */
1137 json = parse_json(ctx->argv[4]);
1138 check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1142 /* Parse rows, add to table. */
1143 json = parse_json(ctx->argv[2]);
1144 if (json->type != JSON_ARRAY) {
1145 ovs_fatal(0, "ROW argument is not JSON array");
1147 n_rows = json->u.array.n;
1148 rows = xmalloc(n_rows * sizeof *rows);
1149 classes = xmalloc(n_rows * sizeof *classes);
1151 for (i = 0; i < n_rows; i++) {
1152 struct ovsdb_row *row;
1156 row = ovsdb_row_create(table);
1157 uuid_generate(ovsdb_row_get_uuid_rw(row));
1158 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1161 /* Initialize row and find equivalence class. */
1162 rows[i].uuid = *ovsdb_row_get_uuid(row);
1163 rows[i].class = NULL;
1164 for (j = 0; j < n_classes; j++) {
1165 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1166 rows[i].class = &classes[j];
1170 if (!rows[i].class) {
1171 rows[i].class = &classes[n_classes];
1172 classes[n_classes].example = ovsdb_row_clone(row);
1176 /* Add row to table. */
1177 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1178 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1179 UUID_ARGS(ovsdb_row_get_uuid(row)));
1181 put_row(table, row);
1186 /* Parse conditions and execute queries. */
1187 json = parse_json(ctx->argv[3]);
1188 if (json->type != JSON_ARRAY) {
1189 ovs_fatal(0, "CONDITION argument is not JSON array");
1191 for (i = 0; i < json->u.array.n; i++) {
1192 struct ovsdb_row_set results;
1193 struct ovsdb_condition cnd;
1196 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1199 for (j = 0; j < n_classes; j++) {
1200 classes[j].count = 0;
1202 ovsdb_row_set_init(&results);
1203 ovsdb_query_distinct(table, &cnd, &columns, &results);
1204 for (j = 0; j < results.n_rows; j++) {
1207 for (k = 0; k < n_rows; k++) {
1208 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1210 rows[k].class->count++;
1214 ovsdb_row_set_destroy(&results);
1216 printf("query %2"PRIuSIZE":", i);
1217 for (j = 0; j < n_rows; j++) {
1218 int count = rows[j].class->count;
1225 printf("%d", count);
1227 } else if (count == 1) {
1228 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1235 ovsdb_condition_destroy(&cnd);
1239 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1245 do_parse_schema(struct ovs_cmdl_context *ctx)
1247 struct ovsdb_schema *schema;
1250 json = parse_json(ctx->argv[1]);
1251 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1253 print_and_free_json(ovsdb_schema_to_json(schema));
1254 ovsdb_schema_destroy(schema);
1258 do_execute(struct ovs_cmdl_context *ctx)
1260 struct ovsdb_schema *schema;
1265 /* Create database. */
1266 json = parse_json(ctx->argv[1]);
1267 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1269 db = ovsdb_create(schema);
1271 for (i = 2; i < ctx->argc; i++) {
1272 struct json *params, *result;
1275 params = parse_json(ctx->argv[i]);
1276 result = ovsdb_execute(db, NULL, params, 0, NULL);
1277 s = json_to_string(result, JSSF_SORT);
1280 json_destroy(params);
1281 json_destroy(result);
1287 struct test_trigger {
1288 struct ovsdb_trigger trigger;
1293 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1295 struct json *result;
1298 result = ovsdb_trigger_steal_result(&t->trigger);
1299 s = json_to_string(result, JSSF_SORT);
1300 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1302 json_destroy(result);
1303 ovsdb_trigger_destroy(&t->trigger);
1308 do_trigger(struct ovs_cmdl_context *ctx)
1310 struct ovsdb_schema *schema;
1311 struct ovsdb_session session;
1312 struct ovsdb_server server;
1319 /* Create database. */
1320 json = parse_json(ctx->argv[1]);
1321 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1323 db = ovsdb_create(schema);
1325 ovsdb_server_init(&server);
1326 ovsdb_server_add_db(&server, db);
1327 ovsdb_session_init(&session, &server);
1331 for (i = 2; i < ctx->argc; i++) {
1332 struct json *params = parse_json(ctx->argv[i]);
1333 if (params->type == JSON_ARRAY
1334 && json_array(params)->n == 2
1335 && json_array(params)->elems[0]->type == JSON_STRING
1336 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1337 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1338 now += json_integer(json_array(params)->elems[1]);
1339 json_destroy(params);
1341 struct test_trigger *t = xmalloc(sizeof *t);
1342 ovsdb_trigger_init(&session, db, &t->trigger, params, now);
1343 t->number = number++;
1344 if (ovsdb_trigger_is_complete(&t->trigger)) {
1345 do_trigger_dump(t, now, "immediate");
1347 printf("t=%lld: new trigger %d\n", now, t->number);
1351 ovsdb_trigger_run(db, now);
1352 while (!list_is_empty(&session.completions)) {
1353 do_trigger_dump(CONTAINER_OF(list_pop_front(&session.completions),
1354 struct test_trigger, trigger.node),
1358 ovsdb_trigger_wait(db, now);
1359 poll_immediate_wake();
1363 ovsdb_server_destroy(&server);
1368 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
1373 /* "transact" command. */
1375 static struct ovsdb *do_transact_db;
1376 static struct ovsdb_txn *do_transact_txn;
1377 static struct ovsdb_table *do_transact_table;
1380 do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED)
1382 ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
1383 do_transact_txn = NULL;
1387 do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED)
1389 ovsdb_txn_abort(do_transact_txn);
1390 do_transact_txn = NULL;
1394 uuid_from_integer(int integer, struct uuid *uuid)
1397 uuid->parts[3] = integer;
1400 static const struct ovsdb_row *
1401 do_transact_find_row(const char *uuid_string)
1403 const struct ovsdb_row *row;
1406 uuid_from_integer(atoi(uuid_string), &uuid);
1407 row = ovsdb_table_get_row(do_transact_table, &uuid);
1409 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1416 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1419 if (integer != -1) {
1420 const struct ovsdb_column *column;
1422 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1424 row->fields[column->index].keys[0].integer = integer;
1429 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1431 const struct ovsdb_column *column;
1433 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1435 return row->fields[column->index].keys[0].integer;
1439 do_transact_set_i_j(struct ovsdb_row *row,
1440 const char *i_string, const char *j_string)
1442 do_transact_set_integer(row, "i", atoi(i_string));
1443 do_transact_set_integer(row, "j", atoi(j_string));
1447 do_transact_insert(struct ovs_cmdl_context *ctx)
1449 struct ovsdb_row *row;
1452 row = ovsdb_row_create(do_transact_table);
1455 uuid = ovsdb_row_get_uuid_rw(row);
1456 uuid_from_integer(atoi(ctx->argv[1]), uuid);
1457 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1458 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1462 do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]);
1465 ovsdb_txn_row_insert(do_transact_txn, row);
1469 do_transact_delete(struct ovs_cmdl_context *ctx)
1471 const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]);
1472 ovsdb_txn_row_delete(do_transact_txn, row);
1476 do_transact_modify(struct ovs_cmdl_context *ctx)
1478 const struct ovsdb_row *row_ro;
1479 struct ovsdb_row *row_rw;
1481 row_ro = do_transact_find_row(ctx->argv[1]);
1482 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1483 do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]);
1487 compare_rows_by_uuid(const void *a_, const void *b_)
1489 struct ovsdb_row *const *ap = a_;
1490 struct ovsdb_row *const *bp = b_;
1492 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1496 do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED)
1498 const struct ovsdb_row **rows;
1499 const struct ovsdb_row *row;
1503 n_rows = hmap_count(&do_transact_table->rows);
1504 rows = xmalloc(n_rows * sizeof *rows);
1506 HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
1509 assert(i == n_rows);
1511 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1513 for (i = 0; i < n_rows; i++) {
1514 printf("\n%"PRId32": i=%d, j=%d",
1515 ovsdb_row_get_uuid(rows[i])->parts[3],
1516 do_transact_get_integer(rows[i], "i"),
1517 do_transact_get_integer(rows[i], "j"));
1524 do_transact(struct ovs_cmdl_context *ctx)
1526 static const struct ovs_cmdl_command do_transact_commands[] = {
1527 { "commit", NULL, 0, 0, do_transact_commit },
1528 { "abort", NULL, 0, 0, do_transact_abort },
1529 { "insert", NULL, 2, 3, do_transact_insert },
1530 { "delete", NULL, 1, 1, do_transact_delete },
1531 { "modify", NULL, 2, 3, do_transact_modify },
1532 { "print", NULL, 0, 0, do_transact_print },
1533 { NULL, NULL, 0, 0, NULL },
1536 struct ovsdb_schema *schema;
1541 json = parse_json("{\"name\": \"testdb\", "
1545 " {\"i\": {\"type\": \"integer\"}, "
1546 " \"j\": {\"type\": \"integer\"}}}}}");
1547 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1549 do_transact_db = ovsdb_create(schema);
1550 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1551 assert(do_transact_table != NULL);
1553 for (i = 1; i < ctx->argc; i++) {
1554 struct json *command;
1558 struct ovs_cmdl_context transact_ctx = { .argc = 0, };
1560 command = parse_json(ctx->argv[i]);
1561 if (command->type != JSON_ARRAY) {
1562 ovs_fatal(0, "transaction %d must be JSON array "
1563 "with at least 1 element", i);
1566 n_args = command->u.array.n;
1567 args = xmalloc((n_args + 1) * sizeof *args);
1568 for (j = 0; j < n_args; j++) {
1569 struct json *s = command->u.array.elems[j];
1570 if (s->type != JSON_STRING) {
1571 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1574 args[j] = xstrdup(json_string(s));
1576 args[n_args] = NULL;
1578 if (!do_transact_txn) {
1579 do_transact_txn = ovsdb_txn_create(do_transact_db);
1582 for (j = 0; j < n_args; j++) {
1586 fputs(args[j], stdout);
1589 transact_ctx.argc = n_args;
1590 transact_ctx.argv = args;
1591 ovs_cmdl_run_command(&transact_ctx, do_transact_commands);
1594 for (j = 0; j < n_args; j++) {
1598 json_destroy(command);
1600 ovsdb_txn_abort(do_transact_txn);
1601 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1605 compare_link1(const void *a_, const void *b_)
1607 const struct idltest_link1 *const *ap = a_;
1608 const struct idltest_link1 *const *bp = b_;
1609 const struct idltest_link1 *a = *ap;
1610 const struct idltest_link1 *b = *bp;
1612 return a->i < b->i ? -1 : a->i > b->i;
1616 print_idl_row_simple(const struct idltest_simple *s, int step)
1620 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1621 step, s->i, s->r, s->b ? "true" : "false",
1622 s->s, UUID_ARGS(&s->u));
1623 for (i = 0; i < s->n_ia; i++) {
1624 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1627 for (i = 0; i < s->n_ra; i++) {
1628 printf("%s%g", i ? " " : "", s->ra[i]);
1631 for (i = 0; i < s->n_ba; i++) {
1632 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1635 for (i = 0; i < s->n_sa; i++) {
1636 printf("%s%s", i ? " " : "", s->sa[i]);
1639 for (i = 0; i < s->n_ua; i++) {
1640 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1642 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1646 print_idl_row_link1(const struct idltest_link1 *l1, int step)
1648 struct idltest_link1 **links;
1651 printf("%03d: i=%"PRId64" k=", step, l1->i);
1653 printf("%"PRId64, l1->k->i);
1656 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1657 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1658 for (i = 0; i < l1->n_ka; i++) {
1659 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1664 printf("%"PRId64, l1->l2->i);
1666 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1670 print_idl_row_link2(const struct idltest_link2 *l2, int step)
1672 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1674 printf("%"PRId64, l2->l1->i);
1676 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1680 print_idl(struct ovsdb_idl *idl, int step)
1682 const struct idltest_simple *s;
1683 const struct idltest_link1 *l1;
1684 const struct idltest_link2 *l2;
1687 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1688 print_idl_row_simple(s, step);
1691 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1692 print_idl_row_link1(l1, step);
1695 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1696 print_idl_row_link2(l2, step);
1700 printf("%03d: empty\n", step);
1705 print_idl_track(struct ovsdb_idl *idl, int step, unsigned int seqno)
1707 const struct idltest_simple *s;
1708 const struct idltest_link1 *l1;
1709 const struct idltest_link2 *l2;
1712 IDLTEST_SIMPLE_FOR_EACH_TRACKED (s, idl) {
1713 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1714 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1716 print_idl_row_simple(s, step);
1720 IDLTEST_LINK1_FOR_EACH_TRACKED (l1, idl) {
1721 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1722 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1724 print_idl_row_link1(l1, step);
1728 IDLTEST_LINK2_FOR_EACH_TRACKED (l2, idl) {
1729 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1730 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1732 print_idl_row_link2(l2, step);
1737 printf("%03d: empty\n", step);
1742 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1747 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1748 char *name = xasprintf("#%"PRIuSIZE"#", *n);
1749 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1750 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1753 } else if (json->type == JSON_ARRAY) {
1756 for (i = 0; i < json->u.array.n; i++) {
1757 parse_uuids(json->u.array.elems[i], symtab, n);
1759 } else if (json->type == JSON_OBJECT) {
1760 const struct shash_node *node;
1762 SHASH_FOR_EACH (node, json_object(json)) {
1763 parse_uuids(node->data, symtab, n);
1769 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1771 if (json->type == JSON_STRING) {
1772 const struct ovsdb_symbol *symbol;
1774 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1776 free(json->u.string);
1777 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1779 } else if (json->type == JSON_ARRAY) {
1782 for (i = 0; i < json->u.array.n; i++) {
1783 substitute_uuids(json->u.array.elems[i], symtab);
1785 } else if (json->type == JSON_OBJECT) {
1786 const struct shash_node *node;
1788 SHASH_FOR_EACH (node, json_object(json)) {
1789 substitute_uuids(node->data, symtab);
1794 static const struct idltest_simple *
1795 idltest_find_simple(struct ovsdb_idl *idl, int i)
1797 const struct idltest_simple *s;
1799 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1808 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1810 char *cmd, *save_ptr1 = NULL;
1811 struct ovsdb_idl_txn *txn;
1812 enum ovsdb_idl_txn_status status;
1813 bool increment = false;
1815 txn = ovsdb_idl_txn_create(idl);
1816 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1817 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1818 char *save_ptr2 = NULL;
1819 char *name, *arg1, *arg2, *arg3;
1821 name = strtok_r(cmd, " ", &save_ptr2);
1822 arg1 = strtok_r(NULL, " ", &save_ptr2);
1823 arg2 = strtok_r(NULL, " ", &save_ptr2);
1824 arg3 = strtok_r(NULL, " ", &save_ptr2);
1826 if (!strcmp(name, "set")) {
1827 const struct idltest_simple *s;
1830 ovs_fatal(0, "\"set\" command requires 3 arguments");
1833 s = idltest_find_simple(idl, atoi(arg1));
1835 ovs_fatal(0, "\"set\" command asks for nonexistent "
1836 "i=%d", atoi(arg1));
1839 if (!strcmp(arg2, "b")) {
1840 idltest_simple_set_b(s, atoi(arg3));
1841 } else if (!strcmp(arg2, "s")) {
1842 idltest_simple_set_s(s, arg3);
1843 } else if (!strcmp(arg2, "u")) {
1845 if (!uuid_from_string(&uuid, arg3)) {
1846 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
1848 idltest_simple_set_u(s, uuid);
1849 } else if (!strcmp(arg2, "r")) {
1850 idltest_simple_set_r(s, atof(arg3));
1852 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1855 } else if (!strcmp(name, "insert")) {
1856 struct idltest_simple *s;
1858 if (!arg1 || arg2) {
1859 ovs_fatal(0, "\"insert\" command requires 1 argument");
1862 s = idltest_simple_insert(txn);
1863 idltest_simple_set_i(s, atoi(arg1));
1864 } else if (!strcmp(name, "delete")) {
1865 const struct idltest_simple *s;
1867 if (!arg1 || arg2) {
1868 ovs_fatal(0, "\"delete\" command requires 1 argument");
1871 s = idltest_find_simple(idl, atoi(arg1));
1873 ovs_fatal(0, "\"delete\" command asks for nonexistent "
1874 "i=%d", atoi(arg1));
1876 idltest_simple_delete(s);
1877 } else if (!strcmp(name, "verify")) {
1878 const struct idltest_simple *s;
1880 if (!arg2 || arg3) {
1881 ovs_fatal(0, "\"verify\" command requires 2 arguments");
1884 s = idltest_find_simple(idl, atoi(arg1));
1886 ovs_fatal(0, "\"verify\" command asks for nonexistent "
1887 "i=%d", atoi(arg1));
1890 if (!strcmp(arg2, "i")) {
1891 idltest_simple_verify_i(s);
1892 } else if (!strcmp(arg2, "b")) {
1893 idltest_simple_verify_b(s);
1894 } else if (!strcmp(arg2, "s")) {
1895 idltest_simple_verify_s(s);
1896 } else if (!strcmp(arg2, "u")) {
1897 idltest_simple_verify_s(s);
1898 } else if (!strcmp(arg2, "r")) {
1899 idltest_simple_verify_r(s);
1901 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
1904 } else if (!strcmp(name, "increment")) {
1905 const struct idltest_simple *s;
1907 if (!arg1 || arg2) {
1908 ovs_fatal(0, "\"increment\" command requires 1 argument");
1911 s = idltest_find_simple(idl, atoi(arg1));
1913 ovs_fatal(0, "\"set\" command asks for nonexistent "
1914 "i=%d", atoi(arg1));
1917 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i);
1919 } else if (!strcmp(name, "abort")) {
1920 ovsdb_idl_txn_abort(txn);
1922 } else if (!strcmp(name, "destroy")) {
1923 printf("%03d: destroy\n", step);
1924 ovsdb_idl_txn_destroy(txn);
1927 ovs_fatal(0, "unknown command %s", name);
1931 status = ovsdb_idl_txn_commit_block(txn);
1932 printf("%03d: commit, status=%s",
1933 step, ovsdb_idl_txn_status_to_string(status));
1935 printf(", increment=%"PRId64,
1936 ovsdb_idl_txn_get_increment_new_value(txn));
1939 ovsdb_idl_txn_destroy(txn);
1943 do_idl(struct ovs_cmdl_context *ctx)
1945 struct jsonrpc *rpc;
1946 struct ovsdb_idl *idl;
1947 unsigned int seqno = 0;
1948 struct ovsdb_symbol_table *symtab;
1957 track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
1959 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
1960 if (ctx->argc > 2) {
1961 struct stream *stream;
1963 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
1964 DSCP_DEFAULT), &stream);
1966 ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
1968 rpc = jsonrpc_open(stream);
1974 ovsdb_idl_track_add_all(idl);
1977 setvbuf(stdout, NULL, _IONBF, 0);
1979 symtab = ovsdb_symbol_table_create();
1980 for (i = 2; i < ctx->argc; i++) {
1981 char *arg = ctx->argv[i];
1982 struct jsonrpc_msg *request, *reply;
1985 /* The previous transaction didn't change anything. */
1988 /* Wait for update. */
1991 if (ovsdb_idl_get_seqno(idl) != seqno) {
1996 ovsdb_idl_wait(idl);
2003 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2004 ovsdb_idl_track_clear(idl);
2006 print_idl(idl, step++);
2009 seqno = ovsdb_idl_get_seqno(idl);
2011 if (!strcmp(arg, "reconnect")) {
2012 printf("%03d: reconnect\n", step++);
2013 ovsdb_idl_force_reconnect(idl);
2014 } else if (arg[0] != '[') {
2015 idl_set(idl, arg, step++);
2017 struct json *json = parse_json(arg);
2018 substitute_uuids(json, symtab);
2019 request = jsonrpc_create_request("transact", json, NULL);
2020 error = jsonrpc_transact_block(rpc, request, &reply);
2021 if (error || reply->error) {
2022 ovs_fatal(error, "jsonrpc transaction failed");
2024 printf("%03d: ", step++);
2025 if (reply->result) {
2026 parse_uuids(reply->result, symtab, &n_uuids);
2028 json_destroy(reply->id);
2030 print_and_free_json(jsonrpc_msg_to_json(reply));
2033 ovsdb_symbol_table_destroy(symtab);
2040 if (ovsdb_idl_get_seqno(idl) != seqno) {
2043 ovsdb_idl_wait(idl);
2046 print_idl(idl, step++);
2047 ovsdb_idl_track_clear(idl);
2048 ovsdb_idl_destroy(idl);
2049 printf("%03d: done\n", step);
2052 static struct ovs_cmdl_command all_commands[] = {
2053 { "log-io", NULL, 2, INT_MAX, do_log_io },
2054 { "default-atoms", NULL, 0, 0, do_default_atoms },
2055 { "default-data", NULL, 0, 0, do_default_data },
2056 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
2057 { "parse-base-type", NULL, 1, 1, do_parse_base_type },
2058 { "parse-type", NULL, 1, 1, do_parse_type },
2059 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
2060 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
2061 { "parse-data", NULL, 2, INT_MAX, do_parse_data },
2062 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
2063 { "sort-atoms", NULL, 2, 2, do_sort_atoms },
2064 { "parse-column", NULL, 2, 2, do_parse_column },
2065 { "parse-table", NULL, 2, 3, do_parse_table },
2066 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
2067 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
2068 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
2069 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
2070 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
2071 { "execute-mutations", NULL, 3, 3, do_execute_mutations },
2072 { "query", NULL, 3, 3, do_query },
2073 { "query-distinct", NULL, 4, 4, do_query_distinct },
2074 { "transact", NULL, 1, INT_MAX, do_transact },
2075 { "parse-schema", NULL, 1, 1, do_parse_schema },
2076 { "execute", NULL, 2, INT_MAX, do_execute },
2077 { "trigger", NULL, 2, INT_MAX, do_trigger },
2078 { "idl", NULL, 1, INT_MAX, do_idl },
2079 { "help", NULL, 0, INT_MAX, do_help },
2080 { NULL, NULL, 0, 0, NULL },
2083 static struct ovs_cmdl_command *
2084 get_all_commands(void)
2086 return all_commands;