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 " evaluate-conditions-any TABLE [CONDITION,...] [ROW,...]\n"
170 " test CONDITIONS to match any of the CONDITONS on TABLE\n"
171 " against each ROW, print results\n"
172 " compare-conditions TABLE [CONDITION,...]\n"
173 " mutually compare all of the CONDITION, print results for\n"
175 " parse-mutations TABLE MUTATION...\n"
176 " parse each MUTATION on TABLE, and re-serialize\n"
177 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
178 " execute MUTATIONS on TABLE on each ROW, print results\n"
179 " query TABLE [ROW,...] [CONDITION,...]\n"
180 " add each ROW to TABLE, then query and print the rows that\n"
181 " satisfy each CONDITION.\n"
182 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
183 " add each ROW to TABLE, then query and print the rows that\n"
184 " satisfy each CONDITION and have distinct COLUMNS.\n"
185 " parse-schema JSON\n"
186 " parse JSON as an OVSDB schema, and re-serialize\n"
187 " transact COMMAND\n"
188 " execute each specified transactional COMMAND:\n"
195 " execute SCHEMA TRANSACTION...\n"
196 " executes each TRANSACTION on an initially empty database\n"
197 " the specified SCHEMA\n"
198 " trigger SCHEMA TRANSACTION...\n"
199 " executes each TRANSACTION on an initially empty database\n"
200 " the specified SCHEMA. A TRANSACTION of the form\n"
201 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
202 " simulated time, for causing triggers to time out.\n"
203 " idl SERVER [TRANSACTION...]\n"
204 " connect to SERVER and dump the contents of the database\n"
205 " as seen initially by the IDL implementation and after\n"
206 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
207 " the database or this command will hang.)\n"
208 " idl-partial-update-map-column SERVER \n"
209 " connect to SERVER and executes different operations to\n"
210 " test the capacity of updating elements inside a map column\n"
211 " displaying the table information after each operation.\n",
212 program_name, program_name);
214 printf("\nOther options:\n"
215 " -t, --timeout=SECS give up after SECS seconds\n"
216 " -h, --help display this help message\n"
217 " -c, --change-track used with the 'idl' command to\n"
218 " enable tracking of IDL changes\n");
222 /* Command helper functions. */
225 parse_json(const char *s)
227 struct json *json = json_from_string(s);
228 if (json->type == JSON_STRING) {
229 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
235 unbox_json(struct json *json)
237 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
238 struct json *inner = json->u.array.elems[0];
239 json->u.array.elems[0] = NULL;
248 print_and_free_json(struct json *json)
250 char *string = json_to_string(json, JSSF_SORT);
257 print_and_free_ovsdb_error(struct ovsdb_error *error)
259 char *string = ovsdb_error_to_string(error);
260 ovsdb_error_destroy(error);
266 check_ovsdb_error(struct ovsdb_error *error)
269 char *s = ovsdb_error_to_string(error);
270 ovsdb_error_destroy(error);
271 ovs_fatal(0, "%s", s);
276 die_if_error(char *error)
279 ovs_fatal(0, "%s", error);
283 /* Command implementations. */
286 do_log_io(struct ovs_cmdl_context *ctx)
288 const char *name = ctx->argv[1];
289 char *mode_string = ctx->argv[2];
291 struct ovsdb_error *error;
292 enum ovsdb_log_open_mode mode;
293 struct ovsdb_log *log;
296 if (!strcmp(mode_string, "read-only")) {
297 mode = OVSDB_LOG_READ_ONLY;
298 } else if (!strcmp(mode_string, "read/write")) {
299 mode = OVSDB_LOG_READ_WRITE;
300 } else if (!strcmp(mode_string, "create")) {
301 mode = OVSDB_LOG_CREATE;
303 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
306 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
307 printf("%s: open successful\n", name);
309 for (i = 3; i < ctx->argc; i++) {
310 const char *command = ctx->argv[i];
311 if (!strcmp(command, "read")) {
314 error = ovsdb_log_read(log, &json);
316 printf("%s: read: ", name);
318 print_and_free_json(json);
320 printf("end of log\n");
324 } else if (!strncmp(command, "write:", 6)) {
325 struct json *json = parse_json(command + 6);
326 error = ovsdb_log_write(log, json);
328 } else if (!strcmp(command, "commit")) {
329 error = ovsdb_log_commit(log);
331 ovs_fatal(0, "unknown log-io command \"%s\"", command);
334 char *s = ovsdb_error_to_string(error);
335 printf("%s: %s failed: %s\n", name, command, s);
337 ovsdb_error_destroy(error);
339 printf("%s: %s successful\n", name, command);
343 ovsdb_log_close(log);
347 do_default_atoms(struct ovs_cmdl_context *ctx OVS_UNUSED)
351 for (type = 0; type < OVSDB_N_TYPES; type++) {
352 union ovsdb_atom atom;
354 if (type == OVSDB_TYPE_VOID) {
358 printf("%s: ", ovsdb_atomic_type_to_string(type));
360 ovsdb_atom_init_default(&atom, type);
361 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
365 ovsdb_atom_destroy(&atom, type);
372 do_default_data(struct ovs_cmdl_context *ctx OVS_UNUSED)
377 for (n_min = 0; n_min <= 1; n_min++) {
378 for (key = 0; key < OVSDB_N_TYPES; key++) {
379 if (key == OVSDB_TYPE_VOID) {
382 for (value = 0; value < OVSDB_N_TYPES; value++) {
383 struct ovsdb_datum datum;
384 struct ovsdb_type type;
386 ovsdb_base_type_init(&type.key, key);
387 ovsdb_base_type_init(&type.value, value);
390 assert(ovsdb_type_is_valid(&type));
392 printf("key %s, value %s, n_min %u: ",
393 ovsdb_atomic_type_to_string(key),
394 ovsdb_atomic_type_to_string(value), n_min);
396 ovsdb_datum_init_default(&datum, &type);
397 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
402 ovsdb_datum_destroy(&datum, &type);
403 ovsdb_type_destroy(&type);
412 do_diff_data(struct ovs_cmdl_context *ctx)
414 struct ovsdb_type type;
416 struct ovsdb_datum new, old, diff, reincarnation;
418 json = unbox_json(parse_json(ctx->argv[1]));
419 check_ovsdb_error(ovsdb_type_from_json(&type, json));
422 /* Arguments in pairs of 'old' and 'new'. */
423 for (int i = 2; i < ctx->argc - 1; i+=2) {
424 struct ovsdb_error *error;
426 json = unbox_json(parse_json(ctx->argv[i]));
427 check_ovsdb_error(ovsdb_datum_from_json(&old, &type, json, NULL));
430 json = unbox_json(parse_json(ctx->argv[i+1]));
431 check_ovsdb_error(ovsdb_transient_datum_from_json(&new, &type, json));
434 /* Generate the diff. */
435 ovsdb_datum_diff(&diff, &old, &new, &type);
437 /* Apply diff to 'old' to create'reincarnation'. */
438 error = ovsdb_datum_apply_diff(&reincarnation, &old, &diff, &type);
440 char *string = ovsdb_error_to_string(error);
441 ovsdb_error_destroy(error);
442 ovs_fatal(0, "%s", string);
445 /* Test to make sure 'new' equals 'reincarnation'. */
446 if (!ovsdb_datum_equals(&new, &reincarnation, &type)) {
447 ovs_fatal(0, "failed to apply diff");
451 json = ovsdb_datum_to_json(&diff, &type);
453 print_and_free_json(json);
455 /* Print reincarnation */
456 json = ovsdb_datum_to_json(&reincarnation, &type);
457 printf ("apply diff: ");
458 print_and_free_json(json);
460 ovsdb_datum_destroy(&new, &type);
461 ovsdb_datum_destroy(&old, &type);
462 ovsdb_datum_destroy(&diff, &type);
463 ovsdb_datum_destroy(&reincarnation, &type);
468 ovsdb_type_destroy(&type);
472 do_parse_atomic_type(struct ovs_cmdl_context *ctx)
474 enum ovsdb_atomic_type type;
477 json = unbox_json(parse_json(ctx->argv[1]));
478 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
480 print_and_free_json(ovsdb_atomic_type_to_json(type));
484 do_parse_base_type(struct ovs_cmdl_context *ctx)
486 struct ovsdb_base_type base;
489 json = unbox_json(parse_json(ctx->argv[1]));
490 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
492 print_and_free_json(ovsdb_base_type_to_json(&base));
493 ovsdb_base_type_destroy(&base);
497 do_parse_type(struct ovs_cmdl_context *ctx)
499 struct ovsdb_type type;
502 json = unbox_json(parse_json(ctx->argv[1]));
503 check_ovsdb_error(ovsdb_type_from_json(&type, json));
505 print_and_free_json(ovsdb_type_to_json(&type));
506 ovsdb_type_destroy(&type);
510 do_parse_atoms(struct ovs_cmdl_context *ctx)
512 struct ovsdb_base_type base;
516 json = unbox_json(parse_json(ctx->argv[1]));
517 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
520 for (i = 2; i < ctx->argc; i++) {
521 struct ovsdb_error *error;
522 union ovsdb_atom atom;
524 json = unbox_json(parse_json(ctx->argv[i]));
525 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
529 print_and_free_ovsdb_error(error);
531 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
532 ovsdb_atom_destroy(&atom, base.type);
535 ovsdb_base_type_destroy(&base);
539 do_parse_atom_strings(struct ovs_cmdl_context *ctx)
541 struct ovsdb_base_type base;
545 json = unbox_json(parse_json(ctx->argv[1]));
546 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
549 for (i = 2; i < ctx->argc; i++) {
550 union ovsdb_atom atom;
553 die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL));
556 ovsdb_atom_to_string(&atom, base.type, &out);
560 ovsdb_atom_destroy(&atom, base.type);
562 ovsdb_base_type_destroy(&base);
566 do_parse_data__(int argc, char *argv[],
568 (*parse)(struct ovsdb_datum *datum,
569 const struct ovsdb_type *type,
570 const struct json *json,
571 struct ovsdb_symbol_table *symtab))
573 struct ovsdb_type type;
577 json = unbox_json(parse_json(argv[1]));
578 check_ovsdb_error(ovsdb_type_from_json(&type, json));
581 for (i = 2; i < argc; i++) {
582 struct ovsdb_datum datum;
584 json = unbox_json(parse_json(argv[i]));
585 check_ovsdb_error(parse(&datum, &type, json, NULL));
588 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
590 ovsdb_datum_destroy(&datum, &type);
592 ovsdb_type_destroy(&type);
596 do_parse_data(struct ovs_cmdl_context *ctx)
598 do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json);
602 do_parse_data_strings(struct ovs_cmdl_context *ctx)
604 struct ovsdb_type type;
608 json = unbox_json(parse_json(ctx->argv[1]));
609 check_ovsdb_error(ovsdb_type_from_json(&type, json));
612 for (i = 2; i < ctx->argc; i++) {
613 struct ovsdb_datum datum;
616 die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL));
619 ovsdb_datum_to_string(&datum, &type, &out);
623 ovsdb_datum_destroy(&datum, &type);
625 ovsdb_type_destroy(&type);
628 static enum ovsdb_atomic_type compare_atoms_atomic_type;
631 compare_atoms(const void *a_, const void *b_)
633 const union ovsdb_atom *a = a_;
634 const union ovsdb_atom *b = b_;
636 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
640 do_sort_atoms(struct ovs_cmdl_context *ctx)
642 struct ovsdb_base_type base;
643 union ovsdb_atom *atoms;
644 struct json *json, **json_atoms;
648 json = unbox_json(parse_json(ctx->argv[1]));
649 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
652 json = unbox_json(parse_json(ctx->argv[2]));
653 if (json->type != JSON_ARRAY) {
654 ovs_fatal(0, "second argument must be array");
657 /* Convert JSON atoms to internal representation. */
658 n_atoms = json->u.array.n;
659 atoms = xmalloc(n_atoms * sizeof *atoms);
660 for (i = 0; i < n_atoms; i++) {
661 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
662 json->u.array.elems[i], NULL));
667 compare_atoms_atomic_type = base.type;
668 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
670 /* Convert internal representation back to JSON. */
671 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
672 for (i = 0; i < n_atoms; i++) {
673 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
674 ovsdb_atom_destroy(&atoms[i], base.type);
676 print_and_free_json(json_array_create(json_atoms, n_atoms));
678 ovsdb_base_type_destroy(&base);
682 do_parse_column(struct ovs_cmdl_context *ctx)
684 struct ovsdb_column *column;
687 json = parse_json(ctx->argv[2]);
688 check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column));
690 print_and_free_json(ovsdb_column_to_json(column));
691 ovsdb_column_destroy(column);
695 do_parse_table(struct ovs_cmdl_context *ctx)
697 struct ovsdb_table_schema *ts;
698 bool default_is_root;
701 default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true");
703 json = parse_json(ctx->argv[2]);
704 check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts));
706 print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
707 ovsdb_table_schema_destroy(ts);
711 do_parse_rows(struct ovs_cmdl_context *ctx)
713 struct ovsdb_column_set all_columns;
714 struct ovsdb_table_schema *ts;
715 struct ovsdb_table *table;
719 json = unbox_json(parse_json(ctx->argv[1]));
720 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
723 table = ovsdb_table_create(ts);
724 ovsdb_column_set_init(&all_columns);
725 ovsdb_column_set_add_all(&all_columns, table);
727 for (i = 2; i < ctx->argc; i++) {
728 struct ovsdb_column_set columns;
729 struct ovsdb_row *row;
731 ovsdb_column_set_init(&columns);
732 row = ovsdb_row_create(table);
734 json = unbox_json(parse_json(ctx->argv[i]));
735 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
738 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
740 if (columns.n_columns) {
746 for (j = 0; j < columns.n_columns; j++) {
747 svec_add(&names, columns.columns[j]->name);
750 s = svec_join(&names, ", ", "");
753 svec_destroy(&names);
758 ovsdb_column_set_destroy(&columns);
759 ovsdb_row_destroy(row);
762 ovsdb_column_set_destroy(&all_columns);
763 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
767 do_compare_rows(struct ovs_cmdl_context *ctx)
769 struct ovsdb_column_set all_columns;
770 struct ovsdb_table_schema *ts;
771 struct ovsdb_table *table;
772 struct ovsdb_row **rows;
778 json = unbox_json(parse_json(ctx->argv[1]));
779 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
782 table = ovsdb_table_create(ts);
783 ovsdb_column_set_init(&all_columns);
784 ovsdb_column_set_add_all(&all_columns, table);
786 n_rows = ctx->argc - 2;
787 rows = xmalloc(sizeof *rows * n_rows);
788 names = xmalloc(sizeof *names * n_rows);
789 for (i = 0; i < n_rows; i++) {
790 rows[i] = ovsdb_row_create(table);
792 json = parse_json(ctx->argv[i + 2]);
793 if (json->type != JSON_ARRAY || json->u.array.n != 2
794 || json->u.array.elems[0]->type != JSON_STRING) {
795 ovs_fatal(0, "\"%s\" does not have expected form "
796 "[\"name\", {data}]", ctx->argv[i]);
798 names[i] = xstrdup(json->u.array.elems[0]->u.string);
799 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
803 for (i = 0; i < n_rows; i++) {
804 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
805 for (j = i + 1; j < n_rows; j++) {
806 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
807 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
808 printf("%s == %s\n", names[i], names[j]);
809 if (i_hash != j_hash) {
810 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
813 } else if (i_hash == j_hash) {
814 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
818 for (i = 0; i < n_rows; i++) {
819 ovsdb_row_destroy(rows[i]);
825 ovsdb_column_set_destroy(&all_columns);
826 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
830 do_parse_conditions(struct ovs_cmdl_context *ctx)
832 struct ovsdb_table_schema *ts;
837 json = unbox_json(parse_json(ctx->argv[1]));
838 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
841 for (i = 2; i < ctx->argc; i++) {
842 struct ovsdb_condition cnd;
843 struct ovsdb_error *error;
845 json = parse_json(ctx->argv[i]);
846 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
848 print_and_free_json(ovsdb_condition_to_json(&cnd));
850 char *s = ovsdb_error_to_string(error);
851 ovs_error(0, "%s", s);
853 ovsdb_error_destroy(error);
858 ovsdb_condition_destroy(&cnd);
860 ovsdb_table_schema_destroy(ts);
865 #define OVSDB_CONDITION_EVERY 0
866 #define OVSDB_CONDITION_ANY 1
869 do_evaluate_condition__(struct ovs_cmdl_context *ctx, int mode)
871 struct ovsdb_table_schema *ts;
872 struct ovsdb_table *table;
873 struct ovsdb_condition *conditions;
875 struct ovsdb_row **rows;
880 /* Parse table schema, create table. */
881 json = unbox_json(parse_json(ctx->argv[1]));
882 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
885 table = ovsdb_table_create(ts);
887 /* Parse conditions. */
888 json = parse_json(ctx->argv[2]);
889 if (json->type != JSON_ARRAY) {
890 ovs_fatal(0, "CONDITION argument is not JSON array");
892 n_conditions = json->u.array.n;
893 conditions = xmalloc(n_conditions * sizeof *conditions);
894 for (i = 0; i < n_conditions; i++) {
895 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
896 NULL, &conditions[i]));
901 json = parse_json(ctx->argv[3]);
902 if (json->type != JSON_ARRAY) {
903 ovs_fatal(0, "ROW argument is not JSON array");
905 n_rows = json->u.array.n;
906 rows = xmalloc(n_rows * sizeof *rows);
907 for (i = 0; i < n_rows; i++) {
908 rows[i] = ovsdb_row_create(table);
909 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
914 for (i = 0; i < n_conditions; i++) {
915 printf("condition %2"PRIuSIZE":", i);
916 for (j = 0; j < n_rows; j++) {
918 if (mode == OVSDB_CONDITION_EVERY) {
919 result = ovsdb_condition_match_every_clause(rows[j],
922 result = ovsdb_condition_match_any_clause(rows[j]->fields,
929 putchar(result ? 'T' : '-');
934 for (i = 0; i < n_conditions; i++) {
935 ovsdb_condition_destroy(&conditions[i]);
938 for (i = 0; i < n_rows; i++) {
939 ovsdb_row_destroy(rows[i]);
942 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
946 do_evaluate_conditions(struct ovs_cmdl_context *ctx)
948 do_evaluate_condition__(ctx, OVSDB_CONDITION_EVERY);
952 do_evaluate_conditions_any(struct ovs_cmdl_context *ctx)
954 do_evaluate_condition__(ctx, OVSDB_CONDITION_ANY);
958 do_compare_conditions(struct ovs_cmdl_context *ctx)
960 struct ovsdb_table_schema *ts;
961 struct ovsdb_table *table;
962 struct ovsdb_condition *conditions;
967 /* Parse table schema, create table. */
968 json = unbox_json(parse_json(ctx->argv[1]));
969 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
972 table = ovsdb_table_create(ts);
974 /* Parse conditions. */
975 json = parse_json(ctx->argv[2]);
976 if (json->type != JSON_ARRAY) {
977 ovs_fatal(0, "CONDITION argument is not JSON array");
979 n_conditions = json->u.array.n;
980 conditions = xmalloc(n_conditions * sizeof *conditions);
982 for (i = 0; i < n_conditions; i++) {
983 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
984 NULL, &conditions[i]));
988 for (i = 0; i < n_conditions - 1; i++) {
989 int res = ovsdb_condition_cmp_3way(&conditions[i], &conditions[i + 1]);
990 printf("condition %"PRIuSIZE"-%"PRIuSIZE": %d\n", i, i + 1, res);
993 for (i = 0; i < n_conditions; i++) {
994 ovsdb_condition_destroy(&conditions[i]);
997 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1001 do_parse_mutations(struct ovs_cmdl_context *ctx)
1003 struct ovsdb_table_schema *ts;
1008 json = unbox_json(parse_json(ctx->argv[1]));
1009 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1012 for (i = 2; i < ctx->argc; i++) {
1013 struct ovsdb_mutation_set set;
1014 struct ovsdb_error *error;
1016 json = parse_json(ctx->argv[i]);
1017 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
1019 print_and_free_json(ovsdb_mutation_set_to_json(&set));
1021 char *s = ovsdb_error_to_string(error);
1022 ovs_error(0, "%s", s);
1024 ovsdb_error_destroy(error);
1029 ovsdb_mutation_set_destroy(&set);
1031 ovsdb_table_schema_destroy(ts);
1037 do_execute_mutations(struct ovs_cmdl_context *ctx)
1039 struct ovsdb_table_schema *ts;
1040 struct ovsdb_table *table;
1041 struct ovsdb_mutation_set *sets;
1043 struct ovsdb_row **rows;
1048 /* Parse table schema, create table. */
1049 json = unbox_json(parse_json(ctx->argv[1]));
1050 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1053 table = ovsdb_table_create(ts);
1055 /* Parse mutations. */
1056 json = parse_json(ctx->argv[2]);
1057 if (json->type != JSON_ARRAY) {
1058 ovs_fatal(0, "MUTATION argument is not JSON array");
1060 n_sets = json->u.array.n;
1061 sets = xmalloc(n_sets * sizeof *sets);
1062 for (i = 0; i < n_sets; i++) {
1063 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
1064 json->u.array.elems[i],
1070 json = parse_json(ctx->argv[3]);
1071 if (json->type != JSON_ARRAY) {
1072 ovs_fatal(0, "ROW argument is not JSON array");
1074 n_rows = json->u.array.n;
1075 rows = xmalloc(n_rows * sizeof *rows);
1076 for (i = 0; i < n_rows; i++) {
1077 rows[i] = ovsdb_row_create(table);
1078 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
1083 for (i = 0; i < n_sets; i++) {
1084 printf("mutation %2"PRIuSIZE":\n", i);
1085 for (j = 0; j < n_rows; j++) {
1086 struct ovsdb_error *error;
1087 struct ovsdb_row *row;
1089 row = ovsdb_row_clone(rows[j]);
1090 error = ovsdb_mutation_set_execute(row, &sets[i]);
1092 printf("row %"PRIuSIZE": ", j);
1094 print_and_free_ovsdb_error(error);
1096 struct ovsdb_column_set columns;
1097 struct shash_node *node;
1099 ovsdb_column_set_init(&columns);
1100 SHASH_FOR_EACH (node, &ts->columns) {
1101 struct ovsdb_column *c = node->data;
1102 if (!ovsdb_datum_equals(&row->fields[c->index],
1103 &rows[j]->fields[c->index],
1105 ovsdb_column_set_add(&columns, c);
1108 if (columns.n_columns) {
1109 print_and_free_json(ovsdb_row_to_json(row, &columns));
1111 printf("no change\n");
1113 ovsdb_column_set_destroy(&columns);
1115 ovsdb_row_destroy(row);
1120 for (i = 0; i < n_sets; i++) {
1121 ovsdb_mutation_set_destroy(&sets[i]);
1124 for (i = 0; i < n_rows; i++) {
1125 ovsdb_row_destroy(rows[i]);
1128 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1131 /* Inserts a row, without bothering to update metadata such as refcounts. */
1133 put_row(struct ovsdb_table *table, struct ovsdb_row *row)
1135 const struct uuid *uuid = ovsdb_row_get_uuid(row);
1136 if (!ovsdb_table_get_row(table, uuid)) {
1137 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1141 struct do_query_cbdata {
1142 struct uuid *row_uuids;
1148 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
1150 struct do_query_cbdata *cbdata = cbdata_;
1153 for (i = 0; i < cbdata->n_rows; i++) {
1154 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1155 cbdata->counts[i]++;
1163 do_query(struct ovs_cmdl_context *ctx)
1165 struct do_query_cbdata cbdata;
1166 struct ovsdb_table_schema *ts;
1167 struct ovsdb_table *table;
1172 /* Parse table schema, create table. */
1173 json = unbox_json(parse_json(ctx->argv[1]));
1174 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1177 table = ovsdb_table_create(ts);
1179 /* Parse rows, add to table. */
1180 json = parse_json(ctx->argv[2]);
1181 if (json->type != JSON_ARRAY) {
1182 ovs_fatal(0, "ROW argument is not JSON array");
1184 cbdata.n_rows = json->u.array.n;
1185 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1186 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1187 for (i = 0; i < cbdata.n_rows; i++) {
1188 struct ovsdb_row *row = ovsdb_row_create(table);
1189 uuid_generate(ovsdb_row_get_uuid_rw(row));
1190 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1192 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1193 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1194 UUID_ARGS(ovsdb_row_get_uuid(row)));
1196 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
1197 put_row(table, row);
1201 /* Parse conditions and execute queries. */
1202 json = parse_json(ctx->argv[3]);
1203 if (json->type != JSON_ARRAY) {
1204 ovs_fatal(0, "CONDITION argument is not JSON array");
1206 for (i = 0; i < json->u.array.n; i++) {
1207 struct ovsdb_condition cnd;
1210 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1213 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1214 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1216 printf("query %2"PRIuSIZE":", i);
1217 for (j = 0; j < cbdata.n_rows; j++) {
1221 if (cbdata.counts[j]) {
1222 printf("%d", cbdata.counts[j]);
1223 if (cbdata.counts[j] > 1) {
1233 ovsdb_condition_destroy(&cnd);
1237 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1242 struct do_query_distinct_class {
1243 struct ovsdb_row *example;
1247 struct do_query_distinct_row {
1249 struct do_query_distinct_class *class;
1253 do_query_distinct(struct ovs_cmdl_context *ctx)
1255 struct ovsdb_column_set columns;
1256 struct ovsdb_table_schema *ts;
1257 struct ovsdb_table *table;
1258 struct do_query_distinct_row *rows;
1260 struct do_query_distinct_class *classes;
1266 /* Parse table schema, create table. */
1267 json = unbox_json(parse_json(ctx->argv[1]));
1268 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1271 table = ovsdb_table_create(ts);
1273 /* Parse column set. */
1274 json = parse_json(ctx->argv[4]);
1275 check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1279 /* Parse rows, add to table. */
1280 json = parse_json(ctx->argv[2]);
1281 if (json->type != JSON_ARRAY) {
1282 ovs_fatal(0, "ROW argument is not JSON array");
1284 n_rows = json->u.array.n;
1285 rows = xmalloc(n_rows * sizeof *rows);
1286 classes = xmalloc(n_rows * sizeof *classes);
1288 for (i = 0; i < n_rows; i++) {
1289 struct ovsdb_row *row;
1293 row = ovsdb_row_create(table);
1294 uuid_generate(ovsdb_row_get_uuid_rw(row));
1295 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1298 /* Initialize row and find equivalence class. */
1299 rows[i].uuid = *ovsdb_row_get_uuid(row);
1300 rows[i].class = NULL;
1301 for (j = 0; j < n_classes; j++) {
1302 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1303 rows[i].class = &classes[j];
1307 if (!rows[i].class) {
1308 rows[i].class = &classes[n_classes];
1309 classes[n_classes].example = ovsdb_row_clone(row);
1313 /* Add row to table. */
1314 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1315 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1316 UUID_ARGS(ovsdb_row_get_uuid(row)));
1318 put_row(table, row);
1323 /* Parse conditions and execute queries. */
1324 json = parse_json(ctx->argv[3]);
1325 if (json->type != JSON_ARRAY) {
1326 ovs_fatal(0, "CONDITION argument is not JSON array");
1328 for (i = 0; i < json->u.array.n; i++) {
1329 struct ovsdb_row_set results;
1330 struct ovsdb_condition cnd;
1333 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1336 for (j = 0; j < n_classes; j++) {
1337 classes[j].count = 0;
1339 ovsdb_row_set_init(&results);
1340 ovsdb_query_distinct(table, &cnd, &columns, &results);
1341 for (j = 0; j < results.n_rows; j++) {
1344 for (k = 0; k < n_rows; k++) {
1345 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1347 rows[k].class->count++;
1351 ovsdb_row_set_destroy(&results);
1353 printf("query %2"PRIuSIZE":", i);
1354 for (j = 0; j < n_rows; j++) {
1355 int count = rows[j].class->count;
1362 printf("%d", count);
1364 } else if (count == 1) {
1365 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1372 ovsdb_condition_destroy(&cnd);
1376 for (i = 0; i < n_classes; i++) {
1377 ovsdb_row_destroy(classes[i].example);
1380 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1388 do_parse_schema(struct ovs_cmdl_context *ctx)
1390 struct ovsdb_schema *schema;
1393 json = parse_json(ctx->argv[1]);
1394 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1396 print_and_free_json(ovsdb_schema_to_json(schema));
1397 ovsdb_schema_destroy(schema);
1401 do_execute(struct ovs_cmdl_context *ctx)
1403 struct ovsdb_schema *schema;
1408 /* Create database. */
1409 json = parse_json(ctx->argv[1]);
1410 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1412 db = ovsdb_create(schema);
1414 for (i = 2; i < ctx->argc; i++) {
1415 struct json *params, *result;
1418 params = parse_json(ctx->argv[i]);
1419 result = ovsdb_execute(db, NULL, params, 0, NULL);
1420 s = json_to_string(result, JSSF_SORT);
1423 json_destroy(params);
1424 json_destroy(result);
1430 struct test_trigger {
1431 struct ovsdb_trigger trigger;
1436 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1438 struct json *result;
1441 result = ovsdb_trigger_steal_result(&t->trigger);
1442 s = json_to_string(result, JSSF_SORT);
1443 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
1445 json_destroy(result);
1446 ovsdb_trigger_destroy(&t->trigger);
1451 do_trigger(struct ovs_cmdl_context *ctx)
1453 struct ovsdb_schema *schema;
1454 struct ovsdb_session session;
1455 struct ovsdb_server server;
1462 /* Create database. */
1463 json = parse_json(ctx->argv[1]);
1464 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1466 db = ovsdb_create(schema);
1468 ovsdb_server_init(&server);
1469 ovsdb_server_add_db(&server, db);
1470 ovsdb_session_init(&session, &server);
1474 for (i = 2; i < ctx->argc; i++) {
1475 struct json *params = parse_json(ctx->argv[i]);
1476 if (params->type == JSON_ARRAY
1477 && json_array(params)->n == 2
1478 && json_array(params)->elems[0]->type == JSON_STRING
1479 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1480 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1481 now += json_integer(json_array(params)->elems[1]);
1482 json_destroy(params);
1484 struct test_trigger *t = xmalloc(sizeof *t);
1485 ovsdb_trigger_init(&session, db, &t->trigger, params, now);
1486 t->number = number++;
1487 if (ovsdb_trigger_is_complete(&t->trigger)) {
1488 do_trigger_dump(t, now, "immediate");
1490 printf("t=%lld: new trigger %d\n", now, t->number);
1494 ovsdb_trigger_run(db, now);
1495 while (!ovs_list_is_empty(&session.completions)) {
1496 do_trigger_dump(CONTAINER_OF(ovs_list_pop_front(&session.completions),
1497 struct test_trigger, trigger.node),
1501 ovsdb_trigger_wait(db, now);
1502 poll_immediate_wake();
1506 ovsdb_server_destroy(&server);
1511 do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
1516 /* "transact" command. */
1518 static struct ovsdb *do_transact_db;
1519 static struct ovsdb_txn *do_transact_txn;
1520 static struct ovsdb_table *do_transact_table;
1523 do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED)
1525 ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
1526 do_transact_txn = NULL;
1530 do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED)
1532 ovsdb_txn_abort(do_transact_txn);
1533 do_transact_txn = NULL;
1537 uuid_from_integer(int integer, struct uuid *uuid)
1540 uuid->parts[3] = integer;
1543 static const struct ovsdb_row *
1544 do_transact_find_row(const char *uuid_string)
1546 const struct ovsdb_row *row;
1549 uuid_from_integer(atoi(uuid_string), &uuid);
1550 row = ovsdb_table_get_row(do_transact_table, &uuid);
1552 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1559 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1562 if (integer != -1) {
1563 const struct ovsdb_column *column;
1565 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1567 row->fields[column->index].keys[0].integer = integer;
1572 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1574 const struct ovsdb_column *column;
1576 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1578 return row->fields[column->index].keys[0].integer;
1582 do_transact_set_i_j(struct ovsdb_row *row,
1583 const char *i_string, const char *j_string)
1585 do_transact_set_integer(row, "i", atoi(i_string));
1586 do_transact_set_integer(row, "j", atoi(j_string));
1590 do_transact_insert(struct ovs_cmdl_context *ctx)
1592 struct ovsdb_row *row;
1595 row = ovsdb_row_create(do_transact_table);
1598 uuid = ovsdb_row_get_uuid_rw(row);
1599 uuid_from_integer(atoi(ctx->argv[1]), uuid);
1600 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1601 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1605 do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]);
1608 ovsdb_txn_row_insert(do_transact_txn, row);
1612 do_transact_delete(struct ovs_cmdl_context *ctx)
1614 const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]);
1615 ovsdb_txn_row_delete(do_transact_txn, row);
1619 do_transact_modify(struct ovs_cmdl_context *ctx)
1621 const struct ovsdb_row *row_ro;
1622 struct ovsdb_row *row_rw;
1624 row_ro = do_transact_find_row(ctx->argv[1]);
1625 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1626 do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]);
1630 compare_rows_by_uuid(const void *a_, const void *b_)
1632 struct ovsdb_row *const *ap = a_;
1633 struct ovsdb_row *const *bp = b_;
1635 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1639 do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED)
1641 const struct ovsdb_row **rows;
1642 const struct ovsdb_row *row;
1646 n_rows = hmap_count(&do_transact_table->rows);
1647 rows = xmalloc(n_rows * sizeof *rows);
1649 HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
1652 assert(i == n_rows);
1654 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1656 for (i = 0; i < n_rows; i++) {
1657 printf("\n%"PRId32": i=%d, j=%d",
1658 ovsdb_row_get_uuid(rows[i])->parts[3],
1659 do_transact_get_integer(rows[i], "i"),
1660 do_transact_get_integer(rows[i], "j"));
1667 do_transact(struct ovs_cmdl_context *ctx)
1669 static const struct ovs_cmdl_command do_transact_commands[] = {
1670 { "commit", NULL, 0, 0, do_transact_commit },
1671 { "abort", NULL, 0, 0, do_transact_abort },
1672 { "insert", NULL, 2, 3, do_transact_insert },
1673 { "delete", NULL, 1, 1, do_transact_delete },
1674 { "modify", NULL, 2, 3, do_transact_modify },
1675 { "print", NULL, 0, 0, do_transact_print },
1676 { NULL, NULL, 0, 0, NULL },
1679 struct ovsdb_schema *schema;
1684 json = parse_json("{\"name\": \"testdb\", "
1688 " {\"i\": {\"type\": \"integer\"}, "
1689 " \"j\": {\"type\": \"integer\"}}}}}");
1690 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1692 do_transact_db = ovsdb_create(schema);
1693 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1694 assert(do_transact_table != NULL);
1696 for (i = 1; i < ctx->argc; i++) {
1697 struct json *command;
1701 struct ovs_cmdl_context transact_ctx = { .argc = 0, };
1703 command = parse_json(ctx->argv[i]);
1704 if (command->type != JSON_ARRAY) {
1705 ovs_fatal(0, "transaction %d must be JSON array "
1706 "with at least 1 element", i);
1709 n_args = command->u.array.n;
1710 args = xmalloc((n_args + 1) * sizeof *args);
1711 for (j = 0; j < n_args; j++) {
1712 struct json *s = command->u.array.elems[j];
1713 if (s->type != JSON_STRING) {
1714 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1717 args[j] = xstrdup(json_string(s));
1719 args[n_args] = NULL;
1721 if (!do_transact_txn) {
1722 do_transact_txn = ovsdb_txn_create(do_transact_db);
1725 for (j = 0; j < n_args; j++) {
1729 fputs(args[j], stdout);
1732 transact_ctx.argc = n_args;
1733 transact_ctx.argv = args;
1734 ovs_cmdl_run_command(&transact_ctx, do_transact_commands);
1737 for (j = 0; j < n_args; j++) {
1741 json_destroy(command);
1743 ovsdb_txn_abort(do_transact_txn);
1744 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1748 compare_link1(const void *a_, const void *b_)
1750 const struct idltest_link1 *const *ap = a_;
1751 const struct idltest_link1 *const *bp = b_;
1752 const struct idltest_link1 *a = *ap;
1753 const struct idltest_link1 *b = *bp;
1755 return a->i < b->i ? -1 : a->i > b->i;
1759 print_idl_row_updated_simple(const struct idltest_simple *s, int step)
1762 bool updated = false;
1764 for (i = 0; i < IDLTEST_SIMPLE_N_COLUMNS; i++) {
1765 if (idltest_simple_is_updated(s, i)) {
1767 printf("%03d: updated columns:", step);
1770 printf(" %s", idltest_simple_columns[i].name);
1779 print_idl_row_updated_link1(const struct idltest_link1 *l1, int step)
1782 bool updated = false;
1784 for (i = 0; i < IDLTEST_LINK1_N_COLUMNS; i++) {
1785 if (idltest_link1_is_updated(l1, i)) {
1787 printf("%03d: updated columns:", step);
1790 printf(" %s", idltest_link1_columns[i].name);
1799 print_idl_row_updated_link2(const struct idltest_link2 *l2, int step)
1802 bool updated = false;
1804 for (i = 0; i < IDLTEST_LINK2_N_COLUMNS; i++) {
1805 if (idltest_link2_is_updated(l2, i)) {
1807 printf("%03d: updated columns:", step);
1810 printf(" %s", idltest_link2_columns[i].name);
1819 print_idl_row_simple(const struct idltest_simple *s, int step)
1823 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1824 step, s->i, s->r, s->b ? "true" : "false",
1825 s->s, UUID_ARGS(&s->u));
1826 for (i = 0; i < s->n_ia; i++) {
1827 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1830 for (i = 0; i < s->n_ra; i++) {
1831 printf("%s%g", i ? " " : "", s->ra[i]);
1834 for (i = 0; i < s->n_ba; i++) {
1835 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1838 for (i = 0; i < s->n_sa; i++) {
1839 printf("%s%s", i ? " " : "", s->sa[i]);
1842 for (i = 0; i < s->n_ua; i++) {
1843 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1845 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1846 print_idl_row_updated_simple(s, step);
1850 print_idl_row_link1(const struct idltest_link1 *l1, int step)
1852 struct idltest_link1 **links;
1855 printf("%03d: i=%"PRId64" k=", step, l1->i);
1857 printf("%"PRId64, l1->k->i);
1860 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1861 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1862 for (i = 0; i < l1->n_ka; i++) {
1863 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1868 printf("%"PRId64, l1->l2->i);
1870 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1871 print_idl_row_updated_link1(l1, step);
1875 print_idl_row_link2(const struct idltest_link2 *l2, int step)
1877 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1879 printf("%"PRId64, l2->l1->i);
1881 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1882 print_idl_row_updated_link2(l2, step);
1886 print_idl(struct ovsdb_idl *idl, int step)
1888 const struct idltest_simple *s;
1889 const struct idltest_link1 *l1;
1890 const struct idltest_link2 *l2;
1893 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1894 print_idl_row_simple(s, step);
1897 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1898 print_idl_row_link1(l1, step);
1901 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1902 print_idl_row_link2(l2, step);
1906 printf("%03d: empty\n", step);
1911 print_idl_track(struct ovsdb_idl *idl, int step, unsigned int seqno)
1913 const struct idltest_simple *s;
1914 const struct idltest_link1 *l1;
1915 const struct idltest_link2 *l2;
1918 IDLTEST_SIMPLE_FOR_EACH_TRACKED (s, idl) {
1919 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1920 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1922 print_idl_row_simple(s, step);
1926 IDLTEST_LINK1_FOR_EACH_TRACKED (l1, idl) {
1927 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1928 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1930 print_idl_row_link1(l1, step);
1934 IDLTEST_LINK2_FOR_EACH_TRACKED (l2, idl) {
1935 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1936 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1938 print_idl_row_link2(l2, step);
1943 printf("%03d: empty\n", step);
1948 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1953 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1954 char *name = xasprintf("#%"PRIuSIZE"#", *n);
1955 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1956 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1959 } else if (json->type == JSON_ARRAY) {
1962 for (i = 0; i < json->u.array.n; i++) {
1963 parse_uuids(json->u.array.elems[i], symtab, n);
1965 } else if (json->type == JSON_OBJECT) {
1966 const struct shash_node *node;
1968 SHASH_FOR_EACH (node, json_object(json)) {
1969 parse_uuids(node->data, symtab, n);
1975 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1977 if (json->type == JSON_STRING) {
1978 const struct ovsdb_symbol *symbol;
1980 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1982 free(json->u.string);
1983 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1985 } else if (json->type == JSON_ARRAY) {
1988 for (i = 0; i < json->u.array.n; i++) {
1989 substitute_uuids(json->u.array.elems[i], symtab);
1991 } else if (json->type == JSON_OBJECT) {
1992 const struct shash_node *node;
1994 SHASH_FOR_EACH (node, json_object(json)) {
1995 substitute_uuids(node->data, symtab);
2000 static const struct idltest_simple *
2001 idltest_find_simple(struct ovsdb_idl *idl, int i)
2003 const struct idltest_simple *s;
2005 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
2014 idl_set(struct ovsdb_idl *idl, char *commands, int step)
2016 char *cmd, *save_ptr1 = NULL;
2017 struct ovsdb_idl_txn *txn;
2018 enum ovsdb_idl_txn_status status;
2019 bool increment = false;
2021 txn = ovsdb_idl_txn_create(idl);
2022 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
2023 cmd = strtok_r(NULL, ",", &save_ptr1)) {
2024 char *save_ptr2 = NULL;
2025 char *name, *arg1, *arg2, *arg3;
2027 name = strtok_r(cmd, " ", &save_ptr2);
2028 arg1 = strtok_r(NULL, " ", &save_ptr2);
2029 arg2 = strtok_r(NULL, " ", &save_ptr2);
2030 arg3 = strtok_r(NULL, " ", &save_ptr2);
2032 if (!strcmp(name, "set")) {
2033 const struct idltest_simple *s;
2036 ovs_fatal(0, "\"set\" command requires 3 arguments");
2039 s = idltest_find_simple(idl, atoi(arg1));
2041 ovs_fatal(0, "\"set\" command asks for nonexistent "
2042 "i=%d", atoi(arg1));
2045 if (!strcmp(arg2, "b")) {
2046 idltest_simple_set_b(s, atoi(arg3));
2047 } else if (!strcmp(arg2, "s")) {
2048 idltest_simple_set_s(s, arg3);
2049 } else if (!strcmp(arg2, "u")) {
2051 if (!uuid_from_string(&uuid, arg3)) {
2052 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
2054 idltest_simple_set_u(s, uuid);
2055 } else if (!strcmp(arg2, "r")) {
2056 idltest_simple_set_r(s, atof(arg3));
2058 ovs_fatal(0, "\"set\" command asks for unknown column %s",
2061 } else if (!strcmp(name, "insert")) {
2062 struct idltest_simple *s;
2064 if (!arg1 || arg2) {
2065 ovs_fatal(0, "\"insert\" command requires 1 argument");
2068 s = idltest_simple_insert(txn);
2069 idltest_simple_set_i(s, atoi(arg1));
2070 } else if (!strcmp(name, "delete")) {
2071 const struct idltest_simple *s;
2073 if (!arg1 || arg2) {
2074 ovs_fatal(0, "\"delete\" command requires 1 argument");
2077 s = idltest_find_simple(idl, atoi(arg1));
2079 ovs_fatal(0, "\"delete\" command asks for nonexistent "
2080 "i=%d", atoi(arg1));
2082 idltest_simple_delete(s);
2083 } else if (!strcmp(name, "verify")) {
2084 const struct idltest_simple *s;
2086 if (!arg2 || arg3) {
2087 ovs_fatal(0, "\"verify\" command requires 2 arguments");
2090 s = idltest_find_simple(idl, atoi(arg1));
2092 ovs_fatal(0, "\"verify\" command asks for nonexistent "
2093 "i=%d", atoi(arg1));
2096 if (!strcmp(arg2, "i")) {
2097 idltest_simple_verify_i(s);
2098 } else if (!strcmp(arg2, "b")) {
2099 idltest_simple_verify_b(s);
2100 } else if (!strcmp(arg2, "s")) {
2101 idltest_simple_verify_s(s);
2102 } else if (!strcmp(arg2, "u")) {
2103 idltest_simple_verify_s(s);
2104 } else if (!strcmp(arg2, "r")) {
2105 idltest_simple_verify_r(s);
2107 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
2110 } else if (!strcmp(name, "increment")) {
2111 const struct idltest_simple *s;
2113 if (!arg1 || arg2) {
2114 ovs_fatal(0, "\"increment\" command requires 1 argument");
2117 s = idltest_find_simple(idl, atoi(arg1));
2119 ovs_fatal(0, "\"set\" command asks for nonexistent "
2120 "i=%d", atoi(arg1));
2123 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i);
2125 } else if (!strcmp(name, "abort")) {
2126 ovsdb_idl_txn_abort(txn);
2128 } else if (!strcmp(name, "destroy")) {
2129 printf("%03d: destroy\n", step);
2130 ovsdb_idl_txn_destroy(txn);
2133 ovs_fatal(0, "unknown command %s", name);
2137 status = ovsdb_idl_txn_commit_block(txn);
2138 printf("%03d: commit, status=%s",
2139 step, ovsdb_idl_txn_status_to_string(status));
2141 printf(", increment=%"PRId64,
2142 ovsdb_idl_txn_get_increment_new_value(txn));
2145 ovsdb_idl_txn_destroy(txn);
2148 static const struct ovsdb_idl_table_class *
2149 find_table_class(const char *name)
2151 if (!strcmp(name, "simple")) {
2152 return &idltest_table_simple;
2153 } else if (!strcmp(name, "link1")) {
2154 return &idltest_table_link1;
2155 } else if (!strcmp(name, "link2")) {
2156 return &idltest_table_link2;
2162 parse_simple_json_clause(struct ovsdb_idl *idl, bool add_cmd,
2166 struct ovsdb_error *error;
2167 enum ovsdb_function function;
2169 if (json->type == JSON_TRUE) {
2170 add_cmd ? idltest_simple_add_clause_true(idl) :
2171 idltest_simple_remove_clause_true(idl);
2173 } else if (json->type == JSON_FALSE) {
2174 add_cmd ? idltest_simple_add_clause_false(idl) :
2175 idltest_simple_remove_clause_false(idl);
2178 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2179 ovs_fatal(0, "Error parsing condition");
2182 c = json_string(json->u.array.elems[0]);
2183 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2186 ovs_fatal(0, "Error parsing clause function %s",
2187 json_string(json->u.array.elems[1]));
2190 /* add clause according to column */
2191 if (!strcmp(c, "b")) {
2192 add_cmd ? idltest_simple_add_clause_b(idl, function,
2193 json_boolean(json->u.array.elems[2])) :
2194 idltest_simple_remove_clause_b(idl, function,
2195 json_boolean(json->u.array.elems[2]));
2196 } else if (!strcmp(c, "i")) {
2197 add_cmd ? idltest_simple_add_clause_i(idl, function,
2198 json_integer(json->u.array.elems[2])) :
2199 idltest_simple_remove_clause_i(idl, function,
2200 json_integer(json->u.array.elems[2]));
2201 } else if (!strcmp(c, "s")) {
2202 add_cmd ? idltest_simple_add_clause_s(idl, function,
2203 json_string(json->u.array.elems[2])) :
2204 idltest_simple_remove_clause_s(idl, function,
2205 json_string(json->u.array.elems[2]));
2206 } else if (!strcmp(c, "u")) {
2208 if (!uuid_from_string(&uuid,
2209 json_string(json->u.array.elems[2]))) {
2210 ovs_fatal(0, "\"%s\" is not a valid UUID",
2211 json_string(json->u.array.elems[2]));
2213 add_cmd ? idltest_simple_add_clause_u(idl, function, uuid) :
2214 idltest_simple_remove_clause_u(idl, function, uuid);
2215 } else if (!strcmp(c, "r")) {
2216 add_cmd ? idltest_simple_add_clause_r(idl, function,
2217 json_real(json->u.array.elems[2])) :
2218 idltest_simple_remove_clause_r(idl, function,
2219 json_real(json->u.array.elems[2]));
2221 ovs_fatal(0, "Unsupported columns name %s", c);
2226 parse_link1_json_clause(struct ovsdb_idl *idl, bool add_cmd,
2230 struct ovsdb_error *error;
2231 enum ovsdb_function function;
2233 if (json->type == JSON_TRUE) {
2234 add_cmd ? idltest_link1_add_clause_true(idl) :
2235 idltest_link1_remove_clause_true(idl);
2237 } else if (json->type == JSON_FALSE) {
2238 add_cmd ? idltest_link1_add_clause_false(idl) :
2239 idltest_link1_remove_clause_false(idl);
2242 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2243 ovs_fatal(0, "Error parsing condition");
2246 c = json_string(json->u.array.elems[0]);
2247 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2250 ovs_fatal(0, "Error parsing clause function %s",
2251 json_string(json->u.array.elems[1]));
2254 /* add clause according to column */
2255 if (!strcmp(c, "i")) {
2256 add_cmd ? idltest_link1_add_clause_i(idl, function,
2257 json_integer(json->u.array.elems[2])) :
2258 idltest_link1_remove_clause_i(idl, function,
2259 json_integer(json->u.array.elems[2]));
2261 ovs_fatal(0, "Unsupported columns name %s", c);
2266 parse_link2_json_clause(struct ovsdb_idl *idl, bool add_cmd, struct json *json)
2269 struct ovsdb_error *error;
2270 enum ovsdb_function function;
2272 if (json->type == JSON_TRUE) {
2273 add_cmd ? idltest_link2_add_clause_true(idl) :
2274 idltest_link2_remove_clause_true(idl);
2276 } else if (json->type == JSON_FALSE) {
2277 add_cmd ? idltest_link2_add_clause_false(idl) :
2278 idltest_link2_remove_clause_false(idl);
2281 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2282 ovs_fatal(0, "Error parsing condition");
2285 c = json_string(json->u.array.elems[0]);
2286 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2289 ovs_fatal(0, "Error parsing clause function %s",
2290 json_string(json->u.array.elems[1]));
2293 /* add clause according to column */
2294 if (!strcmp(c, "i")) {
2295 add_cmd ? idltest_link2_add_clause_i(idl, function,
2296 json_integer(json->u.array.elems[2])) :
2297 idltest_link2_remove_clause_i(idl, function,
2298 json_integer(json->u.array.elems[2]));
2300 ovs_fatal(0, "Unsupported columns name %s", c);
2305 update_conditions(struct ovsdb_idl *idl, char *commands)
2307 char *cmd, *save_ptr1 = NULL;
2308 const struct ovsdb_idl_table_class *tc;
2309 bool add_cmd = false;
2311 for (cmd = strtok_r(commands, ";", &save_ptr1); cmd;
2312 cmd = strtok_r(NULL, ";", &save_ptr1)) {
2313 if (strstr(cmd, "condition add")) {
2314 cmd += strlen("condition add ");
2316 } else if (strstr(cmd, "condition remove")) {
2317 cmd += strlen("condition remove ");
2319 ovs_fatal(0, "condition command should be add or remove");
2322 char *save_ptr2 = NULL;
2323 char *table_name = strtok_r(cmd, " ", &save_ptr2);
2324 struct json *json = parse_json(save_ptr2);
2327 if (json->type != JSON_ARRAY) {
2328 ovs_fatal(0, "condition should be an array");
2331 tc = find_table_class(table_name);
2333 ovs_fatal(0, "Table %s does not exist", table_name);
2336 //ovsdb_idl_condition_reset(idl, tc);
2338 for (i = 0; i < json->u.array.n; i++) {
2339 if (!strcmp(table_name, "simple")) {
2340 parse_simple_json_clause(idl, add_cmd, json->u.array.elems[i]);
2341 } else if (!strcmp(table_name, "link1")) {
2342 parse_link1_json_clause(idl, add_cmd, json->u.array.elems[i]);
2343 } else if (!strcmp(table_name, "link2")) {
2344 parse_link2_json_clause(idl, add_cmd, json->u.array.elems[i]);
2351 do_idl(struct ovs_cmdl_context *ctx)
2353 struct jsonrpc *rpc;
2354 struct ovsdb_idl *idl;
2355 unsigned int seqno = 0;
2356 struct ovsdb_symbol_table *symtab;
2365 track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
2367 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
2368 if (ctx->argc > 2) {
2369 struct stream *stream;
2371 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
2372 DSCP_DEFAULT), &stream);
2374 ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
2376 rpc = jsonrpc_open(stream);
2382 ovsdb_idl_track_add_all(idl);
2385 setvbuf(stdout, NULL, _IONBF, 0);
2387 symtab = ovsdb_symbol_table_create();
2388 if (ctx->argc > 2 && strstr(ctx->argv[2], "condition ")) {
2389 update_conditions(idl, ctx->argv[2]);
2390 printf("%03d: change conditions\n", step++);
2395 for (; i < ctx->argc; i++) {
2396 char *arg = ctx->argv[i];
2397 struct jsonrpc_msg *request, *reply;
2400 /* The previous transaction didn't change anything. */
2403 /* Wait for update. */
2406 if (ovsdb_idl_get_seqno(idl) != seqno) {
2411 ovsdb_idl_wait(idl);
2418 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2419 ovsdb_idl_track_clear(idl);
2421 print_idl(idl, step++);
2424 seqno = ovsdb_idl_get_seqno(idl);
2426 if (!strcmp(arg, "reconnect")) {
2427 printf("%03d: reconnect\n", step++);
2428 ovsdb_idl_force_reconnect(idl);
2429 } else if (strstr(arg, "condition ")) {
2430 update_conditions(idl, arg);
2431 printf("%03d: change conditions\n", step++);
2432 } else if (arg[0] != '[') {
2433 idl_set(idl, arg, step++);
2435 struct json *json = parse_json(arg);
2436 substitute_uuids(json, symtab);
2437 request = jsonrpc_create_request("transact", json, NULL);
2438 error = jsonrpc_transact_block(rpc, request, &reply);
2439 if (error || reply->error) {
2440 ovs_fatal(error, "jsonrpc transaction failed");
2442 printf("%03d: ", step++);
2443 if (reply->result) {
2444 parse_uuids(reply->result, symtab, &n_uuids);
2446 json_destroy(reply->id);
2448 print_and_free_json(jsonrpc_msg_to_json(reply));
2451 ovsdb_symbol_table_destroy(symtab);
2458 if (ovsdb_idl_get_seqno(idl) != seqno) {
2461 ovsdb_idl_wait(idl);
2464 print_idl(idl, step++);
2465 ovsdb_idl_track_clear(idl);
2466 ovsdb_idl_destroy(idl);
2467 printf("%03d: done\n", step);
2471 print_idl_row_simple2(const struct idltest_simple2 *s, int step)
2474 const struct ovsdb_datum *smap, *imap;
2476 smap = idltest_simple2_get_smap(s, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
2477 imap = idltest_simple2_get_imap(s, OVSDB_TYPE_INTEGER, OVSDB_TYPE_STRING);
2478 printf("%03d: name=%s smap=[",
2480 for (i = 0; i < smap->n; i++) {
2481 printf("[%s : %s]%s", smap->keys[i].string, smap->values[i].string,
2482 i < smap->n-1? ",": "");
2485 for (i = 0; i < imap->n; i++) {
2486 printf("[%"PRId64" : %s]%s", imap->keys[i].integer, imap->values[i].string,
2487 i < imap->n-1? ",":"");
2493 dump_simple2(struct ovsdb_idl *idl,
2494 const struct idltest_simple2 *myRow,
2497 IDLTEST_SIMPLE2_FOR_EACH(myRow, idl) {
2498 print_idl_row_simple2(myRow, step);
2504 do_idl_partial_update_map_column(struct ovs_cmdl_context *ctx)
2506 struct ovsdb_idl *idl;
2507 struct ovsdb_idl_txn *myTxn;
2508 const struct idltest_simple2 *myRow;
2509 const struct ovsdb_datum *smap, *imap OVS_UNUSED;
2511 char key_to_delete[100];
2514 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, false, true);
2515 ovsdb_idl_add_table(idl, &idltest_table_simple2);
2516 ovsdb_idl_add_column(idl, &idltest_simple2_col_name);
2517 ovsdb_idl_add_column(idl, &idltest_simple2_col_smap);
2518 ovsdb_idl_add_column(idl, &idltest_simple2_col_imap);
2519 ovsdb_idl_get_initial_snapshot(idl);
2520 setvbuf(stdout, NULL, _IONBF, 0);
2523 /* Display original data in table */
2525 printf("%03d: Getting records\n", step++);
2526 dump_simple2(idl, myRow, step++);
2528 /* Insert new elements in different map columns */
2529 myRow = idltest_simple2_first(idl);
2530 myTxn = ovsdb_idl_txn_create(idl);
2531 idltest_simple2_get_smap(myRow, OVSDB_TYPE_STRING,
2533 idltest_simple2_update_smap_setkey(myRow, "key1", "myList1");
2534 imap = idltest_simple2_get_imap(myRow, OVSDB_TYPE_INTEGER,
2536 idltest_simple2_update_imap_setkey(myRow, 3, "myids2");
2537 idltest_simple2_set_name(myRow, "String2");
2538 ovsdb_idl_txn_commit_block(myTxn);
2539 ovsdb_idl_txn_destroy(myTxn);
2540 ovsdb_idl_get_initial_snapshot(idl);
2541 printf("%03d: After insert element\n", step++);
2542 dump_simple2(idl, myRow, step++);
2544 /* Insert duplicate element */
2545 myTxn = ovsdb_idl_txn_create(idl);
2546 idltest_simple2_update_smap_setkey(myRow, "key1", "myList1");
2547 ovsdb_idl_txn_commit_block(myTxn);
2548 ovsdb_idl_txn_destroy(myTxn);
2549 ovsdb_idl_get_initial_snapshot(idl);
2550 printf("%03d: After insert duplicated element\n", step++);
2551 dump_simple2(idl, myRow, step++);
2553 /* deletes an element of a map column */
2554 myRow = idltest_simple2_first(idl);
2555 myTxn = ovsdb_idl_txn_create(idl);
2556 smap = idltest_simple2_get_smap(myRow, OVSDB_TYPE_STRING,
2558 strcpy(key_to_delete, smap->keys[0].string);
2559 idltest_simple2_update_smap_delkey(myRow, smap->keys[0].string);
2560 ovsdb_idl_txn_commit_block(myTxn);
2561 ovsdb_idl_txn_destroy(myTxn);
2562 ovsdb_idl_get_initial_snapshot(idl);
2563 printf("%03d: After delete element\n", step++);
2564 dump_simple2(idl, myRow, step++);
2566 /* try to delete a deleted element of a map column */
2567 myTxn = ovsdb_idl_txn_create(idl);
2568 idltest_simple2_update_smap_delkey(myRow, key_to_delete);
2569 ovsdb_idl_txn_commit_block(myTxn);
2570 ovsdb_idl_txn_destroy(myTxn);
2571 ovsdb_idl_get_initial_snapshot(idl);
2572 printf("%03d: After trying to delete a deleted element\n", step++);
2573 dump_simple2(idl, myRow, step++);
2575 printf("%03d: End test\n", step);
2579 static struct ovs_cmdl_command all_commands[] = {
2580 { "log-io", NULL, 2, INT_MAX, do_log_io },
2581 { "default-atoms", NULL, 0, 0, do_default_atoms },
2582 { "default-data", NULL, 0, 0, do_default_data },
2583 { "diff-data", NULL, 3, INT_MAX, do_diff_data},
2584 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
2585 { "parse-base-type", NULL, 1, 1, do_parse_base_type },
2586 { "parse-type", NULL, 1, 1, do_parse_type },
2587 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
2588 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
2589 { "parse-data", NULL, 2, INT_MAX, do_parse_data },
2590 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
2591 { "sort-atoms", NULL, 2, 2, do_sort_atoms },
2592 { "parse-column", NULL, 2, 2, do_parse_column },
2593 { "parse-table", NULL, 2, 3, do_parse_table },
2594 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
2595 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
2596 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
2597 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
2598 { "evaluate-conditions-any", NULL, 3, 3, do_evaluate_conditions_any },
2599 { "compare-conditions", NULL, 2, 2, do_compare_conditions },
2600 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
2601 { "execute-mutations", NULL, 3, 3, do_execute_mutations },
2602 { "query", NULL, 3, 3, do_query },
2603 { "query-distinct", NULL, 4, 4, do_query_distinct },
2604 { "transact", NULL, 1, INT_MAX, do_transact },
2605 { "parse-schema", NULL, 1, 1, do_parse_schema },
2606 { "execute", NULL, 2, INT_MAX, do_execute },
2607 { "trigger", NULL, 2, INT_MAX, do_trigger },
2608 { "idl", NULL, 1, INT_MAX, do_idl },
2609 { "idl-partial-update-map-column", NULL, 1, INT_MAX,
2610 do_idl_partial_update_map_column },
2611 { "help", NULL, 0, INT_MAX, do_help },
2612 { NULL, NULL, 0, 0, NULL },
2615 static struct ovs_cmdl_command *
2616 get_all_commands(void)
2618 return all_commands;