2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.ovsdb.rfc.schema.type;
20 import org.onosproject.ovsdb.rfc.exception.AbnormalJsonNodeException;
21 import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType.RefType;
22 import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.google.common.collect.Sets;
28 * BaseType Factory class.
30 public final class BaseTypeFactory {
33 * Constructs a BaseTypeFactory object. This class should not be
36 private BaseTypeFactory() {
40 * Create a BaseType from the JsonNode.
41 * @param baseTypeJson the BaseType JsonNode
42 * @param keyorval the key node or value node
45 public static BaseType getBaseTypeFromJson(JsonNode baseTypeJson, String keyorval) {
46 if (baseTypeJson.isValueNode()) {
47 String type = baseTypeJson.asText().trim();
48 return fromTypeStr(type);
50 if (!baseTypeJson.has(keyorval)) {
51 String message = "Abnormal BaseType JsonNode, it should contain 'key' or 'value' node but was not found"
52 + ObjectMapperUtil.convertToString(baseTypeJson);
53 throw new AbnormalJsonNodeException(message);
55 return fromJsonNode(baseTypeJson.get(keyorval));
60 * Get BaseType by the type value of JsonNode.
61 * @param type the type value of JsonNode
64 private static BaseType fromTypeStr(String type) {
67 return new BooleanBaseType();
69 return new IntegerBaseType();
71 return new RealBaseType();
73 return new StringBaseType();
75 return new UuidBaseType();
82 * json like "string" or json like {"type" : "string", "enum": ["set",
83 * ["access", "native-tagged"]]}" for key or value.
84 * @param type JsonNode
86 private static BaseType fromJsonNode(JsonNode type) {
87 if (type.isTextual()) {
88 return fromTypeStr(type.asText());
89 } else if (type.isObject() && type.has("type")) {
90 String typeStr = type.get("type").asText();
93 return new BooleanBaseType();
95 return getIntegerBaseType(type);
97 return getRealBaseType(type);
99 return getStringBaseType(type);
101 return getUuidBaseType(type);
110 * Get IntegerBaseType by the type value of JsonNode which contains the
112 * @param type the type value of JsonNode
113 * @return IntegerBaseType
115 private static IntegerBaseType getIntegerBaseType(JsonNode type) {
116 int min = Integer.MIN_VALUE;
117 int max = Integer.MAX_VALUE;
118 Set<Integer> enums = Sets.newHashSet();
119 JsonNode node = type.get("minInteger");
123 node = type.get("maxInteger");
127 if (type.has("enum")) {
128 JsonNode anEnum = type.get("enum").get(1);
129 for (JsonNode n : anEnum) {
130 enums.add(n.asInt());
133 return new IntegerBaseType(min, max, enums);
137 * Get RealBaseType by the type value of JsonNode which contains the
139 * @param type the type value of JsonNode
140 * @return RealBaseType
142 private static RealBaseType getRealBaseType(JsonNode type) {
143 double min = Double.MIN_VALUE;
144 double max = Double.MAX_VALUE;
145 Set<Double> enums = Sets.newHashSet();
146 JsonNode node = type.get("minReal");
148 min = node.asDouble();
150 node = type.get("maxReal");
152 max = node.asDouble();
154 if (type.has("enum")) {
155 JsonNode anEnum = type.get("enum").get(1);
156 for (JsonNode n : anEnum) {
157 enums.add(n.asDouble());
160 return new RealBaseType(min, max, enums);
164 * Get StringBaseType by the type value of JsonNode which contains the
166 * @param type the type value of JsonNode
167 * @return StringBaseType
169 private static StringBaseType getStringBaseType(JsonNode type) {
170 int minLength = Integer.MIN_VALUE;
171 int maxLength = Integer.MAX_VALUE;
172 Set<String> enums = Sets.newHashSet();
173 JsonNode node = type.get("minLength");
175 minLength = node.asInt();
177 node = type.get("maxLength");
179 maxLength = node.asInt();
181 if (type.has("enum")) {
182 JsonNode enumVal = type.get("enum");
183 if (enumVal.isArray()) {
184 JsonNode anEnum = enumVal.get(1);
185 for (JsonNode n : anEnum) {
186 enums.add(n.asText());
188 } else if (enumVal.isTextual()) {
189 enums.add(enumVal.asText());
192 return new StringBaseType(minLength, maxLength, enums);
196 * Get UuidBaseType by the type value of JsonNode which contains the
198 * @param type the type value of JsonNode
199 * @return UuidBaseType
201 private static UuidBaseType getUuidBaseType(JsonNode type) {
202 String refTable = null;
203 String refType = RefType.STRONG.refType();
204 JsonNode node = type.get("refTable");
206 refTable = node.asText();
208 node = type.get("refType");
210 refType = node.asText();
212 return new UuidBaseType(refTable, refType);