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;
14 import org.opendaylight.ovsdb.lib.error.TyperException;
15 import org.opendaylight.ovsdb.lib.notation.ReferencedRow;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.google.common.collect.Sets;
21 public abstract class BaseType<E extends BaseType<E>> {
23 private static BaseType[] types = new BaseType[]{
25 new IntegerBaseType(),
27 new BooleanBaseType(),
31 public static BaseType fromJson(JsonNode json, String keyorval) {
32 BaseType baseType = null;
33 if (json.isValueNode()) {
34 for (BaseType baseTypeFactory : types) {
35 String type = json.asText().trim();
36 baseType = baseTypeFactory.fromString(type);
37 if (baseType != null) {
42 if (!json.has(keyorval)) {
43 throw new TyperException("Not a type");
46 for (BaseType baseTypeFactory : types) {
47 baseType = baseTypeFactory.fromJsonNode(json.get(keyorval), keyorval);
48 if (baseType != null) {
56 protected abstract E fromString(String type);
58 protected abstract void getConstraints(E baseType, JsonNode type);
60 protected E fromJsonNode(JsonNode type, String keyorval) {
65 if (type.isTextual()) {
66 baseType = fromString(type.asText());
67 if (baseType != null) {
72 //json like {"type" : "string", "enum": ["set", ["access", "native-tagged"]]}" for key or value
73 if (type.isObject() && type.has("type")) {
74 baseType = fromString(type.get("type").asText());
75 if (baseType != null) {
76 getConstraints(baseType, type);
83 public abstract Object toValue(JsonNode value);
85 public abstract void validate(Object value);
87 public static class IntegerBaseType extends BaseType<IntegerBaseType> {
88 long min = Long.MIN_VALUE;
89 long max = Long.MAX_VALUE;
93 public IntegerBaseType fromString(String typeString) {
94 return "integer".equals(typeString) ? new IntegerBaseType() : null;
98 protected void getConstraints(IntegerBaseType baseType, JsonNode type) {
100 JsonNode node = null;
102 if ((node = type.get("maxInteger")) != null) {
103 baseType.setMax(node.asLong());
106 if ((node = type.get("minInteger")) != null) {
107 baseType.setMin(node.asLong());
114 public Object toValue(JsonNode value) {
115 return value.asLong();
119 public void validate(Object value) {
123 private void populateEnum(JsonNode node) {
124 if (node.has("enum")) {
125 Set<Long> s = Sets.newHashSet();
126 JsonNode anEnum = node.get("enum").get(1);
127 for (JsonNode n : anEnum) {
134 public long getMin() {
138 public void setMin(long min) {
142 public long getMax() {
146 public void setMax(long max) {
150 public Set<Integer> getEnums() {
154 public void setEnums(Set<Integer> enums) {
159 public String toString() {
160 return "IntegerBaseType";
164 public int hashCode() {
165 final int prime = 31;
167 result = prime * result + ((enums == null) ? 0 : enums.hashCode());
168 result = prime * result + (int) (max ^ (max >>> 32));
169 result = prime * result + (int) (min ^ (min >>> 32));
174 public boolean equals(Object obj) {
179 if (getClass() != obj.getClass())
181 IntegerBaseType other = (IntegerBaseType) obj;
183 if (other.enums != null)
185 } else if (!enums.equals(other.enums))
187 if (max != other.max)
189 if (min != other.min)
195 public static class RealBaseType extends BaseType<RealBaseType> {
196 double min = Double.MIN_VALUE;
197 double max = Double.MAX_VALUE;
201 public RealBaseType fromString(String typeString) {
202 return "real".equals(typeString) ? new RealBaseType() : null;
206 protected void getConstraints(RealBaseType baseType, JsonNode type) {
208 JsonNode node = null;
210 if ((node = type.get("maxReal")) != null) {
211 baseType.setMax(node.asLong());
214 if ((node = type.get("minReal")) != null) {
215 baseType.setMin(node.asLong());
222 public Object toValue(JsonNode value) {
223 return value.asDouble();
227 public void validate(Object value) {
231 private void populateEnum(JsonNode node) {
232 if (node.has("enum")) {
233 Set<Double> s = Sets.newHashSet();
234 JsonNode anEnum = node.get("enum").get(1);
235 for (JsonNode n : anEnum) {
241 public double getMin() {
245 public void setMin(double min) {
249 public double getMax() {
253 public void setMax(double max) {
257 public Set<Double> getEnums() {
261 public void setEnums(Set<Double> enums) {
266 public String toString() {
267 return "RealBaseType";
271 public int hashCode() {
272 final int prime = 31;
274 result = prime * result + ((enums == null) ? 0 : enums.hashCode());
276 temp = Double.doubleToLongBits(max);
277 result = prime * result + (int) (temp ^ (temp >>> 32));
278 temp = Double.doubleToLongBits(min);
279 result = prime * result + (int) (temp ^ (temp >>> 32));
284 public boolean equals(Object obj) {
289 if (getClass() != obj.getClass())
291 RealBaseType other = (RealBaseType) obj;
293 if (other.enums != null)
295 } else if (!enums.equals(other.enums))
297 if (Double.doubleToLongBits(max) != Double
298 .doubleToLongBits(other.max))
300 if (Double.doubleToLongBits(min) != Double
301 .doubleToLongBits(other.min))
308 public static class BooleanBaseType extends BaseType {
311 public BooleanBaseType fromString(String typeString) {
312 return "boolean".equals(typeString) ? new BooleanBaseType() : null;
316 protected void getConstraints(BaseType baseType, JsonNode node) {
321 public Object toValue(JsonNode value) {
322 return value.asBoolean();
326 public void validate(Object value) {
331 public String toString() {
332 return "BooleanBaseType";
336 public static class StringBaseType extends BaseType<StringBaseType> {
337 int minLength = Integer.MIN_VALUE;
338 int maxLength = Integer.MAX_VALUE;
342 public StringBaseType fromString(String typeString) {
343 return "string".equals(typeString) ? new StringBaseType() : null;
347 protected void getConstraints(StringBaseType baseType, JsonNode type) {
349 JsonNode node = null;
351 if ((node = type.get("maxLength")) != null) {
352 baseType.setMaxLength(node.asInt());
355 if ((node = type.get("minLength")) != null) {
356 baseType.setMinLength(node.asInt());
359 populateEnum(baseType, type);
363 public Object toValue(JsonNode value) {
364 return value.asText();
368 public void validate(Object value) {
372 private void populateEnum(StringBaseType baseType, JsonNode node) {
373 if (node.has("enum")) {
374 Set<String> s = Sets.newHashSet();
375 JsonNode enumVal = node.get("enum");
376 if (enumVal.isArray()) {
377 JsonNode anEnum = enumVal.get(1);
378 for (JsonNode n : anEnum) {
381 } else if (enumVal.isTextual()) {
382 s.add(enumVal.asText());
384 baseType.setEnums(s);
388 public int getMinLength() {
392 public void setMinLength(int minLength) {
393 this.minLength = minLength;
396 public int getMaxLength() {
400 public void setMaxLength(int maxLength) {
401 this.maxLength = maxLength;
404 public Set<String> getEnums() {
408 public void setEnums(Set<String> enums) {
413 public String toString() {
414 return "StringBaseType";
418 public int hashCode() {
419 final int prime = 31;
421 result = prime * result + ((enums == null) ? 0 : enums.hashCode());
422 result = prime * result + maxLength;
423 result = prime * result + minLength;
428 public boolean equals(Object obj) {
433 if (getClass() != obj.getClass())
435 StringBaseType other = (StringBaseType) obj;
437 if (other.enums != null)
439 } else if (!enums.equals(other.enums))
441 if (maxLength != other.maxLength)
443 if (minLength != other.minLength)
451 public static class UuidBaseType extends BaseType<UuidBaseType> {
452 public static enum RefType {strong, weak}
459 public UuidBaseType fromString(String typeString) {
460 return "uuid".equals(typeString) ? new UuidBaseType() : null;
464 protected void getConstraints(UuidBaseType baseType, JsonNode node) {
466 JsonNode refTable = node.get("refTable");
467 baseType.setRefTable(refTable != null ? refTable.asText() : null);
469 JsonNode refTypeJson = node.get("refType");
470 baseType.setRefType(refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong);
475 public Object toValue(JsonNode value) {
476 if(value.isArray()) {
477 if (value.size() == 2) {
478 if (value.get(0).isTextual() && "uuid".equals(value.get(0).asText())) {
479 return new UUID(value.get(1).asText());
484 * UUIDBaseType used by RefTable from SouthBound will always be an Array of ["uuid", <uuid>].
485 * But there are some cases from northbound where the RefTable type can be expanded to a Row
486 * with contents. In those scenarios, just retain the content and return a ReferencedRow for
487 * the upper layer functions to process it.
489 return new ReferencedRow(refTable, value);
495 public void validate(Object value) {
499 public String getRefTable() {
503 public void setRefTable(String refTable) {
504 this.refTable = refTable;
507 public RefType getRefType() {
511 public void setRefType(RefType refType) {
512 this.refType = refType;
516 public String toString() {
517 return "UuidBaseType";
521 public int hashCode() {
522 final int prime = 31;
524 result = prime * result
525 + ((refTable == null) ? 0 : refTable.hashCode());
526 result = prime * result
527 + ((refType == null) ? 0 : refType.hashCode());
532 public boolean equals(Object obj) {
537 if (getClass() != obj.getClass())
539 UuidBaseType other = (UuidBaseType) obj;
540 if (refTable == null) {
541 if (other.refTable != null)
543 } else if (!refTable.equals(other.refTable))
545 if (refType != other.refType)