summaryrefslogtreecommitdiff
path: root/lib/ovsdb-types.c
blob: 659b50db3c0b040ed2aa08367adff0bcdc99f316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Copyright (c) 2009, 2010 Nicira Networks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "ovsdb-types.h"

#include <limits.h>

#include "dynamic-string.h"
#include "json.h"
#include "ovsdb-error.h"
#include "ovsdb-parser.h"

const struct ovsdb_type ovsdb_type_integer =
    OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_INTEGER);
const struct ovsdb_type ovsdb_type_real =
    OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_REAL);
const struct ovsdb_type ovsdb_type_boolean =
    OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_BOOLEAN);
const struct ovsdb_type ovsdb_type_string =
    OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_STRING);
const struct ovsdb_type ovsdb_type_uuid =
    OVSDB_TYPE_SCALAR_INITIALIZER(OVSDB_TYPE_UUID);

const char *
ovsdb_atomic_type_to_string(enum ovsdb_atomic_type type)
{
    switch (type) {
    case OVSDB_TYPE_VOID:
        return "void";

    case OVSDB_TYPE_INTEGER:
        return "integer";

    case OVSDB_TYPE_REAL:
        return "real";

    case OVSDB_TYPE_BOOLEAN:
        return "boolean";

    case OVSDB_TYPE_STRING:
        return "string";

    case OVSDB_TYPE_UUID:
        return "uuid";

    case OVSDB_N_TYPES:
    default:
        return "<invalid>";
    }
}

struct json *
ovsdb_atomic_type_to_json(enum ovsdb_atomic_type type)
{
    return json_string_create(ovsdb_atomic_type_to_string(type));
}

bool
ovsdb_type_is_valid(const struct ovsdb_type *type)
{
    return (type->key_type != OVSDB_TYPE_VOID
            && ovsdb_atomic_type_is_valid(type->key_type)
            && ovsdb_atomic_type_is_valid(type->value_type)
            && type->n_min <= 1
            && type->n_min <= type->n_max
            && (type->value_type == OVSDB_TYPE_VOID
                || ovsdb_atomic_type_is_valid_key(type->key_type)));
}

bool
ovsdb_atomic_type_from_string(const char *string, enum ovsdb_atomic_type *type)
{
    if (!strcmp(string, "integer")) {
        *type = OVSDB_TYPE_INTEGER;
    } else if (!strcmp(string, "real")) {
        *type = OVSDB_TYPE_REAL;
    } else if (!strcmp(string, "boolean")) {
        *type = OVSDB_TYPE_BOOLEAN;
    } else if (!strcmp(string, "string")) {
        *type = OVSDB_TYPE_STRING;
    } else if (!strcmp(string, "uuid")) {
        *type = OVSDB_TYPE_UUID;
    } else {
        return false;
    }
    return true;
}

struct ovsdb_error *
ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *type,
                            const struct json *json)
{
    if (json->type == JSON_STRING) {
        if (ovsdb_atomic_type_from_string(json_string(json), type)) {
            return NULL;
        } else {
            *type = OVSDB_TYPE_VOID;
            return ovsdb_syntax_error(json, NULL,
                                      "\"%s\" is not an atomic-type",
                                      json_string(json));
        }
    } else {
        *type = OVSDB_TYPE_VOID;
        return ovsdb_syntax_error(json, NULL, "atomic-type expected");
    }
}

static struct ovsdb_error *
n_from_json(const struct json *json, unsigned int *n)
{
    if (!json) {
        return NULL;
    } else if (json->type == JSON_INTEGER
               && json->u.integer >= 0 && json->u.integer < UINT_MAX) {
        *n = json->u.integer;
        return NULL;
    } else {
        return ovsdb_syntax_error(json, NULL, "bad min or max value");
    }
}

char *
ovsdb_type_to_english(const struct ovsdb_type *type)
{
    const char *key = ovsdb_atomic_type_to_string(type->key_type);
    const char *value = ovsdb_atomic_type_to_string(type->value_type);
    if (ovsdb_type_is_scalar(type)) {
        return xstrdup(key);
    } else {
        struct ds s = DS_EMPTY_INITIALIZER;
        ds_put_cstr(&s, ovsdb_type_is_set(type) ? "set" : "map");
        if (type->n_max == UINT_MAX) {
            if (type->n_min) {
                ds_put_format(&s, " of %u or more", type->n_min);
            } else {
                ds_put_cstr(&s, " of");
            }
        } else if (type->n_min) {
            ds_put_format(&s, " of %u to %u", type->n_min, type->n_max);
        } else {
            ds_put_format(&s, " of up to %u", type->n_max);
        }
        if (ovsdb_type_is_set(type)) {
            ds_put_format(&s, " %ss", key);
        } else {
            ds_put_format(&s, " (%s, %s) pairs", key, value);
        }
        return ds_cstr(&s);
    }
}

struct ovsdb_error *
ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
{
    type->value_type = OVSDB_TYPE_VOID;
    type->n_min = 1;
    type->n_max = 1;

    if (json->type == JSON_STRING) {
        return ovsdb_atomic_type_from_json(&type->key_type, json);
    } else if (json->type == JSON_OBJECT) {
        const struct json *key, *value, *min, *max;
        struct ovsdb_error *error;
        struct ovsdb_parser parser;

        ovsdb_parser_init(&parser, json, "ovsdb type");
        key = ovsdb_parser_member(&parser, "key", OP_STRING);
        value = ovsdb_parser_member(&parser, "value", OP_STRING | OP_OPTIONAL);
        min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL);
        max = ovsdb_parser_member(&parser, "max",
                                  OP_INTEGER | OP_STRING | OP_OPTIONAL);
        error = ovsdb_parser_finish(&parser);
        if (error) {
            return error;
        }

        error = ovsdb_atomic_type_from_json(&type->key_type, key);
        if (error) {
            return error;
        }

        if (value) {
            error = ovsdb_atomic_type_from_json(&type->value_type, value);
            if (error) {
                return error;
            }
        }

        error = n_from_json(min, &type->n_min);
        if (error) {
            return error;
        }

        if (max && max->type == JSON_STRING
            && !strcmp(max->u.string, "unlimited")) {
            type->n_max = UINT_MAX;
        } else {
            error = n_from_json(max, &type->n_max);
            if (error) {
                return error;
            }
        }

        if (!ovsdb_type_is_valid(type)) {
            return ovsdb_syntax_error(json, NULL,
                                      "ovsdb type fails constraint checks");
        }

        return NULL;
    } else {
        return ovsdb_syntax_error(json, NULL, "ovsdb type expected");
    }
}

struct json *
ovsdb_type_to_json(const struct ovsdb_type *type)
{
    if (ovsdb_type_is_scalar(type)) {
        return ovsdb_atomic_type_to_json(type->key_type);
    } else {
        struct json *json = json_object_create();
        json_object_put(json, "key",
                        ovsdb_atomic_type_to_json(type->key_type));
        if (type->value_type != OVSDB_TYPE_VOID) {
            json_object_put(json, "value",
                            ovsdb_atomic_type_to_json(type->value_type));
        }
        if (type->n_min != 1) {
            json_object_put(json, "min", json_integer_create(type->n_min));
        }
        if (type->n_max == UINT_MAX) {
            json_object_put_string(json, "max", "unlimited");
        } else if (type->n_max != 1) {
            json_object_put(json, "max", json_integer_create(type->n_max));
        }
        return json;
    }
}