Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / ovsdb-types.c
1 /* Copyright (c) 2009, 2010, 2011 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 "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             const struct json *refType;
416
417             base->u.uuid.refTableName = xstrdup(refTable->u.string);
418
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. */
422
423             refType = ovsdb_parser_member(&parser, "refType",
424                                           OP_ID | OP_OPTIONAL);
425             if (refType) {
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;
431                 } else {
432                     error = ovsdb_syntax_error(json, NULL, "refType must be "
433                                                "\"strong\" or \"weak\" (not "
434                                                "\"%s\")", refType_s);
435                 }
436             } else {
437                 base->u.uuid.refType = OVSDB_REF_STRONG;
438             }
439         }
440     }
441
442     if (error) {
443         ovsdb_error_destroy(ovsdb_parser_finish(&parser));
444     } else {
445         error = ovsdb_parser_finish(&parser);
446     }
447     if (error) {
448         ovsdb_base_type_destroy(base);
449         base->type = OVSDB_TYPE_VOID;
450     }
451     return error;
452 }
453
454 struct json *
455 ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
456 {
457     struct json *json;
458
459     if (!ovsdb_base_type_has_constraints(base)) {
460         return json_string_create(ovsdb_atomic_type_to_string(base->type));
461     }
462
463     json = json_object_create();
464     json_object_put_string(json, "type",
465                            ovsdb_atomic_type_to_string(base->type));
466
467     if (base->enum_) {
468         const struct ovsdb_type *type;
469
470         type = ovsdb_base_type_get_enum_type(base->type);
471         json_object_put(json, "enum", ovsdb_datum_to_json(base->enum_, type));
472     }
473
474     switch (base->type) {
475     case OVSDB_TYPE_VOID:
476         NOT_REACHED();
477
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));
482         }
483         if (base->u.integer.max != INT64_MAX) {
484             json_object_put(json, "maxInteger",
485                             json_integer_create(base->u.integer.max));
486         }
487         break;
488
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));
493         }
494         if (base->u.real.max != DBL_MAX) {
495             json_object_put(json, "maxReal",
496                             json_real_create(base->u.real.max));
497         }
498         break;
499
500     case OVSDB_TYPE_BOOLEAN:
501         break;
502
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));
507         }
508         if (base->u.string.maxLen != UINT_MAX) {
509             json_object_put(json, "maxLength",
510                             json_integer_create(base->u.string.maxLen));
511         }
512         break;
513
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");
520             }
521         }
522         break;
523
524     case OVSDB_N_TYPES:
525         NOT_REACHED();
526
527     default:
528         NOT_REACHED();
529     }
530
531     return json;
532 }
533 \f
534 /* ovsdb_type */
535
536 void
537 ovsdb_type_clone(struct ovsdb_type *dst, const struct ovsdb_type *src)
538 {
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;
543 }
544
545 void
546 ovsdb_type_destroy(struct ovsdb_type *type)
547 {
548     ovsdb_base_type_destroy(&type->key);
549     ovsdb_base_type_destroy(&type->value);
550 }
551
552 bool
553 ovsdb_type_is_valid(const struct ovsdb_type *type)
554 {
555     return (type->key.type != OVSDB_TYPE_VOID
556             && ovsdb_base_type_is_valid(&type->key)
557             && ovsdb_base_type_is_valid(&type->value)
558             && type->n_min <= 1
559             && type->n_max >= 1);
560 }
561
562 static struct ovsdb_error *
563 n_from_json(const struct json *json, unsigned int *n)
564 {
565     if (!json) {
566         return NULL;
567     } else if (json->type == JSON_INTEGER
568                && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
569         *n = json->u.integer;
570         return NULL;
571     } else {
572         return ovsdb_syntax_error(json, NULL, "bad min or max value");
573     }
574 }
575
576 char *
577 ovsdb_type_to_english(const struct ovsdb_type *type)
578 {
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)) {
582         return xstrdup(key);
583     } else {
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) {
587             if (type->n_min) {
588                 ds_put_format(&s, " of %u or more", type->n_min);
589             } else {
590                 ds_put_cstr(&s, " of");
591             }
592         } else if (type->n_min) {
593             ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
594         } else {
595             ds_put_format(&s, " of up to %u", type->n_max);
596         }
597         if (ovsdb_type_is_set(type)) {
598             ds_put_format(&s, " %ss", key);
599         } else {
600             ds_put_format(&s, " (%s, %s) pairs", key, value);
601         }
602         return ds_cstr(&s);
603     }
604 }
605
606 struct ovsdb_error *
607 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
608 {
609     ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID);
610     type->n_min = 1;
611     type->n_max = 1;
612
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;
619
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);
628         if (error) {
629             return error;
630         }
631
632         error = ovsdb_base_type_from_json(&type->key, key);
633         if (error) {
634             return error;
635         }
636
637         if (value) {
638             error = ovsdb_base_type_from_json(&type->value, value);
639             if (error) {
640                 return error;
641             }
642         }
643
644         error = n_from_json(min, &type->n_min);
645         if (error) {
646             return error;
647         }
648
649         if (max && max->type == JSON_STRING
650             && !strcmp(max->u.string, "unlimited")) {
651             type->n_max = UINT_MAX;
652         } else {
653             error = n_from_json(max, &type->n_max);
654             if (error) {
655                 return error;
656             }
657         }
658
659         if (!ovsdb_type_is_valid(type)) {
660             return ovsdb_syntax_error(json, NULL,
661                                       "ovsdb type fails constraint checks");
662         }
663
664         return NULL;
665     } else {
666         return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
667     }
668 }
669
670 struct json *
671 ovsdb_type_to_json(const struct ovsdb_type *type)
672 {
673     if (ovsdb_type_is_scalar(type)
674         && !ovsdb_base_type_has_constraints(&type->key)) {
675         return ovsdb_base_type_to_json(&type->key);
676     } else {
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));
682         }
683         if (type->n_min != 1) {
684             json_object_put(json, "min", json_integer_create(type->n_min));
685         }
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));
690         }
691         return json;
692     }
693 }