ovsdb: Add functions to clone schemas.
[sliver-openvswitch.git] / ovsdb / table.c
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 #include <config.h>
17
18 #include "table.h"
19
20 #include <assert.h>
21
22 #include "json.h"
23 #include "column.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
26 #include "ovsdb-types.h"
27 #include "row.h"
28
29 static void
30 add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column)
31 {
32     assert(!shash_find(&ts->columns, column->name));
33     column->index = shash_count(&ts->columns);
34     shash_add(&ts->columns, column->name, column);
35 }
36
37 struct ovsdb_table_schema *
38 ovsdb_table_schema_create(const char *name, const char *comment, bool mutable)
39 {
40     struct ovsdb_column *uuid, *version;
41     struct ovsdb_table_schema *ts;
42
43     ts = xzalloc(sizeof *ts);
44     ts->name = xstrdup(name);
45     ts->comment = comment ? xstrdup(comment) : NULL;
46     ts->mutable = mutable;
47     shash_init(&ts->columns);
48
49     uuid = ovsdb_column_create(
50         "_uuid", "Unique identifier for this row.",
51         false, true, &ovsdb_type_uuid);
52     add_column(ts, uuid);
53     assert(uuid->index == OVSDB_COL_UUID);
54
55     version = ovsdb_column_create(
56         "_version", "Unique identifier for this version of this row.",
57         false, false, &ovsdb_type_uuid);
58     add_column(ts, version);
59     assert(version->index == OVSDB_COL_VERSION);
60
61     return ts;
62 }
63
64 struct ovsdb_table_schema *
65 ovsdb_table_schema_clone(const struct ovsdb_table_schema *old)
66 {
67     struct ovsdb_table_schema *new;
68     struct shash_node *node;
69
70     new = ovsdb_table_schema_create(old->name, old->comment, old->mutable);
71     SHASH_FOR_EACH (node, &old->columns) {
72         const struct ovsdb_column *column = node->data;
73
74         if (column->name[0] == '_') {
75             /* Added automatically by ovsdb_table_schema_create(). */
76             continue;
77         }
78
79         add_column(new, ovsdb_column_clone(column));
80     }
81     return new;
82 }
83
84 void
85 ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts)
86 {
87     struct shash_node *node;
88
89     SHASH_FOR_EACH (node, &ts->columns) {
90         ovsdb_column_destroy(node->data);
91     }
92     shash_destroy(&ts->columns);
93     free(ts->comment);
94     free(ts->name);
95     free(ts);
96 }
97
98 struct ovsdb_error *
99 ovsdb_table_schema_from_json(const struct json *json, const char *name,
100                              struct ovsdb_table_schema **tsp)
101 {
102     struct ovsdb_table_schema *ts;
103     const struct json *comment, *columns, *mutable;
104     struct shash_node *node;
105     struct ovsdb_parser parser;
106     struct ovsdb_error *error;
107
108     *tsp = NULL;
109
110     ovsdb_parser_init(&parser, json, "table schema for table %s", name);
111     comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
112     columns = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
113     mutable = ovsdb_parser_member(&parser, "mutable",
114                                   OP_TRUE | OP_FALSE | OP_OPTIONAL);
115     error = ovsdb_parser_finish(&parser);
116     if (error) {
117         return error;
118     }
119
120     if (shash_is_empty(json_object(columns))) {
121         return ovsdb_syntax_error(json, NULL,
122                                   "table must have at least one column");
123     }
124
125     ts = ovsdb_table_schema_create(name,
126                                    comment ? json_string(comment) : NULL,
127                                    mutable ? json_boolean(mutable) : true);
128     SHASH_FOR_EACH (node, json_object(columns)) {
129         struct ovsdb_column *column;
130
131         if (node->name[0] == '_') {
132             error = ovsdb_syntax_error(json, NULL, "names beginning with "
133                                        "\"_\" are reserved");
134         } else if (!ovsdb_parser_is_id(node->name)) {
135             error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
136         } else {
137             error = ovsdb_column_from_json(node->data, node->name, &column);
138         }
139         if (error) {
140             ovsdb_table_schema_destroy(ts);
141             return error;
142         }
143
144         add_column(ts, column);
145     }
146     *tsp = ts;
147     return 0;
148 }
149
150 struct json *
151 ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts)
152 {
153     struct json *json, *columns;
154     struct shash_node *node;
155
156     json = json_object_create();
157     if (ts->comment) {
158         json_object_put_string(json, "comment", ts->comment);
159     }
160     if (!ts->mutable) {
161         json_object_put(json, "mutable", json_boolean_create(false));
162     }
163
164     columns = json_object_create();
165
166     SHASH_FOR_EACH (node, &ts->columns) {
167         const struct ovsdb_column *column = node->data;
168         if (node->name[0] != '_') {
169             json_object_put(columns, column->name,
170                             ovsdb_column_to_json(column));
171         }
172     }
173     json_object_put(json, "columns", columns);
174
175     return json;
176 }
177
178 const struct ovsdb_column *
179 ovsdb_table_schema_get_column(const struct ovsdb_table_schema *ts,
180                               const char *name)
181 {
182     return shash_find_data(&ts->columns, name);
183 }
184 \f
185 struct ovsdb_table *
186 ovsdb_table_create(struct ovsdb_table_schema *ts)
187 {
188     struct ovsdb_table *table;
189
190     table = xmalloc(sizeof *table);
191     table->schema = ts;
192     table->txn_table = NULL;
193     hmap_init(&table->rows);
194
195     return table;
196 }
197
198 void
199 ovsdb_table_destroy(struct ovsdb_table *table)
200 {
201     if (table) {
202         struct ovsdb_row *row, *next;
203
204         HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_row, hmap_node,
205                             &table->rows) {
206             ovsdb_row_destroy(row);
207         }
208         hmap_destroy(&table->rows);
209
210         ovsdb_table_schema_destroy(table->schema);
211         free(table);
212     }
213 }
214
215 const struct ovsdb_row *
216 ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid)
217 {
218     struct ovsdb_row *row;
219
220     HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_row, hmap_node, uuid_hash(uuid),
221                              &table->rows) {
222         if (uuid_equals(ovsdb_row_get_uuid(row), uuid)) {
223             return row;
224         }
225     }
226
227     return NULL;
228 }
229
230 /* This is probably not the function you want.  Use ovsdb_txn_row_modify()
231  * instead. */
232 bool
233 ovsdb_table_put_row(struct ovsdb_table *table, struct ovsdb_row *row)
234 {
235     const struct uuid *uuid = ovsdb_row_get_uuid(row);
236     if (!ovsdb_table_get_row(table, uuid)) {
237         hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
238         return true;
239     } else {
240         return false;
241     }
242 }