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