2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2015 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 "byte-order.h"
27 #include "command-line.h"
28 #include "openvswitch/dynamic-string.h"
31 #include "ovsdb-data.h"
32 #include "ovsdb-error.h"
33 #include "ovsdb-idl.h"
34 #include "ovsdb-types.h"
35 #include "ovsdb/column.h"
36 #include "ovsdb/condition.h"
37 #include "ovsdb/file.h"
38 #include "ovsdb/log.h"
39 #include "ovsdb/mutation.h"
40 #include "ovsdb/ovsdb.h"
41 #include "ovsdb/query.h"
42 #include "ovsdb/row.h"
43 #include "ovsdb/server.h"
44 #include "ovsdb/table.h"
45 #include "ovsdb/transaction.h"
46 #include "ovsdb/trigger.h"
47 #include "poll-loop.h"
50 #include "tests/idltest.h"
53 #include "openvswitch/vlog.h"
55 struct test_ovsdb_pvt_context {
59 OVS_NO_RETURN static void usage(void);
60 static void parse_options(int argc, char *argv[],
61 struct test_ovsdb_pvt_context *pvt);
62 static struct ovs_cmdl_command *get_all_commands(void);
65 main(int argc, char *argv[])
67 struct test_ovsdb_pvt_context pvt = {.track = false};
68 struct ovs_cmdl_context ctx = { .argc = 0, .pvt = &pvt};
69 set_program_name(argv[0]);
70 parse_options(argc, argv, &pvt);
71 ctx.argc = argc - optind;
72 ctx.argv = argv + optind;
73 ovs_cmdl_run_command(&ctx, get_all_commands());
78 parse_options(int argc, char *argv[], struct test_ovsdb_pvt_context *pvt)
80 static const struct option long_options[] = {
81 {"timeout", required_argument, NULL, 't'},
82 {"verbose", optional_argument, NULL, 'v'},
83 {"change-track", optional_argument, NULL, 'c'},
84 {"help", no_argument, NULL, 'h'},
87 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
90 unsigned long int timeout;
93 c = getopt_long(argc, argv, short_options, long_options, NULL);
100 timeout = strtoul(optarg, NULL, 10);
102 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
113 vlog_set_verbosity(optarg);
133 printf("%s: Open vSwitch database test utility\n"
134 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
135 " log-io FILE FLAGS COMMAND...\n"
136 " open FILE with FLAGS, run COMMANDs\n"
138 " test ovsdb_atom_default()\n"
140 " test ovsdb_datum_default()\n"
141 " parse-atomic-type TYPE\n"
142 " parse TYPE as OVSDB atomic type, and re-serialize\n"
143 " parse-base-type TYPE\n"
144 " parse TYPE as OVSDB base type, and re-serialize\n"
146 " parse JSON as OVSDB type, and re-serialize\n"
147 " parse-atoms TYPE ATOM...\n"
148 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
149 " parse-atom-strings TYPE ATOM...\n"
150 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
151 " sort-atoms TYPE ATOM...\n"
152 " print JSON ATOMs in sorted order\n"
153 " parse-data TYPE DATUM...\n"
154 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
155 " parse-data-strings TYPE DATUM...\n"
156 " parse string DATUMs as data of given TYPE, and re-serialize\n"
157 " parse-column NAME OBJECT\n"
158 " parse column NAME with info OBJECT, and re-serialize\n"
159 " parse-table NAME OBJECT [DEFAULT-IS-ROOT]\n"
160 " parse table NAME with info OBJECT\n"
161 " parse-row TABLE ROW..., and re-serialize\n"
162 " parse each ROW of defined TABLE\n"
163 " compare-row TABLE ROW...\n"
164 " mutually compare all of the ROWs, print those that are equal\n"
165 " parse-conditions TABLE CONDITION...\n"
166 " parse each CONDITION on TABLE, and re-serialize\n"
167 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
168 " test CONDITIONS on TABLE against each ROW, print results\n"
169 " parse-mutations TABLE MUTATION...\n"
170 " parse each MUTATION on TABLE, and re-serialize\n"
171 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
172 " execute MUTATIONS on TABLE on each ROW, print results\n"
173 " query TABLE [ROW,...] [CONDITION,...]\n"
174 " add each ROW to TABLE, then query and print the rows that\n"
175 " satisfy each CONDITION.\n"
176 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
177 " add each ROW to TABLE, then query and print the rows that\n"
178 " satisfy each CONDITION and have distinct COLUMNS.\n"
179 " parse-schema JSON\n"
180 " parse JSON as an OVSDB schema, and re-serialize\n"
181 " transact COMMAND\n"
182 " execute each specified transactional COMMAND:\n"
189 " execute SCHEMA TRANSACTION...\n"
190 " executes each TRANSACTION on an initially empty database\n"
191 " the specified SCHEMA\n"
192 " trigger SCHEMA TRANSACTION...\n"
193 " executes each TRANSACTION on an initially empty database\n"
194 " the specified SCHEMA. A TRANSACTION of the form\n"
195 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
196 " simulated time, for causing triggers to time out.\n"
197 " idl SERVER [TRANSACTION...]\n"
198 " connect to SERVER and dump the contents of the database\n"
199 " as seen initially by the IDL implementation and after\n"
200 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
201 " the database or this command will hang.)\n",
202 program_name, program_name);
204 printf("\nOther options:\n"
205 " -t, --timeout=SECS give up after SECS seconds\n"
206 " -h, --help display this help message\n"
207 " -c, --change-track used with the 'idl' command to\n"
208 " enable tracking of IDL changes\n");
212 /* Command helper functions. */
215 parse_json(const char *s)
217 struct json *json = json_from_string(s);
218 if (json->type == JSON_STRING) {
219 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
225 unbox_json(struct json *json)
227 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
228 struct json *inner = json->u.array.elems[0];
229 json->u.array.elems[0] = NULL;
238 print_and_free_json(struct json *json)
240 char *string = json_to_string(json, JSSF_SORT);
247 print_and_free_ovsdb_error(struct ovsdb_error *error)
249 char *string = ovsdb_error_to_string(error);
250 ovsdb_error_destroy(error);
256 check_ovsdb_error(struct ovsdb_error *error)
259 char *s = ovsdb_error_to_string(error);
260 ovsdb_error_destroy(error);
261 ovs_fatal(0, "%s", s);
266 die_if_error(char *error)
269 ovs_fatal(0, "%s", error);
273 /* Command implementations. */
276 do_log_io(struct ovs_cmdl_context *ctx)
278 const char *name = ctx->argv[1];
279 char *mode_string = ctx->argv[2];
281 struct ovsdb_error *error;
282 enum ovsdb_log_open_mode mode;
283 struct ovsdb_log *log;
286 if (!strcmp(mode_string, "read-only")) {
287 mode = OVSDB_LOG_READ_ONLY;
288 } else if (!strcmp(mode_string, "read/write")) {
289 mode = OVSDB_LOG_READ_WRITE;
290 } else if (!strcmp(mode_string, "create")) {
291 mode = OVSDB_LOG_CREATE;
293 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
296 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
297 printf("%s: open successful\n", name);
299 for (i = 3; i < ctx->argc; i++) {
300 const char *command = ctx->argv[i];
301 if (!strcmp(command, "read")) {
304 error = ovsdb_log_read(log, &json);
306 printf("%s: read: ", name);
308 print_and_free_json(json);
310 printf("end of log\n");
314 } else if (!strncmp(command, "write:", 6)) {
315 struct json *json = parse_json(command + 6);
316 error = ovsdb_log_write(log, json);
318 } else if (!strcmp(command, "commit")) {
319 error = ovsdb_log_commit(log);
321 ovs_fatal(0, "unknown log-io command \"%s\"", command);
324 char *s = ovsdb_error_to_string(error);
325 printf("%s: %s failed: %s\n", name, command, s);
327 ovsdb_error_destroy(error);
329 printf("%s: %s successful\n", name, command);
333 ovsdb_log_close(log);
337 do_default_atoms(struct ovs_cmdl_context *ctx OVS_UNUSED)
341 for (type = 0; type < OVSDB_N_TYPES; type++) {
342 union ovsdb_atom atom;
344 if (type == OVSDB_TYPE_VOID) {
348 printf("%s: ", ovsdb_atomic_type_to_string(type));
350 ovsdb_atom_init_default(&atom, type);
351 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
355 ovsdb_atom_destroy(&atom, type);
362 do_default_data(struct ovs_cmdl_context *ctx OVS_UNUSED)
367 for (n_min = 0; n_min <= 1; n_min++) {
368 for (key = 0; key < OVSDB_N_TYPES; key++) {
369 if (key == OVSDB_TYPE_VOID) {
372 for (value = 0; value < OVSDB_N_TYPES; value++) {
373 struct ovsdb_datum datum;
374 struct ovsdb_type type;
376 ovsdb_base_type_init(&type.key, key);
377 ovsdb_base_type_init(&type.value, value);
380 assert(ovsdb_type_is_valid(&type));
382 printf("key %s, value %s, n_min %u: ",
383 ovsdb_atomic_type_to_string(key),
384 ovsdb_atomic_type_to_string(value), n_min);
386 ovsdb_datum_init_default(&datum, &type);
387 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
392 ovsdb_datum_destroy(&datum, &type);
393 ovsdb_type_destroy(&type);
402 do_diff_data(struct ovs_cmdl_context *ctx)
404 struct ovsdb_type type;
406 struct ovsdb_datum new, old, diff, reincarnation;
408 json = unbox_json(parse_json(ctx->argv[1]));
409 check_ovsdb_error(ovsdb_type_from_json(&type, json));
412 /* Arguments in pairs of 'old' and 'new'. */
413 for (int i = 2; i < ctx->argc - 1; i+=2) {
414 struct ovsdb_error *error;
416 json = unbox_json(parse_json(ctx->argv[i]));
417 check_ovsdb_error(ovsdb_datum_from_json(&old, &type, json, NULL));
420 json = unbox_json(parse_json(ctx->argv[i+1]));
421 check_ovsdb_error(ovsdb_transient_datum_from_json(&new, &type, json));
424 /* Generate the diff. */
425 ovsdb_datum_diff(&diff, &old, &new, &type);
427 /* Apply diff to 'old' to create'reincarnation'. */
428 error = ovsdb_datum_apply_diff(&reincarnation, &old, &diff, &type);
430 char *string = ovsdb_error_to_string(error);
431 ovsdb_error_destroy(error);
432 ovs_fatal(0, "%s", string);
435 /* Test to make sure 'new' equals 'reincarnation'. */
436 if (!ovsdb_datum_equals(&new, &reincarnation, &type)) {
437 ovs_fatal(0, "failed to apply diff");
441 json = ovsdb_datum_to_json(&diff, &type);
443 print_and_free_json(json);
445 /* Print reincarnation */
446 json = ovsdb_datum_to_json(&reincarnation, &type);
447 printf ("apply diff: ");
448 print_and_free_json(json);
450 ovsdb_datum_destroy(&new, &type);
451 ovsdb_datum_destroy(&old, &type);
452 ovsdb_datum_destroy(&diff, &type);
453 ovsdb_datum_destroy(&reincarnation, &type);
458 ovsdb_type_destroy(&type);
462 do_parse_atomic_type(struct ovs_cmdl_context *ctx)
464 enum ovsdb_atomic_type type;
467 json = unbox_json(parse_json(ctx->argv[1]));
468 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
470 print_and_free_json(ovsdb_atomic_type_to_json(type));
474 do_parse_base_type(struct ovs_cmdl_context *ctx)
476 struct ovsdb_base_type base;
479 json = unbox_json(parse_json(ctx->argv[1]));
480 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
482 print_and_free_json(ovsdb_base_type_to_json(&base));
483 ovsdb_base_type_destroy(&base);
487 do_parse_type(struct ovs_cmdl_context *ctx)
489 struct ovsdb_type type;
492 json = unbox_json(parse_json(ctx->argv[1]));
493 check_ovsdb_error(ovsdb_type_from_json(&type, json));
495 print_and_free_json(ovsdb_type_to_json(&type));
496 ovsdb_type_destroy(&type);
500 do_parse_atoms(struct ovs_cmdl_context *ctx)
502 struct ovsdb_base_type base;
506 json = unbox_json(parse_json(ctx->argv[1]));
507 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
510 for (i = 2; i < ctx->argc; i++) {
511 struct ovsdb_error *error;
512 union ovsdb_atom atom;
514 json = unbox_json(parse_json(ctx->argv[i]));
515 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
519 print_and_free_ovsdb_error(error);
521 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
522 ovsdb_atom_destroy(&atom, base.type);
525 ovsdb_base_type_destroy(&base);
529 do_parse_atom_strings(struct ovs_cmdl_context *ctx)
531 struct ovsdb_base_type base;
535 json = unbox_json(parse_json(ctx->argv[1]));
536 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
539 for (i = 2; i < ctx->argc; i++) {
540 union ovsdb_atom atom;
543 die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL));
546 ovsdb_atom_to_string(&atom, base.type, &out);
550 ovsdb_atom_destroy(&atom, base.type);
552 ovsdb_base_type_destroy(&base);
556 do_parse_data__(int argc, char *argv[],
558 (*parse)(struct ovsdb_datum *datum,
559 const struct ovsdb_type *type,
560 const struct json *json,
561 struct ovsdb_symbol_table *symtab))
563 struct ovsdb_type type;
567 json = unbox_json(parse_json(argv[1]));
568 check_ovsdb_error(ovsdb_type_from_json(&type, json));
571 for (i = 2; i < argc; i++) {
572 struct ovsdb_datum datum;
574 json = unbox_json(parse_json(argv[i]));
575 check_ovsdb_error(parse(&datum, &type, json, NULL));
578 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
580 ovsdb_datum_destroy(&datum, &type);
582 ovsdb_type_destroy(&type);
586 do_parse_data(struct ovs_cmdl_context *ctx)
588 do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json);
592 do_parse_data_strings(struct ovs_cmdl_context *ctx)
594 struct ovsdb_type type;
598 json = unbox_json(parse_json(ctx->argv[1]));
599 check_ovsdb_error(ovsdb_type_from_json(&type, json));
602 for (i = 2; i < ctx->argc; i++) {
603 struct ovsdb_datum datum;
606 die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL));
609 ovsdb_datum_to_string(&datum, &type, &out);
613 ovsdb_datum_destroy(&datum, &type);
615 ovsdb_type_destroy(&type);
618 static enum ovsdb_atomic_type compare_atoms_atomic_type;
621 compare_atoms(const void *a_, const void *b_)
623 const union ovsdb_atom *a = a_;
624 const union ovsdb_atom *b = b_;
626 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
630 do_sort_atoms(struct ovs_cmdl_context *ctx)
632 struct ovsdb_base_type base;
633 union ovsdb_atom *atoms;
634 struct json *json, **json_atoms;
638 json = unbox_json(parse_json(ctx->argv[1]));
639 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
642 json = unbox_json(parse_json(ctx->argv[2]));
643 if (json->type != JSON_ARRAY) {
644 ovs_fatal(0, "second argument must be array");
647 /* Convert JSON atoms to internal representation. */
648 n_atoms = json->u.array.n;
649 atoms = xmalloc(n_atoms * sizeof *atoms);
650 for (i = 0; i < n_atoms; i++) {
651 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
652 json->u.array.elems[i], NULL));
657 compare_atoms_atomic_type = base.type;
658 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
660 /* Convert internal representation back to JSON. */
661 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
662 for (i = 0; i < n_atoms; i++) {
663 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
664 ovsdb_atom_destroy(&atoms[i], base.type);
666 print_and_free_json(json_array_create(json_atoms, n_atoms));
668 ovsdb_base_type_destroy(&base);
672 do_parse_column(struct ovs_cmdl_context *ctx)
674 struct ovsdb_column *column;
677 json = parse_json(ctx->argv[2]);
678 check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column));
680 print_and_free_json(ovsdb_column_to_json(column));
681 ovsdb_column_destroy(column);
685 do_parse_table(struct ovs_cmdl_context *ctx)
687 struct ovsdb_table_schema *ts;
688 bool default_is_root;
691 default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true");
693 json = parse_json(ctx->argv[2]);
694 check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts));
696 print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
697 ovsdb_table_schema_destroy(ts);
701 do_parse_rows(struct ovs_cmdl_context *ctx)
703 struct ovsdb_column_set all_columns;
704 struct ovsdb_table_schema *ts;
705 struct ovsdb_table *table;
709 json = unbox_json(parse_json(ctx->argv[1]));
710 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
713 table = ovsdb_table_create(ts);
714 ovsdb_column_set_init(&all_columns);
715 ovsdb_column_set_add_all(&all_columns, table);
717 for (i = 2; i < ctx->argc; i++) {
718 struct ovsdb_column_set columns;
719 struct ovsdb_row *row;
721 ovsdb_column_set_init(&columns);
722 row = ovsdb_row_create(table);
724 json = unbox_json(parse_json(ctx->argv[i]));
725 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
728 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
730 if (columns.n_columns) {
736 for (j = 0; j < columns.n_columns; j++) {
737 svec_add(&names, columns.columns[j]->name);
740 s = svec_join(&names, ", ", "");
743 svec_destroy(&names);
748 ovsdb_column_set_destroy(&columns);
749 ovsdb_row_destroy(row);
752 ovsdb_column_set_destroy(&all_columns);
753 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
757 do_compare_rows(struct ovs_cmdl_context *ctx)
759 struct ovsdb_column_set all_columns;
760 struct ovsdb_table_schema *ts;
761 struct ovsdb_table *table;
762 struct ovsdb_row **rows;
768 json = unbox_json(parse_json(ctx->argv[1]));
769 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
772 table = ovsdb_table_create(ts);
773 ovsdb_column_set_init(&all_columns);
774 ovsdb_column_set_add_all(&all_columns, table);
776 n_rows = ctx->argc - 2;
777 rows = xmalloc(sizeof *rows * n_rows);
778 names = xmalloc(sizeof *names * n_rows);
779 for (i = 0; i < n_rows; i++) {
780 rows[i] = ovsdb_row_create(table);
782 json = parse_json(ctx->argv[i + 2]);
783 if (json->type != JSON_ARRAY || json->u.array.n != 2
784 || json->u.array.elems[0]->type != JSON_STRING) {
785 ovs_fatal(0, "\"%s\" does not have expected form "
786 "[\"name\", {data}]", ctx->argv[i]);
788 names[i] = xstrdup(json->u.array.elems[0]->u.string);
789 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
793 for (i = 0; i < n_rows; i++) {
794 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
795 for (j = i + 1; j < n_rows; j++) {
796 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
797 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
798 printf("%s == %s\n", names[i], names[j]);
799 if (i_hash != j_hash) {
800 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
803 } else if (i_hash == j_hash) {
804 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
808 for (i = 0; i < n_rows; i++) {
809 ovsdb_row_destroy(rows[i]);
815 ovsdb_column_set_destroy(&all_columns);
816 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
820 do_parse_conditions(struct ovs_cmdl_context *ctx)
822 struct ovsdb_table_schema *ts;
827 json = unbox_json(parse_json(ctx->argv[1]));
828 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
831 for (i = 2; i < ctx->argc; i++) {
832 struct ovsdb_condition cnd;
833 struct ovsdb_error *error;
835 json = parse_json(ctx->argv[i]);
836 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
838 print_and_free_json(ovsdb_condition_to_json(&cnd));
840 char *s = ovsdb_error_to_string(error);
841 ovs_error(0, "%s", s);
843 ovsdb_error_destroy(error);
848 ovsdb_condition_destroy(&cnd);
850 ovsdb_table_schema_destroy(ts);
856 do_evaluate_conditions(struct ovs_cmdl_context *ctx)
858 struct ovsdb_table_schema *ts;
859 struct ovsdb_table *table;
860 struct ovsdb_condition *conditions;
862 struct ovsdb_row **rows;
867 /* Parse table schema, create table. */
868 json = unbox_json(parse_json(ctx->argv[1]));
869 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
872 table = ovsdb_table_create(ts);
874 /* Parse conditions. */
875 json = parse_json(ctx->argv[2]);
876 if (json->type != JSON_ARRAY) {
877 ovs_fatal(0, "CONDITION argument is not JSON array");
879 n_conditions = json->u.array.n;
880 conditions = xmalloc(n_conditions * sizeof *conditions);
881 for (i = 0; i < n_conditions; i++) {
882 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
883 NULL, &conditions[i]));
888 json = parse_json(ctx->argv[3]);
889 if (json->type != JSON_ARRAY) {
890 ovs_fatal(0, "ROW argument is not JSON array");
892 n_rows = json->u.array.n;
893 rows = xmalloc(n_rows * sizeof *rows);
894 for (i = 0; i < n_rows; i++) {
895 rows[i] = ovsdb_row_create(table);
896 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
901 for (i = 0; i < n_conditions; i++) {
902 printf("condition %2"PRIuSIZE":", i);
903 for (j = 0; j < n_rows; j++) {
904 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
908 putchar(result ? 'T' : '-');
913 for (i = 0; i < n_conditions; i++) {
914 ovsdb_condition_destroy(&conditions[i]);
917 for (i = 0; i < n_rows; i++) {
918 ovsdb_row_destroy(rows[i]);
921 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
925 do_parse_mutations(struct ovs_cmdl_context *ctx)
927 struct ovsdb_table_schema *ts;
932 json = unbox_json(parse_json(ctx->argv[1]));
933 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
936 for (i = 2; i < ctx->argc; i++) {
937 struct ovsdb_mutation_set set;
938 struct ovsdb_error *error;
940 json = parse_json(ctx->argv[i]);
941 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
943 print_and_free_json(ovsdb_mutation_set_to_json(&set));
945 char *s = ovsdb_error_to_string(error);
946 ovs_error(0, "%s", s);
948 ovsdb_error_destroy(error);
953 ovsdb_mutation_set_destroy(&set);
955 ovsdb_table_schema_destroy(ts);
961 do_execute_mutations(struct ovs_cmdl_context *ctx)
963 struct ovsdb_table_schema *ts;
964 struct ovsdb_table *table;
965 struct ovsdb_mutation_set *sets;
967 struct ovsdb_row **rows;
972 /* Parse table schema, create table. */
973 json = unbox_json(parse_json(ctx->argv[1]));
974 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
977 table = ovsdb_table_create(ts);
979 /* Parse mutations. */
980 json = parse_json(ctx->argv[2]);
981 if (json->type != JSON_ARRAY) {
982 ovs_fatal(0, "MUTATION argument is not JSON array");
984 n_sets = json->u.array.n;
985 sets = xmalloc(n_sets * sizeof *sets);
986 for (i = 0; i < n_sets; i++) {
987 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
988 json->u.array.elems[i],
994 json = parse_json(ctx->argv[3]);
995 if (json->type != JSON_ARRAY) {
996 ovs_fatal(0, "ROW argument is not JSON array");
998 n_rows = json->u.array.n;
999 rows = xmalloc(n_rows * sizeof *rows);
1000 for (i = 0; i < n_rows; i++) {
1001 rows[i] = ovsdb_row_create(table);
1002 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
1007 for (i = 0; i < n_sets; i++) {
1008 printf("mutation %2"PRIuSIZE":\n", i);
1009 for (j = 0; j < n_rows; j++) {
1010 struct ovsdb_error *error;
1011 struct ovsdb_row *row;
1013 row = ovsdb_row_clone(rows[j]);
1014 error = ovsdb_mutation_set_execute(row, &sets[i]);
1016 printf("row %"PRIuSIZE": ", j);
1018 print_and_free_ovsdb_error(error);
1020 struct ovsdb_column_set columns;
1021 struct shash_node *node;
1023 ovsdb_column_set_init(&columns);
1024 SHASH_FOR_EACH (node, &ts->columns) {
1025 struct ovsdb_column *c = node->data;
1026 if (!ovsdb_datum_equals(&row->fields[c->index],
1027 &rows[j]->fields[c->index],
1029 ovsdb_column_set_add(&columns, c);
1032 if (columns.n_columns) {
1033 print_and_free_json(ovsdb_row_to_json(row, &columns));
1035 printf("no change\n");
1037 ovsdb_column_set_destroy(&columns);
1039 ovsdb_row_destroy(row);
1044 for (i = 0; i < n_sets; i++) {
1045 ovsdb_mutation_set_destroy(&sets[i]);
1048 for (i = 0; i < n_rows; i++) {
1049 ovsdb_row_destroy(rows[i]);
1052 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1055 /* Inserts a row, without bothering to update metadata such as refcounts. */
1057 put_row(struct ovsdb_table *table, struct ovsdb_row *row)
1059 const struct uuid *uuid = ovsdb_row_get_uuid(row);
1060 if (!ovsdb_table_get_row(table, uuid)) {
1061 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1065 struct do_query_cbdata {
1066 struct uuid *row_uuids;
1072 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
1074 struct do_query_cbdata *cbdata = cbdata_;
1077 for (i = 0; i < cbdata->n_rows; i++) {
1078 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1079 cbdata->counts[i]++;
1087 do_query(struct ovs_cmdl_context *ctx)
1089 struct do_query_cbdata cbdata;
1090 struct ovsdb_table_schema *ts;
1091 struct ovsdb_table *table;
1096 /* Parse table schema, create table. */
1097 json = unbox_json(parse_json(ctx->argv[1]));
1098 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1101 table = ovsdb_table_create(ts);
1103 /* Parse rows, add to table. */
1104 json = parse_json(ctx->argv[2]);
1105 if (json->type != JSON_ARRAY) {
1106 ovs_fatal(0, "ROW argument is not JSON array");
1108 cbdata.n_rows = json->u.array.n;
1109 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1110 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1111 for (i = 0; i < cbdata.n_rows; i++) {
1112 struct ovsdb_row *row = ovsdb_row_create(table);
1113 uuid_generate(ovsdb_row_get_uuid_rw(row));
1114 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1116 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1117 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1118 UUID_ARGS(ovsdb_row_get_uuid(row)));
1120 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1121 put_row(table, row);
1125 /* Parse conditions and execute queries. */
1126 json = parse_json(ctx->argv[3]);
1127 if (json->type != JSON_ARRAY) {
1128 ovs_fatal(0, "CONDITION argument is not JSON array");
1130 for (i = 0; i < json->u.array.n; i++) {
1131 struct ovsdb_condition cnd;
1134 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1137 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1138 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1140 printf("query %2"PRIuSIZE":", i);
1141 for (j = 0; j < cbdata.n_rows; j++) {
1145 if (cbdata.counts[j]) {
1146 printf("%d", cbdata.counts[j]);
1147 if (cbdata.counts[j] > 1) {
1157 ovsdb_condition_destroy(&cnd);
1161 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1166 struct do_query_distinct_class {
1167 struct ovsdb_row *example;
1171 struct do_query_distinct_row {
1173 struct do_query_distinct_class *class;
1177 do_query_distinct(struct ovs_cmdl_context *ctx)
1179 struct ovsdb_column_set columns;
1180 struct ovsdb_table_schema *ts;
1181 struct ovsdb_table *table;
1182 struct do_query_distinct_row *rows;
1184 struct do_query_distinct_class *classes;
1190 /* Parse table schema, create table. */
1191 json = unbox_json(parse_json(ctx->argv[1]));
1192 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1195 table = ovsdb_table_create(ts);
1197 /* Parse column set. */
1198 json = parse_json(ctx->argv[4]);
1199 check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1203 /* Parse rows, add to table. */
1204 json = parse_json(ctx->argv[2]);
1205 if (json->type != JSON_ARRAY) {
1206 ovs_fatal(0, "ROW argument is not JSON array");
1208 n_rows = json->u.array.n;
1209 rows = xmalloc(n_rows * sizeof *rows);
1210 classes = xmalloc(n_rows * sizeof *classes);
1212 for (i = 0; i < n_rows; i++) {
1213 struct ovsdb_row *row;
1217 row = ovsdb_row_create(table);
1218 uuid_generate(ovsdb_row_get_uuid_rw(row));
1219 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1222 /* Initialize row and find equivalence class. */
1223 rows[i].uuid = *ovsdb_row_get_uuid(row);
1224 rows[i].class = NULL;
1225 for (j = 0; j < n_classes; j++) {
1226 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1227 rows[i].class = &classes[j];
1231 if (!rows[i].class) {
1232 rows[i].class = &classes[n_classes];
1233 classes[n_classes].example = ovsdb_row_clone(row);
1237 /* Add row to table. */
1238 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1239 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1240 UUID_ARGS(ovsdb_row_get_uuid(row)));
1242 put_row(table, row);
1247 /* Parse conditions and execute queries. */
1248 json = parse_json(ctx->argv[3]);
1249 if (json->type != JSON_ARRAY) {
1250 ovs_fatal(0, "CONDITION argument is not JSON array");
1252 for (i = 0; i < json->u.array.n; i++) {
1253 struct ovsdb_row_set results;
1254 struct ovsdb_condition cnd;
1257 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1260 for (j = 0; j < n_classes; j++) {
1261 classes[j].count = 0;
1263 ovsdb_row_set_init(&results);
1264 ovsdb_query_distinct(table, &cnd, &columns, &results);
1265 for (j = 0; j < results.n_rows; j++) {
1268 for (k = 0; k < n_rows; k++) {
1269 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1271 rows[k].class->count++;
1275 ovsdb_row_set_destroy(&results);
1277 printf("query %2"PRIuSIZE":", i);
1278 for (j = 0; j < n_rows; j++) {
1279 int count = rows[j].class->count;
1286 printf("%d", count);
1288 } else if (count == 1) {
1289 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1296 ovsdb_condition_destroy(&cnd);
1300 for (i = 0; i < n_classes; i++) {
1301 ovsdb_row_destroy(classes[i].example);
1304 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1312 do_parse_schema(struct ovs_cmdl_context *ctx)
1314 struct ovsdb_schema *schema;
1317 json = parse_json(ctx->argv[1]);
1318 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1320 print_and_free_json(ovsdb_schema_to_json(schema));
1321 ovsdb_schema_destroy(schema);
1325 do_execute(struct ovs_cmdl_context *ctx)
1327 struct ovsdb_schema *schema;
1332 /* Create database. */
1333 json = parse_json(ctx->argv[1]);
1334 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1336 db = ovsdb_create(schema);
1338 for (i = 2; i < ctx->argc; i++) {
1339 struct json *params, *result;
1342 params = parse_json(ctx->argv[i]);
1343 result = ovsdb_execute(db, NULL, params, 0, NULL);
1344 s = json_to_string(result, JSSF_SORT);
1347 json_destroy(params);
1348 json_destroy(result);
1354 struct test_trigger {
1355 struct ovsdb_trigger trigger;
1360 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1362 struct json *result;
1365 result = ovsdb_trigger_steal_result(&t->trigger);
1366 s = json_to_string(result, JSSF_SORT);
1367 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1369 json_destroy(result);
1370 ovsdb_trigger_destroy(&t->trigger);
1375 do_trigger(struct ovs_cmdl_context *ctx)
1377 struct ovsdb_schema *schema;
1378 struct ovsdb_session session;
1379 struct ovsdb_server server;
1386 /* Create database. */
1387 json = parse_json(ctx->argv[1]);
1388 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1390 db = ovsdb_create(schema);
1392 ovsdb_server_init(&server);
1393 ovsdb_server_add_db(&server, db);
1394 ovsdb_session_init(&session, &server);
1398 for (i = 2; i < ctx->argc; i++) {
1399 struct json *params = parse_json(ctx->argv[i]);
1400 if (params->type == JSON_ARRAY
1401 && json_array(params)->n == 2
1402 && json_array(params)->elems[0]->type == JSON_STRING
1403 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1404 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1405 now += json_integer(json_array(params)->elems[1]);
1406 json_destroy(params);
1408 struct test_trigger *t = xmalloc(sizeof *t);
1409 ovsdb_trigger_init(&session, db, &t->trigger, params, now);
1410 t->number = number++;
1411 if (ovsdb_trigger_is_complete(&t->trigger)) {
1412 do_trigger_dump(t, now, "immediate");
1414 printf("t=%lld: new trigger %d\n", now, t->number);
1418 ovsdb_trigger_run(db, now);
1419 while (!ovs_list_is_empty(&session.completions)) {
1420 do_trigger_dump(CONTAINER_OF(ovs_list_pop_front(&session.completions),
1421 struct test_trigger, trigger.node),
1425 ovsdb_trigger_wait(db, now);
1426 poll_immediate_wake();
1430 ovsdb_server_destroy(&server);
1435 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
1440 /* "transact" command. */
1442 static struct ovsdb *do_transact_db;
1443 static struct ovsdb_txn *do_transact_txn;
1444 static struct ovsdb_table *do_transact_table;
1447 do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED)
1449 ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
1450 do_transact_txn = NULL;
1454 do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED)
1456 ovsdb_txn_abort(do_transact_txn);
1457 do_transact_txn = NULL;
1461 uuid_from_integer(int integer, struct uuid *uuid)
1464 uuid->parts[3] = integer;
1467 static const struct ovsdb_row *
1468 do_transact_find_row(const char *uuid_string)
1470 const struct ovsdb_row *row;
1473 uuid_from_integer(atoi(uuid_string), &uuid);
1474 row = ovsdb_table_get_row(do_transact_table, &uuid);
1476 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1483 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1486 if (integer != -1) {
1487 const struct ovsdb_column *column;
1489 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1491 row->fields[column->index].keys[0].integer = integer;
1496 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1498 const struct ovsdb_column *column;
1500 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1502 return row->fields[column->index].keys[0].integer;
1506 do_transact_set_i_j(struct ovsdb_row *row,
1507 const char *i_string, const char *j_string)
1509 do_transact_set_integer(row, "i", atoi(i_string));
1510 do_transact_set_integer(row, "j", atoi(j_string));
1514 do_transact_insert(struct ovs_cmdl_context *ctx)
1516 struct ovsdb_row *row;
1519 row = ovsdb_row_create(do_transact_table);
1522 uuid = ovsdb_row_get_uuid_rw(row);
1523 uuid_from_integer(atoi(ctx->argv[1]), uuid);
1524 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1525 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1529 do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]);
1532 ovsdb_txn_row_insert(do_transact_txn, row);
1536 do_transact_delete(struct ovs_cmdl_context *ctx)
1538 const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]);
1539 ovsdb_txn_row_delete(do_transact_txn, row);
1543 do_transact_modify(struct ovs_cmdl_context *ctx)
1545 const struct ovsdb_row *row_ro;
1546 struct ovsdb_row *row_rw;
1548 row_ro = do_transact_find_row(ctx->argv[1]);
1549 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1550 do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]);
1554 compare_rows_by_uuid(const void *a_, const void *b_)
1556 struct ovsdb_row *const *ap = a_;
1557 struct ovsdb_row *const *bp = b_;
1559 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1563 do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED)
1565 const struct ovsdb_row **rows;
1566 const struct ovsdb_row *row;
1570 n_rows = hmap_count(&do_transact_table->rows);
1571 rows = xmalloc(n_rows * sizeof *rows);
1573 HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
1576 assert(i == n_rows);
1578 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1580 for (i = 0; i < n_rows; i++) {
1581 printf("\n%"PRId32": i=%d, j=%d",
1582 ovsdb_row_get_uuid(rows[i])->parts[3],
1583 do_transact_get_integer(rows[i], "i"),
1584 do_transact_get_integer(rows[i], "j"));
1591 do_transact(struct ovs_cmdl_context *ctx)
1593 static const struct ovs_cmdl_command do_transact_commands[] = {
1594 { "commit", NULL, 0, 0, do_transact_commit },
1595 { "abort", NULL, 0, 0, do_transact_abort },
1596 { "insert", NULL, 2, 3, do_transact_insert },
1597 { "delete", NULL, 1, 1, do_transact_delete },
1598 { "modify", NULL, 2, 3, do_transact_modify },
1599 { "print", NULL, 0, 0, do_transact_print },
1600 { NULL, NULL, 0, 0, NULL },
1603 struct ovsdb_schema *schema;
1608 json = parse_json("{\"name\": \"testdb\", "
1612 " {\"i\": {\"type\": \"integer\"}, "
1613 " \"j\": {\"type\": \"integer\"}}}}}");
1614 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1616 do_transact_db = ovsdb_create(schema);
1617 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1618 assert(do_transact_table != NULL);
1620 for (i = 1; i < ctx->argc; i++) {
1621 struct json *command;
1625 struct ovs_cmdl_context transact_ctx = { .argc = 0, };
1627 command = parse_json(ctx->argv[i]);
1628 if (command->type != JSON_ARRAY) {
1629 ovs_fatal(0, "transaction %d must be JSON array "
1630 "with at least 1 element", i);
1633 n_args = command->u.array.n;
1634 args = xmalloc((n_args + 1) * sizeof *args);
1635 for (j = 0; j < n_args; j++) {
1636 struct json *s = command->u.array.elems[j];
1637 if (s->type != JSON_STRING) {
1638 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1641 args[j] = xstrdup(json_string(s));
1643 args[n_args] = NULL;
1645 if (!do_transact_txn) {
1646 do_transact_txn = ovsdb_txn_create(do_transact_db);
1649 for (j = 0; j < n_args; j++) {
1653 fputs(args[j], stdout);
1656 transact_ctx.argc = n_args;
1657 transact_ctx.argv = args;
1658 ovs_cmdl_run_command(&transact_ctx, do_transact_commands);
1661 for (j = 0; j < n_args; j++) {
1665 json_destroy(command);
1667 ovsdb_txn_abort(do_transact_txn);
1668 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1672 compare_link1(const void *a_, const void *b_)
1674 const struct idltest_link1 *const *ap = a_;
1675 const struct idltest_link1 *const *bp = b_;
1676 const struct idltest_link1 *a = *ap;
1677 const struct idltest_link1 *b = *bp;
1679 return a->i < b->i ? -1 : a->i > b->i;
1683 print_idl_row_updated_simple(const struct idltest_simple *s, int step)
1686 bool updated = false;
1688 for (i = 0; i < IDLTEST_SIMPLE_N_COLUMNS; i++) {
1689 if (idltest_simple_is_updated(s, i)) {
1691 printf("%03d: updated columns:", step);
1694 printf(" %s", idltest_simple_columns[i].name);
1703 print_idl_row_updated_link1(const struct idltest_link1 *l1, int step)
1706 bool updated = false;
1708 for (i = 0; i < IDLTEST_LINK1_N_COLUMNS; i++) {
1709 if (idltest_link1_is_updated(l1, i)) {
1711 printf("%03d: updated columns:", step);
1714 printf(" %s", idltest_link1_columns[i].name);
1723 print_idl_row_updated_link2(const struct idltest_link2 *l2, int step)
1726 bool updated = false;
1728 for (i = 0; i < IDLTEST_LINK2_N_COLUMNS; i++) {
1729 if (idltest_link2_is_updated(l2, i)) {
1731 printf("%03d: updated columns:", step);
1734 printf(" %s", idltest_link2_columns[i].name);
1743 print_idl_row_simple(const struct idltest_simple *s, int step)
1747 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1748 step, s->i, s->r, s->b ? "true" : "false",
1749 s->s, UUID_ARGS(&s->u));
1750 for (i = 0; i < s->n_ia; i++) {
1751 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1754 for (i = 0; i < s->n_ra; i++) {
1755 printf("%s%g", i ? " " : "", s->ra[i]);
1758 for (i = 0; i < s->n_ba; i++) {
1759 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1762 for (i = 0; i < s->n_sa; i++) {
1763 printf("%s%s", i ? " " : "", s->sa[i]);
1766 for (i = 0; i < s->n_ua; i++) {
1767 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1769 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1770 print_idl_row_updated_simple(s, step);
1774 print_idl_row_link1(const struct idltest_link1 *l1, int step)
1776 struct idltest_link1 **links;
1779 printf("%03d: i=%"PRId64" k=", step, l1->i);
1781 printf("%"PRId64, l1->k->i);
1784 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1785 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1786 for (i = 0; i < l1->n_ka; i++) {
1787 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1792 printf("%"PRId64, l1->l2->i);
1794 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1795 print_idl_row_updated_link1(l1, step);
1799 print_idl_row_link2(const struct idltest_link2 *l2, int step)
1801 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1803 printf("%"PRId64, l2->l1->i);
1805 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1806 print_idl_row_updated_link2(l2, step);
1810 print_idl(struct ovsdb_idl *idl, int step)
1812 const struct idltest_simple *s;
1813 const struct idltest_link1 *l1;
1814 const struct idltest_link2 *l2;
1817 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1818 print_idl_row_simple(s, step);
1821 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1822 print_idl_row_link1(l1, step);
1825 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1826 print_idl_row_link2(l2, step);
1830 printf("%03d: empty\n", step);
1835 print_idl_track(struct ovsdb_idl *idl, int step, unsigned int seqno)
1837 const struct idltest_simple *s;
1838 const struct idltest_link1 *l1;
1839 const struct idltest_link2 *l2;
1842 IDLTEST_SIMPLE_FOR_EACH_TRACKED (s, idl) {
1843 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1844 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1846 print_idl_row_simple(s, step);
1850 IDLTEST_LINK1_FOR_EACH_TRACKED (l1, idl) {
1851 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1852 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1854 print_idl_row_link1(l1, step);
1858 IDLTEST_LINK2_FOR_EACH_TRACKED (l2, idl) {
1859 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1860 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1862 print_idl_row_link2(l2, step);
1867 printf("%03d: empty\n", step);
1872 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1877 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1878 char *name = xasprintf("#%"PRIuSIZE"#", *n);
1879 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1880 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1883 } else if (json->type == JSON_ARRAY) {
1886 for (i = 0; i < json->u.array.n; i++) {
1887 parse_uuids(json->u.array.elems[i], symtab, n);
1889 } else if (json->type == JSON_OBJECT) {
1890 const struct shash_node *node;
1892 SHASH_FOR_EACH (node, json_object(json)) {
1893 parse_uuids(node->data, symtab, n);
1899 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1901 if (json->type == JSON_STRING) {
1902 const struct ovsdb_symbol *symbol;
1904 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1906 free(json->u.string);
1907 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1909 } else if (json->type == JSON_ARRAY) {
1912 for (i = 0; i < json->u.array.n; i++) {
1913 substitute_uuids(json->u.array.elems[i], symtab);
1915 } else if (json->type == JSON_OBJECT) {
1916 const struct shash_node *node;
1918 SHASH_FOR_EACH (node, json_object(json)) {
1919 substitute_uuids(node->data, symtab);
1924 static const struct idltest_simple *
1925 idltest_find_simple(struct ovsdb_idl *idl, int i)
1927 const struct idltest_simple *s;
1929 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1938 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1940 char *cmd, *save_ptr1 = NULL;
1941 struct ovsdb_idl_txn *txn;
1942 enum ovsdb_idl_txn_status status;
1943 bool increment = false;
1945 txn = ovsdb_idl_txn_create(idl);
1946 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1947 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1948 char *save_ptr2 = NULL;
1949 char *name, *arg1, *arg2, *arg3;
1951 name = strtok_r(cmd, " ", &save_ptr2);
1952 arg1 = strtok_r(NULL, " ", &save_ptr2);
1953 arg2 = strtok_r(NULL, " ", &save_ptr2);
1954 arg3 = strtok_r(NULL, " ", &save_ptr2);
1956 if (!strcmp(name, "set")) {
1957 const struct idltest_simple *s;
1960 ovs_fatal(0, "\"set\" command requires 3 arguments");
1963 s = idltest_find_simple(idl, atoi(arg1));
1965 ovs_fatal(0, "\"set\" command asks for nonexistent "
1966 "i=%d", atoi(arg1));
1969 if (!strcmp(arg2, "b")) {
1970 idltest_simple_set_b(s, atoi(arg3));
1971 } else if (!strcmp(arg2, "s")) {
1972 idltest_simple_set_s(s, arg3);
1973 } else if (!strcmp(arg2, "u")) {
1975 if (!uuid_from_string(&uuid, arg3)) {
1976 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
1978 idltest_simple_set_u(s, uuid);
1979 } else if (!strcmp(arg2, "r")) {
1980 idltest_simple_set_r(s, atof(arg3));
1982 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1985 } else if (!strcmp(name, "insert")) {
1986 struct idltest_simple *s;
1988 if (!arg1 || arg2) {
1989 ovs_fatal(0, "\"insert\" command requires 1 argument");
1992 s = idltest_simple_insert(txn);
1993 idltest_simple_set_i(s, atoi(arg1));
1994 } else if (!strcmp(name, "delete")) {
1995 const struct idltest_simple *s;
1997 if (!arg1 || arg2) {
1998 ovs_fatal(0, "\"delete\" command requires 1 argument");
2001 s = idltest_find_simple(idl, atoi(arg1));
2003 ovs_fatal(0, "\"delete\" command asks for nonexistent "
2004 "i=%d", atoi(arg1));
2006 idltest_simple_delete(s);
2007 } else if (!strcmp(name, "verify")) {
2008 const struct idltest_simple *s;
2010 if (!arg2 || arg3) {
2011 ovs_fatal(0, "\"verify\" command requires 2 arguments");
2014 s = idltest_find_simple(idl, atoi(arg1));
2016 ovs_fatal(0, "\"verify\" command asks for nonexistent "
2017 "i=%d", atoi(arg1));
2020 if (!strcmp(arg2, "i")) {
2021 idltest_simple_verify_i(s);
2022 } else if (!strcmp(arg2, "b")) {
2023 idltest_simple_verify_b(s);
2024 } else if (!strcmp(arg2, "s")) {
2025 idltest_simple_verify_s(s);
2026 } else if (!strcmp(arg2, "u")) {
2027 idltest_simple_verify_s(s);
2028 } else if (!strcmp(arg2, "r")) {
2029 idltest_simple_verify_r(s);
2031 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
2034 } else if (!strcmp(name, "increment")) {
2035 const struct idltest_simple *s;
2037 if (!arg1 || arg2) {
2038 ovs_fatal(0, "\"increment\" command requires 1 argument");
2041 s = idltest_find_simple(idl, atoi(arg1));
2043 ovs_fatal(0, "\"set\" command asks for nonexistent "
2044 "i=%d", atoi(arg1));
2047 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i);
2049 } else if (!strcmp(name, "abort")) {
2050 ovsdb_idl_txn_abort(txn);
2052 } else if (!strcmp(name, "destroy")) {
2053 printf("%03d: destroy\n", step);
2054 ovsdb_idl_txn_destroy(txn);
2057 ovs_fatal(0, "unknown command %s", name);
2061 status = ovsdb_idl_txn_commit_block(txn);
2062 printf("%03d: commit, status=%s",
2063 step, ovsdb_idl_txn_status_to_string(status));
2065 printf(", increment=%"PRId64,
2066 ovsdb_idl_txn_get_increment_new_value(txn));
2069 ovsdb_idl_txn_destroy(txn);
2073 do_idl(struct ovs_cmdl_context *ctx)
2075 struct jsonrpc *rpc;
2076 struct ovsdb_idl *idl;
2077 unsigned int seqno = 0;
2078 struct ovsdb_symbol_table *symtab;
2087 track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
2089 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
2090 if (ctx->argc > 2) {
2091 struct stream *stream;
2093 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
2094 DSCP_DEFAULT), &stream);
2096 ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
2098 rpc = jsonrpc_open(stream);
2104 ovsdb_idl_track_add_all(idl);
2107 setvbuf(stdout, NULL, _IONBF, 0);
2109 symtab = ovsdb_symbol_table_create();
2110 for (i = 2; i < ctx->argc; i++) {
2111 char *arg = ctx->argv[i];
2112 struct jsonrpc_msg *request, *reply;
2115 /* The previous transaction didn't change anything. */
2118 /* Wait for update. */
2121 if (ovsdb_idl_get_seqno(idl) != seqno) {
2126 ovsdb_idl_wait(idl);
2133 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2134 ovsdb_idl_track_clear(idl);
2136 print_idl(idl, step++);
2139 seqno = ovsdb_idl_get_seqno(idl);
2141 if (!strcmp(arg, "reconnect")) {
2142 printf("%03d: reconnect\n", step++);
2143 ovsdb_idl_force_reconnect(idl);
2144 } else if (arg[0] != '[') {
2145 idl_set(idl, arg, step++);
2147 struct json *json = parse_json(arg);
2148 substitute_uuids(json, symtab);
2149 request = jsonrpc_create_request("transact", json, NULL);
2150 error = jsonrpc_transact_block(rpc, request, &reply);
2151 if (error || reply->error) {
2152 ovs_fatal(error, "jsonrpc transaction failed");
2154 printf("%03d: ", step++);
2155 if (reply->result) {
2156 parse_uuids(reply->result, symtab, &n_uuids);
2158 json_destroy(reply->id);
2160 print_and_free_json(jsonrpc_msg_to_json(reply));
2163 ovsdb_symbol_table_destroy(symtab);
2170 if (ovsdb_idl_get_seqno(idl) != seqno) {
2173 ovsdb_idl_wait(idl);
2176 print_idl(idl, step++);
2177 ovsdb_idl_track_clear(idl);
2178 ovsdb_idl_destroy(idl);
2179 printf("%03d: done\n", step);
2182 static struct ovs_cmdl_command all_commands[] = {
2183 { "log-io", NULL, 2, INT_MAX, do_log_io },
2184 { "default-atoms", NULL, 0, 0, do_default_atoms },
2185 { "default-data", NULL, 0, 0, do_default_data },
2186 { "diff-data", NULL, 3, INT_MAX, do_diff_data},
2187 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
2188 { "parse-base-type", NULL, 1, 1, do_parse_base_type },
2189 { "parse-type", NULL, 1, 1, do_parse_type },
2190 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
2191 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
2192 { "parse-data", NULL, 2, INT_MAX, do_parse_data },
2193 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
2194 { "sort-atoms", NULL, 2, 2, do_sort_atoms },
2195 { "parse-column", NULL, 2, 2, do_parse_column },
2196 { "parse-table", NULL, 2, 3, do_parse_table },
2197 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
2198 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
2199 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
2200 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
2201 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
2202 { "execute-mutations", NULL, 3, 3, do_execute_mutations },
2203 { "query", NULL, 3, 3, do_query },
2204 { "query-distinct", NULL, 4, 4, do_query_distinct },
2205 { "transact", NULL, 1, INT_MAX, do_transact },
2206 { "parse-schema", NULL, 1, 1, do_parse_schema },
2207 { "execute", NULL, 2, INT_MAX, do_execute },
2208 { "trigger", NULL, 2, INT_MAX, do_trigger },
2209 { "idl", NULL, 1, INT_MAX, do_idl },
2210 { "help", NULL, 0, INT_MAX, do_help },
2211 { NULL, NULL, 0, 0, NULL },
2214 static struct ovs_cmdl_command *
2215 get_all_commands(void)
2217 return all_commands;