2010-02-08 14:09:36 -08:00
|
|
|
|
/* Copyright (c) 2009, 2010 Nicira Networks
|
2009-11-04 15:11:44 -08:00
|
|
|
|
*
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "ovsdb/column.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "column.h"
|
|
|
|
|
|
#include "json.h"
|
|
|
|
|
|
#include "ovsdb-error.h"
|
|
|
|
|
|
#include "ovsdb-parser.h"
|
|
|
|
|
|
#include "table.h"
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct ovsdb_column *
|
|
|
|
|
|
ovsdb_column_create(const char *name, const char *comment,
|
|
|
|
|
|
bool mutable, bool persistent,
|
|
|
|
|
|
const struct ovsdb_type *type)
|
|
|
|
|
|
{
|
2010-02-10 15:37:52 -08:00
|
|
|
|
/* Doesn't set the new column's 'index': the caller must do that. */
|
2009-11-20 15:40:11 -08:00
|
|
|
|
struct ovsdb_column *column;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-20 15:40:11 -08:00
|
|
|
|
column = xzalloc(sizeof *column);
|
|
|
|
|
|
column->name = xstrdup(name);
|
|
|
|
|
|
column->comment = comment ? xstrdup(comment) : NULL;
|
|
|
|
|
|
column->mutable = mutable;
|
|
|
|
|
|
column->persistent = persistent;
|
2010-02-08 14:09:36 -08:00
|
|
|
|
ovsdb_type_clone(&column->type, type);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-20 15:40:11 -08:00
|
|
|
|
return column;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-02-10 15:37:52 -08:00
|
|
|
|
struct ovsdb_column *
|
|
|
|
|
|
ovsdb_column_clone(const struct ovsdb_column *old)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Doesn't copy the column's 'index': the caller must do that. */
|
|
|
|
|
|
return ovsdb_column_create(old->name, old->comment,
|
|
|
|
|
|
old->mutable, old->persistent,
|
|
|
|
|
|
&old->type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
|
void
|
|
|
|
|
|
ovsdb_column_destroy(struct ovsdb_column *column)
|
|
|
|
|
|
{
|
2010-02-08 14:09:36 -08:00
|
|
|
|
ovsdb_type_destroy(&column->type);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
free(column->name);
|
|
|
|
|
|
free(column->comment);
|
|
|
|
|
|
free(column);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ovsdb_error *
|
|
|
|
|
|
ovsdb_column_from_json(const struct json *json, const char *name,
|
|
|
|
|
|
struct ovsdb_column **columnp)
|
|
|
|
|
|
{
|
|
|
|
|
|
const struct json *comment, *mutable, *ephemeral, *type_json;
|
|
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
|
struct ovsdb_type type;
|
|
|
|
|
|
struct ovsdb_parser parser;
|
|
|
|
|
|
bool persistent;
|
|
|
|
|
|
|
|
|
|
|
|
*columnp = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
ovsdb_parser_init(&parser, json, "schema for column %s", name);
|
|
|
|
|
|
comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
|
|
|
|
|
|
mutable = ovsdb_parser_member(&parser, "mutable",
|
|
|
|
|
|
OP_TRUE | OP_FALSE | OP_OPTIONAL);
|
|
|
|
|
|
ephemeral = ovsdb_parser_member(&parser, "ephemeral",
|
|
|
|
|
|
OP_TRUE | OP_FALSE | OP_OPTIONAL);
|
|
|
|
|
|
type_json = ovsdb_parser_member(&parser, "type", OP_STRING | OP_OBJECT);
|
|
|
|
|
|
error = ovsdb_parser_finish(&parser);
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
error = ovsdb_type_from_json(&type, type_json);
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
persistent = ephemeral ? !json_boolean(ephemeral) : true;
|
|
|
|
|
|
*columnp = ovsdb_column_create(name,
|
|
|
|
|
|
comment ? json_string(comment) : NULL,
|
|
|
|
|
|
mutable ? json_boolean(mutable) : true,
|
|
|
|
|
|
persistent, &type);
|
2010-02-08 14:09:36 -08:00
|
|
|
|
|
|
|
|
|
|
ovsdb_type_destroy(&type);
|
|
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct json *
|
|
|
|
|
|
ovsdb_column_to_json(const struct ovsdb_column *column)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct json *json = json_object_create();
|
|
|
|
|
|
if (column->comment) {
|
|
|
|
|
|
json_object_put_string(json, "comment", column->comment);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!column->mutable) {
|
|
|
|
|
|
json_object_put(json, "mutable", json_boolean_create(false));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!column->persistent) {
|
|
|
|
|
|
json_object_put(json, "ephemeral", json_boolean_create(true));
|
|
|
|
|
|
}
|
|
|
|
|
|
json_object_put(json, "type", ovsdb_type_to_json(&column->type));
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ovsdb_column_set_init(struct ovsdb_column_set *set)
|
|
|
|
|
|
{
|
|
|
|
|
|
set->columns = NULL;
|
|
|
|
|
|
set->n_columns = set->allocated_columns = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ovsdb_column_set_destroy(struct ovsdb_column_set *set)
|
|
|
|
|
|
{
|
|
|
|
|
|
free(set->columns);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ovsdb_column_set_clone(struct ovsdb_column_set *new,
|
|
|
|
|
|
const struct ovsdb_column_set *old)
|
|
|
|
|
|
{
|
|
|
|
|
|
new->columns = xmemdup(old->columns,
|
|
|
|
|
|
old->n_columns * sizeof *old->columns);
|
|
|
|
|
|
new->n_columns = new->allocated_columns = old->n_columns;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ovsdb_error *
|
|
|
|
|
|
ovsdb_column_set_from_json(const struct json *json,
|
|
|
|
|
|
const struct ovsdb_table *table,
|
|
|
|
|
|
struct ovsdb_column_set *set)
|
|
|
|
|
|
{
|
|
|
|
|
|
ovsdb_column_set_init(set);
|
|
|
|
|
|
if (!json) {
|
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &table->schema->columns) {
|
|
|
|
|
|
const struct ovsdb_column *column = node->data;
|
|
|
|
|
|
ovsdb_column_set_add(set, column);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
} else {
|
2009-12-11 16:37:29 -08:00
|
|
|
|
struct ovsdb_error *error = NULL;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
|
|
if (json->type != JSON_ARRAY) {
|
|
|
|
|
|
goto error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* XXX this is O(n**2) */
|
|
|
|
|
|
for (i = 0; i < json->u.array.n; i++) {
|
2010-02-08 14:09:36 -08:00
|
|
|
|
const struct ovsdb_column *column;
|
2009-12-11 16:37:29 -08:00
|
|
|
|
const char *s;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
|
|
|
|
|
if (json->u.array.elems[i]->type != JSON_STRING) {
|
|
|
|
|
|
goto error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-12-11 16:37:29 -08:00
|
|
|
|
s = json->u.array.elems[i]->u.string;
|
|
|
|
|
|
column = shash_find_data(&table->schema->columns, s);
|
|
|
|
|
|
if (!column) {
|
|
|
|
|
|
error = ovsdb_syntax_error(json, NULL, "%s is not a valid "
|
|
|
|
|
|
"column name", s);
|
|
|
|
|
|
goto error;
|
|
|
|
|
|
} else if (ovsdb_column_set_contains(set, column->index)) {
|
2009-11-04 15:11:44 -08:00
|
|
|
|
goto error;
|
|
|
|
|
|
}
|
|
|
|
|
|
ovsdb_column_set_add(set, column);
|
|
|
|
|
|
}
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
2009-12-11 16:37:29 -08:00
|
|
|
|
error:
|
|
|
|
|
|
ovsdb_column_set_destroy(set);
|
|
|
|
|
|
ovsdb_column_set_init(set);
|
|
|
|
|
|
if (!error) {
|
|
|
|
|
|
error = ovsdb_syntax_error(json, NULL, "array of distinct column "
|
|
|
|
|
|
"names expected");
|
|
|
|
|
|
}
|
|
|
|
|
|
return error;
|
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct json *
|
|
|
|
|
|
ovsdb_column_set_to_json(const struct ovsdb_column_set *set)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct json *json;
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
|
|
json = json_array_create_empty();
|
|
|
|
|
|
for (i = 0; i < set->n_columns; i++) {
|
|
|
|
|
|
json_array_add(json, json_string_create(set->columns[i]->name));
|
|
|
|
|
|
}
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
|
void
|
|
|
|
|
|
ovsdb_column_set_add(struct ovsdb_column_set *set,
|
|
|
|
|
|
const struct ovsdb_column *column)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (set->n_columns >= set->allocated_columns) {
|
|
|
|
|
|
set->columns = x2nrealloc(set->columns, &set->allocated_columns,
|
|
|
|
|
|
sizeof *set->columns);
|
|
|
|
|
|
}
|
|
|
|
|
|
set->columns[set->n_columns++] = column;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ovsdb_column_set_add_all(struct ovsdb_column_set *set,
|
|
|
|
|
|
const struct ovsdb_table *table)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &table->schema->columns) {
|
|
|
|
|
|
const struct ovsdb_column *column = node->data;
|
|
|
|
|
|
ovsdb_column_set_add(set, column);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
ovsdb_column_set_contains(const struct ovsdb_column_set *set,
|
|
|
|
|
|
unsigned int column_index)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < set->n_columns; i++) {
|
|
|
|
|
|
if (set->columns[i]->index == column_index) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This comparison is sensitive to ordering of columns within a set, but that's
|
|
|
|
|
|
* good: the only existing caller wants to make sure that hash values are
|
|
|
|
|
|
* comparable, which is only true if column ordering is the same. */
|
|
|
|
|
|
bool
|
|
|
|
|
|
ovsdb_column_set_equals(const struct ovsdb_column_set *a,
|
|
|
|
|
|
const struct ovsdb_column_set *b)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
|
|
if (a->n_columns != b->n_columns) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (i = 0; i < a->n_columns; i++) {
|
|
|
|
|
|
if (a->columns[i] != b->columns[i]) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|