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);
18 // Prevent external instantiation
21 public static BaseType fromJson(final JsonNode json, final String keyorval) {
22 if (json.isValueNode()) {
23 return singletonFor(json.asText().trim());
26 final JsonNode type = json.get(keyorval);
28 throw new TyperException("Not a type");
30 if (type.isTextual()) {
32 return singletonFor(type.asText());
34 if (type.isObject()) {
35 //json like {"type" : "string", "enum": ["set", ["access", "native-tagged"]]}" for key or value
36 final JsonNode nestedType = type.get("type");
37 if (nestedType != null) {
38 final BaseType ret = builderFor(nestedType.asText());
40 ret.fillConstraints(type);
49 abstract void fillConstraints(JsonNode type);
51 public abstract Object toValue(JsonNode value);
53 public abstract void validate(Object value);
55 // Find a simple singleton instance
56 private static BaseType singletonFor(final String type) {
59 return BooleanBaseType.SINGLETON;
61 return IntegerBaseType.SINGLETON;
63 return RealBaseType.SINGLETON;
65 return StringBaseType.SINGLETON;
67 return UuidBaseType.SINGLETON;
69 LOG.debug("Unknown base type {}", type);
74 // Create a new instance for customization
75 private static BaseType builderFor(final String type) {
78 return new BooleanBaseType();
80 return new IntegerBaseType();
82 return new RealBaseType();
84 return new StringBaseType();
86 return new UuidBaseType();
88 LOG.debug("Unknown base type {}", type);