/* Returns the type of the 'enum_' member for an ovsdb_base_type whose 'type' * is 'atomic_type'. */ const struct ovsdb_type * ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type atomic_type) { static struct ovsdb_type *types[OVSDB_N_TYPES]; if (!types[atomic_type]) { struct ovsdb_type *type; types[atomic_type] = type = xmalloc(sizeof *type); ovsdb_base_type_init(&type->key, atomic_type); ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID); type->n_min = 1; type->n_max = UINT_MAX; } return types[atomic_type]; }
void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base) { enum ovsdb_atomic_type type = base->type; ovsdb_base_type_destroy(base); ovsdb_base_type_init(base, type); }
/* Returns the type of the 'enum_' member for an ovsdb_base_type whose 'type' * is 'atomic_type'. */ const struct ovsdb_type * ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type atomic_type) { static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; static struct ovsdb_type *types[OVSDB_N_TYPES]; if (ovsthread_once_start(&once)) { enum ovsdb_atomic_type i; for (i = 0; i < OVSDB_N_TYPES; i++) { struct ovsdb_type *type; types[i] = type = xmalloc(sizeof *type); ovsdb_base_type_init(&type->key, i); ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID); type->n_min = 1; type->n_max = UINT_MAX; } ovsthread_once_done(&once); } return types[atomic_type]; }
struct ovsdb_error * ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json) { ovsdb_base_type_init(&type->value, OVSDB_TYPE_VOID); type->n_min = 1; type->n_max = 1; if (json->type == JSON_STRING) { return ovsdb_base_type_from_json(&type->key, json); } else if (json->type == JSON_OBJECT) { const struct json *key, *value, *min, *max; struct ovsdb_error *error; struct ovsdb_parser parser; ovsdb_parser_init(&parser, json, "ovsdb type"); key = ovsdb_parser_member(&parser, "key", OP_STRING | OP_OBJECT); value = ovsdb_parser_member(&parser, "value", OP_STRING | OP_OBJECT | OP_OPTIONAL); min = ovsdb_parser_member(&parser, "min", OP_INTEGER | OP_OPTIONAL); max = ovsdb_parser_member(&parser, "max", OP_INTEGER | OP_STRING | OP_OPTIONAL); error = ovsdb_parser_finish(&parser); if (error) { return error; } error = ovsdb_base_type_from_json(&type->key, key); if (error) { return error; } if (value) { error = ovsdb_base_type_from_json(&type->value, value); if (error) { return error; } } error = n_from_json(min, &type->n_min); if (error) { return error; } if (max && max->type == JSON_STRING && !strcmp(max->u.string, "unlimited")) { type->n_max = UINT_MAX; } else { error = n_from_json(max, &type->n_max); if (error) { return error; } } if (!ovsdb_type_is_valid(type)) { return ovsdb_syntax_error(json, NULL, "ovsdb type fails constraint checks"); } return NULL; } else { return ovsdb_syntax_error(json, NULL, "ovsdb type expected"); } }
struct ovsdb_error * ovsdb_base_type_from_json(struct ovsdb_base_type *base, const struct json *json) { struct ovsdb_parser parser; struct ovsdb_error *error; const struct json *type, *enum_; if (json->type == JSON_STRING) { error = ovsdb_atomic_type_from_json(&base->type, json); if (error) { return error; } ovsdb_base_type_init(base, base->type); return NULL; } ovsdb_parser_init(&parser, json, "ovsdb type"); type = ovsdb_parser_member(&parser, "type", OP_STRING); if (ovsdb_parser_has_error(&parser)) { base->type = OVSDB_TYPE_VOID; return ovsdb_parser_finish(&parser); } error = ovsdb_atomic_type_from_json(&base->type, type); if (error) { return error; } ovsdb_base_type_init(base, base->type); enum_ = ovsdb_parser_member(&parser, "enum", OP_ANY | OP_OPTIONAL); if (enum_) { base->enum_ = xmalloc(sizeof *base->enum_); error = ovsdb_datum_from_json( base->enum_, ovsdb_base_type_get_enum_type(base->type), enum_, NULL); if (error) { free(base->enum_); base->enum_ = NULL; } } else if (base->type == OVSDB_TYPE_INTEGER) { const struct json *min, *max; min = ovsdb_parser_member(&parser, "minInteger", OP_INTEGER | OP_OPTIONAL); max = ovsdb_parser_member(&parser, "maxInteger", OP_INTEGER | OP_OPTIONAL); base->u.integer.min = min ? min->u.integer : INT64_MIN; base->u.integer.max = max ? max->u.integer : INT64_MAX; if (base->u.integer.min > base->u.integer.max) { error = ovsdb_syntax_error(json, NULL, "minInteger exceeds maxInteger"); } } else if (base->type == OVSDB_TYPE_REAL) { const struct json *min, *max; min = ovsdb_parser_member(&parser, "minReal", OP_NUMBER | OP_OPTIONAL); max = ovsdb_parser_member(&parser, "maxReal", OP_NUMBER | OP_OPTIONAL); base->u.real.min = min ? json_real(min) : -DBL_MAX; base->u.real.max = max ? json_real(max) : DBL_MAX; if (base->u.real.min > base->u.real.max) { error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal"); } } else if (base->type == OVSDB_TYPE_STRING) { if (!error) { error = parse_optional_uint(&parser, "minLength", &base->u.string.minLen); } if (!error) { error = parse_optional_uint(&parser, "maxLength", &base->u.string.maxLen); } if (!error && base->u.string.minLen > base->u.string.maxLen) { error = ovsdb_syntax_error(json, NULL, "minLength exceeds maxLength"); } } else if (base->type == OVSDB_TYPE_UUID) { const struct json *refTable; refTable = ovsdb_parser_member(&parser, "refTable", OP_ID | OP_OPTIONAL); if (refTable) { const struct json *refType; base->u.uuid.refTableName = xstrdup(refTable->u.string); /* We can't set base->u.uuid.refTable here because we don't have * enough context (we might not even be running in ovsdb-server). * ovsdb_create() will set refTable later. */ refType = ovsdb_parser_member(&parser, "refType", OP_ID | OP_OPTIONAL); if (refType) { const char *refType_s = json_string(refType); if (!strcmp(refType_s, "strong")) { base->u.uuid.refType = OVSDB_REF_STRONG; } else if (!strcmp(refType_s, "weak")) { base->u.uuid.refType = OVSDB_REF_WEAK; } else { error = ovsdb_syntax_error(json, NULL, "refType must be " "\"strong\" or \"weak\" (not " "\"%s\")", refType_s); } } else { base->u.uuid.refType = OVSDB_REF_STRONG; } } } if (error) { ovsdb_error_destroy(ovsdb_parser_finish(&parser)); } else { error = ovsdb_parser_finish(&parser); } if (error) { ovsdb_base_type_destroy(base); base->type = OVSDB_TYPE_VOID; } return error; }