1 /* Copyright (c) 2009, 2010, 2011 Nicira, Inc.
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-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
29 const struct ovsdb_type ovsdb_type_integer =
30 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_INTEGER_INIT);
31 const struct ovsdb_type ovsdb_type_real =
32 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_REAL_INIT);
33 const struct ovsdb_type ovsdb_type_boolean =
34 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_BOOLEAN_INIT);
35 const struct ovsdb_type ovsdb_type_string =
36 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_STRING_INIT);
37 const struct ovsdb_type ovsdb_type_uuid =
38 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_UUID_INIT);
40 /* ovsdb_atomic_type */
42 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
48 case OVSDB_TYPE_INTEGER:
54 case OVSDB_TYPE_BOOLEAN:
57 case OVSDB_TYPE_STRING:
70 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
72 return json_string_create(ovsdb_atomic_type_to_string(type));
76 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
78 if (!strcmp(string, "integer")) {
79 *type = OVSDB_TYPE_INTEGER;
80 } else if (!strcmp(string, "real")) {
81 *type = OVSDB_TYPE_REAL;
82 } else if (!strcmp(string, "boolean")) {
83 *type = OVSDB_TYPE_BOOLEAN;
84 } else if (!strcmp(string, "string")) {
85 *type = OVSDB_TYPE_STRING;
86 } else if (!strcmp(string, "uuid")) {
87 *type = OVSDB_TYPE_UUID;
95 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
96 const struct json *json)
98 if (json->type == JSON_STRING) {
99 if (ovsdb_atomic_type_from_string(json_string(json), type)) {
102 *type = OVSDB_TYPE_VOID;
103 return ovsdb_syntax_error(json, NULL,
104 "\"%s\" is not an atomic-type",
108 *type = OVSDB_TYPE_VOID;
109 return ovsdb_syntax_error(json, NULL, "atomic-type expected");
113 /* ovsdb_base_type */
116 ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
121 switch (base->type) {
122 case OVSDB_TYPE_VOID:
125 case OVSDB_TYPE_INTEGER:
126 base->u.integer.min = INT64_MIN;
127 base->u.integer.max = INT64_MAX;
130 case OVSDB_TYPE_REAL:
131 base->u.real.min = -DBL_MAX;
132 base->u.real.max = DBL_MAX;
135 case OVSDB_TYPE_BOOLEAN:
138 case OVSDB_TYPE_STRING:
139 base->u.string.minLen = 0;
140 base->u.string.maxLen = UINT_MAX;
143 case OVSDB_TYPE_UUID:
144 base->u.uuid.refTableName = NULL;
145 base->u.uuid.refTable = NULL;
156 /* Returns the type of the 'enum_' member for an ovsdb_base_type whose 'type'
157 * is 'atomic_type'. */
158 const struct ovsdb_type *
159 ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type atomic_type)
161 static struct ovsdb_type *types[OVSDB_N_TYPES];
163 if (!types[atomic_type]) {
164 struct ovsdb_type *type;
166 types[atomic_type] = type = xmalloc(sizeof *type);
167 ovsdb_base_type_init(&type->key, atomic_type);
168 ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
170 type->n_max = UINT_MAX;
172 return types[atomic_type];
176 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
177 const struct ovsdb_base_type *src)
182 dst->enum_ = xmalloc(sizeof *dst->enum_);
183 ovsdb_datum_clone(dst->enum_, src->enum_,
184 ovsdb_base_type_get_enum_type(dst->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:
197 case OVSDB_TYPE_UUID:
198 if (dst->u.uuid.refTableName) {
199 dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
210 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
214 ovsdb_datum_destroy(base->enum_,
215 ovsdb_base_type_get_enum_type(base->type));
219 switch (base->type) {
220 case OVSDB_TYPE_VOID:
221 case OVSDB_TYPE_INTEGER:
222 case OVSDB_TYPE_REAL:
223 case OVSDB_TYPE_BOOLEAN:
226 case OVSDB_TYPE_STRING:
229 case OVSDB_TYPE_UUID:
230 free(base->u.uuid.refTableName);
243 ovsdb_base_type_is_valid(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 <= base->u.integer.max;
252 case OVSDB_TYPE_REAL:
253 return base->u.real.min <= base->u.real.max;
255 case OVSDB_TYPE_BOOLEAN:
258 case OVSDB_TYPE_STRING:
259 return base->u.string.minLen <= base->u.string.maxLen;
261 case OVSDB_TYPE_UUID:
271 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
277 switch (base->type) {
278 case OVSDB_TYPE_VOID:
281 case OVSDB_TYPE_INTEGER:
282 return (base->u.integer.min != INT64_MIN
283 || base->u.integer.max != INT64_MAX);
285 case OVSDB_TYPE_REAL:
286 return (base->u.real.min != -DBL_MAX
287 || base->u.real.max != DBL_MAX);
289 case OVSDB_TYPE_BOOLEAN:
292 case OVSDB_TYPE_STRING:
293 return base->u.string.minLen != 0 || base->u.string.maxLen != UINT_MAX;
295 case OVSDB_TYPE_UUID:
296 return base->u.uuid.refTableName != NULL;
307 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
309 enum ovsdb_atomic_type type = base->type;
310 ovsdb_base_type_destroy(base);
311 ovsdb_base_type_init(base, type);
314 static struct ovsdb_error *
315 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
318 const struct json *json;
320 json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
322 if (json->u.integer < 0 || json->u.integer > UINT_MAX) {
323 return ovsdb_syntax_error(json, NULL,
324 "%s out of valid range 0 to %u",
327 *uint = json->u.integer;
333 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
334 const struct json *json)
336 struct ovsdb_parser parser;
337 struct ovsdb_error *error;
338 const struct json *type, *enum_;
340 if (json->type == JSON_STRING) {
341 error = ovsdb_atomic_type_from_json(&base->type, json);
345 ovsdb_base_type_init(base, base->type);
349 ovsdb_parser_init(&parser, json, "ovsdb type");
350 type = ovsdb_parser_member(&parser, "type", OP_STRING);
351 if (ovsdb_parser_has_error(&parser)) {
352 base->type = OVSDB_TYPE_VOID;
353 return ovsdb_parser_finish(&parser);
356 error = ovsdb_atomic_type_from_json(&base->type, type);
361 ovsdb_base_type_init(base, base->type);
363 enum_ = ovsdb_parser_member(&parser, "enum", OP_ANY | OP_OPTIONAL);
365 base->enum_ = xmalloc(sizeof *base->enum_);
366 error = ovsdb_datum_from_json(
367 base->enum_, ovsdb_base_type_get_enum_type(base->type),
373 } else if (base->type == OVSDB_TYPE_INTEGER) {
374 const struct json *min, *max;
376 min = ovsdb_parser_member(&parser, "minInteger",
377 OP_INTEGER | OP_OPTIONAL);
378 max = ovsdb_parser_member(&parser, "maxInteger",
379 OP_INTEGER | OP_OPTIONAL);
380 base->u.integer.min = min ? min->u.integer : INT64_MIN;
381 base->u.integer.max = max ? max->u.integer : INT64_MAX;
382 if (base->u.integer.min > base->u.integer.max) {
383 error = ovsdb_syntax_error(json, NULL,
384 "minInteger exceeds maxInteger");
386 } else if (base->type == OVSDB_TYPE_REAL) {
387 const struct json *min, *max;
389 min = ovsdb_parser_member(&parser, "minReal", OP_NUMBER | OP_OPTIONAL);
390 max = ovsdb_parser_member(&parser, "maxReal", OP_NUMBER | OP_OPTIONAL);
391 base->u.real.min = min ? json_real(min) : -DBL_MAX;
392 base->u.real.max = max ? json_real(max) : DBL_MAX;
393 if (base->u.real.min > base->u.real.max) {
394 error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal");
396 } else if (base->type == OVSDB_TYPE_STRING) {
398 error = parse_optional_uint(&parser, "minLength",
399 &base->u.string.minLen);
402 error = parse_optional_uint(&parser, "maxLength",
403 &base->u.string.maxLen);
405 if (!error && base->u.string.minLen > base->u.string.maxLen) {
406 error = ovsdb_syntax_error(json, NULL,
407 "minLength exceeds maxLength");
409 } else if (base->type == OVSDB_TYPE_UUID) {
410 const struct json *refTable;
412 refTable = ovsdb_parser_member(&parser, "refTable",
413 OP_ID | OP_OPTIONAL);
415 const struct json *refType;
417 base->u.uuid.refTableName = xstrdup(refTable->u.string);
419 /* We can't set base->u.uuid.refTable here because we don't have
420 * enough context (we might not even be running in ovsdb-server).
421 * ovsdb_create() will set refTable later. */
423 refType = ovsdb_parser_member(&parser, "refType",
424 OP_ID | OP_OPTIONAL);
426 const char *refType_s = json_string(refType);
427 if (!strcmp(refType_s, "strong")) {
428 base->u.uuid.refType = OVSDB_REF_STRONG;
429 } else if (!strcmp(refType_s, "weak")) {
430 base->u.uuid.refType = OVSDB_REF_WEAK;
432 error = ovsdb_syntax_error(json, NULL, "refType must be "
433 "\"strong\" or \"weak\" (not "
434 "\"%s\")", refType_s);
437 base->u.uuid.refType = OVSDB_REF_STRONG;
443 ovsdb_error_destroy(ovsdb_parser_finish(&parser));
445 error = ovsdb_parser_finish(&parser);
448 ovsdb_base_type_destroy(base);
449 base->type = OVSDB_TYPE_VOID;
455 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
459 if (!ovsdb_base_type_has_constraints(base)) {
460 return json_string_create(ovsdb_atomic_type_to_string(base->type));
463 json = json_object_create();
464 json_object_put_string(json, "type",
465 ovsdb_atomic_type_to_string(base->type));
468 const struct ovsdb_type *type;
470 type = ovsdb_base_type_get_enum_type(base->type);
471 json_object_put(json, "enum", ovsdb_datum_to_json(base->enum_, type));
474 switch (base->type) {
475 case OVSDB_TYPE_VOID:
478 case OVSDB_TYPE_INTEGER:
479 if (base->u.integer.min != INT64_MIN) {
480 json_object_put(json, "minInteger",
481 json_integer_create(base->u.integer.min));
483 if (base->u.integer.max != INT64_MAX) {
484 json_object_put(json, "maxInteger",
485 json_integer_create(base->u.integer.max));
489 case OVSDB_TYPE_REAL:
490 if (base->u.real.min != -DBL_MAX) {
491 json_object_put(json, "minReal",
492 json_real_create(base->u.real.min));
494 if (base->u.real.max != DBL_MAX) {
495 json_object_put(json, "maxReal",
496 json_real_create(base->u.real.max));
500 case OVSDB_TYPE_BOOLEAN:
503 case OVSDB_TYPE_STRING:
504 if (base->u.string.minLen != 0) {
505 json_object_put(json, "minLength",
506 json_integer_create(base->u.string.minLen));
508 if (base->u.string.maxLen != UINT_MAX) {
509 json_object_put(json, "maxLength",
510 json_integer_create(base->u.string.maxLen));
514 case OVSDB_TYPE_UUID:
515 if (base->u.uuid.refTableName) {
516 json_object_put_string(json, "refTable",
517 base->u.uuid.refTableName);
518 if (base->u.uuid.refType == OVSDB_REF_WEAK) {
519 json_object_put_string(json, "refType", "weak");
537 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
539 ovsdb_base_type_clone(&dst->key, &src->key);
540 ovsdb_base_type_clone(&dst->value, &src->value);
541 dst->n_min = src->n_min;
542 dst->n_max = src->n_max;
546 ovsdb_type_destroy(struct ovsdb_type *type)
548 ovsdb_base_type_destroy(&type->key);
549 ovsdb_base_type_destroy(&type->value);
553 ovsdb_type_is_valid(const struct ovsdb_type *type)
555 return (type->key.type != OVSDB_TYPE_VOID
556 && ovsdb_base_type_is_valid(&type->key)
557 && ovsdb_base_type_is_valid(&type->value)
559 && type->n_max >= 1);
562 static struct ovsdb_error *
563 n_from_json(const struct json *json, unsigned int *n)
567 } else if (json->type == JSON_INTEGER
568 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
569 *n = json->u.integer;
572 return ovsdb_syntax_error(json, NULL, "bad min or max value");
577 ovsdb_type_to_english(const struct ovsdb_type *type)
579 const char *key = ovsdb_atomic_type_to_string(type->key.type);
580 const char *value = ovsdb_atomic_type_to_string(type->value.type);
581 if (ovsdb_type_is_scalar(type)) {
584 struct ds s = DS_EMPTY_INITIALIZER;
585 ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
586 if (type->n_max == UINT_MAX) {
588 ds_put_format(&s, " of %u or more", type->n_min);
590 ds_put_cstr(&s, " of");
592 } else if (type->n_min) {
593 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
595 ds_put_format(&s, " of up to %u", type->n_max);
597 if (ovsdb_type_is_set(type)) {
598 ds_put_format(&s, " %ss", key);
600 ds_put_format(&s, " (%s, %s) pairs", key, value);
607 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
609 ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
613 if (json->type == JSON_STRING) {
614 return ovsdb_base_type_from_json(&type->key, json);
615 } else if (json->type == JSON_OBJECT) {
616 const struct json *key, *value, *min, *max;
617 struct ovsdb_error *error;
618 struct ovsdb_parser parser;
620 ovsdb_parser_init(&parser, json, "ovsdb type");
621 key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT);
622 value = ovsdb_parser_member(&parser, "value",
623 OP_STRING | OP_OBJECT | OP_OPTIONAL);
624 min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
625 max = ovsdb_parser_member(&parser, "max",
626 OP_INTEGER | OP_STRING | OP_OPTIONAL);
627 error = ovsdb_parser_finish(&parser);
632 error = ovsdb_base_type_from_json(&type->key, key);
638 error = ovsdb_base_type_from_json(&type->value, value);
644 error = n_from_json(min, &type->n_min);
649 if (max && max->type == JSON_STRING
650 && !strcmp(max->u.string, "unlimited")) {
651 type->n_max = UINT_MAX;
653 error = n_from_json(max, &type->n_max);
659 if (!ovsdb_type_is_valid(type)) {
660 return ovsdb_syntax_error(json, NULL,
661 "ovsdb type fails constraint checks");
666 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
671 ovsdb_type_to_json(const struct ovsdb_type *type)
673 if (ovsdb_type_is_scalar(type)
674 && !ovsdb_base_type_has_constraints(&type->key)) {
675 return ovsdb_base_type_to_json(&type->key);
677 struct json *json = json_object_create();
678 json_object_put(json, "key", ovsdb_base_type_to_json(&type->key));
679 if (type->value.type != OVSDB_TYPE_VOID) {
680 json_object_put(json, "value",
681 ovsdb_base_type_to_json(&type->value));
683 if (type->n_min != 1) {
684 json_object_put(json, "min", json_integer_create(type->n_min));
686 if (type->n_max == UINT_MAX) {
687 json_object_put_string(json, "max", "unlimited");
688 } else if (type->n_max != 1) {
689 json_object_put(json, "max", json_integer_create(type->n_max));