]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovsdb-data.c
debian: Modify ovs-bugtool to include collectd rrd logs
[mirror_ovs.git] / lib / ovsdb-data.c
CommitLineData
c532bf9d 1/* Copyright (c) 2009, 2010 Nicira Networks
f85f8ebb
BP
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <config.h>
17
18#include "ovsdb-data.h"
19
20#include <assert.h>
0194f33a
BP
21#include <ctype.h>
22#include <float.h>
23#include <inttypes.h>
e9f8f936 24#include <limits.h>
f85f8ebb 25
0194f33a 26#include "dynamic-string.h"
f85f8ebb
BP
27#include "hash.h"
28#include "ovsdb-error.h"
29#include "json.h"
30#include "shash.h"
31#include "sort.h"
bd76d25d 32#include "unicode.h"
f85f8ebb
BP
33
34static struct json *
35wrap_json(const char *name, struct json *wrapped)
36{
37 return json_array_create_2(json_string_create(name), wrapped);
38}
39
5413de95
BP
40/* Initializes 'atom' with the default value of the given 'type'.
41 *
42 * The default value for an atom is as defined in ovsdb/SPECS:
43 *
44 * - "integer" or "real": 0
45 *
46 * - "boolean": false
47 *
48 * - "string": "" (the empty string)
49 *
50 * - "uuid": 00000000-0000-0000-0000-000000000000
51 *
52 * The caller must eventually arrange for 'atom' to be destroyed (with
53 * ovsdb_atom_destroy()). */
f85f8ebb
BP
54void
55ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
56{
57 switch (type) {
58 case OVSDB_TYPE_VOID:
59 NOT_REACHED();
60
61 case OVSDB_TYPE_INTEGER:
62 atom->integer = 0;
63 break;
64
65 case OVSDB_TYPE_REAL:
66 atom->real = 0.0;
67 break;
68
69 case OVSDB_TYPE_BOOLEAN:
70 atom->boolean = false;
71 break;
72
73 case OVSDB_TYPE_STRING:
74 atom->string = xmemdup("", 1);
75 break;
76
77 case OVSDB_TYPE_UUID:
78 uuid_zero(&atom->uuid);
79 break;
80
81 case OVSDB_N_TYPES:
82 default:
83 NOT_REACHED();
84 }
85}
86
958ac03a
BP
87/* Returns a read-only atom of the given 'type' that has the default value for
88 * 'type'. The caller must not modify or free the returned atom.
89 *
90 * See ovsdb_atom_init_default() for an explanation of the default value of an
91 * atom. */
92const union ovsdb_atom *
93ovsdb_atom_default(enum ovsdb_atomic_type type)
94{
95 static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
96 static bool inited;
97
98 if (!inited) {
99 int i;
100
101 for (i = 0; i < OVSDB_N_TYPES; i++) {
102 if (i != OVSDB_TYPE_VOID) {
103 ovsdb_atom_init_default(&default_atoms[i], i);
104 }
105 }
106 inited = true;
107 }
108
109 assert(ovsdb_atomic_type_is_valid(type));
110 return &default_atoms[type];
111}
5413de95
BP
112
113/* Returns true if 'atom', which must have the given 'type', has the default
114 * value for that type.
115 *
116 * See ovsdb_atom_init_default() for an explanation of the default value of an
117 * atom. */
c532bf9d
BP
118bool
119ovsdb_atom_is_default(const union ovsdb_atom *atom,
120 enum ovsdb_atomic_type type)
121{
122 switch (type) {
123 case OVSDB_TYPE_VOID:
124 NOT_REACHED();
125
126 case OVSDB_TYPE_INTEGER:
127 return atom->integer == 0;
128
129 case OVSDB_TYPE_REAL:
130 return atom->real == 0.0;
131
132 case OVSDB_TYPE_BOOLEAN:
133 return atom->boolean == false;
134
135 case OVSDB_TYPE_STRING:
136 return atom->string[0] == '\0';
137
138 case OVSDB_TYPE_UUID:
139 return uuid_is_zero(&atom->uuid);
140
141 case OVSDB_N_TYPES:
142 default:
143 NOT_REACHED();
144 }
145}
146
5413de95
BP
147/* Initializes 'new' as a copy of 'old', with the given 'type'.
148 *
149 * The caller must eventually arrange for 'new' to be destroyed (with
150 * ovsdb_atom_destroy()). */
f85f8ebb
BP
151void
152ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
153 enum ovsdb_atomic_type type)
154{
155 switch (type) {
156 case OVSDB_TYPE_VOID:
157 NOT_REACHED();
158
159 case OVSDB_TYPE_INTEGER:
160 new->integer = old->integer;
161 break;
162
163 case OVSDB_TYPE_REAL:
164 new->real = old->real;
165 break;
166
167 case OVSDB_TYPE_BOOLEAN:
168 new->boolean = old->boolean;
169 break;
170
171 case OVSDB_TYPE_STRING:
172 new->string = xstrdup(old->string);
173 break;
174
175 case OVSDB_TYPE_UUID:
176 new->uuid = old->uuid;
177 break;
178
179 case OVSDB_N_TYPES:
180 default:
181 NOT_REACHED();
182 }
183}
184
5413de95 185/* Swaps the contents of 'a' and 'b', which need not have the same type. */
f85f8ebb
BP
186void
187ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
188{
189 union ovsdb_atom tmp = *a;
190 *a = *b;
191 *b = tmp;
192}
193
5413de95
BP
194/* Returns a hash value for 'atom', which has the specified 'type', folding
195 * 'basis' into the calculation. */
f85f8ebb
BP
196uint32_t
197ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
198 uint32_t basis)
199{
200 switch (type) {
201 case OVSDB_TYPE_VOID:
202 NOT_REACHED();
203
204 case OVSDB_TYPE_INTEGER:
205 return hash_int(atom->integer, basis);
206
207 case OVSDB_TYPE_REAL:
208 return hash_double(atom->real, basis);
209
210 case OVSDB_TYPE_BOOLEAN:
211 return hash_boolean(atom->boolean, basis);
212
213 case OVSDB_TYPE_STRING:
214 return hash_string(atom->string, basis);
215
216 case OVSDB_TYPE_UUID:
217 return hash_int(uuid_hash(&atom->uuid), basis);
218
219 case OVSDB_N_TYPES:
220 default:
221 NOT_REACHED();
222 }
223}
224
5413de95
BP
225/* Compares 'a' and 'b', which both have type 'type', and returns a
226 * strcmp()-like result. */
f85f8ebb
BP
227int
228ovsdb_atom_compare_3way(const union ovsdb_atom *a,
229 const union ovsdb_atom *b,
230 enum ovsdb_atomic_type type)
231{
232 switch (type) {
233 case OVSDB_TYPE_VOID:
234 NOT_REACHED();
235
236 case OVSDB_TYPE_INTEGER:
237 return a->integer < b->integer ? -1 : a->integer > b->integer;
238
239 case OVSDB_TYPE_REAL:
240 return a->real < b->real ? -1 : a->real > b->real;
241
242 case OVSDB_TYPE_BOOLEAN:
243 return a->boolean - b->boolean;
244
245 case OVSDB_TYPE_STRING:
246 return strcmp(a->string, b->string);
247
248 case OVSDB_TYPE_UUID:
249 return uuid_compare_3way(&a->uuid, &b->uuid);
250
251 case OVSDB_N_TYPES:
252 default:
253 NOT_REACHED();
254 }
255}
256
257static struct ovsdb_error *
258unwrap_json(const struct json *json, const char *name,
259 enum json_type value_type, const struct json **value)
260{
261 if (json->type != JSON_ARRAY
262 || json->u.array.n != 2
263 || json->u.array.elems[0]->type != JSON_STRING
264 || (name && strcmp(json->u.array.elems[0]->u.string, name))
265 || json->u.array.elems[1]->type != value_type)
266 {
267 return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
268 json_type_to_string(value_type));
269 }
270 *value = json->u.array.elems[1];
271 return NULL;
272}
273
274static struct ovsdb_error *
275parse_json_pair(const struct json *json,
276 const struct json **elem0, const struct json **elem1)
277{
278 if (json->type != JSON_ARRAY || json->u.array.n != 2) {
279 return ovsdb_syntax_error(json, NULL, "expected 2-element array");
280 }
281 *elem0 = json->u.array.elems[0];
282 *elem1 = json->u.array.elems[1];
283 return NULL;
284}
285
fbf925e4 286static struct ovsdb_error * WARN_UNUSED_RESULT
f85f8ebb 287ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
fbf925e4 288 struct ovsdb_symbol_table *symtab)
f85f8ebb
BP
289{
290 struct ovsdb_error *error0;
291 const struct json *value;
292
293 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
294 if (!error0) {
295 const char *uuid_string = json_string(value);
296 if (!uuid_from_string(uuid, uuid_string)) {
297 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
298 uuid_string);
299 }
300 } else if (symtab) {
301 struct ovsdb_error *error1;
302
303 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
304 if (!error1) {
305 const char *name = json_string(value);
f85f8ebb
BP
306
307 ovsdb_error_destroy(error0);
fbf925e4
BP
308 *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
309 return NULL;
f85f8ebb
BP
310 }
311 ovsdb_error_destroy(error1);
312 }
313
314 return error0;
315}
316
bd76d25d
BP
317static struct ovsdb_error * WARN_UNUSED_RESULT
318ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
319 const struct json *json,
fbf925e4 320 struct ovsdb_symbol_table *symtab)
f85f8ebb
BP
321{
322 switch (type) {
323 case OVSDB_TYPE_VOID:
324 NOT_REACHED();
325
326 case OVSDB_TYPE_INTEGER:
327 if (json->type == JSON_INTEGER) {
328 atom->integer = json->u.integer;
329 return NULL;
330 }
331 break;
332
333 case OVSDB_TYPE_REAL:
334 if (json->type == JSON_INTEGER) {
335 atom->real = json->u.integer;
336 return NULL;
337 } else if (json->type == JSON_REAL) {
338 atom->real = json->u.real;
339 return NULL;
340 }
341 break;
342
343 case OVSDB_TYPE_BOOLEAN:
344 if (json->type == JSON_TRUE) {
345 atom->boolean = true;
346 return NULL;
347 } else if (json->type == JSON_FALSE) {
348 atom->boolean = false;
349 return NULL;
350 }
351 break;
352
353 case OVSDB_TYPE_STRING:
354 if (json->type == JSON_STRING) {
355 atom->string = xstrdup(json->u.string);
356 return NULL;
357 }
358 break;
359
360 case OVSDB_TYPE_UUID:
361 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
362
363 case OVSDB_N_TYPES:
364 default:
365 NOT_REACHED();
366 }
367
368 return ovsdb_syntax_error(json, NULL, "expected %s",
369 ovsdb_atomic_type_to_string(type));
370}
371
5413de95
BP
372/* Parses 'json' as an atom of the type described by 'base'. If successful,
373 * returns NULL and initializes 'atom' with the parsed atom. On failure,
374 * returns an error and the contents of 'atom' are indeterminate. The caller
375 * is responsible for freeing the error or the atom that is returned.
376 *
958ac03a
BP
377 * Violations of constraints expressed by 'base' are treated as errors.
378 *
5413de95 379 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
958ac03a
BP
380 * ovsdb/SPECS for information about this, and for the syntax that this
381 * function accepts. */
bd76d25d
BP
382struct ovsdb_error *
383ovsdb_atom_from_json(union ovsdb_atom *atom,
384 const struct ovsdb_base_type *base,
385 const struct json *json,
fbf925e4 386 struct ovsdb_symbol_table *symtab)
bd76d25d
BP
387{
388 struct ovsdb_error *error;
389
390 error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
391 if (error) {
392 return error;
393 }
394
395 error = ovsdb_atom_check_constraints(atom, base);
396 if (error) {
397 ovsdb_atom_destroy(atom, base->type);
398 }
399 return error;
400}
401
5413de95 402/* Converts 'atom', of the specified 'type', to JSON format, and returns the
958ac03a
BP
403 * JSON. The caller is responsible for freeing the returned JSON.
404 *
405 * Refer to ovsdb/SPECS for the format of the JSON that this function
406 * produces. */
f85f8ebb
BP
407struct json *
408ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
409{
410 switch (type) {
411 case OVSDB_TYPE_VOID:
412 NOT_REACHED();
413
414 case OVSDB_TYPE_INTEGER:
415 return json_integer_create(atom->integer);
416
417 case OVSDB_TYPE_REAL:
418 return json_real_create(atom->real);
419
420 case OVSDB_TYPE_BOOLEAN:
421 return json_boolean_create(atom->boolean);
422
423 case OVSDB_TYPE_STRING:
424 return json_string_create(atom->string);
425
426 case OVSDB_TYPE_UUID:
427 return wrap_json("uuid", json_string_create_nocopy(
428 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
429
430 case OVSDB_N_TYPES:
431 default:
432 NOT_REACHED();
433 }
434}
0194f33a 435
bd76d25d
BP
436static char *
437ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
ce5a3e38 438 const char *s, struct ovsdb_symbol_table *symtab)
0194f33a
BP
439{
440 switch (type) {
441 case OVSDB_TYPE_VOID:
442 NOT_REACHED();
443
444 case OVSDB_TYPE_INTEGER: {
445 long long int integer;
446 if (!str_to_llong(s, 10, &integer)) {
1bc6ff29 447 return xasprintf("\"%s\" is not a valid integer", s);
0194f33a
BP
448 }
449 atom->integer = integer;
450 }
451 break;
452
453 case OVSDB_TYPE_REAL:
454 if (!str_to_double(s, &atom->real)) {
1bc6ff29 455 return xasprintf("\"%s\" is not a valid real number", s);
0194f33a 456 }
54a687fd
BP
457 /* Our JSON input routines map negative zero to zero, so do that here
458 * too for consistency. */
459 if (atom->real == 0.0) {
460 atom->real = 0.0;
461 }
0194f33a
BP
462 break;
463
464 case OVSDB_TYPE_BOOLEAN:
465 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
466 || !strcmp(s, "1")) {
467 atom->boolean = true;
468 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
469 || !strcmp(s, "0")) {
470 atom->boolean = false;
471 } else {
1bc6ff29
BP
472 return xasprintf("\"%s\" is not a valid boolean "
473 "(use \"true\" or \"false\")", s);
0194f33a
BP
474 }
475 break;
476
477 case OVSDB_TYPE_STRING:
478 if (*s == '\0') {
1bc6ff29
BP
479 return xstrdup("An empty string is not valid as input; "
480 "use \"\" to represent the empty string");
0194f33a
BP
481 } else if (*s == '"') {
482 size_t s_len = strlen(s);
483
484 if (s_len < 2 || s[s_len - 1] != '"') {
1bc6ff29
BP
485 return xasprintf("%s: missing quote at end of "
486 "quoted string", s);
0194f33a
BP
487 } else if (!json_string_unescape(s + 1, s_len - 2,
488 &atom->string)) {
1bc6ff29
BP
489 char *error = xasprintf("%s: %s", s, atom->string);
490 free(atom->string);
491 return error;
0194f33a
BP
492 }
493 } else {
494 atom->string = xstrdup(s);
495 }
496 break;
497
498 case OVSDB_TYPE_UUID:
ce5a3e38
BP
499 if (*s == '@') {
500 atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
501 } else if (!uuid_from_string(&atom->uuid, s)) {
1bc6ff29 502 return xasprintf("\"%s\" is not a valid UUID", s);
0194f33a
BP
503 }
504 break;
505
506 case OVSDB_N_TYPES:
507 default:
508 NOT_REACHED();
509 }
1bc6ff29
BP
510
511 return NULL;
0194f33a
BP
512}
513
bd76d25d
BP
514/* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
515 * one of the following forms:
516 *
517 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
518 *
519 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
520 * strtod().
521 *
522 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
523 * "no", "off", or "0" for false.
524 *
525 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
526 * an arbitrary string.
527 *
ce5a3e38
BP
528 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
529 * then an identifier beginning with '@' is also acceptable. If the
530 * named identifier is already in 'symtab', then the associated UUID is
531 * used; otherwise, a new, random UUID is used and added to the symbol
532 * table.
bd76d25d
BP
533 *
534 * Returns a null pointer if successful, otherwise an error message describing
5413de95
BP
535 * the problem. On failure, the contents of 'atom' are indeterminate. The
536 * caller is responsible for freeing the atom or the error.
bd76d25d
BP
537 */
538char *
539ovsdb_atom_from_string(union ovsdb_atom *atom,
ce5a3e38
BP
540 const struct ovsdb_base_type *base, const char *s,
541 struct ovsdb_symbol_table *symtab)
bd76d25d
BP
542{
543 struct ovsdb_error *error;
544 char *msg;
545
ce5a3e38 546 msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
bd76d25d
BP
547 if (msg) {
548 return msg;
549 }
550
551 error = ovsdb_atom_check_constraints(atom, base);
552 if (error) {
553 msg = ovsdb_error_to_string(error);
554 ovsdb_error_destroy(error);
555 }
556 return msg;
557}
558
0194f33a
BP
559static bool
560string_needs_quotes(const char *s)
561{
562 const char *p = s;
563 unsigned char c;
564
565 c = *p++;
566 if (!isalpha(c) && c != '_') {
567 return true;
568 }
569
570 while ((c = *p++) != '\0') {
571 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
572 return true;
573 }
574 }
575
576 if (!strcmp(s, "true") || !strcmp(s, "false")) {
577 return true;
578 }
579
580 return false;
581}
582
583/* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
584 * to ovsdb_atom_from_string(). */
585void
586ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
587 struct ds *out)
588{
589 switch (type) {
590 case OVSDB_TYPE_VOID:
591 NOT_REACHED();
592
593 case OVSDB_TYPE_INTEGER:
594 ds_put_format(out, "%"PRId64, atom->integer);
595 break;
596
597 case OVSDB_TYPE_REAL:
598 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
599 break;
600
601 case OVSDB_TYPE_BOOLEAN:
602 ds_put_cstr(out, atom->boolean ? "true" : "false");
603 break;
604
605 case OVSDB_TYPE_STRING:
606 if (string_needs_quotes(atom->string)) {
607 struct json json;
608
609 json.type = JSON_STRING;
610 json.u.string = atom->string;
611 json_to_ds(&json, 0, out);
612 } else {
613 ds_put_cstr(out, atom->string);
614 }
615 break;
616
617 case OVSDB_TYPE_UUID:
618 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
619 break;
620
621 case OVSDB_N_TYPES:
622 default:
623 NOT_REACHED();
624 }
625}
bd76d25d
BP
626
627static struct ovsdb_error *
628check_string_constraints(const char *s,
629 const struct ovsdb_string_constraints *c)
630{
631 size_t n_chars;
632 char *msg;
633
634 msg = utf8_validate(s, &n_chars);
635 if (msg) {
636 struct ovsdb_error *error;
637
638 error = ovsdb_error("constraint violation",
639 "\"%s\" is not a valid UTF-8 string: %s",
640 s, msg);
641 free(msg);
642 return error;
643 }
644
645 if (n_chars < c->minLen) {
646 return ovsdb_error(
647 "constraint violation",
648 "\"%s\" length %zu is less than minimum allowed "
649 "length %u", s, n_chars, c->minLen);
650 } else if (n_chars > c->maxLen) {
651 return ovsdb_error(
652 "constraint violation",
653 "\"%s\" length %zu is greater than maximum allowed "
654 "length %u", s, n_chars, c->maxLen);
655 }
656
bd76d25d
BP
657 return NULL;
658}
659
660/* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
661 * (base->type must specify 'atom''s type.) Returns a null pointer if the
0d0f05b9
BP
662 * constraints are met, otherwise an error that explains the violation.
663 *
664 * Checking UUID constraints is deferred to transaction commit time, so this
665 * function does nothing for UUID constraints. */
bd76d25d
BP
666struct ovsdb_error *
667ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
668 const struct ovsdb_base_type *base)
669{
bfc96d9b
BP
670 if (base->enum_
671 && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
672 struct ovsdb_error *error;
673 struct ds actual = DS_EMPTY_INITIALIZER;
674 struct ds valid = DS_EMPTY_INITIALIZER;
675
676 ovsdb_atom_to_string(atom, base->type, &actual);
677 ovsdb_datum_to_string(base->enum_,
678 ovsdb_base_type_get_enum_type(base->type),
679 &valid);
680 error = ovsdb_error("constraint violation",
681 "%s is not one of the allowed values (%s)",
682 ds_cstr(&actual), ds_cstr(&valid));
683 ds_destroy(&actual);
684 ds_destroy(&valid);
685
686 return error;
687 }
688
bd76d25d
BP
689 switch (base->type) {
690 case OVSDB_TYPE_VOID:
691 NOT_REACHED();
692
693 case OVSDB_TYPE_INTEGER:
694 if (atom->integer >= base->u.integer.min
695 && atom->integer <= base->u.integer.max) {
696 return NULL;
697 } else if (base->u.integer.min != INT64_MIN) {
698 if (base->u.integer.max != INT64_MAX) {
699 return ovsdb_error("constraint violation",
700 "%"PRId64" is not in the valid range "
701 "%"PRId64" to %"PRId64" (inclusive)",
702 atom->integer,
703 base->u.integer.min, base->u.integer.max);
704 } else {
705 return ovsdb_error("constraint violation",
706 "%"PRId64" is less than minimum allowed "
707 "value %"PRId64,
708 atom->integer, base->u.integer.min);
709 }
710 } else {
711 return ovsdb_error("constraint violation",
712 "%"PRId64" is greater than maximum allowed "
713 "value %"PRId64,
714 atom->integer, base->u.integer.max);
715 }
716 NOT_REACHED();
717
718 case OVSDB_TYPE_REAL:
719 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
720 return NULL;
721 } else if (base->u.real.min != -DBL_MAX) {
722 if (base->u.real.max != DBL_MAX) {
723 return ovsdb_error("constraint violation",
724 "%.*g is not in the valid range "
725 "%.*g to %.*g (inclusive)",
726 DBL_DIG, atom->real,
727 DBL_DIG, base->u.real.min,
728 DBL_DIG, base->u.real.max);
729 } else {
730 return ovsdb_error("constraint violation",
731 "%.*g is less than minimum allowed "
732 "value %.*g",
733 DBL_DIG, atom->real,
734 DBL_DIG, base->u.real.min);
735 }
736 } else {
737 return ovsdb_error("constraint violation",
738 "%.*g is greater than maximum allowed "
739 "value %.*g",
740 DBL_DIG, atom->real,
741 DBL_DIG, base->u.real.max);
742 }
743 NOT_REACHED();
744
745 case OVSDB_TYPE_BOOLEAN:
746 return NULL;
747
748 case OVSDB_TYPE_STRING:
749 return check_string_constraints(atom->string, &base->u.string);
750
751 case OVSDB_TYPE_UUID:
752 return NULL;
753
754 case OVSDB_N_TYPES:
755 default:
756 NOT_REACHED();
757 }
758}
f85f8ebb
BP
759\f
760static union ovsdb_atom *
761alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
762{
763 if (type != OVSDB_TYPE_VOID && n) {
764 union ovsdb_atom *atoms;
765 unsigned int i;
766
767 atoms = xmalloc(n * sizeof *atoms);
768 for (i = 0; i < n; i++) {
769 ovsdb_atom_init_default(&atoms[i], type);
770 }
771 return atoms;
772 } else {
773 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
774 * treated as xmalloc(1). */
775 return NULL;
776 }
777}
778
5413de95
BP
779/* Initializes 'datum' as an empty datum. (An empty datum can be treated as
780 * any type.) */
2f47998b
BP
781void
782ovsdb_datum_init_empty(struct ovsdb_datum *datum)
783{
784 datum->n = 0;
785 datum->keys = NULL;
786 datum->values = NULL;
787}
788
5413de95
BP
789/* Initializes 'datum' as a datum that has the default value for 'type'.
790 *
791 * The default value for a particular type is as defined in ovsdb/SPECS:
792 *
793 * - If n_min is 0, then the default value is the empty set (or map).
794 *
795 * - If n_min is 1, the default value is a single value or a single
796 * key-value pair, whose key and value are the defaults for their
797 * atomic types. (See ovsdb_atom_init_default() for details.)
798 *
799 * - n_min > 1 is invalid. See ovsdb_type_is_valid().
800 */
f85f8ebb
BP
801void
802ovsdb_datum_init_default(struct ovsdb_datum *datum,
803 const struct ovsdb_type *type)
804{
805 datum->n = type->n_min;
bd76d25d
BP
806 datum->keys = alloc_default_atoms(type->key.type, datum->n);
807 datum->values = alloc_default_atoms(type->value.type, datum->n);
f85f8ebb
BP
808}
809
958ac03a
BP
810/* Returns a read-only datum of the given 'type' that has the default value for
811 * 'type'. The caller must not modify or free the returned datum.
812 *
813 * See ovsdb_datum_init_default() for an explanation of the default value of a
814 * datum. */
815const struct ovsdb_datum *
816ovsdb_datum_default(const struct ovsdb_type *type)
817{
818 if (type->n_min == 0) {
819 static const struct ovsdb_datum empty;
820 return &empty;
821 } else if (type->n_min == 1) {
822 static struct ovsdb_datum default_data[OVSDB_N_TYPES][OVSDB_N_TYPES];
823 struct ovsdb_datum *d;
824 int kt = type->key.type;
825 int vt = type->value.type;
826
827 assert(ovsdb_type_is_valid(type));
828
829 d = &default_data[kt][vt];
830 if (!d->n) {
831 d->n = 1;
832 d->keys = (union ovsdb_atom *) ovsdb_atom_default(kt);
833 if (vt != OVSDB_TYPE_VOID) {
834 d->values = (union ovsdb_atom *) ovsdb_atom_default(vt);
835 }
836 }
837 return d;
838 } else {
839 NOT_REACHED();
840 }
841}
842
5413de95
BP
843/* Returns true if 'datum', which must have the given 'type', has the default
844 * value for that type.
845 *
846 * See ovsdb_datum_init_default() for an explanation of the default value of a
847 * datum. */
c532bf9d
BP
848bool
849ovsdb_datum_is_default(const struct ovsdb_datum *datum,
850 const struct ovsdb_type *type)
851{
852 size_t i;
853
854 if (datum->n != type->n_min) {
855 return false;
856 }
857 for (i = 0; i < datum->n; i++) {
bd76d25d 858 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
c532bf9d
BP
859 return false;
860 }
bd76d25d
BP
861 if (type->value.type != OVSDB_TYPE_VOID
862 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
c532bf9d
BP
863 return false;
864 }
865 }
866
867 return true;
868}
869
f85f8ebb
BP
870static union ovsdb_atom *
871clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
872{
873 if (type != OVSDB_TYPE_VOID && n) {
874 union ovsdb_atom *new;
875 unsigned int i;
876
877 new = xmalloc(n * sizeof *new);
878 for (i = 0; i < n; i++) {
879 ovsdb_atom_clone(&new[i], &old[i], type);
880 }
881 return new;
882 } else {
883 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
884 * treated as xmalloc(1). */
885 return NULL;
886 }
887}
888
5413de95
BP
889/* Initializes 'new' as a copy of 'old', with the given 'type'.
890 *
891 * The caller must eventually arrange for 'new' to be destroyed (with
892 * ovsdb_datum_destroy()). */
f85f8ebb
BP
893void
894ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
895 const struct ovsdb_type *type)
896{
897 unsigned int n = old->n;
898 new->n = n;
bd76d25d
BP
899 new->keys = clone_atoms(old->keys, type->key.type, n);
900 new->values = clone_atoms(old->values, type->value.type, n);
f85f8ebb
BP
901}
902
903static void
904free_data(enum ovsdb_atomic_type type,
905 union ovsdb_atom *atoms, size_t n_atoms)
906{
907 if (ovsdb_atom_needs_destruction(type)) {
908 unsigned int i;
909 for (i = 0; i < n_atoms; i++) {
910 ovsdb_atom_destroy(&atoms[i], type);
911 }
912 }
913 free(atoms);
914}
915
5413de95
BP
916/* Frees the data owned by 'datum', which must have the given 'type'.
917 *
918 * This does not actually call free(datum). If necessary, the caller must be
919 * responsible for that. */
f85f8ebb
BP
920void
921ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
922{
bd76d25d
BP
923 free_data(type->key.type, datum->keys, datum->n);
924 free_data(type->value.type, datum->values, datum->n);
f85f8ebb
BP
925}
926
5413de95 927/* Swaps the contents of 'a' and 'b', which need not have the same type. */
f85f8ebb
BP
928void
929ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
930{
931 struct ovsdb_datum tmp = *a;
932 *a = *b;
933 *b = tmp;
934}
935
936struct ovsdb_datum_sort_cbdata {
bfc96d9b 937 enum ovsdb_atomic_type key_type;
2b66469b 938 enum ovsdb_atomic_type value_type;
f85f8ebb
BP
939 struct ovsdb_datum *datum;
940};
941
942static int
943ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
944{
945 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
2b66469b 946 int retval;
f85f8ebb 947
2b66469b
BP
948 retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
949 &cbdata->datum->keys[b],
950 cbdata->key_type);
951 if (retval || cbdata->value_type == OVSDB_TYPE_VOID) {
952 return retval;
953 }
954
955 return ovsdb_atom_compare_3way(&cbdata->datum->values[a],
956 &cbdata->datum->values[b],
957 cbdata->value_type);
f85f8ebb
BP
958}
959
960static void
961ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
962{
963 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
964
965 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
bfc96d9b 966 if (cbdata->datum->values) {
f85f8ebb
BP
967 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
968 }
969}
970
2b66469b
BP
971static void
972ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type,
973 enum ovsdb_atomic_type value_type)
974{
975 struct ovsdb_datum_sort_cbdata cbdata;
976
977 cbdata.key_type = key_type;
978 cbdata.value_type = value_type;
979 cbdata.datum = datum;
980 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
981 &cbdata);
982}
983
5413de95
BP
984/* The keys in an ovsdb_datum must be unique and in sorted order. Most
985 * functions that modify an ovsdb_datum maintain these invariants. For those
986 * that don't, this function checks and restores these invariants for 'datum',
987 * whose keys are of type 'key_type'.
988 *
989 * This function returns NULL if successful, otherwise an error message. The
990 * caller must free the returned error when it is no longer needed. On error,
991 * 'datum' is sorted but not unique. */
e9f8f936 992struct ovsdb_error *
bfc96d9b 993ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
f85f8ebb 994{
2b66469b
BP
995 size_t i;
996
f85f8ebb
BP
997 if (datum->n < 2) {
998 return NULL;
2b66469b
BP
999 }
1000
1001 ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID);
1002
1003 for (i = 0; i < datum->n - 1; i++) {
1004 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
1005 key_type)) {
1006 if (datum->values) {
1007 return ovsdb_error(NULL, "map contains duplicate key");
1008 } else {
1009 return ovsdb_error(NULL, "set contains duplicate");
f85f8ebb
BP
1010 }
1011 }
f85f8ebb 1012 }
2b66469b 1013 return NULL;
f85f8ebb
BP
1014}
1015
5413de95
BP
1016/* This function is the same as ovsdb_datum_sort(), except that the caller
1017 * knows that 'datum' is unique. The operation therefore "cannot fail", so
1018 * this function assert-fails if it actually does. */
bfc96d9b
BP
1019void
1020ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
1021 enum ovsdb_atomic_type key_type)
1022{
1023 struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
1024 if (error) {
1025 NOT_REACHED();
1026 }
1027}
1028
2b66469b
BP
1029/* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys
1030 * instead of reporting an error. In a map type, the smallest value among a
1031 * group of duplicate pairs is retained and the others are dropped.
1032 *
1033 * Returns the number of keys (or pairs) that were dropped. */
1034size_t
1035ovsdb_datum_sort_unique(struct ovsdb_datum *datum,
1036 enum ovsdb_atomic_type key_type,
1037 enum ovsdb_atomic_type value_type)
1038{
1039 size_t src, dst;
1040
1041 if (datum->n < 2) {
1042 return 0;
1043 }
1044
1045 ovsdb_datum_sort__(datum, key_type, value_type);
1046
1047 dst = 1;
1048 for (src = 1; src < datum->n; src++) {
1049 if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1],
1050 key_type)) {
1051 ovsdb_atom_destroy(&datum->keys[src], key_type);
1052 if (value_type != OVSDB_TYPE_VOID) {
1053 ovsdb_atom_destroy(&datum->values[src], value_type);
1054 }
1055 } else {
1056 if (src != dst) {
1057 datum->keys[dst] = datum->keys[src];
1058 if (value_type != OVSDB_TYPE_VOID) {
1059 datum->values[dst] = datum->values[src];
1060 }
1061 }
1062 dst++;
1063 }
1064 }
1065 datum->n = dst;
1066 return datum->n - src;
1067}
1068
bd76d25d
BP
1069/* Checks that each of the atoms in 'datum' conforms to the constraints
1070 * specified by its 'type'. Returns an error if a constraint is violated,
1071 * otherwise a null pointer.
1072 *
1073 * This function is not commonly useful because the most ordinary way to obtain
1074 * a datum is ultimately via ovsdb_atom_from_string() or
1075 * ovsdb_atom_from_json(), which check constraints themselves. */
1076struct ovsdb_error *
1077ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
1078 const struct ovsdb_type *type)
1079{
1080 struct ovsdb_error *error;
1081 unsigned int i;
1082
1083 for (i = 0; i < datum->n; i++) {
1084 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
1085 if (error) {
1086 return error;
1087 }
1088 }
1089
1090 if (type->value.type != OVSDB_TYPE_VOID) {
1091 for (i = 0; i < datum->n; i++) {
1092 error = ovsdb_atom_check_constraints(&datum->values[i],
1093 &type->value);
1094 if (error) {
1095 return error;
1096 }
1097 }
1098 }
1099
1100 return NULL;
1101}
1102
2b66469b
BP
1103static struct ovsdb_error *
1104ovsdb_datum_from_json__(struct ovsdb_datum *datum,
1105 const struct ovsdb_type *type,
1106 const struct json *json,
1107 struct ovsdb_symbol_table *symtab)
f85f8ebb
BP
1108{
1109 struct ovsdb_error *error;
1110
ae8f13e2
BP
1111 if (ovsdb_type_is_map(type)
1112 || (json->type == JSON_ARRAY
1113 && json->u.array.n > 0
1114 && json->u.array.elems[0]->type == JSON_STRING
1115 && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
f85f8ebb
BP
1116 bool is_map = ovsdb_type_is_map(type);
1117 const char *class = is_map ? "map" : "set";
1118 const struct json *inner;
1119 unsigned int i;
1120 size_t n;
1121
f85f8ebb
BP
1122 error = unwrap_json(json, class, JSON_ARRAY, &inner);
1123 if (error) {
1124 return error;
1125 }
1126
1127 n = inner->u.array.n;
1128 if (n < type->n_min || n > type->n_max) {
1129 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
1130 "%u members but %zu are present",
1131 class, type->n_min, type->n_max, n);
1132 }
1133
1134 datum->n = 0;
1135 datum->keys = xmalloc(n * sizeof *datum->keys);
1136 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
1137 for (i = 0; i < n; i++) {
1138 const struct json *element = inner->u.array.elems[i];
1139 const struct json *key = NULL;
1140 const struct json *value = NULL;
1141
1142 if (!is_map) {
1143 key = element;
1144 } else {
1145 error = parse_json_pair(element, &key, &value);
1146 if (error) {
1147 goto error;
1148 }
1149 }
1150
bd76d25d 1151 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
f85f8ebb
BP
1152 key, symtab);
1153 if (error) {
1154 goto error;
1155 }
1156
1157 if (is_map) {
1158 error = ovsdb_atom_from_json(&datum->values[i],
bd76d25d 1159 &type->value, value, symtab);
f85f8ebb 1160 if (error) {
bd76d25d 1161 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
f85f8ebb
BP
1162 goto error;
1163 }
1164 }
1165
1166 datum->n++;
1167 }
f85f8ebb
BP
1168 return NULL;
1169
1170 error:
1171 ovsdb_datum_destroy(datum, type);
1172 return error;
ae8f13e2
BP
1173 } else {
1174 datum->n = 1;
1175 datum->keys = xmalloc(sizeof *datum->keys);
1176 datum->values = NULL;
1177
1178 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
1179 json, symtab);
1180 if (error) {
1181 free(datum->keys);
1182 }
1183 return error;
f85f8ebb
BP
1184 }
1185}
1186
2b66469b
BP
1187/* Parses 'json' as a datum of the type described by 'type'. If successful,
1188 * returns NULL and initializes 'datum' with the parsed datum. On failure,
1189 * returns an error and the contents of 'datum' are indeterminate. The caller
1190 * is responsible for freeing the error or the datum that is returned.
1191 *
1192 * Violations of constraints expressed by 'type' are treated as errors.
1193 *
1194 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
1195 * ovsdb/SPECS for information about this, and for the syntax that this
1196 * function accepts. */
1197struct ovsdb_error *
1198ovsdb_datum_from_json(struct ovsdb_datum *datum,
1199 const struct ovsdb_type *type,
1200 const struct json *json,
1201 struct ovsdb_symbol_table *symtab)
1202{
1203 struct ovsdb_error *error;
1204
1205 error = ovsdb_datum_from_json__(datum, type, json, symtab);
1206 if (error) {
1207 return error;
1208 }
1209
1210 error = ovsdb_datum_sort(datum, type->key.type);
1211 if (error) {
1212 ovsdb_datum_destroy(datum, type);
1213 }
1214 return error;
1215}
1216
958ac03a
BP
1217/* Converts 'datum', of the specified 'type', to JSON format, and returns the
1218 * JSON. The caller is responsible for freeing the returned JSON.
1219 *
1220 * 'type' constraints on datum->n are ignored.
1221 *
1222 * Refer to ovsdb/SPECS for the format of the JSON that this function
1223 * produces. */
f85f8ebb
BP
1224struct json *
1225ovsdb_datum_to_json(const struct ovsdb_datum *datum,
1226 const struct ovsdb_type *type)
1227{
ae8f13e2 1228 if (datum->n == 1 && !ovsdb_type_is_map(type)) {
bd76d25d
BP
1229 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
1230 } else if (type->value.type == OVSDB_TYPE_VOID) {
f85f8ebb
BP
1231 struct json **elems;
1232 size_t i;
1233
1234 elems = xmalloc(datum->n * sizeof *elems);
1235 for (i = 0; i < datum->n; i++) {
bd76d25d 1236 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
f85f8ebb
BP
1237 }
1238
1239 return wrap_json("set", json_array_create(elems, datum->n));
1240 } else {
1241 struct json **elems;
1242 size_t i;
1243
1244 elems = xmalloc(datum->n * sizeof *elems);
1245 for (i = 0; i < datum->n; i++) {
1246 elems[i] = json_array_create_2(
bd76d25d
BP
1247 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
1248 ovsdb_atom_to_json(&datum->values[i], type->value.type));
f85f8ebb
BP
1249 }
1250
1251 return wrap_json("map", json_array_create(elems, datum->n));
1252 }
1253}
1254
0194f33a
BP
1255static const char *
1256skip_spaces(const char *p)
1257{
cb456217
BP
1258 while (isspace((unsigned char) *p)) {
1259 p++;
1260 }
1261 return p;
0194f33a
BP
1262}
1263
1bc6ff29 1264static char *
bd76d25d 1265parse_atom_token(const char **s, const struct ovsdb_base_type *base,
ce5a3e38 1266 union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
0194f33a 1267{
1bc6ff29 1268 char *token, *error;
0194f33a 1269
1bc6ff29
BP
1270 error = ovsdb_token_parse(s, &token);
1271 if (!error) {
ce5a3e38 1272 error = ovsdb_atom_from_string(atom, base, token, symtab);
1bc6ff29
BP
1273 free(token);
1274 }
1275 return error;
1276}
0194f33a 1277
1bc6ff29
BP
1278static char *
1279parse_key_value(const char **s, const struct ovsdb_type *type,
ce5a3e38
BP
1280 union ovsdb_atom *key, union ovsdb_atom *value,
1281 struct ovsdb_symbol_table *symtab)
1bc6ff29
BP
1282{
1283 const char *start = *s;
1284 char *error;
1285
ce5a3e38 1286 error = parse_atom_token(s, &type->key, key, symtab);
bd76d25d 1287 if (!error && type->value.type != OVSDB_TYPE_VOID) {
9397a13f 1288 *s = skip_spaces(*s);
1bc6ff29
BP
1289 if (**s == '=') {
1290 (*s)++;
9397a13f 1291 *s = skip_spaces(*s);
ce5a3e38 1292 error = parse_atom_token(s, &type->value, value, symtab);
1bc6ff29
BP
1293 } else {
1294 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1295 start, **s);
1296 }
1297 if (error) {
bd76d25d 1298 ovsdb_atom_destroy(key, type->key.type);
0194f33a 1299 }
0194f33a 1300 }
1bc6ff29 1301 return error;
0194f33a
BP
1302}
1303
1304static void
1305free_key_value(const struct ovsdb_type *type,
1306 union ovsdb_atom *key, union ovsdb_atom *value)
1307{
bd76d25d
BP
1308 ovsdb_atom_destroy(key, type->key.type);
1309 if (type->value.type != OVSDB_TYPE_VOID) {
1310 ovsdb_atom_destroy(value, type->value.type);
0194f33a
BP
1311 }
1312}
1313
1314/* Initializes 'datum' as a datum of the given 'type', parsing its contents
1315 * from 's'. The format of 's' is a series of space or comma separated atoms
1316 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1317 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1318 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
ce5a3e38
BP
1319 * required.
1320 *
1321 * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
1322 * ovsdb_atom_to_string(). */
1bc6ff29 1323char *
0194f33a 1324ovsdb_datum_from_string(struct ovsdb_datum *datum,
ce5a3e38
BP
1325 const struct ovsdb_type *type, const char *s,
1326 struct ovsdb_symbol_table *symtab)
0194f33a
BP
1327{
1328 bool is_map = ovsdb_type_is_map(type);
1bc6ff29 1329 struct ovsdb_error *dberror;
0194f33a
BP
1330 const char *p;
1331 int end_delim;
1bc6ff29 1332 char *error;
0194f33a
BP
1333
1334 ovsdb_datum_init_empty(datum);
1335
1336 /* Swallow a leading delimiter if there is one. */
1337 p = skip_spaces(s);
1338 if (*p == (is_map ? '{' : '[')) {
1339 end_delim = is_map ? '}' : ']';
1340 p = skip_spaces(p + 1);
1341 } else if (!*p) {
1342 if (is_map) {
1bc6ff29 1343 return xstrdup("use \"{}\" to specify the empty map");
0194f33a 1344 } else {
1bc6ff29 1345 return xstrdup("use \"[]\" to specify the empty set");
0194f33a
BP
1346 }
1347 } else {
1348 end_delim = 0;
1349 }
1350
1351 while (*p && *p != end_delim) {
1352 union ovsdb_atom key, value;
1353
1354 if (ovsdb_token_is_delim(*p)) {
1bc6ff29
BP
1355 error = xasprintf("%s: unexpected \"%c\" parsing %s",
1356 s, *p, ovsdb_type_to_english(type));
1357 goto error;
0194f33a
BP
1358 }
1359
1360 /* Add to datum. */
ce5a3e38 1361 error = parse_key_value(&p, type, &key, &value, symtab);
1bc6ff29
BP
1362 if (error) {
1363 goto error;
1364 }
0194f33a
BP
1365 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1366 free_key_value(type, &key, &value);
1367
1368 /* Skip optional white space and comma. */
1369 p = skip_spaces(p);
1370 if (*p == ',') {
1371 p = skip_spaces(p + 1);
1372 }
1373 }
1374
1375 if (*p != end_delim) {
1bc6ff29
BP
1376 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1377 goto error;
0194f33a
BP
1378 }
1379 if (end_delim) {
1380 p = skip_spaces(p + 1);
1381 if (*p) {
1bc6ff29
BP
1382 error = xasprintf("%s: trailing garbage after \"%c\"",
1383 s, end_delim);
1384 goto error;
0194f33a
BP
1385 }
1386 }
1387
1388 if (datum->n < type->n_min) {
1bc6ff29
BP
1389 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1390 s, datum->n, is_map ? "pair(s)" : "value(s)",
1391 type->n_min);
1392 goto error;
0194f33a 1393 } else if (datum->n > type->n_max) {
1bc6ff29
BP
1394 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1395 s, datum->n, is_map ? "pair(s)" : "value(s)",
1396 type->n_max);
1397 goto error;
0194f33a
BP
1398 }
1399
bfc96d9b 1400 dberror = ovsdb_datum_sort(datum, type->key.type);
1bc6ff29
BP
1401 if (dberror) {
1402 ovsdb_error_destroy(dberror);
0194f33a 1403 if (ovsdb_type_is_map(type)) {
1bc6ff29 1404 error = xasprintf("%s: map contains duplicate key", s);
0194f33a 1405 } else {
1bc6ff29 1406 error = xasprintf("%s: set contains duplicate value", s);
0194f33a 1407 }
1bc6ff29 1408 goto error;
0194f33a 1409 }
1bc6ff29
BP
1410
1411 return NULL;
1412
1413error:
1414 ovsdb_datum_destroy(datum, type);
1415 ovsdb_datum_init_empty(datum);
1416 return error;
0194f33a
BP
1417}
1418
1419/* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1420 * to ovsdb_datum_from_string(). */
1421void
1422ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1423 const struct ovsdb_type *type, struct ds *out)
1424{
1425 bool is_map = ovsdb_type_is_map(type);
1426 size_t i;
1427
1428 if (type->n_max > 1 || !datum->n) {
1429 ds_put_char(out, is_map ? '{' : '[');
1430 }
1431 for (i = 0; i < datum->n; i++) {
1432 if (i > 0) {
1433 ds_put_cstr(out, ", ");
1434 }
1435
bd76d25d 1436 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
0194f33a
BP
1437 if (is_map) {
1438 ds_put_char(out, '=');
bd76d25d 1439 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
0194f33a
BP
1440 }
1441 }
1442 if (type->n_max > 1 || !datum->n) {
1443 ds_put_char(out, is_map ? '}' : ']');
1444 }
1445}
1446
e4af5615
BP
1447/* Initializes 'datum' as a string-to-string map whose contents are taken from
1448 * 'sh'. Destroys 'sh'. */
1449void
1450ovsdb_datum_from_shash(struct ovsdb_datum *datum, struct shash *sh)
1451{
1452 struct shash_node *node, *next;
1453 size_t i;
1454
1455 datum->n = shash_count(sh);
1456 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1457 datum->values = xmalloc(datum->n * sizeof *datum->values);
1458
1459 i = 0;
1460 SHASH_FOR_EACH_SAFE (node, next, sh) {
1461 datum->keys[i].string = node->name;
1462 datum->values[i].string = node->data;
1463 shash_steal(sh, node);
1464 i++;
1465 }
1466 assert(i == datum->n);
1467
1468 shash_destroy(sh);
1469 ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1470}
1471
f85f8ebb
BP
1472static uint32_t
1473hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1474 unsigned int n, uint32_t basis)
1475{
1476 if (type != OVSDB_TYPE_VOID) {
1477 unsigned int i;
1478
1479 for (i = 0; i < n; i++) {
1480 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1481 }
1482 }
1483 return basis;
1484}
1485
1486uint32_t
1487ovsdb_datum_hash(const struct ovsdb_datum *datum,
1488 const struct ovsdb_type *type, uint32_t basis)
1489{
bd76d25d
BP
1490 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1491 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1492 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
f85f8ebb
BP
1493 return basis;
1494}
1495
1496static int
1497atom_arrays_compare_3way(const union ovsdb_atom *a,
2f47998b
BP
1498 const union ovsdb_atom *b,
1499 enum ovsdb_atomic_type type,
1500 size_t n)
f85f8ebb
BP
1501{
1502 unsigned int i;
1503
1504 for (i = 0; i < n; i++) {
1505 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1506 if (cmp) {
1507 return cmp;
1508 }
1509 }
1510
1511 return 0;
1512}
1513
1514bool
1515ovsdb_datum_equals(const struct ovsdb_datum *a,
1516 const struct ovsdb_datum *b,
1517 const struct ovsdb_type *type)
1518{
1519 return !ovsdb_datum_compare_3way(a, b, type);
1520}
1521
1522int
1523ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1524 const struct ovsdb_datum *b,
1525 const struct ovsdb_type *type)
1526{
1527 int cmp;
1528
1529 if (a->n != b->n) {
1530 return a->n < b->n ? -1 : 1;
1531 }
1532
bd76d25d 1533 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
f85f8ebb
BP
1534 if (cmp) {
1535 return cmp;
1536 }
1537
bd76d25d
BP
1538 return (type->value.type == OVSDB_TYPE_VOID ? 0
1539 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
f85f8ebb
BP
1540 a->n));
1541}
1542
2f47998b 1543/* If 'key' is one of the keys in 'datum', returns its index within 'datum',
bd76d25d 1544 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
2f47998b
BP
1545 * 'keys' array in 'datum'.
1546 */
1547unsigned int
1548ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1549 const union ovsdb_atom *key,
1550 enum ovsdb_atomic_type key_type)
1551{
1552 unsigned int low = 0;
1553 unsigned int high = datum->n;
1554 while (low < high) {
1555 unsigned int idx = (low + high) / 2;
1556 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1557 if (cmp < 0) {
1558 high = idx;
1559 } else if (cmp > 0) {
1560 low = idx + 1;
1561 } else {
1562 return idx;
1563 }
1564 }
1565 return UINT_MAX;
1566}
1567
1568/* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
bd76d25d 1569 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
2f47998b
BP
1570 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1571 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1572 */
1573unsigned int
1574ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1575 const union ovsdb_atom *key,
1576 enum ovsdb_atomic_type key_type,
1577 const union ovsdb_atom *value,
1578 enum ovsdb_atomic_type value_type)
1579{
1580 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1581 if (idx != UINT_MAX
1582 && value_type != OVSDB_TYPE_VOID
1583 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1584 idx = UINT_MAX;
1585 }
1586 return idx;
1587}
1588
e9f8f936
BP
1589/* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1590 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
bd76d25d 1591 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
e9f8f936
BP
1592 * values. */
1593static unsigned int
1594ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1595 const struct ovsdb_datum *b,
1596 const struct ovsdb_type *type)
f85f8ebb 1597{
2f47998b 1598 return ovsdb_datum_find_key_value(b,
bd76d25d 1599 &a->keys[i], type->key.type,
2f47998b 1600 a->values ? &a->values[i] : NULL,
bd76d25d 1601 type->value.type);
f85f8ebb
BP
1602}
1603
1604/* Returns true if every element in 'a' is also in 'b', false otherwise. */
1605bool
1606ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1607 const struct ovsdb_datum *b,
1608 const struct ovsdb_type *type)
1609{
1610 size_t i;
1611
1612 for (i = 0; i < a->n; i++) {
e9f8f936 1613 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
f85f8ebb
BP
1614 return false;
1615 }
1616 }
1617 return true;
1618}
1619
1620/* Returns true if no element in 'a' is also in 'b', false otherwise. */
1621bool
1622ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1623 const struct ovsdb_datum *b,
1624 const struct ovsdb_type *type)
1625{
1626 size_t i;
1627
1628 for (i = 0; i < a->n; i++) {
e9f8f936 1629 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
f85f8ebb
BP
1630 return false;
1631 }
1632 }
1633 return true;
1634}
e9f8f936
BP
1635
1636static void
1637ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1638 unsigned int capacity)
1639{
1640 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
bd76d25d 1641 if (type->value.type != OVSDB_TYPE_VOID) {
e9f8f936
BP
1642 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1643 }
1644}
1645
2f47998b
BP
1646/* Removes the element with index 'idx' from 'datum', which has type 'type'.
1647 * If 'idx' is not the last element in 'datum', then the removed element is
1648 * replaced by the (former) last element.
1649 *
1650 * This function does not maintain ovsdb_datum invariants. Use
1651 * ovsdb_datum_sort() to check and restore these invariants. */
1652void
1653ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1654 const struct ovsdb_type *type)
e9f8f936 1655{
bd76d25d 1656 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
2f47998b 1657 datum->keys[idx] = datum->keys[datum->n - 1];
bd76d25d
BP
1658 if (type->value.type != OVSDB_TYPE_VOID) {
1659 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
2f47998b
BP
1660 datum->values[idx] = datum->values[datum->n - 1];
1661 }
1662 datum->n--;
1663}
1664
1665/* Adds the element with the given 'key' and 'value' to 'datum', which must
1666 * have the specified 'type'.
1667 *
1668 * This function always allocates memory, so it is not an efficient way to add
1669 * a number of elements to a datum.
1670 *
1671 * This function does not maintain ovsdb_datum invariants. Use
1672 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1673 * 0 or 1 elements cannot violate the invariants anyhow.) */
1674void
1675ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1676 const union ovsdb_atom *key,
1677 const union ovsdb_atom *value,
1678 const struct ovsdb_type *type)
1679{
1680 size_t idx = datum->n++;
1681 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
bd76d25d
BP
1682 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1683 if (type->value.type != OVSDB_TYPE_VOID) {
2f47998b
BP
1684 datum->values = xrealloc(datum->values,
1685 datum->n * sizeof *datum->values);
bd76d25d 1686 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
e9f8f936 1687 }
e9f8f936
BP
1688}
1689
1690void
2f47998b
BP
1691ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1692 const struct ovsdb_type *type, bool replace)
e9f8f936 1693{
e9f8f936 1694 unsigned int n;
2f47998b 1695 size_t bi;
e9f8f936 1696
e9f8f936 1697 n = a->n;
2f47998b
BP
1698 for (bi = 0; bi < b->n; bi++) {
1699 unsigned int ai;
1700
bd76d25d 1701 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
2f47998b 1702 if (ai == UINT_MAX) {
e9f8f936 1703 if (n == a->n) {
2f47998b 1704 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
e9f8f936 1705 }
bd76d25d
BP
1706 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1707 if (type->value.type != OVSDB_TYPE_VOID) {
2f47998b 1708 ovsdb_atom_clone(&a->values[n], &b->values[bi],
bd76d25d 1709 type->value.type);
e9f8f936
BP
1710 }
1711 n++;
bd76d25d
BP
1712 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1713 ovsdb_atom_destroy(&a->values[ai], type->value.type);
2f47998b 1714 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
bd76d25d 1715 type->value.type);
e9f8f936
BP
1716 }
1717 }
1718 if (n != a->n) {
1719 struct ovsdb_error *error;
1720 a->n = n;
bfc96d9b 1721 error = ovsdb_datum_sort(a, type->key.type);
e9f8f936
BP
1722 assert(!error);
1723 }
1724}
1725
1726void
1727ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1728 const struct ovsdb_datum *b,
1729 const struct ovsdb_type *b_type)
1730{
1731 bool changed = false;
1732 size_t i;
1733
bd76d25d
BP
1734 assert(a_type->key.type == b_type->key.type);
1735 assert(a_type->value.type == b_type->value.type
1736 || b_type->value.type == OVSDB_TYPE_VOID);
e9f8f936
BP
1737
1738 /* XXX The big-O of this could easily be improved. */
1739 for (i = 0; i < a->n; ) {
1740 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1741 if (idx != UINT_MAX) {
1742 changed = true;
2f47998b 1743 ovsdb_datum_remove_unsafe(a, i, a_type);
e9f8f936
BP
1744 } else {
1745 i++;
1746 }
1747 }
1748 if (changed) {
bfc96d9b 1749 ovsdb_datum_sort_assert(a, a_type->key.type);
e9f8f936
BP
1750 }
1751}
f85f8ebb
BP
1752\f
1753struct ovsdb_symbol_table {
1754 struct shash sh;
1755};
1756
1757struct ovsdb_symbol_table *
1758ovsdb_symbol_table_create(void)
1759{
1760 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1761 shash_init(&symtab->sh);
1762 return symtab;
1763}
1764
1765void
1766ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1767{
1768 if (symtab) {
506051fc 1769 shash_destroy_free_data(&symtab->sh);
f85f8ebb
BP
1770 free(symtab);
1771 }
1772}
1773
2d2d6d4a 1774struct ovsdb_symbol *
f85f8ebb
BP
1775ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1776 const char *name)
1777{
1778 return shash_find_data(&symtab->sh, name);
1779}
1780
fbf925e4 1781struct ovsdb_symbol *
f85f8ebb 1782ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
2d2d6d4a 1783 const struct uuid *uuid, bool used)
f85f8ebb 1784{
2d2d6d4a
BP
1785 struct ovsdb_symbol *symbol;
1786
1787 assert(!ovsdb_symbol_table_get(symtab, name));
1788 symbol = xmalloc(sizeof *symbol);
1789 symbol->uuid = *uuid;
1790 symbol->used = used;
1791 shash_add(&symtab->sh, name, symbol);
fbf925e4
BP
1792 return symbol;
1793}
1794
1795struct ovsdb_symbol *
1796ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
1797 const char *name)
1798{
1799 struct ovsdb_symbol *symbol;
1800
1801 symbol = ovsdb_symbol_table_get(symtab, name);
1802 if (!symbol) {
1803 struct uuid uuid;
1804
1805 uuid_generate(&uuid);
1806 symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
1807 }
1808 return symbol;
f85f8ebb 1809}
ce5a3e38
BP
1810
1811const char *
1812ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *symtab)
1813{
1814 struct shash_node *node;
1815
1816 SHASH_FOR_EACH (node, &symtab->sh) {
1817 struct ovsdb_symbol *symbol = node->data;
1818 if (!symbol->used) {
1819 return node->name;
1820 }
1821 }
1822
1823 return NULL;
1824}
0194f33a
BP
1825\f
1826/* Extracts a token from the beginning of 's' and returns a pointer just after
1827 * the token. Stores the token itself into '*outp', which the caller is
1828 * responsible for freeing (with free()).
1829 *
1830 * If 's[0]' is a delimiter, the returned token is the empty string.
1831 *
1832 * A token extends from 's' to the first delimiter, as defined by
1833 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1834 * escaped with a backslash, in which case the backslash does not appear in the
1835 * output. Double quotes also cause delimiters to be ignored, but the double
1836 * quotes are retained in the output. (Backslashes inside double quotes are
1837 * not removed, either.)
1838 */
1bc6ff29
BP
1839char *
1840ovsdb_token_parse(const char **s, char **outp)
0194f33a
BP
1841{
1842 const char *p;
1843 struct ds out;
1844 bool in_quotes;
1bc6ff29 1845 char *error;
0194f33a
BP
1846
1847 ds_init(&out);
1848 in_quotes = false;
1bc6ff29 1849 for (p = *s; *p != '\0'; ) {
0194f33a
BP
1850 int c = *p++;
1851 if (c == '\\') {
1852 if (in_quotes) {
1853 ds_put_char(&out, '\\');
1854 }
1855 if (!*p) {
1bc6ff29
BP
1856 error = xasprintf("%s: backslash at end of argument", *s);
1857 goto error;
0194f33a
BP
1858 }
1859 ds_put_char(&out, *p++);
1860 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
1861 p--;
1862 break;
1863 } else {
1864 ds_put_char(&out, c);
1865 if (c == '"') {
1866 in_quotes = !in_quotes;
1867 }
1868 }
1869 }
1870 if (in_quotes) {
1bc6ff29
BP
1871 error = xasprintf("%s: quoted string extends past end of argument",
1872 *s);
1873 goto error;
0194f33a
BP
1874 }
1875 *outp = ds_cstr(&out);
1bc6ff29
BP
1876 *s = p;
1877 return NULL;
1878
1879error:
1880 ds_destroy(&out);
1881 *outp = NULL;
1882 return error;
0194f33a
BP
1883}
1884
1885/* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1886bool
1887ovsdb_token_is_delim(unsigned char c)
1888{
e89e5374 1889 return strchr(":=, []{}!<>", c) != NULL;
0194f33a 1890}