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/log.h"
35 #include "ovsdb/ovsdb.h"
36 #include "ovsdb/query.h"
37 #include "ovsdb/row.h"
38 #include "ovsdb/table.h"
39 #include "ovsdb/transaction.h"
40 #include "ovsdb/trigger.h"
41 #include "poll-loop.h"
47 static struct command all_commands[];
49 static void usage(void) NO_RETURN;
50 static void parse_options(int argc, char *argv[]);
53 main(int argc, char *argv[])
55 set_program_name(argv[0]);
58 parse_options(argc, argv);
59 run_command(argc - optind, argv + optind, all_commands);
64 parse_options(int argc, char *argv[])
66 static struct option long_options[] = {
67 {"verbose", optional_argument, 0, 'v'},
68 {"help", no_argument, 0, 'h'},
71 char *short_options = long_options_to_short_options(long_options);
74 int c = getopt_long(argc, argv, short_options, long_options, NULL);
84 vlog_set_verbosity(optarg);
100 printf("%s: Open vSwitch database test utility\n"
101 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
102 " log-io FILE FLAGS COMMAND...\n"
103 " open FILE with FLAGS, run COMMANDs\n"
104 " parse-atomic-type TYPE\n"
105 " parse TYPE as OVSDB atomic type, and re-serialize\n"
107 " parse JSON as OVSDB type, and re-serialize\n"
108 " parse-atoms TYPE ATOM...\n"
109 " parse ATOMs as atoms of given TYPE, and re-serialize\n"
110 " sort-atoms TYPE ATOM...\n"
111 " print ATOMs in sorted order, and re-serialize\n"
112 " parse-data TYPE DATUM...\n"
113 " parse DATUMs as data of given TYPE, and re-serialize\n"
114 " parse-column NAME OBJECT\n"
115 " parse column NAME with info OBJECT, and re-serialize\n"
116 " parse-table NAME OBJECT\n"
117 " parse table NAME with info OBJECT\n"
118 " parse-row TABLE ROW..., and re-serialize\n"
119 " parse each ROW of defined TABLE\n"
120 " compare-row TABLE ROW...\n"
121 " mutually compare all of the ROWs, print those that are equal\n"
122 " parse-conditions TABLE CONDITION...\n"
123 " parse each CONDITION on TABLE, and re-serialize\n"
124 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
125 " test CONDITIONS on TABLE against each ROW, print results\n"
126 " query TABLE [ROW,...] [CONDITION,...]\n"
127 " add each ROW to TABLE, then query and print the rows that\n"
128 " satisfy each CONDITION.\n"
129 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
130 " add each ROW to TABLE, then query and print the rows that\n"
131 " satisfy each CONDITION and have distinct COLUMNS.\n"
132 " transact COMMAND\n"
133 " execute each specified transactional COMMAND:\n"
140 " execute SCHEMA TRANSACTION...\n"
141 " executes each TRANSACTION on an initially empty database\n"
142 " the specified SCHEMA\n"
143 " trigger SCHEMA TRANSACTION...\n"
144 " executes each TRANSACTION on an initially empty database\n"
145 " the specified SCHEMA. A TRANSACTION of the form\n"
146 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
147 " simulated time, for causing triggers to time out.\n",
148 program_name, program_name);
150 printf("\nOther options:\n"
151 " -h, --help display this help message\n");
155 /* Command helper functions. */
158 parse_json(const char *s)
160 struct json *json = json_from_string(s);
161 if (json->type == JSON_STRING) {
162 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
168 unbox_json(struct json *json)
170 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
171 struct json *inner = json->u.array.elems[0];
172 json->u.array.elems[0] = NULL;
181 print_and_free_json(struct json *json)
183 char *string = json_to_string(json, JSSF_SORT);
190 check_ovsdb_error(struct ovsdb_error *error)
193 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
197 /* Command implementations. */
200 do_log_io(int argc, char *argv[])
202 const char *name = argv[1];
203 char *mode = argv[2];
205 struct ovsdb_error *error;
206 struct ovsdb_log *log;
207 char *save_ptr = NULL;
212 for (flags = 0, token = strtok_r(mode, " |", &save_ptr); token != NULL;
213 token = strtok_r(NULL, " |", &save_ptr))
215 if (!strcmp(token, "O_RDONLY")) {
217 } else if (!strcmp(token, "O_RDWR")) {
219 } else if (!strcmp(token, "O_TRUNC")) {
221 } else if (!strcmp(token, "O_CREAT")) {
223 } else if (!strcmp(token, "O_EXCL")) {
225 } else if (!strcmp(token, "O_TRUNC")) {
230 check_ovsdb_error(ovsdb_log_open(name, flags, &log));
231 printf("%s: open successful\n", name);
233 for (i = 3; i < argc; i++) {
234 const char *command = argv[i];
235 if (!strcmp(command, "read")) {
238 error = ovsdb_log_read(log, &json);
240 printf("%s: read: ", name);
242 print_and_free_json(json);
244 printf("end of log\n");
248 } else if (!strncmp(command, "write:", 6)) {
249 struct json *json = parse_json(command + 6);
250 error = ovsdb_log_write(log, json);
252 } else if (!strcmp(command, "commit")) {
253 error = ovsdb_log_commit(log);
255 ovs_fatal(0, "unknown log-io command \"%s\"", command);
258 char *s = ovsdb_error_to_string(error);
259 printf("%s: %s failed: %s\n", name, command, s);
262 printf("%s: %s successful\n", name, command);
266 ovsdb_log_close(log);
270 do_parse_atomic_type(int argc UNUSED, char *argv[])
272 enum ovsdb_atomic_type type;
275 json = unbox_json(parse_json(argv[1]));
276 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
278 print_and_free_json(ovsdb_atomic_type_to_json(type));
282 do_parse_type(int argc UNUSED, char *argv[])
284 struct ovsdb_type type;
287 json = unbox_json(parse_json(argv[1]));
288 check_ovsdb_error(ovsdb_type_from_json(&type, json));
290 print_and_free_json(ovsdb_type_to_json(&type));
294 do_parse_atoms(int argc, char *argv[])
296 enum ovsdb_atomic_type type;
300 json = unbox_json(parse_json(argv[1]));
301 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
304 for (i = 2; i < argc; i++) {
305 union ovsdb_atom atom;
307 json = unbox_json(parse_json(argv[i]));
308 check_ovsdb_error(ovsdb_atom_from_json(&atom, type, json, NULL));
311 print_and_free_json(ovsdb_atom_to_json(&atom, type));
313 ovsdb_atom_destroy(&atom, type);
318 do_parse_data(int argc, char *argv[])
320 struct ovsdb_type type;
324 json = unbox_json(parse_json(argv[1]));
325 check_ovsdb_error(ovsdb_type_from_json(&type, json));
328 for (i = 2; i < argc; i++) {
329 struct ovsdb_datum datum;
331 json = unbox_json(parse_json(argv[i]));
332 check_ovsdb_error(ovsdb_datum_from_json(&datum, &type, json, NULL));
335 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
337 ovsdb_datum_destroy(&datum, &type);
341 static enum ovsdb_atomic_type compare_atoms_atomic_type;
344 compare_atoms(const void *a_, const void *b_)
346 const union ovsdb_atom *a = a_;
347 const union ovsdb_atom *b = b_;
349 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
353 do_sort_atoms(int argc UNUSED, char *argv[])
355 enum ovsdb_atomic_type type;
356 union ovsdb_atom *atoms;
357 struct json *json, **json_atoms;
361 json = unbox_json(parse_json(argv[1]));
362 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
365 json = unbox_json(parse_json(argv[2]));
366 if (json->type != JSON_ARRAY) {
367 ovs_fatal(0, "second argument must be array");
370 /* Convert JSON atoms to internal representation. */
371 n_atoms = json->u.array.n;
372 atoms = xmalloc(n_atoms * sizeof *atoms);
373 for (i = 0; i < n_atoms; i++) {
374 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], type,
375 json->u.array.elems[i], NULL));
380 compare_atoms_atomic_type = type;
381 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
383 /* Convert internal representation back to JSON. */
384 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
385 for (i = 0; i < n_atoms; i++) {
386 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], type);
387 ovsdb_atom_destroy(&atoms[i], type);
389 print_and_free_json(json_array_create(json_atoms, n_atoms));
393 do_parse_column(int argc UNUSED, char *argv[])
395 struct ovsdb_column *column;
398 json = parse_json(argv[2]);
399 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
401 print_and_free_json(ovsdb_column_to_json(column));
402 ovsdb_column_destroy(column);
406 do_parse_table(int argc UNUSED, char *argv[])
408 struct ovsdb_table_schema *ts;
411 json = parse_json(argv[2]);
412 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
414 print_and_free_json(ovsdb_table_schema_to_json(ts));
415 ovsdb_table_schema_destroy(ts);
419 do_parse_rows(int argc, char *argv[])
421 struct ovsdb_column_set all_columns;
422 struct ovsdb_table_schema *ts;
423 struct ovsdb_table *table;
427 json = unbox_json(parse_json(argv[1]));
428 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
431 table = ovsdb_table_create(ts);
432 ovsdb_column_set_init(&all_columns);
433 ovsdb_column_set_add_all(&all_columns, table);
435 for (i = 2; i < argc; i++) {
436 struct ovsdb_column_set columns;
437 struct ovsdb_row *row;
439 ovsdb_column_set_init(&columns);
440 row = ovsdb_row_create(table);
442 json = unbox_json(parse_json(argv[i]));
443 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
446 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
448 if (columns.n_columns) {
454 for (j = 0; j < columns.n_columns; j++) {
455 svec_add(&names, columns.columns[j]->name);
458 s = svec_join(&names, ", ", "");
461 svec_destroy(&names);
466 ovsdb_column_set_destroy(&columns);
467 ovsdb_row_destroy(row);
470 ovsdb_column_set_destroy(&all_columns);
471 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
475 do_compare_rows(int argc, char *argv[])
477 struct ovsdb_column_set all_columns;
478 struct ovsdb_table_schema *ts;
479 struct ovsdb_table *table;
480 struct ovsdb_row **rows;
486 json = unbox_json(parse_json(argv[1]));
487 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
490 table = ovsdb_table_create(ts);
491 ovsdb_column_set_init(&all_columns);
492 ovsdb_column_set_add_all(&all_columns, table);
495 rows = xmalloc(sizeof *rows * n_rows);
496 names = xmalloc(sizeof *names * n_rows);
497 for (i = 0; i < n_rows; i++) {
498 rows[i] = ovsdb_row_create(table);
500 json = parse_json(argv[i + 2]);
501 if (json->type != JSON_ARRAY || json->u.array.n != 2
502 || json->u.array.elems[0]->type != JSON_STRING) {
503 ovs_fatal(0, "\"%s\" does not have expected form "
504 "[\"name\", {data}]", argv[i]);
506 names[i] = xstrdup(json->u.array.elems[0]->u.string);
507 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
511 for (i = 0; i < n_rows; i++) {
512 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
513 for (j = i + 1; j < n_rows; j++) {
514 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
515 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
516 printf("%s == %s\n", names[i], names[j]);
517 if (i_hash != j_hash) {
518 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
521 } else if (i_hash == j_hash) {
522 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
529 ovsdb_column_set_destroy(&all_columns);
530 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
534 do_parse_conditions(int argc, char *argv[])
536 struct ovsdb_table_schema *ts;
541 json = unbox_json(parse_json(argv[1]));
542 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
545 for (i = 2; i < argc; i++) {
546 struct ovsdb_condition cnd;
547 struct ovsdb_error *error;
549 json = parse_json(argv[i]);
550 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
552 print_and_free_json(ovsdb_condition_to_json(&cnd));
554 char *s = ovsdb_error_to_string(error);
555 ovs_error(0, "%s", s);
557 ovsdb_error_destroy(error);
562 ovsdb_condition_destroy(&cnd);
564 ovsdb_table_schema_destroy(ts);
570 do_evaluate_conditions(int argc UNUSED, char *argv[])
572 struct ovsdb_table_schema *ts;
573 struct ovsdb_table *table;
574 struct ovsdb_condition *conditions;
576 struct ovsdb_row **rows;
581 /* Parse table schema, create table. */
582 json = unbox_json(parse_json(argv[1]));
583 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
586 table = ovsdb_table_create(ts);
588 /* Parse conditions. */
589 json = parse_json(argv[2]);
590 if (json->type != JSON_ARRAY) {
591 ovs_fatal(0, "CONDITION argument is not JSON array");
593 n_conditions = json->u.array.n;
594 conditions = xmalloc(n_conditions * sizeof *conditions);
595 for (i = 0; i < n_conditions; i++) {
596 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
597 NULL, &conditions[i]));
602 json = parse_json(argv[3]);
603 if (json->type != JSON_ARRAY) {
604 ovs_fatal(0, "ROW argument is not JSON array");
606 n_rows = json->u.array.n;
607 rows = xmalloc(n_rows * sizeof *rows);
608 for (i = 0; i < n_rows; i++) {
609 rows[i] = ovsdb_row_create(table);
610 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
615 for (i = 0; i < n_conditions; i++) {
616 printf("condition %2d:", i);
617 for (j = 0; j < n_rows; j++) {
618 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
622 putchar(result ? 'T' : '-');
627 for (i = 0; i < n_conditions; i++) {
628 ovsdb_condition_destroy(&conditions[i]);
630 for (i = 0; i < n_rows; i++) {
631 ovsdb_row_destroy(rows[i]);
633 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
636 struct do_query_cbdata {
637 struct uuid *row_uuids;
643 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
645 struct do_query_cbdata *cbdata = cbdata_;
648 for (i = 0; i < cbdata->n_rows; i++) {
649 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
658 do_query(int argc UNUSED, char *argv[])
660 struct do_query_cbdata cbdata;
661 struct ovsdb_table_schema *ts;
662 struct ovsdb_table *table;
667 /* Parse table schema, create table. */
668 json = unbox_json(parse_json(argv[1]));
669 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
672 table = ovsdb_table_create(ts);
674 /* Parse rows, add to table. */
675 json = parse_json(argv[2]);
676 if (json->type != JSON_ARRAY) {
677 ovs_fatal(0, "ROW argument is not JSON array");
679 cbdata.n_rows = json->u.array.n;
680 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
681 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
682 for (i = 0; i < cbdata.n_rows; i++) {
683 struct ovsdb_row *row = ovsdb_row_create(table);
684 uuid_generate(ovsdb_row_get_uuid_rw(row));
685 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
687 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
688 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
689 UUID_ARGS(ovsdb_row_get_uuid(row)));
691 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
692 ovsdb_table_put_row(table, row);
696 /* Parse conditions and execute queries. */
697 json = parse_json(argv[3]);
698 if (json->type != JSON_ARRAY) {
699 ovs_fatal(0, "CONDITION argument is not JSON array");
701 for (i = 0; i < json->u.array.n; i++) {
702 struct ovsdb_condition cnd;
705 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
708 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
709 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
711 printf("query %2d:", i);
712 for (j = 0; j < cbdata.n_rows; j++) {
716 if (cbdata.counts[j]) {
717 printf("%d", cbdata.counts[j]);
718 if (cbdata.counts[j] > 1) {
728 ovsdb_condition_destroy(&cnd);
732 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
737 struct do_query_distinct_class {
738 struct ovsdb_row *example;
742 struct do_query_distinct_row {
744 struct do_query_distinct_class *class;
748 do_query_distinct(int argc UNUSED, char *argv[])
750 struct ovsdb_column_set columns;
751 struct ovsdb_table_schema *ts;
752 struct ovsdb_table *table;
753 struct do_query_distinct_row *rows;
755 struct do_query_distinct_class *classes;
761 /* Parse table schema, create table. */
762 json = unbox_json(parse_json(argv[1]));
763 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
766 table = ovsdb_table_create(ts);
768 /* Parse column set. */
769 json = parse_json(argv[4]);
770 ovsdb_column_set_from_json(json, table, &columns);
773 /* Parse rows, add to table. */
774 json = parse_json(argv[2]);
775 if (json->type != JSON_ARRAY) {
776 ovs_fatal(0, "ROW argument is not JSON array");
778 n_rows = json->u.array.n;
779 rows = xmalloc(n_rows * sizeof *rows);
780 classes = xmalloc(n_rows * sizeof *classes);
782 for (i = 0; i < n_rows; i++) {
783 struct ovsdb_row *row;
787 row = ovsdb_row_create(table);
788 uuid_generate(ovsdb_row_get_uuid_rw(row));
789 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
792 /* Initialize row and find equivalence class. */
793 rows[i].uuid = *ovsdb_row_get_uuid(row);
794 rows[i].class = NULL;
795 for (j = 0; j < n_classes; j++) {
796 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
797 rows[i].class = &classes[j];
801 if (!rows[i].class) {
802 rows[i].class = &classes[n_classes];
803 classes[n_classes].example = ovsdb_row_clone(row);
807 /* Add row to table. */
808 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
809 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
810 UUID_ARGS(ovsdb_row_get_uuid(row)));
812 ovsdb_table_put_row(table, row);
817 /* Parse conditions and execute queries. */
818 json = parse_json(argv[3]);
819 if (json->type != JSON_ARRAY) {
820 ovs_fatal(0, "CONDITION argument is not JSON array");
822 for (i = 0; i < json->u.array.n; i++) {
823 struct ovsdb_row_set results;
824 struct ovsdb_condition cnd;
826 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
829 for (j = 0; j < n_classes; j++) {
830 classes[j].count = 0;
832 ovsdb_row_set_init(&results);
833 ovsdb_query_distinct(table, &cnd, &columns, &results);
834 for (j = 0; j < results.n_rows; j++) {
835 for (k = 0; k < n_rows; k++) {
836 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
838 rows[k].class->count++;
842 ovsdb_row_set_destroy(&results);
844 printf("query %2d:", i);
845 for (j = 0; j < n_rows; j++) {
846 int count = rows[j].class->count;
855 } else if (count == 1) {
856 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
863 ovsdb_condition_destroy(&cnd);
867 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
873 do_execute(int argc UNUSED, char *argv[])
875 struct ovsdb_schema *schema;
880 /* Create database. */
881 json = parse_json(argv[1]);
882 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
884 db = ovsdb_create(schema);
886 for (i = 2; i < argc; i++) {
887 struct json *params, *result;
890 params = parse_json(argv[i]);
891 result = ovsdb_execute(db, params, 0, NULL);
892 s = json_to_string(result, JSSF_SORT);
894 json_destroy(params);
895 json_destroy(result);
901 struct test_trigger {
902 struct ovsdb_trigger trigger;
907 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
912 result = ovsdb_trigger_steal_result(&t->trigger);
913 s = json_to_string(result, JSSF_SORT);
914 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
915 json_destroy(result);
916 ovsdb_trigger_destroy(&t->trigger);
921 do_trigger(int argc UNUSED, char *argv[])
923 struct ovsdb_schema *schema;
924 struct list completions;
931 /* Create database. */
932 json = parse_json(argv[1]);
933 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
935 db = ovsdb_create(schema);
937 list_init(&completions);
940 for (i = 2; i < argc; i++) {
941 struct json *params = parse_json(argv[i]);
942 if (params->type == JSON_ARRAY
943 && json_array(params)->n == 2
944 && json_array(params)->elems[0]->type == JSON_STRING
945 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
946 && json_array(params)->elems[1]->type == JSON_INTEGER) {
947 now += json_integer(json_array(params)->elems[1]);
948 json_destroy(params);
950 struct test_trigger *t = xmalloc(sizeof *t);
951 ovsdb_trigger_init(db, &t->trigger, params, &completions, now);
952 t->number = number++;
953 if (ovsdb_trigger_is_complete(&t->trigger)) {
954 do_trigger_dump(t, now, "immediate");
956 printf("t=%lld: new trigger %d\n", now, t->number);
960 ovsdb_trigger_run(db, now);
961 while (!list_is_empty(&completions)) {
962 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions),
963 struct test_trigger, trigger.node),
967 ovsdb_trigger_wait(db, now);
968 poll_immediate_wake();
976 do_help(int argc UNUSED, char *argv[] UNUSED)
981 /* "transact" command. */
983 static struct ovsdb *do_transact_db;
984 static struct ovsdb_txn *do_transact_txn;
985 static struct ovsdb_table *do_transact_table;
988 do_transact_commit(int argc UNUSED, char *argv[] UNUSED)
990 ovsdb_txn_commit(do_transact_txn, false);
991 do_transact_txn = NULL;
995 do_transact_abort(int argc UNUSED, char *argv[] UNUSED)
997 ovsdb_txn_abort(do_transact_txn);
998 do_transact_txn = NULL;
1002 uuid_from_integer(int integer, struct uuid *uuid)
1005 uuid->parts[3] = integer;
1008 static const struct ovsdb_row *
1009 do_transact_find_row(const char *uuid_string)
1011 const struct ovsdb_row *row;
1014 uuid_from_integer(atoi(uuid_string), &uuid);
1015 row = ovsdb_table_get_row(do_transact_table, &uuid);
1017 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1024 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1027 if (integer != -1) {
1028 const struct ovsdb_column *column;
1030 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1032 row->fields[column->index].keys[0].integer = integer;
1037 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1039 const struct ovsdb_column *column;
1041 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1043 return row->fields[column->index].keys[0].integer;
1047 do_transact_set_i_j(struct ovsdb_row *row,
1048 const char *i_string, const char *j_string)
1050 do_transact_set_integer(row, "i", atoi(i_string));
1051 do_transact_set_integer(row, "j", atoi(j_string));
1055 do_transact_insert(int argc UNUSED, char *argv[] UNUSED)
1057 struct ovsdb_row *row;
1060 row = ovsdb_row_create(do_transact_table);
1063 uuid = ovsdb_row_get_uuid_rw(row);
1064 uuid_from_integer(atoi(argv[1]), uuid);
1065 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1066 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1070 do_transact_set_i_j(row, argv[2], argv[3]);
1073 ovsdb_txn_row_insert(do_transact_txn, row);
1077 do_transact_delete(int argc UNUSED, char *argv[] UNUSED)
1079 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1080 ovsdb_txn_row_delete(do_transact_txn, row);
1084 do_transact_modify(int argc UNUSED, char *argv[] UNUSED)
1086 const struct ovsdb_row *row_ro;
1087 struct ovsdb_row *row_rw;
1089 row_ro = do_transact_find_row(argv[1]);
1090 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1091 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1095 compare_rows_by_uuid(const void *a_, const void *b_)
1097 struct ovsdb_row *const *ap = a_;
1098 struct ovsdb_row *const *bp = b_;
1100 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1104 do_transact_print(int argc UNUSED, char *argv[] UNUSED)
1106 const struct ovsdb_row **rows;
1107 const struct ovsdb_row *row;
1111 n_rows = hmap_count(&do_transact_table->rows);
1112 rows = xmalloc(n_rows * sizeof *rows);
1114 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1115 &do_transact_table->rows) {
1118 assert(i == n_rows);
1120 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1122 for (i = 0; i < n_rows; i++) {
1123 printf("\n%"PRId32": i=%d, j=%d",
1124 ovsdb_row_get_uuid(rows[i])->parts[3],
1125 do_transact_get_integer(rows[i], "i"),
1126 do_transact_get_integer(rows[i], "j"));
1133 do_transact(int argc, char *argv[])
1135 static const struct command do_transact_commands[] = {
1136 { "commit", 0, 0, do_transact_commit },
1137 { "abort", 0, 0, do_transact_abort },
1138 { "insert", 2, 3, do_transact_insert },
1139 { "delete", 1, 1, do_transact_delete },
1140 { "modify", 2, 3, do_transact_modify },
1141 { "print", 0, 0, do_transact_print },
1142 { NULL, 0, 0, NULL },
1145 struct ovsdb_schema *schema;
1150 json = parse_json("{\"name\": \"testdb\", "
1154 " {\"i\": {\"type\": \"integer\"}, "
1155 " \"j\": {\"type\": \"integer\"}}}}}");
1156 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1158 do_transact_db = ovsdb_create(schema);
1159 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1160 assert(do_transact_table != NULL);
1162 for (i = 1; i < argc; i++) {
1163 struct json *command;
1168 command = parse_json(argv[i]);
1169 if (command->type != JSON_ARRAY) {
1170 ovs_fatal(0, "transaction %d must be JSON array "
1171 "with at least 1 element", i);
1174 n_args = command->u.array.n;
1175 args = xmalloc((n_args + 1) * sizeof *args);
1176 for (j = 0; j < n_args; j++) {
1177 struct json *s = command->u.array.elems[j];
1178 if (s->type != JSON_STRING) {
1179 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1182 args[j] = xstrdup(json_string(s));
1184 args[n_args] = NULL;
1186 if (!do_transact_txn) {
1187 do_transact_txn = ovsdb_txn_create(do_transact_db);
1190 for (j = 0; j < n_args; j++) {
1194 fputs(args[j], stdout);
1197 run_command(n_args, args, do_transact_commands);
1200 for (j = 0; j < n_args; j++) {
1204 json_destroy(command);
1206 ovsdb_txn_abort(do_transact_txn);
1207 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1210 static struct command all_commands[] = {
1211 { "log-io", 2, INT_MAX, do_log_io },
1212 { "parse-atomic-type", 1, 1, do_parse_atomic_type },
1213 { "parse-type", 1, 1, do_parse_type },
1214 { "parse-atoms", 2, INT_MAX, do_parse_atoms },
1215 { "parse-data", 2, INT_MAX, do_parse_data },
1216 { "sort-atoms", 2, 2, do_sort_atoms },
1217 { "parse-column", 2, 2, do_parse_column },
1218 { "parse-table", 2, 2, do_parse_table },
1219 { "parse-rows", 2, INT_MAX, do_parse_rows },
1220 { "compare-rows", 2, INT_MAX, do_compare_rows },
1221 { "parse-conditions", 2, INT_MAX, do_parse_conditions },
1222 { "evaluate-conditions", 3, 3, do_evaluate_conditions },
1223 { "query", 3, 3, do_query },
1224 { "query-distinct", 4, 4, do_query_distinct },
1225 { "transact", 1, INT_MAX, do_transact },
1226 { "execute", 2, INT_MAX, do_execute },
1227 { "trigger", 2, INT_MAX, do_trigger },
1228 { "help", 0, INT_MAX, do_help },
1229 { NULL, 0, 0, NULL },