1 /* Copyright (c) 2009 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ovsdb-types.h"
22 #include "dynamic-string.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
27 const struct ovsdb_type ovsdb_type_integer =
28 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_INTEGER);
29 const struct ovsdb_type ovsdb_type_real =
30 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_REAL);
31 const struct ovsdb_type ovsdb_type_boolean =
32 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_BOOLEAN);
33 const struct ovsdb_type ovsdb_type_string =
34 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_STRING);
35 const struct ovsdb_type ovsdb_type_uuid =
36 OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_UUID);
39 ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
45 case OVSDB_TYPE_INTEGER:
51 case OVSDB_TYPE_BOOLEAN:
54 case OVSDB_TYPE_STRING:
67 ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
69 return json_string_create(ovsdb_atomic_type_to_string(type));
73 ovsdb_type_is_valid(const struct ovsdb_type *type)
75 return (type->key_type != OVSDB_TYPE_VOID
76 && ovsdb_atomic_type_is_valid(type->key_type)
77 && ovsdb_atomic_type_is_valid(type->value_type)
78 && type->n_min <= type->n_max
79 && (type->value_type == OVSDB_TYPE_VOID
80 || ovsdb_atomic_type_is_valid_key(type->key_type)));
84 ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
86 if (!strcmp(string, "integer")) {
87 *type = OVSDB_TYPE_INTEGER;
88 } else if (!strcmp(string, "real")) {
89 *type = OVSDB_TYPE_REAL;
90 } else if (!strcmp(string, "boolean")) {
91 *type = OVSDB_TYPE_BOOLEAN;
92 } else if (!strcmp(string, "string")) {
93 *type = OVSDB_TYPE_STRING;
94 } else if (!strcmp(string, "uuid")) {
95 *type = OVSDB_TYPE_UUID;
103 ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
104 const struct json *json)
106 if (json->type == JSON_STRING) {
107 if (ovsdb_atomic_type_from_string(json_string(json), type)) {
110 *type = OVSDB_TYPE_VOID;
111 return ovsdb_syntax_error(json, NULL,
112 "\"%s\" is not an atomic-type",
116 *type = OVSDB_TYPE_VOID;
117 return ovsdb_syntax_error(json, NULL, "atomic-type expected");
121 static struct ovsdb_error *
122 n_from_json(const struct json *json, unsigned int *n)
126 } else if (json->type == JSON_INTEGER
127 && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
128 *n = json->u.integer;
131 return ovsdb_syntax_error(json, NULL, "bad min or max value");
136 ovsdb_type_to_english(const struct ovsdb_type *type)
138 const char *key = ovsdb_atomic_type_to_string(type->key_type);
139 const char *value = ovsdb_atomic_type_to_string(type->value_type);
140 if (ovsdb_type_is_scalar(type)) {
143 struct ds s = DS_EMPTY_INITIALIZER;
144 ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
145 if (type->n_max == UINT_MAX) {
147 ds_put_format(&s, " of %u or more", type->n_min);
149 ds_put_cstr(&s, " of");
151 } else if (type->n_min) {
152 ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
154 ds_put_format(&s, " of up to %u", type->n_max);
156 if (ovsdb_type_is_set(type)) {
157 ds_put_format(&s, " %ss", key);
159 ds_put_format(&s, " (%s, %s) pairs", key, value);
166 ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
168 type->value_type = OVSDB_TYPE_VOID;
172 if (json->type == JSON_STRING) {
173 return ovsdb_atomic_type_from_json(&type->key_type, json);
174 } else if (json->type == JSON_OBJECT) {
175 const struct json *key, *value, *min, *max;
176 struct ovsdb_error *error;
177 struct ovsdb_parser parser;
179 ovsdb_parser_init(&parser, json, "ovsdb type");
180 key = ovsdb_parser_member(&parser, "key", OP_STRING);
181 value = ovsdb_parser_member(&parser, "value", OP_STRING | OP_OPTIONAL);
182 min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
183 max = ovsdb_parser_member(&parser, "max",
184 OP_INTEGER | OP_STRING | OP_OPTIONAL);
185 error = ovsdb_parser_finish(&parser);
190 error = ovsdb_atomic_type_from_json(&type->key_type, key);
196 error = ovsdb_atomic_type_from_json(&type->value_type, value);
202 error = n_from_json(min, &type->n_min);
207 if (max && max->type == JSON_STRING
208 && !strcmp(max->u.string, "unlimited")) {
209 type->n_max = UINT_MAX;
211 error = n_from_json(max, &type->n_max);
217 if (!ovsdb_type_is_valid(type)) {
218 return ovsdb_syntax_error(json, NULL,
219 "ovsdb type fails constraint checks");
224 return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
229 ovsdb_type_to_json(const struct ovsdb_type *type)
231 if (ovsdb_type_is_scalar(type)) {
232 return ovsdb_atomic_type_to_json(type->key_type);
234 struct json *json = json_object_create();
235 json_object_put(json, "key",
236 ovsdb_atomic_type_to_json(type->key_type));
237 if (type->value_type != OVSDB_TYPE_VOID) {
238 json_object_put(json, "value",
239 ovsdb_atomic_type_to_json(type->value_type));
241 if (type->n_min != 1) {
242 json_object_put(json, "min", json_integer_create(type->n_min));
244 if (type->n_max == UINT_MAX) {
245 json_object_put_string(json, "max", "unlimited");
246 } else if (type->n_max != 1) {
247 json_object_put(json, "max", json_integer_create(type->n_max));