2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #include "command-line.h"
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/table.h"
43 #include "ovsdb/transaction.h"
44 #include "ovsdb/trigger.h"
45 #include "poll-loop.h"
48 #include "tests/idltest.h"
53 static struct command all_commands
[];
55 static void usage(void) NO_RETURN
;
56 static void parse_options(int argc
, char *argv
[]);
59 main(int argc
, char *argv
[])
61 set_program_name(argv
[0]);
64 parse_options(argc
, argv
);
65 run_command(argc
- optind
, argv
+ optind
, all_commands
);
70 parse_options(int argc
, char *argv
[])
72 static struct option long_options
[] = {
73 {"timeout", required_argument
, 0, 't'},
74 {"verbose", optional_argument
, 0, 'v'},
75 {"help", no_argument
, 0, 'h'},
78 char *short_options
= long_options_to_short_options(long_options
);
81 unsigned long int timeout
;
84 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
91 timeout
= strtoul(optarg
, NULL
, 10);
93 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
104 vlog_set_verbosity(optarg
);
120 printf("%s: Open vSwitch database test utility\n"
121 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
122 " log-io FILE FLAGS COMMAND...\n"
123 " open FILE with FLAGS, run COMMANDs\n"
124 " parse-atomic-type TYPE\n"
125 " parse TYPE as OVSDB atomic type, and re-serialize\n"
126 " parse-base-type TYPE\n"
127 " parse TYPE as OVSDB base type, and re-serialize\n"
129 " parse JSON as OVSDB type, and re-serialize\n"
130 " parse-atoms TYPE ATOM...\n"
131 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
132 " parse-atom-strings TYPE ATOM...\n"
133 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
134 " sort-atoms TYPE ATOM...\n"
135 " print JSON ATOMs in sorted order\n"
136 " parse-data TYPE DATUM...\n"
137 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
138 " parse-data-strings TYPE DATUM...\n"
139 " parse string DATUMs as data of given TYPE, and re-serialize\n"
140 " parse-column NAME OBJECT\n"
141 " parse column NAME with info OBJECT, and re-serialize\n"
142 " parse-table NAME OBJECT\n"
143 " parse table NAME with info OBJECT\n"
144 " parse-row TABLE ROW..., and re-serialize\n"
145 " parse each ROW of defined TABLE\n"
146 " compare-row TABLE ROW...\n"
147 " mutually compare all of the ROWs, print those that are equal\n"
148 " parse-conditions TABLE CONDITION...\n"
149 " parse each CONDITION on TABLE, and re-serialize\n"
150 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
151 " test CONDITIONS on TABLE against each ROW, print results\n"
152 " parse-mutations TABLE MUTATION...\n"
153 " parse each MUTATION on TABLE, and re-serialize\n"
154 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
155 " execute MUTATIONS on TABLE on each ROW, print results\n"
156 " query TABLE [ROW,...] [CONDITION,...]\n"
157 " add each ROW to TABLE, then query and print the rows that\n"
158 " satisfy each CONDITION.\n"
159 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
160 " add each ROW to TABLE, then query and print the rows that\n"
161 " satisfy each CONDITION and have distinct COLUMNS.\n"
162 " parse-schema JSON\n"
163 " parse JSON as an OVSDB schema, and re-serialize\n"
164 " transact COMMAND\n"
165 " execute each specified transactional COMMAND:\n"
172 " execute SCHEMA TRANSACTION...\n"
173 " executes each TRANSACTION on an initially empty database\n"
174 " the specified SCHEMA\n"
175 " trigger SCHEMA TRANSACTION...\n"
176 " executes each TRANSACTION on an initially empty database\n"
177 " the specified SCHEMA. A TRANSACTION of the form\n"
178 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
179 " simulated time, for causing triggers to time out.\n"
180 " idl SERVER [TRANSACTION...]\n"
181 " connect to SERVER and dump the contents of the database\n"
182 " as seen initially by the IDL implementation and after\n"
183 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
184 " the database or this command will hang.)\n",
185 program_name
, program_name
);
187 printf("\nOther options:\n"
188 " -t, --timeout=SECS give up after SECS seconds\n"
189 " -h, --help display this help message\n");
193 /* Command helper functions. */
196 parse_json(const char *s
)
198 struct json
*json
= json_from_string(s
);
199 if (json
->type
== JSON_STRING
) {
200 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
206 unbox_json(struct json
*json
)
208 if (json
->type
== JSON_ARRAY
&& json
->u
.array
.n
== 1) {
209 struct json
*inner
= json
->u
.array
.elems
[0];
210 json
->u
.array
.elems
[0] = NULL
;
219 print_and_free_json(struct json
*json
)
221 char *string
= json_to_string(json
, JSSF_SORT
);
228 print_and_free_ovsdb_error(struct ovsdb_error
*error
)
230 char *string
= ovsdb_error_to_string(error
);
231 ovsdb_error_destroy(error
);
237 check_ovsdb_error(struct ovsdb_error
*error
)
240 char *s
= ovsdb_error_to_string(error
);
241 ovsdb_error_destroy(error
);
242 ovs_fatal(0, "%s", s
);
247 die_if_error(char *error
)
250 ovs_fatal(0, "%s", error
);
254 /* Command implementations. */
257 do_log_io(int argc
, char *argv
[])
259 const char *name
= argv
[1];
260 char *mode
= argv
[2];
262 struct ovsdb_error
*error
;
263 struct ovsdb_log
*log
;
264 char *save_ptr
= NULL
;
269 for (flags
= 0, token
= strtok_r(mode
, " |", &save_ptr
); token
!= NULL
;
270 token
= strtok_r(NULL
, " |", &save_ptr
))
272 if (!strcmp(token
, "O_RDONLY")) {
274 } else if (!strcmp(token
, "O_RDWR")) {
276 } else if (!strcmp(token
, "O_TRUNC")) {
278 } else if (!strcmp(token
, "O_CREAT")) {
280 } else if (!strcmp(token
, "O_EXCL")) {
282 } else if (!strcmp(token
, "O_TRUNC")) {
287 check_ovsdb_error(ovsdb_log_open(name
, flags
, &log
));
288 printf("%s: open successful\n", name
);
290 for (i
= 3; i
< argc
; i
++) {
291 const char *command
= argv
[i
];
292 if (!strcmp(command
, "read")) {
295 error
= ovsdb_log_read(log
, &json
);
297 printf("%s: read: ", name
);
299 print_and_free_json(json
);
301 printf("end of log\n");
305 } else if (!strncmp(command
, "write:", 6)) {
306 struct json
*json
= parse_json(command
+ 6);
307 error
= ovsdb_log_write(log
, json
);
309 } else if (!strcmp(command
, "commit")) {
310 error
= ovsdb_log_commit(log
);
312 ovs_fatal(0, "unknown log-io command \"%s\"", command
);
315 char *s
= ovsdb_error_to_string(error
);
316 printf("%s: %s failed: %s\n", name
, command
, s
);
318 ovsdb_error_destroy(error
);
320 printf("%s: %s successful\n", name
, command
);
324 ovsdb_log_close(log
);
328 do_parse_atomic_type(int argc OVS_UNUSED
, char *argv
[])
330 enum ovsdb_atomic_type type
;
333 json
= unbox_json(parse_json(argv
[1]));
334 check_ovsdb_error(ovsdb_atomic_type_from_json(&type
, json
));
336 print_and_free_json(ovsdb_atomic_type_to_json(type
));
340 do_parse_base_type(int argc OVS_UNUSED
, char *argv
[])
342 struct ovsdb_base_type base
;
345 json
= unbox_json(parse_json(argv
[1]));
346 check_ovsdb_error(ovsdb_base_type_from_json(&base
, json
));
348 print_and_free_json(ovsdb_base_type_to_json(&base
));
349 ovsdb_base_type_destroy(&base
);
353 do_parse_type(int argc OVS_UNUSED
, char *argv
[])
355 struct ovsdb_type type
;
358 json
= unbox_json(parse_json(argv
[1]));
359 check_ovsdb_error(ovsdb_type_from_json(&type
, json
));
361 print_and_free_json(ovsdb_type_to_json(&type
));
362 ovsdb_type_destroy(&type
);
366 do_parse_atoms(int argc
, char *argv
[])
368 struct ovsdb_base_type base
;
372 json
= unbox_json(parse_json(argv
[1]));
373 check_ovsdb_error(ovsdb_base_type_from_json(&base
, json
));
376 for (i
= 2; i
< argc
; i
++) {
377 struct ovsdb_error
*error
;
378 union ovsdb_atom atom
;
380 json
= unbox_json(parse_json(argv
[i
]));
381 error
= ovsdb_atom_from_json(&atom
, &base
, json
, NULL
);
385 print_and_free_ovsdb_error(error
);
387 print_and_free_json(ovsdb_atom_to_json(&atom
, base
.type
));
388 ovsdb_atom_destroy(&atom
, base
.type
);
391 ovsdb_base_type_destroy(&base
);
395 do_parse_atom_strings(int argc
, char *argv
[])
397 struct ovsdb_base_type base
;
401 json
= unbox_json(parse_json(argv
[1]));
402 check_ovsdb_error(ovsdb_base_type_from_json(&base
, json
));
405 for (i
= 2; i
< argc
; i
++) {
406 union ovsdb_atom atom
;
409 die_if_error(ovsdb_atom_from_string(&atom
, &base
, argv
[i
]));
412 ovsdb_atom_to_string(&atom
, base
.type
, &out
);
416 ovsdb_atom_destroy(&atom
, base
.type
);
418 ovsdb_base_type_destroy(&base
);
422 do_parse_data(int argc
, char *argv
[])
424 struct ovsdb_type type
;
428 json
= unbox_json(parse_json(argv
[1]));
429 check_ovsdb_error(ovsdb_type_from_json(&type
, json
));
432 for (i
= 2; i
< argc
; i
++) {
433 struct ovsdb_datum datum
;
435 json
= unbox_json(parse_json(argv
[i
]));
436 check_ovsdb_error(ovsdb_datum_from_json(&datum
, &type
, json
, NULL
));
439 print_and_free_json(ovsdb_datum_to_json(&datum
, &type
));
441 ovsdb_datum_destroy(&datum
, &type
);
443 ovsdb_type_destroy(&type
);
447 do_parse_data_strings(int argc
, char *argv
[])
449 struct ovsdb_type type
;
453 json
= unbox_json(parse_json(argv
[1]));
454 check_ovsdb_error(ovsdb_type_from_json(&type
, json
));
457 for (i
= 2; i
< argc
; i
++) {
458 struct ovsdb_datum datum
;
461 die_if_error(ovsdb_datum_from_string(&datum
, &type
, argv
[i
]));
464 ovsdb_datum_to_string(&datum
, &type
, &out
);
468 ovsdb_datum_destroy(&datum
, &type
);
470 ovsdb_type_destroy(&type
);
473 static enum ovsdb_atomic_type compare_atoms_atomic_type
;
476 compare_atoms(const void *a_
, const void *b_
)
478 const union ovsdb_atom
*a
= a_
;
479 const union ovsdb_atom
*b
= b_
;
481 return ovsdb_atom_compare_3way(a
, b
, compare_atoms_atomic_type
);
485 do_sort_atoms(int argc OVS_UNUSED
, char *argv
[])
487 struct ovsdb_base_type base
;
488 union ovsdb_atom
*atoms
;
489 struct json
*json
, **json_atoms
;
493 json
= unbox_json(parse_json(argv
[1]));
494 check_ovsdb_error(ovsdb_base_type_from_json(&base
, json
));
497 json
= unbox_json(parse_json(argv
[2]));
498 if (json
->type
!= JSON_ARRAY
) {
499 ovs_fatal(0, "second argument must be array");
502 /* Convert JSON atoms to internal representation. */
503 n_atoms
= json
->u
.array
.n
;
504 atoms
= xmalloc(n_atoms
* sizeof *atoms
);
505 for (i
= 0; i
< n_atoms
; i
++) {
506 check_ovsdb_error(ovsdb_atom_from_json(&atoms
[i
], &base
,
507 json
->u
.array
.elems
[i
], NULL
));
512 compare_atoms_atomic_type
= base
.type
;
513 qsort(atoms
, n_atoms
, sizeof *atoms
, compare_atoms
);
515 /* Convert internal representation back to JSON. */
516 json_atoms
= xmalloc(n_atoms
* sizeof *json_atoms
);
517 for (i
= 0; i
< n_atoms
; i
++) {
518 json_atoms
[i
] = ovsdb_atom_to_json(&atoms
[i
], base
.type
);
519 ovsdb_atom_destroy(&atoms
[i
], base
.type
);
521 print_and_free_json(json_array_create(json_atoms
, n_atoms
));
523 ovsdb_base_type_destroy(&base
);
527 do_parse_column(int argc OVS_UNUSED
, char *argv
[])
529 struct ovsdb_column
*column
;
532 json
= parse_json(argv
[2]);
533 check_ovsdb_error(ovsdb_column_from_json(json
, argv
[1], &column
));
535 print_and_free_json(ovsdb_column_to_json(column
));
536 ovsdb_column_destroy(column
);
540 do_parse_table(int argc OVS_UNUSED
, char *argv
[])
542 struct ovsdb_table_schema
*ts
;
545 json
= parse_json(argv
[2]);
546 check_ovsdb_error(ovsdb_table_schema_from_json(json
, argv
[1], &ts
));
548 print_and_free_json(ovsdb_table_schema_to_json(ts
));
549 ovsdb_table_schema_destroy(ts
);
553 do_parse_rows(int argc
, char *argv
[])
555 struct ovsdb_column_set all_columns
;
556 struct ovsdb_table_schema
*ts
;
557 struct ovsdb_table
*table
;
561 json
= unbox_json(parse_json(argv
[1]));
562 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
565 table
= ovsdb_table_create(ts
);
566 ovsdb_column_set_init(&all_columns
);
567 ovsdb_column_set_add_all(&all_columns
, table
);
569 for (i
= 2; i
< argc
; i
++) {
570 struct ovsdb_column_set columns
;
571 struct ovsdb_row
*row
;
573 ovsdb_column_set_init(&columns
);
574 row
= ovsdb_row_create(table
);
576 json
= unbox_json(parse_json(argv
[i
]));
577 check_ovsdb_error(ovsdb_row_from_json(row
, json
, NULL
, &columns
));
580 print_and_free_json(ovsdb_row_to_json(row
, &all_columns
));
582 if (columns
.n_columns
) {
588 for (j
= 0; j
< columns
.n_columns
; j
++) {
589 svec_add(&names
, columns
.columns
[j
]->name
);
592 s
= svec_join(&names
, ", ", "");
595 svec_destroy(&names
);
600 ovsdb_column_set_destroy(&columns
);
601 ovsdb_row_destroy(row
);
604 ovsdb_column_set_destroy(&all_columns
);
605 ovsdb_table_destroy(table
); /* Also destroys 'ts'. */
609 do_compare_rows(int argc
, char *argv
[])
611 struct ovsdb_column_set all_columns
;
612 struct ovsdb_table_schema
*ts
;
613 struct ovsdb_table
*table
;
614 struct ovsdb_row
**rows
;
620 json
= unbox_json(parse_json(argv
[1]));
621 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
624 table
= ovsdb_table_create(ts
);
625 ovsdb_column_set_init(&all_columns
);
626 ovsdb_column_set_add_all(&all_columns
, table
);
629 rows
= xmalloc(sizeof *rows
* n_rows
);
630 names
= xmalloc(sizeof *names
* n_rows
);
631 for (i
= 0; i
< n_rows
; i
++) {
632 rows
[i
] = ovsdb_row_create(table
);
634 json
= parse_json(argv
[i
+ 2]);
635 if (json
->type
!= JSON_ARRAY
|| json
->u
.array
.n
!= 2
636 || json
->u
.array
.elems
[0]->type
!= JSON_STRING
) {
637 ovs_fatal(0, "\"%s\" does not have expected form "
638 "[\"name\", {data}]", argv
[i
]);
640 names
[i
] = xstrdup(json
->u
.array
.elems
[0]->u
.string
);
641 check_ovsdb_error(ovsdb_row_from_json(rows
[i
], json
->u
.array
.elems
[1],
645 for (i
= 0; i
< n_rows
; i
++) {
646 uint32_t i_hash
= ovsdb_row_hash_columns(rows
[i
], &all_columns
, 0);
647 for (j
= i
+ 1; j
< n_rows
; j
++) {
648 uint32_t j_hash
= ovsdb_row_hash_columns(rows
[j
], &all_columns
, 0);
649 if (ovsdb_row_equal_columns(rows
[i
], rows
[j
], &all_columns
)) {
650 printf("%s == %s\n", names
[i
], names
[j
]);
651 if (i_hash
!= j_hash
) {
652 printf("but hash(%s) != hash(%s)\n", names
[i
], names
[j
]);
655 } else if (i_hash
== j_hash
) {
656 printf("hash(%s) == hash(%s)\n", names
[i
], names
[j
]);
660 for (i
= 0; i
< n_rows
; i
++) {
661 ovsdb_row_destroy(rows
[i
]);
667 ovsdb_column_set_destroy(&all_columns
);
668 ovsdb_table_destroy(table
); /* Also destroys 'ts'. */
672 do_parse_conditions(int argc
, char *argv
[])
674 struct ovsdb_table_schema
*ts
;
679 json
= unbox_json(parse_json(argv
[1]));
680 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
683 for (i
= 2; i
< argc
; i
++) {
684 struct ovsdb_condition cnd
;
685 struct ovsdb_error
*error
;
687 json
= parse_json(argv
[i
]);
688 error
= ovsdb_condition_from_json(ts
, json
, NULL
, &cnd
);
690 print_and_free_json(ovsdb_condition_to_json(&cnd
));
692 char *s
= ovsdb_error_to_string(error
);
693 ovs_error(0, "%s", s
);
695 ovsdb_error_destroy(error
);
700 ovsdb_condition_destroy(&cnd
);
702 ovsdb_table_schema_destroy(ts
);
708 do_evaluate_conditions(int argc OVS_UNUSED
, char *argv
[])
710 struct ovsdb_table_schema
*ts
;
711 struct ovsdb_table
*table
;
712 struct ovsdb_condition
*conditions
;
714 struct ovsdb_row
**rows
;
719 /* Parse table schema, create table. */
720 json
= unbox_json(parse_json(argv
[1]));
721 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
724 table
= ovsdb_table_create(ts
);
726 /* Parse conditions. */
727 json
= parse_json(argv
[2]);
728 if (json
->type
!= JSON_ARRAY
) {
729 ovs_fatal(0, "CONDITION argument is not JSON array");
731 n_conditions
= json
->u
.array
.n
;
732 conditions
= xmalloc(n_conditions
* sizeof *conditions
);
733 for (i
= 0; i
< n_conditions
; i
++) {
734 check_ovsdb_error(ovsdb_condition_from_json(ts
, json
->u
.array
.elems
[i
],
735 NULL
, &conditions
[i
]));
740 json
= parse_json(argv
[3]);
741 if (json
->type
!= JSON_ARRAY
) {
742 ovs_fatal(0, "ROW argument is not JSON array");
744 n_rows
= json
->u
.array
.n
;
745 rows
= xmalloc(n_rows
* sizeof *rows
);
746 for (i
= 0; i
< n_rows
; i
++) {
747 rows
[i
] = ovsdb_row_create(table
);
748 check_ovsdb_error(ovsdb_row_from_json(rows
[i
], json
->u
.array
.elems
[i
],
753 for (i
= 0; i
< n_conditions
; i
++) {
754 printf("condition %2d:", i
);
755 for (j
= 0; j
< n_rows
; j
++) {
756 bool result
= ovsdb_condition_evaluate(rows
[j
], &conditions
[i
]);
760 putchar(result
? 'T' : '-');
765 for (i
= 0; i
< n_conditions
; i
++) {
766 ovsdb_condition_destroy(&conditions
[i
]);
769 for (i
= 0; i
< n_rows
; i
++) {
770 ovsdb_row_destroy(rows
[i
]);
773 ovsdb_table_destroy(table
); /* Also destroys 'ts'. */
777 do_parse_mutations(int argc
, char *argv
[])
779 struct ovsdb_table_schema
*ts
;
784 json
= unbox_json(parse_json(argv
[1]));
785 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
788 for (i
= 2; i
< argc
; i
++) {
789 struct ovsdb_mutation_set set
;
790 struct ovsdb_error
*error
;
792 json
= parse_json(argv
[i
]);
793 error
= ovsdb_mutation_set_from_json(ts
, json
, NULL
, &set
);
795 print_and_free_json(ovsdb_mutation_set_to_json(&set
));
797 char *s
= ovsdb_error_to_string(error
);
798 ovs_error(0, "%s", s
);
800 ovsdb_error_destroy(error
);
805 ovsdb_mutation_set_destroy(&set
);
807 ovsdb_table_schema_destroy(ts
);
813 do_execute_mutations(int argc OVS_UNUSED
, char *argv
[])
815 struct ovsdb_table_schema
*ts
;
816 struct ovsdb_table
*table
;
817 struct ovsdb_mutation_set
*sets
;
819 struct ovsdb_row
**rows
;
824 /* Parse table schema, create table. */
825 json
= unbox_json(parse_json(argv
[1]));
826 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
829 table
= ovsdb_table_create(ts
);
831 /* Parse mutations. */
832 json
= parse_json(argv
[2]);
833 if (json
->type
!= JSON_ARRAY
) {
834 ovs_fatal(0, "MUTATION argument is not JSON array");
836 n_sets
= json
->u
.array
.n
;
837 sets
= xmalloc(n_sets
* sizeof *sets
);
838 for (i
= 0; i
< n_sets
; i
++) {
839 check_ovsdb_error(ovsdb_mutation_set_from_json(ts
,
840 json
->u
.array
.elems
[i
],
846 json
= parse_json(argv
[3]);
847 if (json
->type
!= JSON_ARRAY
) {
848 ovs_fatal(0, "ROW argument is not JSON array");
850 n_rows
= json
->u
.array
.n
;
851 rows
= xmalloc(n_rows
* sizeof *rows
);
852 for (i
= 0; i
< n_rows
; i
++) {
853 rows
[i
] = ovsdb_row_create(table
);
854 check_ovsdb_error(ovsdb_row_from_json(rows
[i
], json
->u
.array
.elems
[i
],
859 for (i
= 0; i
< n_sets
; i
++) {
860 printf("mutation %2d:\n", i
);
861 for (j
= 0; j
< n_rows
; j
++) {
862 struct ovsdb_error
*error
;
863 struct ovsdb_row
*row
;
865 row
= ovsdb_row_clone(rows
[j
]);
866 error
= ovsdb_mutation_set_execute(row
, &sets
[i
]);
868 printf("row %zu: ", j
);
870 print_and_free_ovsdb_error(error
);
872 struct ovsdb_column_set columns
;
873 struct shash_node
*node
;
875 ovsdb_column_set_init(&columns
);
876 SHASH_FOR_EACH (node
, &ts
->columns
) {
877 struct ovsdb_column
*c
= node
->data
;
878 if (!ovsdb_datum_equals(&row
->fields
[c
->index
],
879 &rows
[j
]->fields
[c
->index
],
881 ovsdb_column_set_add(&columns
, c
);
884 if (columns
.n_columns
) {
885 print_and_free_json(ovsdb_row_to_json(row
, &columns
));
887 printf("no change\n");
889 ovsdb_column_set_destroy(&columns
);
891 ovsdb_row_destroy(row
);
896 for (i
= 0; i
< n_sets
; i
++) {
897 ovsdb_mutation_set_destroy(&sets
[i
]);
900 for (i
= 0; i
< n_rows
; i
++) {
901 ovsdb_row_destroy(rows
[i
]);
904 ovsdb_table_destroy(table
); /* Also destroys 'ts'. */
907 struct do_query_cbdata
{
908 struct uuid
*row_uuids
;
914 do_query_cb(const struct ovsdb_row
*row
, void *cbdata_
)
916 struct do_query_cbdata
*cbdata
= cbdata_
;
919 for (i
= 0; i
< cbdata
->n_rows
; i
++) {
920 if (uuid_equals(ovsdb_row_get_uuid(row
), &cbdata
->row_uuids
[i
])) {
929 do_query(int argc OVS_UNUSED
, char *argv
[])
931 struct do_query_cbdata cbdata
;
932 struct ovsdb_table_schema
*ts
;
933 struct ovsdb_table
*table
;
938 /* Parse table schema, create table. */
939 json
= unbox_json(parse_json(argv
[1]));
940 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
943 table
= ovsdb_table_create(ts
);
945 /* Parse rows, add to table. */
946 json
= parse_json(argv
[2]);
947 if (json
->type
!= JSON_ARRAY
) {
948 ovs_fatal(0, "ROW argument is not JSON array");
950 cbdata
.n_rows
= json
->u
.array
.n
;
951 cbdata
.row_uuids
= xmalloc(cbdata
.n_rows
* sizeof *cbdata
.row_uuids
);
952 cbdata
.counts
= xmalloc(cbdata
.n_rows
* sizeof *cbdata
.counts
);
953 for (i
= 0; i
< cbdata
.n_rows
; i
++) {
954 struct ovsdb_row
*row
= ovsdb_row_create(table
);
955 uuid_generate(ovsdb_row_get_uuid_rw(row
));
956 check_ovsdb_error(ovsdb_row_from_json(row
, json
->u
.array
.elems
[i
],
958 if (ovsdb_table_get_row(table
, ovsdb_row_get_uuid(row
))) {
959 ovs_fatal(0, "duplicate UUID "UUID_FMT
" in table",
960 UUID_ARGS(ovsdb_row_get_uuid(row
)));
962 cbdata
.row_uuids
[i
] = *ovsdb_row_get_uuid(row
);
963 ovsdb_table_put_row(table
, row
);
967 /* Parse conditions and execute queries. */
968 json
= parse_json(argv
[3]);
969 if (json
->type
!= JSON_ARRAY
) {
970 ovs_fatal(0, "CONDITION argument is not JSON array");
972 for (i
= 0; i
< json
->u
.array
.n
; i
++) {
973 struct ovsdb_condition cnd
;
976 check_ovsdb_error(ovsdb_condition_from_json(ts
, json
->u
.array
.elems
[i
],
979 memset(cbdata
.counts
, 0, cbdata
.n_rows
* sizeof *cbdata
.counts
);
980 ovsdb_query(table
, &cnd
, do_query_cb
, &cbdata
);
982 printf("query %2d:", i
);
983 for (j
= 0; j
< cbdata
.n_rows
; j
++) {
987 if (cbdata
.counts
[j
]) {
988 printf("%d", cbdata
.counts
[j
]);
989 if (cbdata
.counts
[j
] > 1) {
999 ovsdb_condition_destroy(&cnd
);
1003 ovsdb_table_destroy(table
); /* Also destroys 'ts'. */
1008 struct do_query_distinct_class
{
1009 struct ovsdb_row
*example
;
1013 struct do_query_distinct_row
{
1015 struct do_query_distinct_class
*class;
1019 do_query_distinct(int argc OVS_UNUSED
, char *argv
[])
1021 struct ovsdb_column_set columns
;
1022 struct ovsdb_table_schema
*ts
;
1023 struct ovsdb_table
*table
;
1024 struct do_query_distinct_row
*rows
;
1026 struct do_query_distinct_class
*classes
;
1032 /* Parse table schema, create table. */
1033 json
= unbox_json(parse_json(argv
[1]));
1034 check_ovsdb_error(ovsdb_table_schema_from_json(json
, "mytable", &ts
));
1037 table
= ovsdb_table_create(ts
);
1039 /* Parse column set. */
1040 json
= parse_json(argv
[4]);
1041 check_ovsdb_error(ovsdb_column_set_from_json(json
, table
, &columns
));
1044 /* Parse rows, add to table. */
1045 json
= parse_json(argv
[2]);
1046 if (json
->type
!= JSON_ARRAY
) {
1047 ovs_fatal(0, "ROW argument is not JSON array");
1049 n_rows
= json
->u
.array
.n
;
1050 rows
= xmalloc(n_rows
* sizeof *rows
);
1051 classes
= xmalloc(n_rows
* sizeof *classes
);
1053 for (i
= 0; i
< n_rows
; i
++) {
1054 struct ovsdb_row
*row
;
1058 row
= ovsdb_row_create(table
);
1059 uuid_generate(ovsdb_row_get_uuid_rw(row
));
1060 check_ovsdb_error(ovsdb_row_from_json(row
, json
->u
.array
.elems
[i
],
1063 /* Initialize row and find equivalence class. */
1064 rows
[i
].uuid
= *ovsdb_row_get_uuid(row
);
1065 rows
[i
].class = NULL
;
1066 for (j
= 0; j
< n_classes
; j
++) {
1067 if (ovsdb_row_equal_columns(row
, classes
[j
].example
, &columns
)) {
1068 rows
[i
].class = &classes
[j
];
1072 if (!rows
[i
].class) {
1073 rows
[i
].class = &classes
[n_classes
];
1074 classes
[n_classes
].example
= ovsdb_row_clone(row
);
1078 /* Add row to table. */
1079 if (ovsdb_table_get_row(table
, ovsdb_row_get_uuid(row
))) {
1080 ovs_fatal(0, "duplicate UUID "UUID_FMT
" in table",
1081 UUID_ARGS(ovsdb_row_get_uuid(row
)));
1083 ovsdb_table_put_row(table
, row
);
1088 /* Parse conditions and execute queries. */
1089 json
= parse_json(argv
[3]);
1090 if (json
->type
!= JSON_ARRAY
) {
1091 ovs_fatal(0, "CONDITION argument is not JSON array");
1093 for (i
= 0; i
< json
->u
.array
.n
; i
++) {
1094 struct ovsdb_row_set results
;
1095 struct ovsdb_condition cnd
;
1097 check_ovsdb_error(ovsdb_condition_from_json(ts
, json
->u
.array
.elems
[i
],
1100 for (j
= 0; j
< n_classes
; j
++) {
1101 classes
[j
].count
= 0;
1103 ovsdb_row_set_init(&results
);
1104 ovsdb_query_distinct(table
, &cnd
, &columns
, &results
);
1105 for (j
= 0; j
< results
.n_rows
; j
++) {
1106 for (k
= 0; k
< n_rows
; k
++) {
1107 if (uuid_equals(ovsdb_row_get_uuid(results
.rows
[j
]),
1109 rows
[k
].class->count
++;
1113 ovsdb_row_set_destroy(&results
);
1115 printf("query %2d:", i
);
1116 for (j
= 0; j
< n_rows
; j
++) {
1117 int count
= rows
[j
].class->count
;
1124 printf("%d", count
);
1126 } else if (count
== 1) {
1127 putchar("abcdefghijklmnopqrstuvwxyz"[rows
[j
].class - classes
]);
1134 ovsdb_condition_destroy(&cnd
);
1138 ovsdb_table_destroy(table
); /* Also destroys 'ts'. */
1144 do_parse_schema(int argc OVS_UNUSED
, char *argv
[])
1146 struct ovsdb_schema
*schema
;
1149 json
= parse_json(argv
[1]);
1150 check_ovsdb_error(ovsdb_schema_from_json(json
, &schema
));
1152 print_and_free_json(ovsdb_schema_to_json(schema
));
1153 ovsdb_schema_destroy(schema
);
1157 do_execute(int argc OVS_UNUSED
, char *argv
[])
1159 struct ovsdb_schema
*schema
;
1164 /* Create database. */
1165 json
= parse_json(argv
[1]);
1166 check_ovsdb_error(ovsdb_schema_from_json(json
, &schema
));
1168 db
= ovsdb_create(schema
);
1170 for (i
= 2; i
< argc
; i
++) {
1171 struct json
*params
, *result
;
1174 params
= parse_json(argv
[i
]);
1175 result
= ovsdb_execute(db
, params
, 0, NULL
);
1176 s
= json_to_string(result
, JSSF_SORT
);
1179 json_destroy(params
);
1180 json_destroy(result
);
1186 struct test_trigger
{
1187 struct ovsdb_trigger trigger
;
1192 do_trigger_dump(struct test_trigger
*t
, long long int now
, const char *title
)
1194 struct json
*result
;
1197 result
= ovsdb_trigger_steal_result(&t
->trigger
);
1198 s
= json_to_string(result
, JSSF_SORT
);
1199 printf("t=%lld: trigger %d (%s): %s\n", now
, t
->number
, title
, s
);
1201 json_destroy(result
);
1202 ovsdb_trigger_destroy(&t
->trigger
);
1207 do_trigger(int argc OVS_UNUSED
, char *argv
[])
1209 struct ovsdb_schema
*schema
;
1210 struct list completions
;
1217 /* Create database. */
1218 json
= parse_json(argv
[1]);
1219 check_ovsdb_error(ovsdb_schema_from_json(json
, &schema
));
1221 db
= ovsdb_create(schema
);
1223 list_init(&completions
);
1226 for (i
= 2; i
< argc
; i
++) {
1227 struct json
*params
= parse_json(argv
[i
]);
1228 if (params
->type
== JSON_ARRAY
1229 && json_array(params
)->n
== 2
1230 && json_array(params
)->elems
[0]->type
== JSON_STRING
1231 && !strcmp(json_string(json_array(params
)->elems
[0]), "advance")
1232 && json_array(params
)->elems
[1]->type
== JSON_INTEGER
) {
1233 now
+= json_integer(json_array(params
)->elems
[1]);
1234 json_destroy(params
);
1236 struct test_trigger
*t
= xmalloc(sizeof *t
);
1237 ovsdb_trigger_init(db
, &t
->trigger
, params
, &completions
, now
);
1238 t
->number
= number
++;
1239 if (ovsdb_trigger_is_complete(&t
->trigger
)) {
1240 do_trigger_dump(t
, now
, "immediate");
1242 printf("t=%lld: new trigger %d\n", now
, t
->number
);
1246 ovsdb_trigger_run(db
, now
);
1247 while (!list_is_empty(&completions
)) {
1248 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions
),
1249 struct test_trigger
, trigger
.node
),
1253 ovsdb_trigger_wait(db
, now
);
1254 poll_immediate_wake();
1262 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1267 /* "transact" command. */
1269 static struct ovsdb
*do_transact_db
;
1270 static struct ovsdb_txn
*do_transact_txn
;
1271 static struct ovsdb_table
*do_transact_table
;
1274 do_transact_commit(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1276 ovsdb_txn_commit(do_transact_txn
, false);
1277 do_transact_txn
= NULL
;
1281 do_transact_abort(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1283 ovsdb_txn_abort(do_transact_txn
);
1284 do_transact_txn
= NULL
;
1288 uuid_from_integer(int integer
, struct uuid
*uuid
)
1291 uuid
->parts
[3] = integer
;
1294 static const struct ovsdb_row
*
1295 do_transact_find_row(const char *uuid_string
)
1297 const struct ovsdb_row
*row
;
1300 uuid_from_integer(atoi(uuid_string
), &uuid
);
1301 row
= ovsdb_table_get_row(do_transact_table
, &uuid
);
1303 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT
,
1310 do_transact_set_integer(struct ovsdb_row
*row
, const char *column_name
,
1313 if (integer
!= -1) {
1314 const struct ovsdb_column
*column
;
1316 column
= ovsdb_table_schema_get_column(do_transact_table
->schema
,
1318 row
->fields
[column
->index
].keys
[0].integer
= integer
;
1323 do_transact_get_integer(const struct ovsdb_row
*row
, const char *column_name
)
1325 const struct ovsdb_column
*column
;
1327 column
= ovsdb_table_schema_get_column(do_transact_table
->schema
,
1329 return row
->fields
[column
->index
].keys
[0].integer
;
1333 do_transact_set_i_j(struct ovsdb_row
*row
,
1334 const char *i_string
, const char *j_string
)
1336 do_transact_set_integer(row
, "i", atoi(i_string
));
1337 do_transact_set_integer(row
, "j", atoi(j_string
));
1341 do_transact_insert(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1343 struct ovsdb_row
*row
;
1346 row
= ovsdb_row_create(do_transact_table
);
1349 uuid
= ovsdb_row_get_uuid_rw(row
);
1350 uuid_from_integer(atoi(argv
[1]), uuid
);
1351 if (ovsdb_table_get_row(do_transact_table
, uuid
)) {
1352 ovs_fatal(0, "table already contains row with UUID "UUID_FMT
,
1356 do_transact_set_i_j(row
, argv
[2], argv
[3]);
1359 ovsdb_txn_row_insert(do_transact_txn
, row
);
1363 do_transact_delete(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1365 const struct ovsdb_row
*row
= do_transact_find_row(argv
[1]);
1366 ovsdb_txn_row_delete(do_transact_txn
, row
);
1370 do_transact_modify(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1372 const struct ovsdb_row
*row_ro
;
1373 struct ovsdb_row
*row_rw
;
1375 row_ro
= do_transact_find_row(argv
[1]);
1376 row_rw
= ovsdb_txn_row_modify(do_transact_txn
, row_ro
);
1377 do_transact_set_i_j(row_rw
, argv
[2], argv
[3]);
1381 compare_rows_by_uuid(const void *a_
, const void *b_
)
1383 struct ovsdb_row
*const *ap
= a_
;
1384 struct ovsdb_row
*const *bp
= b_
;
1386 return uuid_compare_3way(ovsdb_row_get_uuid(*ap
), ovsdb_row_get_uuid(*bp
));
1390 do_transact_print(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1392 const struct ovsdb_row
**rows
;
1393 const struct ovsdb_row
*row
;
1397 n_rows
= hmap_count(&do_transact_table
->rows
);
1398 rows
= xmalloc(n_rows
* sizeof *rows
);
1400 HMAP_FOR_EACH (row
, struct ovsdb_row
, hmap_node
,
1401 &do_transact_table
->rows
) {
1404 assert(i
== n_rows
);
1406 qsort(rows
, n_rows
, sizeof *rows
, compare_rows_by_uuid
);
1408 for (i
= 0; i
< n_rows
; i
++) {
1409 printf("\n%"PRId32
": i=%d, j=%d",
1410 ovsdb_row_get_uuid(rows
[i
])->parts
[3],
1411 do_transact_get_integer(rows
[i
], "i"),
1412 do_transact_get_integer(rows
[i
], "j"));
1419 do_transact(int argc
, char *argv
[])
1421 static const struct command do_transact_commands
[] = {
1422 { "commit", 0, 0, do_transact_commit
},
1423 { "abort", 0, 0, do_transact_abort
},
1424 { "insert", 2, 3, do_transact_insert
},
1425 { "delete", 1, 1, do_transact_delete
},
1426 { "modify", 2, 3, do_transact_modify
},
1427 { "print", 0, 0, do_transact_print
},
1428 { NULL
, 0, 0, NULL
},
1431 struct ovsdb_schema
*schema
;
1436 json
= parse_json("{\"name\": \"testdb\", "
1440 " {\"i\": {\"type\": \"integer\"}, "
1441 " \"j\": {\"type\": \"integer\"}}}}}");
1442 check_ovsdb_error(ovsdb_schema_from_json(json
, &schema
));
1444 do_transact_db
= ovsdb_create(schema
);
1445 do_transact_table
= ovsdb_get_table(do_transact_db
, "mytable");
1446 assert(do_transact_table
!= NULL
);
1448 for (i
= 1; i
< argc
; i
++) {
1449 struct json
*command
;
1454 command
= parse_json(argv
[i
]);
1455 if (command
->type
!= JSON_ARRAY
) {
1456 ovs_fatal(0, "transaction %d must be JSON array "
1457 "with at least 1 element", i
);
1460 n_args
= command
->u
.array
.n
;
1461 args
= xmalloc((n_args
+ 1) * sizeof *args
);
1462 for (j
= 0; j
< n_args
; j
++) {
1463 struct json
*s
= command
->u
.array
.elems
[j
];
1464 if (s
->type
!= JSON_STRING
) {
1465 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1468 args
[j
] = xstrdup(json_string(s
));
1470 args
[n_args
] = NULL
;
1472 if (!do_transact_txn
) {
1473 do_transact_txn
= ovsdb_txn_create(do_transact_db
);
1476 for (j
= 0; j
< n_args
; j
++) {
1480 fputs(args
[j
], stdout
);
1483 run_command(n_args
, args
, do_transact_commands
);
1486 for (j
= 0; j
< n_args
; j
++) {
1490 json_destroy(command
);
1492 ovsdb_txn_abort(do_transact_txn
);
1493 ovsdb_destroy(do_transact_db
); /* Also destroys 'schema'. */
1497 compare_link1(const void *a_
, const void *b_
)
1499 const struct idltest_link1
*const *ap
= a_
;
1500 const struct idltest_link1
*const *bp
= b_
;
1501 const struct idltest_link1
*a
= *ap
;
1502 const struct idltest_link1
*b
= *bp
;
1504 return a
->i
< b
->i
? -1 : a
->i
> b
->i
;
1508 print_idl(struct ovsdb_idl
*idl
, int step
)
1510 const struct idltest_simple
*s
;
1511 const struct idltest_link1
*l1
;
1512 const struct idltest_link2
*l2
;
1515 IDLTEST_SIMPLE_FOR_EACH (s
, idl
) {
1518 printf("%03d: i=%"PRId64
" r=%g b=%s s=%s u="UUID_FMT
" ia=[",
1519 step
, s
->i
, s
->r
, s
->b
? "true" : "false",
1520 s
->s
, UUID_ARGS(&s
->u
));
1521 for (i
= 0; i
< s
->n_ia
; i
++) {
1522 printf("%s%"PRId64
, i
? " " : "", s
->ia
[i
]);
1525 for (i
= 0; i
< s
->n_ra
; i
++) {
1526 printf("%s%g", i
? " " : "", s
->ra
[i
]);
1529 for (i
= 0; i
< s
->n_ba
; i
++) {
1530 printf("%s%s", i
? " " : "", s
->ba
[i
] ? "true" : "false");
1533 for (i
= 0; i
< s
->n_sa
; i
++) {
1534 printf("%s%s", i
? " " : "", s
->sa
[i
]);
1537 for (i
= 0; i
< s
->n_ua
; i
++) {
1538 printf("%s"UUID_FMT
, i
? " " : "", UUID_ARGS(&s
->ua
[i
]));
1540 printf("] uuid="UUID_FMT
"\n", UUID_ARGS(&s
->header_
.uuid
));
1543 IDLTEST_LINK1_FOR_EACH (l1
, idl
) {
1544 struct idltest_link1
**links
;
1547 printf("%03d: i=%"PRId64
" k=", step
, l1
->i
);
1549 printf("%"PRId64
, l1
->k
->i
);
1552 links
= xmemdup(l1
->ka
, l1
->n_ka
* sizeof *l1
->ka
);
1553 qsort(links
, l1
->n_ka
, sizeof *links
, compare_link1
);
1554 for (i
= 0; i
< l1
->n_ka
; i
++) {
1555 printf("%s%"PRId64
, i
? " " : "", links
[i
]->i
);
1560 printf("%"PRId64
, l1
->l2
->i
);
1562 printf(" uuid="UUID_FMT
"\n", UUID_ARGS(&l1
->header_
.uuid
));
1565 IDLTEST_LINK2_FOR_EACH (l2
, idl
) {
1566 printf("%03d: i=%"PRId64
" l1=", step
, l2
->i
);
1568 printf("%"PRId64
, l2
->l1
->i
);
1570 printf(" uuid="UUID_FMT
"\n", UUID_ARGS(&l2
->header_
.uuid
));
1574 printf("%03d: empty\n", step
);
1579 print_updated_idl(struct ovsdb_idl
*idl
, struct jsonrpc
*rpc
,
1580 int step
, unsigned int seqno
)
1583 unsigned int new_seqno
;
1589 new_seqno
= ovsdb_idl_get_seqno(idl
);
1590 if (new_seqno
!= seqno
) {
1591 print_idl(idl
, step
);
1598 ovsdb_idl_wait(idl
);
1604 parse_uuids(const struct json
*json
, struct ovsdb_symbol_table
*symtab
,
1609 if (json
->type
== JSON_STRING
&& uuid_from_string(&uuid
, json
->u
.string
)) {
1610 char *name
= xasprintf("#%d#", *n
);
1611 fprintf(stderr
, "%s = "UUID_FMT
"\n", name
, UUID_ARGS(&uuid
));
1612 ovsdb_symbol_table_put(symtab
, name
, &uuid
, false);
1615 } else if (json
->type
== JSON_ARRAY
) {
1618 for (i
= 0; i
< json
->u
.array
.n
; i
++) {
1619 parse_uuids(json
->u
.array
.elems
[i
], symtab
, n
);
1621 } else if (json
->type
== JSON_OBJECT
) {
1622 const struct shash_node
*node
;
1624 SHASH_FOR_EACH (node
, json_object(json
)) {
1625 parse_uuids(node
->data
, symtab
, n
);
1631 substitute_uuids(struct json
*json
, const struct ovsdb_symbol_table
*symtab
)
1633 if (json
->type
== JSON_STRING
) {
1634 const struct ovsdb_symbol
*symbol
;
1636 symbol
= ovsdb_symbol_table_get(symtab
, json
->u
.string
);
1638 free(json
->u
.string
);
1639 json
->u
.string
= xasprintf(UUID_FMT
, UUID_ARGS(&symbol
->uuid
));
1641 } else if (json
->type
== JSON_ARRAY
) {
1644 for (i
= 0; i
< json
->u
.array
.n
; i
++) {
1645 substitute_uuids(json
->u
.array
.elems
[i
], symtab
);
1647 } else if (json
->type
== JSON_OBJECT
) {
1648 const struct shash_node
*node
;
1650 SHASH_FOR_EACH (node
, json_object(json
)) {
1651 substitute_uuids(node
->data
, symtab
);
1656 static const struct idltest_simple
*
1657 idltest_find_simple(struct ovsdb_idl
*idl
, int i
)
1659 const struct idltest_simple
*s
;
1661 IDLTEST_SIMPLE_FOR_EACH (s
, idl
) {
1670 idl_set(struct ovsdb_idl
*idl
, char *commands
, int step
)
1672 char *cmd
, *save_ptr1
= NULL
;
1673 struct ovsdb_idl_txn
*txn
;
1674 enum ovsdb_idl_txn_status status
;
1675 bool increment
= false;
1677 txn
= ovsdb_idl_txn_create(idl
);
1678 for (cmd
= strtok_r(commands
, ",", &save_ptr1
); cmd
;
1679 cmd
= strtok_r(NULL
, ",", &save_ptr1
)) {
1680 char *save_ptr2
= NULL
;
1681 char *name
, *arg1
, *arg2
, *arg3
;
1683 name
= strtok_r(cmd
, " ", &save_ptr2
);
1684 arg1
= strtok_r(NULL
, " ", &save_ptr2
);
1685 arg2
= strtok_r(NULL
, " ", &save_ptr2
);
1686 arg3
= strtok_r(NULL
, " ", &save_ptr2
);
1688 if (!strcmp(name
, "set")) {
1689 const struct idltest_simple
*s
;
1692 ovs_fatal(0, "\"set\" command requires 3 arguments");
1695 s
= idltest_find_simple(idl
, atoi(arg1
));
1697 ovs_fatal(0, "\"set\" command asks for nonexistent "
1698 "i=%d", atoi(arg1
));
1701 if (!strcmp(arg2
, "b")) {
1702 idltest_simple_set_b(s
, atoi(arg3
));
1703 } else if (!strcmp(arg2
, "s")) {
1704 idltest_simple_set_s(s
, arg3
);
1705 } else if (!strcmp(arg2
, "u")) {
1707 uuid_from_string(&uuid
, arg3
);
1708 idltest_simple_set_u(s
, uuid
);
1709 } else if (!strcmp(arg2
, "r")) {
1710 idltest_simple_set_r(s
, atof(arg3
));
1712 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1715 } else if (!strcmp(name
, "insert")) {
1716 struct idltest_simple
*s
;
1718 if (!arg1
|| arg2
) {
1719 ovs_fatal(0, "\"set\" command requires 1 argument");
1722 s
= idltest_simple_insert(txn
);
1723 idltest_simple_set_i(s
, atoi(arg1
));
1724 } else if (!strcmp(name
, "delete")) {
1725 const struct idltest_simple
*s
;
1727 if (!arg1
|| arg2
) {
1728 ovs_fatal(0, "\"set\" command requires 1 argument");
1731 s
= idltest_find_simple(idl
, atoi(arg1
));
1733 ovs_fatal(0, "\"set\" command asks for nonexistent "
1734 "i=%d", atoi(arg1
));
1736 idltest_simple_delete(s
);
1737 } else if (!strcmp(name
, "increment")) {
1738 if (!arg2
|| arg3
) {
1739 ovs_fatal(0, "\"set\" command requires 2 arguments");
1741 ovsdb_idl_txn_increment(txn
, arg1
, arg2
, NULL
);
1744 ovs_fatal(0, "unknown command %s", name
);
1748 while ((status
= ovsdb_idl_txn_commit(txn
)) == TXN_INCOMPLETE
) {
1750 ovsdb_idl_wait(idl
);
1751 ovsdb_idl_txn_wait(txn
);
1754 printf("%03d: commit, status=%s",
1755 step
, ovsdb_idl_txn_status_to_string(status
));
1757 printf(", increment=%"PRId64
,
1758 ovsdb_idl_txn_get_increment_new_value(txn
));
1761 ovsdb_idl_txn_destroy(txn
);
1765 do_idl(int argc
, char *argv
[])
1767 struct jsonrpc
*rpc
;
1768 struct ovsdb_idl
*idl
;
1769 unsigned int seqno
= 0;
1770 struct ovsdb_symbol_table
*symtab
;
1778 idl
= ovsdb_idl_create(argv
[1], &idltest_idl_class
);
1780 struct stream
*stream
;
1782 error
= stream_open_block(argv
[1], &stream
);
1784 ovs_fatal(error
, "failed to connect to \"%s\"", argv
[1]);
1786 rpc
= jsonrpc_open(stream
);
1791 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1793 symtab
= ovsdb_symbol_table_create();
1794 for (i
= 2; i
< argc
; i
++) {
1795 char *arg
= argv
[i
];
1796 struct jsonrpc_msg
*request
, *reply
;
1800 /* The previous transaction didn't change anything. */
1803 seqno
= print_updated_idl(idl
, rpc
, step
++, seqno
);
1806 if (!strcmp(arg
, "reconnect")) {
1807 printf("%03d: reconnect\n", step
++);
1808 ovsdb_idl_force_reconnect(idl
);
1809 } else if (arg
[0] != '[') {
1810 idl_set(idl
, arg
, step
++);
1812 struct json
*json
= parse_json(arg
);
1813 substitute_uuids(json
, symtab
);
1814 request
= jsonrpc_create_request("transact", json
, NULL
);
1815 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
1817 ovs_fatal(error
, "jsonrpc transaction failed");
1819 printf("%03d: ", step
++);
1820 if (reply
->result
) {
1821 parse_uuids(reply
->result
, symtab
, &n_uuids
);
1823 json_destroy(reply
->id
);
1825 print_and_free_json(jsonrpc_msg_to_json(reply
));
1828 ovsdb_symbol_table_destroy(symtab
);
1833 print_updated_idl(idl
, NULL
, step
++, seqno
);
1834 ovsdb_idl_destroy(idl
);
1835 printf("%03d: done\n", step
);
1838 static struct command all_commands
[] = {
1839 { "log-io", 2, INT_MAX
, do_log_io
},
1840 { "parse-atomic-type", 1, 1, do_parse_atomic_type
},
1841 { "parse-base-type", 1, 1, do_parse_base_type
},
1842 { "parse-type", 1, 1, do_parse_type
},
1843 { "parse-atoms", 2, INT_MAX
, do_parse_atoms
},
1844 { "parse-atom-strings", 2, INT_MAX
, do_parse_atom_strings
},
1845 { "parse-data", 2, INT_MAX
, do_parse_data
},
1846 { "parse-data-strings", 2, INT_MAX
, do_parse_data_strings
},
1847 { "sort-atoms", 2, 2, do_sort_atoms
},
1848 { "parse-column", 2, 2, do_parse_column
},
1849 { "parse-table", 2, 2, do_parse_table
},
1850 { "parse-rows", 2, INT_MAX
, do_parse_rows
},
1851 { "compare-rows", 2, INT_MAX
, do_compare_rows
},
1852 { "parse-conditions", 2, INT_MAX
, do_parse_conditions
},
1853 { "evaluate-conditions", 3, 3, do_evaluate_conditions
},
1854 { "parse-mutations", 2, INT_MAX
, do_parse_mutations
},
1855 { "execute-mutations", 3, 3, do_execute_mutations
},
1856 { "query", 3, 3, do_query
},
1857 { "query-distinct", 4, 4, do_query_distinct
},
1858 { "transact", 1, INT_MAX
, do_transact
},
1859 { "parse-schema", 1, 1, do_parse_schema
},
1860 { "execute", 2, INT_MAX
, do_execute
},
1861 { "trigger", 2, INT_MAX
, do_trigger
},
1862 { "idl", 1, INT_MAX
, do_idl
},
1863 { "help", 0, INT_MAX
, do_help
},
1864 { NULL
, 0, 0, NULL
},