c900b38cad6b95ba5ce18cad8c2f97dc86e686c5
[ovsdb.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / lib / schema / BaseType.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
3  *
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
7  *
8  * Authors : Ashwin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.schema;
11
12 import com.fasterxml.jackson.databind.JsonNode;
13 import com.google.common.collect.Sets;
14
15 import java.util.Set;
16
17 public abstract class BaseType<E extends BaseType<E>> {
18
19     private static BaseType[] types = new BaseType[]{
20             new StringBaseType(),
21             new IntegerBaseType(),
22             new RealBaseType(),
23             new BooleanBaseType(),
24             new UuidBaseType(),
25     };
26
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) {
34                     break;
35                 }
36             }
37         } else {
38             if (!json.has(keyorval)) {
39                 throw new RuntimeException("Not a type");
40             }
41
42             for (BaseType baseTypeFactory : types) {
43                 baseType = baseTypeFactory.fromJsonNode(json.get(keyorval), keyorval);
44                 if (baseType != null) {
45                     break;
46                 }
47             }
48         }
49         return baseType;
50     }
51
52     protected abstract E fromString(String type);
53
54     protected abstract void getConstraints(E baseType, JsonNode type);
55
56     protected E fromJsonNode(JsonNode type, String keyorval) {
57
58         E baseType = null;
59
60         //json like  "string"
61         if (type.isTextual()) {
62             baseType = fromString(type.asText());
63             if (baseType != null) {
64                 return baseType;
65             }
66         }
67
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);
73             }
74         }
75
76         return baseType;
77     }
78
79     public static class IntegerBaseType extends BaseType<IntegerBaseType> {
80         long min = Long.MIN_VALUE;
81         long max = Long.MAX_VALUE;
82         Set<Integer> enums;
83
84         public IntegerBaseType fromString(String typeString) {
85             return "integer".equals(typeString) ? new IntegerBaseType() : null;
86         }
87
88         @Override
89         protected void getConstraints(IntegerBaseType baseType, JsonNode type) {
90
91             JsonNode node = null;
92
93             if ((node = type.get("maxInteger")) != null) {
94                 baseType.setMax(node.asLong());
95             }
96
97             if ((node = type.get("minInteger")) != null) {
98                 baseType.setMin(node.asLong());
99             }
100
101             populateEnum(type);
102         }
103
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) {
109                     s.add(n.asLong());
110                 }
111             }
112         }
113
114
115         public long getMin() {
116             return min;
117         }
118
119         public void setMin(long min) {
120             this.min = min;
121         }
122
123         public long getMax() {
124             return max;
125         }
126
127         public void setMax(long max) {
128             this.max = max;
129         }
130
131         public Set<Integer> getEnums() {
132             return enums;
133         }
134
135         public void setEnums(Set<Integer> enums) {
136             this.enums = enums;
137         }
138
139         @Override
140         public String toString() {
141             return "IntegerBaseType";
142         }
143     }
144
145     public static class RealBaseType extends BaseType<RealBaseType> {
146         double min = Double.MIN_VALUE;
147         double max = Double.MAX_VALUE;
148         Set<Double> enums;
149
150         public RealBaseType fromString(String typeString) {
151             return "real".equals(typeString) ? new RealBaseType() : null;
152         }
153
154         @Override
155         protected void getConstraints(RealBaseType baseType, JsonNode type) {
156
157             JsonNode node = null;
158
159             if ((node = type.get("maxReal")) != null) {
160                 baseType.setMax(node.asLong());
161             }
162
163             if ((node = type.get("minReal")) != null) {
164                 baseType.setMin(node.asLong());
165             }
166
167             populateEnum(type);
168         }
169
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) {
175                     s.add(n.asDouble());
176                 }
177             }
178         }
179
180         public double getMin() {
181             return min;
182         }
183
184         public void setMin(double min) {
185             this.min = min;
186         }
187
188         public double getMax() {
189             return max;
190         }
191
192         public void setMax(double max) {
193             this.max = max;
194         }
195
196         public Set<Double> getEnums() {
197             return enums;
198         }
199
200         public void setEnums(Set<Double> enums) {
201             this.enums = enums;
202         }
203
204         @Override
205         public String toString() {
206             return "RealBaseType";
207         }
208     }
209
210
211     public static class BooleanBaseType extends BaseType {
212
213         public BooleanBaseType fromString(String typeString) {
214             return "boolean".equals(typeString) ? new BooleanBaseType() : null;
215         }
216
217         @Override
218         protected void getConstraints(BaseType baseType, JsonNode node) {
219             //no op
220         }
221
222         @Override
223         public String toString() {
224             return "BooleanBaseType";
225         }
226     }
227
228     public static class StringBaseType extends BaseType<StringBaseType> {
229         int minLength = Integer.MIN_VALUE;
230         int maxLength = Integer.MAX_VALUE;
231         Set<String> enums;
232
233         public StringBaseType fromString(String typeString) {
234             return "string".equals(typeString) ? new StringBaseType() : null;
235         }
236
237         @Override
238         protected void getConstraints(StringBaseType baseType, JsonNode type) {
239
240             JsonNode node = null;
241
242             if ((node = type.get("maxLength")) != null) {
243                 baseType.setMaxLength(node.asInt());
244             }
245
246             if ((node = type.get("minLength")) != null) {
247                 baseType.setMinLength(node.asInt());
248             }
249
250             populateEnum(baseType, type);
251         }
252
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) {
258                     s.add(n.asText());
259                 }
260                 baseType.setEnums(s);
261             }
262         }
263
264         public int getMinLength() {
265             return minLength;
266         }
267
268         public void setMinLength(int minLength) {
269             this.minLength = minLength;
270         }
271
272         public int getMaxLength() {
273             return maxLength;
274         }
275
276         public void setMaxLength(int maxLength) {
277             this.maxLength = maxLength;
278         }
279
280         public Set<String> getEnums() {
281             return enums;
282         }
283
284         public void setEnums(Set<String> enums) {
285             this.enums = enums;
286         }
287
288         public String toString() {
289             return "StringBaseType";
290         }
291
292     }
293
294
295     public static class UuidBaseType extends BaseType<UuidBaseType> {
296         public static enum RefType {strong, weak}
297
298         String refTable;
299         RefType refType;
300
301
302         public UuidBaseType fromString(String typeString) {
303             return "uuid".equals(typeString) ? new UuidBaseType() : null;
304         }
305
306         @Override
307         protected void getConstraints(UuidBaseType baseType, JsonNode node) {
308
309             JsonNode refTable = node.get("refTable");
310             baseType.setRefTable(refTable != null ? refTable.asText() : null);
311
312             JsonNode refTypeJson = node.get("refType");
313             baseType.setRefType(refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong);
314
315         }
316
317         public String getRefTable() {
318             return refTable;
319         }
320
321         public void setRefTable(String refTable) {
322             this.refTable = refTable;
323         }
324
325         public RefType getRefType() {
326             return refType;
327         }
328
329         public void setRefType(RefType refType) {
330             this.refType = refType;
331         }
332
333         public String toString() {
334             return "UuidBaseType";
335         }
336     }
337 }