Global replace of Nicira Networks.
[sliver-openvswitch.git] / ovsdb / table.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 "table.h"
19
20 #include <assert.h>
21 #include <limits.h>
22
23 #include "json.h"
24 #include "column.h"
25 #include "ovsdb-error.h"
26 #include "ovsdb-parser.h"
27 #include "ovsdb-types.h"
28 #include "row.h"
29
30 static void
31 add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column)
32 {
33     assert(!shash_find(&ts->columns, column->name));
34     column->index = shash_count(&ts->columns);
35     shash_add(&ts->columns, column->name, column);
36 }
37
38 struct ovsdb_table_schema *
39 ovsdb_table_schema_create(const char *name, bool mutable,
40                           unsigned int max_rows, bool is_root)
41 {
42     struct ovsdb_column *uuid, *version;
43     struct ovsdb_table_schema *ts;
44
45     ts = xzalloc(sizeof *ts);
46     ts->name = xstrdup(name);
47     ts->mutable = mutable;
48     shash_init(&ts->columns);
49     ts->max_rows = max_rows;
50     ts->is_root = is_root;
51
52     uuid = ovsdb_column_create("_uuid", false, true, &ovsdb_type_uuid);
53     add_column(ts, uuid);
54     assert(uuid->index == OVSDB_COL_UUID);
55
56     version = ovsdb_column_create("_version", false, false, &ovsdb_type_uuid);
57     add_column(ts, version);
58     assert(version->index == OVSDB_COL_VERSION);
59
60     ts->n_indexes = 0;
61     ts->indexes = NULL;
62
63     return ts;
64 }
65
66 struct ovsdb_table_schema *
67 ovsdb_table_schema_clone(const struct ovsdb_table_schema *old)
68 {
69     struct ovsdb_table_schema *new;
70     struct shash_node *node;
71     size_t i;
72
73     new = ovsdb_table_schema_create(old->name, old->mutable,
74                                     old->max_rows, old->is_root);
75     SHASH_FOR_EACH (node, &old->columns) {
76         const struct ovsdb_column *column = node->data;
77
78         if (column->name[0] == '_') {
79             /* Added automatically by ovsdb_table_schema_create(). */
80             continue;
81         }
82
83         add_column(new, ovsdb_column_clone(column));
84     }
85
86     new->n_indexes = old->n_indexes;
87     new->indexes = xmalloc(new->n_indexes * sizeof *new->indexes);
88     for (i = 0; i < new->n_indexes; i++) {
89         const struct ovsdb_column_set *old_index = &old->indexes[i];
90         struct ovsdb_column_set *new_index = &new->indexes[i];
91         size_t j;
92
93         ovsdb_column_set_init(new_index);
94         for (j = 0; j < old_index->n_columns; j++) {
95             const struct ovsdb_column *old_column = old_index->columns[j];
96             const struct ovsdb_column *new_column;
97
98             new_column = ovsdb_table_schema_get_column(new, old_column->name);
99             ovsdb_column_set_add(new_index, new_column);
100         }
101     }
102
103     return new;
104 }
105
106 void
107 ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts)
108 {
109     struct shash_node *node;
110     size_t i;
111
112     for (i = 0; i < ts->n_indexes; i++) {
113         ovsdb_column_set_destroy(&ts->indexes[i]);
114     }
115     free(ts->indexes);
116
117     SHASH_FOR_EACH (node, &ts->columns) {
118         ovsdb_column_destroy(node->data);
119     }
120     shash_destroy(&ts->columns);
121     free(ts->name);
122     free(ts);
123 }
124
125 struct ovsdb_error *
126 ovsdb_table_schema_from_json(const struct json *json, const char *name,
127                              struct ovsdb_table_schema **tsp)
128 {
129     struct ovsdb_table_schema *ts;
130     const struct json *columns, *mutable, *max_rows, *is_root, *indexes;
131     struct shash_node *node;
132     struct ovsdb_parser parser;
133     struct ovsdb_error *error;
134     long long int n_max_rows;
135
136     *tsp = NULL;
137
138     ovsdb_parser_init(&parser, json, "table schema for table %s", name);
139     columns = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
140     mutable = ovsdb_parser_member(&parser, "mutable",
141                                   OP_TRUE | OP_FALSE | OP_OPTIONAL);
142     max_rows = ovsdb_parser_member(&parser, "maxRows",
143                                    OP_INTEGER | OP_OPTIONAL);
144     is_root = ovsdb_parser_member(&parser, "isRoot", OP_BOOLEAN | OP_OPTIONAL);
145     indexes = ovsdb_parser_member(&parser, "indexes", OP_ARRAY | OP_OPTIONAL);
146     error = ovsdb_parser_finish(&parser);
147     if (error) {
148         return error;
149     }
150
151     if (max_rows) {
152         if (json_integer(max_rows) <= 0) {
153             return ovsdb_syntax_error(json, NULL,
154                                       "maxRows must be at least 1");
155         }
156         n_max_rows = max_rows->u.integer;
157     } else {
158         n_max_rows = UINT_MAX;
159     }
160
161     if (shash_is_empty(json_object(columns))) {
162         return ovsdb_syntax_error(json, NULL,
163                                   "table must have at least one column");
164     }
165
166     ts = ovsdb_table_schema_create(name,
167                                    mutable ? json_boolean(mutable) : true,
168                                    MIN(n_max_rows, UINT_MAX),
169                                    is_root ? json_boolean(is_root) : false);
170     SHASH_FOR_EACH (node, json_object(columns)) {
171         struct ovsdb_column *column;
172
173         if (node->name[0] == '_') {
174             error = ovsdb_syntax_error(json, NULL, "names beginning with "
175                                        "\"_\" are reserved");
176         } else if (!ovsdb_parser_is_id(node->name)) {
177             error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
178         } else {
179             error = ovsdb_column_from_json(node->data, node->name, &column);
180         }
181         if (error) {
182             goto error;
183         }
184
185         add_column(ts, column);
186     }
187
188     if (indexes) {
189         size_t i;
190
191         ts->indexes = xmalloc(indexes->u.array.n * sizeof *ts->indexes);
192         for (i = 0; i < indexes->u.array.n; i++) {
193             struct ovsdb_column_set *index = &ts->indexes[i];
194             size_t j;
195
196             error = ovsdb_column_set_from_json(indexes->u.array.elems[i],
197                                                ts, index);
198             if (error) {
199                 goto error;
200             }
201             if (index->n_columns == 0) {
202                 error = ovsdb_syntax_error(json, NULL, "index must have "
203                                            "at least one column");
204                 goto error;
205             }
206             ts->n_indexes++;
207
208             for (j = 0; j < index->n_columns; j++) {
209                 const struct ovsdb_column *column = index->columns[j];
210
211                 if (!column->persistent) {
212                     error = ovsdb_syntax_error(json, NULL, "ephemeral columns "
213                                                "(such as %s) may not be "
214                                                "indexed", column->name);
215                     goto error;
216                 }
217             }
218         }
219     }
220
221     *tsp = ts;
222     return NULL;
223
224 error:
225     ovsdb_table_schema_destroy(ts);
226     return error;
227 }
228
229 /* Returns table schema 'ts' serialized into JSON.
230  *
231  * The "isRoot" member is included in the JSON only if its value would differ
232  * from 'default_is_root'.  Ordinarily 'default_is_root' should be false,
233  * because ordinarily a table would be not be part of the root set if its
234  * "isRoot" member is omitted.  However, garbage collection was not orginally
235  * included in OVSDB, so in older schemas that do not include any "isRoot"
236  * members, every table is implicitly part of the root set.  To serialize such
237  * a schema in a way that can be read by older OVSDB tools, specify
238  * 'default_is_root' as true. */
239 struct json *
240 ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts,
241                            bool default_is_root)
242 {
243     struct json *json, *columns;
244     struct shash_node *node;
245
246     json = json_object_create();
247     if (!ts->mutable) {
248         json_object_put(json, "mutable", json_boolean_create(false));
249     }
250     if (default_is_root != ts->is_root) {
251         json_object_put(json, "isRoot", json_boolean_create(ts->is_root));
252     }
253
254     columns = json_object_create();
255
256     SHASH_FOR_EACH (node, &ts->columns) {
257         const struct ovsdb_column *column = node->data;
258         if (node->name[0] != '_') {
259             json_object_put(columns, column->name,
260                             ovsdb_column_to_json(column));
261         }
262     }
263     json_object_put(json, "columns", columns);
264     if (ts->max_rows != UINT_MAX) {
265         json_object_put(json, "maxRows", json_integer_create(ts->max_rows));
266     }
267
268     if (ts->n_indexes) {
269         struct json **indexes;
270         size_t i;
271
272         indexes = xmalloc(ts->n_indexes * sizeof *indexes);
273         for (i = 0; i < ts->n_indexes; i++) {
274             indexes[i] = ovsdb_column_set_to_json(&ts->indexes[i]);
275         }
276         json_object_put(json, "indexes",
277                         json_array_create(indexes, ts->n_indexes));
278     }
279
280     return json;
281 }
282
283 const struct ovsdb_column *
284 ovsdb_table_schema_get_column(const struct ovsdb_table_schema *ts,
285                               const char *name)
286 {
287     return shash_find_data(&ts->columns, name);
288 }
289 \f
290 struct ovsdb_table *
291 ovsdb_table_create(struct ovsdb_table_schema *ts)
292 {
293     struct ovsdb_table *table;
294     size_t i;
295
296     table = xmalloc(sizeof *table);
297     table->schema = ts;
298     table->txn_table = NULL;
299     table->indexes = xmalloc(ts->n_indexes * sizeof *table->indexes);
300     for (i = 0; i < ts->n_indexes; i++) {
301         hmap_init(&table->indexes[i]);
302     }
303     hmap_init(&table->rows);
304
305     return table;
306 }
307
308 void
309 ovsdb_table_destroy(struct ovsdb_table *table)
310 {
311     if (table) {
312         struct ovsdb_row *row, *next;
313         size_t i;
314
315         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &table->rows) {
316             ovsdb_row_destroy(row);
317         }
318         hmap_destroy(&table->rows);
319
320         for (i = 0; i < table->schema->n_indexes; i++) {
321             hmap_destroy(&table->indexes[i]);
322         }
323         free(table->indexes);
324
325         ovsdb_table_schema_destroy(table->schema);
326         free(table);
327     }
328 }
329
330 const struct ovsdb_row *
331 ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid)
332 {
333     struct ovsdb_row *row;
334
335     HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &table->rows) {
336         if (uuid_equals(ovsdb_row_get_uuid(row), uuid)) {
337             return row;
338         }
339     }
340
341     return NULL;
342 }