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, 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, (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, (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 node, ListSchemaNode schema) throws IOException {
113 writer.name(node.getNodeType().getLocalName());
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);
125 writer.beginObject();
126 writeChildrenOfParent(writer, node, schema);
133 private void writeLeafList(JsonWriter writer, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
134 writer.name(node.getNodeType().getLocalName());
137 CompositeNode parent = node.getParent();
138 List<SimpleNode<?>> nodeLeafLists = parent.getSimpleNodesByName(node.getNodeType());
139 for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
140 writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
146 private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
147 writer.name(node.getNodeType().getLocalName());
148 writeValueOfNodeByType(writer, node, schema.getType());
151 private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type)
153 if (!(node.getValue() instanceof String)) {
154 throw new IllegalStateException("Value in SimpleNode should be type String");
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);
173 writer.value(value != null ? value : "");
177 private void processTypeIsUnionType(JsonWriter writer, UnionTypeDefinition unionType, String value)
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));
192 private boolean isBoolean(String value) {
193 if (value.equals("true") || value.equals("false")) {
199 private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
205 private boolean isNumber(String value) {
207 Double.valueOf(value);
208 } catch (NumberFormatException e) {
214 private boolean containsType(UnionTypeDefinition unionType, Class<?>... searchedTypes) {
215 List<TypeDefinition<?>> allUnionSubtypes = resolveAllUnionSubtypesFrom(unionType);
217 for (TypeDefinition<?> unionSubtype : allUnionSubtypes) {
218 for (Class<?> searchedType : searchedTypes) {
219 if (searchedType.isInstance(unionSubtype)) {
227 private List<TypeDefinition<?>> resolveAllUnionSubtypesFrom(UnionTypeDefinition inputType) {
228 List<TypeDefinition<?>> result = new ArrayList<>();
229 for (TypeDefinition<?> subtype : inputType.getTypes()) {
230 TypeDefinition<?> resolvedSubtype = subtype;
232 resolvedSubtype = resolveBaseTypeFrom(subtype);
234 if (resolvedSubtype instanceof UnionTypeDefinition) {
235 List<TypeDefinition<?>> subtypesFromRecursion = resolveAllUnionSubtypesFrom((UnionTypeDefinition) resolvedSubtype);
236 result.addAll(subtypesFromRecursion);
238 result.add(resolvedSubtype);
245 private TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
246 return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type;
249 private static final class NumberForJsonWriter extends Number {
251 private static final long serialVersionUID = -3147729419814417666L;
252 private final String value;
254 public NumberForJsonWriter(String value) {
259 public int intValue() {
260 throw new IllegalStateException("Should not be invoked");
264 public long longValue() {
265 throw new IllegalStateException("Should not be invoked");
269 public float floatValue() {
270 throw new IllegalStateException("Should not be invoked");
274 public double doubleValue() {
275 throw new IllegalStateException("Should not be invoked");
279 public String toString() {