2 * Copyright (c) 2009 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"
28 #include "ovsdb-data.h"
29 #include "ovsdb-error.h"
30 #include "ovsdb-types.h"
31 #include "ovsdb/column.h"
32 #include "ovsdb/condition.h"
33 #include "ovsdb/file.h"
34 #include "ovsdb/ovsdb.h"
35 #include "ovsdb/query.h"
36 #include "ovsdb/row.h"
37 #include "ovsdb/table.h"
38 #include "ovsdb/transaction.h"
39 #include "ovsdb/trigger.h"
40 #include "poll-loop.h"
46 static struct command all_commands[];
48 static void usage(void) NO_RETURN;
49 static void parse_options(int argc, char *argv[]);
52 main(int argc, char *argv[])
54 set_program_name(argv[0]);
57 parse_options(argc, argv);
58 run_command(argc - optind, argv + optind, all_commands);
63 parse_options(int argc, char *argv[])
65 static struct option long_options[] = {
66 {"verbose", optional_argument, 0, 'v'},
67 {"help", no_argument, 0, 'h'},
70 char *short_options = long_options_to_short_options(long_options);
73 int c = getopt_long(argc, argv, short_options, long_options, NULL);
83 vlog_set_verbosity(optarg);
99 printf("%s: Open vSwitch database test utility\n"
100 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
101 " file-io FILE FLAGS COMMAND...\n"
102 " open FILE with FLAGS, run COMMANDs\n"
103 " parse-atomic-type TYPE\n"
104 " parse TYPE as OVSDB atomic type, and re-serialize\n"
106 " parse JSON as OVSDB type, and re-serialize\n"
107 " parse-atoms TYPE ATOM...\n"
108 " parse ATOMs as atoms of given TYPE, and re-serialize\n"
109 " sort-atoms TYPE ATOM...\n"
110 " print ATOMs in sorted order, and re-serialize\n"
111 " parse-data TYPE DATUM...\n"
112 " parse DATUMs as data of given TYPE, and re-serialize\n"
113 " parse-column NAME OBJECT\n"
114 " parse column NAME with info OBJECT, and re-serialize\n"
115 " parse-table NAME OBJECT\n"
116 " parse table NAME with info OBJECT\n"
117 " parse-row TABLE ROW..., and re-serialize\n"
118 " parse each ROW of defined TABLE\n"
119 " compare-row TABLE ROW...\n"
120 " mutually compare all of the ROWs, print those that are equal\n"
121 " parse-conditions TABLE CONDITION...\n"
122 " parse each CONDITION on TABLE, and re-serialize\n"
123 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
124 " test CONDITIONS on TABLE against each ROW, print results\n"
125 " query TABLE [ROW,...] [CONDITION,...]\n"
126 " add each ROW to TABLE, then query and print the rows that\n"
127 " satisfy each CONDITION.\n"
128 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
129 " add each ROW to TABLE, then query and print the rows that\n"
130 " satisfy each CONDITION and have distinct COLUMNS.\n"
131 " transact COMMAND\n"
132 " execute each specified transactional COMMAND:\n"
139 " execute SCHEMA TRANSACTION...\n"
140 " executes each TRANSACTION on an initially empty database\n"
141 " the specified SCHEMA\n"
142 " trigger SCHEMA TRANSACTION...\n"
143 " executes each TRANSACTION on an initially empty database\n"
144 " the specified SCHEMA. A TRANSACTION of the form\n"
145 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
146 " simulated time, for causing triggers to time out.\n",
147 program_name, program_name);
149 printf("\nOther options:\n"
150 " -h, --help display this help message\n");
154 /* Command helper functions. */
157 parse_json(const char *s)
159 struct json *json = json_from_string(s);
160 if (json->type == JSON_STRING) {
161 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
167 unbox_json(struct json *json)
169 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
170 struct json *inner = json->u.array.elems[0];
171 json->u.array.elems[0] = NULL;
180 print_and_free_json(struct json *json)
182 char *string = json_to_string(json, JSSF_SORT);
189 check_ovsdb_error(struct ovsdb_error *error)
192 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
196 /* Command implementations. */
199 do_file_io(int argc, char *argv[])
201 const char *name = argv[1];
202 char *mode = argv[2];
204 struct ovsdb_error *error;
205 struct ovsdb_file *file;
206 char *save_ptr = NULL;
211 for (flags = 0, token = strtok_r(mode, " |", &save_ptr); token != NULL;
212 token = strtok_r(NULL, " |", &save_ptr))
214 if (!strcmp(token, "O_RDONLY")) {
216 } else if (!strcmp(token, "O_RDWR")) {
218 } else if (!strcmp(token, "O_TRUNC")) {
220 } else if (!strcmp(token, "O_CREAT")) {
222 } else if (!strcmp(token, "O_EXCL")) {
224 } else if (!strcmp(token, "O_TRUNC")) {
229 check_ovsdb_error(ovsdb_file_open(name, flags, &file));
230 printf("%s: open successful\n", name);
232 for (i = 3; i < argc; i++) {
233 const char *command = argv[i];
234 if (!strcmp(command, "read")) {
237 error = ovsdb_file_read(file, &json);
239 printf("%s: read: ", name);
241 print_and_free_json(json);
243 printf("end of file\n");
247 } else if (!strncmp(command, "write:", 6)) {
248 struct json *json = parse_json(command + 6);
249 error = ovsdb_file_write(file, json);
251 } else if (!strcmp(command, "commit")) {
252 error = ovsdb_file_commit(file);
254 ovs_fatal(0, "unknown file-io command \"%s\"", command);
257 char *s = ovsdb_error_to_string(error);
258 printf("%s: %s failed: %s\n", name, command, s);
261 printf("%s: %s successful\n", name, command);
265 ovsdb_file_close(file);
269 do_parse_atomic_type(int argc UNUSED, char *argv[])
271 enum ovsdb_atomic_type type;
274 json = unbox_json(parse_json(argv[1]));
275 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
277 print_and_free_json(ovsdb_atomic_type_to_json(type));
281 do_parse_type(int argc UNUSED, char *argv[])
283 struct ovsdb_type type;
286 json = unbox_json(parse_json(argv[1]));
287 check_ovsdb_error(ovsdb_type_from_json(&type, json));
289 print_and_free_json(ovsdb_type_to_json(&type));
293 do_parse_atoms(int argc, char *argv[])
295 enum ovsdb_atomic_type type;
299 json = unbox_json(parse_json(argv[1]));
300 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
303 for (i = 2; i < argc; i++) {
304 union ovsdb_atom atom;
306 json = unbox_json(parse_json(argv[i]));
307 check_ovsdb_error(ovsdb_atom_from_json(&atom, type, json, NULL));
310 print_and_free_json(ovsdb_atom_to_json(&atom, type));
312 ovsdb_atom_destroy(&atom, type);
317 do_parse_data(int argc, char *argv[])
319 struct ovsdb_type type;
323 json = unbox_json(parse_json(argv[1]));
324 check_ovsdb_error(ovsdb_type_from_json(&type, json));
327 for (i = 2; i < argc; i++) {
328 struct ovsdb_datum datum;
330 json = unbox_json(parse_json(argv[i]));
331 check_ovsdb_error(ovsdb_datum_from_json(&datum, &type, json, NULL));
334 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
336 ovsdb_datum_destroy(&datum, &type);
340 static enum ovsdb_atomic_type compare_atoms_atomic_type;
343 compare_atoms(const void *a_, const void *b_)
345 const union ovsdb_atom *a = a_;
346 const union ovsdb_atom *b = b_;
348 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
352 do_sort_atoms(int argc UNUSED, char *argv[])
354 enum ovsdb_atomic_type type;
355 union ovsdb_atom *atoms;
356 struct json *json, **json_atoms;
360 json = unbox_json(parse_json(argv[1]));
361 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
364 json = unbox_json(parse_json(argv[2]));
365 if (json->type != JSON_ARRAY) {
366 ovs_fatal(0, "second argument must be array");
369 /* Convert JSON atoms to internal representation. */
370 n_atoms = json->u.array.n;
371 atoms = xmalloc(n_atoms * sizeof *atoms);
372 for (i = 0; i < n_atoms; i++) {
373 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], type,
374 json->u.array.elems[i], NULL));
379 compare_atoms_atomic_type = type;
380 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
382 /* Convert internal representation back to JSON. */
383 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
384 for (i = 0; i < n_atoms; i++) {
385 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], type);
386 ovsdb_atom_destroy(&atoms[i], type);
388 print_and_free_json(json_array_create(json_atoms, n_atoms));
392 do_parse_column(int argc UNUSED, char *argv[])
394 struct ovsdb_column *column;
397 json = parse_json(argv[2]);
398 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
400 print_and_free_json(ovsdb_column_to_json(column));
401 ovsdb_column_destroy(column);
405 do_parse_table(int argc UNUSED, char *argv[])
407 struct ovsdb_table_schema *ts;
410 json = parse_json(argv[2]);
411 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
413 print_and_free_json(ovsdb_table_schema_to_json(ts));
414 ovsdb_table_schema_destroy(ts);
418 do_parse_rows(int argc, char *argv[])
420 struct ovsdb_column_set all_columns;
421 struct ovsdb_table_schema *ts;
422 struct ovsdb_table *table;
426 json = unbox_json(parse_json(argv[1]));
427 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
430 table = ovsdb_table_create(ts);
431 ovsdb_column_set_init(&all_columns);
432 ovsdb_column_set_add_all(&all_columns, table);
434 for (i = 2; i < argc; i++) {
435 struct ovsdb_column_set columns;
436 struct ovsdb_row *row;
438 ovsdb_column_set_init(&columns);
439 row = ovsdb_row_create(table);
441 json = unbox_json(parse_json(argv[i]));
442 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
445 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
447 if (columns.n_columns) {
453 for (j = 0; j < columns.n_columns; j++) {
454 svec_add(&names, columns.columns[j]->name);
457 s = svec_join(&names, ", ", "");
460 svec_destroy(&names);
465 ovsdb_column_set_destroy(&columns);
466 ovsdb_row_destroy(row);
469 ovsdb_column_set_destroy(&all_columns);
470 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
474 do_compare_rows(int argc, char *argv[])
476 struct ovsdb_column_set all_columns;
477 struct ovsdb_table_schema *ts;
478 struct ovsdb_table *table;
479 struct ovsdb_row **rows;
485 json = unbox_json(parse_json(argv[1]));
486 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
489 table = ovsdb_table_create(ts);
490 ovsdb_column_set_init(&all_columns);
491 ovsdb_column_set_add_all(&all_columns, table);
494 rows = xmalloc(sizeof *rows * n_rows);
495 names = xmalloc(sizeof *names * n_rows);
496 for (i = 0; i < n_rows; i++) {
497 rows[i] = ovsdb_row_create(table);
499 json = parse_json(argv[i + 2]);
500 if (json->type != JSON_ARRAY || json->u.array.n != 2
501 || json->u.array.elems[0]->type != JSON_STRING) {
502 ovs_fatal(0, "\"%s\" does not have expected form "
503 "[\"name\", {data}]", argv[i]);
505 names[i] = xstrdup(json->u.array.elems[0]->u.string);
506 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
510 for (i = 0; i < n_rows; i++) {
511 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
512 for (j = i + 1; j < n_rows; j++) {
513 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
514 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
515 printf("%s == %s\n", names[i], names[j]);
516 if (i_hash != j_hash) {
517 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
520 } else if (i_hash == j_hash) {
521 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
528 ovsdb_column_set_destroy(&all_columns);
529 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
533 do_parse_conditions(int argc, char *argv[])
535 struct ovsdb_table_schema *ts;
540 json = unbox_json(parse_json(argv[1]));
541 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
544 for (i = 2; i < argc; i++) {
545 struct ovsdb_condition cnd;
546 struct ovsdb_error *error;
548 json = parse_json(argv[i]);
549 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
551 print_and_free_json(ovsdb_condition_to_json(&cnd));
553 char *s = ovsdb_error_to_string(error);
554 ovs_error(0, "%s", s);
556 ovsdb_error_destroy(error);
561 ovsdb_condition_destroy(&cnd);
563 ovsdb_table_schema_destroy(ts);
569 do_evaluate_conditions(int argc UNUSED, char *argv[])
571 struct ovsdb_table_schema *ts;
572 struct ovsdb_table *table;
573 struct ovsdb_condition *conditions;
575 struct ovsdb_row **rows;
580 /* Parse table schema, create table. */
581 json = unbox_json(parse_json(argv[1]));
582 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
585 table = ovsdb_table_create(ts);
587 /* Parse conditions. */
588 json = parse_json(argv[2]);
589 if (json->type != JSON_ARRAY) {
590 ovs_fatal(0, "CONDITION argument is not JSON array");
592 n_conditions = json->u.array.n;
593 conditions = xmalloc(n_conditions * sizeof *conditions);
594 for (i = 0; i < n_conditions; i++) {
595 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
596 NULL, &conditions[i]));
601 json = parse_json(argv[3]);
602 if (json->type != JSON_ARRAY) {
603 ovs_fatal(0, "ROW argument is not JSON array");
605 n_rows = json->u.array.n;
606 rows = xmalloc(n_rows * sizeof *rows);
607 for (i = 0; i < n_rows; i++) {
608 rows[i] = ovsdb_row_create(table);
609 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
614 for (i = 0; i < n_conditions; i++) {
615 printf("condition %2d:", i);
616 for (j = 0; j < n_rows; j++) {
617 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
621 putchar(result ? 'T' : '-');
626 for (i = 0; i < n_conditions; i++) {
627 ovsdb_condition_destroy(&conditions[i]);
629 for (i = 0; i < n_rows; i++) {
630 ovsdb_row_destroy(rows[i]);
632 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
635 struct do_query_cbdata {
636 struct uuid *row_uuids;
642 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
644 struct do_query_cbdata *cbdata = cbdata_;
647 for (i = 0; i < cbdata->n_rows; i++) {
648 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
657 do_query(int argc UNUSED, char *argv[])
659 struct do_query_cbdata cbdata;
660 struct ovsdb_table_schema *ts;
661 struct ovsdb_table *table;
666 /* Parse table schema, create table. */
667 json = unbox_json(parse_json(argv[1]));
668 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
671 table = ovsdb_table_create(ts);
673 /* Parse rows, add to table. */
674 json = parse_json(argv[2]);
675 if (json->type != JSON_ARRAY) {
676 ovs_fatal(0, "ROW argument is not JSON array");
678 cbdata.n_rows = json->u.array.n;
679 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
680 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
681 for (i = 0; i < cbdata.n_rows; i++) {
682 struct ovsdb_row *row = ovsdb_row_create(table);
683 uuid_generate(ovsdb_row_get_uuid_rw(row));
684 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
686 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
687 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
688 UUID_ARGS(ovsdb_row_get_uuid(row)));
690 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
691 ovsdb_table_put_row(table, row);
695 /* Parse conditions and execute queries. */
696 json = parse_json(argv[3]);
697 if (json->type != JSON_ARRAY) {
698 ovs_fatal(0, "CONDITION argument is not JSON array");
700 for (i = 0; i < json->u.array.n; i++) {
701 struct ovsdb_condition cnd;
704 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
707 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
708 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
710 printf("query %2d:", i);
711 for (j = 0; j < cbdata.n_rows; j++) {
715 if (cbdata.counts[j]) {
716 printf("%d", cbdata.counts[j]);
717 if (cbdata.counts[j] > 1) {
727 ovsdb_condition_destroy(&cnd);
731 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
736 struct do_query_distinct_class {
737 struct ovsdb_row *example;
741 struct do_query_distinct_row {
743 struct do_query_distinct_class *class;
747 do_query_distinct(int argc UNUSED, char *argv[])
749 struct ovsdb_column_set columns;
750 struct ovsdb_table_schema *ts;
751 struct ovsdb_table *table;
752 struct do_query_distinct_row *rows;
754 struct do_query_distinct_class *classes;
760 /* Parse table schema, create table. */
761 json = unbox_json(parse_json(argv[1]));
762 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
765 table = ovsdb_table_create(ts);
767 /* Parse column set. */
768 json = parse_json(argv[4]);
769 ovsdb_column_set_from_json(json, table, &columns);
772 /* Parse rows, add to table. */
773 json = parse_json(argv[2]);
774 if (json->type != JSON_ARRAY) {
775 ovs_fatal(0, "ROW argument is not JSON array");
777 n_rows = json->u.array.n;
778 rows = xmalloc(n_rows * sizeof *rows);
779 classes = xmalloc(n_rows * sizeof *classes);
781 for (i = 0; i < n_rows; i++) {
782 struct ovsdb_row *row;
786 row = ovsdb_row_create(table);
787 uuid_generate(ovsdb_row_get_uuid_rw(row));
788 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
791 /* Initialize row and find equivalence class. */
792 rows[i].uuid = *ovsdb_row_get_uuid(row);
793 rows[i].class = NULL;
794 for (j = 0; j < n_classes; j++) {
795 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
796 rows[i].class = &classes[j];
800 if (!rows[i].class) {
801 rows[i].class = &classes[n_classes];
802 classes[n_classes].example = ovsdb_row_clone(row);
806 /* Add row to table. */
807 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
808 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
809 UUID_ARGS(ovsdb_row_get_uuid(row)));
811 ovsdb_table_put_row(table, row);
816 /* Parse conditions and execute queries. */
817 json = parse_json(argv[3]);
818 if (json->type != JSON_ARRAY) {
819 ovs_fatal(0, "CONDITION argument is not JSON array");
821 for (i = 0; i < json->u.array.n; i++) {
822 struct ovsdb_row_set results;
823 struct ovsdb_condition cnd;
825 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
828 for (j = 0; j < n_classes; j++) {
829 classes[j].count = 0;
831 ovsdb_row_set_init(&results);
832 ovsdb_query_distinct(table, &cnd, &columns, &results);
833 for (j = 0; j < results.n_rows; j++) {
834 for (k = 0; k < n_rows; k++) {
835 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
837 rows[k].class->count++;
841 ovsdb_row_set_destroy(&results);
843 printf("query %2d:", i);
844 for (j = 0; j < n_rows; j++) {
845 int count = rows[j].class->count;
854 } else if (count == 1) {
855 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
862 ovsdb_condition_destroy(&cnd);
866 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
872 do_execute(int argc UNUSED, char *argv[])
874 struct ovsdb_schema *schema;
879 /* Create database. */
880 json = parse_json(argv[1]);
881 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
883 db = ovsdb_create(NULL, schema);
885 for (i = 2; i < argc; i++) {
886 struct json *params, *result;
889 params = parse_json(argv[i]);
890 result = ovsdb_execute(db, params, 0, NULL);
891 s = json_to_string(result, JSSF_SORT);
893 json_destroy(params);
894 json_destroy(result);
900 struct test_trigger {
901 struct ovsdb_trigger trigger;
906 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
911 result = ovsdb_trigger_steal_result(&t->trigger);
912 s = json_to_string(result, JSSF_SORT);
913 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
914 json_destroy(result);
915 ovsdb_trigger_destroy(&t->trigger);
920 do_trigger(int argc UNUSED, char *argv[])
922 struct ovsdb_schema *schema;
923 struct list completions;
930 /* Create database. */
931 json = parse_json(argv[1]);
932 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
934 db = ovsdb_create(NULL, schema);
936 list_init(&completions);
939 for (i = 2; i < argc; i++) {
940 struct json *params = parse_json(argv[i]);
941 if (params->type == JSON_ARRAY
942 && json_array(params)->n == 2
943 && json_array(params)->elems[0]->type == JSON_STRING
944 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
945 && json_array(params)->elems[1]->type == JSON_INTEGER) {
946 now += json_integer(json_array(params)->elems[1]);
947 json_destroy(params);
949 struct test_trigger *t = xmalloc(sizeof *t);
950 ovsdb_trigger_init(db, &t->trigger, params, &completions, now);
951 t->number = number++;
952 if (ovsdb_trigger_is_complete(&t->trigger)) {
953 do_trigger_dump(t, now, "immediate");
955 printf("t=%lld: new trigger %d\n", now, t->number);
959 ovsdb_trigger_run(db, now);
960 while (!list_is_empty(&completions)) {
961 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions),
962 struct test_trigger, trigger.node),
966 ovsdb_trigger_wait(db, now);
967 poll_immediate_wake();
975 do_help(int argc UNUSED, char *argv[] UNUSED)
980 /* "transact" command. */
982 static struct ovsdb *do_transact_db;
983 static struct ovsdb_txn *do_transact_txn;
984 static struct ovsdb_table *do_transact_table;
987 do_transact_commit(int argc UNUSED, char *argv[] UNUSED)
989 ovsdb_txn_commit(do_transact_txn);
990 do_transact_txn = NULL;
994 do_transact_abort(int argc UNUSED, char *argv[] UNUSED)
996 ovsdb_txn_abort(do_transact_txn);
997 do_transact_txn = NULL;
1001 uuid_from_integer(int integer, struct uuid *uuid)
1004 uuid->parts[3] = integer;
1007 static const struct ovsdb_row *
1008 do_transact_find_row(const char *uuid_string)
1010 const struct ovsdb_row *row;
1013 uuid_from_integer(atoi(uuid_string), &uuid);
1014 row = ovsdb_table_get_row(do_transact_table, &uuid);
1016 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1023 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1026 if (integer != -1) {
1027 const struct ovsdb_column *column;
1029 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1031 row->fields[column->index].keys[0].integer = integer;
1036 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1038 const struct ovsdb_column *column;
1040 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1042 return row->fields[column->index].keys[0].integer;
1046 do_transact_set_i_j(struct ovsdb_row *row,
1047 const char *i_string, const char *j_string)
1049 do_transact_set_integer(row, "i", atoi(i_string));
1050 do_transact_set_integer(row, "j", atoi(j_string));
1054 do_transact_insert(int argc UNUSED, char *argv[] UNUSED)
1056 struct ovsdb_row *row;
1059 row = ovsdb_row_create(do_transact_table);
1062 uuid = ovsdb_row_get_uuid_rw(row);
1063 uuid_from_integer(atoi(argv[1]), uuid);
1064 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1065 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1069 do_transact_set_i_j(row, argv[2], argv[3]);
1072 ovsdb_txn_row_insert(do_transact_txn, row);
1076 do_transact_delete(int argc UNUSED, char *argv[] UNUSED)
1078 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1079 ovsdb_txn_row_delete(do_transact_txn, row);
1083 do_transact_modify(int argc UNUSED, char *argv[] UNUSED)
1085 const struct ovsdb_row *row_ro;
1086 struct ovsdb_row *row_rw;
1088 row_ro = do_transact_find_row(argv[1]);
1089 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1090 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1094 compare_rows_by_uuid(const void *a_, const void *b_)
1096 struct ovsdb_row *const *ap = a_;
1097 struct ovsdb_row *const *bp = b_;
1099 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1103 do_transact_print(int argc UNUSED, char *argv[] UNUSED)
1105 const struct ovsdb_row **rows;
1106 const struct ovsdb_row *row;
1110 n_rows = hmap_count(&do_transact_table->rows);
1111 rows = xmalloc(n_rows * sizeof *rows);
1113 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1114 &do_transact_table->rows) {
1117 assert(i == n_rows);
1119 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1121 for (i = 0; i < n_rows; i++) {
1122 printf("\n%"PRId32": i=%d, j=%d",
1123 ovsdb_row_get_uuid(rows[i])->parts[3],
1124 do_transact_get_integer(rows[i], "i"),
1125 do_transact_get_integer(rows[i], "j"));
1132 do_transact(int argc, char *argv[])
1134 static const struct command do_transact_commands[] = {
1135 { "commit", 0, 0, do_transact_commit },
1136 { "abort", 0, 0, do_transact_abort },
1137 { "insert", 2, 3, do_transact_insert },
1138 { "delete", 1, 1, do_transact_delete },
1139 { "modify", 2, 3, do_transact_modify },
1140 { "print", 0, 0, do_transact_print },
1141 { NULL, 0, 0, NULL },
1144 struct ovsdb_schema *schema;
1149 json = parse_json("{\"name\": \"testdb\", "
1153 " {\"i\": {\"type\": \"integer\"}, "
1154 " \"j\": {\"type\": \"integer\"}}}}}");
1155 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1157 do_transact_db = ovsdb_create(NULL, schema);
1158 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1159 assert(do_transact_table != NULL);
1161 for (i = 1; i < argc; i++) {
1162 struct json *command;
1167 command = parse_json(argv[i]);
1168 if (command->type != JSON_ARRAY) {
1169 ovs_fatal(0, "transaction %d must be JSON array "
1170 "with at least 1 element", i);
1173 n_args = command->u.array.n;
1174 args = xmalloc((n_args + 1) * sizeof *args);
1175 for (j = 0; j < n_args; j++) {
1176 struct json *s = command->u.array.elems[j];
1177 if (s->type != JSON_STRING) {
1178 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1181 args[j] = xstrdup(json_string(s));
1183 args[n_args] = NULL;
1185 if (!do_transact_txn) {
1186 do_transact_txn = ovsdb_txn_create(do_transact_db);
1189 for (j = 0; j < n_args; j++) {
1193 fputs(args[j], stdout);
1196 run_command(n_args, args, do_transact_commands);
1199 for (j = 0; j < n_args; j++) {
1203 json_destroy(command);
1205 ovsdb_txn_abort(do_transact_txn);
1206 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1209 static struct command all_commands[] = {
1210 { "file-io", 2, INT_MAX, do_file_io },
1211 { "parse-atomic-type", 1, 1, do_parse_atomic_type },
1212 { "parse-type", 1, 1, do_parse_type },
1213 { "parse-atoms", 2, INT_MAX, do_parse_atoms },
1214 { "parse-data", 2, INT_MAX, do_parse_data },
1215 { "sort-atoms", 2, 2, do_sort_atoms },
1216 { "parse-column", 2, 2, do_parse_column },
1217 { "parse-table", 2, 2, do_parse_table },
1218 { "parse-rows", 2, INT_MAX, do_parse_rows },
1219 { "compare-rows", 2, INT_MAX, do_compare_rows },
1220 { "parse-conditions", 2, INT_MAX, do_parse_conditions },
1221 { "evaluate-conditions", 3, 3, do_evaluate_conditions },
1222 { "query", 3, 3, do_query },
1223 { "query-distinct", 4, 4, do_query_distinct },
1224 { "transact", 1, INT_MAX, do_transact },
1225 { "execute", 2, INT_MAX, do_execute },
1226 { "trigger", 2, INT_MAX, do_trigger },
1227 { "help", 0, INT_MAX, do_help },
1228 { NULL, 0, 0, NULL },