]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-types.h
stopwatch: Remove tabs from output.
[mirror_ovs.git] / lib / ovsdb-types.h
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 #ifndef OVSDB_TYPES_H
17 #define OVSDB_TYPES_H 1
18
19 #include <float.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include "compiler.h"
23 #include "uuid.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 struct json;
30
31 /* An atomic type: one that OVSDB regards as a single unit of data. */
32 enum ovsdb_atomic_type {
33 OVSDB_TYPE_VOID, /* No value. */
34 OVSDB_TYPE_INTEGER, /* Signed 64-bit integer. */
35 OVSDB_TYPE_REAL, /* IEEE 754 double-precision floating point. */
36 OVSDB_TYPE_BOOLEAN, /* True or false. */
37 OVSDB_TYPE_STRING, /* UTF-8 string. */
38 OVSDB_TYPE_UUID, /* RFC 4122 UUID referencing a table row. */
39 OVSDB_N_TYPES
40 };
41
42 static inline bool ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type);
43 bool ovsdb_atomic_type_from_string(const char *, enum ovsdb_atomic_type *);
44 struct ovsdb_error *ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *,
45 const struct json *);
46 const char *ovsdb_atomic_type_to_string(enum ovsdb_atomic_type);
47 struct json *ovsdb_atomic_type_to_json(enum ovsdb_atomic_type);
48 \f
49 /* An atomic type plus optional constraints. */
50
51 enum ovsdb_ref_type {
52 OVSDB_REF_STRONG, /* Target must exist. */
53 OVSDB_REF_WEAK /* Delete reference if target disappears. */
54 };
55
56 struct ovsdb_base_type {
57 enum ovsdb_atomic_type type;
58
59 /* If nonnull, a datum with keys of type 'type' that expresses all the
60 * valid values for this base_type. */
61 struct ovsdb_datum *enum_;
62
63 union {
64 struct ovsdb_integer_constraints {
65 int64_t min; /* minInteger or INT64_MIN. */
66 int64_t max; /* maxInteger or INT64_MAX. */
67 } integer;
68
69 struct ovsdb_real_constraints {
70 double min; /* minReal or -DBL_MAX. */
71 double max; /* minReal or DBL_MAX. */
72 } real;
73
74 /* No constraints for Boolean types. */
75
76 struct ovsdb_string_constraints {
77 unsigned int minLen; /* minLength or 0. */
78 unsigned int maxLen; /* maxLength or UINT_MAX. */
79 } string;
80
81 struct ovsdb_uuid_constraints {
82 char *refTableName; /* Name of referenced table, or NULL. */
83 struct ovsdb_table *refTable; /* Referenced table, if available. */
84 enum ovsdb_ref_type refType; /* Reference type. */
85 } uuid;
86 };
87 };
88
89 #define OVSDB_BASE_VOID_INIT { .type = OVSDB_TYPE_VOID }
90 #define OVSDB_BASE_INTEGER_INIT { .type = OVSDB_TYPE_INTEGER, \
91 .integer = { INT64_MIN, INT64_MAX } }
92 #define OVSDB_BASE_REAL_INIT { .type = OVSDB_TYPE_REAL, \
93 .real = { -DBL_MAX, DBL_MAX } }
94 #define OVSDB_BASE_BOOLEAN_INIT { .type = OVSDB_TYPE_BOOLEAN }
95 #define OVSDB_BASE_STRING_INIT { .type = OVSDB_TYPE_STRING, \
96 .string = { 0, UINT_MAX } }
97 #define OVSDB_BASE_UUID_INIT { .type = OVSDB_TYPE_UUID, \
98 .uuid = { NULL, NULL, 0 } }
99
100 void ovsdb_base_type_init(struct ovsdb_base_type *, enum ovsdb_atomic_type);
101 void ovsdb_base_type_clone(struct ovsdb_base_type *,
102 const struct ovsdb_base_type *);
103 void ovsdb_base_type_destroy(struct ovsdb_base_type *);
104
105 bool ovsdb_base_type_is_valid(const struct ovsdb_base_type *);
106 bool ovsdb_base_type_has_constraints(const struct ovsdb_base_type *);
107 void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *);
108 const struct ovsdb_type *ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type);
109
110 struct ovsdb_error *ovsdb_base_type_from_json(struct ovsdb_base_type *,
111 const struct json *)
112 OVS_WARN_UNUSED_RESULT;
113 struct json *ovsdb_base_type_to_json(const struct ovsdb_base_type *);
114
115 static inline bool ovsdb_base_type_is_ref(const struct ovsdb_base_type *);
116 static inline bool ovsdb_base_type_is_strong_ref(
117 const struct ovsdb_base_type *);
118 static inline bool ovsdb_base_type_is_weak_ref(const struct ovsdb_base_type *);
119 \f
120 /* An OVSDB type.
121 *
122 * Several rules constrain the valid types. See ovsdb_type_is_valid() (in
123 * ovsdb-types.c) for details.
124 *
125 * If 'value_type' is OVSDB_TYPE_VOID, 'n_min' is 1, and 'n_max' is 1, then the
126 * type is a single atomic 'key_type'.
127 *
128 * If 'value_type' is OVSDB_TYPE_VOID and 'n_min' or 'n_max' (or both) has a
129 * value other than 1, then the type is a set of 'key_type'. If 'n_min' is 0
130 * and 'n_max' is 1, then the type can also be considered an optional
131 * 'key_type'.
132 *
133 * If 'value_type' is not OVSDB_TYPE_VOID, then the type is a map from
134 * 'key_type' to 'value_type'. If 'n_min' is 0 and 'n_max' is 1, then the type
135 * can also be considered an optional pair of 'key_type' and 'value_type'.
136 */
137 struct ovsdb_type {
138 struct ovsdb_base_type key;
139 struct ovsdb_base_type value;
140 unsigned int n_min;
141 unsigned int n_max; /* UINT_MAX stands in for "unlimited". */
142 };
143
144 #define OVSDB_TYPE_SCALAR_INITIALIZER(KEY) { KEY, OVSDB_BASE_VOID_INIT, 1, 1 }
145
146 extern const struct ovsdb_type ovsdb_type_integer;
147 extern const struct ovsdb_type ovsdb_type_real;
148 extern const struct ovsdb_type ovsdb_type_boolean;
149 extern const struct ovsdb_type ovsdb_type_string;
150 extern const struct ovsdb_type ovsdb_type_uuid;
151
152 void ovsdb_type_clone(struct ovsdb_type *, const struct ovsdb_type *);
153 void ovsdb_type_destroy(struct ovsdb_type *);
154
155 bool ovsdb_type_is_valid(const struct ovsdb_type *);
156
157 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
158 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);
159 static inline bool ovsdb_type_is_optional_scalar(
160 const struct ovsdb_type *);
161 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *);
162 static inline bool ovsdb_type_is_set(const struct ovsdb_type *);
163 static inline bool ovsdb_type_is_map(const struct ovsdb_type *);
164
165 char *ovsdb_type_to_english(const struct ovsdb_type *);
166
167 struct ovsdb_error *ovsdb_type_from_json(struct ovsdb_type *,
168 const struct json *)
169 OVS_WARN_UNUSED_RESULT;
170 struct json *ovsdb_type_to_json(const struct ovsdb_type *);
171 \f
172 /* Inline function implementations. */
173
174 static inline bool
175 ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type atomic_type)
176 {
177 return (int) atomic_type >= 0 && atomic_type < OVSDB_N_TYPES;
178 }
179
180 static inline bool
181 ovsdb_base_type_is_ref(const struct ovsdb_base_type *base)
182 {
183 return base->type == OVSDB_TYPE_UUID && base->uuid.refTableName;
184 }
185
186 static inline bool
187 ovsdb_base_type_is_strong_ref(const struct ovsdb_base_type *base)
188 {
189 return (ovsdb_base_type_is_ref(base)
190 && base->uuid.refType == OVSDB_REF_STRONG);
191 }
192
193 static inline bool
194 ovsdb_base_type_is_weak_ref(const struct ovsdb_base_type *base)
195 {
196 return (ovsdb_base_type_is_ref(base)
197 && base->uuid.refType == OVSDB_REF_WEAK);
198 }
199
200 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *type)
201 {
202 return (type->value.type == OVSDB_TYPE_VOID
203 && type->n_min == 1 && type->n_max == 1);
204 }
205
206 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *type)
207 {
208 return type->n_min == 0;
209 }
210
211 static inline bool ovsdb_type_is_optional_scalar(
212 const struct ovsdb_type *type)
213 {
214 return (type->value.type == OVSDB_TYPE_VOID
215 && type->n_min == 0 && type->n_max == 1);
216 }
217
218 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *type)
219 {
220 return type->n_max > 1;
221 }
222
223 static inline bool ovsdb_type_is_set(const struct ovsdb_type *type)
224 {
225 return (type->value.type == OVSDB_TYPE_VOID
226 && (type->n_min != 1 || type->n_max != 1));
227 }
228
229 static inline bool ovsdb_type_is_map(const struct ovsdb_type *type)
230 {
231 return type->value.type != OVSDB_TYPE_VOID;
232 }
233
234 #ifdef __cplusplus
235 }
236 #endif
237
238 #endif /* ovsdb-types.h */