]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-types.c
Rename NOT_REACHED to OVS_NOT_REACHED
[mirror_ovs.git] / lib / ovsdb-types.c
1 /* Copyright (c) 2009, 2010, 2011, 2013 Nicira, Inc.
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 "ovs-thread.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
29
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);
40 \f
41 /* ovsdb_atomic_type */
42 const char *
43 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
44 {
45 switch (type) {
46 case OVSDB_TYPE_VOID:
47 return "void";
48
49 case OVSDB_TYPE_INTEGER:
50 return "integer";
51
52 case OVSDB_TYPE_REAL:
53 return "real";
54
55 case OVSDB_TYPE_BOOLEAN:
56 return "boolean";
57
58 case OVSDB_TYPE_STRING:
59 return "string";
60
61 case OVSDB_TYPE_UUID:
62 return "uuid";
63
64 case OVSDB_N_TYPES:
65 default:
66 return "<invalid>";
67 }
68 }
69
70 struct json *
71 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
72 {
73 return json_string_create(ovsdb_atomic_type_to_string(type));
74 }
75
76 bool
77 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
78 {
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;
89 } else {
90 return false;
91 }
92 return true;
93 }
94
95 struct ovsdb_error *
96 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
97 const struct json *json)
98 {
99 if (json->type == JSON_STRING) {
100 if (ovsdb_atomic_type_from_string(json_string(json), type)) {
101 return NULL;
102 } else {
103 *type = OVSDB_TYPE_VOID;
104 return ovsdb_syntax_error(json, NULL,
105 "\"%s\" is not an atomic-type",
106 json_string(json));
107 }
108 } else {
109 *type = OVSDB_TYPE_VOID;
110 return ovsdb_syntax_error(json, NULL, "atomic-type expected");
111 }
112 }
113 \f
114 /* ovsdb_base_type */
115
116 void
117 ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
118 {
119 base->type = type;
120 base->enum_ = NULL;
121
122 switch (base->type) {
123 case OVSDB_TYPE_VOID:
124 break;
125
126 case OVSDB_TYPE_INTEGER:
127 base->u.integer.min = INT64_MIN;
128 base->u.integer.max = INT64_MAX;
129 break;
130
131 case OVSDB_TYPE_REAL:
132 base->u.real.min = -DBL_MAX;
133 base->u.real.max = DBL_MAX;
134 break;
135
136 case OVSDB_TYPE_BOOLEAN:
137 break;
138
139 case OVSDB_TYPE_STRING:
140 base->u.string.minLen = 0;
141 base->u.string.maxLen = UINT_MAX;
142 break;
143
144 case OVSDB_TYPE_UUID:
145 base->u.uuid.refTableName = NULL;
146 base->u.uuid.refTable = NULL;
147 break;
148
149 case OVSDB_N_TYPES:
150 OVS_NOT_REACHED();
151
152 default:
153 OVS_NOT_REACHED();
154 }
155 }
156
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)
161 {
162 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
163 static struct ovsdb_type *types[OVSDB_N_TYPES];
164
165 if (ovsthread_once_start(&once)) {
166 enum ovsdb_atomic_type i;
167
168 for (i = 0; i < OVSDB_N_TYPES; i++) {
169 struct ovsdb_type *type;
170
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);
174 type->n_min = 1;
175 type->n_max = UINT_MAX;
176 }
177
178 ovsthread_once_done(&once);
179 }
180 return types[atomic_type];
181 }
182
183 void
184 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
185 const struct ovsdb_base_type *src)
186 {
187 *dst = *src;
188
189 if (src->enum_) {
190 dst->enum_ = xmalloc(sizeof *dst->enum_);
191 ovsdb_datum_clone(dst->enum_, src->enum_,
192 ovsdb_base_type_get_enum_type(dst->type));
193 }
194
195 switch (dst->type) {
196 case OVSDB_TYPE_VOID:
197 case OVSDB_TYPE_INTEGER:
198 case OVSDB_TYPE_REAL:
199 case OVSDB_TYPE_BOOLEAN:
200 break;
201
202 case OVSDB_TYPE_STRING:
203 break;
204
205 case OVSDB_TYPE_UUID:
206 if (dst->u.uuid.refTableName) {
207 dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
208 }
209 break;
210
211 case OVSDB_N_TYPES:
212 default:
213 OVS_NOT_REACHED();
214 }
215 }
216
217 void
218 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
219 {
220 if (base) {
221 if (base->enum_) {
222 ovsdb_datum_destroy(base->enum_,
223 ovsdb_base_type_get_enum_type(base->type));
224 free(base->enum_);
225 }
226
227 switch (base->type) {
228 case OVSDB_TYPE_VOID:
229 case OVSDB_TYPE_INTEGER:
230 case OVSDB_TYPE_REAL:
231 case OVSDB_TYPE_BOOLEAN:
232 break;
233
234 case OVSDB_TYPE_STRING:
235 break;
236
237 case OVSDB_TYPE_UUID:
238 free(base->u.uuid.refTableName);
239 break;
240
241 case OVSDB_N_TYPES:
242 OVS_NOT_REACHED();
243
244 default:
245 OVS_NOT_REACHED();
246 }
247 }
248 }
249
250 bool
251 ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
252 {
253 switch (base->type) {
254 case OVSDB_TYPE_VOID:
255 return true;
256
257 case OVSDB_TYPE_INTEGER:
258 return base->u.integer.min <= base->u.integer.max;
259
260 case OVSDB_TYPE_REAL:
261 return base->u.real.min <= base->u.real.max;
262
263 case OVSDB_TYPE_BOOLEAN:
264 return true;
265
266 case OVSDB_TYPE_STRING:
267 return base->u.string.minLen <= base->u.string.maxLen;
268
269 case OVSDB_TYPE_UUID:
270 return true;
271
272 case OVSDB_N_TYPES:
273 default:
274 return false;
275 }
276 }
277
278 bool
279 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
280 {
281 if (base->enum_) {
282 return true;
283 }
284
285 switch (base->type) {
286 case OVSDB_TYPE_VOID:
287 OVS_NOT_REACHED();
288
289 case OVSDB_TYPE_INTEGER:
290 return (base->u.integer.min != INT64_MIN
291 || base->u.integer.max != INT64_MAX);
292
293 case OVSDB_TYPE_REAL:
294 return (base->u.real.min != -DBL_MAX
295 || base->u.real.max != DBL_MAX);
296
297 case OVSDB_TYPE_BOOLEAN:
298 return false;
299
300 case OVSDB_TYPE_STRING:
301 return base->u.string.minLen != 0 || base->u.string.maxLen != UINT_MAX;
302
303 case OVSDB_TYPE_UUID:
304 return base->u.uuid.refTableName != NULL;
305
306 case OVSDB_N_TYPES:
307 OVS_NOT_REACHED();
308
309 default:
310 OVS_NOT_REACHED();
311 }
312 }
313
314 void
315 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
316 {
317 enum ovsdb_atomic_type type = base->type;
318 ovsdb_base_type_destroy(base);
319 ovsdb_base_type_init(base, type);
320 }
321
322 static struct ovsdb_error *
323 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
324 unsigned int *uint)
325 {
326 const struct json *json;
327
328 json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
329 if (json) {
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",
333 member, UINT_MAX);
334 }
335 *uint = json->u.integer;
336 }
337 return NULL;
338 }
339
340 struct ovsdb_error *
341 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
342 const struct json *json)
343 {
344 struct ovsdb_parser parser;
345 struct ovsdb_error *error;
346 const struct json *type, *enum_;
347
348 if (json->type == JSON_STRING) {
349 error = ovsdb_atomic_type_from_json(&base->type, json);
350 if (error) {
351 return error;
352 }
353 ovsdb_base_type_init(base, base->type);
354 return NULL;
355 }
356
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);
362 }
363
364 error = ovsdb_atomic_type_from_json(&base->type, type);
365 if (error) {
366 return error;
367 }
368
369 ovsdb_base_type_init(base, base->type);
370
371 enum_ = ovsdb_parser_member(&parser, "enum", OP_ANY | OP_OPTIONAL);
372 if (enum_) {
373 base->enum_ = xmalloc(sizeof *base->enum_);
374 error = ovsdb_datum_from_json(
375 base->enum_, ovsdb_base_type_get_enum_type(base->type),
376 enum_, NULL);
377 if (error) {
378 free(base->enum_);
379 base->enum_ = NULL;
380 }
381 } else if (base->type == OVSDB_TYPE_INTEGER) {
382 const struct json *min, *max;
383
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");
393 }
394 } else if (base->type == OVSDB_TYPE_REAL) {
395 const struct json *min, *max;
396
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");
403 }
404 } else if (base->type == OVSDB_TYPE_STRING) {
405 if (!error) {
406 error = parse_optional_uint(&parser, "minLength",
407 &base->u.string.minLen);
408 }
409 if (!error) {
410 error = parse_optional_uint(&parser, "maxLength",
411 &base->u.string.maxLen);
412 }
413 if (!error && base->u.string.minLen > base->u.string.maxLen) {
414 error = ovsdb_syntax_error(json, NULL,
415 "minLength exceeds maxLength");
416 }
417 } else if (base->type == OVSDB_TYPE_UUID) {
418 const struct json *refTable;
419
420 refTable = ovsdb_parser_member(&parser, "refTable",
421 OP_ID | OP_OPTIONAL);
422 if (refTable) {
423 const struct json *refType;
424
425 base->u.uuid.refTableName = xstrdup(refTable->u.string);
426
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. */
430
431 refType = ovsdb_parser_member(&parser, "refType",
432 OP_ID | OP_OPTIONAL);
433 if (refType) {
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;
439 } else {
440 error = ovsdb_syntax_error(json, NULL, "refType must be "
441 "\"strong\" or \"weak\" (not "
442 "\"%s\")", refType_s);
443 }
444 } else {
445 base->u.uuid.refType = OVSDB_REF_STRONG;
446 }
447 }
448 }
449
450 if (error) {
451 ovsdb_error_destroy(ovsdb_parser_finish(&parser));
452 } else {
453 error = ovsdb_parser_finish(&parser);
454 }
455 if (error) {
456 ovsdb_base_type_destroy(base);
457 base->type = OVSDB_TYPE_VOID;
458 }
459 return error;
460 }
461
462 struct json *
463 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
464 {
465 struct json *json;
466
467 if (!ovsdb_base_type_has_constraints(base)) {
468 return json_string_create(ovsdb_atomic_type_to_string(base->type));
469 }
470
471 json = json_object_create();
472 json_object_put_string(json, "type",
473 ovsdb_atomic_type_to_string(base->type));
474
475 if (base->enum_) {
476 const struct ovsdb_type *type;
477
478 type = ovsdb_base_type_get_enum_type(base->type);
479 json_object_put(json, "enum", ovsdb_datum_to_json(base->enum_, type));
480 }
481
482 switch (base->type) {
483 case OVSDB_TYPE_VOID:
484 OVS_NOT_REACHED();
485
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));
490 }
491 if (base->u.integer.max != INT64_MAX) {
492 json_object_put(json, "maxInteger",
493 json_integer_create(base->u.integer.max));
494 }
495 break;
496
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));
501 }
502 if (base->u.real.max != DBL_MAX) {
503 json_object_put(json, "maxReal",
504 json_real_create(base->u.real.max));
505 }
506 break;
507
508 case OVSDB_TYPE_BOOLEAN:
509 break;
510
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));
515 }
516 if (base->u.string.maxLen != UINT_MAX) {
517 json_object_put(json, "maxLength",
518 json_integer_create(base->u.string.maxLen));
519 }
520 break;
521
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");
528 }
529 }
530 break;
531
532 case OVSDB_N_TYPES:
533 OVS_NOT_REACHED();
534
535 default:
536 OVS_NOT_REACHED();
537 }
538
539 return json;
540 }
541 \f
542 /* ovsdb_type */
543
544 void
545 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
546 {
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;
551 }
552
553 void
554 ovsdb_type_destroy(struct ovsdb_type *type)
555 {
556 ovsdb_base_type_destroy(&type->key);
557 ovsdb_base_type_destroy(&type->value);
558 }
559
560 bool
561 ovsdb_type_is_valid(const struct ovsdb_type *type)
562 {
563 return (type->key.type != OVSDB_TYPE_VOID
564 && ovsdb_base_type_is_valid(&type->key)
565 && ovsdb_base_type_is_valid(&type->value)
566 && type->n_min <= 1
567 && type->n_max >= 1);
568 }
569
570 static struct ovsdb_error *
571 n_from_json(const struct json *json, unsigned int *n)
572 {
573 if (!json) {
574 return NULL;
575 } else if (json->type == JSON_INTEGER
576 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
577 *n = json->u.integer;
578 return NULL;
579 } else {
580 return ovsdb_syntax_error(json, NULL, "bad min or max value");
581 }
582 }
583
584 char *
585 ovsdb_type_to_english(const struct ovsdb_type *type)
586 {
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)) {
590 return xstrdup(key);
591 } else {
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) {
595 if (type->n_min) {
596 ds_put_format(&s, " of %u or more", type->n_min);
597 } else {
598 ds_put_cstr(&s, " of");
599 }
600 } else if (type->n_min) {
601 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
602 } else {
603 ds_put_format(&s, " of up to %u", type->n_max);
604 }
605 if (ovsdb_type_is_set(type)) {
606 ds_put_format(&s, " %ss", key);
607 } else {
608 ds_put_format(&s, " (%s, %s) pairs", key, value);
609 }
610 return ds_cstr(&s);
611 }
612 }
613
614 struct ovsdb_error *
615 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
616 {
617 ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
618 type->n_min = 1;
619 type->n_max = 1;
620
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;
627
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);
636 if (error) {
637 return error;
638 }
639
640 error = ovsdb_base_type_from_json(&type->key, key);
641 if (error) {
642 return error;
643 }
644
645 if (value) {
646 error = ovsdb_base_type_from_json(&type->value, value);
647 if (error) {
648 return error;
649 }
650 }
651
652 error = n_from_json(min, &type->n_min);
653 if (error) {
654 return error;
655 }
656
657 if (max && max->type == JSON_STRING
658 && !strcmp(max->u.string, "unlimited")) {
659 type->n_max = UINT_MAX;
660 } else {
661 error = n_from_json(max, &type->n_max);
662 if (error) {
663 return error;
664 }
665 }
666
667 if (!ovsdb_type_is_valid(type)) {
668 return ovsdb_syntax_error(json, NULL,
669 "ovsdb type fails constraint checks");
670 }
671
672 return NULL;
673 } else {
674 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
675 }
676 }
677
678 struct json *
679 ovsdb_type_to_json(const struct ovsdb_type *type)
680 {
681 if (ovsdb_type_is_scalar(type)
682 && !ovsdb_base_type_has_constraints(&type->key)) {
683 return ovsdb_base_type_to_json(&type->key);
684 } else {
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));
690 }
691 if (type->n_min != 1) {
692 json_object_put(json, "min", json_integer_create(type->n_min));
693 }
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));
698 }
699 return json;
700 }
701 }