More test for improving of code coverage + test refactoring
[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.net.URI;
7 import java.util.*;
8
9 import javax.activation.UnsupportedDataTypeException;
10
11 import org.opendaylight.controller.sal.restconf.impl.*;
12 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.*;
15 import org.opendaylight.yangtools.yang.model.api.*;
16 import org.opendaylight.yangtools.yang.model.api.type.*;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.base.Preconditions;
21 import com.google.gson.stream.JsonWriter;
22
23 class JsonMapper {
24
25     private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
26     private final Set<ListSchemaNode> foundLists = new HashSet<>();
27     private final Logger logger = LoggerFactory.getLogger(JsonMapper.class);
28
29     public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
30         Preconditions.checkNotNull(writer);
31         Preconditions.checkNotNull(data);
32         Preconditions.checkNotNull(schema);
33
34         writer.beginObject();
35
36         if (schema instanceof ContainerSchemaNode) {
37             writeContainer(writer, data, (ContainerSchemaNode) schema);
38         } else if (schema instanceof ListSchemaNode) {
39             writeList(writer, null, data, (ListSchemaNode) schema);
40         } else {
41             throw new UnsupportedDataTypeException(
42                     "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
43         }
44
45         writer.endObject();
46
47         foundLeafLists.clear();
48         foundLists.clear();
49     }
50
51     private void writeChildrenOfParent(JsonWriter writer, CompositeNode parent, DataNodeContainer parentSchema)
52             throws IOException {
53         checkNotNull(parent);
54         checkNotNull(parentSchema);
55
56         for (Node<?> child : parent.getChildren()) {
57             DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
58
59             if (childSchema == null) {
60                 throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
61                         + "\" is not conform to schema");
62             }
63
64             if (childSchema instanceof ContainerSchemaNode) {
65                 Preconditions.checkState(child instanceof CompositeNode,
66                         "Data representation of Container should be CompositeNode - " + child.getNodeType());
67                 writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema);
68             } else if (childSchema instanceof ListSchemaNode) {
69                 if (!foundLists.contains(childSchema)) {
70                     Preconditions.checkState(child instanceof CompositeNode,
71                             "Data representation of List should be CompositeNode - " + child.getNodeType());
72                     foundLists.add((ListSchemaNode) childSchema);
73                     writeList(writer, parent, (CompositeNode) child, (ListSchemaNode) childSchema);
74                 }
75             } else if (childSchema instanceof LeafListSchemaNode) {
76                 if (!foundLeafLists.contains(childSchema)) {
77                     Preconditions.checkState(child instanceof SimpleNode<?>,
78                             "Data representation of LeafList should be SimpleNode - " + child.getNodeType());
79                     foundLeafLists.add((LeafListSchemaNode) childSchema);
80                     writeLeafList(writer, parent, (SimpleNode<?>) child, (LeafListSchemaNode) childSchema);
81                 }
82             } else if (childSchema instanceof LeafSchemaNode) {
83                 Preconditions.checkState(child instanceof SimpleNode<?>,
84                         "Data representation of LeafList should be SimpleNode - " + child.getNodeType());
85                 writeLeaf(writer, (SimpleNode<?>) child, (LeafSchemaNode) childSchema);
86             } else {
87                 throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, "
88                         + "LeafListSchemaNode, or LeafSchemaNode. Other types are not supported yet.");
89             }
90         }
91
92         for (Node<?> child : parent.getChildren()) {
93             DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
94             if (childSchema instanceof LeafListSchemaNode) {
95                 foundLeafLists.remove((LeafListSchemaNode) childSchema);
96             } else if (childSchema instanceof ListSchemaNode) {
97                 foundLists.remove((ListSchemaNode) childSchema);
98             }
99         }
100     }
101
102     private DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
103         for (DataSchemaNode dsn : dataSchemaNode) {
104             if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
105                 return dsn;
106             } else if (dsn instanceof ChoiceNode) {
107                 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
108                     DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
109                     if (foundDsn != null) {
110                         return foundDsn;
111                     }
112                 }
113             }
114         }
115         return null;
116     }
117
118     private void writeContainer(JsonWriter writer, CompositeNode node, ContainerSchemaNode schema) throws IOException {
119         writeName(node, schema, writer);
120         writer.beginObject();
121         writeChildrenOfParent(writer, node, schema);
122         writer.endObject();
123     }
124
125     private void writeList(JsonWriter writer, CompositeNode nodeParent, CompositeNode node, ListSchemaNode schema)
126             throws IOException {
127         writeName(node, schema, writer);
128         writer.beginArray();
129
130         if (nodeParent != null) {
131             List<CompositeNode> nodeLists = nodeParent.getCompositesByName(node.getNodeType());
132             for (CompositeNode nodeList : nodeLists) {
133                 writer.beginObject();
134                 writeChildrenOfParent(writer, nodeList, schema);
135                 writer.endObject();
136             }
137         } else {
138             writer.beginObject();
139             writeChildrenOfParent(writer, node, schema);
140             writer.endObject();
141         }
142
143         writer.endArray();
144     }
145
146     private void writeLeafList(JsonWriter writer, CompositeNode nodeParent, SimpleNode<?> node,
147             LeafListSchemaNode schema) throws IOException {
148         writeName(node, schema, writer);
149         writer.beginArray();
150
151         List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
152         for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
153             writeValueOfNodeByType(writer, nodeLeafList, schema.getType(), schema);
154         }
155         writer.endArray();
156     }
157
158     private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
159         writeName(node, schema, writer);
160         writeValueOfNodeByType(writer, node, schema.getType(), schema);
161     }
162
163     private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type,
164             DataSchemaNode schema) throws IOException {
165
166         TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(type);
167
168         // TODO check InstanceIdentifierTypeDefinition
169         if (baseType instanceof IdentityrefTypeDefinition) {
170             if (node.getValue() instanceof QName) {
171                 IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType).serialize(node.getValue());
172                 IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0);
173                 String moduleName = ControllerContext.getInstance().findModuleByNamespace(
174                         URI.create(valueFromDTO.getNamespace()));
175                 writer.value(moduleName + ":" + valueFromDTO.getValue());
176             } else {
177                 logger.debug("Value of " + baseType.getQName().getNamespace() + ":"
178                         + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is "
179                         + node.getValue().getClass());
180                 writer.value(String.valueOf(node.getValue()));
181             }
182         } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
183                 || baseType instanceof UnsignedIntegerTypeDefinition) {
184             writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType).serialize(node.getValue())));
185         } else if (baseType instanceof BooleanTypeDefinition) {
186             writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType).serialize(node.getValue())));
187         } else if (baseType instanceof EmptyTypeDefinition) {
188             writeEmptyDataTypeToJson(writer);
189         } else {
190             String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue()));
191             if (value == null) {
192                 value = String.valueOf(node.getValue());
193             }
194             writer.value(value.equals("null") ? "" : value);
195         }
196     }
197
198     private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
199         writer.beginArray();
200         writer.nullValue();
201         writer.endArray();
202     }
203
204     private void writeName(Node<?> node, DataSchemaNode schema, JsonWriter writer) throws IOException {
205         String nameForOutput = node.getNodeType().getLocalName();
206         if (schema.isAugmenting()) {
207             ControllerContext contContext = ControllerContext.getInstance();
208             CharSequence moduleName;
209             moduleName = contContext.toRestconfIdentifier(schema.getQName());
210             if (moduleName != null) {
211                 nameForOutput = moduleName.toString();
212             }
213         }
214         writer.name(nameForOutput);
215     }
216
217     private static final class NumberForJsonWriter extends Number {
218
219         private static final long serialVersionUID = -3147729419814417666L;
220         private final String value;
221
222         public NumberForJsonWriter(String value) {
223             this.value = value;
224         }
225
226         @Override
227         public int intValue() {
228             throw new IllegalStateException("Should not be invoked");
229         }
230
231         @Override
232         public long longValue() {
233             throw new IllegalStateException("Should not be invoked");
234         }
235
236         @Override
237         public float floatValue() {
238             throw new IllegalStateException("Should not be invoked");
239         }
240
241         @Override
242         public double doubleValue() {
243             throw new IllegalStateException("Should not be invoked");
244         }
245
246         @Override
247         public String toString() {
248             return value;
249         }
250
251     }
252
253 }