]> git.proxmox.com Git - ovs.git/blame - tests/test-ovsdb.c
ofproto-dpif-xlate: Preserve stack across patch port.
[ovs.git] / tests / test-ovsdb.c
CommitLineData
f85f8ebb 1/*
fba6bd1d 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
f85f8ebb
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
19#include <assert.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <inttypes.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26#include "command-line.h"
5c414a2e 27#include "dynamic-string.h"
f85f8ebb 28#include "json.h"
c3bb4bd7 29#include "jsonrpc.h"
f85f8ebb
BP
30#include "ovsdb-data.h"
31#include "ovsdb-error.h"
c3bb4bd7 32#include "ovsdb-idl.h"
f85f8ebb
BP
33#include "ovsdb-types.h"
34#include "ovsdb/column.h"
35#include "ovsdb/condition.h"
bd06962a 36#include "ovsdb/file.h"
41709ccc 37#include "ovsdb/log.h"
e9f8f936 38#include "ovsdb/mutation.h"
f85f8ebb
BP
39#include "ovsdb/ovsdb.h"
40#include "ovsdb/query.h"
41#include "ovsdb/row.h"
e317253b 42#include "ovsdb/server.h"
f85f8ebb
BP
43#include "ovsdb/table.h"
44#include "ovsdb/transaction.h"
45#include "ovsdb/trigger.h"
46#include "poll-loop.h"
c3bb4bd7 47#include "stream.h"
f85f8ebb 48#include "svec.h"
c3bb4bd7 49#include "tests/idltest.h"
f85f8ebb
BP
50#include "timeval.h"
51#include "util.h"
e6211adc 52#include "openvswitch/vlog.h"
f85f8ebb 53
cab50449 54OVS_NO_RETURN static void usage(void);
f85f8ebb 55static void parse_options(int argc, char *argv[]);
5f383751 56static struct ovs_cmdl_command *get_all_commands(void);
f85f8ebb
BP
57
58int
59main(int argc, char *argv[])
60{
61 set_program_name(argv[0]);
f85f8ebb 62 parse_options(argc, argv);
5f383751 63 ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands());
f85f8ebb
BP
64 return 0;
65}
66
67static void
68parse_options(int argc, char *argv[])
69{
07fc4ed3 70 static const struct option long_options[] = {
e3c17733
BP
71 {"timeout", required_argument, NULL, 't'},
72 {"verbose", optional_argument, NULL, 'v'},
73 {"help", no_argument, NULL, 'h'},
74 {NULL, 0, NULL, 0},
f85f8ebb 75 };
5f383751 76 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
f85f8ebb
BP
77
78 for (;;) {
c3bb4bd7
BP
79 unsigned long int timeout;
80 int c;
81
82 c = getopt_long(argc, argv, short_options, long_options, NULL);
f85f8ebb
BP
83 if (c == -1) {
84 break;
85 }
86
87 switch (c) {
c3bb4bd7
BP
88 case 't':
89 timeout = strtoul(optarg, NULL, 10);
90 if (timeout <= 0) {
91 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
92 optarg);
93 } else {
94 time_alarm(timeout);
95 }
96 break;
97
f85f8ebb
BP
98 case 'h':
99 usage();
100
101 case 'v':
102 vlog_set_verbosity(optarg);
103 break;
104
105 case '?':
106 exit(EXIT_FAILURE);
107
108 default:
109 abort();
110 }
111 }
112 free(short_options);
113}
114
115static void
116usage(void)
117{
118 printf("%s: Open vSwitch database test utility\n"
119 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
41709ccc 120 " log-io FILE FLAGS COMMAND...\n"
f85f8ebb 121 " open FILE with FLAGS, run COMMANDs\n"
958ac03a
BP
122 " default-atoms\n"
123 " test ovsdb_atom_default()\n"
124 " default-data\n"
125 " test ovsdb_datum_default()\n"
f85f8ebb
BP
126 " parse-atomic-type TYPE\n"
127 " parse TYPE as OVSDB atomic type, and re-serialize\n"
bd76d25d
BP
128 " parse-base-type TYPE\n"
129 " parse TYPE as OVSDB base type, and re-serialize\n"
f85f8ebb
BP
130 " parse-type JSON\n"
131 " parse JSON as OVSDB type, and re-serialize\n"
132 " parse-atoms TYPE ATOM...\n"
5c414a2e
BP
133 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
134 " parse-atom-strings TYPE ATOM...\n"
135 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
f85f8ebb 136 " sort-atoms TYPE ATOM...\n"
5c414a2e 137 " print JSON ATOMs in sorted order\n"
f85f8ebb 138 " parse-data TYPE DATUM...\n"
5c414a2e
BP
139 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
140 " parse-data-strings TYPE DATUM...\n"
141 " parse string DATUMs as data of given TYPE, and re-serialize\n"
f85f8ebb
BP
142 " parse-column NAME OBJECT\n"
143 " parse column NAME with info OBJECT, and re-serialize\n"
c5f341ab 144 " parse-table NAME OBJECT [DEFAULT-IS-ROOT]\n"
f85f8ebb
BP
145 " parse table NAME with info OBJECT\n"
146 " parse-row TABLE ROW..., and re-serialize\n"
147 " parse each ROW of defined TABLE\n"
148 " compare-row TABLE ROW...\n"
149 " mutually compare all of the ROWs, print those that are equal\n"
150 " parse-conditions TABLE CONDITION...\n"
151 " parse each CONDITION on TABLE, and re-serialize\n"
152 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
153 " test CONDITIONS on TABLE against each ROW, print results\n"
e9f8f936
BP
154 " parse-mutations TABLE MUTATION...\n"
155 " parse each MUTATION on TABLE, and re-serialize\n"
156 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
157 " execute MUTATIONS on TABLE on each ROW, print results\n"
f85f8ebb
BP
158 " query TABLE [ROW,...] [CONDITION,...]\n"
159 " add each ROW to TABLE, then query and print the rows that\n"
160 " satisfy each CONDITION.\n"
161 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
162 " add each ROW to TABLE, then query and print the rows that\n"
163 " satisfy each CONDITION and have distinct COLUMNS.\n"
0d0f05b9
BP
164 " parse-schema JSON\n"
165 " parse JSON as an OVSDB schema, and re-serialize\n"
f85f8ebb
BP
166 " transact COMMAND\n"
167 " execute each specified transactional COMMAND:\n"
168 " commit\n"
169 " abort\n"
170 " insert UUID I J\n"
171 " delete UUID\n"
172 " modify UUID I J\n"
173 " print\n"
174 " execute SCHEMA TRANSACTION...\n"
175 " executes each TRANSACTION on an initially empty database\n"
176 " the specified SCHEMA\n"
177 " trigger SCHEMA TRANSACTION...\n"
178 " executes each TRANSACTION on an initially empty database\n"
179 " the specified SCHEMA. A TRANSACTION of the form\n"
180 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
c3bb4bd7
BP
181 " simulated time, for causing triggers to time out.\n"
182 " idl SERVER [TRANSACTION...]\n"
183 " connect to SERVER and dump the contents of the database\n"
184 " as seen initially by the IDL implementation and after\n"
185 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
186 " the database or this command will hang.)\n",
f85f8ebb
BP
187 program_name, program_name);
188 vlog_usage();
189 printf("\nOther options:\n"
c3bb4bd7 190 " -t, --timeout=SECS give up after SECS seconds\n"
f85f8ebb
BP
191 " -h, --help display this help message\n");
192 exit(EXIT_SUCCESS);
193}
194\f
195/* Command helper functions. */
196
197static struct json *
198parse_json(const char *s)
199{
200 struct json *json = json_from_string(s);
201 if (json->type == JSON_STRING) {
202 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
203 }
204 return json;
205}
206
207static struct json *
208unbox_json(struct json *json)
209{
210 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
211 struct json *inner = json->u.array.elems[0];
212 json->u.array.elems[0] = NULL;
213 json_destroy(json);
214 return inner;
215 } else {
216 return json;
217 }
218}
219
7bd02255 220static void
f85f8ebb
BP
221print_and_free_json(struct json *json)
222{
223 char *string = json_to_string(json, JSSF_SORT);
224 json_destroy(json);
225 puts(string);
226 free(string);
227}
228
e9f8f936
BP
229static void
230print_and_free_ovsdb_error(struct ovsdb_error *error)
231{
232 char *string = ovsdb_error_to_string(error);
233 ovsdb_error_destroy(error);
234 puts(string);
235 free(string);
236}
237
f85f8ebb
BP
238static void
239check_ovsdb_error(struct ovsdb_error *error)
240{
241 if (error) {
93ff0290
BP
242 char *s = ovsdb_error_to_string(error);
243 ovsdb_error_destroy(error);
244 ovs_fatal(0, "%s", s);
f85f8ebb
BP
245 }
246}
5c414a2e
BP
247
248static void
249die_if_error(char *error)
250{
251 if (error) {
252 ovs_fatal(0, "%s", error);
253 }
254}
f85f8ebb
BP
255\f
256/* Command implementations. */
257
258static void
41709ccc 259do_log_io(int argc, char *argv[])
f85f8ebb
BP
260{
261 const char *name = argv[1];
7446f148 262 char *mode_string = argv[2];
f85f8ebb
BP
263
264 struct ovsdb_error *error;
7446f148 265 enum ovsdb_log_open_mode mode;
41709ccc 266 struct ovsdb_log *log;
f85f8ebb
BP
267 int i;
268
7446f148
BP
269 if (!strcmp(mode_string, "read-only")) {
270 mode = OVSDB_LOG_READ_ONLY;
271 } else if (!strcmp(mode_string, "read/write")) {
272 mode = OVSDB_LOG_READ_WRITE;
273 } else if (!strcmp(mode_string, "create")) {
274 mode = OVSDB_LOG_CREATE;
275 } else {
276 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
f85f8ebb
BP
277 }
278
7446f148 279 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
f85f8ebb
BP
280 printf("%s: open successful\n", name);
281
282 for (i = 3; i < argc; i++) {
283 const char *command = argv[i];
284 if (!strcmp(command, "read")) {
285 struct json *json;
286
41709ccc 287 error = ovsdb_log_read(log, &json);
f85f8ebb
BP
288 if (!error) {
289 printf("%s: read: ", name);
290 if (json) {
291 print_and_free_json(json);
292 } else {
41709ccc 293 printf("end of log\n");
f85f8ebb
BP
294 }
295 continue;
296 }
297 } else if (!strncmp(command, "write:", 6)) {
298 struct json *json = parse_json(command + 6);
41709ccc 299 error = ovsdb_log_write(log, json);
f85f8ebb
BP
300 json_destroy(json);
301 } else if (!strcmp(command, "commit")) {
41709ccc 302 error = ovsdb_log_commit(log);
f85f8ebb 303 } else {
41709ccc 304 ovs_fatal(0, "unknown log-io command \"%s\"", command);
f85f8ebb
BP
305 }
306 if (error) {
307 char *s = ovsdb_error_to_string(error);
308 printf("%s: %s failed: %s\n", name, command, s);
309 free(s);
93ff0290 310 ovsdb_error_destroy(error);
f85f8ebb
BP
311 } else {
312 printf("%s: %s successful\n", name, command);
313 }
314 }
315
41709ccc 316 ovsdb_log_close(log);
f85f8ebb
BP
317}
318
958ac03a
BP
319static void
320do_default_atoms(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
321{
322 int type;
323
324 for (type = 0; type < OVSDB_N_TYPES; type++) {
325 union ovsdb_atom atom;
326
327 if (type == OVSDB_TYPE_VOID) {
328 continue;
329 }
330
331 printf("%s: ", ovsdb_atomic_type_to_string(type));
332
333 ovsdb_atom_init_default(&atom, type);
334 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
335 printf("wrong\n");
336 exit(1);
337 }
338 ovsdb_atom_destroy(&atom, type);
339
340 printf("OK\n");
341 }
342}
343
344static void
345do_default_data(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
346{
347 unsigned int n_min;
348 int key, value;
349
350 for (n_min = 0; n_min <= 1; n_min++) {
351 for (key = 0; key < OVSDB_N_TYPES; key++) {
352 if (key == OVSDB_TYPE_VOID) {
353 continue;
354 }
355 for (value = 0; value < OVSDB_N_TYPES; value++) {
356 struct ovsdb_datum datum;
357 struct ovsdb_type type;
358
359 ovsdb_base_type_init(&type.key, key);
360 ovsdb_base_type_init(&type.value, value);
361 type.n_min = n_min;
362 type.n_max = 1;
363 assert(ovsdb_type_is_valid(&type));
364
365 printf("key %s, value %s, n_min %u: ",
366 ovsdb_atomic_type_to_string(key),
367 ovsdb_atomic_type_to_string(value), n_min);
368
369 ovsdb_datum_init_default(&datum, &type);
370 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
371 &type)) {
372 printf("wrong\n");
373 exit(1);
374 }
375 ovsdb_datum_destroy(&datum, &type);
376 ovsdb_type_destroy(&type);
377
378 printf("OK\n");
379 }
380 }
381 }
382}
383
f85f8ebb 384static void
c69ee87c 385do_parse_atomic_type(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
386{
387 enum ovsdb_atomic_type type;
388 struct json *json;
389
390 json = unbox_json(parse_json(argv[1]));
391 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
392 json_destroy(json);
393 print_and_free_json(ovsdb_atomic_type_to_json(type));
394}
395
bd76d25d 396static void
c69ee87c 397do_parse_base_type(int argc OVS_UNUSED, char *argv[])
bd76d25d
BP
398{
399 struct ovsdb_base_type base;
400 struct json *json;
401
402 json = unbox_json(parse_json(argv[1]));
403 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
404 json_destroy(json);
405 print_and_free_json(ovsdb_base_type_to_json(&base));
406 ovsdb_base_type_destroy(&base);
407}
408
f85f8ebb 409static void
c69ee87c 410do_parse_type(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
411{
412 struct ovsdb_type type;
413 struct json *json;
414
415 json = unbox_json(parse_json(argv[1]));
416 check_ovsdb_error(ovsdb_type_from_json(&type, json));
417 json_destroy(json);
418 print_and_free_json(ovsdb_type_to_json(&type));
bd76d25d 419 ovsdb_type_destroy(&type);
f85f8ebb
BP
420}
421
422static void
423do_parse_atoms(int argc, char *argv[])
424{
bd76d25d 425 struct ovsdb_base_type base;
f85f8ebb
BP
426 struct json *json;
427 int i;
428
429 json = unbox_json(parse_json(argv[1]));
bd76d25d 430 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
f85f8ebb
BP
431 json_destroy(json);
432
433 for (i = 2; i < argc; i++) {
bd76d25d 434 struct ovsdb_error *error;
f85f8ebb
BP
435 union ovsdb_atom atom;
436
437 json = unbox_json(parse_json(argv[i]));
bd76d25d 438 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
f85f8ebb
BP
439 json_destroy(json);
440
bd76d25d
BP
441 if (error) {
442 print_and_free_ovsdb_error(error);
443 } else {
7bd02255 444 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
bd76d25d
BP
445 ovsdb_atom_destroy(&atom, base.type);
446 }
f85f8ebb 447 }
bd76d25d 448 ovsdb_base_type_destroy(&base);
f85f8ebb
BP
449}
450
5c414a2e
BP
451static void
452do_parse_atom_strings(int argc, char *argv[])
453{
bd76d25d 454 struct ovsdb_base_type base;
5c414a2e
BP
455 struct json *json;
456 int i;
457
458 json = unbox_json(parse_json(argv[1]));
bd76d25d 459 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
5c414a2e
BP
460 json_destroy(json);
461
462 for (i = 2; i < argc; i++) {
463 union ovsdb_atom atom;
464 struct ds out;
465
ce5a3e38 466 die_if_error(ovsdb_atom_from_string(&atom, &base, argv[i], NULL));
5c414a2e
BP
467
468 ds_init(&out);
bd76d25d 469 ovsdb_atom_to_string(&atom, base.type, &out);
5c414a2e
BP
470 puts(ds_cstr(&out));
471 ds_destroy(&out);
472
bd76d25d 473 ovsdb_atom_destroy(&atom, base.type);
5c414a2e 474 }
bd76d25d 475 ovsdb_base_type_destroy(&base);
5c414a2e
BP
476}
477
f85f8ebb 478static void
2b66469b
BP
479do_parse_data__(int argc, char *argv[],
480 struct ovsdb_error *
481 (*parse)(struct ovsdb_datum *datum,
482 const struct ovsdb_type *type,
483 const struct json *json,
484 struct ovsdb_symbol_table *symtab))
f85f8ebb
BP
485{
486 struct ovsdb_type type;
487 struct json *json;
488 int i;
489
490 json = unbox_json(parse_json(argv[1]));
491 check_ovsdb_error(ovsdb_type_from_json(&type, json));
492 json_destroy(json);
493
494 for (i = 2; i < argc; i++) {
495 struct ovsdb_datum datum;
496
497 json = unbox_json(parse_json(argv[i]));
2b66469b 498 check_ovsdb_error(parse(&datum, &type, json, NULL));
f85f8ebb
BP
499 json_destroy(json);
500
7bd02255 501 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
f85f8ebb
BP
502
503 ovsdb_datum_destroy(&datum, &type);
504 }
bd76d25d 505 ovsdb_type_destroy(&type);
f85f8ebb
BP
506}
507
2b66469b
BP
508static void
509do_parse_data(int argc, char *argv[])
510{
511 do_parse_data__(argc, argv, ovsdb_datum_from_json);
512}
513
5c414a2e
BP
514static void
515do_parse_data_strings(int argc, char *argv[])
516{
517 struct ovsdb_type type;
518 struct json *json;
519 int i;
520
521 json = unbox_json(parse_json(argv[1]));
522 check_ovsdb_error(ovsdb_type_from_json(&type, json));
523 json_destroy(json);
524
525 for (i = 2; i < argc; i++) {
526 struct ovsdb_datum datum;
527 struct ds out;
528
ce5a3e38 529 die_if_error(ovsdb_datum_from_string(&datum, &type, argv[i], NULL));
5c414a2e
BP
530
531 ds_init(&out);
532 ovsdb_datum_to_string(&datum, &type, &out);
533 puts(ds_cstr(&out));
534 ds_destroy(&out);
535
536 ovsdb_datum_destroy(&datum, &type);
537 }
bd76d25d 538 ovsdb_type_destroy(&type);
5c414a2e
BP
539}
540
f85f8ebb
BP
541static enum ovsdb_atomic_type compare_atoms_atomic_type;
542
543static int
544compare_atoms(const void *a_, const void *b_)
545{
546 const union ovsdb_atom *a = a_;
547 const union ovsdb_atom *b = b_;
548
549 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
550}
551
552static void
c69ee87c 553do_sort_atoms(int argc OVS_UNUSED, char *argv[])
f85f8ebb 554{
bd76d25d 555 struct ovsdb_base_type base;
f85f8ebb
BP
556 union ovsdb_atom *atoms;
557 struct json *json, **json_atoms;
558 size_t n_atoms;
559 int i;
560
561 json = unbox_json(parse_json(argv[1]));
bd76d25d 562 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
f85f8ebb
BP
563 json_destroy(json);
564
565 json = unbox_json(parse_json(argv[2]));
566 if (json->type != JSON_ARRAY) {
567 ovs_fatal(0, "second argument must be array");
568 }
569
570 /* Convert JSON atoms to internal representation. */
571 n_atoms = json->u.array.n;
572 atoms = xmalloc(n_atoms * sizeof *atoms);
573 for (i = 0; i < n_atoms; i++) {
bd76d25d 574 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
f85f8ebb
BP
575 json->u.array.elems[i], NULL));
576 }
577 json_destroy(json);
578
579 /* Sort atoms. */
bd76d25d 580 compare_atoms_atomic_type = base.type;
f85f8ebb
BP
581 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
582
583 /* Convert internal representation back to JSON. */
584 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
585 for (i = 0; i < n_atoms; i++) {
bd76d25d
BP
586 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
587 ovsdb_atom_destroy(&atoms[i], base.type);
f85f8ebb
BP
588 }
589 print_and_free_json(json_array_create(json_atoms, n_atoms));
93ff0290 590 free(atoms);
bd76d25d 591 ovsdb_base_type_destroy(&base);
f85f8ebb
BP
592}
593
594static void
c69ee87c 595do_parse_column(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
596{
597 struct ovsdb_column *column;
598 struct json *json;
599
600 json = parse_json(argv[2]);
601 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
602 json_destroy(json);
603 print_and_free_json(ovsdb_column_to_json(column));
604 ovsdb_column_destroy(column);
605}
606
607static void
c69ee87c 608do_parse_table(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
609{
610 struct ovsdb_table_schema *ts;
c5f341ab 611 bool default_is_root;
f85f8ebb
BP
612 struct json *json;
613
c5f341ab
BP
614 default_is_root = argc > 3 && !strcmp(argv[3], "true");
615
f85f8ebb
BP
616 json = parse_json(argv[2]);
617 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
618 json_destroy(json);
c5f341ab 619 print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
f85f8ebb
BP
620 ovsdb_table_schema_destroy(ts);
621}
622
623static void
624do_parse_rows(int argc, char *argv[])
625{
626 struct ovsdb_column_set all_columns;
627 struct ovsdb_table_schema *ts;
628 struct ovsdb_table *table;
629 struct json *json;
630 int i;
631
632 json = unbox_json(parse_json(argv[1]));
633 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
634 json_destroy(json);
635
636 table = ovsdb_table_create(ts);
637 ovsdb_column_set_init(&all_columns);
638 ovsdb_column_set_add_all(&all_columns, table);
639
640 for (i = 2; i < argc; i++) {
641 struct ovsdb_column_set columns;
642 struct ovsdb_row *row;
643
644 ovsdb_column_set_init(&columns);
645 row = ovsdb_row_create(table);
646
647 json = unbox_json(parse_json(argv[i]));
648 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
649 json_destroy(json);
650
651 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
652
653 if (columns.n_columns) {
654 struct svec names;
655 size_t j;
656 char *s;
657
658 svec_init(&names);
659 for (j = 0; j < columns.n_columns; j++) {
660 svec_add(&names, columns.columns[j]->name);
661 }
662 svec_sort(&names);
663 s = svec_join(&names, ", ", "");
664 puts(s);
665 free(s);
666 svec_destroy(&names);
667 } else {
668 printf("<none>\n");
669 }
670
671 ovsdb_column_set_destroy(&columns);
672 ovsdb_row_destroy(row);
673 }
674
675 ovsdb_column_set_destroy(&all_columns);
676 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
677}
678
679static void
680do_compare_rows(int argc, char *argv[])
681{
682 struct ovsdb_column_set all_columns;
683 struct ovsdb_table_schema *ts;
684 struct ovsdb_table *table;
685 struct ovsdb_row **rows;
686 struct json *json;
687 char **names;
688 int n_rows;
689 int i, j;
690
691 json = unbox_json(parse_json(argv[1]));
692 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
693 json_destroy(json);
694
695 table = ovsdb_table_create(ts);
696 ovsdb_column_set_init(&all_columns);
697 ovsdb_column_set_add_all(&all_columns, table);
698
699 n_rows = argc - 2;
700 rows = xmalloc(sizeof *rows * n_rows);
701 names = xmalloc(sizeof *names * n_rows);
702 for (i = 0; i < n_rows; i++) {
703 rows[i] = ovsdb_row_create(table);
704
705 json = parse_json(argv[i + 2]);
706 if (json->type != JSON_ARRAY || json->u.array.n != 2
707 || json->u.array.elems[0]->type != JSON_STRING) {
708 ovs_fatal(0, "\"%s\" does not have expected form "
709 "[\"name\", {data}]", argv[i]);
710 }
711 names[i] = xstrdup(json->u.array.elems[0]->u.string);
712 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
713 NULL, NULL));
714 json_destroy(json);
715 }
716 for (i = 0; i < n_rows; i++) {
717 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
718 for (j = i + 1; j < n_rows; j++) {
719 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
720 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
721 printf("%s == %s\n", names[i], names[j]);
722 if (i_hash != j_hash) {
723 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
724 abort();
725 }
726 } else if (i_hash == j_hash) {
727 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
728 }
729 }
730 }
93ff0290
BP
731 for (i = 0; i < n_rows; i++) {
732 ovsdb_row_destroy(rows[i]);
733 free(names[i]);
734 }
f85f8ebb
BP
735 free(rows);
736 free(names);
737
738 ovsdb_column_set_destroy(&all_columns);
739 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
740}
741
742static void
743do_parse_conditions(int argc, char *argv[])
744{
745 struct ovsdb_table_schema *ts;
746 struct json *json;
747 int exit_code = 0;
748 int i;
749
750 json = unbox_json(parse_json(argv[1]));
751 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
752 json_destroy(json);
753
754 for (i = 2; i < argc; i++) {
755 struct ovsdb_condition cnd;
756 struct ovsdb_error *error;
757
758 json = parse_json(argv[i]);
759 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
760 if (!error) {
761 print_and_free_json(ovsdb_condition_to_json(&cnd));
762 } else {
763 char *s = ovsdb_error_to_string(error);
764 ovs_error(0, "%s", s);
765 free(s);
766 ovsdb_error_destroy(error);
767 exit_code = 1;
768 }
769 json_destroy(json);
770
771 ovsdb_condition_destroy(&cnd);
772 }
773 ovsdb_table_schema_destroy(ts);
774
775 exit(exit_code);
776}
777
778static void
c69ee87c 779do_evaluate_conditions(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
780{
781 struct ovsdb_table_schema *ts;
782 struct ovsdb_table *table;
783 struct ovsdb_condition *conditions;
784 size_t n_conditions;
785 struct ovsdb_row **rows;
786 size_t n_rows;
787 struct json *json;
788 size_t i, j;
789
790 /* Parse table schema, create table. */
791 json = unbox_json(parse_json(argv[1]));
792 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
793 json_destroy(json);
794
795 table = ovsdb_table_create(ts);
796
797 /* Parse conditions. */
798 json = parse_json(argv[2]);
799 if (json->type != JSON_ARRAY) {
800 ovs_fatal(0, "CONDITION argument is not JSON array");
801 }
802 n_conditions = json->u.array.n;
803 conditions = xmalloc(n_conditions * sizeof *conditions);
804 for (i = 0; i < n_conditions; i++) {
805 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
806 NULL, &conditions[i]));
807 }
808 json_destroy(json);
809
810 /* Parse rows. */
811 json = parse_json(argv[3]);
812 if (json->type != JSON_ARRAY) {
813 ovs_fatal(0, "ROW argument is not JSON array");
814 }
815 n_rows = json->u.array.n;
816 rows = xmalloc(n_rows * sizeof *rows);
817 for (i = 0; i < n_rows; i++) {
818 rows[i] = ovsdb_row_create(table);
819 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
820 NULL, NULL));
821 }
822 json_destroy(json);
823
824 for (i = 0; i < n_conditions; i++) {
34582733 825 printf("condition %2"PRIuSIZE":", i);
f85f8ebb
BP
826 for (j = 0; j < n_rows; j++) {
827 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
828 if (j % 5 == 0) {
829 putchar(' ');
830 }
831 putchar(result ? 'T' : '-');
832 }
833 printf("\n");
834 }
835
836 for (i = 0; i < n_conditions; i++) {
837 ovsdb_condition_destroy(&conditions[i]);
838 }
93ff0290 839 free(conditions);
f85f8ebb
BP
840 for (i = 0; i < n_rows; i++) {
841 ovsdb_row_destroy(rows[i]);
842 }
93ff0290 843 free(rows);
f85f8ebb
BP
844 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
845}
846
e9f8f936
BP
847static void
848do_parse_mutations(int argc, char *argv[])
849{
850 struct ovsdb_table_schema *ts;
851 struct json *json;
852 int exit_code = 0;
853 int i;
854
855 json = unbox_json(parse_json(argv[1]));
856 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
857 json_destroy(json);
858
859 for (i = 2; i < argc; i++) {
860 struct ovsdb_mutation_set set;
861 struct ovsdb_error *error;
862
863 json = parse_json(argv[i]);
864 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
865 if (!error) {
866 print_and_free_json(ovsdb_mutation_set_to_json(&set));
867 } else {
868 char *s = ovsdb_error_to_string(error);
869 ovs_error(0, "%s", s);
870 free(s);
871 ovsdb_error_destroy(error);
872 exit_code = 1;
873 }
874 json_destroy(json);
875
876 ovsdb_mutation_set_destroy(&set);
877 }
878 ovsdb_table_schema_destroy(ts);
879
880 exit(exit_code);
881}
882
883static void
c69ee87c 884do_execute_mutations(int argc OVS_UNUSED, char *argv[])
e9f8f936
BP
885{
886 struct ovsdb_table_schema *ts;
887 struct ovsdb_table *table;
888 struct ovsdb_mutation_set *sets;
889 size_t n_sets;
890 struct ovsdb_row **rows;
891 size_t n_rows;
892 struct json *json;
893 size_t i, j;
894
895 /* Parse table schema, create table. */
896 json = unbox_json(parse_json(argv[1]));
897 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
898 json_destroy(json);
899
900 table = ovsdb_table_create(ts);
901
902 /* Parse mutations. */
903 json = parse_json(argv[2]);
904 if (json->type != JSON_ARRAY) {
905 ovs_fatal(0, "MUTATION argument is not JSON array");
906 }
907 n_sets = json->u.array.n;
908 sets = xmalloc(n_sets * sizeof *sets);
909 for (i = 0; i < n_sets; i++) {
910 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
911 json->u.array.elems[i],
912 NULL, &sets[i]));
913 }
914 json_destroy(json);
915
916 /* Parse rows. */
917 json = parse_json(argv[3]);
918 if (json->type != JSON_ARRAY) {
919 ovs_fatal(0, "ROW argument is not JSON array");
920 }
921 n_rows = json->u.array.n;
922 rows = xmalloc(n_rows * sizeof *rows);
923 for (i = 0; i < n_rows; i++) {
924 rows[i] = ovsdb_row_create(table);
925 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
926 NULL, NULL));
927 }
928 json_destroy(json);
929
930 for (i = 0; i < n_sets; i++) {
34582733 931 printf("mutation %2"PRIuSIZE":\n", i);
e9f8f936
BP
932 for (j = 0; j < n_rows; j++) {
933 struct ovsdb_error *error;
934 struct ovsdb_row *row;
935
936 row = ovsdb_row_clone(rows[j]);
937 error = ovsdb_mutation_set_execute(row, &sets[i]);
938
34582733 939 printf("row %"PRIuSIZE": ", j);
e9f8f936
BP
940 if (error) {
941 print_and_free_ovsdb_error(error);
942 } else {
943 struct ovsdb_column_set columns;
944 struct shash_node *node;
945
946 ovsdb_column_set_init(&columns);
947 SHASH_FOR_EACH (node, &ts->columns) {
948 struct ovsdb_column *c = node->data;
949 if (!ovsdb_datum_equals(&row->fields[c->index],
950 &rows[j]->fields[c->index],
951 &c->type)) {
952 ovsdb_column_set_add(&columns, c);
953 }
954 }
955 if (columns.n_columns) {
956 print_and_free_json(ovsdb_row_to_json(row, &columns));
957 } else {
958 printf("no change\n");
959 }
960 ovsdb_column_set_destroy(&columns);
961 }
962 ovsdb_row_destroy(row);
963 }
964 printf("\n");
965 }
966
967 for (i = 0; i < n_sets; i++) {
968 ovsdb_mutation_set_destroy(&sets[i]);
969 }
93ff0290 970 free(sets);
e9f8f936
BP
971 for (i = 0; i < n_rows; i++) {
972 ovsdb_row_destroy(rows[i]);
973 }
93ff0290 974 free(rows);
e9f8f936
BP
975 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
976}
977
29d7226e
BP
978/* Inserts a row, without bothering to update metadata such as refcounts. */
979static void
980put_row(struct ovsdb_table *table, struct ovsdb_row *row)
981{
982 const struct uuid *uuid = ovsdb_row_get_uuid(row);
983 if (!ovsdb_table_get_row(table, uuid)) {
984 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
985 }
986}
987
f85f8ebb
BP
988struct do_query_cbdata {
989 struct uuid *row_uuids;
990 int *counts;
991 size_t n_rows;
992};
993
994static bool
995do_query_cb(const struct ovsdb_row *row, void *cbdata_)
996{
997 struct do_query_cbdata *cbdata = cbdata_;
998 size_t i;
999
1000 for (i = 0; i < cbdata->n_rows; i++) {
1001 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1002 cbdata->counts[i]++;
1003 }
1004 }
1005
1006 return true;
1007}
1008
1009static void
c69ee87c 1010do_query(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
1011{
1012 struct do_query_cbdata cbdata;
1013 struct ovsdb_table_schema *ts;
1014 struct ovsdb_table *table;
1015 struct json *json;
1016 int exit_code = 0;
1017 size_t i;
1018
1019 /* Parse table schema, create table. */
1020 json = unbox_json(parse_json(argv[1]));
1021 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1022 json_destroy(json);
1023
1024 table = ovsdb_table_create(ts);
1025
1026 /* Parse rows, add to table. */
1027 json = parse_json(argv[2]);
1028 if (json->type != JSON_ARRAY) {
1029 ovs_fatal(0, "ROW argument is not JSON array");
1030 }
1031 cbdata.n_rows = json->u.array.n;
1032 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1033 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1034 for (i = 0; i < cbdata.n_rows; i++) {
1035 struct ovsdb_row *row = ovsdb_row_create(table);
1036 uuid_generate(ovsdb_row_get_uuid_rw(row));
1037 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1038 NULL, NULL));
1039 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1040 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1041 UUID_ARGS(ovsdb_row_get_uuid(row)));
1042 }
1043 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
29d7226e 1044 put_row(table, row);
f85f8ebb
BP
1045 }
1046 json_destroy(json);
1047
1048 /* Parse conditions and execute queries. */
1049 json = parse_json(argv[3]);
1050 if (json->type != JSON_ARRAY) {
1051 ovs_fatal(0, "CONDITION argument is not JSON array");
1052 }
1053 for (i = 0; i < json->u.array.n; i++) {
1054 struct ovsdb_condition cnd;
1055 size_t j;
1056
1057 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1058 NULL, &cnd));
1059
1060 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1061 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1062
34582733 1063 printf("query %2"PRIuSIZE":", i);
f85f8ebb
BP
1064 for (j = 0; j < cbdata.n_rows; j++) {
1065 if (j % 5 == 0) {
1066 putchar(' ');
1067 }
1068 if (cbdata.counts[j]) {
1069 printf("%d", cbdata.counts[j]);
1070 if (cbdata.counts[j] > 1) {
1071 /* Dup! */
1072 exit_code = 1;
1073 }
1074 } else {
1075 putchar('-');
1076 }
1077 }
1078 putchar('\n');
1079
1080 ovsdb_condition_destroy(&cnd);
1081 }
1082 json_destroy(json);
1083
1084 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1085
1086 exit(exit_code);
1087}
1088
1089struct do_query_distinct_class {
1090 struct ovsdb_row *example;
1091 int count;
1092};
1093
1094struct do_query_distinct_row {
1095 struct uuid uuid;
1096 struct do_query_distinct_class *class;
1097};
1098
1099static void
c69ee87c 1100do_query_distinct(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
1101{
1102 struct ovsdb_column_set columns;
1103 struct ovsdb_table_schema *ts;
1104 struct ovsdb_table *table;
1105 struct do_query_distinct_row *rows;
1106 size_t n_rows;
1107 struct do_query_distinct_class *classes;
1108 size_t n_classes;
1109 struct json *json;
1110 int exit_code = 0;
2a022368 1111 size_t i;
f85f8ebb
BP
1112
1113 /* Parse table schema, create table. */
1114 json = unbox_json(parse_json(argv[1]));
1115 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1116 json_destroy(json);
1117
1118 table = ovsdb_table_create(ts);
1119
1120 /* Parse column set. */
1121 json = parse_json(argv[4]);
1cb29ab0
BP
1122 check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1123 &columns));
f85f8ebb
BP
1124 json_destroy(json);
1125
1126 /* Parse rows, add to table. */
1127 json = parse_json(argv[2]);
1128 if (json->type != JSON_ARRAY) {
1129 ovs_fatal(0, "ROW argument is not JSON array");
1130 }
1131 n_rows = json->u.array.n;
1132 rows = xmalloc(n_rows * sizeof *rows);
1133 classes = xmalloc(n_rows * sizeof *classes);
1134 n_classes = 0;
1135 for (i = 0; i < n_rows; i++) {
1136 struct ovsdb_row *row;
1137 size_t j;
1138
1139 /* Parse row. */
1140 row = ovsdb_row_create(table);
1141 uuid_generate(ovsdb_row_get_uuid_rw(row));
1142 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1143 NULL, NULL));
1144
1145 /* Initialize row and find equivalence class. */
1146 rows[i].uuid = *ovsdb_row_get_uuid(row);
1147 rows[i].class = NULL;
1148 for (j = 0; j < n_classes; j++) {
1149 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1150 rows[i].class = &classes[j];
1151 break;
1152 }
1153 }
1154 if (!rows[i].class) {
1155 rows[i].class = &classes[n_classes];
1156 classes[n_classes].example = ovsdb_row_clone(row);
1157 n_classes++;
1158 }
1159
1160 /* Add row to table. */
1161 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1162 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1163 UUID_ARGS(ovsdb_row_get_uuid(row)));
1164 }
29d7226e 1165 put_row(table, row);
f85f8ebb
BP
1166
1167 }
1168 json_destroy(json);
1169
1170 /* Parse conditions and execute queries. */
1171 json = parse_json(argv[3]);
1172 if (json->type != JSON_ARRAY) {
1173 ovs_fatal(0, "CONDITION argument is not JSON array");
1174 }
1175 for (i = 0; i < json->u.array.n; i++) {
1176 struct ovsdb_row_set results;
1177 struct ovsdb_condition cnd;
2a022368 1178 size_t j;
f85f8ebb
BP
1179
1180 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1181 NULL, &cnd));
1182
1183 for (j = 0; j < n_classes; j++) {
1184 classes[j].count = 0;
1185 }
1186 ovsdb_row_set_init(&results);
1187 ovsdb_query_distinct(table, &cnd, &columns, &results);
1188 for (j = 0; j < results.n_rows; j++) {
2a022368
BP
1189 size_t k;
1190
f85f8ebb
BP
1191 for (k = 0; k < n_rows; k++) {
1192 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1193 &rows[k].uuid)) {
1194 rows[k].class->count++;
1195 }
1196 }
1197 }
1198 ovsdb_row_set_destroy(&results);
1199
34582733 1200 printf("query %2"PRIuSIZE":", i);
f85f8ebb
BP
1201 for (j = 0; j < n_rows; j++) {
1202 int count = rows[j].class->count;
1203
1204 if (j % 5 == 0) {
1205 putchar(' ');
1206 }
1207 if (count > 1) {
1208 /* Dup! */
1209 printf("%d", count);
1210 exit_code = 1;
1211 } else if (count == 1) {
1212 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1213 } else {
1214 putchar('-');
1215 }
1216 }
1217 putchar('\n');
1218
1219 ovsdb_condition_destroy(&cnd);
1220 }
1221 json_destroy(json);
1222
1223 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1224
1225 exit(exit_code);
1226}
1227
0d0f05b9 1228static void
c69ee87c 1229do_parse_schema(int argc OVS_UNUSED, char *argv[])
0d0f05b9
BP
1230{
1231 struct ovsdb_schema *schema;
1232 struct json *json;
1233
1234 json = parse_json(argv[1]);
1235 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1236 json_destroy(json);
1237 print_and_free_json(ovsdb_schema_to_json(schema));
1238 ovsdb_schema_destroy(schema);
1239}
1240
f85f8ebb 1241static void
c69ee87c 1242do_execute(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
1243{
1244 struct ovsdb_schema *schema;
1245 struct json *json;
1246 struct ovsdb *db;
1247 int i;
1248
1249 /* Create database. */
1250 json = parse_json(argv[1]);
1251 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1252 json_destroy(json);
bd06962a 1253 db = ovsdb_create(schema);
f85f8ebb
BP
1254
1255 for (i = 2; i < argc; i++) {
1256 struct json *params, *result;
1257 char *s;
1258
1259 params = parse_json(argv[i]);
da897f41 1260 result = ovsdb_execute(db, NULL, params, 0, NULL);
f85f8ebb
BP
1261 s = json_to_string(result, JSSF_SORT);
1262 printf("%s\n", s);
93ff0290 1263 free(s);
f85f8ebb
BP
1264 json_destroy(params);
1265 json_destroy(result);
1266 }
1267
1268 ovsdb_destroy(db);
1269}
1270
1271struct test_trigger {
1272 struct ovsdb_trigger trigger;
1273 int number;
1274};
1275
1276static void
1277do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1278{
1279 struct json *result;
1280 char *s;
1281
1282 result = ovsdb_trigger_steal_result(&t->trigger);
1283 s = json_to_string(result, JSSF_SORT);
1284 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
93ff0290 1285 free(s);
f85f8ebb
BP
1286 json_destroy(result);
1287 ovsdb_trigger_destroy(&t->trigger);
1288 free(t);
1289}
1290
1291static void
c69ee87c 1292do_trigger(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
1293{
1294 struct ovsdb_schema *schema;
e317253b 1295 struct ovsdb_session session;
b4e8d170 1296 struct ovsdb_server server;
f85f8ebb
BP
1297 struct json *json;
1298 struct ovsdb *db;
1299 long long int now;
1300 int number;
1301 int i;
1302
1303 /* Create database. */
1304 json = parse_json(argv[1]);
1305 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1306 json_destroy(json);
bd06962a 1307 db = ovsdb_create(schema);
f85f8ebb 1308
b4e8d170
BP
1309 ovsdb_server_init(&server);
1310 ovsdb_server_add_db(&server, db);
1311 ovsdb_session_init(&session, &server);
e317253b 1312
f85f8ebb
BP
1313 now = 0;
1314 number = 0;
1315 for (i = 2; i < argc; i++) {
1316 struct json *params = parse_json(argv[i]);
1317 if (params->type == JSON_ARRAY
1318 && json_array(params)->n == 2
1319 && json_array(params)->elems[0]->type == JSON_STRING
1320 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1321 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1322 now += json_integer(json_array(params)->elems[1]);
1323 json_destroy(params);
1324 } else {
1325 struct test_trigger *t = xmalloc(sizeof *t);
b4e8d170 1326 ovsdb_trigger_init(&session, db, &t->trigger, params, now);
f85f8ebb
BP
1327 t->number = number++;
1328 if (ovsdb_trigger_is_complete(&t->trigger)) {
1329 do_trigger_dump(t, now, "immediate");
1330 } else {
1331 printf("t=%lld: new trigger %d\n", now, t->number);
1332 }
1333 }
1334
1335 ovsdb_trigger_run(db, now);
e317253b
BP
1336 while (!list_is_empty(&session.completions)) {
1337 do_trigger_dump(CONTAINER_OF(list_pop_front(&session.completions),
f85f8ebb
BP
1338 struct test_trigger, trigger.node),
1339 now, "delayed");
1340 }
1341
1342 ovsdb_trigger_wait(db, now);
1343 poll_immediate_wake();
1344 poll_block();
1345 }
1346
b4e8d170 1347 ovsdb_server_destroy(&server);
f85f8ebb
BP
1348 ovsdb_destroy(db);
1349}
1350
1351static void
c69ee87c 1352do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
1353{
1354 usage();
1355}
1356\f
1357/* "transact" command. */
1358
1359static struct ovsdb *do_transact_db;
1360static struct ovsdb_txn *do_transact_txn;
1361static struct ovsdb_table *do_transact_table;
1362
1363static void
c69ee87c 1364do_transact_commit(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb 1365{
85683160 1366 ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
f85f8ebb
BP
1367 do_transact_txn = NULL;
1368}
1369
1370static void
c69ee87c 1371do_transact_abort(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
1372{
1373 ovsdb_txn_abort(do_transact_txn);
1374 do_transact_txn = NULL;
1375}
1376
1377static void
1378uuid_from_integer(int integer, struct uuid *uuid)
1379{
1380 uuid_zero(uuid);
1381 uuid->parts[3] = integer;
1382}
1383
1384static const struct ovsdb_row *
1385do_transact_find_row(const char *uuid_string)
1386{
1387 const struct ovsdb_row *row;
1388 struct uuid uuid;
1389
1390 uuid_from_integer(atoi(uuid_string), &uuid);
1391 row = ovsdb_table_get_row(do_transact_table, &uuid);
1392 if (!row) {
1393 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1394 UUID_ARGS(&uuid));
1395 }
1396 return row;
1397}
1398
1399static void
1400do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1401 int integer)
1402{
1403 if (integer != -1) {
1404 const struct ovsdb_column *column;
1405
1406 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1407 column_name);
1408 row->fields[column->index].keys[0].integer = integer;
1409 }
1410}
1411
1412static int
1413do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1414{
1415 const struct ovsdb_column *column;
1416
1417 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1418 column_name);
1419 return row->fields[column->index].keys[0].integer;
1420}
1421
1422static void
1423do_transact_set_i_j(struct ovsdb_row *row,
1424 const char *i_string, const char *j_string)
1425{
1426 do_transact_set_integer(row, "i", atoi(i_string));
1427 do_transact_set_integer(row, "j", atoi(j_string));
1428}
1429
1430static void
c69ee87c 1431do_transact_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
1432{
1433 struct ovsdb_row *row;
1434 struct uuid *uuid;
1435
1436 row = ovsdb_row_create(do_transact_table);
1437
1438 /* Set UUID. */
1439 uuid = ovsdb_row_get_uuid_rw(row);
1440 uuid_from_integer(atoi(argv[1]), uuid);
1441 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1442 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1443 UUID_ARGS(uuid));
1444 }
1445
1446 do_transact_set_i_j(row, argv[2], argv[3]);
1447
1448 /* Insert row. */
1449 ovsdb_txn_row_insert(do_transact_txn, row);
1450}
1451
1452static void
c69ee87c 1453do_transact_delete(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
1454{
1455 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1456 ovsdb_txn_row_delete(do_transact_txn, row);
1457}
1458
1459static void
c69ee87c 1460do_transact_modify(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
1461{
1462 const struct ovsdb_row *row_ro;
1463 struct ovsdb_row *row_rw;
1464
1465 row_ro = do_transact_find_row(argv[1]);
1466 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1467 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1468}
1469
1470static int
1471compare_rows_by_uuid(const void *a_, const void *b_)
1472{
1473 struct ovsdb_row *const *ap = a_;
1474 struct ovsdb_row *const *bp = b_;
1475
1476 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1477}
1478
1479static void
c69ee87c 1480do_transact_print(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
1481{
1482 const struct ovsdb_row **rows;
1483 const struct ovsdb_row *row;
1484 size_t n_rows;
1485 size_t i;
1486
1487 n_rows = hmap_count(&do_transact_table->rows);
1488 rows = xmalloc(n_rows * sizeof *rows);
1489 i = 0;
4e8e4213 1490 HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
f85f8ebb
BP
1491 rows[i++] = row;
1492 }
1493 assert(i == n_rows);
1494
1495 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1496
1497 for (i = 0; i < n_rows; i++) {
1498 printf("\n%"PRId32": i=%d, j=%d",
1499 ovsdb_row_get_uuid(rows[i])->parts[3],
1500 do_transact_get_integer(rows[i], "i"),
1501 do_transact_get_integer(rows[i], "j"));
1502 }
1503
1504 free(rows);
1505}
1506
1507static void
1508do_transact(int argc, char *argv[])
1509{
5f383751 1510 static const struct ovs_cmdl_command do_transact_commands[] = {
451de37e
AW
1511 { "commit", NULL, 0, 0, do_transact_commit },
1512 { "abort", NULL, 0, 0, do_transact_abort },
1513 { "insert", NULL, 2, 3, do_transact_insert },
1514 { "delete", NULL, 1, 1, do_transact_delete },
1515 { "modify", NULL, 2, 3, do_transact_modify },
1516 { "print", NULL, 0, 0, do_transact_print },
1517 { NULL, NULL, 0, 0, NULL },
f85f8ebb
BP
1518 };
1519
1520 struct ovsdb_schema *schema;
1521 struct json *json;
1522 int i;
1523
1524 /* Create table. */
1525 json = parse_json("{\"name\": \"testdb\", "
1526 " \"tables\": "
1527 " {\"mytable\": "
1528 " {\"columns\": "
1529 " {\"i\": {\"type\": \"integer\"}, "
1530 " \"j\": {\"type\": \"integer\"}}}}}");
1531 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1532 json_destroy(json);
bd06962a 1533 do_transact_db = ovsdb_create(schema);
f85f8ebb
BP
1534 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1535 assert(do_transact_table != NULL);
1536
1537 for (i = 1; i < argc; i++) {
1538 struct json *command;
1539 size_t n_args;
1540 char **args;
1541 int j;
1542
1543 command = parse_json(argv[i]);
1544 if (command->type != JSON_ARRAY) {
1545 ovs_fatal(0, "transaction %d must be JSON array "
1546 "with at least 1 element", i);
1547 }
1548
1549 n_args = command->u.array.n;
1550 args = xmalloc((n_args + 1) * sizeof *args);
1551 for (j = 0; j < n_args; j++) {
1552 struct json *s = command->u.array.elems[j];
1553 if (s->type != JSON_STRING) {
1554 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1555 i, j);
1556 }
1557 args[j] = xstrdup(json_string(s));
1558 }
1559 args[n_args] = NULL;
1560
1561 if (!do_transact_txn) {
1562 do_transact_txn = ovsdb_txn_create(do_transact_db);
1563 }
1564
1565 for (j = 0; j < n_args; j++) {
1566 if (j) {
1567 putchar(' ');
1568 }
1569 fputs(args[j], stdout);
1570 }
1571 fputs(":", stdout);
5f383751 1572 ovs_cmdl_run_command(n_args, args, do_transact_commands);
f85f8ebb
BP
1573 putchar('\n');
1574
1575 for (j = 0; j < n_args; j++) {
1576 free(args[j]);
1577 }
1578 free(args);
1579 json_destroy(command);
1580 }
1581 ovsdb_txn_abort(do_transact_txn);
1582 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1583}
1584
c3bb4bd7 1585static int
e9011ac8 1586compare_link1(const void *a_, const void *b_)
c3bb4bd7 1587{
e9011ac8
BP
1588 const struct idltest_link1 *const *ap = a_;
1589 const struct idltest_link1 *const *bp = b_;
1590 const struct idltest_link1 *a = *ap;
1591 const struct idltest_link1 *b = *bp;
c3bb4bd7
BP
1592
1593 return a->i < b->i ? -1 : a->i > b->i;
1594}
1595
1596static void
1597print_idl(struct ovsdb_idl *idl, int step)
1598{
1599 const struct idltest_simple *s;
e9011ac8
BP
1600 const struct idltest_link1 *l1;
1601 const struct idltest_link2 *l2;
c3bb4bd7
BP
1602 int n = 0;
1603
1604 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1605 size_t i;
1606
1607 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1608 step, s->i, s->r, s->b ? "true" : "false",
1609 s->s, UUID_ARGS(&s->u));
1610 for (i = 0; i < s->n_ia; i++) {
1611 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1612 }
1613 printf("] ra=[");
1614 for (i = 0; i < s->n_ra; i++) {
1615 printf("%s%g", i ? " " : "", s->ra[i]);
1616 }
1617 printf("] ba=[");
1618 for (i = 0; i < s->n_ba; i++) {
1619 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1620 }
1621 printf("] sa=[");
1622 for (i = 0; i < s->n_sa; i++) {
1623 printf("%s%s", i ? " " : "", s->sa[i]);
1624 }
1625 printf("] ua=[");
1626 for (i = 0; i < s->n_ua; i++) {
1627 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1628 }
1629 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1630 n++;
1631 }
e9011ac8
BP
1632 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1633 struct idltest_link1 **links;
c3bb4bd7
BP
1634 size_t i;
1635
e9011ac8
BP
1636 printf("%03d: i=%"PRId64" k=", step, l1->i);
1637 if (l1->k) {
1638 printf("%"PRId64, l1->k->i);
c3bb4bd7
BP
1639 }
1640 printf(" ka=[");
e9011ac8
BP
1641 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1642 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1643 for (i = 0; i < l1->n_ka; i++) {
c3bb4bd7
BP
1644 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1645 }
1646 free(links);
e9011ac8
BP
1647 printf("] l2=");
1648 if (l1->l2) {
1649 printf("%"PRId64, l1->l2->i);
1650 }
1651 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1652 n++;
1653 }
1654 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1655 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1656 if (l2->l1) {
1657 printf("%"PRId64, l2->l1->i);
1658 }
1659 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
c3bb4bd7
BP
1660 n++;
1661 }
1662 if (!n) {
1663 printf("%03d: empty\n", step);
1664 }
1665}
1666
c3bb4bd7
BP
1667static void
1668parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1669 size_t *n)
1670{
1671 struct uuid uuid;
1672
1673 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
34582733 1674 char *name = xasprintf("#%"PRIuSIZE"#", *n);
2d2d6d4a
BP
1675 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1676 ovsdb_symbol_table_put(symtab, name, &uuid, false);
c3bb4bd7
BP
1677 free(name);
1678 *n += 1;
1679 } else if (json->type == JSON_ARRAY) {
1680 size_t i;
1681
1682 for (i = 0; i < json->u.array.n; i++) {
1683 parse_uuids(json->u.array.elems[i], symtab, n);
1684 }
1685 } else if (json->type == JSON_OBJECT) {
1686 const struct shash_node *node;
1687
1688 SHASH_FOR_EACH (node, json_object(json)) {
1689 parse_uuids(node->data, symtab, n);
1690 }
1691 }
1692}
1693
1694static void
1695substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1696{
1697 if (json->type == JSON_STRING) {
2d2d6d4a 1698 const struct ovsdb_symbol *symbol;
c3bb4bd7 1699
2d2d6d4a
BP
1700 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1701 if (symbol) {
c3bb4bd7 1702 free(json->u.string);
2d2d6d4a 1703 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
c3bb4bd7
BP
1704 }
1705 } else if (json->type == JSON_ARRAY) {
1706 size_t i;
1707
1708 for (i = 0; i < json->u.array.n; i++) {
1709 substitute_uuids(json->u.array.elems[i], symtab);
1710 }
1711 } else if (json->type == JSON_OBJECT) {
1712 const struct shash_node *node;
1713
1714 SHASH_FOR_EACH (node, json_object(json)) {
1715 substitute_uuids(node->data, symtab);
1716 }
1717 }
1718}
1719
475281c0
BP
1720static const struct idltest_simple *
1721idltest_find_simple(struct ovsdb_idl *idl, int i)
1722{
1723 const struct idltest_simple *s;
1724
1725 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1726 if (s->i == i) {
1727 return s;
1728 }
1729 }
1730 return NULL;
1731}
1732
1733static void
1734idl_set(struct ovsdb_idl *idl, char *commands, int step)
1735{
1736 char *cmd, *save_ptr1 = NULL;
1737 struct ovsdb_idl_txn *txn;
1738 enum ovsdb_idl_txn_status status;
b54e22e9 1739 bool increment = false;
475281c0
BP
1740
1741 txn = ovsdb_idl_txn_create(idl);
1742 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1743 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1744 char *save_ptr2 = NULL;
1745 char *name, *arg1, *arg2, *arg3;
1746
1747 name = strtok_r(cmd, " ", &save_ptr2);
1748 arg1 = strtok_r(NULL, " ", &save_ptr2);
1749 arg2 = strtok_r(NULL, " ", &save_ptr2);
1750 arg3 = strtok_r(NULL, " ", &save_ptr2);
1751
1752 if (!strcmp(name, "set")) {
1753 const struct idltest_simple *s;
1754
1755 if (!arg3) {
1756 ovs_fatal(0, "\"set\" command requires 3 arguments");
1757 }
1758
1759 s = idltest_find_simple(idl, atoi(arg1));
1760 if (!s) {
1761 ovs_fatal(0, "\"set\" command asks for nonexistent "
1762 "i=%d", atoi(arg1));
1763 }
1764
1765 if (!strcmp(arg2, "b")) {
1766 idltest_simple_set_b(s, atoi(arg3));
1767 } else if (!strcmp(arg2, "s")) {
1768 idltest_simple_set_s(s, arg3);
1769 } else if (!strcmp(arg2, "u")) {
1770 struct uuid uuid;
bca51200
BP
1771 if (!uuid_from_string(&uuid, arg3)) {
1772 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
1773 }
475281c0
BP
1774 idltest_simple_set_u(s, uuid);
1775 } else if (!strcmp(arg2, "r")) {
1776 idltest_simple_set_r(s, atof(arg3));
1777 } else {
1778 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1779 arg2);
1780 }
1781 } else if (!strcmp(name, "insert")) {
1782 struct idltest_simple *s;
1783
1784 if (!arg1 || arg2) {
6484e7cd 1785 ovs_fatal(0, "\"insert\" command requires 1 argument");
475281c0
BP
1786 }
1787
1788 s = idltest_simple_insert(txn);
1789 idltest_simple_set_i(s, atoi(arg1));
1790 } else if (!strcmp(name, "delete")) {
1791 const struct idltest_simple *s;
1792
1793 if (!arg1 || arg2) {
6484e7cd 1794 ovs_fatal(0, "\"delete\" command requires 1 argument");
475281c0
BP
1795 }
1796
1797 s = idltest_find_simple(idl, atoi(arg1));
1798 if (!s) {
6484e7cd 1799 ovs_fatal(0, "\"delete\" command asks for nonexistent "
475281c0
BP
1800 "i=%d", atoi(arg1));
1801 }
1802 idltest_simple_delete(s);
a91c6104
BP
1803 } else if (!strcmp(name, "verify")) {
1804 const struct idltest_simple *s;
1805
1806 if (!arg2 || arg3) {
1807 ovs_fatal(0, "\"verify\" command requires 2 arguments");
1808 }
1809
1810 s = idltest_find_simple(idl, atoi(arg1));
1811 if (!s) {
1812 ovs_fatal(0, "\"verify\" command asks for nonexistent "
1813 "i=%d", atoi(arg1));
1814 }
1815
1816 if (!strcmp(arg2, "i")) {
1817 idltest_simple_verify_i(s);
1818 } else if (!strcmp(arg2, "b")) {
1819 idltest_simple_verify_b(s);
1820 } else if (!strcmp(arg2, "s")) {
1821 idltest_simple_verify_s(s);
1822 } else if (!strcmp(arg2, "u")) {
1823 idltest_simple_verify_s(s);
1824 } else if (!strcmp(arg2, "r")) {
1825 idltest_simple_verify_r(s);
1826 } else {
1827 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
1828 arg2);
1829 }
b54e22e9 1830 } else if (!strcmp(name, "increment")) {
94fbe1aa
BP
1831 const struct idltest_simple *s;
1832
1833 if (!arg1 || arg2) {
1834 ovs_fatal(0, "\"increment\" command requires 1 argument");
b54e22e9 1835 }
94fbe1aa
BP
1836
1837 s = idltest_find_simple(idl, atoi(arg1));
1838 if (!s) {
1839 ovs_fatal(0, "\"set\" command asks for nonexistent "
1840 "i=%d", atoi(arg1));
1841 }
1842
1843 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i);
b54e22e9 1844 increment = true;
2096903b
BP
1845 } else if (!strcmp(name, "abort")) {
1846 ovsdb_idl_txn_abort(txn);
1847 break;
1848 } else if (!strcmp(name, "destroy")) {
1849 printf("%03d: destroy\n", step);
1850 ovsdb_idl_txn_destroy(txn);
1851 return;
475281c0
BP
1852 } else {
1853 ovs_fatal(0, "unknown command %s", name);
1854 }
1855 }
1856
af96ccd2 1857 status = ovsdb_idl_txn_commit_block(txn);
b54e22e9 1858 printf("%03d: commit, status=%s",
475281c0 1859 step, ovsdb_idl_txn_status_to_string(status));
b54e22e9
BP
1860 if (increment) {
1861 printf(", increment=%"PRId64,
1862 ovsdb_idl_txn_get_increment_new_value(txn));
1863 }
1864 putchar('\n');
475281c0
BP
1865 ovsdb_idl_txn_destroy(txn);
1866}
1867
c3bb4bd7
BP
1868static void
1869do_idl(int argc, char *argv[])
1870{
1871 struct jsonrpc *rpc;
1872 struct ovsdb_idl *idl;
1873 unsigned int seqno = 0;
1874 struct ovsdb_symbol_table *symtab;
1875 size_t n_uuids = 0;
1876 int step = 0;
1877 int error;
1878 int i;
1879
bd76d25d
BP
1880 idltest_init();
1881
fba6bd1d 1882 idl = ovsdb_idl_create(argv[1], &idltest_idl_class, true, true);
c3bb4bd7
BP
1883 if (argc > 2) {
1884 struct stream *stream;
1885
f125905c
MM
1886 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
1887 DSCP_DEFAULT), &stream);
c3bb4bd7
BP
1888 if (error) {
1889 ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
1890 }
1891 rpc = jsonrpc_open(stream);
1892 } else {
1893 rpc = NULL;
1894 }
1895
76a6e630 1896 setvbuf(stdout, NULL, _IONBF, 0);
0d0f05b9 1897
c3bb4bd7
BP
1898 symtab = ovsdb_symbol_table_create();
1899 for (i = 2; i < argc; i++) {
0d0f05b9 1900 char *arg = argv[i];
c3bb4bd7 1901 struct jsonrpc_msg *request, *reply;
c3bb4bd7 1902
0d0f05b9
BP
1903 if (*arg == '+') {
1904 /* The previous transaction didn't change anything. */
1905 arg++;
1906 } else {
4ea21243 1907 /* Wait for update. */
854a94d9
BP
1908 for (;;) {
1909 ovsdb_idl_run(idl);
1910 if (ovsdb_idl_get_seqno(idl) != seqno) {
1911 break;
1912 }
4ea21243
BP
1913 jsonrpc_run(rpc);
1914
1915 ovsdb_idl_wait(idl);
1916 jsonrpc_wait(rpc);
1917 poll_block();
1918 }
1919
1920 /* Print update. */
1921 print_idl(idl, step++);
0d0f05b9 1922 }
4ea21243 1923 seqno = ovsdb_idl_get_seqno(idl);
c3bb4bd7 1924
0d0f05b9 1925 if (!strcmp(arg, "reconnect")) {
c3bb4bd7
BP
1926 printf("%03d: reconnect\n", step++);
1927 ovsdb_idl_force_reconnect(idl);
0d0f05b9
BP
1928 } else if (arg[0] != '[') {
1929 idl_set(idl, arg, step++);
c3bb4bd7 1930 } else {
0d0f05b9 1931 struct json *json = parse_json(arg);
c3bb4bd7
BP
1932 substitute_uuids(json, symtab);
1933 request = jsonrpc_create_request("transact", json, NULL);
1934 error = jsonrpc_transact_block(rpc, request, &reply);
d35f8e72 1935 if (error || reply->error) {
c3bb4bd7
BP
1936 ovs_fatal(error, "jsonrpc transaction failed");
1937 }
1938 printf("%03d: ", step++);
1939 if (reply->result) {
1940 parse_uuids(reply->result, symtab, &n_uuids);
1941 }
1942 json_destroy(reply->id);
1943 reply->id = NULL;
1944 print_and_free_json(jsonrpc_msg_to_json(reply));
1945 }
1946 }
1947 ovsdb_symbol_table_destroy(symtab);
1948
1949 if (rpc) {
1950 jsonrpc_close(rpc);
1951 }
854a94d9
BP
1952 for (;;) {
1953 ovsdb_idl_run(idl);
1954 if (ovsdb_idl_get_seqno(idl) != seqno) {
1955 break;
1956 }
4ea21243
BP
1957 ovsdb_idl_wait(idl);
1958 poll_block();
1959 }
1960 print_idl(idl, step++);
c3bb4bd7
BP
1961 ovsdb_idl_destroy(idl);
1962 printf("%03d: done\n", step);
1963}
1964
5f383751 1965static struct ovs_cmdl_command all_commands[] = {
451de37e
AW
1966 { "log-io", NULL, 2, INT_MAX, do_log_io },
1967 { "default-atoms", NULL, 0, 0, do_default_atoms },
1968 { "default-data", NULL, 0, 0, do_default_data },
1969 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
1970 { "parse-base-type", NULL, 1, 1, do_parse_base_type },
1971 { "parse-type", NULL, 1, 1, do_parse_type },
1972 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
1973 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
1974 { "parse-data", NULL, 2, INT_MAX, do_parse_data },
1975 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
1976 { "sort-atoms", NULL, 2, 2, do_sort_atoms },
1977 { "parse-column", NULL, 2, 2, do_parse_column },
1978 { "parse-table", NULL, 2, 3, do_parse_table },
1979 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
1980 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
1981 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
1982 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
1983 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
1984 { "execute-mutations", NULL, 3, 3, do_execute_mutations },
1985 { "query", NULL, 3, 3, do_query },
1986 { "query-distinct", NULL, 4, 4, do_query_distinct },
1987 { "transact", NULL, 1, INT_MAX, do_transact },
1988 { "parse-schema", NULL, 1, 1, do_parse_schema },
1989 { "execute", NULL, 2, INT_MAX, do_execute },
1990 { "trigger", NULL, 2, INT_MAX, do_trigger },
1991 { "idl", NULL, 1, INT_MAX, do_idl },
1992 { "help", NULL, 0, INT_MAX, do_help },
1993 { NULL, NULL, 0, 0, NULL },
f85f8ebb 1994};
d2586fce 1995
5f383751 1996static struct ovs_cmdl_command *
d2586fce
GS
1997get_all_commands(void)
1998{
1999 return all_commands;
2000}