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