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:
156 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
157 const struct ovsdb_base_type *src)
162 case OVSDB_TYPE_VOID:
163 case OVSDB_TYPE_INTEGER:
164 case OVSDB_TYPE_REAL:
165 case OVSDB_TYPE_BOOLEAN:
168 case OVSDB_TYPE_STRING:
169 if (dst->u.string.re) {
170 pcre_refcount(dst->u.string.re, 1);
174 case OVSDB_TYPE_UUID:
184 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
187 switch (base->type) {
188 case OVSDB_TYPE_VOID:
189 case OVSDB_TYPE_INTEGER:
190 case OVSDB_TYPE_REAL:
191 case OVSDB_TYPE_BOOLEAN:
194 case OVSDB_TYPE_STRING:
195 if (base->u.string.re && !pcre_refcount(base->u.string.re, -1)) {
196 pcre_free(base->u.string.re);
197 free(base->u.string.reMatch);
198 free(base->u.string.reComment);
202 case OVSDB_TYPE_UUID:
215 ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
217 switch (base->type) {
218 case OVSDB_TYPE_VOID:
221 case OVSDB_TYPE_INTEGER:
222 return base->u.integer.min <= base->u.integer.max;
224 case OVSDB_TYPE_REAL:
225 return base->u.real.min <= base->u.real.max;
227 case OVSDB_TYPE_BOOLEAN:
230 case OVSDB_TYPE_STRING:
231 return base->u.string.minLen <= base->u.string.maxLen;
233 case OVSDB_TYPE_UUID:
243 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
245 switch (base->type) {
246 case OVSDB_TYPE_VOID:
249 case OVSDB_TYPE_INTEGER:
250 return (base->u.integer.min != INT64_MIN
251 || base->u.integer.max != INT64_MAX);
253 case OVSDB_TYPE_REAL:
254 return (base->u.real.min != -DBL_MAX
255 || base->u.real.max != DBL_MAX);
257 case OVSDB_TYPE_BOOLEAN:
260 case OVSDB_TYPE_STRING:
261 return (base->u.string.reMatch != NULL
262 || base->u.string.minLen != 0
263 || base->u.string.maxLen != UINT_MAX);
265 case OVSDB_TYPE_UUID:
277 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
279 enum ovsdb_atomic_type type = base->type;
280 ovsdb_base_type_destroy(base);
281 ovsdb_base_type_init(base, type);
285 ovsdb_base_type_set_regex(struct ovsdb_base_type *base,
286 const char *reMatch, const char *reComment)
288 const char *errorString;
292 /* Compile pattern, anchoring it at both ends. */
294 if (pattern[0] == '\0' || strchr(pattern, '\0')[-1] != '$') {
295 pattern = xasprintf("%s$", pattern);
297 base->u.string.re = pcre_compile(pattern, (PCRE_ANCHORED | PCRE_UTF8
298 | PCRE_JAVASCRIPT_COMPAT),
299 &errorString, &errorOffset, NULL);
300 if (pattern != reMatch) {
301 free((char *) pattern);
303 if (!base->u.string.re) {
304 return ovsdb_syntax_error(NULL, "invalid regular expression",
305 "\"%s\" is not a valid regular "
306 "expression: %s", reMatch, errorString);
309 /* Save regular expression. */
310 pcre_refcount(base->u.string.re, 1);
311 base->u.string.reMatch = xstrdup(reMatch);
312 base->u.string.reComment = reComment ? xstrdup(reComment) : NULL;
316 static struct ovsdb_error *
317 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
320 const struct json *json;
322 json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
324 if (json->u.integer < 0 || json->u.integer > UINT_MAX) {
325 return ovsdb_syntax_error(json, NULL,
326 "%s out of valid range 0 to %u",
329 *uint = json->u.integer;
335 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
336 const struct json *json)
338 struct ovsdb_parser parser;
339 struct ovsdb_error *error;
340 const struct json *type;
342 if (json->type == JSON_STRING) {
343 error = ovsdb_atomic_type_from_json(&base->type, json);
347 ovsdb_base_type_init(base, base->type);
351 ovsdb_parser_init(&parser, json, "ovsdb type");
352 type = ovsdb_parser_member(&parser, "type", OP_STRING);
353 if (ovsdb_parser_has_error(&parser)) {
354 base->type = OVSDB_TYPE_VOID;
355 return ovsdb_parser_finish(&parser);
358 error = ovsdb_atomic_type_from_json(&base->type, type);
363 ovsdb_base_type_init(base, base->type);
364 if (base->type == OVSDB_TYPE_INTEGER) {
365 const struct json *min, *max;
367 min = ovsdb_parser_member(&parser, "minInteger",
368 OP_INTEGER | OP_OPTIONAL);
369 max = ovsdb_parser_member(&parser, "maxInteger",
370 OP_INTEGER | OP_OPTIONAL);
371 base->u.integer.min = min ? min->u.integer : INT64_MIN;
372 base->u.integer.max = max ? max->u.integer : INT64_MAX;
373 if (base->u.integer.min > base->u.integer.max) {
374 error = ovsdb_syntax_error(json, NULL,
375 "minInteger exceeds maxInteger");
377 } else if (base->type == OVSDB_TYPE_REAL) {
378 const struct json *min, *max;
380 min = ovsdb_parser_member(&parser, "minReal", OP_NUMBER | OP_OPTIONAL);
381 max = ovsdb_parser_member(&parser, "maxReal", OP_NUMBER | OP_OPTIONAL);
382 base->u.real.min = min ? json_real(min) : -DBL_MAX;
383 base->u.real.max = max ? json_real(max) : DBL_MAX;
384 if (base->u.real.min > base->u.real.max) {
385 error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal");
387 } else if (base->type == OVSDB_TYPE_STRING) {
388 const struct json *reMatch;
390 reMatch = ovsdb_parser_member(&parser, "reMatch",
391 OP_STRING | OP_OPTIONAL);
393 const struct json *reComment;
395 reComment = ovsdb_parser_member(&parser, "reComment",
396 OP_STRING | OP_OPTIONAL);
397 error = ovsdb_base_type_set_regex(
398 base, json_string(reMatch),
399 reComment ? json_string(reComment) : NULL);
403 error = parse_optional_uint(&parser, "minLength",
404 &base->u.string.minLen);
407 error = parse_optional_uint(&parser, "maxLength",
408 &base->u.string.maxLen);
410 if (!error && base->u.string.minLen > base->u.string.maxLen) {
411 error = ovsdb_syntax_error(json, NULL,
412 "minLength exceeds maxLength");
417 ovsdb_error_destroy(ovsdb_parser_finish(&parser));
419 error = ovsdb_parser_finish(&parser);
422 ovsdb_base_type_destroy(base);
423 base->type = OVSDB_TYPE_VOID;
429 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
433 if (!ovsdb_base_type_has_constraints(base)) {
434 return json_string_create(ovsdb_atomic_type_to_string(base->type));
437 json = json_object_create();
438 json_object_put_string(json, "type",
439 ovsdb_atomic_type_to_string(base->type));
440 switch (base->type) {
441 case OVSDB_TYPE_VOID:
444 case OVSDB_TYPE_INTEGER:
445 if (base->u.integer.min != INT64_MIN) {
446 json_object_put(json, "minInteger",
447 json_integer_create(base->u.integer.min));
449 if (base->u.integer.max != INT64_MAX) {
450 json_object_put(json, "maxInteger",
451 json_integer_create(base->u.integer.max));
455 case OVSDB_TYPE_REAL:
456 if (base->u.real.min != -DBL_MAX) {
457 json_object_put(json, "minReal",
458 json_real_create(base->u.real.min));
460 if (base->u.real.max != DBL_MAX) {
461 json_object_put(json, "maxReal",
462 json_real_create(base->u.real.max));
466 case OVSDB_TYPE_BOOLEAN:
469 case OVSDB_TYPE_STRING:
470 if (base->u.string.reMatch) {
471 json_object_put_string(json, "reMatch", base->u.string.reMatch);
472 if (base->u.string.reComment) {
473 json_object_put_string(json, "reComment",
474 base->u.string.reComment);
477 if (base->u.string.minLen != 0) {
478 json_object_put(json, "minLength",
479 json_integer_create(base->u.string.minLen));
481 if (base->u.string.maxLen != UINT_MAX) {
482 json_object_put(json, "maxLength",
483 json_integer_create(base->u.string.maxLen));
487 case OVSDB_TYPE_UUID:
503 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
505 ovsdb_base_type_clone(&dst->key, &src->key);
506 ovsdb_base_type_clone(&dst->value, &src->value);
507 dst->n_min = src->n_min;
508 dst->n_max = src->n_max;
512 ovsdb_type_destroy(struct ovsdb_type *type)
514 ovsdb_base_type_destroy(&type->key);
515 ovsdb_base_type_destroy(&type->value);
519 ovsdb_type_is_valid(const struct ovsdb_type *type)
521 return (type->key.type != OVSDB_TYPE_VOID
522 && ovsdb_base_type_is_valid(&type->key)
523 && ovsdb_base_type_is_valid(&type->value)
525 && type->n_min <= type->n_max);
528 static struct ovsdb_error *
529 n_from_json(const struct json *json, unsigned int *n)
533 } else if (json->type == JSON_INTEGER
534 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
535 *n = json->u.integer;
538 return ovsdb_syntax_error(json, NULL, "bad min or max value");
543 ovsdb_type_to_english(const struct ovsdb_type *type)
545 const char *key = ovsdb_atomic_type_to_string(type->key.type);
546 const char *value = ovsdb_atomic_type_to_string(type->value.type);
547 if (ovsdb_type_is_scalar(type)) {
550 struct ds s = DS_EMPTY_INITIALIZER;
551 ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
552 if (type->n_max == UINT_MAX) {
554 ds_put_format(&s, " of %u or more", type->n_min);
556 ds_put_cstr(&s, " of");
558 } else if (type->n_min) {
559 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
561 ds_put_format(&s, " of up to %u", type->n_max);
563 if (ovsdb_type_is_set(type)) {
564 ds_put_format(&s, " %ss", key);
566 ds_put_format(&s, " (%s, %s) pairs", key, value);
573 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
575 type->value.type = OVSDB_TYPE_VOID;
579 if (json->type == JSON_STRING) {
580 return ovsdb_base_type_from_json(&type->key, json);
581 } else if (json->type == JSON_OBJECT) {
582 const struct json *key, *value, *min, *max;
583 struct ovsdb_error *error;
584 struct ovsdb_parser parser;
586 ovsdb_parser_init(&parser, json, "ovsdb type");
587 key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT);
588 value = ovsdb_parser_member(&parser, "value",
589 OP_STRING | OP_OBJECT | OP_OPTIONAL);
590 min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
591 max = ovsdb_parser_member(&parser, "max",
592 OP_INTEGER | OP_STRING | OP_OPTIONAL);
593 error = ovsdb_parser_finish(&parser);
598 error = ovsdb_base_type_from_json(&type->key, key);
604 error = ovsdb_base_type_from_json(&type->value, value);
610 error = n_from_json(min, &type->n_min);
615 if (max && max->type == JSON_STRING
616 && !strcmp(max->u.string, "unlimited")) {
617 type->n_max = UINT_MAX;
619 error = n_from_json(max, &type->n_max);
625 if (!ovsdb_type_is_valid(type)) {
626 return ovsdb_syntax_error(json, NULL,
627 "ovsdb type fails constraint checks");
632 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
637 ovsdb_type_to_json(const struct ovsdb_type *type)
639 if (ovsdb_type_is_scalar(type)
640 && !ovsdb_base_type_has_constraints(&type->key)) {
641 return ovsdb_base_type_to_json(&type->key);
643 struct json *json = json_object_create();
644 json_object_put(json, "key", ovsdb_base_type_to_json(&type->key));
645 if (type->value.type != OVSDB_TYPE_VOID) {
646 json_object_put(json, "value",
647 ovsdb_base_type_to_json(&type->value));
649 if (type->n_min != 1) {
650 json_object_put(json, "min", json_integer_create(type->n_min));
652 if (type->n_max == UINT_MAX) {
653 json_object_put_string(json, "max", "unlimited");
654 } else if (type->n_max != 1) {
655 json_object_put(json, "max", json_integer_create(type->n_max));