Merge "Bring some reliability in the eclipse and maven mixed builds"
[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.HashSet;
7 import java.util.List;
8 import java.util.Set;
9
10 import javax.activation.UnsupportedDataTypeException;
11
12 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
13 import org.opendaylight.yangtools.yang.data.api.Node;
14 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
28
29 import com.google.gson.stream.JsonWriter;
30
31 class JsonMapper {
32     
33     private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
34     private final Set<ListSchemaNode> foundLists = new HashSet<>();
35     
36     public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
37         writer.beginObject();
38         
39         if (schema instanceof ContainerSchemaNode) {
40             writeContainer(writer, (CompositeNode) data, (ContainerSchemaNode) schema);
41         } else if (schema instanceof ListSchemaNode) {
42             writeList(writer, (CompositeNode) data, (ListSchemaNode) schema);
43         } else {
44             throw new UnsupportedDataTypeException(
45                     "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
46         }
47         
48         writer.endObject();
49         
50         foundLeafLists.clear();
51         foundLists.clear();
52     }
53
54     private void writeChildrenOfParent(JsonWriter writer, CompositeNode parent, DataNodeContainer parentSchema) throws IOException {
55         checkNotNull(parent);
56         checkNotNull(parentSchema);
57         
58         for (Node<?> child : parent.getChildren()) {
59             DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes());
60             if (childSchema == null) {
61                 throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
62                         + "\" is not conform to schema");
63             }
64             
65             if (childSchema instanceof ContainerSchemaNode) {
66                 writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema);
67             } else if (childSchema instanceof ListSchemaNode) {
68                 if (!foundLists.contains(childSchema)) {
69                     foundLists.add((ListSchemaNode) childSchema);
70                     writeList(writer, (CompositeNode) child, (ListSchemaNode) childSchema);
71                 }
72             } else if (childSchema instanceof LeafListSchemaNode) {
73                 if (!foundLeafLists.contains(childSchema)) {
74                     foundLeafLists.add((LeafListSchemaNode) childSchema);
75                     writeLeafList(writer, (SimpleNode<?>) child, (LeafListSchemaNode) childSchema);
76                 }
77             } else if (childSchema instanceof LeafSchemaNode) {
78                 writeLeaf(writer, (SimpleNode<?>) child, (LeafSchemaNode) childSchema);
79             } else {
80                 throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, "
81                         + "LeafListSchemaNode, or LeafSchemaNode. Other types are not supported yet.");
82             }
83         }
84         
85         for (Node<?> child : parent.getChildren()) {
86             DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes());
87             if (childSchema instanceof LeafListSchemaNode) {
88                 foundLeafLists.remove((LeafListSchemaNode) childSchema);
89             } else if (childSchema instanceof ListSchemaNode) {
90                 foundLists.remove((ListSchemaNode) childSchema);
91             }
92         }
93     }
94     
95     private DataSchemaNode findSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
96         for (DataSchemaNode dsn : dataSchemaNode) {
97             if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
98                 return dsn;
99             }
100         }
101         return null;
102     }
103     
104     private void writeContainer(JsonWriter writer, CompositeNode node, ContainerSchemaNode schema) throws IOException {
105         writer.name(node.getNodeType().getLocalName());
106         writer.beginObject();
107         writeChildrenOfParent(writer, node, schema);
108         writer.endObject();
109     }
110     
111     private void writeList(JsonWriter writer, CompositeNode node, ListSchemaNode schema) throws IOException {
112             writer.name(node.getNodeType().getLocalName());
113             writer.beginArray();
114             
115             if (node.getParent() != null) {
116                 CompositeNode parent = node.getParent();
117                 List<CompositeNode> nodeLists = parent.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, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
133             writer.name(node.getNodeType().getLocalName());
134             writer.beginArray();
135             
136             CompositeNode parent = node.getParent();
137             List<SimpleNode<?>> nodeLeafLists = parent.getSimpleNodesByName(node.getNodeType());
138             for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
139                 writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
140             }
141             
142             writer.endArray();
143     }
144     
145     private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
146         writer.name(node.getNodeType().getLocalName());
147         writeValueOfNodeByType(writer, node, schema.getType());
148     }
149     
150     private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type) 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, IdentityrefTypeDefinition, UnionTypeDefinition
157         if (type.getBaseType() != null) {
158             writeValueOfNodeByType(writer, node, type.getBaseType());
159         } else if (type instanceof InstanceIdentifierTypeDefinition) {
160             writer.value(((InstanceIdentifierTypeDefinition) type).getPathStatement().toString());
161         } else if (type instanceof DecimalTypeDefinition 
162                 || type instanceof IntegerTypeDefinition
163                 || type instanceof UnsignedIntegerTypeDefinition) {
164             writer.value(new NumberForJsonWriter(value));
165         } else if (type instanceof BooleanTypeDefinition) {
166             writer.value(Boolean.parseBoolean(value));
167         } else if (type instanceof EmptyTypeDefinition) {
168             writer.beginArray();
169             writer.nullValue();
170             writer.endArray();
171         } else {
172             writer.value(value);
173         }
174     }
175     
176     private static final class NumberForJsonWriter extends Number {
177         
178         private static final long serialVersionUID = -3147729419814417666L;
179         private final String value;
180         
181         public NumberForJsonWriter(String value) {
182             this.value = value;
183         }
184
185         @Override
186         public int intValue() {
187             throw new IllegalStateException("Should not be invoked");
188         }
189
190         @Override
191         public long longValue() {
192             throw new IllegalStateException("Should not be invoked");
193         }
194
195         @Override
196         public float floatValue() {
197             throw new IllegalStateException("Should not be invoked");
198         }
199
200         @Override
201         public double doubleValue() {
202             throw new IllegalStateException("Should not be invoked");
203         }
204
205         @Override
206         public String toString() {
207             return value;
208         }
209         
210     }
211
212 }