2 * Copyright (C) 2014 EBay Software Foundation
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 * Authors : Ashwin Raveendran
10 package org.opendaylight.ovsdb.lib.schema;
12 import com.fasterxml.jackson.databind.JsonNode;
13 import com.google.common.collect.Sets;
17 public abstract class BaseType<E extends BaseType<E>> {
19 private static BaseType[] types = new BaseType[]{
21 new IntegerBaseType(),
23 new BooleanBaseType(),
27 public static BaseType fromJson(JsonNode json, String keyorval) {
28 BaseType baseType = null;
29 if (json.isValueNode()) {
30 for (BaseType baseTypeFactory : types) {
31 String type = json.asText().trim();
32 baseType = baseTypeFactory.fromString(type);
33 if (baseType != null) {
38 if (!json.has(keyorval)) {
39 throw new RuntimeException("Not a type");
42 for (BaseType baseTypeFactory : types) {
43 baseType = baseTypeFactory.fromJsonNode(json.get(keyorval), keyorval);
44 if (baseType != null) {
52 protected abstract E fromString(String type);
54 protected abstract void getConstraints(E baseType, JsonNode type);
56 protected E fromJsonNode(JsonNode type, String keyorval) {
61 if (type.isTextual()) {
62 baseType = fromString(type.asText());
63 if (baseType != null) {
68 //json like {"type" : "string", "enum": ["set", ["access", "native-tagged"]]}" for key or value
69 if (type.isObject() && type.has("type")) {
70 baseType = fromString(type.get("type").asText());
71 if (baseType != null) {
72 getConstraints(baseType, type);
79 public static class IntegerBaseType extends BaseType<IntegerBaseType> {
80 long min = Long.MIN_VALUE;
81 long max = Long.MAX_VALUE;
84 public IntegerBaseType fromString(String typeString) {
85 return "integer".equals(typeString) ? new IntegerBaseType() : null;
89 protected void getConstraints(IntegerBaseType baseType, JsonNode type) {
93 if ((node = type.get("maxInteger")) != null) {
94 baseType.setMax(node.asLong());
97 if ((node = type.get("minInteger")) != null) {
98 baseType.setMin(node.asLong());
104 private void populateEnum(JsonNode node) {
105 if (node.has("enum")) {
106 Set<Long> s = Sets.newHashSet();
107 JsonNode anEnum = node.get("enum").get(1);
108 for (JsonNode n : anEnum) {
115 public long getMin() {
119 public void setMin(long min) {
123 public long getMax() {
127 public void setMax(long max) {
131 public Set<Integer> getEnums() {
135 public void setEnums(Set<Integer> enums) {
140 public String toString() {
141 return "IntegerBaseType";
145 public static class RealBaseType extends BaseType<RealBaseType> {
146 double min = Double.MIN_VALUE;
147 double max = Double.MAX_VALUE;
150 public RealBaseType fromString(String typeString) {
151 return "real".equals(typeString) ? new RealBaseType() : null;
155 protected void getConstraints(RealBaseType baseType, JsonNode type) {
157 JsonNode node = null;
159 if ((node = type.get("maxReal")) != null) {
160 baseType.setMax(node.asLong());
163 if ((node = type.get("minReal")) != null) {
164 baseType.setMin(node.asLong());
170 private void populateEnum(JsonNode node) {
171 if (node.has("enum")) {
172 Set<Double> s = Sets.newHashSet();
173 JsonNode anEnum = node.get("enum").get(1);
174 for (JsonNode n : anEnum) {
180 public double getMin() {
184 public void setMin(double min) {
188 public double getMax() {
192 public void setMax(double max) {
196 public Set<Double> getEnums() {
200 public void setEnums(Set<Double> enums) {
205 public String toString() {
206 return "RealBaseType";
211 public static class BooleanBaseType extends BaseType {
213 public BooleanBaseType fromString(String typeString) {
214 return "boolean".equals(typeString) ? new BooleanBaseType() : null;
218 protected void getConstraints(BaseType baseType, JsonNode node) {
223 public String toString() {
224 return "BooleanBaseType";
228 public static class StringBaseType extends BaseType<StringBaseType> {
229 int minLength = Integer.MIN_VALUE;
230 int maxLength = Integer.MAX_VALUE;
233 public StringBaseType fromString(String typeString) {
234 return "string".equals(typeString) ? new StringBaseType() : null;
238 protected void getConstraints(StringBaseType baseType, JsonNode type) {
240 JsonNode node = null;
242 if ((node = type.get("maxLength")) != null) {
243 baseType.setMaxLength(node.asInt());
246 if ((node = type.get("minLength")) != null) {
247 baseType.setMinLength(node.asInt());
250 populateEnum(baseType, type);
253 private void populateEnum(StringBaseType baseType, JsonNode node) {
254 if (node.has("enum")) {
255 Set<String> s = Sets.newHashSet();
256 JsonNode anEnum = node.get("enum").get(1);
257 for (JsonNode n : anEnum) {
260 baseType.setEnums(s);
264 public int getMinLength() {
268 public void setMinLength(int minLength) {
269 this.minLength = minLength;
272 public int getMaxLength() {
276 public void setMaxLength(int maxLength) {
277 this.maxLength = maxLength;
280 public Set<String> getEnums() {
284 public void setEnums(Set<String> enums) {
288 public String toString() {
289 return "StringBaseType";
295 public static class UuidBaseType extends BaseType<UuidBaseType> {
296 public static enum RefType {strong, weak}
302 public UuidBaseType fromString(String typeString) {
303 return "uuid".equals(typeString) ? new UuidBaseType() : null;
307 protected void getConstraints(UuidBaseType baseType, JsonNode node) {
309 JsonNode refTable = node.get("refTable");
310 baseType.setRefTable(refTable != null ? refTable.asText() : null);
312 JsonNode refTypeJson = node.get("refType");
313 baseType.setRefType(refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong);
317 public String getRefTable() {
321 public void setRefTable(String refTable) {
322 this.refTable = refTable;
325 public RefType getRefType() {
329 public void setRefType(RefType refType) {
330 this.refType = refType;
333 public String toString() {
334 return "UuidBaseType";