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