1 /* Copyright (c) 2009, 2010 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ovsdb-types.h"
23 #include "dynamic-string.h"
25 #include "ovsdb-error.h"
26 #include "ovsdb-parser.h"
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);
39 /* ovsdb_atomic_type */
41 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
47 case OVSDB_TYPE_INTEGER:
53 case OVSDB_TYPE_BOOLEAN:
56 case OVSDB_TYPE_STRING:
69 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
71 return json_string_create(ovsdb_atomic_type_to_string(type));
75 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
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;
94 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
95 const struct json *json)
97 if (json->type == JSON_STRING) {
98 if (ovsdb_atomic_type_from_string(json_string(json), type)) {
101 *type = OVSDB_TYPE_VOID;
102 return ovsdb_syntax_error(json, NULL,
103 "\"%s\" is not an atomic-type",
107 *type = OVSDB_TYPE_VOID;
108 return ovsdb_syntax_error(json, NULL, "atomic-type expected");
112 /* ovsdb_base_type */
115 ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
119 switch (base->type) {
120 case OVSDB_TYPE_VOID:
123 case OVSDB_TYPE_INTEGER:
124 base->u.integer.min = INT64_MIN;
125 base->u.integer.max = INT64_MAX;
128 case OVSDB_TYPE_REAL:
129 base->u.real.min = -DBL_MAX;
130 base->u.real.max = DBL_MAX;
133 case OVSDB_TYPE_BOOLEAN:
136 case OVSDB_TYPE_STRING:
137 base->u.string.re = NULL;
138 base->u.string.reMatch = NULL;
139 base->u.string.reComment = NULL;
140 base->u.string.minLen = 0;
141 base->u.string.maxLen = UINT_MAX;
144 case OVSDB_TYPE_UUID:
145 base->u.uuid.refTableName = NULL;
146 base->u.uuid.refTable = NULL;
158 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
159 const struct ovsdb_base_type *src)
164 case OVSDB_TYPE_VOID:
165 case OVSDB_TYPE_INTEGER:
166 case OVSDB_TYPE_REAL:
167 case OVSDB_TYPE_BOOLEAN:
170 case OVSDB_TYPE_STRING:
171 if (dst->u.string.re) {
172 pcre_refcount(dst->u.string.re, 1);
176 case OVSDB_TYPE_UUID:
177 if (dst->u.uuid.refTableName) {
178 dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
189 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
192 switch (base->type) {
193 case OVSDB_TYPE_VOID:
194 case OVSDB_TYPE_INTEGER:
195 case OVSDB_TYPE_REAL:
196 case OVSDB_TYPE_BOOLEAN:
199 case OVSDB_TYPE_STRING:
200 if (base->u.string.re && !pcre_refcount(base->u.string.re, -1)) {
201 pcre_free(base->u.string.re);
202 free(base->u.string.reMatch);
203 free(base->u.string.reComment);
207 case OVSDB_TYPE_UUID:
208 free(base->u.uuid.refTableName);
221 ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
223 switch (base->type) {
224 case OVSDB_TYPE_VOID:
227 case OVSDB_TYPE_INTEGER:
228 return base->u.integer.min <= base->u.integer.max;
230 case OVSDB_TYPE_REAL:
231 return base->u.real.min <= base->u.real.max;
233 case OVSDB_TYPE_BOOLEAN:
236 case OVSDB_TYPE_STRING:
237 return base->u.string.minLen <= base->u.string.maxLen;
239 case OVSDB_TYPE_UUID:
249 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
251 switch (base->type) {
252 case OVSDB_TYPE_VOID:
255 case OVSDB_TYPE_INTEGER:
256 return (base->u.integer.min != INT64_MIN
257 || base->u.integer.max != INT64_MAX);
259 case OVSDB_TYPE_REAL:
260 return (base->u.real.min != -DBL_MAX
261 || base->u.real.max != DBL_MAX);
263 case OVSDB_TYPE_BOOLEAN:
266 case OVSDB_TYPE_STRING:
267 return (base->u.string.reMatch != NULL
268 || base->u.string.minLen != 0
269 || base->u.string.maxLen != UINT_MAX);
271 case OVSDB_TYPE_UUID:
272 return base->u.uuid.refTableName != NULL;
283 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
285 enum ovsdb_atomic_type type = base->type;
286 ovsdb_base_type_destroy(base);
287 ovsdb_base_type_init(base, type);
291 ovsdb_base_type_set_regex(struct ovsdb_base_type *base,
292 const char *reMatch, const char *reComment)
294 const char *errorString;
298 /* Compile pattern, anchoring it at both ends. */
300 if (pattern[0] == '\0' || strchr(pattern, '\0')[-1] != '$') {
301 pattern = xasprintf("%s$", pattern);
303 base->u.string.re = pcre_compile(pattern, (PCRE_ANCHORED | PCRE_UTF8
304 | PCRE_JAVASCRIPT_COMPAT),
305 &errorString, &errorOffset, NULL);
306 if (pattern != reMatch) {
307 free((char *) pattern);
309 if (!base->u.string.re) {
310 return ovsdb_syntax_error(NULL, "invalid regular expression",
311 "\"%s\" is not a valid regular "
312 "expression: %s", reMatch, errorString);
315 /* Save regular expression. */
316 pcre_refcount(base->u.string.re, 1);
317 base->u.string.reMatch = xstrdup(reMatch);
318 base->u.string.reComment = reComment ? xstrdup(reComment) : NULL;
322 static struct ovsdb_error *
323 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
326 const struct json *json;
328 json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
330 if (json->u.integer < 0 || json->u.integer > UINT_MAX) {
331 return ovsdb_syntax_error(json, NULL,
332 "%s out of valid range 0 to %u",
335 *uint = json->u.integer;
341 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
342 const struct json *json)
344 struct ovsdb_parser parser;
345 struct ovsdb_error *error;
346 const struct json *type;
348 if (json->type == JSON_STRING) {
349 error = ovsdb_atomic_type_from_json(&base->type, json);
353 ovsdb_base_type_init(base, base->type);
357 ovsdb_parser_init(&parser, json, "ovsdb type");
358 type = ovsdb_parser_member(&parser, "type", OP_STRING);
359 if (ovsdb_parser_has_error(&parser)) {
360 base->type = OVSDB_TYPE_VOID;
361 return ovsdb_parser_finish(&parser);
364 error = ovsdb_atomic_type_from_json(&base->type, type);
369 ovsdb_base_type_init(base, base->type);
370 if (base->type == OVSDB_TYPE_INTEGER) {
371 const struct json *min, *max;
373 min = ovsdb_parser_member(&parser, "minInteger",
374 OP_INTEGER | OP_OPTIONAL);
375 max = ovsdb_parser_member(&parser, "maxInteger",
376 OP_INTEGER | OP_OPTIONAL);
377 base->u.integer.min = min ? min->u.integer : INT64_MIN;
378 base->u.integer.max = max ? max->u.integer : INT64_MAX;
379 if (base->u.integer.min > base->u.integer.max) {
380 error = ovsdb_syntax_error(json, NULL,
381 "minInteger exceeds maxInteger");
383 } else if (base->type == OVSDB_TYPE_REAL) {
384 const struct json *min, *max;
386 min = ovsdb_parser_member(&parser, "minReal", OP_NUMBER | OP_OPTIONAL);
387 max = ovsdb_parser_member(&parser, "maxReal", OP_NUMBER | OP_OPTIONAL);
388 base->u.real.min = min ? json_real(min) : -DBL_MAX;
389 base->u.real.max = max ? json_real(max) : DBL_MAX;
390 if (base->u.real.min > base->u.real.max) {
391 error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal");
393 } else if (base->type == OVSDB_TYPE_STRING) {
394 const struct json *reMatch;
396 reMatch = ovsdb_parser_member(&parser, "reMatch",
397 OP_STRING | OP_OPTIONAL);
399 const struct json *reComment;
401 reComment = ovsdb_parser_member(&parser, "reComment",
402 OP_STRING | OP_OPTIONAL);
403 error = ovsdb_base_type_set_regex(
404 base, json_string(reMatch),
405 reComment ? json_string(reComment) : NULL);
409 error = parse_optional_uint(&parser, "minLength",
410 &base->u.string.minLen);
413 error = parse_optional_uint(&parser, "maxLength",
414 &base->u.string.maxLen);
416 if (!error && base->u.string.minLen > base->u.string.maxLen) {
417 error = ovsdb_syntax_error(json, NULL,
418 "minLength exceeds maxLength");
420 } else if (base->type == OVSDB_TYPE_UUID) {
421 const struct json *refTable;
423 refTable = ovsdb_parser_member(&parser, "refTable",
424 OP_ID | OP_OPTIONAL);
426 base->u.uuid.refTableName = xstrdup(refTable->u.string);
427 /* We can't set base->u.uuid.refTable here because we don't have
428 * enough context (we might not even be running in ovsdb-server).
429 * ovsdb_create() will set refTable later. */
434 ovsdb_error_destroy(ovsdb_parser_finish(&parser));
436 error = ovsdb_parser_finish(&parser);
439 ovsdb_base_type_destroy(base);
440 base->type = OVSDB_TYPE_VOID;
446 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
450 if (!ovsdb_base_type_has_constraints(base)) {
451 return json_string_create(ovsdb_atomic_type_to_string(base->type));
454 json = json_object_create();
455 json_object_put_string(json, "type",
456 ovsdb_atomic_type_to_string(base->type));
457 switch (base->type) {
458 case OVSDB_TYPE_VOID:
461 case OVSDB_TYPE_INTEGER:
462 if (base->u.integer.min != INT64_MIN) {
463 json_object_put(json, "minInteger",
464 json_integer_create(base->u.integer.min));
466 if (base->u.integer.max != INT64_MAX) {
467 json_object_put(json, "maxInteger",
468 json_integer_create(base->u.integer.max));
472 case OVSDB_TYPE_REAL:
473 if (base->u.real.min != -DBL_MAX) {
474 json_object_put(json, "minReal",
475 json_real_create(base->u.real.min));
477 if (base->u.real.max != DBL_MAX) {
478 json_object_put(json, "maxReal",
479 json_real_create(base->u.real.max));
483 case OVSDB_TYPE_BOOLEAN:
486 case OVSDB_TYPE_STRING:
487 if (base->u.string.reMatch) {
488 json_object_put_string(json, "reMatch", base->u.string.reMatch);
489 if (base->u.string.reComment) {
490 json_object_put_string(json, "reComment",
491 base->u.string.reComment);
494 if (base->u.string.minLen != 0) {
495 json_object_put(json, "minLength",
496 json_integer_create(base->u.string.minLen));
498 if (base->u.string.maxLen != UINT_MAX) {
499 json_object_put(json, "maxLength",
500 json_integer_create(base->u.string.maxLen));
504 case OVSDB_TYPE_UUID:
505 if (base->u.uuid.refTableName) {
506 json_object_put_string(json, "refTable",
507 base->u.uuid.refTableName);
524 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
526 ovsdb_base_type_clone(&dst->key, &src->key);
527 ovsdb_base_type_clone(&dst->value, &src->value);
528 dst->n_min = src->n_min;
529 dst->n_max = src->n_max;
533 ovsdb_type_destroy(struct ovsdb_type *type)
535 ovsdb_base_type_destroy(&type->key);
536 ovsdb_base_type_destroy(&type->value);
540 ovsdb_type_is_valid(const struct ovsdb_type *type)
542 return (type->key.type != OVSDB_TYPE_VOID
543 && ovsdb_base_type_is_valid(&type->key)
544 && ovsdb_base_type_is_valid(&type->value)
546 && type->n_min <= type->n_max);
549 static struct ovsdb_error *
550 n_from_json(const struct json *json, unsigned int *n)
554 } else if (json->type == JSON_INTEGER
555 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
556 *n = json->u.integer;
559 return ovsdb_syntax_error(json, NULL, "bad min or max value");
564 ovsdb_type_to_english(const struct ovsdb_type *type)
566 const char *key = ovsdb_atomic_type_to_string(type->key.type);
567 const char *value = ovsdb_atomic_type_to_string(type->value.type);
568 if (ovsdb_type_is_scalar(type)) {
571 struct ds s = DS_EMPTY_INITIALIZER;
572 ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
573 if (type->n_max == UINT_MAX) {
575 ds_put_format(&s, " of %u or more", type->n_min);
577 ds_put_cstr(&s, " of");
579 } else if (type->n_min) {
580 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
582 ds_put_format(&s, " of up to %u", type->n_max);
584 if (ovsdb_type_is_set(type)) {
585 ds_put_format(&s, " %ss", key);
587 ds_put_format(&s, " (%s, %s) pairs", key, value);
594 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
596 type->value.type = OVSDB_TYPE_VOID;
600 if (json->type == JSON_STRING) {
601 return ovsdb_base_type_from_json(&type->key, json);
602 } else if (json->type == JSON_OBJECT) {
603 const struct json *key, *value, *min, *max;
604 struct ovsdb_error *error;
605 struct ovsdb_parser parser;
607 ovsdb_parser_init(&parser, json, "ovsdb type");
608 key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT);
609 value = ovsdb_parser_member(&parser, "value",
610 OP_STRING | OP_OBJECT | OP_OPTIONAL);
611 min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
612 max = ovsdb_parser_member(&parser, "max",
613 OP_INTEGER | OP_STRING | OP_OPTIONAL);
614 error = ovsdb_parser_finish(&parser);
619 error = ovsdb_base_type_from_json(&type->key, key);
625 error = ovsdb_base_type_from_json(&type->value, value);
631 error = n_from_json(min, &type->n_min);
636 if (max && max->type == JSON_STRING
637 && !strcmp(max->u.string, "unlimited")) {
638 type->n_max = UINT_MAX;
640 error = n_from_json(max, &type->n_max);
646 if (!ovsdb_type_is_valid(type)) {
647 return ovsdb_syntax_error(json, NULL,
648 "ovsdb type fails constraint checks");
653 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
658 ovsdb_type_to_json(const struct ovsdb_type *type)
660 if (ovsdb_type_is_scalar(type)
661 && !ovsdb_base_type_has_constraints(&type->key)) {
662 return ovsdb_base_type_to_json(&type->key);
664 struct json *json = json_object_create();
665 json_object_put(json, "key", ovsdb_base_type_to_json(&type->key));
666 if (type->value.type != OVSDB_TYPE_VOID) {
667 json_object_put(json, "value",
668 ovsdb_base_type_to_json(&type->value));
670 if (type->n_min != 1) {
671 json_object_put(json, "min", json_integer_create(type->n_min));
673 if (type->n_max == UINT_MAX) {
674 json_object_put_string(json, "max", "unlimited");
675 } else if (type->n_max != 1) {
676 json_object_put(json, "max", json_integer_create(type->n_max));