blob: 86dbb2e6dcfbef78429d9fc26c821cdc667bcc47 [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package org.onosproject.ovsdb.rfc.schema.type;
17
18import java.util.Set;
19
Sho SHIMIZU8700d422015-08-26 18:13:44 -070020import org.onosproject.ovsdb.rfc.exception.AbnormalJsonNodeException;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070021import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType.RefType;
22import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
23
24import com.fasterxml.jackson.databind.JsonNode;
25import com.google.common.collect.Sets;
26
27/**
28 * BaseType Factory class.
29 */
30public final class BaseTypeFactory {
31
32 /**
33 * Constructs a BaseTypeFactory object. This class should not be
34 * instantiated.
35 */
36 private BaseTypeFactory() {
37 }
38
39 /**
40 * Create a BaseType from the JsonNode.
41 * @param baseTypeJson the BaseType JsonNode
42 * @param keyorval the key node or value node
43 * @return BaseType
44 */
45 public static BaseType getBaseTypeFromJson(JsonNode baseTypeJson, String keyorval) {
46 if (baseTypeJson.isValueNode()) {
47 String type = baseTypeJson.asText().trim();
48 return fromTypeStr(type);
49 } else {
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);
54 }
55 return fromJsonNode(baseTypeJson.get(keyorval));
56 }
57 }
58
59 /**
60 * Get BaseType by the type value of JsonNode.
61 * @param type the type value of JsonNode
62 * @return BaseType
63 */
64 private static BaseType fromTypeStr(String type) {
65 switch (type) {
66 case "boolean":
67 return new BooleanBaseType();
68 case "integer":
69 return new IntegerBaseType();
70 case "real":
71 return new RealBaseType();
72 case "string":
73 return new StringBaseType();
74 case "uuid":
75 return new UuidBaseType();
76 default:
77 return null;
78 }
79 }
80
81 /**
82 * json like "string" or json like {"type" : "string", "enum": ["set",
83 * ["access", "native-tagged"]]}" for key or value.
84 * @param type JsonNode
85 */
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();
91 switch (typeStr) {
92 case "boolean":
93 return new BooleanBaseType();
94 case "integer":
95 return getIntegerBaseType(type);
96 case "real":
97 return getRealBaseType(type);
98 case "string":
99 return getStringBaseType(type);
100 case "uuid":
101 return getUuidBaseType(type);
102 default:
103 return null;
104 }
105 }
106 return null;
107 }
108
109 /**
110 * Get IntegerBaseType by the type value of JsonNode which contains the
111 * constraints.
112 * @param type the type value of JsonNode
113 * @return IntegerBaseType
114 */
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");
120 if (node != null) {
121 min = node.asInt();
122 }
123 node = type.get("maxInteger");
124 if (node != null) {
125 max = node.asInt();
126 }
127 if (type.has("enum")) {
128 JsonNode anEnum = type.get("enum").get(1);
129 for (JsonNode n : anEnum) {
130 enums.add(n.asInt());
131 }
132 }
133 return new IntegerBaseType(min, max, enums);
134 }
135
136 /**
137 * Get RealBaseType by the type value of JsonNode which contains the
138 * constraints.
139 * @param type the type value of JsonNode
140 * @return RealBaseType
141 */
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");
147 if (node != null) {
148 min = node.asDouble();
149 }
150 node = type.get("maxReal");
151 if (node != null) {
152 max = node.asDouble();
153 }
154 if (type.has("enum")) {
155 JsonNode anEnum = type.get("enum").get(1);
156 for (JsonNode n : anEnum) {
157 enums.add(n.asDouble());
158 }
159 }
160 return new RealBaseType(min, max, enums);
161 }
162
163 /**
164 * Get StringBaseType by the type value of JsonNode which contains the
165 * constraints.
166 * @param type the type value of JsonNode
167 * @return StringBaseType
168 */
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");
174 if (node != null) {
175 minLength = node.asInt();
176 }
177 node = type.get("maxLength");
178 if (node != null) {
179 maxLength = node.asInt();
180 }
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());
187 }
188 } else if (enumVal.isTextual()) {
189 enums.add(enumVal.asText());
190 }
191 }
192 return new StringBaseType(minLength, maxLength, enums);
193 }
194
195 /**
196 * Get UuidBaseType by the type value of JsonNode which contains the
197 * constraints.
198 * @param type the type value of JsonNode
199 * @return UuidBaseType
200 */
201 private static UuidBaseType getUuidBaseType(JsonNode type) {
202 String refTable = null;
203 String refType = RefType.STRONG.refType();
204 JsonNode node = type.get("refTable");
205 if (node != null) {
206 refTable = node.asText();
207 }
208 node = type.get("refType");
209 if (node != null) {
210 refType = node.asText();
211 }
212 return new UuidBaseType(refTable, refType);
213 }
214}