1 package org.opendaylight.controller.sal.rest.impl;
3 import static com.google.common.base.Preconditions.checkNotNull;
5 import java.io.IOException;
6 import java.util.HashSet;
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.ContainerSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
15 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
27 import com.google.gson.stream.JsonWriter;
31 private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
32 private final Set<ListSchemaNode> foundLists = new HashSet<>();
34 public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
36 writeChildrenOfParent(writer, data, schema);
38 foundLeafLists.clear();
42 private void writeChildrenOfParent(JsonWriter writer, CompositeNode parent, DataNodeContainer parentSchema) throws IOException {
44 checkNotNull(parentSchema);
46 for (Node<?> child : parent.getChildren()) {
47 DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes());
48 if (childSchema instanceof ContainerSchemaNode) {
49 writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema);
50 } else if (childSchema instanceof ListSchemaNode) {
51 if (!foundLists.contains(childSchema)) {
52 foundLists.add((ListSchemaNode) childSchema);
53 writeList(writer, (CompositeNode) child, (ListSchemaNode) childSchema);
55 } else if (childSchema instanceof LeafListSchemaNode) {
56 if (!foundLeafLists.contains(childSchema)) {
57 foundLeafLists.add((LeafListSchemaNode) childSchema);
58 writeLeafList(writer, (SimpleNode<?>) child, (LeafListSchemaNode) childSchema);
60 } else if (childSchema instanceof LeafSchemaNode) {
61 writeLeaf(writer, (SimpleNode<?>) child, (LeafSchemaNode) childSchema);
65 for (Node<?> child : parent.getChildren()) {
66 DataSchemaNode childSchema = findSchemaForNode(child, parentSchema.getChildNodes());
67 if (childSchema instanceof LeafListSchemaNode) {
68 foundLeafLists.remove((LeafListSchemaNode) childSchema);
69 } else if (childSchema instanceof ListSchemaNode) {
70 foundLists.remove((ListSchemaNode) childSchema);
75 private DataSchemaNode findSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
76 for (DataSchemaNode dsn : dataSchemaNode) {
77 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
84 private void writeContainer(JsonWriter writer, CompositeNode node, ContainerSchemaNode schema) throws IOException {
85 writer.name(node.getNodeType().getLocalName());
87 writeChildrenOfParent(writer, node, schema);
91 private void writeList(JsonWriter writer, CompositeNode node, ListSchemaNode schema) throws IOException {
92 writer.name(node.getNodeType().getLocalName());
95 if (node.getParent() != null) {
96 CompositeNode parent = node.getParent();
97 List<CompositeNode> nodeLists = parent.getCompositesByName(node.getNodeType());
98 for (CompositeNode nodeList : nodeLists) {
100 writeChildrenOfParent(writer, nodeList, schema);
104 writer.beginObject();
105 writeChildrenOfParent(writer, node, schema);
112 private void writeLeafList(JsonWriter writer, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
113 writer.name(node.getNodeType().getLocalName());
116 CompositeNode parent = node.getParent();
117 List<SimpleNode<?>> nodeLeafLists = parent.getSimpleNodesByName(node.getNodeType());
118 for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
119 writeValueOfNodeByType(writer, nodeLeafList, schema.getType());
125 private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
126 writer.name(node.getNodeType().getLocalName());
127 writeValueOfNodeByType(writer, node, schema.getType());
130 private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type) throws IOException {
131 if (!(node.getValue() instanceof String)) {
132 throw new IllegalStateException("Value in SimpleNode should be type String");
135 String value = (String) node.getValue();
136 // TODO check Leafref, InstanceIdentifierTypeDefinition, IdentityrefTypeDefinition, UnionTypeDefinition
137 if (type.getBaseType() != null) {
138 writeValueOfNodeByType(writer, node, type.getBaseType());
139 } else if (type instanceof InstanceIdentifierTypeDefinition) {
140 writer.value(((InstanceIdentifierTypeDefinition) type).getPathStatement().toString());
141 } else if (type instanceof DecimalTypeDefinition
142 || type instanceof IntegerTypeDefinition
143 || type instanceof UnsignedIntegerTypeDefinition) {
144 writer.value(new NumberForJsonWriter(value));
145 } else if (type instanceof BooleanTypeDefinition) {
146 writer.value(Boolean.parseBoolean(value));
147 } else if (type instanceof EmptyTypeDefinition) {
148 writer.value("[null]");
154 private static final class NumberForJsonWriter extends Number {
156 private static final long serialVersionUID = -3147729419814417666L;
157 private final String value;
159 public NumberForJsonWriter(String value) {
164 public int intValue() {
165 throw new IllegalStateException("Should not be invoked");
169 public long longValue() {
170 throw new IllegalStateException("Should not be invoked");
174 public float floatValue() {
175 throw new IllegalStateException("Should not be invoked");
179 public double doubleValue() {
180 throw new IllegalStateException("Should not be invoked");
184 public String toString() {