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