]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-types.c
Merge "master" into "next".
[mirror_ovs.git] / lib / ovsdb-types.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
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-types.h"
19
20 #include <float.h>
21 #include <limits.h>
22
23 #include "dynamic-string.h"
24 #include "json.h"
25 #include "ovsdb-error.h"
26 #include "ovsdb-parser.h"
27
28 const struct ovsdb_type ovsdb_type_integer =
29 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_INTEGER_INIT);
30 const struct ovsdb_type ovsdb_type_real =
31 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_REAL_INIT);
32 const struct ovsdb_type ovsdb_type_boolean =
33 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_BOOLEAN_INIT);
34 const struct ovsdb_type ovsdb_type_string =
35 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_STRING_INIT);
36 const struct ovsdb_type ovsdb_type_uuid =
37 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_UUID_INIT);
38 \f
39 /* ovsdb_atomic_type */
40 const char *
41 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
42 {
43 switch (type) {
44 case OVSDB_TYPE_VOID:
45 return "void";
46
47 case OVSDB_TYPE_INTEGER:
48 return "integer";
49
50 case OVSDB_TYPE_REAL:
51 return "real";
52
53 case OVSDB_TYPE_BOOLEAN:
54 return "boolean";
55
56 case OVSDB_TYPE_STRING:
57 return "string";
58
59 case OVSDB_TYPE_UUID:
60 return "uuid";
61
62 case OVSDB_N_TYPES:
63 default:
64 return "<invalid>";
65 }
66 }
67
68 struct json *
69 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
70 {
71 return json_string_create(ovsdb_atomic_type_to_string(type));
72 }
73
74 bool
75 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
76 {
77 if (!strcmp(string, "integer")) {
78 *type = OVSDB_TYPE_INTEGER;
79 } else if (!strcmp(string, "real")) {
80 *type = OVSDB_TYPE_REAL;
81 } else if (!strcmp(string, "boolean")) {
82 *type = OVSDB_TYPE_BOOLEAN;
83 } else if (!strcmp(string, "string")) {
84 *type = OVSDB_TYPE_STRING;
85 } else if (!strcmp(string, "uuid")) {
86 *type = OVSDB_TYPE_UUID;
87 } else {
88 return false;
89 }
90 return true;
91 }
92
93 struct ovsdb_error *
94 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
95 const struct json *json)
96 {
97 if (json->type == JSON_STRING) {
98 if (ovsdb_atomic_type_from_string(json_string(json), type)) {
99 return NULL;
100 } else {
101 *type = OVSDB_TYPE_VOID;
102 return ovsdb_syntax_error(json, NULL,
103 "\"%s\" is not an atomic-type",
104 json_string(json));
105 }
106 } else {
107 *type = OVSDB_TYPE_VOID;
108 return ovsdb_syntax_error(json, NULL, "atomic-type expected");
109 }
110 }
111 \f
112 /* ovsdb_base_type */
113
114 void
115 ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
116 {
117 base->type = type;
118
119 switch (base->type) {
120 case OVSDB_TYPE_VOID:
121 break;
122
123 case OVSDB_TYPE_INTEGER:
124 base->u.integer.min = INT64_MIN;
125 base->u.integer.max = INT64_MAX;
126 break;
127
128 case OVSDB_TYPE_REAL:
129 base->u.real.min = -DBL_MAX;
130 base->u.real.max = DBL_MAX;
131 break;
132
133 case OVSDB_TYPE_BOOLEAN:
134 break;
135
136 case OVSDB_TYPE_STRING:
137 #ifdef HAVE_PCRE
138 base->u.string.re = NULL;
139 #endif
140 base->u.string.reMatch = NULL;
141 base->u.string.reComment = NULL;
142 base->u.string.minLen = 0;
143 base->u.string.maxLen = UINT_MAX;
144 break;
145
146 case OVSDB_TYPE_UUID:
147 base->u.uuid.refTableName = NULL;
148 base->u.uuid.refTable = NULL;
149 break;
150
151 case OVSDB_N_TYPES:
152 NOT_REACHED();
153
154 default:
155 NOT_REACHED();
156 }
157 }
158
159 void
160 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
161 const struct ovsdb_base_type *src)
162 {
163 *dst = *src;
164
165 switch (dst->type) {
166 case OVSDB_TYPE_VOID:
167 case OVSDB_TYPE_INTEGER:
168 case OVSDB_TYPE_REAL:
169 case OVSDB_TYPE_BOOLEAN:
170 break;
171
172 case OVSDB_TYPE_STRING:
173 #if HAVE_PCRE
174 if (dst->u.string.re) {
175 pcre_refcount(dst->u.string.re, 1);
176 }
177 #else
178 if (dst->u.string.reMatch) {
179 dst->u.string.reMatch = xstrdup(dst->u.string.reMatch);
180 }
181 if (dst->u.string.reComment) {
182 dst->u.string.reComment = xstrdup(dst->u.string.reComment);
183 }
184 #endif
185 break;
186
187
188 case OVSDB_TYPE_UUID:
189 if (dst->u.uuid.refTableName) {
190 dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
191 }
192 break;
193
194 case OVSDB_N_TYPES:
195 default:
196 NOT_REACHED();
197 }
198 }
199
200 void
201 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
202 {
203 if (base) {
204 switch (base->type) {
205 case OVSDB_TYPE_VOID:
206 case OVSDB_TYPE_INTEGER:
207 case OVSDB_TYPE_REAL:
208 case OVSDB_TYPE_BOOLEAN:
209 break;
210
211 case OVSDB_TYPE_STRING:
212 #ifdef HAVE_PCRE
213 if (base->u.string.re && !pcre_refcount(base->u.string.re, -1)) {
214 pcre_free(base->u.string.re);
215 free(base->u.string.reMatch);
216 free(base->u.string.reComment);
217 }
218 #else
219 free(base->u.string.reMatch);
220 free(base->u.string.reComment);
221 #endif
222 break;
223
224 case OVSDB_TYPE_UUID:
225 free(base->u.uuid.refTableName);
226 break;
227
228 case OVSDB_N_TYPES:
229 NOT_REACHED();
230
231 default:
232 NOT_REACHED();
233 }
234 }
235 }
236
237 bool
238 ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
239 {
240 switch (base->type) {
241 case OVSDB_TYPE_VOID:
242 return true;
243
244 case OVSDB_TYPE_INTEGER:
245 return base->u.integer.min <= base->u.integer.max;
246
247 case OVSDB_TYPE_REAL:
248 return base->u.real.min <= base->u.real.max;
249
250 case OVSDB_TYPE_BOOLEAN:
251 return true;
252
253 case OVSDB_TYPE_STRING:
254 return base->u.string.minLen <= base->u.string.maxLen;
255
256 case OVSDB_TYPE_UUID:
257 return true;
258
259 case OVSDB_N_TYPES:
260 default:
261 return false;
262 }
263 }
264
265 bool
266 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
267 {
268 switch (base->type) {
269 case OVSDB_TYPE_VOID:
270 NOT_REACHED();
271
272 case OVSDB_TYPE_INTEGER:
273 return (base->u.integer.min != INT64_MIN
274 || base->u.integer.max != INT64_MAX);
275
276 case OVSDB_TYPE_REAL:
277 return (base->u.real.min != -DBL_MAX
278 || base->u.real.max != DBL_MAX);
279
280 case OVSDB_TYPE_BOOLEAN:
281 return false;
282
283 case OVSDB_TYPE_STRING:
284 return (base->u.string.reMatch != NULL
285 || base->u.string.minLen != 0
286 || base->u.string.maxLen != UINT_MAX);
287
288 case OVSDB_TYPE_UUID:
289 return base->u.uuid.refTableName != NULL;
290
291 case OVSDB_N_TYPES:
292 NOT_REACHED();
293
294 default:
295 NOT_REACHED();
296 }
297 }
298
299 void
300 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
301 {
302 enum ovsdb_atomic_type type = base->type;
303 ovsdb_base_type_destroy(base);
304 ovsdb_base_type_init(base, type);
305 }
306
307 struct ovsdb_error *
308 ovsdb_base_type_set_regex(struct ovsdb_base_type *base,
309 const char *reMatch, const char *reComment)
310 {
311 #ifdef HAVE_PCRE
312 const char *errorString;
313 const char *pattern;
314 int errorOffset;
315
316 /* Compile pattern, anchoring it at both ends. */
317 pattern = reMatch;
318 if (pattern[0] == '\0' || strchr(pattern, '\0')[-1] != '$') {
319 pattern = xasprintf("%s$", pattern);
320 }
321
322 #ifndef PCRE_JAVASCRIPT_COMPAT /* Added in PCRE 7.7. */
323 #define PCRE_JAVASCRIPT_COMPAT 0
324 #endif
325 base->u.string.re = pcre_compile(pattern, (PCRE_ANCHORED | PCRE_UTF8
326 | PCRE_JAVASCRIPT_COMPAT),
327 &errorString, &errorOffset, NULL);
328 if (pattern != reMatch) {
329 free((char *) pattern);
330 }
331 if (!base->u.string.re) {
332 return ovsdb_syntax_error(NULL, "invalid regular expression",
333 "\"%s\" is not a valid regular "
334 "expression: %s", reMatch, errorString);
335 }
336 pcre_refcount(base->u.string.re, 1);
337 #endif
338
339 /* Save regular expression. */
340 base->u.string.reMatch = xstrdup(reMatch);
341 base->u.string.reComment = reComment ? xstrdup(reComment) : NULL;
342 return NULL;
343 }
344
345 static struct ovsdb_error *
346 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
347 unsigned int *uint)
348 {
349 const struct json *json;
350
351 json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
352 if (json) {
353 if (json->u.integer < 0 || json->u.integer > UINT_MAX) {
354 return ovsdb_syntax_error(json, NULL,
355 "%s out of valid range 0 to %u",
356 member, UINT_MAX);
357 }
358 *uint = json->u.integer;
359 }
360 return NULL;
361 }
362
363 struct ovsdb_error *
364 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
365 const struct json *json)
366 {
367 struct ovsdb_parser parser;
368 struct ovsdb_error *error;
369 const struct json *type;
370
371 if (json->type == JSON_STRING) {
372 error = ovsdb_atomic_type_from_json(&base->type, json);
373 if (error) {
374 return error;
375 }
376 ovsdb_base_type_init(base, base->type);
377 return NULL;
378 }
379
380 ovsdb_parser_init(&parser, json, "ovsdb type");
381 type = ovsdb_parser_member(&parser, "type", OP_STRING);
382 if (ovsdb_parser_has_error(&parser)) {
383 base->type = OVSDB_TYPE_VOID;
384 return ovsdb_parser_finish(&parser);
385 }
386
387 error = ovsdb_atomic_type_from_json(&base->type, type);
388 if (error) {
389 return error;
390 }
391
392 ovsdb_base_type_init(base, base->type);
393 if (base->type == OVSDB_TYPE_INTEGER) {
394 const struct json *min, *max;
395
396 min = ovsdb_parser_member(&parser, "minInteger",
397 OP_INTEGER | OP_OPTIONAL);
398 max = ovsdb_parser_member(&parser, "maxInteger",
399 OP_INTEGER | OP_OPTIONAL);
400 base->u.integer.min = min ? min->u.integer : INT64_MIN;
401 base->u.integer.max = max ? max->u.integer : INT64_MAX;
402 if (base->u.integer.min > base->u.integer.max) {
403 error = ovsdb_syntax_error(json, NULL,
404 "minInteger exceeds maxInteger");
405 }
406 } else if (base->type == OVSDB_TYPE_REAL) {
407 const struct json *min, *max;
408
409 min = ovsdb_parser_member(&parser, "minReal", OP_NUMBER | OP_OPTIONAL);
410 max = ovsdb_parser_member(&parser, "maxReal", OP_NUMBER | OP_OPTIONAL);
411 base->u.real.min = min ? json_real(min) : -DBL_MAX;
412 base->u.real.max = max ? json_real(max) : DBL_MAX;
413 if (base->u.real.min > base->u.real.max) {
414 error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal");
415 }
416 } else if (base->type == OVSDB_TYPE_STRING) {
417 const struct json *reMatch;
418
419 reMatch = ovsdb_parser_member(&parser, "reMatch",
420 OP_STRING | OP_OPTIONAL);
421 if (reMatch) {
422 const struct json *reComment;
423
424 reComment = ovsdb_parser_member(&parser, "reComment",
425 OP_STRING | OP_OPTIONAL);
426 error = ovsdb_base_type_set_regex(
427 base, json_string(reMatch),
428 reComment ? json_string(reComment) : NULL);
429 }
430
431 if (!error) {
432 error = parse_optional_uint(&parser, "minLength",
433 &base->u.string.minLen);
434 }
435 if (!error) {
436 error = parse_optional_uint(&parser, "maxLength",
437 &base->u.string.maxLen);
438 }
439 if (!error && base->u.string.minLen > base->u.string.maxLen) {
440 error = ovsdb_syntax_error(json, NULL,
441 "minLength exceeds maxLength");
442 }
443 } else if (base->type == OVSDB_TYPE_UUID) {
444 const struct json *refTable;
445
446 refTable = ovsdb_parser_member(&parser, "refTable",
447 OP_ID | OP_OPTIONAL);
448 if (refTable) {
449 base->u.uuid.refTableName = xstrdup(refTable->u.string);
450 /* We can't set base->u.uuid.refTable here because we don't have
451 * enough context (we might not even be running in ovsdb-server).
452 * ovsdb_create() will set refTable later. */
453 }
454 }
455
456 if (error) {
457 ovsdb_error_destroy(ovsdb_parser_finish(&parser));
458 } else {
459 error = ovsdb_parser_finish(&parser);
460 }
461 if (error) {
462 ovsdb_base_type_destroy(base);
463 base->type = OVSDB_TYPE_VOID;
464 }
465 return error;
466 }
467
468 struct json *
469 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
470 {
471 struct json *json;
472
473 if (!ovsdb_base_type_has_constraints(base)) {
474 return json_string_create(ovsdb_atomic_type_to_string(base->type));
475 }
476
477 json = json_object_create();
478 json_object_put_string(json, "type",
479 ovsdb_atomic_type_to_string(base->type));
480 switch (base->type) {
481 case OVSDB_TYPE_VOID:
482 NOT_REACHED();
483
484 case OVSDB_TYPE_INTEGER:
485 if (base->u.integer.min != INT64_MIN) {
486 json_object_put(json, "minInteger",
487 json_integer_create(base->u.integer.min));
488 }
489 if (base->u.integer.max != INT64_MAX) {
490 json_object_put(json, "maxInteger",
491 json_integer_create(base->u.integer.max));
492 }
493 break;
494
495 case OVSDB_TYPE_REAL:
496 if (base->u.real.min != -DBL_MAX) {
497 json_object_put(json, "minReal",
498 json_real_create(base->u.real.min));
499 }
500 if (base->u.real.max != DBL_MAX) {
501 json_object_put(json, "maxReal",
502 json_real_create(base->u.real.max));
503 }
504 break;
505
506 case OVSDB_TYPE_BOOLEAN:
507 break;
508
509 case OVSDB_TYPE_STRING:
510 if (base->u.string.reMatch) {
511 json_object_put_string(json, "reMatch", base->u.string.reMatch);
512 if (base->u.string.reComment) {
513 json_object_put_string(json, "reComment",
514 base->u.string.reComment);
515 }
516 }
517 if (base->u.string.minLen != 0) {
518 json_object_put(json, "minLength",
519 json_integer_create(base->u.string.minLen));
520 }
521 if (base->u.string.maxLen != UINT_MAX) {
522 json_object_put(json, "maxLength",
523 json_integer_create(base->u.string.maxLen));
524 }
525 break;
526
527 case OVSDB_TYPE_UUID:
528 if (base->u.uuid.refTableName) {
529 json_object_put_string(json, "refTable",
530 base->u.uuid.refTableName);
531 }
532 break;
533
534 case OVSDB_N_TYPES:
535 NOT_REACHED();
536
537 default:
538 NOT_REACHED();
539 }
540
541 return json;
542 }
543 \f
544 /* ovsdb_type */
545
546 void
547 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
548 {
549 ovsdb_base_type_clone(&dst->key, &src->key);
550 ovsdb_base_type_clone(&dst->value, &src->value);
551 dst->n_min = src->n_min;
552 dst->n_max = src->n_max;
553 }
554
555 void
556 ovsdb_type_destroy(struct ovsdb_type *type)
557 {
558 ovsdb_base_type_destroy(&type->key);
559 ovsdb_base_type_destroy(&type->value);
560 }
561
562 bool
563 ovsdb_type_is_valid(const struct ovsdb_type *type)
564 {
565 return (type->key.type != OVSDB_TYPE_VOID
566 && ovsdb_base_type_is_valid(&type->key)
567 && ovsdb_base_type_is_valid(&type->value)
568 && type->n_min <= 1
569 && type->n_min <= type->n_max
570 && type->n_max >= 1);
571 }
572
573 static struct ovsdb_error *
574 n_from_json(const struct json *json, unsigned int *n)
575 {
576 if (!json) {
577 return NULL;
578 } else if (json->type == JSON_INTEGER
579 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
580 *n = json->u.integer;
581 return NULL;
582 } else {
583 return ovsdb_syntax_error(json, NULL, "bad min or max value");
584 }
585 }
586
587 char *
588 ovsdb_type_to_english(const struct ovsdb_type *type)
589 {
590 const char *key = ovsdb_atomic_type_to_string(type->key.type);
591 const char *value = ovsdb_atomic_type_to_string(type->value.type);
592 if (ovsdb_type_is_scalar(type)) {
593 return xstrdup(key);
594 } else {
595 struct ds s = DS_EMPTY_INITIALIZER;
596 ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
597 if (type->n_max == UINT_MAX) {
598 if (type->n_min) {
599 ds_put_format(&s, " of %u or more", type->n_min);
600 } else {
601 ds_put_cstr(&s, " of");
602 }
603 } else if (type->n_min) {
604 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
605 } else {
606 ds_put_format(&s, " of up to %u", type->n_max);
607 }
608 if (ovsdb_type_is_set(type)) {
609 ds_put_format(&s, " %ss", key);
610 } else {
611 ds_put_format(&s, " (%s, %s) pairs", key, value);
612 }
613 return ds_cstr(&s);
614 }
615 }
616
617 struct ovsdb_error *
618 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
619 {
620 type->value.type = OVSDB_TYPE_VOID;
621 type->n_min = 1;
622 type->n_max = 1;
623
624 if (json->type == JSON_STRING) {
625 return ovsdb_base_type_from_json(&type->key, json);
626 } else if (json->type == JSON_OBJECT) {
627 const struct json *key, *value, *min, *max;
628 struct ovsdb_error *error;
629 struct ovsdb_parser parser;
630
631 ovsdb_parser_init(&parser, json, "ovsdb type");
632 key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT);
633 value = ovsdb_parser_member(&parser, "value",
634 OP_STRING | OP_OBJECT | OP_OPTIONAL);
635 min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
636 max = ovsdb_parser_member(&parser, "max",
637 OP_INTEGER | OP_STRING | OP_OPTIONAL);
638 error = ovsdb_parser_finish(&parser);
639 if (error) {
640 return error;
641 }
642
643 error = ovsdb_base_type_from_json(&type->key, key);
644 if (error) {
645 return error;
646 }
647
648 if (value) {
649 error = ovsdb_base_type_from_json(&type->value, value);
650 if (error) {
651 return error;
652 }
653 }
654
655 error = n_from_json(min, &type->n_min);
656 if (error) {
657 return error;
658 }
659
660 if (max && max->type == JSON_STRING
661 && !strcmp(max->u.string, "unlimited")) {
662 type->n_max = UINT_MAX;
663 } else {
664 error = n_from_json(max, &type->n_max);
665 if (error) {
666 return error;
667 }
668 }
669
670 if (!ovsdb_type_is_valid(type)) {
671 return ovsdb_syntax_error(json, NULL,
672 "ovsdb type fails constraint checks");
673 }
674
675 return NULL;
676 } else {
677 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
678 }
679 }
680
681 struct json *
682 ovsdb_type_to_json(const struct ovsdb_type *type)
683 {
684 if (ovsdb_type_is_scalar(type)
685 && !ovsdb_base_type_has_constraints(&type->key)) {
686 return ovsdb_base_type_to_json(&type->key);
687 } else {
688 struct json *json = json_object_create();
689 json_object_put(json, "key", ovsdb_base_type_to_json(&type->key));
690 if (type->value.type != OVSDB_TYPE_VOID) {
691 json_object_put(json, "value",
692 ovsdb_base_type_to_json(&type->value));
693 }
694 if (type->n_min != 1) {
695 json_object_put(json, "min", json_integer_create(type->n_min));
696 }
697 if (type->n_max == UINT_MAX) {
698 json_object_put_string(json, "max", "unlimited");
699 } else if (type->n_max != 1) {
700 json_object_put(json, "max", json_integer_create(type->n_max));
701 }
702 return json;
703 }
704 }