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