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 "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_diff_data(struct ovs_cmdl_context *ctx)
403 struct ovsdb_type type;
405 struct ovsdb_datum new, old, diff, reincarnation;
407 json = unbox_json(parse_json(ctx->argv[1]));
408 check_ovsdb_error(ovsdb_type_from_json(&type, json));
411 /* Arguments in pairs of 'old' and 'new'. */
412 for (int i = 2; i < ctx->argc - 1; i+=2) {
413 struct ovsdb_error *error;
415 json = unbox_json(parse_json(ctx->argv[i]));
416 check_ovsdb_error(ovsdb_datum_from_json(&old, &type, json, NULL));
419 json = unbox_json(parse_json(ctx->argv[i+1]));
420 check_ovsdb_error(ovsdb_transient_datum_from_json(&new, &type, json));
423 /* Generate the diff. */
424 ovsdb_datum_diff(&diff, &old, &new, &type);
426 /* Apply diff to 'old' to create'reincarnation'. */
427 error = ovsdb_datum_apply_diff(&reincarnation, &old, &diff, &type);
429 char *string = ovsdb_error_to_string(error);
430 ovsdb_error_destroy(error);
431 ovs_fatal(0, "%s", string);
434 /* Test to make sure 'new' equals 'reincarnation'. */
435 if (!ovsdb_datum_equals(&new, &reincarnation, &type)) {
436 ovs_fatal(0, "failed to apply diff");
440 json = ovsdb_datum_to_json(&diff, &type);
442 print_and_free_json(json);
444 /* Print reincarnation */
445 json = ovsdb_datum_to_json(&reincarnation, &type);
446 printf ("apply diff: ");
447 print_and_free_json(json);
449 ovsdb_datum_destroy(&new, &type);
450 ovsdb_datum_destroy(&old, &type);
451 ovsdb_datum_destroy(&diff, &type);
452 ovsdb_datum_destroy(&reincarnation, &type);
457 ovsdb_type_destroy(&type);
461 do_parse_atomic_type(struct ovs_cmdl_context *ctx)
463 enum ovsdb_atomic_type type;
466 json = unbox_json(parse_json(ctx->argv[1]));
467 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
469 print_and_free_json(ovsdb_atomic_type_to_json(type));
473 do_parse_base_type(struct ovs_cmdl_context *ctx)
475 struct ovsdb_base_type base;
478 json = unbox_json(parse_json(ctx->argv[1]));
479 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
481 print_and_free_json(ovsdb_base_type_to_json(&base));
482 ovsdb_base_type_destroy(&base);
486 do_parse_type(struct ovs_cmdl_context *ctx)
488 struct ovsdb_type type;
491 json = unbox_json(parse_json(ctx->argv[1]));
492 check_ovsdb_error(ovsdb_type_from_json(&type, json));
494 print_and_free_json(ovsdb_type_to_json(&type));
495 ovsdb_type_destroy(&type);
499 do_parse_atoms(struct ovs_cmdl_context *ctx)
501 struct ovsdb_base_type base;
505 json = unbox_json(parse_json(ctx->argv[1]));
506 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
509 for (i = 2; i < ctx->argc; i++) {
510 struct ovsdb_error *error;
511 union ovsdb_atom atom;
513 json = unbox_json(parse_json(ctx->argv[i]));
514 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
518 print_and_free_ovsdb_error(error);
520 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
521 ovsdb_atom_destroy(&atom, base.type);
524 ovsdb_base_type_destroy(&base);
528 do_parse_atom_strings(struct ovs_cmdl_context *ctx)
530 struct ovsdb_base_type base;
534 json = unbox_json(parse_json(ctx->argv[1]));
535 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
538 for (i = 2; i < ctx->argc; i++) {
539 union ovsdb_atom atom;
542 die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL));
545 ovsdb_atom_to_string(&atom, base.type, &out);
549 ovsdb_atom_destroy(&atom, base.type);
551 ovsdb_base_type_destroy(&base);
555 do_parse_data__(int argc, char *argv[],
557 (*parse)(struct ovsdb_datum *datum,
558 const struct ovsdb_type *type,
559 const struct json *json,
560 struct ovsdb_symbol_table *symtab))
562 struct ovsdb_type type;
566 json = unbox_json(parse_json(argv[1]));
567 check_ovsdb_error(ovsdb_type_from_json(&type, json));
570 for (i = 2; i < argc; i++) {
571 struct ovsdb_datum datum;
573 json = unbox_json(parse_json(argv[i]));
574 check_ovsdb_error(parse(&datum, &type, json, NULL));
577 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
579 ovsdb_datum_destroy(&datum, &type);
581 ovsdb_type_destroy(&type);
585 do_parse_data(struct ovs_cmdl_context *ctx)
587 do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json);
591 do_parse_data_strings(struct ovs_cmdl_context *ctx)
593 struct ovsdb_type type;
597 json = unbox_json(parse_json(ctx->argv[1]));
598 check_ovsdb_error(ovsdb_type_from_json(&type, json));
601 for (i = 2; i < ctx->argc; i++) {
602 struct ovsdb_datum datum;
605 die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL));
608 ovsdb_datum_to_string(&datum, &type, &out);
612 ovsdb_datum_destroy(&datum, &type);
614 ovsdb_type_destroy(&type);
617 static enum ovsdb_atomic_type compare_atoms_atomic_type;
620 compare_atoms(const void *a_, const void *b_)
622 const union ovsdb_atom *a = a_;
623 const union ovsdb_atom *b = b_;
625 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
629 do_sort_atoms(struct ovs_cmdl_context *ctx)
631 struct ovsdb_base_type base;
632 union ovsdb_atom *atoms;
633 struct json *json, **json_atoms;
637 json = unbox_json(parse_json(ctx->argv[1]));
638 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
641 json = unbox_json(parse_json(ctx->argv[2]));
642 if (json->type != JSON_ARRAY) {
643 ovs_fatal(0, "second argument must be array");
646 /* Convert JSON atoms to internal representation. */
647 n_atoms = json->u.array.n;
648 atoms = xmalloc(n_atoms * sizeof *atoms);
649 for (i = 0; i < n_atoms; i++) {
650 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
651 json->u.array.elems[i], NULL));
656 compare_atoms_atomic_type = base.type;
657 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
659 /* Convert internal representation back to JSON. */
660 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
661 for (i = 0; i < n_atoms; i++) {
662 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
663 ovsdb_atom_destroy(&atoms[i], base.type);
665 print_and_free_json(json_array_create(json_atoms, n_atoms));
667 ovsdb_base_type_destroy(&base);
671 do_parse_column(struct ovs_cmdl_context *ctx)
673 struct ovsdb_column *column;
676 json = parse_json(ctx->argv[2]);
677 check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column));
679 print_and_free_json(ovsdb_column_to_json(column));
680 ovsdb_column_destroy(column);
684 do_parse_table(struct ovs_cmdl_context *ctx)
686 struct ovsdb_table_schema *ts;
687 bool default_is_root;
690 default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true");
692 json = parse_json(ctx->argv[2]);
693 check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts));
695 print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
696 ovsdb_table_schema_destroy(ts);
700 do_parse_rows(struct ovs_cmdl_context *ctx)
702 struct ovsdb_column_set all_columns;
703 struct ovsdb_table_schema *ts;
704 struct ovsdb_table *table;
708 json = unbox_json(parse_json(ctx->argv[1]));
709 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
712 table = ovsdb_table_create(ts);
713 ovsdb_column_set_init(&all_columns);
714 ovsdb_column_set_add_all(&all_columns, table);
716 for (i = 2; i < ctx->argc; i++) {
717 struct ovsdb_column_set columns;
718 struct ovsdb_row *row;
720 ovsdb_column_set_init(&columns);
721 row = ovsdb_row_create(table);
723 json = unbox_json(parse_json(ctx->argv[i]));
724 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
727 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
729 if (columns.n_columns) {
735 for (j = 0; j < columns.n_columns; j++) {
736 svec_add(&names, columns.columns[j]->name);
739 s = svec_join(&names, ", ", "");
742 svec_destroy(&names);
747 ovsdb_column_set_destroy(&columns);
748 ovsdb_row_destroy(row);
751 ovsdb_column_set_destroy(&all_columns);
752 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
756 do_compare_rows(struct ovs_cmdl_context *ctx)
758 struct ovsdb_column_set all_columns;
759 struct ovsdb_table_schema *ts;
760 struct ovsdb_table *table;
761 struct ovsdb_row **rows;
767 json = unbox_json(parse_json(ctx->argv[1]));
768 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
771 table = ovsdb_table_create(ts);
772 ovsdb_column_set_init(&all_columns);
773 ovsdb_column_set_add_all(&all_columns, table);
775 n_rows = ctx->argc - 2;
776 rows = xmalloc(sizeof *rows * n_rows);
777 names = xmalloc(sizeof *names * n_rows);
778 for (i = 0; i < n_rows; i++) {
779 rows[i] = ovsdb_row_create(table);
781 json = parse_json(ctx->argv[i + 2]);
782 if (json->type != JSON_ARRAY || json->u.array.n != 2
783 || json->u.array.elems[0]->type != JSON_STRING) {
784 ovs_fatal(0, "\"%s\" does not have expected form "
785 "[\"name\", {data}]", ctx->argv[i]);
787 names[i] = xstrdup(json->u.array.elems[0]->u.string);
788 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
792 for (i = 0; i < n_rows; i++) {
793 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
794 for (j = i + 1; j < n_rows; j++) {
795 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
796 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
797 printf("%s == %s\n", names[i], names[j]);
798 if (i_hash != j_hash) {
799 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
802 } else if (i_hash == j_hash) {
803 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
807 for (i = 0; i < n_rows; i++) {
808 ovsdb_row_destroy(rows[i]);
814 ovsdb_column_set_destroy(&all_columns);
815 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
819 do_parse_conditions(struct ovs_cmdl_context *ctx)
821 struct ovsdb_table_schema *ts;
826 json = unbox_json(parse_json(ctx->argv[1]));
827 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
830 for (i = 2; i < ctx->argc; i++) {
831 struct ovsdb_condition cnd;
832 struct ovsdb_error *error;
834 json = parse_json(ctx->argv[i]);
835 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
837 print_and_free_json(ovsdb_condition_to_json(&cnd));
839 char *s = ovsdb_error_to_string(error);
840 ovs_error(0, "%s", s);
842 ovsdb_error_destroy(error);
847 ovsdb_condition_destroy(&cnd);
849 ovsdb_table_schema_destroy(ts);
855 do_evaluate_conditions(struct ovs_cmdl_context *ctx)
857 struct ovsdb_table_schema *ts;
858 struct ovsdb_table *table;
859 struct ovsdb_condition *conditions;
861 struct ovsdb_row **rows;
866 /* Parse table schema, create table. */
867 json = unbox_json(parse_json(ctx->argv[1]));
868 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
871 table = ovsdb_table_create(ts);
873 /* Parse conditions. */
874 json = parse_json(ctx->argv[2]);
875 if (json->type != JSON_ARRAY) {
876 ovs_fatal(0, "CONDITION argument is not JSON array");
878 n_conditions = json->u.array.n;
879 conditions = xmalloc(n_conditions * sizeof *conditions);
880 for (i = 0; i < n_conditions; i++) {
881 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
882 NULL, &conditions[i]));
887 json = parse_json(ctx->argv[3]);
888 if (json->type != JSON_ARRAY) {
889 ovs_fatal(0, "ROW argument is not JSON array");
891 n_rows = json->u.array.n;
892 rows = xmalloc(n_rows * sizeof *rows);
893 for (i = 0; i < n_rows; i++) {
894 rows[i] = ovsdb_row_create(table);
895 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
900 for (i = 0; i < n_conditions; i++) {
901 printf("condition %2"PRIuSIZE":", i);
902 for (j = 0; j < n_rows; j++) {
903 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
907 putchar(result ? 'T' : '-');
912 for (i = 0; i < n_conditions; i++) {
913 ovsdb_condition_destroy(&conditions[i]);
916 for (i = 0; i < n_rows; i++) {
917 ovsdb_row_destroy(rows[i]);
920 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
924 do_parse_mutations(struct ovs_cmdl_context *ctx)
926 struct ovsdb_table_schema *ts;
931 json = unbox_json(parse_json(ctx->argv[1]));
932 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
935 for (i = 2; i < ctx->argc; i++) {
936 struct ovsdb_mutation_set set;
937 struct ovsdb_error *error;
939 json = parse_json(ctx->argv[i]);
940 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
942 print_and_free_json(ovsdb_mutation_set_to_json(&set));
944 char *s = ovsdb_error_to_string(error);
945 ovs_error(0, "%s", s);
947 ovsdb_error_destroy(error);
952 ovsdb_mutation_set_destroy(&set);
954 ovsdb_table_schema_destroy(ts);
960 do_execute_mutations(struct ovs_cmdl_context *ctx)
962 struct ovsdb_table_schema *ts;
963 struct ovsdb_table *table;
964 struct ovsdb_mutation_set *sets;
966 struct ovsdb_row **rows;
971 /* Parse table schema, create table. */
972 json = unbox_json(parse_json(ctx->argv[1]));
973 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
976 table = ovsdb_table_create(ts);
978 /* Parse mutations. */
979 json = parse_json(ctx->argv[2]);
980 if (json->type != JSON_ARRAY) {
981 ovs_fatal(0, "MUTATION argument is not JSON array");
983 n_sets = json->u.array.n;
984 sets = xmalloc(n_sets * sizeof *sets);
985 for (i = 0; i < n_sets; i++) {
986 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
987 json->u.array.elems[i],
993 json = parse_json(ctx->argv[3]);
994 if (json->type != JSON_ARRAY) {
995 ovs_fatal(0, "ROW argument is not JSON array");
997 n_rows = json->u.array.n;
998 rows = xmalloc(n_rows * sizeof *rows);
999 for (i = 0; i < n_rows; i++) {
1000 rows[i] = ovsdb_row_create(table);
1001 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
1006 for (i = 0; i < n_sets; i++) {
1007 printf("mutation %2"PRIuSIZE":\n", i);
1008 for (j = 0; j < n_rows; j++) {
1009 struct ovsdb_error *error;
1010 struct ovsdb_row *row;
1012 row = ovsdb_row_clone(rows[j]);
1013 error = ovsdb_mutation_set_execute(row, &sets[i]);
1015 printf("row %"PRIuSIZE": ", j);
1017 print_and_free_ovsdb_error(error);
1019 struct ovsdb_column_set columns;
1020 struct shash_node *node;
1022 ovsdb_column_set_init(&columns);
1023 SHASH_FOR_EACH (node, &ts->columns) {
1024 struct ovsdb_column *c = node->data;
1025 if (!ovsdb_datum_equals(&row->fields[c->index],
1026 &rows[j]->fields[c->index],
1028 ovsdb_column_set_add(&columns, c);
1031 if (columns.n_columns) {
1032 print_and_free_json(ovsdb_row_to_json(row, &columns));
1034 printf("no change\n");
1036 ovsdb_column_set_destroy(&columns);
1038 ovsdb_row_destroy(row);
1043 for (i = 0; i < n_sets; i++) {
1044 ovsdb_mutation_set_destroy(&sets[i]);
1047 for (i = 0; i < n_rows; i++) {
1048 ovsdb_row_destroy(rows[i]);
1051 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1054 /* Inserts a row, without bothering to update metadata such as refcounts. */
1056 put_row(struct ovsdb_table *table, struct ovsdb_row *row)
1058 const struct uuid *uuid = ovsdb_row_get_uuid(row);
1059 if (!ovsdb_table_get_row(table, uuid)) {
1060 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1064 struct do_query_cbdata {
1065 struct uuid *row_uuids;
1071 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
1073 struct do_query_cbdata *cbdata = cbdata_;
1076 for (i = 0; i < cbdata->n_rows; i++) {
1077 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1078 cbdata->counts[i]++;
1086 do_query(struct ovs_cmdl_context *ctx)
1088 struct do_query_cbdata cbdata;
1089 struct ovsdb_table_schema *ts;
1090 struct ovsdb_table *table;
1095 /* Parse table schema, create table. */
1096 json = unbox_json(parse_json(ctx->argv[1]));
1097 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1100 table = ovsdb_table_create(ts);
1102 /* Parse rows, add to table. */
1103 json = parse_json(ctx->argv[2]);
1104 if (json->type != JSON_ARRAY) {
1105 ovs_fatal(0, "ROW argument is not JSON array");
1107 cbdata.n_rows = json->u.array.n;
1108 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1109 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1110 for (i = 0; i < cbdata.n_rows; i++) {
1111 struct ovsdb_row *row = ovsdb_row_create(table);
1112 uuid_generate(ovsdb_row_get_uuid_rw(row));
1113 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1115 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1116 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1117 UUID_ARGS(ovsdb_row_get_uuid(row)));
1119 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1120 put_row(table, row);
1124 /* Parse conditions and execute queries. */
1125 json = parse_json(ctx->argv[3]);
1126 if (json->type != JSON_ARRAY) {
1127 ovs_fatal(0, "CONDITION argument is not JSON array");
1129 for (i = 0; i < json->u.array.n; i++) {
1130 struct ovsdb_condition cnd;
1133 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1136 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1137 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1139 printf("query %2"PRIuSIZE":", i);
1140 for (j = 0; j < cbdata.n_rows; j++) {
1144 if (cbdata.counts[j]) {
1145 printf("%d", cbdata.counts[j]);
1146 if (cbdata.counts[j] > 1) {
1156 ovsdb_condition_destroy(&cnd);
1160 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1165 struct do_query_distinct_class {
1166 struct ovsdb_row *example;
1170 struct do_query_distinct_row {
1172 struct do_query_distinct_class *class;
1176 do_query_distinct(struct ovs_cmdl_context *ctx)
1178 struct ovsdb_column_set columns;
1179 struct ovsdb_table_schema *ts;
1180 struct ovsdb_table *table;
1181 struct do_query_distinct_row *rows;
1183 struct do_query_distinct_class *classes;
1189 /* Parse table schema, create table. */
1190 json = unbox_json(parse_json(ctx->argv[1]));
1191 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1194 table = ovsdb_table_create(ts);
1196 /* Parse column set. */
1197 json = parse_json(ctx->argv[4]);
1198 check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1202 /* Parse rows, add to table. */
1203 json = parse_json(ctx->argv[2]);
1204 if (json->type != JSON_ARRAY) {
1205 ovs_fatal(0, "ROW argument is not JSON array");
1207 n_rows = json->u.array.n;
1208 rows = xmalloc(n_rows * sizeof *rows);
1209 classes = xmalloc(n_rows * sizeof *classes);
1211 for (i = 0; i < n_rows; i++) {
1212 struct ovsdb_row *row;
1216 row = ovsdb_row_create(table);
1217 uuid_generate(ovsdb_row_get_uuid_rw(row));
1218 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1221 /* Initialize row and find equivalence class. */
1222 rows[i].uuid = *ovsdb_row_get_uuid(row);
1223 rows[i].class = NULL;
1224 for (j = 0; j < n_classes; j++) {
1225 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1226 rows[i].class = &classes[j];
1230 if (!rows[i].class) {
1231 rows[i].class = &classes[n_classes];
1232 classes[n_classes].example = ovsdb_row_clone(row);
1236 /* Add row to table. */
1237 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1238 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1239 UUID_ARGS(ovsdb_row_get_uuid(row)));
1241 put_row(table, row);
1246 /* Parse conditions and execute queries. */
1247 json = parse_json(ctx->argv[3]);
1248 if (json->type != JSON_ARRAY) {
1249 ovs_fatal(0, "CONDITION argument is not JSON array");
1251 for (i = 0; i < json->u.array.n; i++) {
1252 struct ovsdb_row_set results;
1253 struct ovsdb_condition cnd;
1256 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1259 for (j = 0; j < n_classes; j++) {
1260 classes[j].count = 0;
1262 ovsdb_row_set_init(&results);
1263 ovsdb_query_distinct(table, &cnd, &columns, &results);
1264 for (j = 0; j < results.n_rows; j++) {
1267 for (k = 0; k < n_rows; k++) {
1268 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1270 rows[k].class->count++;
1274 ovsdb_row_set_destroy(&results);
1276 printf("query %2"PRIuSIZE":", i);
1277 for (j = 0; j < n_rows; j++) {
1278 int count = rows[j].class->count;
1285 printf("%d", count);
1287 } else if (count == 1) {
1288 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1295 ovsdb_condition_destroy(&cnd);
1299 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1307 do_parse_schema(struct ovs_cmdl_context *ctx)
1309 struct ovsdb_schema *schema;
1312 json = parse_json(ctx->argv[1]);
1313 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1315 print_and_free_json(ovsdb_schema_to_json(schema));
1316 ovsdb_schema_destroy(schema);
1320 do_execute(struct ovs_cmdl_context *ctx)
1322 struct ovsdb_schema *schema;
1327 /* Create database. */
1328 json = parse_json(ctx->argv[1]);
1329 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1331 db = ovsdb_create(schema);
1333 for (i = 2; i < ctx->argc; i++) {
1334 struct json *params, *result;
1337 params = parse_json(ctx->argv[i]);
1338 result = ovsdb_execute(db, NULL, params, 0, NULL);
1339 s = json_to_string(result, JSSF_SORT);
1342 json_destroy(params);
1343 json_destroy(result);
1349 struct test_trigger {
1350 struct ovsdb_trigger trigger;
1355 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1357 struct json *result;
1360 result = ovsdb_trigger_steal_result(&t->trigger);
1361 s = json_to_string(result, JSSF_SORT);
1362 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1364 json_destroy(result);
1365 ovsdb_trigger_destroy(&t->trigger);
1370 do_trigger(struct ovs_cmdl_context *ctx)
1372 struct ovsdb_schema *schema;
1373 struct ovsdb_session session;
1374 struct ovsdb_server server;
1381 /* Create database. */
1382 json = parse_json(ctx->argv[1]);
1383 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1385 db = ovsdb_create(schema);
1387 ovsdb_server_init(&server);
1388 ovsdb_server_add_db(&server, db);
1389 ovsdb_session_init(&session, &server);
1393 for (i = 2; i < ctx->argc; i++) {
1394 struct json *params = parse_json(ctx->argv[i]);
1395 if (params->type == JSON_ARRAY
1396 && json_array(params)->n == 2
1397 && json_array(params)->elems[0]->type == JSON_STRING
1398 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1399 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1400 now += json_integer(json_array(params)->elems[1]);
1401 json_destroy(params);
1403 struct test_trigger *t = xmalloc(sizeof *t);
1404 ovsdb_trigger_init(&session, db, &t->trigger, params, now);
1405 t->number = number++;
1406 if (ovsdb_trigger_is_complete(&t->trigger)) {
1407 do_trigger_dump(t, now, "immediate");
1409 printf("t=%lld: new trigger %d\n", now, t->number);
1413 ovsdb_trigger_run(db, now);
1414 while (!list_is_empty(&session.completions)) {
1415 do_trigger_dump(CONTAINER_OF(list_pop_front(&session.completions),
1416 struct test_trigger, trigger.node),
1420 ovsdb_trigger_wait(db, now);
1421 poll_immediate_wake();
1425 ovsdb_server_destroy(&server);
1430 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
1435 /* "transact" command. */
1437 static struct ovsdb *do_transact_db;
1438 static struct ovsdb_txn *do_transact_txn;
1439 static struct ovsdb_table *do_transact_table;
1442 do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED)
1444 ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
1445 do_transact_txn = NULL;
1449 do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED)
1451 ovsdb_txn_abort(do_transact_txn);
1452 do_transact_txn = NULL;
1456 uuid_from_integer(int integer, struct uuid *uuid)
1459 uuid->parts[3] = integer;
1462 static const struct ovsdb_row *
1463 do_transact_find_row(const char *uuid_string)
1465 const struct ovsdb_row *row;
1468 uuid_from_integer(atoi(uuid_string), &uuid);
1469 row = ovsdb_table_get_row(do_transact_table, &uuid);
1471 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1478 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1481 if (integer != -1) {
1482 const struct ovsdb_column *column;
1484 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1486 row->fields[column->index].keys[0].integer = integer;
1491 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1493 const struct ovsdb_column *column;
1495 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1497 return row->fields[column->index].keys[0].integer;
1501 do_transact_set_i_j(struct ovsdb_row *row,
1502 const char *i_string, const char *j_string)
1504 do_transact_set_integer(row, "i", atoi(i_string));
1505 do_transact_set_integer(row, "j", atoi(j_string));
1509 do_transact_insert(struct ovs_cmdl_context *ctx)
1511 struct ovsdb_row *row;
1514 row = ovsdb_row_create(do_transact_table);
1517 uuid = ovsdb_row_get_uuid_rw(row);
1518 uuid_from_integer(atoi(ctx->argv[1]), uuid);
1519 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1520 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1524 do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]);
1527 ovsdb_txn_row_insert(do_transact_txn, row);
1531 do_transact_delete(struct ovs_cmdl_context *ctx)
1533 const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]);
1534 ovsdb_txn_row_delete(do_transact_txn, row);
1538 do_transact_modify(struct ovs_cmdl_context *ctx)
1540 const struct ovsdb_row *row_ro;
1541 struct ovsdb_row *row_rw;
1543 row_ro = do_transact_find_row(ctx->argv[1]);
1544 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1545 do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]);
1549 compare_rows_by_uuid(const void *a_, const void *b_)
1551 struct ovsdb_row *const *ap = a_;
1552 struct ovsdb_row *const *bp = b_;
1554 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1558 do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED)
1560 const struct ovsdb_row **rows;
1561 const struct ovsdb_row *row;
1565 n_rows = hmap_count(&do_transact_table->rows);
1566 rows = xmalloc(n_rows * sizeof *rows);
1568 HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
1571 assert(i == n_rows);
1573 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1575 for (i = 0; i < n_rows; i++) {
1576 printf("\n%"PRId32": i=%d, j=%d",
1577 ovsdb_row_get_uuid(rows[i])->parts[3],
1578 do_transact_get_integer(rows[i], "i"),
1579 do_transact_get_integer(rows[i], "j"));
1586 do_transact(struct ovs_cmdl_context *ctx)
1588 static const struct ovs_cmdl_command do_transact_commands[] = {
1589 { "commit", NULL, 0, 0, do_transact_commit },
1590 { "abort", NULL, 0, 0, do_transact_abort },
1591 { "insert", NULL, 2, 3, do_transact_insert },
1592 { "delete", NULL, 1, 1, do_transact_delete },
1593 { "modify", NULL, 2, 3, do_transact_modify },
1594 { "print", NULL, 0, 0, do_transact_print },
1595 { NULL, NULL, 0, 0, NULL },
1598 struct ovsdb_schema *schema;
1603 json = parse_json("{\"name\": \"testdb\", "
1607 " {\"i\": {\"type\": \"integer\"}, "
1608 " \"j\": {\"type\": \"integer\"}}}}}");
1609 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1611 do_transact_db = ovsdb_create(schema);
1612 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1613 assert(do_transact_table != NULL);
1615 for (i = 1; i < ctx->argc; i++) {
1616 struct json *command;
1620 struct ovs_cmdl_context transact_ctx = { .argc = 0, };
1622 command = parse_json(ctx->argv[i]);
1623 if (command->type != JSON_ARRAY) {
1624 ovs_fatal(0, "transaction %d must be JSON array "
1625 "with at least 1 element", i);
1628 n_args = command->u.array.n;
1629 args = xmalloc((n_args + 1) * sizeof *args);
1630 for (j = 0; j < n_args; j++) {
1631 struct json *s = command->u.array.elems[j];
1632 if (s->type != JSON_STRING) {
1633 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1636 args[j] = xstrdup(json_string(s));
1638 args[n_args] = NULL;
1640 if (!do_transact_txn) {
1641 do_transact_txn = ovsdb_txn_create(do_transact_db);
1644 for (j = 0; j < n_args; j++) {
1648 fputs(args[j], stdout);
1651 transact_ctx.argc = n_args;
1652 transact_ctx.argv = args;
1653 ovs_cmdl_run_command(&transact_ctx, do_transact_commands);
1656 for (j = 0; j < n_args; j++) {
1660 json_destroy(command);
1662 ovsdb_txn_abort(do_transact_txn);
1663 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1667 compare_link1(const void *a_, const void *b_)
1669 const struct idltest_link1 *const *ap = a_;
1670 const struct idltest_link1 *const *bp = b_;
1671 const struct idltest_link1 *a = *ap;
1672 const struct idltest_link1 *b = *bp;
1674 return a->i < b->i ? -1 : a->i > b->i;
1678 print_idl_row_updated_simple(const struct idltest_simple *s, int step)
1681 bool updated = false;
1683 for (i = 0; i < IDLTEST_SIMPLE_N_COLUMNS; i++) {
1684 if (idltest_simple_is_updated(s, i)) {
1686 printf("%03d: updated columns:", step);
1689 printf(" %s", idltest_simple_columns[i].name);
1698 print_idl_row_updated_link1(const struct idltest_link1 *l1, int step)
1701 bool updated = false;
1703 for (i = 0; i < IDLTEST_LINK1_N_COLUMNS; i++) {
1704 if (idltest_link1_is_updated(l1, i)) {
1706 printf("%03d: updated columns:", step);
1709 printf(" %s", idltest_link1_columns[i].name);
1718 print_idl_row_updated_link2(const struct idltest_link2 *l2, int step)
1721 bool updated = false;
1723 for (i = 0; i < IDLTEST_LINK2_N_COLUMNS; i++) {
1724 if (idltest_link2_is_updated(l2, i)) {
1726 printf("%03d: updated columns:", step);
1729 printf(" %s", idltest_link2_columns[i].name);
1738 print_idl_row_simple(const struct idltest_simple *s, int step)
1742 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1743 step, s->i, s->r, s->b ? "true" : "false",
1744 s->s, UUID_ARGS(&s->u));
1745 for (i = 0; i < s->n_ia; i++) {
1746 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1749 for (i = 0; i < s->n_ra; i++) {
1750 printf("%s%g", i ? " " : "", s->ra[i]);
1753 for (i = 0; i < s->n_ba; i++) {
1754 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1757 for (i = 0; i < s->n_sa; i++) {
1758 printf("%s%s", i ? " " : "", s->sa[i]);
1761 for (i = 0; i < s->n_ua; i++) {
1762 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1764 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1765 print_idl_row_updated_simple(s, step);
1769 print_idl_row_link1(const struct idltest_link1 *l1, int step)
1771 struct idltest_link1 **links;
1774 printf("%03d: i=%"PRId64" k=", step, l1->i);
1776 printf("%"PRId64, l1->k->i);
1779 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1780 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1781 for (i = 0; i < l1->n_ka; i++) {
1782 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1787 printf("%"PRId64, l1->l2->i);
1789 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1790 print_idl_row_updated_link1(l1, step);
1794 print_idl_row_link2(const struct idltest_link2 *l2, int step)
1796 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1798 printf("%"PRId64, l2->l1->i);
1800 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1801 print_idl_row_updated_link2(l2, step);
1805 print_idl(struct ovsdb_idl *idl, int step)
1807 const struct idltest_simple *s;
1808 const struct idltest_link1 *l1;
1809 const struct idltest_link2 *l2;
1812 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1813 print_idl_row_simple(s, step);
1816 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1817 print_idl_row_link1(l1, step);
1820 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1821 print_idl_row_link2(l2, step);
1825 printf("%03d: empty\n", step);
1830 print_idl_track(struct ovsdb_idl *idl, int step, unsigned int seqno)
1832 const struct idltest_simple *s;
1833 const struct idltest_link1 *l1;
1834 const struct idltest_link2 *l2;
1837 IDLTEST_SIMPLE_FOR_EACH_TRACKED (s, idl) {
1838 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1839 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1841 print_idl_row_simple(s, step);
1845 IDLTEST_LINK1_FOR_EACH_TRACKED (l1, idl) {
1846 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1847 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1849 print_idl_row_link1(l1, step);
1853 IDLTEST_LINK2_FOR_EACH_TRACKED (l2, idl) {
1854 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1855 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1857 print_idl_row_link2(l2, step);
1862 printf("%03d: empty\n", step);
1867 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1872 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1873 char *name = xasprintf("#%"PRIuSIZE"#", *n);
1874 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1875 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1878 } else if (json->type == JSON_ARRAY) {
1881 for (i = 0; i < json->u.array.n; i++) {
1882 parse_uuids(json->u.array.elems[i], symtab, n);
1884 } else if (json->type == JSON_OBJECT) {
1885 const struct shash_node *node;
1887 SHASH_FOR_EACH (node, json_object(json)) {
1888 parse_uuids(node->data, symtab, n);
1894 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1896 if (json->type == JSON_STRING) {
1897 const struct ovsdb_symbol *symbol;
1899 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1901 free(json->u.string);
1902 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1904 } else if (json->type == JSON_ARRAY) {
1907 for (i = 0; i < json->u.array.n; i++) {
1908 substitute_uuids(json->u.array.elems[i], symtab);
1910 } else if (json->type == JSON_OBJECT) {
1911 const struct shash_node *node;
1913 SHASH_FOR_EACH (node, json_object(json)) {
1914 substitute_uuids(node->data, symtab);
1919 static const struct idltest_simple *
1920 idltest_find_simple(struct ovsdb_idl *idl, int i)
1922 const struct idltest_simple *s;
1924 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1933 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1935 char *cmd, *save_ptr1 = NULL;
1936 struct ovsdb_idl_txn *txn;
1937 enum ovsdb_idl_txn_status status;
1938 bool increment = false;
1940 txn = ovsdb_idl_txn_create(idl);
1941 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1942 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1943 char *save_ptr2 = NULL;
1944 char *name, *arg1, *arg2, *arg3;
1946 name = strtok_r(cmd, " ", &save_ptr2);
1947 arg1 = strtok_r(NULL, " ", &save_ptr2);
1948 arg2 = strtok_r(NULL, " ", &save_ptr2);
1949 arg3 = strtok_r(NULL, " ", &save_ptr2);
1951 if (!strcmp(name, "set")) {
1952 const struct idltest_simple *s;
1955 ovs_fatal(0, "\"set\" command requires 3 arguments");
1958 s = idltest_find_simple(idl, atoi(arg1));
1960 ovs_fatal(0, "\"set\" command asks for nonexistent "
1961 "i=%d", atoi(arg1));
1964 if (!strcmp(arg2, "b")) {
1965 idltest_simple_set_b(s, atoi(arg3));
1966 } else if (!strcmp(arg2, "s")) {
1967 idltest_simple_set_s(s, arg3);
1968 } else if (!strcmp(arg2, "u")) {
1970 if (!uuid_from_string(&uuid, arg3)) {
1971 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
1973 idltest_simple_set_u(s, uuid);
1974 } else if (!strcmp(arg2, "r")) {
1975 idltest_simple_set_r(s, atof(arg3));
1977 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1980 } else if (!strcmp(name, "insert")) {
1981 struct idltest_simple *s;
1983 if (!arg1 || arg2) {
1984 ovs_fatal(0, "\"insert\" command requires 1 argument");
1987 s = idltest_simple_insert(txn);
1988 idltest_simple_set_i(s, atoi(arg1));
1989 } else if (!strcmp(name, "delete")) {
1990 const struct idltest_simple *s;
1992 if (!arg1 || arg2) {
1993 ovs_fatal(0, "\"delete\" command requires 1 argument");
1996 s = idltest_find_simple(idl, atoi(arg1));
1998 ovs_fatal(0, "\"delete\" command asks for nonexistent "
1999 "i=%d", atoi(arg1));
2001 idltest_simple_delete(s);
2002 } else if (!strcmp(name, "verify")) {
2003 const struct idltest_simple *s;
2005 if (!arg2 || arg3) {
2006 ovs_fatal(0, "\"verify\" command requires 2 arguments");
2009 s = idltest_find_simple(idl, atoi(arg1));
2011 ovs_fatal(0, "\"verify\" command asks for nonexistent "
2012 "i=%d", atoi(arg1));
2015 if (!strcmp(arg2, "i")) {
2016 idltest_simple_verify_i(s);
2017 } else if (!strcmp(arg2, "b")) {
2018 idltest_simple_verify_b(s);
2019 } else if (!strcmp(arg2, "s")) {
2020 idltest_simple_verify_s(s);
2021 } else if (!strcmp(arg2, "u")) {
2022 idltest_simple_verify_s(s);
2023 } else if (!strcmp(arg2, "r")) {
2024 idltest_simple_verify_r(s);
2026 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
2029 } else if (!strcmp(name, "increment")) {
2030 const struct idltest_simple *s;
2032 if (!arg1 || arg2) {
2033 ovs_fatal(0, "\"increment\" command requires 1 argument");
2036 s = idltest_find_simple(idl, atoi(arg1));
2038 ovs_fatal(0, "\"set\" command asks for nonexistent "
2039 "i=%d", atoi(arg1));
2042 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i);
2044 } else if (!strcmp(name, "abort")) {
2045 ovsdb_idl_txn_abort(txn);
2047 } else if (!strcmp(name, "destroy")) {
2048 printf("%03d: destroy\n", step);
2049 ovsdb_idl_txn_destroy(txn);
2052 ovs_fatal(0, "unknown command %s", name);
2056 status = ovsdb_idl_txn_commit_block(txn);
2057 printf("%03d: commit, status=%s",
2058 step, ovsdb_idl_txn_status_to_string(status));
2060 printf(", increment=%"PRId64,
2061 ovsdb_idl_txn_get_increment_new_value(txn));
2064 ovsdb_idl_txn_destroy(txn);
2068 do_idl(struct ovs_cmdl_context *ctx)
2070 struct jsonrpc *rpc;
2071 struct ovsdb_idl *idl;
2072 unsigned int seqno = 0;
2073 struct ovsdb_symbol_table *symtab;
2082 track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
2084 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
2085 if (ctx->argc > 2) {
2086 struct stream *stream;
2088 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
2089 DSCP_DEFAULT), &stream);
2091 ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
2093 rpc = jsonrpc_open(stream);
2099 ovsdb_idl_track_add_all(idl);
2102 setvbuf(stdout, NULL, _IONBF, 0);
2104 symtab = ovsdb_symbol_table_create();
2105 for (i = 2; i < ctx->argc; i++) {
2106 char *arg = ctx->argv[i];
2107 struct jsonrpc_msg *request, *reply;
2110 /* The previous transaction didn't change anything. */
2113 /* Wait for update. */
2116 if (ovsdb_idl_get_seqno(idl) != seqno) {
2121 ovsdb_idl_wait(idl);
2128 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2129 ovsdb_idl_track_clear(idl);
2131 print_idl(idl, step++);
2134 seqno = ovsdb_idl_get_seqno(idl);
2136 if (!strcmp(arg, "reconnect")) {
2137 printf("%03d: reconnect\n", step++);
2138 ovsdb_idl_force_reconnect(idl);
2139 } else if (arg[0] != '[') {
2140 idl_set(idl, arg, step++);
2142 struct json *json = parse_json(arg);
2143 substitute_uuids(json, symtab);
2144 request = jsonrpc_create_request("transact", json, NULL);
2145 error = jsonrpc_transact_block(rpc, request, &reply);
2146 if (error || reply->error) {
2147 ovs_fatal(error, "jsonrpc transaction failed");
2149 printf("%03d: ", step++);
2150 if (reply->result) {
2151 parse_uuids(reply->result, symtab, &n_uuids);
2153 json_destroy(reply->id);
2155 print_and_free_json(jsonrpc_msg_to_json(reply));
2158 ovsdb_symbol_table_destroy(symtab);
2165 if (ovsdb_idl_get_seqno(idl) != seqno) {
2168 ovsdb_idl_wait(idl);
2171 print_idl(idl, step++);
2172 ovsdb_idl_track_clear(idl);
2173 ovsdb_idl_destroy(idl);
2174 printf("%03d: done\n", step);
2177 static struct ovs_cmdl_command all_commands[] = {
2178 { "log-io", NULL, 2, INT_MAX, do_log_io },
2179 { "default-atoms", NULL, 0, 0, do_default_atoms },
2180 { "default-data", NULL, 0, 0, do_default_data },
2181 { "diff-data", NULL, 3, INT_MAX, do_diff_data},
2182 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
2183 { "parse-base-type", NULL, 1, 1, do_parse_base_type },
2184 { "parse-type", NULL, 1, 1, do_parse_type },
2185 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
2186 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
2187 { "parse-data", NULL, 2, INT_MAX, do_parse_data },
2188 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
2189 { "sort-atoms", NULL, 2, 2, do_sort_atoms },
2190 { "parse-column", NULL, 2, 2, do_parse_column },
2191 { "parse-table", NULL, 2, 3, do_parse_table },
2192 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
2193 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
2194 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
2195 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
2196 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
2197 { "execute-mutations", NULL, 3, 3, do_execute_mutations },
2198 { "query", NULL, 3, 3, do_query },
2199 { "query-distinct", NULL, 4, 4, do_query_distinct },
2200 { "transact", NULL, 1, INT_MAX, do_transact },
2201 { "parse-schema", NULL, 1, 1, do_parse_schema },
2202 { "execute", NULL, 2, INT_MAX, do_execute },
2203 { "trigger", NULL, 2, INT_MAX, do_trigger },
2204 { "idl", NULL, 1, INT_MAX, do_idl },
2205 { "help", NULL, 0, INT_MAX, do_help },
2206 { NULL, NULL, 0, 0, NULL },
2209 static struct ovs_cmdl_command *
2210 get_all_commands(void)
2212 return all_commands;