Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.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,
31                     bool mutable, bool persistent,
32                     const struct ovsdb_type *type)
33 {
34     /* Doesn't set the new column's 'index': the caller must do that. */
35     struct ovsdb_column *column;
36
37     column = xzalloc(sizeof *column);
38     column->name = xstrdup(name);
39     column->mutable = mutable;
40     column->persistent = persistent;
41     ovsdb_type_clone(&column->type, type);
42
43     return column;
44 }
45
46 struct ovsdb_column *
47 ovsdb_column_clone(const struct ovsdb_column *old)
48 {
49     /* Doesn't copy the column's 'index': the caller must do that. */
50     return ovsdb_column_create(old->name,
51                                old->mutable, old->persistent,
52                                &old->type);
53 }
54
55 void
56 ovsdb_column_destroy(struct ovsdb_column *column)
57 {
58     ovsdb_type_destroy(&column->type);
59     free(column->name);
60     free(column);
61 }
62
63 struct ovsdb_error *
64 ovsdb_column_from_json(const struct json *json, const char *name,
65                        struct ovsdb_column **columnp)
66 {
67     const struct json *mutable, *ephemeral, *type_json;
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);
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,
93                                    mutable ? json_boolean(mutable) : true,
94                                    persistent, &type);
95
96     ovsdb_type_destroy(&type);
97
98     return NULL;
99 }
100
101 struct json *
102 ovsdb_column_to_json(const struct ovsdb_column *column)
103 {
104     struct json *json = json_object_create();
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
115 void
116 ovsdb_column_set_init(struct ovsdb_column_set *set)
117 {
118     set->columns = NULL;
119     set->n_columns = set->allocated_columns = 0;
120 }
121
122 void
123 ovsdb_column_set_destroy(struct ovsdb_column_set *set)
124 {
125     free(set->columns);
126 }
127
128 void
129 ovsdb_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
137 struct ovsdb_error *
138 ovsdb_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 {
153         struct ovsdb_error *error = NULL;
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++) {
162             const struct ovsdb_column *column;
163             const char *s;
164
165             if (json->u.array.elems[i]->type != JSON_STRING) {
166                 goto error;
167             }
168
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)) {
176                 goto error;
177             }
178             ovsdb_column_set_add(set, column);
179         }
180         return NULL;
181
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     }
191 }
192
193 struct json *
194 ovsdb_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
206 void
207 ovsdb_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
217 void
218 ovsdb_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
229 bool
230 ovsdb_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. */
246 bool
247 ovsdb_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 }