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