]> git.proxmox.com Git - mirror_ovs.git/blame - tests/test-ovsdb.c
ovsdb-idl: Style and comment improvements for conditional replication.
[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[] = {
451de37e
AW
1689 { "commit", NULL, 0, 0, do_transact_commit },
1690 { "abort", NULL, 0, 0, do_transact_abort },
1691 { "insert", NULL, 2, 3, do_transact_insert },
1692 { "delete", NULL, 1, 1, do_transact_delete },
1693 { "modify", NULL, 2, 3, do_transact_modify },
1694 { "print", NULL, 0, 0, do_transact_print },
1695 { NULL, NULL, 0, 0, NULL },
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);
2041 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
2042 cmd = strtok_r(NULL, ",", &save_ptr1)) {
2043 char *save_ptr2 = NULL;
2044 char *name, *arg1, *arg2, *arg3;
2045
2046 name = strtok_r(cmd, " ", &save_ptr2);
2047 arg1 = strtok_r(NULL, " ", &save_ptr2);
2048 arg2 = strtok_r(NULL, " ", &save_ptr2);
2049 arg3 = strtok_r(NULL, " ", &save_ptr2);
2050
2051 if (!strcmp(name, "set")) {
2052 const struct idltest_simple *s;
2053
2054 if (!arg3) {
2055 ovs_fatal(0, "\"set\" command requires 3 arguments");
2056 }
2057
2058 s = idltest_find_simple(idl, atoi(arg1));
2059 if (!s) {
2060 ovs_fatal(0, "\"set\" command asks for nonexistent "
2061 "i=%d", atoi(arg1));
2062 }
2063
2064 if (!strcmp(arg2, "b")) {
2065 idltest_simple_set_b(s, atoi(arg3));
2066 } else if (!strcmp(arg2, "s")) {
2067 idltest_simple_set_s(s, arg3);
2068 } else if (!strcmp(arg2, "u")) {
2069 struct uuid uuid;
bca51200
BP
2070 if (!uuid_from_string(&uuid, arg3)) {
2071 ovs_fatal(0, "\"%s\" is not a valid UUID", arg3);
2072 }
475281c0
BP
2073 idltest_simple_set_u(s, uuid);
2074 } else if (!strcmp(arg2, "r")) {
2075 idltest_simple_set_r(s, atof(arg3));
2076 } else {
2077 ovs_fatal(0, "\"set\" command asks for unknown column %s",
2078 arg2);
2079 }
2080 } else if (!strcmp(name, "insert")) {
2081 struct idltest_simple *s;
2082
2083 if (!arg1 || arg2) {
6484e7cd 2084 ovs_fatal(0, "\"insert\" command requires 1 argument");
475281c0
BP
2085 }
2086
2087 s = idltest_simple_insert(txn);
2088 idltest_simple_set_i(s, atoi(arg1));
2089 } else if (!strcmp(name, "delete")) {
2090 const struct idltest_simple *s;
2091
2092 if (!arg1 || arg2) {
6484e7cd 2093 ovs_fatal(0, "\"delete\" command requires 1 argument");
475281c0
BP
2094 }
2095
2096 s = idltest_find_simple(idl, atoi(arg1));
2097 if (!s) {
6484e7cd 2098 ovs_fatal(0, "\"delete\" command asks for nonexistent "
475281c0
BP
2099 "i=%d", atoi(arg1));
2100 }
2101 idltest_simple_delete(s);
a91c6104
BP
2102 } else if (!strcmp(name, "verify")) {
2103 const struct idltest_simple *s;
2104
2105 if (!arg2 || arg3) {
2106 ovs_fatal(0, "\"verify\" command requires 2 arguments");
2107 }
2108
2109 s = idltest_find_simple(idl, atoi(arg1));
2110 if (!s) {
2111 ovs_fatal(0, "\"verify\" command asks for nonexistent "
2112 "i=%d", atoi(arg1));
2113 }
2114
2115 if (!strcmp(arg2, "i")) {
2116 idltest_simple_verify_i(s);
2117 } else if (!strcmp(arg2, "b")) {
2118 idltest_simple_verify_b(s);
2119 } else if (!strcmp(arg2, "s")) {
2120 idltest_simple_verify_s(s);
2121 } else if (!strcmp(arg2, "u")) {
2122 idltest_simple_verify_s(s);
2123 } else if (!strcmp(arg2, "r")) {
2124 idltest_simple_verify_r(s);
2125 } else {
2126 ovs_fatal(0, "\"verify\" command asks for unknown column %s",
2127 arg2);
2128 }
b54e22e9 2129 } else if (!strcmp(name, "increment")) {
94fbe1aa
BP
2130 const struct idltest_simple *s;
2131
2132 if (!arg1 || arg2) {
2133 ovs_fatal(0, "\"increment\" command requires 1 argument");
b54e22e9 2134 }
94fbe1aa
BP
2135
2136 s = idltest_find_simple(idl, atoi(arg1));
2137 if (!s) {
2138 ovs_fatal(0, "\"set\" command asks for nonexistent "
2139 "i=%d", atoi(arg1));
2140 }
2141
de32cec7
BP
2142 ovsdb_idl_txn_increment(txn, &s->header_, &idltest_simple_col_i,
2143 false);
b54e22e9 2144 increment = true;
2096903b
BP
2145 } else if (!strcmp(name, "abort")) {
2146 ovsdb_idl_txn_abort(txn);
2147 break;
2148 } else if (!strcmp(name, "destroy")) {
2149 printf("%03d: destroy\n", step);
2150 ovsdb_idl_txn_destroy(txn);
2151 return;
475281c0
BP
2152 } else {
2153 ovs_fatal(0, "unknown command %s", name);
2154 }
2155 }
2156
af96ccd2 2157 status = ovsdb_idl_txn_commit_block(txn);
b54e22e9 2158 printf("%03d: commit, status=%s",
475281c0 2159 step, ovsdb_idl_txn_status_to_string(status));
b54e22e9
BP
2160 if (increment) {
2161 printf(", increment=%"PRId64,
2162 ovsdb_idl_txn_get_increment_new_value(txn));
2163 }
2164 putchar('\n');
475281c0
BP
2165 ovsdb_idl_txn_destroy(txn);
2166}
2167
16ebb90e
LS
2168static const struct ovsdb_idl_table_class *
2169find_table_class(const char *name)
2170{
2171 if (!strcmp(name, "simple")) {
2172 return &idltest_table_simple;
2173 } else if (!strcmp(name, "link1")) {
2174 return &idltest_table_link1;
2175 } else if (!strcmp(name, "link2")) {
2176 return &idltest_table_link2;
2177 }
2178 return NULL;
2179}
2180
2181static void
2182parse_simple_json_clause(struct ovsdb_idl *idl, bool add_cmd,
2183 struct json *json)
2184{
2185 const char *c;
2186 struct ovsdb_error *error;
2187 enum ovsdb_function function;
2188
2189 if (json->type == JSON_TRUE) {
2190 add_cmd ? idltest_simple_add_clause_true(idl) :
2191 idltest_simple_remove_clause_true(idl);
2192 return;
2193 } else if (json->type == JSON_FALSE) {
2194 add_cmd ? idltest_simple_add_clause_false(idl) :
2195 idltest_simple_remove_clause_false(idl);
2196 return;
2197 }
2198 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2199 ovs_fatal(0, "Error parsing condition");
2200 }
2201
2202 c = json_string(json->u.array.elems[0]);
2203 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2204 &function);
2205 if (error) {
2206 ovs_fatal(0, "Error parsing clause function %s",
2207 json_string(json->u.array.elems[1]));
2208 }
2209
2210 /* add clause according to column */
2211 if (!strcmp(c, "b")) {
2212 add_cmd ? idltest_simple_add_clause_b(idl, function,
2213 json_boolean(json->u.array.elems[2])) :
2214 idltest_simple_remove_clause_b(idl, function,
2215 json_boolean(json->u.array.elems[2]));
2216 } else if (!strcmp(c, "i")) {
2217 add_cmd ? idltest_simple_add_clause_i(idl, function,
2218 json_integer(json->u.array.elems[2])) :
2219 idltest_simple_remove_clause_i(idl, function,
2220 json_integer(json->u.array.elems[2]));
2221 } else if (!strcmp(c, "s")) {
2222 add_cmd ? idltest_simple_add_clause_s(idl, function,
2223 json_string(json->u.array.elems[2])) :
2224 idltest_simple_remove_clause_s(idl, function,
2225 json_string(json->u.array.elems[2]));
2226 } else if (!strcmp(c, "u")) {
2227 struct uuid uuid;
2228 if (!uuid_from_string(&uuid,
2229 json_string(json->u.array.elems[2]))) {
2230 ovs_fatal(0, "\"%s\" is not a valid UUID",
2231 json_string(json->u.array.elems[2]));
2232 }
2233 add_cmd ? idltest_simple_add_clause_u(idl, function, uuid) :
2234 idltest_simple_remove_clause_u(idl, function, uuid);
2235 } else if (!strcmp(c, "r")) {
2236 add_cmd ? idltest_simple_add_clause_r(idl, function,
2237 json_real(json->u.array.elems[2])) :
2238 idltest_simple_remove_clause_r(idl, function,
2239 json_real(json->u.array.elems[2]));
2240 } else {
2241 ovs_fatal(0, "Unsupported columns name %s", c);
2242 }
2243}
2244
2245static void
2246parse_link1_json_clause(struct ovsdb_idl *idl, bool add_cmd,
2247 struct json *json)
2248{
2249 const char *c;
2250 struct ovsdb_error *error;
2251 enum ovsdb_function function;
2252
2253 if (json->type == JSON_TRUE) {
2254 add_cmd ? idltest_link1_add_clause_true(idl) :
2255 idltest_link1_remove_clause_true(idl);
2256 return;
2257 } else if (json->type == JSON_FALSE) {
2258 add_cmd ? idltest_link1_add_clause_false(idl) :
2259 idltest_link1_remove_clause_false(idl);
2260 return;
2261 }
2262 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2263 ovs_fatal(0, "Error parsing condition");
2264 }
2265
2266 c = json_string(json->u.array.elems[0]);
2267 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2268 &function);
2269 if (error) {
2270 ovs_fatal(0, "Error parsing clause function %s",
2271 json_string(json->u.array.elems[1]));
2272 }
2273
2274 /* add clause according to column */
2275 if (!strcmp(c, "i")) {
2276 add_cmd ? idltest_link1_add_clause_i(idl, function,
2277 json_integer(json->u.array.elems[2])) :
2278 idltest_link1_remove_clause_i(idl, function,
2279 json_integer(json->u.array.elems[2]));
2280 } else {
2281 ovs_fatal(0, "Unsupported columns name %s", c);
2282 }
2283}
2284
2285static void
2286parse_link2_json_clause(struct ovsdb_idl *idl, bool add_cmd, struct json *json)
2287{
2288 const char *c;
2289 struct ovsdb_error *error;
2290 enum ovsdb_function function;
2291
2292 if (json->type == JSON_TRUE) {
2293 add_cmd ? idltest_link2_add_clause_true(idl) :
2294 idltest_link2_remove_clause_true(idl);
2295 return;
2296 } else if (json->type == JSON_FALSE) {
2297 add_cmd ? idltest_link2_add_clause_false(idl) :
2298 idltest_link2_remove_clause_false(idl);
2299 return;
2300 }
2301 if (json->type != JSON_ARRAY || json->u.array.n != 3) {
2302 ovs_fatal(0, "Error parsing condition");
2303 }
2304
2305 c = json_string(json->u.array.elems[0]);
2306 error = ovsdb_function_from_string(json_string(json->u.array.elems[1]),
2307 &function);
2308 if (error) {
2309 ovs_fatal(0, "Error parsing clause function %s",
2310 json_string(json->u.array.elems[1]));
2311 }
2312
2313 /* add clause according to column */
2314 if (!strcmp(c, "i")) {
2315 add_cmd ? idltest_link2_add_clause_i(idl, function,
2316 json_integer(json->u.array.elems[2])) :
2317 idltest_link2_remove_clause_i(idl, function,
2318 json_integer(json->u.array.elems[2]));
2319 } else {
2320 ovs_fatal(0, "Unsupported columns name %s", c);
2321 }
2322}
2323
2324static void
2325update_conditions(struct ovsdb_idl *idl, char *commands)
2326{
2327 char *cmd, *save_ptr1 = NULL;
2328 const struct ovsdb_idl_table_class *tc;
2329 bool add_cmd = false;
2330
2331 for (cmd = strtok_r(commands, ";", &save_ptr1); cmd;
2332 cmd = strtok_r(NULL, ";", &save_ptr1)) {
2333 if (strstr(cmd, "condition add")) {
2334 cmd += strlen("condition add ");
2335 add_cmd = true;
2336 } else if (strstr(cmd, "condition remove")) {
2337 cmd += strlen("condition remove ");
2338 } else {
2339 ovs_fatal(0, "condition command should be add or remove");
2340 }
2341
2342 char *save_ptr2 = NULL;
2343 char *table_name = strtok_r(cmd, " ", &save_ptr2);
2344 struct json *json = parse_json(save_ptr2);
2345 int i;
2346
2347 if (json->type != JSON_ARRAY) {
2348 ovs_fatal(0, "condition should be an array");
2349 }
2350
2351 tc = find_table_class(table_name);
2352 if (!tc) {
2353 ovs_fatal(0, "Table %s does not exist", table_name);
2354 }
2355
2356 //ovsdb_idl_condition_reset(idl, tc);
2357
2358 for (i = 0; i < json->u.array.n; i++) {
2359 if (!strcmp(table_name, "simple")) {
2360 parse_simple_json_clause(idl, add_cmd, json->u.array.elems[i]);
2361 } else if (!strcmp(table_name, "link1")) {
2362 parse_link1_json_clause(idl, add_cmd, json->u.array.elems[i]);
2363 } else if (!strcmp(table_name, "link2")) {
2364 parse_link2_json_clause(idl, add_cmd, json->u.array.elems[i]);
2365 }
2366 }
7e160f66 2367 json_destroy(json);
16ebb90e
LS
2368 }
2369}
2370
c3bb4bd7 2371static void
1636c761 2372do_idl(struct ovs_cmdl_context *ctx)
c3bb4bd7
BP
2373{
2374 struct jsonrpc *rpc;
2375 struct ovsdb_idl *idl;
2376 unsigned int seqno = 0;
2377 struct ovsdb_symbol_table *symtab;
2378 size_t n_uuids = 0;
2379 int step = 0;
2380 int error;
2381 int i;
932104f4 2382 bool track;
c3bb4bd7 2383
bd76d25d
BP
2384 idltest_init();
2385
932104f4
SA
2386 track = ((struct test_ovsdb_pvt_context *)(ctx->pvt))->track;
2387
1636c761
RB
2388 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true);
2389 if (ctx->argc > 2) {
c3bb4bd7
BP
2390 struct stream *stream;
2391
1636c761 2392 error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
f125905c 2393 DSCP_DEFAULT), &stream);
c3bb4bd7 2394 if (error) {
1636c761 2395 ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]);
c3bb4bd7
BP
2396 }
2397 rpc = jsonrpc_open(stream);
2398 } else {
2399 rpc = NULL;
2400 }
2401
932104f4
SA
2402 if (track) {
2403 ovsdb_idl_track_add_all(idl);
2404 }
2405
76a6e630 2406 setvbuf(stdout, NULL, _IONBF, 0);
0d0f05b9 2407
c3bb4bd7 2408 symtab = ovsdb_symbol_table_create();
16ebb90e
LS
2409 if (ctx->argc > 2 && strstr(ctx->argv[2], "condition ")) {
2410 update_conditions(idl, ctx->argv[2]);
2411 printf("%03d: change conditions\n", step++);
2412 i = 3;
2413 } else {
2414 i = 2;
2415 }
2416 for (; i < ctx->argc; i++) {
1636c761 2417 char *arg = ctx->argv[i];
c3bb4bd7 2418 struct jsonrpc_msg *request, *reply;
c3bb4bd7 2419
0d0f05b9
BP
2420 if (*arg == '+') {
2421 /* The previous transaction didn't change anything. */
2422 arg++;
2423 } else {
4ea21243 2424 /* Wait for update. */
854a94d9
BP
2425 for (;;) {
2426 ovsdb_idl_run(idl);
2427 if (ovsdb_idl_get_seqno(idl) != seqno) {
2428 break;
2429 }
4ea21243
BP
2430 jsonrpc_run(rpc);
2431
2432 ovsdb_idl_wait(idl);
2433 jsonrpc_wait(rpc);
2434 poll_block();
2435 }
2436
2437 /* Print update. */
932104f4
SA
2438 if (track) {
2439 print_idl_track(idl, step++, ovsdb_idl_get_seqno(idl));
2440 ovsdb_idl_track_clear(idl);
2441 } else {
2442 print_idl(idl, step++);
2443 }
0d0f05b9 2444 }
4ea21243 2445 seqno = ovsdb_idl_get_seqno(idl);
c3bb4bd7 2446
0d0f05b9 2447 if (!strcmp(arg, "reconnect")) {
c3bb4bd7
BP
2448 printf("%03d: reconnect\n", step++);
2449 ovsdb_idl_force_reconnect(idl);
16ebb90e
LS
2450 } else if (strstr(arg, "condition ")) {
2451 update_conditions(idl, arg);
2452 printf("%03d: change conditions\n", step++);
0d0f05b9
BP
2453 } else if (arg[0] != '[') {
2454 idl_set(idl, arg, step++);
c3bb4bd7 2455 } else {
0d0f05b9 2456 struct json *json = parse_json(arg);
c3bb4bd7
BP
2457 substitute_uuids(json, symtab);
2458 request = jsonrpc_create_request("transact", json, NULL);
2459 error = jsonrpc_transact_block(rpc, request, &reply);
d35f8e72 2460 if (error || reply->error) {
c3bb4bd7
BP
2461 ovs_fatal(error, "jsonrpc transaction failed");
2462 }
2463 printf("%03d: ", step++);
2464 if (reply->result) {
2465 parse_uuids(reply->result, symtab, &n_uuids);
2466 }
2467 json_destroy(reply->id);
2468 reply->id = NULL;
2469 print_and_free_json(jsonrpc_msg_to_json(reply));
2470 }
2471 }
2472 ovsdb_symbol_table_destroy(symtab);
2473
2474 if (rpc) {
2475 jsonrpc_close(rpc);
2476 }
854a94d9
BP
2477 for (;;) {
2478 ovsdb_idl_run(idl);
2479 if (ovsdb_idl_get_seqno(idl) != seqno) {
2480 break;
2481 }
4ea21243
BP
2482 ovsdb_idl_wait(idl);
2483 poll_block();
2484 }
2485 print_idl(idl, step++);
932104f4 2486 ovsdb_idl_track_clear(idl);
c3bb4bd7
BP
2487 ovsdb_idl_destroy(idl);
2488 printf("%03d: done\n", step);
2489}
2490
7251075c
EA
2491static void
2492print_idl_row_simple2(const struct idltest_simple2 *s, int step)
2493{
2494 size_t i;
2495 const struct ovsdb_datum *smap, *imap;
2496
2497 smap = idltest_simple2_get_smap(s, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
2498 imap = idltest_simple2_get_imap(s, OVSDB_TYPE_INTEGER, OVSDB_TYPE_STRING);
2499 printf("%03d: name=%s smap=[",
2500 step, s->name);
2501 for (i = 0; i < smap->n; i++) {
2502 printf("[%s : %s]%s", smap->keys[i].string, smap->values[i].string,
2503 i < smap->n-1? ",": "");
2504 }
2505 printf("] imap=[");
2506 for (i = 0; i < imap->n; i++) {
2507 printf("[%"PRId64" : %s]%s", imap->keys[i].integer, imap->values[i].string,
2508 i < imap->n-1? ",":"");
2509 }
2510 printf("]\n");
2511}
2512
2513static void
2514dump_simple2(struct ovsdb_idl *idl,
2515 const struct idltest_simple2 *myRow,
2516 int step)
2517{
2518 IDLTEST_SIMPLE2_FOR_EACH(myRow, idl) {
2519 print_idl_row_simple2(myRow, step);
2520 }
2521}
2522
7251075c
EA
2523static void
2524do_idl_partial_update_map_column(struct ovs_cmdl_context *ctx)
2525{
2526 struct ovsdb_idl *idl;
2527 struct ovsdb_idl_txn *myTxn;
2528 const struct idltest_simple2 *myRow;
2529 const struct ovsdb_datum *smap, *imap OVS_UNUSED;
2530 int step = 0;
2531 char key_to_delete[100];
2532
2533 idltest_init();
2534 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, false, true);
2535 ovsdb_idl_add_table(idl, &idltest_table_simple2);
2536 ovsdb_idl_add_column(idl, &idltest_simple2_col_name);
2537 ovsdb_idl_add_column(idl, &idltest_simple2_col_smap);
2538 ovsdb_idl_add_column(idl, &idltest_simple2_col_imap);
2539 ovsdb_idl_get_initial_snapshot(idl);
2540 setvbuf(stdout, NULL, _IONBF, 0);
2541 ovsdb_idl_run(idl);
2542
f1ab6e06 2543 /* Display original data in table. */
7251075c
EA
2544 myRow = NULL;
2545 printf("%03d: Getting records\n", step++);
2546 dump_simple2(idl, myRow, step++);
2547
f1ab6e06 2548 /* Insert new elements in different map columns. */
7251075c
EA
2549 myRow = idltest_simple2_first(idl);
2550 myTxn = ovsdb_idl_txn_create(idl);
6ee95363 2551 idltest_simple2_get_smap(myRow, OVSDB_TYPE_STRING,
7251075c
EA
2552 OVSDB_TYPE_STRING);
2553 idltest_simple2_update_smap_setkey(myRow, "key1", "myList1");
2554 imap = idltest_simple2_get_imap(myRow, OVSDB_TYPE_INTEGER,
2555 OVSDB_TYPE_STRING);
2556 idltest_simple2_update_imap_setkey(myRow, 3, "myids2");
2557 idltest_simple2_set_name(myRow, "String2");
2558 ovsdb_idl_txn_commit_block(myTxn);
2559 ovsdb_idl_txn_destroy(myTxn);
2560 ovsdb_idl_get_initial_snapshot(idl);
2561 printf("%03d: After insert element\n", step++);
2562 dump_simple2(idl, myRow, step++);
2563
f1ab6e06 2564 /* Insert duplicate element. */
7251075c
EA
2565 myTxn = ovsdb_idl_txn_create(idl);
2566 idltest_simple2_update_smap_setkey(myRow, "key1", "myList1");
2567 ovsdb_idl_txn_commit_block(myTxn);
2568 ovsdb_idl_txn_destroy(myTxn);
2569 ovsdb_idl_get_initial_snapshot(idl);
2570 printf("%03d: After insert duplicated element\n", step++);
2571 dump_simple2(idl, myRow, step++);
2572
f1ab6e06 2573 /* Deletes an element of a map column. */
7251075c
EA
2574 myRow = idltest_simple2_first(idl);
2575 myTxn = ovsdb_idl_txn_create(idl);
2576 smap = idltest_simple2_get_smap(myRow, OVSDB_TYPE_STRING,
2577 OVSDB_TYPE_STRING);
2578 strcpy(key_to_delete, smap->keys[0].string);
2579 idltest_simple2_update_smap_delkey(myRow, smap->keys[0].string);
2580 ovsdb_idl_txn_commit_block(myTxn);
2581 ovsdb_idl_txn_destroy(myTxn);
2582 ovsdb_idl_get_initial_snapshot(idl);
2583 printf("%03d: After delete element\n", step++);
2584 dump_simple2(idl, myRow, step++);
2585
f1ab6e06 2586 /* Try to delete a deleted element of a map column. */
7251075c
EA
2587 myTxn = ovsdb_idl_txn_create(idl);
2588 idltest_simple2_update_smap_delkey(myRow, key_to_delete);
2589 ovsdb_idl_txn_commit_block(myTxn);
2590 ovsdb_idl_txn_destroy(myTxn);
2591 ovsdb_idl_get_initial_snapshot(idl);
2592 printf("%03d: After trying to delete a deleted element\n", step++);
2593 dump_simple2(idl, myRow, step++);
2594
2595 printf("%03d: End test\n", step);
2596 return;
2597}
2598
f1ab6e06
RM
2599static void
2600print_idl_row_simple3(const struct idltest_simple3 *s, int step)
2601{
2602 size_t i;
2603 const struct ovsdb_datum *uset;
2604 const struct ovsdb_datum *uref;
2605
2606 uset = idltest_simple3_get_uset(s, OVSDB_TYPE_UUID);
2607 printf("%03d: name=%s uset=[",
2608 step, s->name);
2609 for (i = 0; i < uset->n; i++) {
2610 printf("["UUID_FMT"]%s", UUID_ARGS(&(uset->keys[i].uuid)), i < uset->n-1? ",": "");
2611 }
2612 uref = idltest_simple3_get_uref(s, OVSDB_TYPE_UUID);
2613 printf("] uref=[");
2614 for (i = 0; i < uref->n; i++) {
2615 printf("["UUID_FMT"]%s", UUID_ARGS(&(uref->keys[i].uuid)), i < uref->n-1? ",": "");
2616 }
2617 printf("]\n");
2618}
2619
2620static void
2621dump_simple3(struct ovsdb_idl *idl,
2622 const struct idltest_simple3 *myRow,
2623 int step)
2624{
2625 IDLTEST_SIMPLE3_FOR_EACH(myRow, idl) {
2626 print_idl_row_simple3(myRow, step);
2627 }
2628}
2629
2630static void
2631do_idl_partial_update_set_column(struct ovs_cmdl_context *ctx)
2632{
2633 struct ovsdb_idl *idl;
2634 struct ovsdb_idl_txn *myTxn;
2635 const struct idltest_simple3 *myRow;
2636 struct idltest_simple4 *myRow2;
2637 const struct ovsdb_datum *uset OVS_UNUSED;
2638 const struct ovsdb_datum *uref OVS_UNUSED;
2639 int step = 0;
2640
2641 idltest_init();
2642 idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, false, true);
2643 ovsdb_idl_add_table(idl, &idltest_table_simple3);
2644 ovsdb_idl_add_column(idl, &idltest_simple3_col_name);
2645 ovsdb_idl_add_column(idl, &idltest_simple3_col_uset);
2646 ovsdb_idl_add_column(idl, &idltest_simple3_col_uref);
2647 ovsdb_idl_add_table(idl, &idltest_table_simple4);
2648 ovsdb_idl_add_column(idl, &idltest_simple4_col_name);
2649 ovsdb_idl_get_initial_snapshot(idl);
2650 setvbuf(stdout, NULL, _IONBF, 0);
2651 ovsdb_idl_run(idl);
2652
2653 /* Display original data in table. */
2654 myRow = NULL;
2655 printf("%03d: Getting records\n", step++);
2656 dump_simple3(idl, myRow, step++);
2657
2658 /* Insert new elements in different map columns. */
2659 myRow = idltest_simple3_first(idl);
2660 myTxn = ovsdb_idl_txn_create(idl);
2661 idltest_simple3_get_uset(myRow, OVSDB_TYPE_UUID);
2662 struct uuid uuid_to_add;
2663 uuid_from_string(&uuid_to_add, "001e43d2-dd3f-4616-ab6a-83a490bb0991");
2664 idltest_simple3_update_uset_addvalue(myRow, uuid_to_add);
2665 idltest_simple3_set_name(myRow, "String2");
2666 ovsdb_idl_txn_commit_block(myTxn);
2667 ovsdb_idl_txn_destroy(myTxn);
2668 ovsdb_idl_get_initial_snapshot(idl);
2669 printf("%03d: After rename+add new value\n", step++);
2670 dump_simple3(idl, myRow, step++);
2671
2672 /* Insert duplicate element. */
2673 myTxn = ovsdb_idl_txn_create(idl);
2674 struct uuid uuid_to_add2;
2675 uuid_from_string(&uuid_to_add2, "0026b3ba-571b-4729-8227-d860a5210ab8");
2676 idltest_simple3_update_uset_addvalue(myRow, uuid_to_add2);
2677 ovsdb_idl_txn_commit_block(myTxn);
2678 ovsdb_idl_txn_destroy(myTxn);
2679 ovsdb_idl_get_initial_snapshot(idl);
2680 printf("%03d: After add new value\n", step++);
2681 dump_simple3(idl, myRow, step++);
2682
2683 /* Deletes an element of a set column. */
2684 myRow = idltest_simple3_first(idl);
2685 myTxn = ovsdb_idl_txn_create(idl);
2686 uset = idltest_simple3_get_uset(myRow, OVSDB_TYPE_UUID);
2687 idltest_simple3_update_uset_delvalue(myRow, uuid_to_add);
2688 ovsdb_idl_txn_commit_block(myTxn);
2689 ovsdb_idl_txn_destroy(myTxn);
2690 ovsdb_idl_get_initial_snapshot(idl);
2691 printf("%03d: After delete value\n", step++);
2692 dump_simple3(idl, myRow, step++);
2693
2694 /* Try to delete a deleted element of a map column. */
2695 myRow = idltest_simple3_first(idl);
2696 myTxn = ovsdb_idl_txn_create(idl);
2697 idltest_simple3_update_uset_delvalue(myRow, uuid_to_add);
2698 ovsdb_idl_txn_commit_block(myTxn);
2699 ovsdb_idl_txn_destroy(myTxn);
2700 ovsdb_idl_get_initial_snapshot(idl);
2701 printf("%03d: After trying to delete a deleted value\n", step++);
2702 dump_simple3(idl, myRow, step++);
2703
2704 /* Adds to a table and update a strong reference in another table. */
2705 myRow = idltest_simple3_first(idl);
2706 myTxn = ovsdb_idl_txn_create(idl);
2707 myRow2 = idltest_simple4_insert(myTxn);
2708 idltest_simple4_set_name(myRow2, "test");
2709 idltest_simple3_update_uref_addvalue(myRow, myRow2);
2710 ovsdb_idl_txn_commit_block(myTxn);
2711 ovsdb_idl_txn_destroy(myTxn);
2712 ovsdb_idl_get_initial_snapshot(idl);
2713 printf("%03d: After add to other table + set of strong ref\n", step++);
2714 dump_simple3(idl, myRow, step++);
2715 printf("%03d: End test\n", step);
2716 return;
2717}
2718
5f383751 2719static struct ovs_cmdl_command all_commands[] = {
451de37e
AW
2720 { "log-io", NULL, 2, INT_MAX, do_log_io },
2721 { "default-atoms", NULL, 0, 0, do_default_atoms },
2722 { "default-data", NULL, 0, 0, do_default_data },
1a6f1cef 2723 { "diff-data", NULL, 3, INT_MAX, do_diff_data},
451de37e
AW
2724 { "parse-atomic-type", NULL, 1, 1, do_parse_atomic_type },
2725 { "parse-base-type", NULL, 1, 1, do_parse_base_type },
2726 { "parse-type", NULL, 1, 1, do_parse_type },
2727 { "parse-atoms", NULL, 2, INT_MAX, do_parse_atoms },
2728 { "parse-atom-strings", NULL, 2, INT_MAX, do_parse_atom_strings },
2729 { "parse-data", NULL, 2, INT_MAX, do_parse_data },
2730 { "parse-data-strings", NULL, 2, INT_MAX, do_parse_data_strings },
2731 { "sort-atoms", NULL, 2, 2, do_sort_atoms },
2732 { "parse-column", NULL, 2, 2, do_parse_column },
2733 { "parse-table", NULL, 2, 3, do_parse_table },
2734 { "parse-rows", NULL, 2, INT_MAX, do_parse_rows },
2735 { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
2736 { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
2737 { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
ae9cab37
LS
2738 { "evaluate-conditions-any", NULL, 3, 3, do_evaluate_conditions_any },
2739 { "compare-conditions", NULL, 2, 2, do_compare_conditions },
451de37e
AW
2740 { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
2741 { "execute-mutations", NULL, 3, 3, do_execute_mutations },
2742 { "query", NULL, 3, 3, do_query },
2743 { "query-distinct", NULL, 4, 4, do_query_distinct },
2744 { "transact", NULL, 1, INT_MAX, do_transact },
2745 { "parse-schema", NULL, 1, 1, do_parse_schema },
2746 { "execute", NULL, 2, INT_MAX, do_execute },
e51879e9 2747 { "execute-readonly", NULL, 2, INT_MAX, do_execute_ro },
451de37e
AW
2748 { "trigger", NULL, 2, INT_MAX, do_trigger },
2749 { "idl", NULL, 1, INT_MAX, do_idl },
7251075c
EA
2750 { "idl-partial-update-map-column", NULL, 1, INT_MAX,
2751 do_idl_partial_update_map_column },
f1ab6e06
RM
2752 { "idl-partial-update-set-column", NULL, 1, INT_MAX,
2753 do_idl_partial_update_set_column },
451de37e
AW
2754 { "help", NULL, 0, INT_MAX, do_help },
2755 { NULL, NULL, 0, 0, NULL },
f85f8ebb 2756};
d2586fce 2757
5f383751 2758static struct ovs_cmdl_command *
d2586fce
GS
2759get_all_commands(void)
2760{
2761 return all_commands;
2762}