]> git.proxmox.com Git - mirror_ovs.git/blame - tests/test-ovsdb.c
ovn-northd: Sort options in put_dhcp(v6)_opts.
[mirror_ovs.git] / tests / test-ovsdb.c
CommitLineData
f85f8ebb 1/*
de32cec7 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2015, 2016 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
f4248336 26#include "byte-order.h"
f85f8ebb 27#include "command-line.h"
3e8a2ad1 28#include "openvswitch/dynamic-string.h"
ee89ea7b 29#include "openvswitch/json.h"
c3bb4bd7 30#include "jsonrpc.h"
f85f8ebb
BP
31#include "ovsdb-data.h"
32#include "ovsdb-error.h"
c3bb4bd7 33#include "ovsdb-idl.h"
f85f8ebb
BP
34#include "ovsdb-types.h"
35#include "ovsdb/column.h"
36#include "ovsdb/condition.h"
bd06962a 37#include "ovsdb/file.h"
41709ccc 38#include "ovsdb/log.h"
e9f8f936 39#include "ovsdb/mutation.h"
f85f8ebb
BP
40#include "ovsdb/ovsdb.h"
41#include "ovsdb/query.h"
42#include "ovsdb/row.h"
e317253b 43#include "ovsdb/server.h"
f85f8ebb
BP
44#include "ovsdb/table.h"
45#include "ovsdb/transaction.h"
46#include "ovsdb/trigger.h"
47#include "poll-loop.h"
c3bb4bd7 48#include "stream.h"
f85f8ebb 49#include "svec.h"
c3bb4bd7 50#include "tests/idltest.h"
f85f8ebb
BP
51#include "timeval.h"
52#include "util.h"
e6211adc 53#include "openvswitch/vlog.h"
f85f8ebb 54
932104f4
SA
55struct test_ovsdb_pvt_context {
56 bool track;
57};
58
cab50449 59OVS_NO_RETURN static void usage(void);
932104f4
SA
60static void parse_options(int argc, char *argv[],
61 struct test_ovsdb_pvt_context *pvt);
5f383751 62static struct ovs_cmdl_command *get_all_commands(void);
f85f8ebb
BP
63
64int
65main(int argc, char *argv[])
66{
932104f4
SA
67 struct test_ovsdb_pvt_context pvt = {.track = false};
68 struct ovs_cmdl_context ctx = { .argc = 0, .pvt = &pvt};
f85f8ebb 69 set_program_name(argv[0]);
932104f4 70 parse_options(argc, argv, &pvt);
1636c761
RB
71 ctx.argc = argc - optind;
72 ctx.argv = argv + optind;
73 ovs_cmdl_run_command(&ctx, get_all_commands());
f85f8ebb
BP
74 return 0;
75}
76
77static void
932104f4 78parse_options(int argc, char *argv[], struct test_ovsdb_pvt_context *pvt)
f85f8ebb 79{
07fc4ed3 80 static const struct option long_options[] = {
e3c17733
BP
81 {"timeout", required_argument, NULL, 't'},
82 {"verbose", optional_argument, NULL, 'v'},
932104f4 83 {"change-track", optional_argument, NULL, 'c'},
e3c17733
BP
84 {"help", no_argument, NULL, 'h'},
85 {NULL, 0, NULL, 0},
f85f8ebb 86 };
5f383751 87 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
f85f8ebb
BP
88
89 for (;;) {
c3bb4bd7
BP
90 unsigned long int timeout;
91 int c;
92
93 c = getopt_long(argc, argv, short_options, long_options, NULL);
f85f8ebb
BP
94 if (c == -1) {
95 break;
96 }
97
98 switch (c) {
c3bb4bd7
BP
99 case 't':
100 timeout = strtoul(optarg, NULL, 10);
101 if (timeout <= 0) {
102 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
103 optarg);
104 } else {
105 time_alarm(timeout);
106 }
107 break;
108
f85f8ebb
BP
109 case 'h':
110 usage();
111
112 case 'v':
113 vlog_set_verbosity(optarg);
114 break;
115
932104f4
SA
116 case 'c':
117 pvt->track = true;
118 break;
119
f85f8ebb
BP
120 case '?':
121 exit(EXIT_FAILURE);
122
123 default:
124 abort();
125 }
126 }
127 free(short_options);
128}
129
130static void
131usage(void)
132{
133 printf("%s: Open vSwitch database test utility\n"
134 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
41709ccc 135 " log-io FILE FLAGS COMMAND...\n"
f85f8ebb 136 " open FILE with FLAGS, run COMMANDs\n"
958ac03a
BP
137 " default-atoms\n"
138 " test ovsdb_atom_default()\n"
139 " default-data\n"
140 " test ovsdb_datum_default()\n"
f85f8ebb
BP
141 " parse-atomic-type TYPE\n"
142 " parse TYPE as OVSDB atomic type, and re-serialize\n"
bd76d25d
BP
143 " parse-base-type TYPE\n"
144 " parse TYPE as OVSDB base type, and re-serialize\n"
f85f8ebb
BP
145 " parse-type JSON\n"
146 " parse JSON as OVSDB type, and re-serialize\n"
147 " parse-atoms TYPE ATOM...\n"
5c414a2e
BP
148 " parse JSON ATOMs as atoms of TYPE, and re-serialize\n"
149 " parse-atom-strings TYPE ATOM...\n"
150 " parse string ATOMs as atoms of given TYPE, and re-serialize\n"
f85f8ebb 151 " sort-atoms TYPE ATOM...\n"
5c414a2e 152 " print JSON ATOMs in sorted order\n"
f85f8ebb 153 " parse-data TYPE DATUM...\n"
5c414a2e
BP
154 " parse JSON DATUMs as data of given TYPE, and re-serialize\n"
155 " parse-data-strings TYPE DATUM...\n"
156 " parse string DATUMs as data of given TYPE, and re-serialize\n"
f85f8ebb
BP
157 " parse-column NAME OBJECT\n"
158 " parse column NAME with info OBJECT, and re-serialize\n"
c5f341ab 159 " parse-table NAME OBJECT [DEFAULT-IS-ROOT]\n"
f85f8ebb
BP
160 " parse table NAME with info OBJECT\n"
161 " parse-row TABLE ROW..., and re-serialize\n"
162 " parse each ROW of defined TABLE\n"
163 " compare-row TABLE ROW...\n"
164 " mutually compare all of the ROWs, print those that are equal\n"
165 " parse-conditions TABLE CONDITION...\n"
166 " parse each CONDITION on TABLE, and re-serialize\n"
167 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
168 " test CONDITIONS on TABLE against each ROW, print results\n"
ae9cab37
LS
169 " evaluate-conditions-any TABLE [CONDITION,...] [ROW,...]\n"
170 " test CONDITIONS to match any of the CONDITONS on TABLE\n"
171 " against each ROW, print results\n"
172 " compare-conditions TABLE [CONDITION,...]\n"
173 " mutually compare all of the CONDITION, print results for\n"
174 " each pair\n"
e9f8f936
BP
175 " parse-mutations TABLE MUTATION...\n"
176 " parse each MUTATION on TABLE, and re-serialize\n"
177 " execute-mutations TABLE [MUTATION,...] [ROW,...]\n"
178 " execute MUTATIONS on TABLE on each ROW, print results\n"
f85f8ebb
BP
179 " query TABLE [ROW,...] [CONDITION,...]\n"
180 " add each ROW to TABLE, then query and print the rows that\n"
181 " satisfy each CONDITION.\n"
182 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
183 " add each ROW to TABLE, then query and print the rows that\n"
184 " satisfy each CONDITION and have distinct COLUMNS.\n"
0d0f05b9
BP
185 " parse-schema JSON\n"
186 " parse JSON as an OVSDB schema, and re-serialize\n"
f85f8ebb
BP
187 " transact COMMAND\n"
188 " execute each specified transactional COMMAND:\n"
189 " commit\n"
190 " abort\n"
191 " insert UUID I J\n"
192 " delete UUID\n"
193 " modify UUID I J\n"
194 " print\n"
195 " execute SCHEMA TRANSACTION...\n"
196 " executes each TRANSACTION on an initially empty database\n"
197 " the specified SCHEMA\n"
e51879e9
AZ
198 " execute-readonly SCHEMA TRANSACTION...\n"
199 " same as execute, except the TRANSACTION will be executed\n"
200 " against the database server that is in read only mode\n"
f85f8ebb
BP
201 " trigger SCHEMA TRANSACTION...\n"
202 " executes each TRANSACTION on an initially empty database\n"
203 " the specified SCHEMA. A TRANSACTION of the form\n"
204 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
c3bb4bd7
BP
205 " simulated time, for causing triggers to time out.\n"
206 " idl SERVER [TRANSACTION...]\n"
207 " connect to SERVER and dump the contents of the database\n"
208 " as seen initially by the IDL implementation and after\n"
209 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
7251075c
EA
210 " the database or this command will hang.)\n"
211 " idl-partial-update-map-column SERVER \n"
212 " connect to SERVER and executes different operations to\n"
213 " test the capacity of updating elements inside a map column\n"
f1ab6e06
RM
214 " displaying the table information after each operation.\n"
215 " idl-partial-update-set-column SERVER \n"
216 " connect to SERVER and executes different operations to\n"
217 " test the capacity of updating elements inside a set column\n"
7251075c 218 " displaying the table information after each operation.\n",
f85f8ebb
BP
219 program_name, program_name);
220 vlog_usage();
221 printf("\nOther options:\n"
c3bb4bd7 222 " -t, --timeout=SECS give up after SECS seconds\n"
932104f4
SA
223 " -h, --help display this help message\n"
224 " -c, --change-track used with the 'idl' command to\n"
225 " enable tracking of IDL changes\n");
f85f8ebb
BP
226 exit(EXIT_SUCCESS);
227}
228\f
229/* Command helper functions. */
230
231static struct json *
232parse_json(const char *s)
233{
234 struct json *json = json_from_string(s);
235 if (json->type == JSON_STRING) {
236 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
237 }
238 return json;
239}
240
241static struct json *
242unbox_json(struct json *json)
243{
244 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
245 struct json *inner = json->u.array.elems[0];
246 json->u.array.elems[0] = NULL;
247 json_destroy(json);
248 return inner;
249 } else {
250 return json;
251 }
252}
253
7bd02255 254static void
f85f8ebb
BP
255print_and_free_json(struct json *json)
256{
257 char *string = json_to_string(json, JSSF_SORT);
258 json_destroy(json);
259 puts(string);
260 free(string);
261}
262
e9f8f936
BP
263static void
264print_and_free_ovsdb_error(struct ovsdb_error *error)
265{
266 char *string = ovsdb_error_to_string(error);
267 ovsdb_error_destroy(error);
268 puts(string);
269 free(string);
270}
271
f85f8ebb
BP
272static void
273check_ovsdb_error(struct ovsdb_error *error)
274{
275 if (error) {
93ff0290
BP
276 char *s = ovsdb_error_to_string(error);
277 ovsdb_error_destroy(error);
278 ovs_fatal(0, "%s", s);
f85f8ebb
BP
279 }
280}
5c414a2e
BP
281
282static void
283die_if_error(char *error)
284{
285 if (error) {
286 ovs_fatal(0, "%s", error);
287 }
288}
f85f8ebb
BP
289\f
290/* Command implementations. */
291
292static void
1636c761 293do_log_io(struct ovs_cmdl_context *ctx)
f85f8ebb 294{
1636c761
RB
295 const char *name = ctx->argv[1];
296 char *mode_string = ctx->argv[2];
f85f8ebb
BP
297
298 struct ovsdb_error *error;
7446f148 299 enum ovsdb_log_open_mode mode;
41709ccc 300 struct ovsdb_log *log;
f85f8ebb
BP
301 int i;
302
7446f148
BP
303 if (!strcmp(mode_string, "read-only")) {
304 mode = OVSDB_LOG_READ_ONLY;
305 } else if (!strcmp(mode_string, "read/write")) {
306 mode = OVSDB_LOG_READ_WRITE;
307 } else if (!strcmp(mode_string, "create")) {
308 mode = OVSDB_LOG_CREATE;
309 } else {
310 ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
f85f8ebb
BP
311 }
312
7446f148 313 check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
f85f8ebb
BP
314 printf("%s: open successful\n", name);
315
1636c761
RB
316 for (i = 3; i < ctx->argc; i++) {
317 const char *command = ctx->argv[i];
f85f8ebb
BP
318 if (!strcmp(command, "read")) {
319 struct json *json;
320
41709ccc 321 error = ovsdb_log_read(log, &json);
f85f8ebb
BP
322 if (!error) {
323 printf("%s: read: ", name);
324 if (json) {
325 print_and_free_json(json);
326 } else {
41709ccc 327 printf("end of log\n");
f85f8ebb
BP
328 }
329 continue;
330 }
331 } else if (!strncmp(command, "write:", 6)) {
332 struct json *json = parse_json(command + 6);
41709ccc 333 error = ovsdb_log_write(log, json);
f85f8ebb
BP
334 json_destroy(json);
335 } else if (!strcmp(command, "commit")) {
41709ccc 336 error = ovsdb_log_commit(log);
f85f8ebb 337 } else {
41709ccc 338 ovs_fatal(0, "unknown log-io command \"%s\"", command);
f85f8ebb
BP
339 }
340 if (error) {
341 char *s = ovsdb_error_to_string(error);
342 printf("%s: %s failed: %s\n", name, command, s);
343 free(s);
93ff0290 344 ovsdb_error_destroy(error);
f85f8ebb
BP
345 } else {
346 printf("%s: %s successful\n", name, command);
347 }
348 }
349
41709ccc 350 ovsdb_log_close(log);
f85f8ebb
BP
351}
352
958ac03a 353static void
1636c761 354do_default_atoms(struct ovs_cmdl_context *ctx OVS_UNUSED)
958ac03a
BP
355{
356 int type;
357
358 for (type = 0; type < OVSDB_N_TYPES; type++) {
359 union ovsdb_atom atom;
360
361 if (type == OVSDB_TYPE_VOID) {
362 continue;
363 }
364
365 printf("%s: ", ovsdb_atomic_type_to_string(type));
366
367 ovsdb_atom_init_default(&atom, type);
368 if (!ovsdb_atom_equals(&atom, ovsdb_atom_default(type), type)) {
369 printf("wrong\n");
370 exit(1);
371 }
372 ovsdb_atom_destroy(&atom, type);
373
374 printf("OK\n");
375 }
376}
377
378static void
1636c761 379do_default_data(struct ovs_cmdl_context *ctx OVS_UNUSED)
958ac03a
BP
380{
381 unsigned int n_min;
382 int key, value;
383
384 for (n_min = 0; n_min <= 1; n_min++) {
385 for (key = 0; key < OVSDB_N_TYPES; key++) {
386 if (key == OVSDB_TYPE_VOID) {
387 continue;
388 }
389 for (value = 0; value < OVSDB_N_TYPES; value++) {
390 struct ovsdb_datum datum;
391 struct ovsdb_type type;
392
393 ovsdb_base_type_init(&type.key, key);
394 ovsdb_base_type_init(&type.value, value);
395 type.n_min = n_min;
396 type.n_max = 1;
397 assert(ovsdb_type_is_valid(&type));
398
399 printf("key %s, value %s, n_min %u: ",
400 ovsdb_atomic_type_to_string(key),
401 ovsdb_atomic_type_to_string(value), n_min);
402
403 ovsdb_datum_init_default(&datum, &type);
404 if (!ovsdb_datum_equals(&datum, ovsdb_datum_default(&type),
405 &type)) {
406 printf("wrong\n");
407 exit(1);
408 }
409 ovsdb_datum_destroy(&datum, &type);
410 ovsdb_type_destroy(&type);
411
412 printf("OK\n");
413 }
414 }
415 }
416}
417
1a6f1cef
AZ
418static void
419do_diff_data(struct ovs_cmdl_context *ctx)
420{
421 struct ovsdb_type type;
422 struct json *json;
423 struct ovsdb_datum new, old, diff, reincarnation;
424
425 json = unbox_json(parse_json(ctx->argv[1]));
426 check_ovsdb_error(ovsdb_type_from_json(&type, json));
427 json_destroy(json);
428
429 /* Arguments in pairs of 'old' and 'new'. */
430 for (int i = 2; i < ctx->argc - 1; i+=2) {
431 struct ovsdb_error *error;
432
433 json = unbox_json(parse_json(ctx->argv[i]));
434 check_ovsdb_error(ovsdb_datum_from_json(&old, &type, json, NULL));
435 json_destroy(json);
436
437 json = unbox_json(parse_json(ctx->argv[i+1]));
438 check_ovsdb_error(ovsdb_transient_datum_from_json(&new, &type, json));
439 json_destroy(json);
440
441 /* Generate the diff. */
442 ovsdb_datum_diff(&diff, &old, &new, &type);
443
444 /* Apply diff to 'old' to create'reincarnation'. */
445 error = ovsdb_datum_apply_diff(&reincarnation, &old, &diff, &type);
446 if (error) {
cf2bc30c
WT
447 char *string = ovsdb_error_to_string(error);
448 ovsdb_error_destroy(error);
449 ovs_fatal(0, "%s", string);
1a6f1cef
AZ
450 }
451
452 /* Test to make sure 'new' equals 'reincarnation'. */
453 if (!ovsdb_datum_equals(&new, &reincarnation, &type)) {
454 ovs_fatal(0, "failed to apply diff");
455 }
456
457 /* Print diff */
458 json = ovsdb_datum_to_json(&diff, &type);
459 printf ("diff: ");
460 print_and_free_json(json);
461
462 /* Print reincarnation */
463 json = ovsdb_datum_to_json(&reincarnation, &type);
464 printf ("apply diff: ");
465 print_and_free_json(json);
466
467 ovsdb_datum_destroy(&new, &type);
468 ovsdb_datum_destroy(&old, &type);
469 ovsdb_datum_destroy(&diff, &type);
470 ovsdb_datum_destroy(&reincarnation, &type);
471
472 printf("OK\n");
473 }
474
475 ovsdb_type_destroy(&type);
476}
477
f85f8ebb 478static void
1636c761 479do_parse_atomic_type(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
480{
481 enum ovsdb_atomic_type type;
482 struct json *json;
483
1636c761 484 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
485 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
486 json_destroy(json);
487 print_and_free_json(ovsdb_atomic_type_to_json(type));
488}
489
bd76d25d 490static void
1636c761 491do_parse_base_type(struct ovs_cmdl_context *ctx)
bd76d25d
BP
492{
493 struct ovsdb_base_type base;
494 struct json *json;
495
1636c761 496 json = unbox_json(parse_json(ctx->argv[1]));
bd76d25d
BP
497 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
498 json_destroy(json);
499 print_and_free_json(ovsdb_base_type_to_json(&base));
500 ovsdb_base_type_destroy(&base);
501}
502
f85f8ebb 503static void
1636c761 504do_parse_type(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
505{
506 struct ovsdb_type type;
507 struct json *json;
508
1636c761 509 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
510 check_ovsdb_error(ovsdb_type_from_json(&type, json));
511 json_destroy(json);
512 print_and_free_json(ovsdb_type_to_json(&type));
bd76d25d 513 ovsdb_type_destroy(&type);
f85f8ebb
BP
514}
515
516static void
1636c761 517do_parse_atoms(struct ovs_cmdl_context *ctx)
f85f8ebb 518{
bd76d25d 519 struct ovsdb_base_type base;
f85f8ebb
BP
520 struct json *json;
521 int i;
522
1636c761 523 json = unbox_json(parse_json(ctx->argv[1]));
bd76d25d 524 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
f85f8ebb
BP
525 json_destroy(json);
526
1636c761 527 for (i = 2; i < ctx->argc; i++) {
bd76d25d 528 struct ovsdb_error *error;
f85f8ebb
BP
529 union ovsdb_atom atom;
530
1636c761 531 json = unbox_json(parse_json(ctx->argv[i]));
bd76d25d 532 error = ovsdb_atom_from_json(&atom, &base, json, NULL);
f85f8ebb
BP
533 json_destroy(json);
534
bd76d25d
BP
535 if (error) {
536 print_and_free_ovsdb_error(error);
537 } else {
7bd02255 538 print_and_free_json(ovsdb_atom_to_json(&atom, base.type));
bd76d25d
BP
539 ovsdb_atom_destroy(&atom, base.type);
540 }
f85f8ebb 541 }
bd76d25d 542 ovsdb_base_type_destroy(&base);
f85f8ebb
BP
543}
544
5c414a2e 545static void
1636c761 546do_parse_atom_strings(struct ovs_cmdl_context *ctx)
5c414a2e 547{
bd76d25d 548 struct ovsdb_base_type base;
5c414a2e
BP
549 struct json *json;
550 int i;
551
1636c761 552 json = unbox_json(parse_json(ctx->argv[1]));
bd76d25d 553 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
5c414a2e
BP
554 json_destroy(json);
555
1636c761 556 for (i = 2; i < ctx->argc; i++) {
5c414a2e
BP
557 union ovsdb_atom atom;
558 struct ds out;
559
1636c761 560 die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL));
5c414a2e
BP
561
562 ds_init(&out);
bd76d25d 563 ovsdb_atom_to_string(&atom, base.type, &out);
5c414a2e
BP
564 puts(ds_cstr(&out));
565 ds_destroy(&out);
566
bd76d25d 567 ovsdb_atom_destroy(&atom, base.type);
5c414a2e 568 }
bd76d25d 569 ovsdb_base_type_destroy(&base);
5c414a2e
BP
570}
571
f85f8ebb 572static void
2b66469b
BP
573do_parse_data__(int argc, char *argv[],
574 struct ovsdb_error *
575 (*parse)(struct ovsdb_datum *datum,
576 const struct ovsdb_type *type,
577 const struct json *json,
578 struct ovsdb_symbol_table *symtab))
f85f8ebb
BP
579{
580 struct ovsdb_type type;
581 struct json *json;
582 int i;
583
584 json = unbox_json(parse_json(argv[1]));
585 check_ovsdb_error(ovsdb_type_from_json(&type, json));
586 json_destroy(json);
587
588 for (i = 2; i < argc; i++) {
589 struct ovsdb_datum datum;
590
591 json = unbox_json(parse_json(argv[i]));
2b66469b 592 check_ovsdb_error(parse(&datum, &type, json, NULL));
f85f8ebb
BP
593 json_destroy(json);
594
7bd02255 595 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
f85f8ebb
BP
596
597 ovsdb_datum_destroy(&datum, &type);
598 }
bd76d25d 599 ovsdb_type_destroy(&type);
f85f8ebb
BP
600}
601
2b66469b 602static void
1636c761 603do_parse_data(struct ovs_cmdl_context *ctx)
2b66469b 604{
1636c761 605 do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json);
2b66469b
BP
606}
607
5c414a2e 608static void
1636c761 609do_parse_data_strings(struct ovs_cmdl_context *ctx)
5c414a2e
BP
610{
611 struct ovsdb_type type;
612 struct json *json;
613 int i;
614
1636c761 615 json = unbox_json(parse_json(ctx->argv[1]));
5c414a2e
BP
616 check_ovsdb_error(ovsdb_type_from_json(&type, json));
617 json_destroy(json);
618
1636c761 619 for (i = 2; i < ctx->argc; i++) {
5c414a2e
BP
620 struct ovsdb_datum datum;
621 struct ds out;
622
1636c761 623 die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL));
5c414a2e
BP
624
625 ds_init(&out);
626 ovsdb_datum_to_string(&datum, &type, &out);
627 puts(ds_cstr(&out));
628 ds_destroy(&out);
629
630 ovsdb_datum_destroy(&datum, &type);
631 }
bd76d25d 632 ovsdb_type_destroy(&type);
5c414a2e
BP
633}
634
f85f8ebb
BP
635static enum ovsdb_atomic_type compare_atoms_atomic_type;
636
637static int
638compare_atoms(const void *a_, const void *b_)
639{
640 const union ovsdb_atom *a = a_;
641 const union ovsdb_atom *b = b_;
642
643 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
644}
645
646static void
1636c761 647do_sort_atoms(struct ovs_cmdl_context *ctx)
f85f8ebb 648{
bd76d25d 649 struct ovsdb_base_type base;
f85f8ebb
BP
650 union ovsdb_atom *atoms;
651 struct json *json, **json_atoms;
652 size_t n_atoms;
653 int i;
654
1636c761 655 json = unbox_json(parse_json(ctx->argv[1]));
bd76d25d 656 check_ovsdb_error(ovsdb_base_type_from_json(&base, json));
f85f8ebb
BP
657 json_destroy(json);
658
1636c761 659 json = unbox_json(parse_json(ctx->argv[2]));
f85f8ebb
BP
660 if (json->type != JSON_ARRAY) {
661 ovs_fatal(0, "second argument must be array");
662 }
663
664 /* Convert JSON atoms to internal representation. */
665 n_atoms = json->u.array.n;
666 atoms = xmalloc(n_atoms * sizeof *atoms);
667 for (i = 0; i < n_atoms; i++) {
bd76d25d 668 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], &base,
f85f8ebb
BP
669 json->u.array.elems[i], NULL));
670 }
671 json_destroy(json);
672
673 /* Sort atoms. */
bd76d25d 674 compare_atoms_atomic_type = base.type;
f85f8ebb
BP
675 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
676
677 /* Convert internal representation back to JSON. */
678 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
679 for (i = 0; i < n_atoms; i++) {
bd76d25d
BP
680 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], base.type);
681 ovsdb_atom_destroy(&atoms[i], base.type);
f85f8ebb
BP
682 }
683 print_and_free_json(json_array_create(json_atoms, n_atoms));
93ff0290 684 free(atoms);
bd76d25d 685 ovsdb_base_type_destroy(&base);
f85f8ebb
BP
686}
687
688static void
1636c761 689do_parse_column(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
690{
691 struct ovsdb_column *column;
692 struct json *json;
693
1636c761
RB
694 json = parse_json(ctx->argv[2]);
695 check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column));
f85f8ebb
BP
696 json_destroy(json);
697 print_and_free_json(ovsdb_column_to_json(column));
698 ovsdb_column_destroy(column);
699}
700
701static void
1636c761 702do_parse_table(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
703{
704 struct ovsdb_table_schema *ts;
c5f341ab 705 bool default_is_root;
f85f8ebb
BP
706 struct json *json;
707
1636c761 708 default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true");
c5f341ab 709
1636c761
RB
710 json = parse_json(ctx->argv[2]);
711 check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts));
f85f8ebb 712 json_destroy(json);
c5f341ab 713 print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root));
f85f8ebb
BP
714 ovsdb_table_schema_destroy(ts);
715}
716
717static void
1636c761 718do_parse_rows(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
719{
720 struct ovsdb_column_set all_columns;
721 struct ovsdb_table_schema *ts;
722 struct ovsdb_table *table;
723 struct json *json;
724 int i;
725
1636c761 726 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
727 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
728 json_destroy(json);
729
730 table = ovsdb_table_create(ts);
731 ovsdb_column_set_init(&all_columns);
732 ovsdb_column_set_add_all(&all_columns, table);
733
1636c761 734 for (i = 2; i < ctx->argc; i++) {
f85f8ebb
BP
735 struct ovsdb_column_set columns;
736 struct ovsdb_row *row;
737
738 ovsdb_column_set_init(&columns);
739 row = ovsdb_row_create(table);
740
1636c761 741 json = unbox_json(parse_json(ctx->argv[i]));
f85f8ebb
BP
742 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
743 json_destroy(json);
744
745 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
746
747 if (columns.n_columns) {
748 struct svec names;
749 size_t j;
750 char *s;
751
752 svec_init(&names);
753 for (j = 0; j < columns.n_columns; j++) {
754 svec_add(&names, columns.columns[j]->name);
755 }
756 svec_sort(&names);
757 s = svec_join(&names, ", ", "");
758 puts(s);
759 free(s);
760 svec_destroy(&names);
761 } else {
762 printf("<none>\n");
763 }
764
765 ovsdb_column_set_destroy(&columns);
766 ovsdb_row_destroy(row);
767 }
768
769 ovsdb_column_set_destroy(&all_columns);
770 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
771}
772
773static void
1636c761 774do_compare_rows(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
775{
776 struct ovsdb_column_set all_columns;
777 struct ovsdb_table_schema *ts;
778 struct ovsdb_table *table;
779 struct ovsdb_row **rows;
780 struct json *json;
781 char **names;
782 int n_rows;
783 int i, j;
784
1636c761 785 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
786 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
787 json_destroy(json);
788
789 table = ovsdb_table_create(ts);
790 ovsdb_column_set_init(&all_columns);
791 ovsdb_column_set_add_all(&all_columns, table);
792
1636c761 793 n_rows = ctx->argc - 2;
f85f8ebb
BP
794 rows = xmalloc(sizeof *rows * n_rows);
795 names = xmalloc(sizeof *names * n_rows);
796 for (i = 0; i < n_rows; i++) {
797 rows[i] = ovsdb_row_create(table);
798
1636c761 799 json = parse_json(ctx->argv[i + 2]);
f85f8ebb
BP
800 if (json->type != JSON_ARRAY || json->u.array.n != 2
801 || json->u.array.elems[0]->type != JSON_STRING) {
802 ovs_fatal(0, "\"%s\" does not have expected form "
1636c761 803 "[\"name\", {data}]", ctx->argv[i]);
f85f8ebb
BP
804 }
805 names[i] = xstrdup(json->u.array.elems[0]->u.string);
806 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
807 NULL, NULL));
808 json_destroy(json);
809 }
810 for (i = 0; i < n_rows; i++) {
811 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
812 for (j = i + 1; j < n_rows; j++) {
813 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
814 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
815 printf("%s == %s\n", names[i], names[j]);
816 if (i_hash != j_hash) {
817 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
818 abort();
819 }
820 } else if (i_hash == j_hash) {
821 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
822 }
823 }
824 }
93ff0290
BP
825 for (i = 0; i < n_rows; i++) {
826 ovsdb_row_destroy(rows[i]);
827 free(names[i]);
828 }
f85f8ebb
BP
829 free(rows);
830 free(names);
831
832 ovsdb_column_set_destroy(&all_columns);
833 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
834}
835
836static void
1636c761 837do_parse_conditions(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
838{
839 struct ovsdb_table_schema *ts;
840 struct json *json;
841 int exit_code = 0;
842 int i;
843
1636c761 844 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
845 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
846 json_destroy(json);
847
1636c761 848 for (i = 2; i < ctx->argc; i++) {
f85f8ebb
BP
849 struct ovsdb_condition cnd;
850 struct ovsdb_error *error;
851
1636c761 852 json = parse_json(ctx->argv[i]);
f85f8ebb
BP
853 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
854 if (!error) {
855 print_and_free_json(ovsdb_condition_to_json(&cnd));
856 } else {
857 char *s = ovsdb_error_to_string(error);
858 ovs_error(0, "%s", s);
859 free(s);
860 ovsdb_error_destroy(error);
861 exit_code = 1;
862 }
863 json_destroy(json);
864
865 ovsdb_condition_destroy(&cnd);
866 }
867 ovsdb_table_schema_destroy(ts);
868
869 exit(exit_code);
870}
871
ae9cab37
LS
872#define OVSDB_CONDITION_EVERY 0
873#define OVSDB_CONDITION_ANY 1
874
f85f8ebb 875static void
ae9cab37 876do_evaluate_condition__(struct ovs_cmdl_context *ctx, int mode)
f85f8ebb
BP
877{
878 struct ovsdb_table_schema *ts;
879 struct ovsdb_table *table;
880 struct ovsdb_condition *conditions;
881 size_t n_conditions;
882 struct ovsdb_row **rows;
883 size_t n_rows;
884 struct json *json;
885 size_t i, j;
886
887 /* Parse table schema, create table. */
1636c761 888 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
889 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
890 json_destroy(json);
891
892 table = ovsdb_table_create(ts);
893
894 /* Parse conditions. */
1636c761 895 json = parse_json(ctx->argv[2]);
f85f8ebb
BP
896 if (json->type != JSON_ARRAY) {
897 ovs_fatal(0, "CONDITION argument is not JSON array");
898 }
899 n_conditions = json->u.array.n;
900 conditions = xmalloc(n_conditions * sizeof *conditions);
901 for (i = 0; i < n_conditions; i++) {
902 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
903 NULL, &conditions[i]));
904 }
905 json_destroy(json);
906
907 /* Parse rows. */
1636c761 908 json = parse_json(ctx->argv[3]);
f85f8ebb
BP
909 if (json->type != JSON_ARRAY) {
910 ovs_fatal(0, "ROW argument is not JSON array");
911 }
912 n_rows = json->u.array.n;
913 rows = xmalloc(n_rows * sizeof *rows);
914 for (i = 0; i < n_rows; i++) {
915 rows[i] = ovsdb_row_create(table);
916 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
917 NULL, NULL));
918 }
919 json_destroy(json);
920
921 for (i = 0; i < n_conditions; i++) {
34582733 922 printf("condition %2"PRIuSIZE":", i);
f85f8ebb 923 for (j = 0; j < n_rows; j++) {
ae9cab37
LS
924 bool result;
925 if (mode == OVSDB_CONDITION_EVERY) {
926 result = ovsdb_condition_match_every_clause(rows[j],
927 &conditions[i]);
928 } else {
929 result = ovsdb_condition_match_any_clause(rows[j]->fields,
930 &conditions[i],
931 NULL);
932 }
f85f8ebb
BP
933 if (j % 5 == 0) {
934 putchar(' ');
935 }
936 putchar(result ? 'T' : '-');
937 }
938 printf("\n");
939 }
940
941 for (i = 0; i < n_conditions; i++) {
942 ovsdb_condition_destroy(&conditions[i]);
943 }
93ff0290 944 free(conditions);
f85f8ebb
BP
945 for (i = 0; i < n_rows; i++) {
946 ovsdb_row_destroy(rows[i]);
947 }
93ff0290 948 free(rows);
f85f8ebb
BP
949 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
950}
951
ae9cab37
LS
952static void
953do_evaluate_conditions(struct ovs_cmdl_context *ctx)
954{
955 do_evaluate_condition__(ctx, OVSDB_CONDITION_EVERY);
956}
957
958static void
959do_evaluate_conditions_any(struct ovs_cmdl_context *ctx)
960{
961 do_evaluate_condition__(ctx, OVSDB_CONDITION_ANY);
962}
963
964static void
965do_compare_conditions(struct ovs_cmdl_context *ctx)
966{
967 struct ovsdb_table_schema *ts;
968 struct ovsdb_table *table;
969 struct ovsdb_condition *conditions;
970 size_t n_conditions;
971 struct json *json;
972 size_t i;
973
974 /* Parse table schema, create table. */
975 json = unbox_json(parse_json(ctx->argv[1]));
976 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
977 json_destroy(json);
978
979 table = ovsdb_table_create(ts);
980
981 /* Parse conditions. */
982 json = parse_json(ctx->argv[2]);
983 if (json->type != JSON_ARRAY) {
984 ovs_fatal(0, "CONDITION argument is not JSON array");
985 }
986 n_conditions = json->u.array.n;
987 conditions = xmalloc(n_conditions * sizeof *conditions);
988
989 for (i = 0; i < n_conditions; i++) {
990 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
991 NULL, &conditions[i]));
992 }
993 json_destroy(json);
994
995 for (i = 0; i < n_conditions - 1; i++) {
996 int res = ovsdb_condition_cmp_3way(&conditions[i], &conditions[i + 1]);
997 printf("condition %"PRIuSIZE"-%"PRIuSIZE": %d\n", i, i + 1, res);
998 }
999
1000 for (i = 0; i < n_conditions; i++) {
1001 ovsdb_condition_destroy(&conditions[i]);
1002 }
1003 free(conditions);
1004 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1005}
1006
e9f8f936 1007static void
1636c761 1008do_parse_mutations(struct ovs_cmdl_context *ctx)
e9f8f936
BP
1009{
1010 struct ovsdb_table_schema *ts;
1011 struct json *json;
1012 int exit_code = 0;
1013 int i;
1014
1636c761 1015 json = unbox_json(parse_json(ctx->argv[1]));
e9f8f936
BP
1016 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1017 json_destroy(json);
1018
1636c761 1019 for (i = 2; i < ctx->argc; i++) {
e9f8f936
BP
1020 struct ovsdb_mutation_set set;
1021 struct ovsdb_error *error;
1022
1636c761 1023 json = parse_json(ctx->argv[i]);
e9f8f936
BP
1024 error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
1025 if (!error) {
1026 print_and_free_json(ovsdb_mutation_set_to_json(&set));
1027 } else {
1028 char *s = ovsdb_error_to_string(error);
1029 ovs_error(0, "%s", s);
1030 free(s);
1031 ovsdb_error_destroy(error);
1032 exit_code = 1;
1033 }
1034 json_destroy(json);
1035
1036 ovsdb_mutation_set_destroy(&set);
1037 }
1038 ovsdb_table_schema_destroy(ts);
1039
1040 exit(exit_code);
1041}
1042
1043static void
1636c761 1044do_execute_mutations(struct ovs_cmdl_context *ctx)
e9f8f936
BP
1045{
1046 struct ovsdb_table_schema *ts;
1047 struct ovsdb_table *table;
1048 struct ovsdb_mutation_set *sets;
1049 size_t n_sets;
1050 struct ovsdb_row **rows;
1051 size_t n_rows;
1052 struct json *json;
1053 size_t i, j;
1054
1055 /* Parse table schema, create table. */
1636c761 1056 json = unbox_json(parse_json(ctx->argv[1]));
e9f8f936
BP
1057 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1058 json_destroy(json);
1059
1060 table = ovsdb_table_create(ts);
1061
1062 /* Parse mutations. */
1636c761 1063 json = parse_json(ctx->argv[2]);
e9f8f936
BP
1064 if (json->type != JSON_ARRAY) {
1065 ovs_fatal(0, "MUTATION argument is not JSON array");
1066 }
1067 n_sets = json->u.array.n;
1068 sets = xmalloc(n_sets * sizeof *sets);
1069 for (i = 0; i < n_sets; i++) {
1070 check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
1071 json->u.array.elems[i],
1072 NULL, &sets[i]));
1073 }
1074 json_destroy(json);
1075
1076 /* Parse rows. */
1636c761 1077 json = parse_json(ctx->argv[3]);
e9f8f936
BP
1078 if (json->type != JSON_ARRAY) {
1079 ovs_fatal(0, "ROW argument is not JSON array");
1080 }
1081 n_rows = json->u.array.n;
1082 rows = xmalloc(n_rows * sizeof *rows);
1083 for (i = 0; i < n_rows; i++) {
1084 rows[i] = ovsdb_row_create(table);
1085 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
1086 NULL, NULL));
1087 }
1088 json_destroy(json);
1089
1090 for (i = 0; i < n_sets; i++) {
34582733 1091 printf("mutation %2"PRIuSIZE":\n", i);
e9f8f936
BP
1092 for (j = 0; j < n_rows; j++) {
1093 struct ovsdb_error *error;
1094 struct ovsdb_row *row;
1095
1096 row = ovsdb_row_clone(rows[j]);
1097 error = ovsdb_mutation_set_execute(row, &sets[i]);
1098
34582733 1099 printf("row %"PRIuSIZE": ", j);
e9f8f936
BP
1100 if (error) {
1101 print_and_free_ovsdb_error(error);
1102 } else {
1103 struct ovsdb_column_set columns;
1104 struct shash_node *node;
1105
1106 ovsdb_column_set_init(&columns);
1107 SHASH_FOR_EACH (node, &ts->columns) {
1108 struct ovsdb_column *c = node->data;
1109 if (!ovsdb_datum_equals(&row->fields[c->index],
1110 &rows[j]->fields[c->index],
1111 &c->type)) {
1112 ovsdb_column_set_add(&columns, c);
1113 }
1114 }
1115 if (columns.n_columns) {
1116 print_and_free_json(ovsdb_row_to_json(row, &columns));
1117 } else {
1118 printf("no change\n");
1119 }
1120 ovsdb_column_set_destroy(&columns);
1121 }
1122 ovsdb_row_destroy(row);
1123 }
1124 printf("\n");
1125 }
1126
1127 for (i = 0; i < n_sets; i++) {
1128 ovsdb_mutation_set_destroy(&sets[i]);
1129 }
93ff0290 1130 free(sets);
e9f8f936
BP
1131 for (i = 0; i < n_rows; i++) {
1132 ovsdb_row_destroy(rows[i]);
1133 }
93ff0290 1134 free(rows);
e9f8f936
BP
1135 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1136}
1137
29d7226e
BP
1138/* Inserts a row, without bothering to update metadata such as refcounts. */
1139static void
1140put_row(struct ovsdb_table *table, struct ovsdb_row *row)
1141{
1142 const struct uuid *uuid = ovsdb_row_get_uuid(row);
1143 if (!ovsdb_table_get_row(table, uuid)) {
1144 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
1145 }
1146}
1147
f85f8ebb
BP
1148struct do_query_cbdata {
1149 struct uuid *row_uuids;
1150 int *counts;
1151 size_t n_rows;
1152};
1153
1154static bool
1155do_query_cb(const struct ovsdb_row *row, void *cbdata_)
1156{
1157 struct do_query_cbdata *cbdata = cbdata_;
1158 size_t i;
1159
1160 for (i = 0; i < cbdata->n_rows; i++) {
1161 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
1162 cbdata->counts[i]++;
1163 }
1164 }
1165
1166 return true;
1167}
1168
1169static void
1636c761 1170do_query(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
1171{
1172 struct do_query_cbdata cbdata;
1173 struct ovsdb_table_schema *ts;
1174 struct ovsdb_table *table;
1175 struct json *json;
1176 int exit_code = 0;
1177 size_t i;
1178
1179 /* Parse table schema, create table. */
1636c761 1180 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
1181 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1182 json_destroy(json);
1183
1184 table = ovsdb_table_create(ts);
1185
1186 /* Parse rows, add to table. */
1636c761 1187 json = parse_json(ctx->argv[2]);
f85f8ebb
BP
1188 if (json->type != JSON_ARRAY) {
1189 ovs_fatal(0, "ROW argument is not JSON array");
1190 }
1191 cbdata.n_rows = json->u.array.n;
1192 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
1193 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
1194 for (i = 0; i < cbdata.n_rows; i++) {
1195 struct ovsdb_row *row = ovsdb_row_create(table);
1196 uuid_generate(ovsdb_row_get_uuid_rw(row));
1197 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1198 NULL, NULL));
1199 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1200 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1201 UUID_ARGS(ovsdb_row_get_uuid(row)));
1202 }
1203 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
29d7226e 1204 put_row(table, row);
f85f8ebb
BP
1205 }
1206 json_destroy(json);
1207
1208 /* Parse conditions and execute queries. */
1636c761 1209 json = parse_json(ctx->argv[3]);
f85f8ebb
BP
1210 if (json->type != JSON_ARRAY) {
1211 ovs_fatal(0, "CONDITION argument is not JSON array");
1212 }
1213 for (i = 0; i < json->u.array.n; i++) {
1214 struct ovsdb_condition cnd;
1215 size_t j;
1216
1217 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1218 NULL, &cnd));
1219
1220 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
1221 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
1222
34582733 1223 printf("query %2"PRIuSIZE":", i);
f85f8ebb
BP
1224 for (j = 0; j < cbdata.n_rows; j++) {
1225 if (j % 5 == 0) {
1226 putchar(' ');
1227 }
1228 if (cbdata.counts[j]) {
1229 printf("%d", cbdata.counts[j]);
1230 if (cbdata.counts[j] > 1) {
1231 /* Dup! */
1232 exit_code = 1;
1233 }
1234 } else {
1235 putchar('-');
1236 }
1237 }
1238 putchar('\n');
1239
1240 ovsdb_condition_destroy(&cnd);
1241 }
1242 json_destroy(json);
1243
1244 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1245
1246 exit(exit_code);
1247}
1248
1249struct do_query_distinct_class {
1250 struct ovsdb_row *example;
1251 int count;
1252};
1253
1254struct do_query_distinct_row {
1255 struct uuid uuid;
1256 struct do_query_distinct_class *class;
1257};
1258
1259static void
1636c761 1260do_query_distinct(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
1261{
1262 struct ovsdb_column_set columns;
1263 struct ovsdb_table_schema *ts;
1264 struct ovsdb_table *table;
1265 struct do_query_distinct_row *rows;
1266 size_t n_rows;
1267 struct do_query_distinct_class *classes;
1268 size_t n_classes;
1269 struct json *json;
1270 int exit_code = 0;
2a022368 1271 size_t i;
f85f8ebb
BP
1272
1273 /* Parse table schema, create table. */
1636c761 1274 json = unbox_json(parse_json(ctx->argv[1]));
f85f8ebb
BP
1275 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
1276 json_destroy(json);
1277
1278 table = ovsdb_table_create(ts);
1279
1280 /* Parse column set. */
1636c761 1281 json = parse_json(ctx->argv[4]);
1cb29ab0
BP
1282 check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema,
1283 &columns));
f85f8ebb
BP
1284 json_destroy(json);
1285
1286 /* Parse rows, add to table. */
1636c761 1287 json = parse_json(ctx->argv[2]);
f85f8ebb
BP
1288 if (json->type != JSON_ARRAY) {
1289 ovs_fatal(0, "ROW argument is not JSON array");
1290 }
1291 n_rows = json->u.array.n;
1292 rows = xmalloc(n_rows * sizeof *rows);
1293 classes = xmalloc(n_rows * sizeof *classes);
1294 n_classes = 0;
1295 for (i = 0; i < n_rows; i++) {
1296 struct ovsdb_row *row;
1297 size_t j;
1298
1299 /* Parse row. */
1300 row = ovsdb_row_create(table);
1301 uuid_generate(ovsdb_row_get_uuid_rw(row));
1302 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
1303 NULL, NULL));
1304
1305 /* Initialize row and find equivalence class. */
1306 rows[i].uuid = *ovsdb_row_get_uuid(row);
1307 rows[i].class = NULL;
1308 for (j = 0; j < n_classes; j++) {
1309 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
1310 rows[i].class = &classes[j];
1311 break;
1312 }
1313 }
1314 if (!rows[i].class) {
1315 rows[i].class = &classes[n_classes];
1316 classes[n_classes].example = ovsdb_row_clone(row);
1317 n_classes++;
1318 }
1319
1320 /* Add row to table. */
1321 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
1322 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
1323 UUID_ARGS(ovsdb_row_get_uuid(row)));
1324 }
29d7226e 1325 put_row(table, row);
f85f8ebb
BP
1326
1327 }
1328 json_destroy(json);
1329
1330 /* Parse conditions and execute queries. */
1636c761 1331 json = parse_json(ctx->argv[3]);
f85f8ebb
BP
1332 if (json->type != JSON_ARRAY) {
1333 ovs_fatal(0, "CONDITION argument is not JSON array");
1334 }
1335 for (i = 0; i < json->u.array.n; i++) {
1336 struct ovsdb_row_set results;
1337 struct ovsdb_condition cnd;
2a022368 1338 size_t j;
f85f8ebb
BP
1339
1340 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
1341 NULL, &cnd));
1342
1343 for (j = 0; j < n_classes; j++) {
1344 classes[j].count = 0;
1345 }
1346 ovsdb_row_set_init(&results);
1347 ovsdb_query_distinct(table, &cnd, &columns, &results);
1348 for (j = 0; j < results.n_rows; j++) {
2a022368
BP
1349 size_t k;
1350
f85f8ebb
BP
1351 for (k = 0; k < n_rows; k++) {
1352 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
1353 &rows[k].uuid)) {
1354 rows[k].class->count++;
1355 }
1356 }
1357 }
1358 ovsdb_row_set_destroy(&results);
1359
34582733 1360 printf("query %2"PRIuSIZE":", i);
f85f8ebb
BP
1361 for (j = 0; j < n_rows; j++) {
1362 int count = rows[j].class->count;
1363
1364 if (j % 5 == 0) {
1365 putchar(' ');
1366 }
1367 if (count > 1) {
1368 /* Dup! */
1369 printf("%d", count);
1370 exit_code = 1;
1371 } else if (count == 1) {
1372 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
1373 } else {
1374 putchar('-');
1375 }
1376 }
1377 putchar('\n');
1378
1379 ovsdb_condition_destroy(&cnd);
1380 }
1381 json_destroy(json);
1382
9ecd6449
WT
1383 for (i = 0; i < n_classes; i++) {
1384 ovsdb_row_destroy(classes[i].example);
1385 }
1386
f85f8ebb
BP
1387 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
1388
dd7e1cbd
WT
1389 free(rows);
1390 free(classes);
f85f8ebb
BP
1391 exit(exit_code);
1392}
1393
0d0f05b9 1394static void
1636c761 1395do_parse_schema(struct ovs_cmdl_context *ctx)
0d0f05b9
BP
1396{
1397 struct ovsdb_schema *schema;
1398 struct json *json;
1399
1636c761 1400 json = parse_json(ctx->argv[1]);
0d0f05b9
BP
1401 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1402 json_destroy(json);
1403 print_and_free_json(ovsdb_schema_to_json(schema));
1404 ovsdb_schema_destroy(schema);
1405}
1406
f85f8ebb 1407static void
e51879e9 1408do_execute__(struct ovs_cmdl_context *ctx, bool ro)
f85f8ebb
BP
1409{
1410 struct ovsdb_schema *schema;
1411 struct json *json;
1412 struct ovsdb *db;
1413 int i;
1414
1415 /* Create database. */
1636c761 1416 json = parse_json(ctx->argv[1]);
f85f8ebb
BP
1417 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1418 json_destroy(json);
bd06962a 1419 db = ovsdb_create(schema);
f85f8ebb 1420
1636c761 1421 for (i = 2; i < ctx->argc; i++) {
f85f8ebb
BP
1422 struct json *params, *result;
1423 char *s;
1424
1636c761 1425 params = parse_json(ctx->argv[i]);
e51879e9 1426 result = ovsdb_execute(db, NULL, params, ro, 0, NULL);
f85f8ebb
BP
1427 s = json_to_string(result, JSSF_SORT);
1428 printf("%s\n", s);
93ff0290 1429 free(s);
f85f8ebb
BP
1430 json_destroy(params);
1431 json_destroy(result);
1432 }
1433
1434 ovsdb_destroy(db);
1435}
1436
e51879e9
AZ
1437static void
1438do_execute_ro(struct ovs_cmdl_context *ctx)
1439{
1440 do_execute__(ctx, true);
1441}
1442
1443static void
1444do_execute(struct ovs_cmdl_context *ctx)
1445{
1446 do_execute__(ctx, false);
1447}
1448
f85f8ebb
BP
1449struct test_trigger {
1450 struct ovsdb_trigger trigger;
1451 int number;
1452};
1453
1454static void
1455do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
1456{
1457 struct json *result;
1458 char *s;
1459
1460 result = ovsdb_trigger_steal_result(&t->trigger);
1461 s = json_to_string(result, JSSF_SORT);
1462 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
93ff0290 1463 free(s);
f85f8ebb
BP
1464 json_destroy(result);
1465 ovsdb_trigger_destroy(&t->trigger);
1466 free(t);
1467}
1468
1469static void
1636c761 1470do_trigger(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
1471{
1472 struct ovsdb_schema *schema;
e317253b 1473 struct ovsdb_session session;
b4e8d170 1474 struct ovsdb_server server;
f85f8ebb
BP
1475 struct json *json;
1476 struct ovsdb *db;
1477 long long int now;
1478 int number;
1479 int i;
1480
1481 /* Create database. */
1636c761 1482 json = parse_json(ctx->argv[1]);
f85f8ebb
BP
1483 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1484 json_destroy(json);
bd06962a 1485 db = ovsdb_create(schema);
f85f8ebb 1486
b4e8d170
BP
1487 ovsdb_server_init(&server);
1488 ovsdb_server_add_db(&server, db);
1489 ovsdb_session_init(&session, &server);
e317253b 1490
f85f8ebb
BP
1491 now = 0;
1492 number = 0;
1636c761
RB
1493 for (i = 2; i < ctx->argc; i++) {
1494 struct json *params = parse_json(ctx->argv[i]);
f85f8ebb
BP
1495 if (params->type == JSON_ARRAY
1496 && json_array(params)->n == 2
1497 && json_array(params)->elems[0]->type == JSON_STRING
1498 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
1499 && json_array(params)->elems[1]->type == JSON_INTEGER) {
1500 now += json_integer(json_array(params)->elems[1]);
1501 json_destroy(params);
1502 } else {
1503 struct test_trigger *t = xmalloc(sizeof *t);
e51879e9 1504 ovsdb_trigger_init(&session, db, &t->trigger, params, now, false);
f85f8ebb
BP
1505 t->number = number++;
1506 if (ovsdb_trigger_is_complete(&t->trigger)) {
1507 do_trigger_dump(t, now, "immediate");
1508 } else {
1509 printf("t=%lld: new trigger %d\n", now, t->number);
1510 }
1511 }
1512
1513 ovsdb_trigger_run(db, now);
417e7e66
BW
1514 while (!ovs_list_is_empty(&session.completions)) {
1515 do_trigger_dump(CONTAINER_OF(ovs_list_pop_front(&session.completions),
f85f8ebb
BP
1516 struct test_trigger, trigger.node),
1517 now, "delayed");
1518 }
1519
1520 ovsdb_trigger_wait(db, now);
1521 poll_immediate_wake();
1522 poll_block();
1523 }
1524
b4e8d170 1525 ovsdb_server_destroy(&server);
f85f8ebb
BP
1526 ovsdb_destroy(db);
1527}
1528
1529static void
1636c761 1530do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
f85f8ebb
BP
1531{
1532 usage();
1533}
1534\f
1535/* "transact" command. */
1536
1537static struct ovsdb *do_transact_db;
1538static struct ovsdb_txn *do_transact_txn;
1539static struct ovsdb_table *do_transact_table;
1540
1541static void
1636c761 1542do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED)
f85f8ebb 1543{
85683160 1544 ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false));
f85f8ebb
BP
1545 do_transact_txn = NULL;
1546}
1547
1548static void
1636c761 1549do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED)
f85f8ebb
BP
1550{
1551 ovsdb_txn_abort(do_transact_txn);
1552 do_transact_txn = NULL;
1553}
1554
1555static void
1556uuid_from_integer(int integer, struct uuid *uuid)
1557{
1558 uuid_zero(uuid);
1559 uuid->parts[3] = integer;
1560}
1561
1562static const struct ovsdb_row *
1563do_transact_find_row(const char *uuid_string)
1564{
1565 const struct ovsdb_row *row;
1566 struct uuid uuid;
1567
1568 uuid_from_integer(atoi(uuid_string), &uuid);
1569 row = ovsdb_table_get_row(do_transact_table, &uuid);
1570 if (!row) {
1571 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1572 UUID_ARGS(&uuid));
1573 }
1574 return row;
1575}
1576
1577static void
1578do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1579 int integer)
1580{
1581 if (integer != -1) {
1582 const struct ovsdb_column *column;
1583
1584 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1585 column_name);
1586 row->fields[column->index].keys[0].integer = integer;
1587 }
1588}
1589
1590static int
1591do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1592{
1593 const struct ovsdb_column *column;
1594
1595 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1596 column_name);
1597 return row->fields[column->index].keys[0].integer;
1598}
1599
1600static void
1601do_transact_set_i_j(struct ovsdb_row *row,
1602 const char *i_string, const char *j_string)
1603{
1604 do_transact_set_integer(row, "i", atoi(i_string));
1605 do_transact_set_integer(row, "j", atoi(j_string));
1606}
1607
1608static void
1636c761 1609do_transact_insert(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
1610{
1611 struct ovsdb_row *row;
1612 struct uuid *uuid;
1613
1614 row = ovsdb_row_create(do_transact_table);
1615
1616 /* Set UUID. */
1617 uuid = ovsdb_row_get_uuid_rw(row);
1636c761 1618 uuid_from_integer(atoi(ctx->argv[1]), uuid);
f85f8ebb
BP
1619 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1620 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1621 UUID_ARGS(uuid));
1622 }
1623
1636c761 1624 do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]);
f85f8ebb
BP
1625
1626 /* Insert row. */
1627 ovsdb_txn_row_insert(do_transact_txn, row);
1628}
1629
1630static void
1636c761 1631do_transact_delete(struct ovs_cmdl_context *ctx)
f85f8ebb 1632{
1636c761 1633 const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]);
f85f8ebb
BP
1634 ovsdb_txn_row_delete(do_transact_txn, row);
1635}
1636
1637static void
1636c761 1638do_transact_modify(struct ovs_cmdl_context *ctx)
f85f8ebb
BP
1639{
1640 const struct ovsdb_row *row_ro;
1641 struct ovsdb_row *row_rw;
1642
1636c761 1643 row_ro = do_transact_find_row(ctx->argv[1]);
f85f8ebb 1644 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1636c761 1645 do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]);
f85f8ebb
BP
1646}
1647
1648static int
1649compare_rows_by_uuid(const void *a_, const void *b_)
1650{
1651 struct ovsdb_row *const *ap = a_;
1652 struct ovsdb_row *const *bp = b_;
1653
1654 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1655}
1656
1657static void
1636c761 1658do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED)
f85f8ebb
BP
1659{
1660 const struct ovsdb_row **rows;
1661 const struct ovsdb_row *row;
1662 size_t n_rows;
1663 size_t i;
1664
1665 n_rows = hmap_count(&do_transact_table->rows);
1666 rows = xmalloc(n_rows * sizeof *rows);
1667 i = 0;
4e8e4213 1668 HMAP_FOR_EACH (row, hmap_node, &do_transact_table->rows) {
f85f8ebb
BP
1669 rows[i++] = row;
1670 }
1671 assert(i == n_rows);
1672
1673 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1674
1675 for (i = 0; i < n_rows; i++) {
1676 printf("\n%"PRId32": i=%d, j=%d",
1677 ovsdb_row_get_uuid(rows[i])->parts[3],
1678 do_transact_get_integer(rows[i], "i"),
1679 do_transact_get_integer(rows[i], "j"));
1680 }
1681
1682 free(rows);
1683}
1684
1685static void
1636c761 1686do_transact(struct ovs_cmdl_context *ctx)
f85f8ebb 1687{
5f383751 1688 static const struct ovs_cmdl_command do_transact_commands[] = {
1f4a7252
RM
1689 { "commit", NULL, 0, 0, do_transact_commit, OVS_RO },
1690 { "abort", NULL, 0, 0, do_transact_abort, OVS_RO },
1691 { "insert", NULL, 2, 3, do_transact_insert, OVS_RO },
1692 { "delete", NULL, 1, 1, do_transact_delete, OVS_RO },
1693 { "modify", NULL, 2, 3, do_transact_modify, OVS_RO },
1694 { "print", NULL, 0, 0, do_transact_print, OVS_RO },
1695 { NULL, NULL, 0, 0, NULL, OVS_RO },
f85f8ebb
BP
1696 };
1697
1698 struct ovsdb_schema *schema;
1699 struct json *json;
1700 int i;
1701
1702 /* Create table. */
1703 json = parse_json("{\"name\": \"testdb\", "
1704 " \"tables\": "
1705 " {\"mytable\": "
1706 " {\"columns\": "
1707 " {\"i\": {\"type\": \"integer\"}, "
1708 " \"j\": {\"type\": \"integer\"}}}}}");
1709 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1710 json_destroy(json);
bd06962a 1711 do_transact_db = ovsdb_create(schema);
f85f8ebb
BP
1712 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1713 assert(do_transact_table != NULL);
1714
1636c761 1715 for (i = 1; i < ctx->argc; i++) {
f85f8ebb
BP
1716 struct json *command;
1717 size_t n_args;
1718 char **args;
1719 int j;
1636c761 1720 struct ovs_cmdl_context transact_ctx = { .argc = 0, };
f85f8ebb 1721
1636c761 1722 command = parse_json(ctx->argv[i]);
f85f8ebb
BP
1723 if (command->type != JSON_ARRAY) {
1724 ovs_fatal(0, "transaction %d must be JSON array "
1725 "with at least 1 element", i);
1726 }
1727
1728 n_args = command->u.array.n;
1729 args = xmalloc((n_args + 1) * sizeof *args);
1730 for (j = 0; j < n_args; j++) {
1731 struct json *s = command->u.array.elems[j];
1732 if (s->type != JSON_STRING) {
1733 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1734 i, j);
1735 }
1736 args[j] = xstrdup(json_string(s));
1737 }
1738 args[n_args] = NULL;
1739
1740 if (!do_transact_txn) {
1741 do_transact_txn = ovsdb_txn_create(do_transact_db);
1742 }
1743
1744 for (j = 0; j < n_args; j++) {
1745 if (j) {
1746 putchar(' ');
1747 }
1748 fputs(args[j], stdout);
1749 }
1750 fputs(":", stdout);
1636c761
RB
1751 transact_ctx.argc = n_args;
1752 transact_ctx.argv = args;
1753 ovs_cmdl_run_command(&transact_ctx, do_transact_commands);
f85f8ebb
BP
1754 putchar('\n');
1755
1756 for (j = 0; j < n_args; j++) {
1757 free(args[j]);
1758 }
1759 free(args);
1760 json_destroy(command);
1761 }
1762 ovsdb_txn_abort(do_transact_txn);
1763 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1764}
1765
c3bb4bd7 1766static int
e9011ac8 1767compare_link1(const void *a_, const void *b_)
c3bb4bd7 1768{
e9011ac8
BP
1769 const struct idltest_link1 *const *ap = a_;
1770 const struct idltest_link1 *const *bp = b_;
1771 const struct idltest_link1 *a = *ap;
1772 const struct idltest_link1 *b = *bp;
c3bb4bd7
BP
1773
1774 return a->i < b->i ? -1 : a->i > b->i;
1775}
1776
32d37ce8
SA
1777static void
1778print_idl_row_updated_simple(const struct idltest_simple *s, int step)
1779{
1780 size_t i;
1781 bool updated = false;
1782
1783 for (i = 0; i < IDLTEST_SIMPLE_N_COLUMNS; i++) {
1784 if (idltest_simple_is_updated(s, i)) {
1785 if (!updated) {
1786 printf("%03d: updated columns:", step);
1787 updated = true;
1788 }
1789 printf(" %s", idltest_simple_columns[i].name);
1790 }
1791 }
1792 if (updated) {
1793 printf("\n");
1794 }
1795}
1796
1797static void
1798print_idl_row_updated_link1(const struct idltest_link1 *l1, int step)
1799{
1800 size_t i;
1801 bool updated = false;
1802
1803 for (i = 0; i < IDLTEST_LINK1_N_COLUMNS; i++) {
1804 if (idltest_link1_is_updated(l1, i)) {
1805 if (!updated) {
1806 printf("%03d: updated columns:", step);
1807 updated = true;
1808 }
1809 printf(" %s", idltest_link1_columns[i].name);
1810 }
1811 }
1812 if (updated) {
1813 printf("\n");
1814 }
1815}
1816
1817static void
1818print_idl_row_updated_link2(const struct idltest_link2 *l2, int step)
1819{
1820 size_t i;
1821 bool updated = false;
1822
1823 for (i = 0; i < IDLTEST_LINK2_N_COLUMNS; i++) {
1824 if (idltest_link2_is_updated(l2, i)) {
1825 if (!updated) {
1826 printf("%03d: updated columns:", step);
1827 updated = true;
1828 }
1829 printf(" %s", idltest_link2_columns[i].name);
1830 }
1831 }
1832 if (updated) {
1833 printf("\n");
1834 }
1835}
1836
932104f4
SA
1837static void
1838print_idl_row_simple(const struct idltest_simple *s, int step)
1839{
1840 size_t i;
1841
1842 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1843 step, s->i, s->r, s->b ? "true" : "false",
1844 s->s, UUID_ARGS(&s->u));
1845 for (i = 0; i < s->n_ia; i++) {
1846 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1847 }
1848 printf("] ra=[");
1849 for (i = 0; i < s->n_ra; i++) {
1850 printf("%s%g", i ? " " : "", s->ra[i]);
1851 }
1852 printf("] ba=[");
1853 for (i = 0; i < s->n_ba; i++) {
1854 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1855 }
1856 printf("] sa=[");
1857 for (i = 0; i < s->n_sa; i++) {
1858 printf("%s%s", i ? " " : "", s->sa[i]);
1859 }
1860 printf("] ua=[");
1861 for (i = 0; i < s->n_ua; i++) {
1862 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1863 }
1864 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
32d37ce8 1865 print_idl_row_updated_simple(s, step);
932104f4
SA
1866}
1867
1868static void
1869print_idl_row_link1(const struct idltest_link1 *l1, int step)
1870{
1871 struct idltest_link1 **links;
1872 size_t i;
1873
1874 printf("%03d: i=%"PRId64" k=", step, l1->i);
1875 if (l1->k) {
1876 printf("%"PRId64, l1->k->i);
1877 }
1878 printf(" ka=[");
1879 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1880 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1881 for (i = 0; i < l1->n_ka; i++) {
1882 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1883 }
1884 free(links);
1885 printf("] l2=");
1886 if (l1->l2) {
1887 printf("%"PRId64, l1->l2->i);
1888 }
1889 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
32d37ce8 1890 print_idl_row_updated_link1(l1, step);
932104f4
SA
1891}
1892
1893static void
1894print_idl_row_link2(const struct idltest_link2 *l2, int step)
1895{
1896 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1897 if (l2->l1) {
1898 printf("%"PRId64, l2->l1->i);
1899 }
1900 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
32d37ce8 1901 print_idl_row_updated_link2(l2, step);
932104f4
SA
1902}
1903
c3bb4bd7
BP
1904static void
1905print_idl(struct ovsdb_idl *idl, int step)
1906{
1907 const struct idltest_simple *s;
e9011ac8
BP
1908 const struct idltest_link1 *l1;
1909 const struct idltest_link2 *l2;
c3bb4bd7
BP
1910 int n = 0;
1911
1912 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
932104f4 1913 print_idl_row_simple(s, step);
c3bb4bd7
BP
1914 n++;
1915 }
e9011ac8 1916 IDLTEST_LINK1_FOR_EACH (l1, idl) {
932104f4
SA
1917 print_idl_row_link1(l1, step);
1918 n++;
1919 }
1920 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1921 print_idl_row_link2(l2, step);
1922 n++;
1923 }
1924 if (!n) {
1925 printf("%03d: empty\n", step);
1926 }
1927}
c3bb4bd7 1928
932104f4
SA
1929static void
1930print_idl_track(struct ovsdb_idl *idl, int step, unsigned int seqno)
1931{
1932 const struct idltest_simple *s;
1933 const struct idltest_link1 *l1;
1934 const struct idltest_link2 *l2;
1935 int n = 0;
1936
1937 IDLTEST_SIMPLE_FOR_EACH_TRACKED (s, idl) {
1938 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1939 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1940 } else {
1941 print_idl_row_simple(s, step);
c3bb4bd7 1942 }
932104f4
SA
1943 n++;
1944 }
1945 IDLTEST_LINK1_FOR_EACH_TRACKED (l1, idl) {
1946 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1947 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1948 } else {
1949 print_idl_row_link1(l1, step);
e9011ac8 1950 }
e9011ac8
BP
1951 n++;
1952 }
932104f4
SA
1953 IDLTEST_LINK2_FOR_EACH_TRACKED (l2, idl) {
1954 if (idltest_simple_row_get_seqno(s, OVSDB_IDL_CHANGE_DELETE) >= seqno) {
1955 printf("%03d: ##deleted## uuid="UUID_FMT"\n", step, UUID_ARGS(&s->header_.uuid));
1956 } else {
1957 print_idl_row_link2(l2, step);
e9011ac8 1958 }
c3bb4bd7
BP
1959 n++;
1960 }
1961 if (!n) {
1962 printf("%03d: empty\n", step);
1963 }
1964}
1965
c3bb4bd7
BP
1966static void
1967parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1968 size_t *n)
1969{
1970 struct uuid uuid;
1971
1972 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
34582733 1973 char *name = xasprintf("#%"PRIuSIZE"#", *n);
2d2d6d4a
BP
1974 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1975 ovsdb_symbol_table_put(symtab, name, &uuid, false);
c3bb4bd7
BP
1976 free(name);
1977 *n += 1;
1978 } else if (json->type == JSON_ARRAY) {
1979 size_t i;
1980
1981 for (i = 0; i < json->u.array.n; i++) {
1982 parse_uuids(json->u.array.elems[i], symtab, n);
1983 }
1984 } else if (json->type == JSON_OBJECT) {
1985 const struct shash_node *node;
1986
1987 SHASH_FOR_EACH (node, json_object(json)) {
1988 parse_uuids(node->data, symtab, n);
1989 }
1990 }
1991}
1992
1993static void
1994substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1995{
1996 if (json->type == JSON_STRING) {
2d2d6d4a 1997 const struct ovsdb_symbol *symbol;
c3bb4bd7 1998
2d2d6d4a
BP
1999 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
2000 if (symbol) {
c3bb4bd7 2001 free(json->u.string);
2d2d6d4a 2002 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
c3bb4bd7
BP
2003 }
2004 } else if (json->type == JSON_ARRAY) {
2005 size_t i;
2006
2007 for (i = 0; i < json->u.array.n; i++) {
2008 substitute_uuids(json->u.array.elems[i], symtab);
2009 }
2010 } else if (json->type == JSON_OBJECT) {
2011 const struct shash_node *node;
2012
2013 SHASH_FOR_EACH (node, json_object(json)) {
2014 substitute_uuids(node->data, symtab);
2015 }
2016 }
2017}
2018
475281c0
BP
2019static const struct idltest_simple *
2020idltest_find_simple(struct ovsdb_idl *idl, int i)
2021{
2022 const struct idltest_simple *s;
2023
2024 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
2025 if (s->i == i) {
2026 return s;
2027 }
2028 }
2029 return NULL;
2030}
2031
2032static void
2033idl_set(struct ovsdb_idl *idl, char *commands, int step)
2034{
2035 char *cmd, *save_ptr1 = NULL;
2036 struct ovsdb_idl_txn *txn;
2037 enum ovsdb_idl_txn_status status;
b54e22e9 2038 bool increment = false;
475281c0
BP
2039
2040 txn = ovsdb_idl_txn_create(idl);
11990a52 2041 ovsdb_idl_check_consistency(idl);
475281c0
BP
2042 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
2043 cmd = strtok_r(NULL, ",", &save_ptr1)) {
2044 char *save_ptr2 = NULL;
2045 char *name, *arg1, *arg2, *arg3;
2046
2047 name = strtok_r(cmd, " ", &save_ptr2);
2048 arg1 = strtok_r(NULL, " ", &save_ptr2);
2049 arg2 = strtok_r(NULL, " ", &save_ptr2);
2050 arg3 = strtok_r(NULL, " ", &save_ptr2);
2051
2052 if (!strcmp(name, "set")) {
2053 const struct idltest_simple *s;
2054
2055 if (!arg3) {
2056 ovs_fatal(0, "\"set\" command requires 3 arguments");
2057 }
2058
2059 s = idltest_find_simple(idl, atoi(arg1));
2060 if (!s) {
2061 ovs_fatal(0, "\"set\" command asks for nonexistent "
2062 "i=%d", atoi(arg1));
2063 }
2064
2065 if (!strcmp(arg2, "b")) {
2066 idltest_simple_set_b(s, atoi(arg3));
2067 } else if (!strcmp(arg2, "s")) {
2068 idltest_simple_set_s(s, arg3);
2069 } else if (!strcmp(arg2, "u")) {
2070 struct uuid uuid;
bca51200
BP
2071 if (!uuid_from_string(&uuid, arg3)) {
2072 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
2073 }
475281c0
BP
2074 idltest_simple_set_u(s, uuid);
2075 } else if (!strcmp(arg2, "r")) {
2076 idltest_simple_set_r(s, atof(arg3));
2077 } else {
2078 ovs_fatal(0, "\"set\" command asks for unknown column %s",
2079 arg2);
2080 }
2081 } else if (!strcmp(name, "insert")) {
2082 struct idltest_simple *s;
2083
2084 if (!arg1 || arg2) {
6484e7cd 2085 ovs_fatal(0, "\"insert\" command requires 1 argument");
475281c0
BP
2086 }
2087
2088 s = idltest_simple_insert(txn);
2089 idltest_simple_set_i(s, atoi(arg1));
2090 } else if (!strcmp(name, "delete")) {
2091 const struct idltest_simple *s;
2092
2093 if (!arg1 || arg2) {
6484e7cd 2094 ovs_fatal(0, "\"delete\" command requires 1 argument");
475281c0
BP
2095 }
2096
2097 s = idltest_find_simple(idl, atoi(arg1));
2098 if (!s) {
6484e7cd 2099 ovs_fatal(0, "\"delete\" command asks for nonexistent "
475281c0
BP
2100 "i=%d", atoi(arg1));
2101 }
2102 idltest_simple_delete(s);
a91c6104
BP
2103 } else if (!strcmp(name, "verify")) {
2104 const struct idltest_simple *s;
2105
2106 if (!arg2 || arg3) {
2107 ovs_fatal(0, "\"verify\" command requires 2 arguments");
2108 }
2109
2110 s = idltest_find_simple(idl, atoi(arg1));
2111 if (!s) {
2112 ovs_fatal(0, "\"verify\" command asks for nonexistent "
2113 "i=%d", atoi(arg1));
2114 }
2115
2116 if (!strcmp(arg2, "i")) {
2117 idltest_simple_verify_i(s);
2118 } else if (!strcmp(arg2, "b")) {
2119 idltest_simple_verify_b(s);
2120 } else if (!strcmp(arg2, "s")) {
2121 idltest_simple_verify_s(s);
2122 } else if (!strcmp(arg2, "u")) {
2123 idltest_simple_verify_s(s);
2124 } else if (!strcmp(arg2, "r")) {
2125 idltest_simple_verify_r(s);
2126 } else {
2127 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
2128 arg2);
2129 }
b54e22e9 2130 } else if (!strcmp(name, "increment")) {
94fbe1aa
BP
2131 const struct idltest_simple *s;
2132
2133 if (!arg1 || arg2) {
2134 ovs_fatal(0, "\"increment\" command requires 1 argument");
b54e22e9 2135 }
94fbe1aa
BP
2136
2137 s = idltest_find_simple(idl, atoi(arg1));
2138 if (!s) {
2139 ovs_fatal(0, "\"set\" command asks for nonexistent "
2140 "i=%d", atoi(arg1));
2141 }
2142
de32cec7
BP
2143 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i,
2144 false);
b54e22e9 2145 increment = true;
2096903b
BP
2146 } else if (!strcmp(name, "abort")) {
2147 ovsdb_idl_txn_abort(txn);
11990a52 2148 ovsdb_idl_check_consistency(idl);
2096903b
BP
2149 break;
2150 } else if (!strcmp(name, "destroy")) {
2151 printf("%03d: destroy\n", step);
2152 ovsdb_idl_txn_destroy(txn);
11990a52 2153 ovsdb_idl_check_consistency(idl);
2096903b 2154 return;
475281c0
BP
2155 } else {
2156 ovs_fatal(0, "unknown command %s", name);
2157 }
11990a52 2158 ovsdb_idl_check_consistency(idl);
475281c0
BP
2159 }
2160
af96ccd2 2161 status = ovsdb_idl_txn_commit_block(txn);
b54e22e9 2162 printf("%03d: commit, status=%s",
475281c0 2163 step, ovsdb_idl_txn_status_to_string(status));
b54e22e9
BP
2164 if (increment) {
2165 printf(", increment=%"PRId64,
2166 ovsdb_idl_txn_get_increment_new_value(txn));
2167 }
2168 putchar('\n');
475281c0 2169 ovsdb_idl_txn_destroy(txn);
11990a52 2170 ovsdb_idl_check_consistency(idl);
475281c0
BP
2171}
2172
16ebb90e
LS
2173static const struct ovsdb_idl_table_class *
2174find_table_class(const char *name)
2175{
2176 if (!strcmp(name, "simple")) {
2177 return &idltest_table_simple;
2178 } else if (!strcmp(name, "link1")) {
2179 return &idltest_table_link1;
2180 } else if (!strcmp(name, "link2")) {
2181 return &idltest_table_link2;
2182 }
2183 return NULL;
2184}
2185
2186static void
2187parse_simple_json_clause(struct ovsdb_idl *idl, bool add_cmd,
2188 struct json *json)
2189{
2190 const char *c;
2191 struct ovsdb_error *error;
2192 enum ovsdb_function function;
2193
2194 if (json->type == JSON_TRUE) {
2195 add_cmd ? idltest_simple_add_clause_true(idl) :
2196 idltest_simple_remove_clause_true(idl);
2197 return;
2198 } else if (json->type == JSON_FALSE) {
2199 add_cmd ? idltest_simple_add_clause_false(idl) :
2200 idltest_simple_remove_clause_false(idl);
2201 return;
2202 }
2203 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2204 ovs_fatal(0, "Error parsing condition");
2205 }
2206
2207 c = json_string(json->u.array.elems[0]);
2208 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2209 &function);
2210 if (error) {
2211 ovs_fatal(0, "Error parsing clause function %s",
2212 json_string(json->u.array.elems[1]));
2213 }
2214
2215 /* add clause according to column */
2216 if (!strcmp(c, "b")) {
2217 add_cmd ? idltest_simple_add_clause_b(idl, function,
2218 json_boolean(json->u.array.elems[2])) :
2219 idltest_simple_remove_clause_b(idl, function,
2220 json_boolean(json->u.array.elems[2]));
2221 } else if (!strcmp(c, "i")) {
2222 add_cmd ? idltest_simple_add_clause_i(idl, function,
2223 json_integer(json->u.array.elems[2])) :
2224 idltest_simple_remove_clause_i(idl, function,
2225 json_integer(json->u.array.elems[2]));
2226 } else if (!strcmp(c, "s")) {
2227 add_cmd ? idltest_simple_add_clause_s(idl, function,
2228 json_string(json->u.array.elems[2])) :
2229 idltest_simple_remove_clause_s(idl, function,
2230 json_string(json->u.array.elems[2]));
2231 } else if (!strcmp(c, "u")) {
2232 struct uuid uuid;
2233 if (!uuid_from_string(&uuid,
2234 json_string(json->u.array.elems[2]))) {
2235 ovs_fatal(0, "\"%s\" is not a valid UUID",
2236 json_string(json->u.array.elems[2]));
2237 }
2238 add_cmd ? idltest_simple_add_clause_u(idl, function, uuid) :
2239 idltest_simple_remove_clause_u(idl, function, uuid);
2240 } else if (!strcmp(c, "r")) {
2241 add_cmd ? idltest_simple_add_clause_r(idl, function,
2242 json_real(json->u.array.elems[2])) :
2243 idltest_simple_remove_clause_r(idl, function,
2244 json_real(json->u.array.elems[2]));
2245 } else {
2246 ovs_fatal(0, "Unsupported columns name %s", c);
2247 }
2248}
2249
2250static void
2251parse_link1_json_clause(struct ovsdb_idl *idl, bool add_cmd,
2252 struct json *json)
2253{
2254 const char *c;
2255 struct ovsdb_error *error;
2256 enum ovsdb_function function;
2257
2258 if (json->type == JSON_TRUE) {
2259 add_cmd ? idltest_link1_add_clause_true(idl) :
2260 idltest_link1_remove_clause_true(idl);
2261 return;
2262 } else if (json->type == JSON_FALSE) {
2263 add_cmd ? idltest_link1_add_clause_false(idl) :
2264 idltest_link1_remove_clause_false(idl);
2265 return;
2266 }
2267 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2268 ovs_fatal(0, "Error parsing condition");
2269 }
2270
2271 c = json_string(json->u.array.elems[0]);
2272 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2273 &function);
2274 if (error) {
2275 ovs_fatal(0, "Error parsing clause function %s",
2276 json_string(json->u.array.elems[1]));
2277 }
2278
2279 /* add clause according to column */
2280 if (!strcmp(c, "i")) {
2281 add_cmd ? idltest_link1_add_clause_i(idl, function,
2282 json_integer(json->u.array.elems[2])) :
2283 idltest_link1_remove_clause_i(idl, function,
2284 json_integer(json->u.array.elems[2]));
2285 } else {
2286 ovs_fatal(0, "Unsupported columns name %s", c);
2287 }
2288}
2289
2290static void
2291parse_link2_json_clause(struct ovsdb_idl *idl, bool add_cmd, struct json *json)
2292{
2293 const char *c;
2294 struct ovsdb_error *error;
2295 enum ovsdb_function function;
2296
2297 if (json->type == JSON_TRUE) {
2298 add_cmd ? idltest_link2_add_clause_true(idl) :
2299 idltest_link2_remove_clause_true(idl);
2300 return;
2301 } else if (json->type == JSON_FALSE) {
2302 add_cmd ? idltest_link2_add_clause_false(idl) :
2303 idltest_link2_remove_clause_false(idl);
2304 return;
2305 }
2306 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2307 ovs_fatal(0, "Error parsing condition");
2308 }
2309
2310 c = json_string(json->u.array.elems[0]);
2311 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2312 &function);
2313 if (error) {
2314 ovs_fatal(0, "Error parsing clause function %s",
2315 json_string(json->u.array.elems[1]));
2316 }
2317
2318 /* add clause according to column */
2319 if (!strcmp(c, "i")) {
2320 add_cmd ? idltest_link2_add_clause_i(idl, function,
2321 json_integer(json->u.array.elems[2])) :
2322 idltest_link2_remove_clause_i(idl, function,
2323 json_integer(json->u.array.elems[2]));
2324 } else {
2325 ovs_fatal(0, "Unsupported columns name %s", c);
2326 }
2327}
2328
2329static void
2330update_conditions(struct ovsdb_idl *idl, char *commands)
2331{
2332 char *cmd, *save_ptr1 = NULL;
2333 const struct ovsdb_idl_table_class *tc;
2334 bool add_cmd = false;
2335
2336 for (cmd = strtok_r(commands, ";", &save_ptr1); cmd;
2337 cmd = strtok_r(NULL, ";", &save_ptr1)) {
2338 if (strstr(cmd, "condition add")) {
2339 cmd += strlen("condition add ");
2340 add_cmd = true;
2341 } else if (strstr(cmd, "condition remove")) {
2342 cmd += strlen("condition remove ");
2343 } else {
2344 ovs_fatal(0, "condition command should be add or remove");
2345 }
2346
2347 char *save_ptr2 = NULL;
2348 char *table_name = strtok_r(cmd, " ", &save_ptr2);
2349 struct json *json = parse_json(save_ptr2);
2350 int i;
2351
2352 if (json->type != JSON_ARRAY) {
2353 ovs_fatal(0, "condition should be an array");
2354 }
2355
2356 tc = find_table_class(table_name);
2357 if (!tc) {
2358 ovs_fatal(0, "Table %s does not exist", table_name);
2359 }
2360
2361 //ovsdb_idl_condition_reset(idl, tc);
2362
2363 for (i = 0; i < json->u.array.n; i++) {
2364 if (!strcmp(table_name, "simple")) {
2365 parse_simple_json_clause(idl, add_cmd, json->u.array.elems[i]);
2366 } else if (!strcmp(table_name, "link1")) {
2367 parse_link1_json_clause(idl, add_cmd, json->u.array.elems[i]);
2368 } else if (!strcmp(table_name, "link2")) {
2369 parse_link2_json_clause(idl, add_cmd, json->u.array.elems[i]);
2370 }
2371 }
7e160f66 2372 json_destroy(json);
16ebb90e
LS
2373 }
2374}
2375
c3bb4bd7 2376static void
1636c761 2377do_idl(struct ovs_cmdl_context *ctx)
c3bb4bd7
BP
2378{
2379 struct jsonrpc *rpc;
2380 struct ovsdb_idl *idl;
2381 unsigned int seqno = 0;
2382 struct ovsdb_symbol_table *symtab;
2383 size_t n_uuids = 0;
2384 int step = 0;
2385 int error;
2386 int i;
932104f4 2387 bool track;
c3bb4bd7 2388
932104f4
SA
2389 track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
2390
1636c761
RB
2391 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
2392 if (ctx->argc > 2) {
c3bb4bd7
BP
2393 struct stream *stream;
2394
1636c761 2395 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
f125905c 2396 DSCP_DEFAULT), &stream);
c3bb4bd7 2397 if (error) {
1636c761 2398 ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
c3bb4bd7
BP
2399 }
2400 rpc = jsonrpc_open(stream);
2401 } else {
2402 rpc = NULL;
2403 }
2404
932104f4
SA
2405 if (track) {
2406 ovsdb_idl_track_add_all(idl);
2407 }
2408
76a6e630 2409 setvbuf(stdout, NULL, _IONBF, 0);
0d0f05b9 2410
c3bb4bd7 2411 symtab = ovsdb_symbol_table_create();
16ebb90e
LS
2412 if (ctx->argc > 2 && strstr(ctx->argv[2], "condition ")) {
2413 update_conditions(idl, ctx->argv[2]);
2414 printf("%03d: change conditions\n", step++);
2415 i = 3;
2416 } else {
2417 i = 2;
2418 }
2419 for (; i < ctx->argc; i++) {
1636c761 2420 char *arg = ctx->argv[i];
c3bb4bd7 2421 struct jsonrpc_msg *request, *reply;
c3bb4bd7 2422
0d0f05b9
BP
2423 if (*arg == '+') {
2424 /* The previous transaction didn't change anything. */
2425 arg++;
2426 } else {
4ea21243 2427 /* Wait for update. */
854a94d9
BP
2428 for (;;) {
2429 ovsdb_idl_run(idl);
11990a52 2430 ovsdb_idl_check_consistency(idl);
854a94d9
BP
2431 if (ovsdb_idl_get_seqno(idl) != seqno) {
2432 break;
2433 }
4ea21243
BP
2434 jsonrpc_run(rpc);
2435
2436 ovsdb_idl_wait(idl);
2437 jsonrpc_wait(rpc);
2438 poll_block();
2439 }
2440
2441 /* Print update. */
932104f4
SA
2442 if (track) {
2443 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2444 ovsdb_idl_track_clear(idl);
2445 } else {
2446 print_idl(idl, step++);
2447 }
0d0f05b9 2448 }
4ea21243 2449 seqno = ovsdb_idl_get_seqno(idl);
c3bb4bd7 2450
0d0f05b9 2451 if (!strcmp(arg, "reconnect")) {
c3bb4bd7
BP
2452 printf("%03d: reconnect\n", step++);
2453 ovsdb_idl_force_reconnect(idl);
16ebb90e
LS
2454 } else if (strstr(arg, "condition ")) {
2455 update_conditions(idl, arg);
2456 printf("%03d: change conditions\n", step++);
0d0f05b9
BP
2457 } else if (arg[0] != '[') {
2458 idl_set(idl, arg, step++);
c3bb4bd7 2459 } else {
0d0f05b9 2460 struct json *json = parse_json(arg);
c3bb4bd7
BP
2461 substitute_uuids(json, symtab);
2462 request = jsonrpc_create_request("transact", json, NULL);
2463 error = jsonrpc_transact_block(rpc, request, &reply);
d35f8e72 2464 if (error || reply->error) {
c3bb4bd7
BP
2465 ovs_fatal(error, "jsonrpc transaction failed");
2466 }
2467 printf("%03d: ", step++);
2468 if (reply->result) {
2469 parse_uuids(reply->result, symtab, &n_uuids);
2470 }
2471 json_destroy(reply->id);
2472 reply->id = NULL;
2473 print_and_free_json(jsonrpc_msg_to_json(reply));
2474 }
2475 }
2476 ovsdb_symbol_table_destroy(symtab);
2477
2478 if (rpc) {
2479 jsonrpc_close(rpc);
2480 }
854a94d9
BP
2481 for (;;) {
2482 ovsdb_idl_run(idl);
11990a52 2483 ovsdb_idl_check_consistency(idl);
854a94d9
BP
2484 if (ovsdb_idl_get_seqno(idl) != seqno) {
2485 break;
2486 }
4ea21243
BP
2487 ovsdb_idl_wait(idl);
2488 poll_block();
2489 }
2490 print_idl(idl, step++);
932104f4 2491 ovsdb_idl_track_clear(idl);
c3bb4bd7
BP
2492 ovsdb_idl_destroy(idl);
2493 printf("%03d: done\n", step);
2494}
2495
7251075c
EA
2496static void
2497print_idl_row_simple2(const struct idltest_simple2 *s, int step)
2498{
2499 size_t i;
2500 const struct ovsdb_datum *smap, *imap;
2501
2502 smap = idltest_simple2_get_smap(s, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
2503 imap = idltest_simple2_get_imap(s, OVSDB_TYPE_INTEGER, OVSDB_TYPE_STRING);
2504 printf("%03d: name=%s smap=[",
2505 step, s->name);
2506 for (i = 0; i < smap->n; i++) {
2507 printf("[%s : %s]%s", smap->keys[i].string, smap->values[i].string,
2508 i < smap->n-1? ",": "");
2509 }
2510 printf("] imap=[");
2511 for (i = 0; i < imap->n; i++) {
2512 printf("[%"PRId64" : %s]%s", imap->keys[i].integer, imap->values[i].string,
2513 i < imap->n-1? ",":"");
2514 }
2515 printf("]\n");
2516}
2517
2518static void
2519dump_simple2(struct ovsdb_idl *idl,
2520 const struct idltest_simple2 *myRow,
2521 int step)
2522{
2523 IDLTEST_SIMPLE2_FOR_EACH(myRow, idl) {
2524 print_idl_row_simple2(myRow, step);
2525 }
2526}
2527
7251075c
EA
2528static void
2529do_idl_partial_update_map_column(struct ovs_cmdl_context *ctx)
2530{
2531 struct ovsdb_idl *idl;
2532 struct ovsdb_idl_txn *myTxn;
2533 const struct idltest_simple2 *myRow;
2534 const struct ovsdb_datum *smap, *imap OVS_UNUSED;
2535 int step = 0;
2536 char key_to_delete[100];
2537
7251075c
EA
2538 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, false, true);
2539 ovsdb_idl_add_table(idl, &idltest_table_simple2);
2540 ovsdb_idl_add_column(idl, &idltest_simple2_col_name);
2541 ovsdb_idl_add_column(idl, &idltest_simple2_col_smap);
2542 ovsdb_idl_add_column(idl, &idltest_simple2_col_imap);
2543 ovsdb_idl_get_initial_snapshot(idl);
2544 setvbuf(stdout, NULL, _IONBF, 0);
2545 ovsdb_idl_run(idl);
2546
f1ab6e06 2547 /* Display original data in table. */
7251075c
EA
2548 myRow = NULL;
2549 printf("%03d: Getting records\n", step++);
2550 dump_simple2(idl, myRow, step++);
2551
f1ab6e06 2552 /* Insert new elements in different map columns. */
7251075c
EA
2553 myRow = idltest_simple2_first(idl);
2554 myTxn = ovsdb_idl_txn_create(idl);
6ee95363 2555 idltest_simple2_get_smap(myRow, OVSDB_TYPE_STRING,
7251075c
EA
2556 OVSDB_TYPE_STRING);
2557 idltest_simple2_update_smap_setkey(myRow, "key1", "myList1");
2558 imap = idltest_simple2_get_imap(myRow, OVSDB_TYPE_INTEGER,
2559 OVSDB_TYPE_STRING);
2560 idltest_simple2_update_imap_setkey(myRow, 3, "myids2");
2561 idltest_simple2_set_name(myRow, "String2");
2562 ovsdb_idl_txn_commit_block(myTxn);
2563 ovsdb_idl_txn_destroy(myTxn);
2564 ovsdb_idl_get_initial_snapshot(idl);
2565 printf("%03d: After insert element\n", step++);
2566 dump_simple2(idl, myRow, step++);
2567
f1ab6e06 2568 /* Insert duplicate element. */
7251075c
EA
2569 myTxn = ovsdb_idl_txn_create(idl);
2570 idltest_simple2_update_smap_setkey(myRow, "key1", "myList1");
2571 ovsdb_idl_txn_commit_block(myTxn);
2572 ovsdb_idl_txn_destroy(myTxn);
2573 ovsdb_idl_get_initial_snapshot(idl);
2574 printf("%03d: After insert duplicated element\n", step++);
2575 dump_simple2(idl, myRow, step++);
2576
f1ab6e06 2577 /* Deletes an element of a map column. */
7251075c
EA
2578 myRow = idltest_simple2_first(idl);
2579 myTxn = ovsdb_idl_txn_create(idl);
2580 smap = idltest_simple2_get_smap(myRow, OVSDB_TYPE_STRING,
2581 OVSDB_TYPE_STRING);
2582 strcpy(key_to_delete, smap->keys[0].string);
2583 idltest_simple2_update_smap_delkey(myRow, smap->keys[0].string);
2584 ovsdb_idl_txn_commit_block(myTxn);
2585 ovsdb_idl_txn_destroy(myTxn);
2586 ovsdb_idl_get_initial_snapshot(idl);
2587 printf("%03d: After delete element\n", step++);
2588 dump_simple2(idl, myRow, step++);
2589
f1ab6e06 2590 /* Try to delete a deleted element of a map column. */
7251075c
EA
2591 myTxn = ovsdb_idl_txn_create(idl);
2592 idltest_simple2_update_smap_delkey(myRow, key_to_delete);
2593 ovsdb_idl_txn_commit_block(myTxn);
2594 ovsdb_idl_txn_destroy(myTxn);
2595 ovsdb_idl_get_initial_snapshot(idl);
2596 printf("%03d: After trying to delete a deleted element\n", step++);
2597 dump_simple2(idl, myRow, step++);
2598
2599 printf("%03d: End test\n", step);
2600 return;
2601}
2602
f1ab6e06
RM
2603static void
2604print_idl_row_simple3(const struct idltest_simple3 *s, int step)
2605{
2606 size_t i;
2607 const struct ovsdb_datum *uset;
2608 const struct ovsdb_datum *uref;
2609
2610 uset = idltest_simple3_get_uset(s, OVSDB_TYPE_UUID);
2611 printf("%03d: name=%s uset=[",
2612 step, s->name);
2613 for (i = 0; i < uset->n; i++) {
2614 printf("["UUID_FMT"]%s", UUID_ARGS(&(uset->keys[i].uuid)), i < uset->n-1? ",": "");
2615 }
2616 uref = idltest_simple3_get_uref(s, OVSDB_TYPE_UUID);
2617 printf("] uref=[");
2618 for (i = 0; i < uref->n; i++) {
2619 printf("["UUID_FMT"]%s", UUID_ARGS(&(uref->keys[i].uuid)), i < uref->n-1? ",": "");
2620 }
2621 printf("]\n");
2622}
2623
2624static void
2625dump_simple3(struct ovsdb_idl *idl,
2626 const struct idltest_simple3 *myRow,
2627 int step)
2628{
2629 IDLTEST_SIMPLE3_FOR_EACH(myRow, idl) {
2630 print_idl_row_simple3(myRow, step);
2631 }
2632}
2633
2634static void
2635do_idl_partial_update_set_column(struct ovs_cmdl_context *ctx)
2636{
2637 struct ovsdb_idl *idl;
2638 struct ovsdb_idl_txn *myTxn;
2639 const struct idltest_simple3 *myRow;
2640 struct idltest_simple4 *myRow2;
2641 const struct ovsdb_datum *uset OVS_UNUSED;
2642 const struct ovsdb_datum *uref OVS_UNUSED;
2643 int step = 0;
2644
f1ab6e06
RM
2645 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, false, true);
2646 ovsdb_idl_add_table(idl, &idltest_table_simple3);
2647 ovsdb_idl_add_column(idl, &idltest_simple3_col_name);
2648 ovsdb_idl_add_column(idl, &idltest_simple3_col_uset);
2649 ovsdb_idl_add_column(idl, &idltest_simple3_col_uref);
2650 ovsdb_idl_add_table(idl, &idltest_table_simple4);
2651 ovsdb_idl_add_column(idl, &idltest_simple4_col_name);
2652 ovsdb_idl_get_initial_snapshot(idl);
2653 setvbuf(stdout, NULL, _IONBF, 0);
2654 ovsdb_idl_run(idl);
2655
2656 /* Display original data in table. */
2657 myRow = NULL;
2658 printf("%03d: Getting records\n", step++);
2659 dump_simple3(idl, myRow, step++);
2660
2661 /* Insert new elements in different map columns. */
2662 myRow = idltest_simple3_first(idl);
2663 myTxn = ovsdb_idl_txn_create(idl);
2664 idltest_simple3_get_uset(myRow, OVSDB_TYPE_UUID);
2665 struct uuid uuid_to_add;
2666 uuid_from_string(&uuid_to_add, "001e43d2-dd3f-4616-ab6a-83a490bb0991");
2667 idltest_simple3_update_uset_addvalue(myRow, uuid_to_add);
2668 idltest_simple3_set_name(myRow, "String2");
2669 ovsdb_idl_txn_commit_block(myTxn);
2670 ovsdb_idl_txn_destroy(myTxn);
2671 ovsdb_idl_get_initial_snapshot(idl);
2672 printf("%03d: After rename+add new value\n", step++);
2673 dump_simple3(idl, myRow, step++);
2674
2675 /* Insert duplicate element. */
2676 myTxn = ovsdb_idl_txn_create(idl);
2677 struct uuid uuid_to_add2;
2678 uuid_from_string(&uuid_to_add2, "0026b3ba-571b-4729-8227-d860a5210ab8");
2679 idltest_simple3_update_uset_addvalue(myRow, uuid_to_add2);
2680 ovsdb_idl_txn_commit_block(myTxn);
2681 ovsdb_idl_txn_destroy(myTxn);
2682 ovsdb_idl_get_initial_snapshot(idl);
2683 printf("%03d: After add new value\n", step++);
2684 dump_simple3(idl, myRow, step++);
2685
2686 /* Deletes an element of a set column. */
2687 myRow = idltest_simple3_first(idl);
2688 myTxn = ovsdb_idl_txn_create(idl);
2689 uset = idltest_simple3_get_uset(myRow, OVSDB_TYPE_UUID);
2690 idltest_simple3_update_uset_delvalue(myRow, uuid_to_add);
2691 ovsdb_idl_txn_commit_block(myTxn);
2692 ovsdb_idl_txn_destroy(myTxn);
2693 ovsdb_idl_get_initial_snapshot(idl);
2694 printf("%03d: After delete value\n", step++);
2695 dump_simple3(idl, myRow, step++);
2696
2697 /* Try to delete a deleted element of a map column. */
2698 myRow = idltest_simple3_first(idl);
2699 myTxn = ovsdb_idl_txn_create(idl);
2700 idltest_simple3_update_uset_delvalue(myRow, uuid_to_add);
2701 ovsdb_idl_txn_commit_block(myTxn);
2702 ovsdb_idl_txn_destroy(myTxn);
2703 ovsdb_idl_get_initial_snapshot(idl);
2704 printf("%03d: After trying to delete a deleted value\n", step++);
2705 dump_simple3(idl, myRow, step++);
2706
2707 /* Adds to a table and update a strong reference in another table. */
2708 myRow = idltest_simple3_first(idl);
2709 myTxn = ovsdb_idl_txn_create(idl);
2710 myRow2 = idltest_simple4_insert(myTxn);
2711 idltest_simple4_set_name(myRow2, "test");
2712 idltest_simple3_update_uref_addvalue(myRow, myRow2);
2713 ovsdb_idl_txn_commit_block(myTxn);
2714 ovsdb_idl_txn_destroy(myTxn);
2715 ovsdb_idl_get_initial_snapshot(idl);
2716 printf("%03d: After add to other table + set of strong ref\n", step++);
2717 dump_simple3(idl, myRow, step++);
2718 printf("%03d: End test\n", step);
2719 return;
2720}
2721
5f383751 2722static struct ovs_cmdl_command all_commands[] = {
1f4a7252
RM
2723 { "log-io", NULL, 2, INT_MAX, do_log_io, OVS_RO },
2724 { "default-atoms", NULL, 0, 0, do_default_atoms, OVS_RO },
2725 { "default-data", NULL, 0, 0, do_default_data, OVS_RO },
2726 { "diff-data", NULL, 3, INT_MAX, do_diff_data, OVS_RO },
2727 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type, OVS_RO },
2728 { "parse-base-type", NULL, 1, 1, do_parse_base_type, OVS_RO },
2729 { "parse-type", NULL, 1, 1, do_parse_type, OVS_RO },
2730 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms, OVS_RO },
2731 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings, OVS_RO },
2732 { "parse-data", NULL, 2, INT_MAX, do_parse_data, OVS_RO },
2733 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings, OVS_RO },
2734 { "sort-atoms", NULL, 2, 2, do_sort_atoms, OVS_RO },
2735 { "parse-column", NULL, 2, 2, do_parse_column, OVS_RO },
2736 { "parse-table", NULL, 2, 3, do_parse_table, OVS_RO },
2737 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows, OVS_RO },
2738 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows, OVS_RO },
2739 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions, OVS_RO },
2740 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions, OVS_RO },
2741 { "evaluate-conditions-any", NULL, 3, 3, do_evaluate_conditions_any, OVS_RO },
2742 { "compare-conditions", NULL, 2, 2, do_compare_conditions, OVS_RO },
2743 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations, OVS_RO },
2744 { "execute-mutations", NULL, 3, 3, do_execute_mutations, OVS_RO },
2745 { "query", NULL, 3, 3, do_query, OVS_RO },
2746 { "query-distinct", NULL, 4, 4, do_query_distinct, OVS_RO },
2747 { "transact", NULL, 1, INT_MAX, do_transact, OVS_RO },
2748 { "parse-schema", NULL, 1, 1, do_parse_schema, OVS_RO },
2749 { "execute", NULL, 2, INT_MAX, do_execute, OVS_RO },
2750 { "execute-readonly", NULL, 2, INT_MAX, do_execute_ro, OVS_RO },
2751 { "trigger", NULL, 2, INT_MAX, do_trigger, OVS_RO },
2752 { "idl", NULL, 1, INT_MAX, do_idl, OVS_RO },
7251075c 2753 { "idl-partial-update-map-column", NULL, 1, INT_MAX,
1f4a7252 2754 do_idl_partial_update_map_column, OVS_RO },
f1ab6e06 2755 { "idl-partial-update-set-column", NULL, 1, INT_MAX,
1f4a7252
RM
2756 do_idl_partial_update_set_column, OVS_RO },
2757 { "help", NULL, 0, INT_MAX, do_help, OVS_RO },
2758 { NULL, NULL, 0, 0, NULL, OVS_RO },
f85f8ebb 2759};
d2586fce 2760
5f383751 2761static struct ovs_cmdl_command *
d2586fce
GS
2762get_all_commands(void)
2763{
2764 return all_commands;
2765}