Union type processing added to JsonMapper
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonMapper.java
1 package org.opendaylight.controller.sal.rest.impl;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4
5 import java.io.IOException;
6 import java.util.*;
7
8 import javax.activation.UnsupportedDataTypeException;
9
10 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
11 import org.opendaylight.yangtools.yang.data.api.Node;
12 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
13 import org.opendaylight.yangtools.yang.model.api.*;
14 import org.opendaylight.yangtools.yang.model.api.type.*;
15
16 import com.google.common.base.Preconditions;
17 import com.google.gson.stream.JsonWriter;
18
19 class JsonMapper {
20
21     private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
22     private final Set<ListSchemaNode> foundLists = new HashSet<>();
23
24     public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
25         Preconditions.checkNotNull(writer);
26         Preconditions.checkNotNull(data);
27         Preconditions.checkNotNull(schema);
28
29         writer.beginObject();
30
31         if (schema instanceof ContainerSchemaNode) {
32             writeContainer(writer, data, (ContainerSchemaNode) schema);
33         } else if (schema instanceof ListSchemaNode) {
34             writeList(writer, data, (ListSchemaNode) schema);
35         } else {
36             throw new UnsupportedDataTypeException(
37                     "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
38         }
39
40         writer.endObject();
41
42         foundLeafLists.clear();
43         foundLists.clear();
44     }
45
46     private void writeChildrenOfParent(JsonWriter writer, CompositeNode parent, DataNodeContainer parentSchema)
47             throws IOException {
48         checkNotNull(parent);
49         checkNotNull(parentSchema);
50
51         for (Node<?> child : parent.getChildren()) {
52             DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
53             if (childSchema == null) {
54                 throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
55                         + "\" is not conform to schema");
56             }
57
58             if (childSchema instanceof ContainerSchemaNode) {
59                 Preconditions.checkState(child instanceof CompositeNode,
60                         "Data representation of Container should be CompositeNode - " + child.getNodeType());
61                 writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema);
62             } else if (childSchema instanceof ListSchemaNode) {
63                 if (!foundLists.contains(childSchema)) {
64                     Preconditions.checkState(child instanceof CompositeNode,
65                             "Data representation of List should be CompositeNode - " + child.getNodeType());
66                     foundLists.add((ListSchemaNode) childSchema);
67                     writeList(writer, (CompositeNode) child, (ListSchemaNode) childSchema);
68                 }
69             } else if (childSchema instanceof LeafListSchemaNode) {
70                 if (!foundLeafLists.contains(childSchema)) {
71                     Preconditions.checkState(child instanceof SimpleNode<?>,
72                             "Data representation of LeafList should be SimpleNode - " + child.getNodeType());
73                     foundLeafLists.add((LeafListSchemaNode) childSchema);
74                     writeLeafList(writer, (SimpleNode<?>) child, (LeafListSchemaNode) childSchema);
75                 }
76             } else if (childSchema instanceof LeafSchemaNode) {
77                 Preconditions.checkState(child instanceof SimpleNode<?>,
78                         "Data representation of LeafList should be SimpleNode - " + child.getNodeType());
79                 writeLeaf(writer, (SimpleNode<?>) child, (LeafSchemaNode) childSchema);
80             } else {
81                 throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, "
82                         + "LeafListSchemaNode, or LeafSchemaNode. Other types are not supported yet.");
83             }
84         }
85
86         for (Node<?> child : parent.getChildren()) {
87             DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
88             if (childSchema instanceof LeafListSchemaNode) {
89                 foundLeafLists.remove((LeafListSchemaNode) childSchema);
90             } else if (childSchema instanceof ListSchemaNode) {
91                 foundLists.remove((ListSchemaNode) childSchema);
92             }
93         }
94     }
95
96     private DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
97         for (DataSchemaNode dsn : dataSchemaNode) {
98             if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
99                 return dsn;
100             }
101         }
102         return null;
103     }
104
105     private void writeContainer(JsonWriter writer, CompositeNode node, ContainerSchemaNode schema) throws IOException {
106         writer.name(node.getNodeType().getLocalName());
107         writer.beginObject();
108         writeChildrenOfParent(writer, node, schema);
109         writer.endObject();
110     }
111
112     private void writeList(JsonWriter writer, CompositeNode node, ListSchemaNode schema) throws IOException {
113         writer.name(node.getNodeType().getLocalName());
114         writer.beginArray();
115
116         if (node.getParent() != null) {
117             CompositeNode parent = node.getParent();
118             List<CompositeNode> nodeLists = parent.getCompositesByName(node.getNodeType());
119             for (CompositeNode nodeList : nodeLists) {
120                 writer.beginObject();
121                 writeChildrenOfParent(writer, nodeList, schema);
122                 writer.endObject();
123             }
124         } else {
125             writer.beginObject();
126             writeChildrenOfParent(writer, node, schema);
127             writer.endObject();
128         }
129
130         writer.endArray();
131     }
132
133     private void writeLeafList(JsonWriter writer, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
134         writer.name(node.getNodeType().getLocalName());
135         writer.beginArray();
136
137         CompositeNode parent = node.getParent();
138         List<SimpleNode<?>> nodeLeafLists = parent.getSimpleNodesByName(node.getNodeType());
139         for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
140             writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
141         }
142
143         writer.endArray();
144     }
145
146     private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
147         writer.name(node.getNodeType().getLocalName());
148         writeValueOfNodeByType(writer, node, schema.getType());
149     }
150
151     private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
152             throws IOException {
153         if (!(node.getValue() instanceof String)) {
154             throw new IllegalStateException("Value in SimpleNode should be type String");
155         }
156
157         String value = (String) node.getValue();
158         // TODO check Leafref, InstanceIdentifierTypeDefinition,
159         // IdentityrefTypeDefinition, UnionTypeDefinition
160         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
161         if (baseType instanceof InstanceIdentifierTypeDefinition) {
162             writer.value(((InstanceIdentifierTypeDefinition) baseType).getPathStatement().toString());
163         } else if (baseType instanceof UnionTypeDefinition) {
164             processTypeIsUnionType(writer, (UnionTypeDefinition) baseType, value);
165         } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
166                 || baseType instanceof UnsignedIntegerTypeDefinition) {
167             writer.value(new NumberForJsonWriter(value));
168         } else if (baseType instanceof BooleanTypeDefinition) {
169             writer.value(Boolean.parseBoolean(value));
170         } else if (baseType instanceof EmptyTypeDefinition) {
171             writeEmptyDataTypeToJson(writer);
172         } else {
173             writer.value(value != null ? value : "");
174         }
175     }
176
177     private void processTypeIsUnionType(JsonWriter writer, UnionTypeDefinition unionType, String value)
178             throws IOException {
179         if (value == null) {
180             writeEmptyDataTypeToJson(writer);
181         } else if ((isNumber(value))
182                 && containsType(unionType, UnsignedIntegerTypeDefinition.class, IntegerTypeDefinition.class,
183                         DecimalTypeDefinition.class)) {
184             writer.value(new NumberForJsonWriter(value));
185         } else if (isBoolean(value) && containsType(unionType, BooleanTypeDefinition.class)) {
186             writer.value(Boolean.parseBoolean(value));
187         } else {
188             writer.value(value);
189         }
190     }
191
192     private boolean isBoolean(String value) {
193         if (value.equals("true") || value.equals("false")) {
194             return true;
195         }
196         return false;
197     }
198
199     private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
200         writer.beginArray();
201         writer.nullValue();
202         writer.endArray();
203     }
204
205     private boolean isNumber(String value) {
206         try {
207             Double.valueOf(value);
208         } catch (NumberFormatException e) {
209             return false;
210         }
211         return true;
212     }
213
214     private boolean containsType(UnionTypeDefinition unionType, Class<?>... searchedTypes) {
215         List<TypeDefinition<?>> allUnionSubtypes = resolveAllUnionSubtypesFrom(unionType);
216
217         for (TypeDefinition<?> unionSubtype : allUnionSubtypes) {
218             for (Class<?> searchedType : searchedTypes) {
219                 if (searchedType.isInstance(unionSubtype)) {
220                     return true;
221                 }
222             }
223         }
224         return false;
225     }
226
227     private List<TypeDefinition<?>> resolveAllUnionSubtypesFrom(UnionTypeDefinition inputType) {
228         List<TypeDefinition<?>> result = new ArrayList<>();
229         for (TypeDefinition<?> subtype : inputType.getTypes()) {
230             TypeDefinition<?> resolvedSubtype = subtype;
231
232             resolvedSubtype = resolveBaseTypeFrom(subtype);
233
234             if (resolvedSubtype instanceof UnionTypeDefinition) {
235                 List<TypeDefinition<?>> subtypesFromRecursion = resolveAllUnionSubtypesFrom((UnionTypeDefinition) resolvedSubtype);
236                 result.addAll(subtypesFromRecursion);
237             } else {
238                 result.add(resolvedSubtype);
239             }
240         }
241
242         return result;
243     }
244
245     private TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
246         return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type;
247     }
248
249     private static final class NumberForJsonWriter extends Number {
250
251         private static final long serialVersionUID = -3147729419814417666L;
252         private final String value;
253
254         public NumberForJsonWriter(String value) {
255             this.value = value;
256         }
257
258         @Override
259         public int intValue() {
260             throw new IllegalStateException("Should not be invoked");
261         }
262
263         @Override
264         public long longValue() {
265             throw new IllegalStateException("Should not be invoked");
266         }
267
268         @Override
269         public float floatValue() {
270             throw new IllegalStateException("Should not be invoked");
271         }
272
273         @Override
274         public double doubleValue() {
275             throw new IllegalStateException("Should not be invoked");
276         }
277
278         @Override
279         public String toString() {
280             return value;
281         }
282
283     }
284
285 }