2 * Copyright © 2014, 2017 EBay Software Foundation and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.ovsdb.lib.schema;
10 import com.fasterxml.jackson.databind.JsonNode;
11 import org.opendaylight.ovsdb.lib.error.TyperException;
12 import org.slf4j.Logger;
14 public abstract class BaseType<E extends BaseType<E>> {
15 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(BaseType.class);
17 public static BaseType fromJson(final JsonNode json, final String keyorval) {
18 if (json.isValueNode()) {
19 return fromString(json.asText().trim());
22 final JsonNode type = json.get(keyorval);
24 throw new TyperException("Not a type");
26 if (type.isTextual()) {
28 return fromString(type.asText());
30 if (type.isObject()) {
31 //json like {"type" : "string", "enum": ["set", ["access", "native-tagged"]]}" for key or value
32 final JsonNode nestedType = type.get("type");
33 if (nestedType != null) {
34 final BaseType ret = fromString(nestedType.asText());
36 ret.fillConstraints(type);
45 abstract void fillConstraints(JsonNode type);
47 public abstract Object toValue(JsonNode value);
49 public abstract void validate(Object value);
51 private static BaseType fromString(final String type) {
54 return new BooleanBaseType();
56 return new IntegerBaseType();
58 return new RealBaseType();
60 return new StringBaseType();
62 return new UuidBaseType();
64 LOG.debug("Unknown base type {}", type);