1 /* Copyright (c) 2009, 2010, 2011, 2013 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 "ovs-thread.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
30 const struct ovsdb_type ovsdb_type_integer
=
31 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_INTEGER_INIT
);
32 const struct ovsdb_type ovsdb_type_real
=
33 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_REAL_INIT
);
34 const struct ovsdb_type ovsdb_type_boolean
=
35 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_BOOLEAN_INIT
);
36 const struct ovsdb_type ovsdb_type_string
=
37 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_STRING_INIT
);
38 const struct ovsdb_type ovsdb_type_uuid
=
39 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_BASE_UUID_INIT
);
41 /* ovsdb_atomic_type */
43 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type
)
49 case OVSDB_TYPE_INTEGER
:
55 case OVSDB_TYPE_BOOLEAN
:
58 case OVSDB_TYPE_STRING
:
71 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type
)
73 return json_string_create(ovsdb_atomic_type_to_string(type
));
77 ovsdb_atomic_type_from_string(const char *string
, enum ovsdb_atomic_type
*type
)
79 if (!strcmp(string
, "integer")) {
80 *type
= OVSDB_TYPE_INTEGER
;
81 } else if (!strcmp(string
, "real")) {
82 *type
= OVSDB_TYPE_REAL
;
83 } else if (!strcmp(string
, "boolean")) {
84 *type
= OVSDB_TYPE_BOOLEAN
;
85 } else if (!strcmp(string
, "string")) {
86 *type
= OVSDB_TYPE_STRING
;
87 } else if (!strcmp(string
, "uuid")) {
88 *type
= OVSDB_TYPE_UUID
;
96 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type
*type
,
97 const struct json
*json
)
99 if (json
->type
== JSON_STRING
) {
100 if (ovsdb_atomic_type_from_string(json_string(json
), type
)) {
103 *type
= OVSDB_TYPE_VOID
;
104 return ovsdb_syntax_error(json
, NULL
,
105 "\"%s\" is not an atomic-type",
109 *type
= OVSDB_TYPE_VOID
;
110 return ovsdb_syntax_error(json
, NULL
, "atomic-type expected");
114 /* ovsdb_base_type */
117 ovsdb_base_type_init(struct ovsdb_base_type
*base
, enum ovsdb_atomic_type type
)
122 switch (base
->type
) {
123 case OVSDB_TYPE_VOID
:
126 case OVSDB_TYPE_INTEGER
:
127 base
->u
.integer
.min
= INT64_MIN
;
128 base
->u
.integer
.max
= INT64_MAX
;
131 case OVSDB_TYPE_REAL
:
132 base
->u
.real
.min
= -DBL_MAX
;
133 base
->u
.real
.max
= DBL_MAX
;
136 case OVSDB_TYPE_BOOLEAN
:
139 case OVSDB_TYPE_STRING
:
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
;
157 /* Returns the type of the 'enum_' member for an ovsdb_base_type whose 'type'
158 * is 'atomic_type'. */
159 const struct ovsdb_type
*
160 ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type atomic_type
)
162 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
163 static struct ovsdb_type
*types
[OVSDB_N_TYPES
];
165 if (ovsthread_once_start(&once
)) {
166 enum ovsdb_atomic_type i
;
168 for (i
= 0; i
< OVSDB_N_TYPES
; i
++) {
169 struct ovsdb_type
*type
;
171 types
[i
] = type
= xmalloc(sizeof *type
);
172 ovsdb_base_type_init(&type
->key
, i
);
173 ovsdb_base_type_init(&type
->value
, OVSDB_TYPE_VOID
);
175 type
->n_max
= UINT_MAX
;
178 ovsthread_once_done(&once
);
180 return types
[atomic_type
];
184 ovsdb_base_type_clone(struct ovsdb_base_type
*dst
,
185 const struct ovsdb_base_type
*src
)
190 dst
->enum_
= xmalloc(sizeof *dst
->enum_
);
191 ovsdb_datum_clone(dst
->enum_
, src
->enum_
,
192 ovsdb_base_type_get_enum_type(dst
->type
));
196 case OVSDB_TYPE_VOID
:
197 case OVSDB_TYPE_INTEGER
:
198 case OVSDB_TYPE_REAL
:
199 case OVSDB_TYPE_BOOLEAN
:
202 case OVSDB_TYPE_STRING
:
205 case OVSDB_TYPE_UUID
:
206 if (dst
->u
.uuid
.refTableName
) {
207 dst
->u
.uuid
.refTableName
= xstrdup(dst
->u
.uuid
.refTableName
);
218 ovsdb_base_type_destroy(struct ovsdb_base_type
*base
)
222 ovsdb_datum_destroy(base
->enum_
,
223 ovsdb_base_type_get_enum_type(base
->type
));
227 switch (base
->type
) {
228 case OVSDB_TYPE_VOID
:
229 case OVSDB_TYPE_INTEGER
:
230 case OVSDB_TYPE_REAL
:
231 case OVSDB_TYPE_BOOLEAN
:
234 case OVSDB_TYPE_STRING
:
237 case OVSDB_TYPE_UUID
:
238 free(base
->u
.uuid
.refTableName
);
251 ovsdb_base_type_is_valid(const struct ovsdb_base_type
*base
)
253 switch (base
->type
) {
254 case OVSDB_TYPE_VOID
:
257 case OVSDB_TYPE_INTEGER
:
258 return base
->u
.integer
.min
<= base
->u
.integer
.max
;
260 case OVSDB_TYPE_REAL
:
261 return base
->u
.real
.min
<= base
->u
.real
.max
;
263 case OVSDB_TYPE_BOOLEAN
:
266 case OVSDB_TYPE_STRING
:
267 return base
->u
.string
.minLen
<= base
->u
.string
.maxLen
;
269 case OVSDB_TYPE_UUID
:
279 ovsdb_base_type_has_constraints(const struct ovsdb_base_type
*base
)
285 switch (base
->type
) {
286 case OVSDB_TYPE_VOID
:
289 case OVSDB_TYPE_INTEGER
:
290 return (base
->u
.integer
.min
!= INT64_MIN
291 || base
->u
.integer
.max
!= INT64_MAX
);
293 case OVSDB_TYPE_REAL
:
294 return (base
->u
.real
.min
!= -DBL_MAX
295 || base
->u
.real
.max
!= DBL_MAX
);
297 case OVSDB_TYPE_BOOLEAN
:
300 case OVSDB_TYPE_STRING
:
301 return base
->u
.string
.minLen
!= 0 || base
->u
.string
.maxLen
!= UINT_MAX
;
303 case OVSDB_TYPE_UUID
:
304 return base
->u
.uuid
.refTableName
!= NULL
;
315 ovsdb_base_type_clear_constraints(struct ovsdb_base_type
*base
)
317 enum ovsdb_atomic_type type
= base
->type
;
318 ovsdb_base_type_destroy(base
);
319 ovsdb_base_type_init(base
, type
);
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
, *enum_
;
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
);
371 enum_
= ovsdb_parser_member(&parser
, "enum", OP_ANY
| OP_OPTIONAL
);
373 base
->enum_
= xmalloc(sizeof *base
->enum_
);
374 error
= ovsdb_datum_from_json(
375 base
->enum_
, ovsdb_base_type_get_enum_type(base
->type
),
381 } else if (base
->type
== OVSDB_TYPE_INTEGER
) {
382 const struct json
*min
, *max
;
384 min
= ovsdb_parser_member(&parser
, "minInteger",
385 OP_INTEGER
| OP_OPTIONAL
);
386 max
= ovsdb_parser_member(&parser
, "maxInteger",
387 OP_INTEGER
| OP_OPTIONAL
);
388 base
->u
.integer
.min
= min
? min
->u
.integer
: INT64_MIN
;
389 base
->u
.integer
.max
= max
? max
->u
.integer
: INT64_MAX
;
390 if (base
->u
.integer
.min
> base
->u
.integer
.max
) {
391 error
= ovsdb_syntax_error(json
, NULL
,
392 "minInteger exceeds maxInteger");
394 } else if (base
->type
== OVSDB_TYPE_REAL
) {
395 const struct json
*min
, *max
;
397 min
= ovsdb_parser_member(&parser
, "minReal", OP_NUMBER
| OP_OPTIONAL
);
398 max
= ovsdb_parser_member(&parser
, "maxReal", OP_NUMBER
| OP_OPTIONAL
);
399 base
->u
.real
.min
= min
? json_real(min
) : -DBL_MAX
;
400 base
->u
.real
.max
= max
? json_real(max
) : DBL_MAX
;
401 if (base
->u
.real
.min
> base
->u
.real
.max
) {
402 error
= ovsdb_syntax_error(json
, NULL
, "minReal exceeds maxReal");
404 } else if (base
->type
== OVSDB_TYPE_STRING
) {
406 error
= parse_optional_uint(&parser
, "minLength",
407 &base
->u
.string
.minLen
);
410 error
= parse_optional_uint(&parser
, "maxLength",
411 &base
->u
.string
.maxLen
);
413 if (!error
&& base
->u
.string
.minLen
> base
->u
.string
.maxLen
) {
414 error
= ovsdb_syntax_error(json
, NULL
,
415 "minLength exceeds maxLength");
417 } else if (base
->type
== OVSDB_TYPE_UUID
) {
418 const struct json
*refTable
;
420 refTable
= ovsdb_parser_member(&parser
, "refTable",
421 OP_ID
| OP_OPTIONAL
);
423 const struct json
*refType
;
425 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. */
431 refType
= ovsdb_parser_member(&parser
, "refType",
432 OP_ID
| OP_OPTIONAL
);
434 const char *refType_s
= json_string(refType
);
435 if (!strcmp(refType_s
, "strong")) {
436 base
->u
.uuid
.refType
= OVSDB_REF_STRONG
;
437 } else if (!strcmp(refType_s
, "weak")) {
438 base
->u
.uuid
.refType
= OVSDB_REF_WEAK
;
440 error
= ovsdb_syntax_error(json
, NULL
, "refType must be "
441 "\"strong\" or \"weak\" (not "
442 "\"%s\")", refType_s
);
445 base
->u
.uuid
.refType
= OVSDB_REF_STRONG
;
451 ovsdb_error_destroy(ovsdb_parser_finish(&parser
));
453 error
= ovsdb_parser_finish(&parser
);
456 ovsdb_base_type_destroy(base
);
457 base
->type
= OVSDB_TYPE_VOID
;
463 ovsdb_base_type_to_json(const struct ovsdb_base_type
*base
)
467 if (!ovsdb_base_type_has_constraints(base
)) {
468 return json_string_create(ovsdb_atomic_type_to_string(base
->type
));
471 json
= json_object_create();
472 json_object_put_string(json
, "type",
473 ovsdb_atomic_type_to_string(base
->type
));
476 const struct ovsdb_type
*type
;
478 type
= ovsdb_base_type_get_enum_type(base
->type
);
479 json_object_put(json
, "enum", ovsdb_datum_to_json(base
->enum_
, type
));
482 switch (base
->type
) {
483 case OVSDB_TYPE_VOID
:
486 case OVSDB_TYPE_INTEGER
:
487 if (base
->u
.integer
.min
!= INT64_MIN
) {
488 json_object_put(json
, "minInteger",
489 json_integer_create(base
->u
.integer
.min
));
491 if (base
->u
.integer
.max
!= INT64_MAX
) {
492 json_object_put(json
, "maxInteger",
493 json_integer_create(base
->u
.integer
.max
));
497 case OVSDB_TYPE_REAL
:
498 if (base
->u
.real
.min
!= -DBL_MAX
) {
499 json_object_put(json
, "minReal",
500 json_real_create(base
->u
.real
.min
));
502 if (base
->u
.real
.max
!= DBL_MAX
) {
503 json_object_put(json
, "maxReal",
504 json_real_create(base
->u
.real
.max
));
508 case OVSDB_TYPE_BOOLEAN
:
511 case OVSDB_TYPE_STRING
:
512 if (base
->u
.string
.minLen
!= 0) {
513 json_object_put(json
, "minLength",
514 json_integer_create(base
->u
.string
.minLen
));
516 if (base
->u
.string
.maxLen
!= UINT_MAX
) {
517 json_object_put(json
, "maxLength",
518 json_integer_create(base
->u
.string
.maxLen
));
522 case OVSDB_TYPE_UUID
:
523 if (base
->u
.uuid
.refTableName
) {
524 json_object_put_string(json
, "refTable",
525 base
->u
.uuid
.refTableName
);
526 if (base
->u
.uuid
.refType
== OVSDB_REF_WEAK
) {
527 json_object_put_string(json
, "refType", "weak");
545 ovsdb_type_clone(struct ovsdb_type
*dst
, const struct ovsdb_type
*src
)
547 ovsdb_base_type_clone(&dst
->key
, &src
->key
);
548 ovsdb_base_type_clone(&dst
->value
, &src
->value
);
549 dst
->n_min
= src
->n_min
;
550 dst
->n_max
= src
->n_max
;
554 ovsdb_type_destroy(struct ovsdb_type
*type
)
556 ovsdb_base_type_destroy(&type
->key
);
557 ovsdb_base_type_destroy(&type
->value
);
561 ovsdb_type_is_valid(const struct ovsdb_type
*type
)
563 return (type
->key
.type
!= OVSDB_TYPE_VOID
564 && ovsdb_base_type_is_valid(&type
->key
)
565 && ovsdb_base_type_is_valid(&type
->value
)
567 && type
->n_max
>= 1);
570 static struct ovsdb_error
*
571 n_from_json(const struct json
*json
, unsigned int *n
)
575 } else if (json
->type
== JSON_INTEGER
576 && json
->u
.integer
>= 0 && json
->u
.integer
< UINT_MAX
) {
577 *n
= json
->u
.integer
;
580 return ovsdb_syntax_error(json
, NULL
, "bad min or max value");
585 ovsdb_type_to_english(const struct ovsdb_type
*type
)
587 const char *key
= ovsdb_atomic_type_to_string(type
->key
.type
);
588 const char *value
= ovsdb_atomic_type_to_string(type
->value
.type
);
589 if (ovsdb_type_is_scalar(type
)) {
592 struct ds s
= DS_EMPTY_INITIALIZER
;
593 ds_put_cstr(&s
, ovsdb_type_is_set(type
) ? "set" : "map");
594 if (type
->n_max
== UINT_MAX
) {
596 ds_put_format(&s
, " of %u or more", type
->n_min
);
598 ds_put_cstr(&s
, " of");
600 } else if (type
->n_min
) {
601 ds_put_format(&s
, " of %u to %u", type
->n_min
, type
->n_max
);
603 ds_put_format(&s
, " of up to %u", type
->n_max
);
605 if (ovsdb_type_is_set(type
)) {
606 ds_put_format(&s
, " %ss", key
);
608 ds_put_format(&s
, " (%s, %s) pairs", key
, value
);
615 ovsdb_type_from_json(struct ovsdb_type
*type
, const struct json
*json
)
617 ovsdb_base_type_init(&type
->value
, OVSDB_TYPE_VOID
);
621 if (json
->type
== JSON_STRING
) {
622 return ovsdb_base_type_from_json(&type
->key
, json
);
623 } else if (json
->type
== JSON_OBJECT
) {
624 const struct json
*key
, *value
, *min
, *max
;
625 struct ovsdb_error
*error
;
626 struct ovsdb_parser parser
;
628 ovsdb_parser_init(&parser
, json
, "ovsdb type");
629 key
= ovsdb_parser_member(&parser
, "key", OP_STRING
| OP_OBJECT
);
630 value
= ovsdb_parser_member(&parser
, "value",
631 OP_STRING
| OP_OBJECT
| OP_OPTIONAL
);
632 min
= ovsdb_parser_member(&parser
, "min", OP_INTEGER
| OP_OPTIONAL
);
633 max
= ovsdb_parser_member(&parser
, "max",
634 OP_INTEGER
| OP_STRING
| OP_OPTIONAL
);
635 error
= ovsdb_parser_finish(&parser
);
640 error
= ovsdb_base_type_from_json(&type
->key
, key
);
646 error
= ovsdb_base_type_from_json(&type
->value
, value
);
652 error
= n_from_json(min
, &type
->n_min
);
657 if (max
&& max
->type
== JSON_STRING
658 && !strcmp(max
->u
.string
, "unlimited")) {
659 type
->n_max
= UINT_MAX
;
661 error
= n_from_json(max
, &type
->n_max
);
667 if (!ovsdb_type_is_valid(type
)) {
668 return ovsdb_syntax_error(json
, NULL
,
669 "ovsdb type fails constraint checks");
674 return ovsdb_syntax_error(json
, NULL
, "ovsdb type expected");
679 ovsdb_type_to_json(const struct ovsdb_type
*type
)
681 if (ovsdb_type_is_scalar(type
)
682 && !ovsdb_base_type_has_constraints(&type
->key
)) {
683 return ovsdb_base_type_to_json(&type
->key
);
685 struct json
*json
= json_object_create();
686 json_object_put(json
, "key", ovsdb_base_type_to_json(&type
->key
));
687 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
688 json_object_put(json
, "value",
689 ovsdb_base_type_to_json(&type
->value
));
691 if (type
->n_min
!= 1) {
692 json_object_put(json
, "min", json_integer_create(type
->n_min
));
694 if (type
->n_max
== UINT_MAX
) {
695 json_object_put_string(json
, "max", "unlimited");
696 } else if (type
->n_max
!= 1) {
697 json_object_put(json
, "max", json_integer_create(type
->n_max
));