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