1 package org.opendaylight.controller.sal.rest.impl;
3 import static com.google.common.base.Preconditions.checkNotNull;
5 import java.io.IOException;
8 import javax.activation.UnsupportedDataTypeException;
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.*;
16 import com.google.common.base.Preconditions;
17 import com.google.gson.stream.JsonWriter;
21 private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
22 private final Set<ListSchemaNode> foundLists = new HashSet<>();
24 public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
25 Preconditions.checkNotNull(writer);
26 Preconditions.checkNotNull(data);
27 Preconditions.checkNotNull(schema);
31 if (schema instanceof ContainerSchemaNode) {
32 writeContainer(writer, data, (ContainerSchemaNode) schema);
33 } else if (schema instanceof ListSchemaNode) {
34 writeList(writer, null, data, (ListSchemaNode) schema);
36 throw new UnsupportedDataTypeException(
37 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
42 foundLeafLists.clear();
46 private void writeChildrenOfParent(JsonWriter writer, CompositeNode parent, DataNodeContainer parentSchema)
49 checkNotNull(parentSchema);
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");
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);
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);
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);
81 throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, "
82 + "LeafListSchemaNode, or LeafSchemaNode. Other types are not supported yet.");
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);
96 private DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
97 for (DataSchemaNode dsn : dataSchemaNode) {
98 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
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);
112 private void writeList(JsonWriter writer, CompositeNode nodeParent, CompositeNode node, ListSchemaNode schema) throws IOException {
113 writer.name(node.getNodeType().getLocalName());
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);
124 writer.beginObject();
125 writeChildrenOfParent(writer, node, schema);
132 private void writeLeafList(JsonWriter writer, CompositeNode nodeParent, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
133 writer.name(node.getNodeType().getLocalName());
136 List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
137 for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
138 writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
144 private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
145 writer.name(node.getNodeType().getLocalName());
146 writeValueOfNodeByType(writer, node, schema.getType());
149 private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
151 if (!(node.getValue() instanceof String)) {
152 throw new IllegalStateException("Value in SimpleNode should be type String");
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);
171 writer.value(value != null ? value : "");
175 private void processTypeIsUnionType(JsonWriter writer, UnionTypeDefinition unionType, String value)
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));
190 private boolean isBoolean(String value) {
191 if (value.equals("true") || value.equals("false")) {
197 private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
203 private boolean isNumber(String value) {
205 Double.valueOf(value);
206 } catch (NumberFormatException e) {
212 private boolean containsType(UnionTypeDefinition unionType, Class<?>... searchedTypes) {
213 List<TypeDefinition<?>> allUnionSubtypes = resolveAllUnionSubtypesFrom(unionType);
215 for (TypeDefinition<?> unionSubtype : allUnionSubtypes) {
216 for (Class<?> searchedType : searchedTypes) {
217 if (searchedType.isInstance(unionSubtype)) {
225 private List<TypeDefinition<?>> resolveAllUnionSubtypesFrom(UnionTypeDefinition inputType) {
226 List<TypeDefinition<?>> result = new ArrayList<>();
227 for (TypeDefinition<?> subtype : inputType.getTypes()) {
228 TypeDefinition<?> resolvedSubtype = subtype;
230 resolvedSubtype = resolveBaseTypeFrom(subtype);
232 if (resolvedSubtype instanceof UnionTypeDefinition) {
233 List<TypeDefinition<?>> subtypesFromRecursion = resolveAllUnionSubtypesFrom((UnionTypeDefinition) resolvedSubtype);
234 result.addAll(subtypesFromRecursion);
236 result.add(resolvedSubtype);
243 private TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
244 return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type;
247 private static final class NumberForJsonWriter extends Number {
249 private static final long serialVersionUID = -3147729419814417666L;
250 private final String value;
252 public NumberForJsonWriter(String value) {
257 public int intValue() {
258 throw new IllegalStateException("Should not be invoked");
262 public long longValue() {
263 throw new IllegalStateException("Should not be invoked");
267 public float floatValue() {
268 throw new IllegalStateException("Should not be invoked");
272 public double doubleValue() {
273 throw new IllegalStateException("Should not be invoked");
277 public String toString() {