ovsdb: Add support for "enum" constraints.
[sliver-openvswitch.git] / lib / ovsdb-types.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
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 "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
28
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);
39 \f
40 /* ovsdb_atomic_type */
41 const char *
42 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
43 {
44     switch (type) {
45     case OVSDB_TYPE_VOID:
46         return "void";
47
48     case OVSDB_TYPE_INTEGER:
49         return "integer";
50
51     case OVSDB_TYPE_REAL:
52         return "real";
53
54     case OVSDB_TYPE_BOOLEAN:
55         return "boolean";
56
57     case OVSDB_TYPE_STRING:
58         return "string";
59
60     case OVSDB_TYPE_UUID:
61         return "uuid";
62
63     case OVSDB_N_TYPES:
64     default:
65         return "<invalid>";
66     }
67 }
68
69 struct json *
70 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
71 {
72     return json_string_create(ovsdb_atomic_type_to_string(type));
73 }
74
75 bool
76 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
77 {
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;
88     } else {
89         return false;
90     }
91     return true;
92 }
93
94 struct ovsdb_error *
95 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
96                             const struct json *json)
97 {
98     if (json->type == JSON_STRING) {
99         if (ovsdb_atomic_type_from_string(json_string(json), type)) {
100             return NULL;
101         } else {
102             *type = OVSDB_TYPE_VOID;
103             return ovsdb_syntax_error(json, NULL,
104                                       "\"%s\" is not an atomic-type",
105                                       json_string(json));
106         }
107     } else {
108         *type = OVSDB_TYPE_VOID;
109         return ovsdb_syntax_error(json, NULL, "atomic-type expected");
110     }
111 }
112 \f
113 /* ovsdb_base_type */
114
115 void
116 ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
117 {
118     base->type = type;
119     base->enum_ = NULL;
120
121     switch (base->type) {
122     case OVSDB_TYPE_VOID:
123         break;
124
125     case OVSDB_TYPE_INTEGER:
126         base->u.integer.min = INT64_MIN;
127         base->u.integer.max = INT64_MAX;
128         break;
129
130     case OVSDB_TYPE_REAL:
131         base->u.real.min = -DBL_MAX;
132         base->u.real.max = DBL_MAX;
133         break;
134
135     case OVSDB_TYPE_BOOLEAN:
136         break;
137
138     case OVSDB_TYPE_STRING:
139         base->u.string.minLen = 0;
140         base->u.string.maxLen = UINT_MAX;
141         break;
142
143     case OVSDB_TYPE_UUID:
144         base->u.uuid.refTableName = NULL;
145         base->u.uuid.refTable = NULL;
146         break;
147
148     case OVSDB_N_TYPES:
149         NOT_REACHED();
150
151     default:
152         NOT_REACHED();
153     }
154 }
155
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)
160 {
161     static struct ovsdb_type *types[OVSDB_N_TYPES];
162
163     if (!types[atomic_type]) {
164         struct ovsdb_type *type;
165
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);
169         type->n_min = 1;
170         type->n_max = UINT_MAX;
171     }
172     return types[atomic_type];
173 }
174
175 void
176 ovsdb_base_type_clone(struct ovsdb_base_type *dst,
177                       const struct ovsdb_base_type *src)
178 {
179     *dst = *src;
180
181     if (src->enum_) {
182         dst->enum_ = xmalloc(sizeof *dst->enum_);
183         ovsdb_datum_clone(dst->enum_, src->enum_,
184                           ovsdb_base_type_get_enum_type(dst->type));
185     }
186
187     switch (dst->type) {
188     case OVSDB_TYPE_VOID:
189     case OVSDB_TYPE_INTEGER:
190     case OVSDB_TYPE_REAL:
191     case OVSDB_TYPE_BOOLEAN:
192         break;
193
194     case OVSDB_TYPE_STRING:
195         break;
196
197     case OVSDB_TYPE_UUID:
198         if (dst->u.uuid.refTableName) {
199             dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
200         }
201         break;
202
203     case OVSDB_N_TYPES:
204     default:
205         NOT_REACHED();
206     }
207 }
208
209 void
210 ovsdb_base_type_destroy(struct ovsdb_base_type *base)
211 {
212     if (base) {
213         if (base->enum_) {
214             ovsdb_datum_destroy(base->enum_,
215                                 ovsdb_base_type_get_enum_type(base->type));
216             free(base->enum_);
217         }
218
219         switch (base->type) {
220         case OVSDB_TYPE_VOID:
221         case OVSDB_TYPE_INTEGER:
222         case OVSDB_TYPE_REAL:
223         case OVSDB_TYPE_BOOLEAN:
224             break;
225
226         case OVSDB_TYPE_STRING:
227             break;
228
229         case OVSDB_TYPE_UUID:
230             free(base->u.uuid.refTableName);
231             break;
232
233         case OVSDB_N_TYPES:
234             NOT_REACHED();
235
236         default:
237             NOT_REACHED();
238         }
239     }
240 }
241
242 bool
243 ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
244 {
245     switch (base->type) {
246     case OVSDB_TYPE_VOID:
247         return true;
248
249     case OVSDB_TYPE_INTEGER:
250         return base->u.integer.min <= base->u.integer.max;
251
252     case OVSDB_TYPE_REAL:
253         return base->u.real.min <= base->u.real.max;
254
255     case OVSDB_TYPE_BOOLEAN:
256         return true;
257
258     case OVSDB_TYPE_STRING:
259         return base->u.string.minLen <= base->u.string.maxLen;
260
261     case OVSDB_TYPE_UUID:
262         return true;
263
264     case OVSDB_N_TYPES:
265     default:
266         return false;
267     }
268 }
269
270 bool
271 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
272 {
273     if (base->enum_) {
274         return true;
275     }
276
277     switch (base->type) {
278     case OVSDB_TYPE_VOID:
279         NOT_REACHED();
280
281     case OVSDB_TYPE_INTEGER:
282         return (base->u.integer.min != INT64_MIN
283                 || base->u.integer.max != INT64_MAX);
284
285     case OVSDB_TYPE_REAL:
286         return (base->u.real.min != -DBL_MAX
287                 || base->u.real.max != DBL_MAX);
288
289     case OVSDB_TYPE_BOOLEAN:
290         return false;
291
292     case OVSDB_TYPE_STRING:
293         return base->u.string.minLen != 0 || base->u.string.maxLen != UINT_MAX;
294
295     case OVSDB_TYPE_UUID:
296         return base->u.uuid.refTableName != NULL;
297
298     case OVSDB_N_TYPES:
299         NOT_REACHED();
300
301     default:
302         NOT_REACHED();
303     }
304 }
305
306 void
307 ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
308 {
309     enum ovsdb_atomic_type type = base->type;
310     ovsdb_base_type_destroy(base);
311     ovsdb_base_type_init(base, type);
312 }
313
314 static struct ovsdb_error *
315 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
316                     unsigned int *uint)
317 {
318     const struct json *json;
319
320     json = ovsdb_parser_member(parser, member, OP_INTEGER | OP_OPTIONAL);
321     if (json) {
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",
325                                       member, UINT_MAX);
326         }
327         *uint = json->u.integer;
328     }
329     return NULL;
330 }
331
332 struct ovsdb_error *
333 ovsdb_base_type_from_json(struct ovsdb_base_type *base,
334                           const struct json *json)
335 {
336     struct ovsdb_parser parser;
337     struct ovsdb_error *error;
338     const struct json *type, *enum_;
339
340     if (json->type == JSON_STRING) {
341         error = ovsdb_atomic_type_from_json(&base->type, json);
342         if (error) {
343             return error;
344         }
345         ovsdb_base_type_init(base, base->type);
346         return NULL;
347     }
348
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);
354     }
355
356     error = ovsdb_atomic_type_from_json(&base->type, type);
357     if (error) {
358         return error;
359     }
360
361     ovsdb_base_type_init(base, base->type);
362
363     enum_ = ovsdb_parser_member(&parser, "enum", OP_ANY | OP_OPTIONAL);
364     if (enum_) {
365         base->enum_ = xmalloc(sizeof *base->enum_);
366         error = ovsdb_datum_from_json(
367             base->enum_, ovsdb_base_type_get_enum_type(base->type),
368             enum_, NULL);
369         if (error) {
370             free(base->enum_);
371             base->enum_ = NULL;
372         }
373     } else if (base->type == OVSDB_TYPE_INTEGER) {
374         const struct json *min, *max;
375
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");
385         }
386     } else if (base->type == OVSDB_TYPE_REAL) {
387         const struct json *min, *max;
388
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");
395         }
396     } else if (base->type == OVSDB_TYPE_STRING) {
397         if (!error) {
398             error = parse_optional_uint(&parser, "minLength",
399                                         &base->u.string.minLen);
400         }
401         if (!error) {
402             error = parse_optional_uint(&parser, "maxLength",
403                                         &base->u.string.maxLen);
404         }
405         if (!error && base->u.string.minLen > base->u.string.maxLen) {
406             error = ovsdb_syntax_error(json, NULL,
407                                        "minLength exceeds maxLength");
408         }
409     } else if (base->type == OVSDB_TYPE_UUID) {
410         const struct json *refTable;
411
412         refTable = ovsdb_parser_member(&parser, "refTable",
413                                        OP_ID | OP_OPTIONAL);
414         if (refTable) {
415             base->u.uuid.refTableName = xstrdup(refTable->u.string);
416             /* We can't set base->u.uuid.refTable here because we don't have
417              * enough context (we might not even be running in ovsdb-server).
418              * ovsdb_create() will set refTable later. */
419         }
420     }
421
422     if (error) {
423         ovsdb_error_destroy(ovsdb_parser_finish(&parser));
424     } else {
425         error = ovsdb_parser_finish(&parser);
426     }
427     if (error) {
428         ovsdb_base_type_destroy(base);
429         base->type = OVSDB_TYPE_VOID;
430     }
431     return error;
432 }
433
434 struct json *
435 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
436 {
437     struct json *json;
438
439     if (!ovsdb_base_type_has_constraints(base)) {
440         return json_string_create(ovsdb_atomic_type_to_string(base->type));
441     }
442
443     json = json_object_create();
444     json_object_put_string(json, "type",
445                            ovsdb_atomic_type_to_string(base->type));
446
447     if (base->enum_) {
448         const struct ovsdb_type *type;
449
450         type = ovsdb_base_type_get_enum_type(base->type);
451         json_object_put(json, "enum", ovsdb_datum_to_json(base->enum_, type));
452     }
453
454     switch (base->type) {
455     case OVSDB_TYPE_VOID:
456         NOT_REACHED();
457
458     case OVSDB_TYPE_INTEGER:
459         if (base->u.integer.min != INT64_MIN) {
460             json_object_put(json, "minInteger",
461                             json_integer_create(base->u.integer.min));
462         }
463         if (base->u.integer.max != INT64_MAX) {
464             json_object_put(json, "maxInteger",
465                             json_integer_create(base->u.integer.max));
466         }
467         break;
468
469     case OVSDB_TYPE_REAL:
470         if (base->u.real.min != -DBL_MAX) {
471             json_object_put(json, "minReal",
472                             json_real_create(base->u.real.min));
473         }
474         if (base->u.real.max != DBL_MAX) {
475             json_object_put(json, "maxReal",
476                             json_real_create(base->u.real.max));
477         }
478         break;
479
480     case OVSDB_TYPE_BOOLEAN:
481         break;
482
483     case OVSDB_TYPE_STRING:
484         if (base->u.string.minLen != 0) {
485             json_object_put(json, "minLength",
486                             json_integer_create(base->u.string.minLen));
487         }
488         if (base->u.string.maxLen != UINT_MAX) {
489             json_object_put(json, "maxLength",
490                             json_integer_create(base->u.string.maxLen));
491         }
492         break;
493
494     case OVSDB_TYPE_UUID:
495         if (base->u.uuid.refTableName) {
496             json_object_put_string(json, "refTable",
497                                    base->u.uuid.refTableName);
498         }
499         break;
500
501     case OVSDB_N_TYPES:
502         NOT_REACHED();
503
504     default:
505         NOT_REACHED();
506     }
507
508     return json;
509 }
510 \f
511 /* ovsdb_type */
512
513 void
514 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
515 {
516     ovsdb_base_type_clone(&dst->key, &src->key);
517     ovsdb_base_type_clone(&dst->value, &src->value);
518     dst->n_min = src->n_min;
519     dst->n_max = src->n_max;
520 }
521
522 void
523 ovsdb_type_destroy(struct ovsdb_type *type)
524 {
525     ovsdb_base_type_destroy(&type->key);
526     ovsdb_base_type_destroy(&type->value);
527 }
528
529 bool
530 ovsdb_type_is_valid(const struct ovsdb_type *type)
531 {
532     return (type->key.type != OVSDB_TYPE_VOID
533             && ovsdb_base_type_is_valid(&type->key)
534             && ovsdb_base_type_is_valid(&type->value)
535             && type->n_min <= 1
536             && type->n_min <= type->n_max
537             && type->n_max >= 1);
538 }
539
540 static struct ovsdb_error *
541 n_from_json(const struct json *json, unsigned int *n)
542 {
543     if (!json) {
544         return NULL;
545     } else if (json->type == JSON_INTEGER
546                && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
547         *n = json->u.integer;
548         return NULL;
549     } else {
550         return ovsdb_syntax_error(json, NULL, "bad min or max value");
551     }
552 }
553
554 char *
555 ovsdb_type_to_english(const struct ovsdb_type *type)
556 {
557     const char *key = ovsdb_atomic_type_to_string(type->key.type);
558     const char *value = ovsdb_atomic_type_to_string(type->value.type);
559     if (ovsdb_type_is_scalar(type)) {
560         return xstrdup(key);
561     } else {
562         struct ds s = DS_EMPTY_INITIALIZER;
563         ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
564         if (type->n_max == UINT_MAX) {
565             if (type->n_min) {
566                 ds_put_format(&s, " of %u or more", type->n_min);
567             } else {
568                 ds_put_cstr(&s, " of");
569             }
570         } else if (type->n_min) {
571             ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
572         } else {
573             ds_put_format(&s, " of up to %u", type->n_max);
574         }
575         if (ovsdb_type_is_set(type)) {
576             ds_put_format(&s, " %ss", key);
577         } else {
578             ds_put_format(&s, " (%s, %s) pairs", key, value);
579         }
580         return ds_cstr(&s);
581     }
582 }
583
584 struct ovsdb_error *
585 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
586 {
587     ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
588     type->n_min = 1;
589     type->n_max = 1;
590
591     if (json->type == JSON_STRING) {
592         return ovsdb_base_type_from_json(&type->key, json);
593     } else if (json->type == JSON_OBJECT) {
594         const struct json *key, *value, *min, *max;
595         struct ovsdb_error *error;
596         struct ovsdb_parser parser;
597
598         ovsdb_parser_init(&parser, json, "ovsdb type");
599         key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT);
600         value = ovsdb_parser_member(&parser, "value",
601                                     OP_STRING | OP_OBJECT | OP_OPTIONAL);
602         min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
603         max = ovsdb_parser_member(&parser, "max",
604                                   OP_INTEGER | OP_STRING | OP_OPTIONAL);
605         error = ovsdb_parser_finish(&parser);
606         if (error) {
607             return error;
608         }
609
610         error = ovsdb_base_type_from_json(&type->key, key);
611         if (error) {
612             return error;
613         }
614
615         if (value) {
616             error = ovsdb_base_type_from_json(&type->value, value);
617             if (error) {
618                 return error;
619             }
620         }
621
622         error = n_from_json(min, &type->n_min);
623         if (error) {
624             return error;
625         }
626
627         if (max && max->type == JSON_STRING
628             && !strcmp(max->u.string, "unlimited")) {
629             type->n_max = UINT_MAX;
630         } else {
631             error = n_from_json(max, &type->n_max);
632             if (error) {
633                 return error;
634             }
635         }
636
637         if (!ovsdb_type_is_valid(type)) {
638             return ovsdb_syntax_error(json, NULL,
639                                       "ovsdb type fails constraint checks");
640         }
641
642         return NULL;
643     } else {
644         return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
645     }
646 }
647
648 struct json *
649 ovsdb_type_to_json(const struct ovsdb_type *type)
650 {
651     if (ovsdb_type_is_scalar(type)
652         && !ovsdb_base_type_has_constraints(&type->key)) {
653         return ovsdb_base_type_to_json(&type->key);
654     } else {
655         struct json *json = json_object_create();
656         json_object_put(json, "key", ovsdb_base_type_to_json(&type->key));
657         if (type->value.type != OVSDB_TYPE_VOID) {
658             json_object_put(json, "value",
659                             ovsdb_base_type_to_json(&type->value));
660         }
661         if (type->n_min != 1) {
662             json_object_put(json, "min", json_integer_create(type->n_min));
663         }
664         if (type->n_max == UINT_MAX) {
665             json_object_put_string(json, "max", "unlimited");
666         } else if (type->n_max != 1) {
667             json_object_put(json, "max", json_integer_create(type->n_max));
668         }
669         return json;
670     }
671 }