Merge "Fix bug 153: change the key container-config to ContainerConfig in the contain...
[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, null, 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, parent, (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, parent, (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 nodeParent, CompositeNode node, ListSchemaNode schema) throws IOException {
113         writer.name(node.getNodeType().getLocalName());
114         writer.beginArray();
115
116         if (nodeParent != null) {
117             List<CompositeNode> nodeLists = nodeParent.getCompositesByName(node.getNodeType());
118             for (CompositeNode nodeList : nodeLists) {
119                 writer.beginObject();
120                 writeChildrenOfParent(writer, nodeList, schema);
121                 writer.endObject();
122             }
123         } else {
124             writer.beginObject();
125             writeChildrenOfParent(writer, node, schema);
126             writer.endObject();
127         }
128
129         writer.endArray();
130     }
131
132     private void writeLeafList(JsonWriter writer, CompositeNode nodeParent, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
133         writer.name(node.getNodeType().getLocalName());
134         writer.beginArray();
135
136         List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
137         for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
138             writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
139         }
140
141         writer.endArray();
142     }
143
144     private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
145         writer.name(node.getNodeType().getLocalName());
146         writeValueOfNodeByType(writer, node, schema.getType());
147     }
148
149     private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
150             throws IOException {
151         if (!(node.getValue() instanceof String)) {
152             throw new IllegalStateException("Value in SimpleNode should be type String");
153         }
154
155         String value = (String) node.getValue();
156         // TODO check Leafref, InstanceIdentifierTypeDefinition,
157         // IdentityrefTypeDefinition, UnionTypeDefinition
158         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
159         if (baseType instanceof InstanceIdentifierTypeDefinition) {
160             writer.value(((InstanceIdentifierTypeDefinition) baseType).getPathStatement().toString());
161         } else if (baseType instanceof UnionTypeDefinition) {
162             processTypeIsUnionType(writer, (UnionTypeDefinition) baseType, value);
163         } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
164                 || baseType instanceof UnsignedIntegerTypeDefinition) {
165             writer.value(new NumberForJsonWriter(value));
166         } else if (baseType instanceof BooleanTypeDefinition) {
167             writer.value(Boolean.parseBoolean(value));
168         } else if (baseType instanceof EmptyTypeDefinition) {
169             writeEmptyDataTypeToJson(writer);
170         } else {
171             writer.value(value != null ? value : "");
172         }
173     }
174
175     private void processTypeIsUnionType(JsonWriter writer, UnionTypeDefinition unionType, String value)
176             throws IOException {
177         if (value == null) {
178             writeEmptyDataTypeToJson(writer);
179         } else if ((isNumber(value))
180                 && containsType(unionType, UnsignedIntegerTypeDefinition.class, IntegerTypeDefinition.class,
181                         DecimalTypeDefinition.class)) {
182             writer.value(new NumberForJsonWriter(value));
183         } else if (isBoolean(value) && containsType(unionType, BooleanTypeDefinition.class)) {
184             writer.value(Boolean.parseBoolean(value));
185         } else {
186             writer.value(value);
187         }
188     }
189
190     private boolean isBoolean(String value) {
191         if (value.equals("true") || value.equals("false")) {
192             return true;
193         }
194         return false;
195     }
196
197     private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
198         writer.beginArray();
199         writer.nullValue();
200         writer.endArray();
201     }
202
203     private boolean isNumber(String value) {
204         try {
205             Double.valueOf(value);
206         } catch (NumberFormatException e) {
207             return false;
208         }
209         return true;
210     }
211
212     private boolean containsType(UnionTypeDefinition unionType, Class<?>... searchedTypes) {
213         List<TypeDefinition<?>> allUnionSubtypes = resolveAllUnionSubtypesFrom(unionType);
214
215         for (TypeDefinition<?> unionSubtype : allUnionSubtypes) {
216             for (Class<?> searchedType : searchedTypes) {
217                 if (searchedType.isInstance(unionSubtype)) {
218                     return true;
219                 }
220             }
221         }
222         return false;
223     }
224
225     private List<TypeDefinition<?>> resolveAllUnionSubtypesFrom(UnionTypeDefinition inputType) {
226         List<TypeDefinition<?>> result = new ArrayList<>();
227         for (TypeDefinition<?> subtype : inputType.getTypes()) {
228             TypeDefinition<?> resolvedSubtype = subtype;
229
230             resolvedSubtype = resolveBaseTypeFrom(subtype);
231
232             if (resolvedSubtype instanceof UnionTypeDefinition) {
233                 List<TypeDefinition<?>> subtypesFromRecursion = resolveAllUnionSubtypesFrom((UnionTypeDefinition) resolvedSubtype);
234                 result.addAll(subtypesFromRecursion);
235             } else {
236                 result.add(resolvedSubtype);
237             }
238         }
239
240         return result;
241     }
242
243     private TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
244         return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type;
245     }
246
247     private static final class NumberForJsonWriter extends Number {
248
249         private static final long serialVersionUID = -3147729419814417666L;
250         private final String value;
251
252         public NumberForJsonWriter(String value) {
253             this.value = value;
254         }
255
256         @Override
257         public int intValue() {
258             throw new IllegalStateException("Should not be invoked");
259         }
260
261         @Override
262         public long longValue() {
263             throw new IllegalStateException("Should not be invoked");
264         }
265
266         @Override
267         public float floatValue() {
268             throw new IllegalStateException("Should not be invoked");
269         }
270
271         @Override
272         public double doubleValue() {
273             throw new IllegalStateException("Should not be invoked");
274         }
275
276         @Override
277         public String toString() {
278             return value;
279         }
280
281     }
282
283 }