X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fovsdb-data.c;h=bb8781c25d5c87dd4ef062af349fb88bf3f475a9;hb=05bfbcb693680794c5a0398587a7da6b46b3e3f5;hp=20d333eff177f6b93633f4b81d3bd627350d7acf;hpb=829d41bdc8fd4841b97eedf2ae7e6b8309e951e1;p=sliver-openvswitch.git diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 20d333eff..bb8781c25 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010 Nicira Networks +/* Copyright (c) 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ #include "dynamic-string.h" #include "hash.h" #include "ovsdb-error.h" +#include "ovsdb-parser.h" #include "json.h" #include "shash.h" #include "sort.h" @@ -284,9 +285,28 @@ parse_json_pair(const struct json *json, return NULL; } +static void +ovsdb_symbol_referenced(struct ovsdb_symbol *symbol, + const struct ovsdb_base_type *base) +{ + assert(base->type == OVSDB_TYPE_UUID); + + if (base->u.uuid.refTableName) { + switch (base->u.uuid.refType) { + case OVSDB_REF_STRONG: + symbol->strong_ref = true; + break; + case OVSDB_REF_WEAK: + symbol->weak_ref = true; + break; + } + } +} + static struct ovsdb_error * WARN_UNUSED_RESULT ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, - struct ovsdb_symbol_table *symtab) + struct ovsdb_symbol_table *symtab, + const struct ovsdb_base_type *base) { struct ovsdb_error *error0; const struct json *value; @@ -303,10 +323,17 @@ ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value); if (!error1) { - const char *name = json_string(value); + struct ovsdb_symbol *symbol; ovsdb_error_destroy(error0); - *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid; + if (!ovsdb_parser_is_id(json_string(value))) { + return ovsdb_syntax_error(json, NULL, "named-uuid string is " + "not a valid "); + } + + symbol = ovsdb_symbol_table_insert(symtab, json_string(value)); + *uuid = symbol->uuid; + ovsdb_symbol_referenced(symbol, base); return NULL; } ovsdb_error_destroy(error1); @@ -316,10 +343,13 @@ ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, } static struct ovsdb_error * WARN_UNUSED_RESULT -ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, +ovsdb_atom_from_json__(union ovsdb_atom *atom, + const struct ovsdb_base_type *base, const struct json *json, struct ovsdb_symbol_table *symtab) { + enum ovsdb_atomic_type type = base->type; + switch (type) { case OVSDB_TYPE_VOID: NOT_REACHED(); @@ -359,7 +389,7 @@ ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, break; case OVSDB_TYPE_UUID: - return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab); + return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab, base); case OVSDB_N_TYPES: default: @@ -379,7 +409,9 @@ ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, * * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to * ovsdb/SPECS for information about this, and for the syntax that this - * function accepts. */ + * function accepts. If 'base' is a reference and a symbol is parsed, then the + * symbol's 'strong_ref' or 'weak_ref' member is set to true, as + * appropriate. */ struct ovsdb_error * ovsdb_atom_from_json(union ovsdb_atom *atom, const struct ovsdb_base_type *base, @@ -388,7 +420,7 @@ ovsdb_atom_from_json(union ovsdb_atom *atom, { struct ovsdb_error *error; - error = ovsdb_atom_from_json__(atom, base->type, json, symtab); + error = ovsdb_atom_from_json__(atom, base, json, symtab); if (error) { return error; } @@ -435,9 +467,12 @@ ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type) } static char * -ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, - const char *s, struct ovsdb_symbol_table *symtab) +ovsdb_atom_from_string__(union ovsdb_atom *atom, + const struct ovsdb_base_type *base, const char *s, + struct ovsdb_symbol_table *symtab) { + enum ovsdb_atomic_type type = base->type; + switch (type) { case OVSDB_TYPE_VOID: NOT_REACHED(); @@ -498,7 +533,9 @@ ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, case OVSDB_TYPE_UUID: if (*s == '@') { - atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid; + struct ovsdb_symbol *symbol = ovsdb_symbol_table_insert(symtab, s); + atom->uuid = symbol->uuid; + ovsdb_symbol_referenced(symbol, base); } else if (!uuid_from_string(&atom->uuid, s)) { return xasprintf("\"%s\" is not a valid UUID", s); } @@ -530,7 +567,9 @@ ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, * then an identifier beginning with '@' is also acceptable. If the * named identifier is already in 'symtab', then the associated UUID is * used; otherwise, a new, random UUID is used and added to the symbol - * table. + * table. If 'base' is a reference and a symbol is parsed, then the + * symbol's 'strong_ref' or 'weak_ref' member is set to true, as + * appropriate. * * Returns a null pointer if successful, otherwise an error message describing * the problem. On failure, the contents of 'atom' are indeterminate. The @@ -544,7 +583,7 @@ ovsdb_atom_from_string(union ovsdb_atom *atom, struct ovsdb_error *error; char *msg; - msg = ovsdb_atom_from_string__(atom, base->type, s, symtab); + msg = ovsdb_atom_from_string__(atom, base, s, symtab); if (msg) { return msg; } @@ -625,6 +664,20 @@ ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, } } +/* Appends 'atom' (which has the given 'type') to 'out', in a bare string + * format that cannot be parsed uniformly back into a datum but is easier for + * shell scripts, etc., to deal with. */ +void +ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, + struct ds *out) +{ + if (type == OVSDB_TYPE_STRING) { + ds_put_cstr(out, atom->string); + } else { + ovsdb_atom_to_string(atom, type, out); + } +} + static struct ovsdb_error * check_string_constraints(const char *s, const struct ovsdb_string_constraints *c) @@ -637,8 +690,7 @@ check_string_constraints(const char *s, struct ovsdb_error *error; error = ovsdb_error("constraint violation", - "\"%s\" is not a valid UTF-8 string: %s", - s, msg); + "not a valid UTF-8 string: %s", msg); free(msg); return error; } @@ -1226,30 +1278,30 @@ struct json * ovsdb_datum_to_json(const struct ovsdb_datum *datum, const struct ovsdb_type *type) { - if (datum->n == 1 && !ovsdb_type_is_map(type)) { - return ovsdb_atom_to_json(&datum->keys[0], type->key.type); - } else if (type->value.type == OVSDB_TYPE_VOID) { + if (ovsdb_type_is_map(type)) { struct json **elems; size_t i; elems = xmalloc(datum->n * sizeof *elems); for (i = 0; i < datum->n; i++) { - elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type); + elems[i] = json_array_create_2( + ovsdb_atom_to_json(&datum->keys[i], type->key.type), + ovsdb_atom_to_json(&datum->values[i], type->value.type)); } - return wrap_json("set", json_array_create(elems, datum->n)); + return wrap_json("map", json_array_create(elems, datum->n)); + } else if (datum->n == 1) { + return ovsdb_atom_to_json(&datum->keys[0], type->key.type); } else { struct json **elems; size_t i; elems = xmalloc(datum->n * sizeof *elems); for (i = 0; i < datum->n; i++) { - elems[i] = json_array_create_2( - ovsdb_atom_to_json(&datum->keys[i], type->key.type), - ovsdb_atom_to_json(&datum->values[i], type->value.type)); + elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type); } - return wrap_json("map", json_array_create(elems, datum->n)); + return wrap_json("set", json_array_create(elems, datum->n)); } } @@ -1353,8 +1405,10 @@ ovsdb_datum_from_string(struct ovsdb_datum *datum, union ovsdb_atom key, value; if (ovsdb_token_is_delim(*p)) { + char *type_str = ovsdb_type_to_english(type); error = xasprintf("%s: unexpected \"%c\" parsing %s", - s, *p, ovsdb_type_to_english(type)); + s, *p, type_str); + free(type_str); goto error; } @@ -1445,6 +1499,29 @@ ovsdb_datum_to_string(const struct ovsdb_datum *datum, } } +/* Appends to 'out' the 'datum' (with the given 'type') in a bare string format + * that cannot be parsed uniformly back into a datum but is easier for shell + * scripts, etc., to deal with. */ +void +ovsdb_datum_to_bare(const struct ovsdb_datum *datum, + const struct ovsdb_type *type, struct ds *out) +{ + bool is_map = ovsdb_type_is_map(type); + size_t i; + + for (i = 0; i < datum->n; i++) { + if (i > 0) { + ds_put_cstr(out, " "); + } + + ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out); + if (is_map) { + ds_put_char(out, '='); + ovsdb_atom_to_bare(&datum->values[i], type->value.type, out); + } + } +} + /* Initializes 'datum' as a string-to-string map whose contents are taken from * 'sh'. Destroys 'sh'. */ void @@ -1610,6 +1687,9 @@ ovsdb_datum_includes_all(const struct ovsdb_datum *a, { size_t i; + if (a->n > b->n) { + return false; + } for (i = 0; i < a->n; i++) { if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) { return false; @@ -1751,10 +1831,6 @@ ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type, } } -struct ovsdb_symbol_table { - struct shash sh; -}; - struct ovsdb_symbol_table * ovsdb_symbol_table_create(void) { @@ -1781,14 +1857,16 @@ ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab, struct ovsdb_symbol * ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, - const struct uuid *uuid, bool used) + const struct uuid *uuid, bool created) { struct ovsdb_symbol *symbol; assert(!ovsdb_symbol_table_get(symtab, name)); symbol = xmalloc(sizeof *symbol); symbol->uuid = *uuid; - symbol->used = used; + symbol->created = created; + symbol->strong_ref = false; + symbol->weak_ref = false; shash_add(&symtab->sh, name, symbol); return symbol; } @@ -1808,21 +1886,6 @@ ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab, } return symbol; } - -const char * -ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *symtab) -{ - struct shash_node *node; - - SHASH_FOR_EACH (node, &symtab->sh) { - struct ovsdb_symbol *symbol = node->data; - if (!symbol->used) { - return node->name; - } - } - - return NULL; -} /* Extracts a token from the beginning of 's' and returns a pointer just after * the token. Stores the token itself into '*outp', which the caller is