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