ovsdb: Add simple constraints.
[sliver-openvswitch.git] / lib / ovsdb-types.h
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 #ifndef OVSDB_TYPES_H
17 #define OVSDB_TYPES_H 1
18
19 #include <float.h>
20 #include <pcre.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include "compiler.h"
24 #include "uuid.h"
25
26 struct json;
27
28 /* An atomic type: one that OVSDB regards as a single unit of data. */
29 enum ovsdb_atomic_type {
30     OVSDB_TYPE_VOID,            /* No value. */
31     OVSDB_TYPE_INTEGER,         /* Signed 64-bit integer. */
32     OVSDB_TYPE_REAL,            /* IEEE 754 double-precision floating point. */
33     OVSDB_TYPE_BOOLEAN,         /* True or false. */
34     OVSDB_TYPE_STRING,          /* UTF-8 string. */
35     OVSDB_TYPE_UUID,            /* RFC 4122 UUID referencing a table row. */
36     OVSDB_N_TYPES
37 };
38
39 static inline bool ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type);
40 bool ovsdb_atomic_type_from_string(const char *, enum ovsdb_atomic_type *);
41 struct ovsdb_error *ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *,
42                                                 const struct json *);
43 const char *ovsdb_atomic_type_to_string(enum ovsdb_atomic_type);
44 struct json *ovsdb_atomic_type_to_json(enum ovsdb_atomic_type);
45 \f
46 /* An atomic type plus optional constraints. */
47
48 struct ovsdb_base_type {
49     enum ovsdb_atomic_type type;
50     union {
51         struct ovsdb_integer_constraints {
52             int64_t min;        /* minInteger or INT64_MIN. */
53             int64_t max;        /* maxInteger or INT64_MAX. */
54         } integer;
55
56         struct ovsdb_real_constraints {
57             double min;         /* minReal or -DBL_MAX. */
58             double max;         /* minReal or DBL_MAX. */
59         } real;
60
61         /* No constraints for Boolean types. */
62
63         struct ovsdb_string_constraints {
64             pcre *re;           /* Compiled regular expression. */
65             char *reMatch;      /* reMatch or NULL. */
66             char *reComment;    /* reComment or NULL. */
67             unsigned int minLen; /* minLength or 0. */
68             unsigned int maxLen; /* maxLength or UINT_MAX. */
69         } string;
70     } u;
71 };
72
73 #define OVSDB_BASE_VOID_INIT    { .type = OVSDB_TYPE_VOID }
74 #define OVSDB_BASE_INTEGER_INIT { .type = OVSDB_TYPE_INTEGER,           \
75                                   .u.integer = { INT64_MIN, INT64_MAX } }
76 #define OVSDB_BASE_REAL_INIT    { .type = OVSDB_TYPE_REAL,          \
77                                   .u.real = { -DBL_MAX, DBL_MAX } }
78 #define OVSDB_BASE_BOOLEAN_INIT { .type = OVSDB_TYPE_BOOLEAN }
79 #define OVSDB_BASE_STRING_INIT  { .type = OVSDB_TYPE_STRING,        \
80                                   .u.string = { NULL, NULL, NULL,   \
81                                                 0, UINT_MAX } }
82 #define OVSDB_BASE_UUID_INIT    { .type = OVSDB_TYPE_UUID }
83
84 void ovsdb_base_type_init(struct ovsdb_base_type *, enum ovsdb_atomic_type);
85 void ovsdb_base_type_clone(struct ovsdb_base_type *,
86                            const struct ovsdb_base_type *);
87 void ovsdb_base_type_destroy(struct ovsdb_base_type *);
88
89 bool ovsdb_base_type_is_valid(const struct ovsdb_base_type *);
90 bool ovsdb_base_type_has_constraints(const struct ovsdb_base_type *);
91 void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *);
92 struct ovsdb_error *ovsdb_base_type_set_regex(struct ovsdb_base_type *,
93                                               const char *reMatch,
94                                               const char *reComment)
95     WARN_UNUSED_RESULT;
96
97 struct ovsdb_error *ovsdb_base_type_from_json(struct ovsdb_base_type *,
98                                               const struct json *)
99     WARN_UNUSED_RESULT;
100 struct json *ovsdb_base_type_to_json(const struct ovsdb_base_type *);
101 \f
102 /* An OVSDB type.
103  *
104  * Several rules constrain the valid types.  See ovsdb_type_is_valid() (in
105  * ovsdb-types.c) for details.
106  *
107  * If 'value_type' is OVSDB_TYPE_VOID, 'n_min' is 1, and 'n_max' is 1, then the
108  * type is a single atomic 'key_type'.
109  *
110  * If 'value_type' is OVSDB_TYPE_VOID and 'n_min' or 'n_max' (or both) has a
111  * value other than 1, then the type is a set of 'key_type'.  If 'n_min' is 0
112  * and 'n_max' is 1, then the type can also be considered an optional
113  * 'key_type'.
114  *
115  * If 'value_type' is not OVSDB_TYPE_VOID, then the type is a map from
116  * 'key_type' to 'value_type'.  If 'n_min' is 0 and 'n_max' is 1, then the type
117  * can also be considered an optional pair of 'key_type' and 'value_type'.
118  */
119 struct ovsdb_type {
120     struct ovsdb_base_type key;
121     struct ovsdb_base_type value;
122     unsigned int n_min;
123     unsigned int n_max;         /* UINT_MAX stands in for "unlimited". */
124 };
125
126 #define OVSDB_TYPE_SCALAR_INITIALIZER(KEY) { KEY, OVSDB_BASE_VOID_INIT, 1, 1 }
127
128 extern const struct ovsdb_type ovsdb_type_integer;
129 extern const struct ovsdb_type ovsdb_type_real;
130 extern const struct ovsdb_type ovsdb_type_boolean;
131 extern const struct ovsdb_type ovsdb_type_string;
132 extern const struct ovsdb_type ovsdb_type_uuid;
133
134 void ovsdb_type_clone(struct ovsdb_type *, const struct ovsdb_type *);
135 void ovsdb_type_destroy(struct ovsdb_type *);
136
137 bool ovsdb_type_is_valid(const struct ovsdb_type *);
138
139 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
140 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);
141 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *);
142 static inline bool ovsdb_type_is_set(const struct ovsdb_type *);
143 static inline bool ovsdb_type_is_map(const struct ovsdb_type *);
144
145 char *ovsdb_type_to_english(const struct ovsdb_type *);
146
147 struct ovsdb_error *ovsdb_type_from_json(struct ovsdb_type *,
148                                          const struct json *)
149     WARN_UNUSED_RESULT;
150 struct json *ovsdb_type_to_json(const struct ovsdb_type *);
151 \f
152 /* Inline function implementations. */
153
154 static inline bool
155 ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type atomic_type)
156 {
157     return atomic_type >= 0 && atomic_type < OVSDB_N_TYPES;
158 }
159
160 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *type)
161 {
162     return (type->value.type == OVSDB_TYPE_VOID
163             && type->n_min == 1 && type->n_max == 1);
164 }
165
166 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *type)
167 {
168     return type->n_min == 0;
169 }
170
171 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *type)
172 {
173     return type->n_max > 1;
174 }
175
176 static inline bool ovsdb_type_is_set(const struct ovsdb_type *type)
177 {
178     return (type->value.type == OVSDB_TYPE_VOID
179             && (type->n_min != 1 || type->n_max != 1));
180 }
181
182 static inline bool ovsdb_type_is_map(const struct ovsdb_type *type)
183 {
184     return type->value.type != OVSDB_TYPE_VOID;
185 }
186
187 #endif /* ovsdb-types.h */