]> git.proxmox.com Git - ovs.git/blame - ovsdb/column.c
datapath: Account for "vlan: introduce *vlan_hwaccel_push_inside helpers"
[ovs.git] / ovsdb / column.c
CommitLineData
e0edde6f 1/* Copyright (c) 2009, 2010, 2011 Nicira, Inc.
f85f8ebb
BP
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 "ovsdb/column.h"
19
20#include <stdlib.h>
21
22#include "column.h"
25d49835 23#include "dynamic-string.h"
f85f8ebb
BP
24#include "json.h"
25#include "ovsdb-error.h"
26#include "ovsdb-parser.h"
27#include "table.h"
28#include "util.h"
29
30struct ovsdb_column *
2e57b537 31ovsdb_column_create(const char *name,
f85f8ebb
BP
32 bool mutable, bool persistent,
33 const struct ovsdb_type *type)
34{
58985e09 35 /* Doesn't set the new column's 'index': the caller must do that. */
6bf4c631 36 struct ovsdb_column *column;
f85f8ebb 37
6bf4c631
BP
38 column = xzalloc(sizeof *column);
39 column->name = xstrdup(name);
6bf4c631
BP
40 column->mutable = mutable;
41 column->persistent = persistent;
bd76d25d 42 ovsdb_type_clone(&column->type, type);
f85f8ebb 43
6bf4c631 44 return column;
f85f8ebb
BP
45}
46
58985e09
BP
47struct ovsdb_column *
48ovsdb_column_clone(const struct ovsdb_column *old)
49{
50 /* Doesn't copy the column's 'index': the caller must do that. */
2e57b537 51 return ovsdb_column_create(old->name,
58985e09
BP
52 old->mutable, old->persistent,
53 &old->type);
54}
55
f85f8ebb
BP
56void
57ovsdb_column_destroy(struct ovsdb_column *column)
58{
bd76d25d 59 ovsdb_type_destroy(&column->type);
f85f8ebb 60 free(column->name);
f85f8ebb
BP
61 free(column);
62}
63
64struct ovsdb_error *
65ovsdb_column_from_json(const struct json *json, const char *name,
66 struct ovsdb_column **columnp)
67{
2e57b537 68 const struct json *mutable, *ephemeral, *type_json;
f85f8ebb
BP
69 struct ovsdb_error *error;
70 struct ovsdb_type type;
71 struct ovsdb_parser parser;
72 bool persistent;
73
74 *columnp = NULL;
75
76 ovsdb_parser_init(&parser, json, "schema for column %s", name);
f85f8ebb
BP
77 mutable = ovsdb_parser_member(&parser, "mutable",
78 OP_TRUE | OP_FALSE | OP_OPTIONAL);
79 ephemeral = ovsdb_parser_member(&parser, "ephemeral",
80 OP_TRUE | OP_FALSE | OP_OPTIONAL);
81 type_json = ovsdb_parser_member(&parser, "type", OP_STRING | OP_OBJECT);
82 error = ovsdb_parser_finish(&parser);
83 if (error) {
84 return error;
85 }
86
87 error = ovsdb_type_from_json(&type, type_json);
88 if (error) {
89 return error;
90 }
91
92 persistent = ephemeral ? !json_boolean(ephemeral) : true;
93 *columnp = ovsdb_column_create(name,
f85f8ebb
BP
94 mutable ? json_boolean(mutable) : true,
95 persistent, &type);
bd76d25d
BP
96
97 ovsdb_type_destroy(&type);
98
f85f8ebb
BP
99 return NULL;
100}
101
102struct json *
103ovsdb_column_to_json(const struct ovsdb_column *column)
104{
105 struct json *json = json_object_create();
f85f8ebb
BP
106 if (!column->mutable) {
107 json_object_put(json, "mutable", json_boolean_create(false));
108 }
109 if (!column->persistent) {
110 json_object_put(json, "ephemeral", json_boolean_create(true));
111 }
112 json_object_put(json, "type", ovsdb_type_to_json(&column->type));
113 return json;
114}
115\f
116void
117ovsdb_column_set_init(struct ovsdb_column_set *set)
118{
119 set->columns = NULL;
120 set->n_columns = set->allocated_columns = 0;
121}
122
123void
124ovsdb_column_set_destroy(struct ovsdb_column_set *set)
125{
126 free(set->columns);
127}
128
129void
130ovsdb_column_set_clone(struct ovsdb_column_set *new,
131 const struct ovsdb_column_set *old)
132{
133 new->columns = xmemdup(old->columns,
134 old->n_columns * sizeof *old->columns);
135 new->n_columns = new->allocated_columns = old->n_columns;
136}
137
138struct ovsdb_error *
139ovsdb_column_set_from_json(const struct json *json,
1cb29ab0 140 const struct ovsdb_table_schema *schema,
f85f8ebb
BP
141 struct ovsdb_column_set *set)
142{
143 ovsdb_column_set_init(set);
144 if (!json) {
145 struct shash_node *node;
146
1cb29ab0 147 SHASH_FOR_EACH (node, &schema->columns) {
f85f8ebb
BP
148 const struct ovsdb_column *column = node->data;
149 ovsdb_column_set_add(set, column);
150 }
151
152 return NULL;
153 } else {
99b2042d 154 struct ovsdb_error *error = NULL;
f85f8ebb
BP
155 size_t i;
156
157 if (json->type != JSON_ARRAY) {
158 goto error;
159 }
160
161 /* XXX this is O(n**2) */
162 for (i = 0; i < json->u.array.n; i++) {
bd76d25d 163 const struct ovsdb_column *column;
99b2042d 164 const char *s;
f85f8ebb
BP
165
166 if (json->u.array.elems[i]->type != JSON_STRING) {
167 goto error;
168 }
169
99b2042d 170 s = json->u.array.elems[i]->u.string;
1cb29ab0 171 column = shash_find_data(&schema->columns, s);
99b2042d
BP
172 if (!column) {
173 error = ovsdb_syntax_error(json, NULL, "%s is not a valid "
174 "column name", s);
175 goto error;
176 } else if (ovsdb_column_set_contains(set, column->index)) {
f85f8ebb
BP
177 goto error;
178 }
179 ovsdb_column_set_add(set, column);
180 }
f85f8ebb 181 return NULL;
f85f8ebb 182
99b2042d
BP
183 error:
184 ovsdb_column_set_destroy(set);
185 ovsdb_column_set_init(set);
186 if (!error) {
187 error = ovsdb_syntax_error(json, NULL, "array of distinct column "
188 "names expected");
189 }
190 return error;
191 }
f85f8ebb
BP
192}
193
a8425c53
BP
194struct json *
195ovsdb_column_set_to_json(const struct ovsdb_column_set *set)
196{
197 struct json *json;
198 size_t i;
199
200 json = json_array_create_empty();
201 for (i = 0; i < set->n_columns; i++) {
202 json_array_add(json, json_string_create(set->columns[i]->name));
203 }
204 return json;
205}
206
25d49835
BP
207/* Returns an English string listing the contents of 'set', e.g. "columns
208 * \"a\", \"b\", and \"c\"". The caller must free the string. */
209char *
210ovsdb_column_set_to_string(const struct ovsdb_column_set *set)
211{
212 if (!set->n_columns) {
213 return xstrdup("no columns");
214 } else {
215 struct ds s;
216 size_t i;
217
218 ds_init(&s);
219 ds_put_format(&s, "column%s ", set->n_columns > 1 ? "s" : "");
220 for (i = 0; i < set->n_columns; i++) {
221 const char *delimiter = english_list_delimiter(i, set->n_columns);
222 ds_put_format(&s, "%s\"%s\"", delimiter, set->columns[i]->name);
223 }
224 return ds_steal_cstr(&s);
225 }
226}
227
f85f8ebb
BP
228void
229ovsdb_column_set_add(struct ovsdb_column_set *set,
230 const struct ovsdb_column *column)
231{
232 if (set->n_columns >= set->allocated_columns) {
233 set->columns = x2nrealloc(set->columns, &set->allocated_columns,
234 sizeof *set->columns);
235 }
236 set->columns[set->n_columns++] = column;
237}
238
239void
240ovsdb_column_set_add_all(struct ovsdb_column_set *set,
241 const struct ovsdb_table *table)
242{
243 struct shash_node *node;
244
245 SHASH_FOR_EACH (node, &table->schema->columns) {
246 const struct ovsdb_column *column = node->data;
247 ovsdb_column_set_add(set, column);
248 }
249}
250
251bool
252ovsdb_column_set_contains(const struct ovsdb_column_set *set,
253 unsigned int column_index)
254{
255 size_t i;
256
257 for (i = 0; i < set->n_columns; i++) {
258 if (set->columns[i]->index == column_index) {
259 return true;
260 }
261 }
262 return false;
263}
264
265/* This comparison is sensitive to ordering of columns within a set, but that's
266 * good: the only existing caller wants to make sure that hash values are
267 * comparable, which is only true if column ordering is the same. */
268bool
269ovsdb_column_set_equals(const struct ovsdb_column_set *a,
270 const struct ovsdb_column_set *b)
271{
272 size_t i;
273
274 if (a->n_columns != b->n_columns) {
275 return false;
276 }
277 for (i = 0; i < a->n_columns; i++) {
278 if (a->columns[i] != b->columns[i]) {
279 return false;
280 }
281 }
282 return true;
283}