1 package org.opendaylight.controller.sal.rest.impl;
3 import static com.google.common.base.Preconditions.checkNotNull;
5 import java.io.IOException;
7 import java.util.HashSet;
11 import javax.activation.UnsupportedDataTypeException;
13 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
14 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO;
15 import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue;
16 import org.opendaylight.controller.sal.restconf.impl.RestCodec;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.data.api.Node;
20 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.base.Preconditions;
40 import com.google.gson.stream.JsonWriter;
44 private final Set<LeafListSchemaNode> foundLeafLists = new HashSet<>();
45 private final Set<ListSchemaNode> foundLists = new HashSet<>();
46 private final Logger logger = LoggerFactory.getLogger(JsonMapper.class);
48 public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException {
49 Preconditions.checkNotNull(writer);
50 Preconditions.checkNotNull(data);
51 Preconditions.checkNotNull(schema);
55 if (schema instanceof ContainerSchemaNode) {
56 writeContainer(writer, data, (ContainerSchemaNode) schema);
57 } else if (schema instanceof ListSchemaNode) {
58 writeList(writer, null, data, (ListSchemaNode) schema);
60 throw new UnsupportedDataTypeException(
61 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
66 foundLeafLists.clear();
70 private void writeChildrenOfParent(JsonWriter writer, CompositeNode parent, DataNodeContainer parentSchema)
73 checkNotNull(parentSchema);
75 for (Node<?> child : parent.getChildren()) {
76 DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
78 if (childSchema == null) {
79 throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
80 + "\" is not conform to schema");
83 if (childSchema instanceof ContainerSchemaNode) {
84 Preconditions.checkState(child instanceof CompositeNode,
85 "Data representation of Container should be CompositeNode - " + child.getNodeType());
86 writeContainer(writer, (CompositeNode) child, (ContainerSchemaNode) childSchema);
87 } else if (childSchema instanceof ListSchemaNode) {
88 if (!foundLists.contains(childSchema)) {
89 Preconditions.checkState(child instanceof CompositeNode,
90 "Data representation of List should be CompositeNode - " + child.getNodeType());
91 foundLists.add((ListSchemaNode) childSchema);
92 writeList(writer, parent, (CompositeNode) child, (ListSchemaNode) childSchema);
94 } else if (childSchema instanceof LeafListSchemaNode) {
95 if (!foundLeafLists.contains(childSchema)) {
96 Preconditions.checkState(child instanceof SimpleNode<?>,
97 "Data representation of LeafList should be SimpleNode - " + child.getNodeType());
98 foundLeafLists.add((LeafListSchemaNode) childSchema);
99 writeLeafList(writer, parent, (SimpleNode<?>) child, (LeafListSchemaNode) childSchema);
101 } else if (childSchema instanceof LeafSchemaNode) {
102 Preconditions.checkState(child instanceof SimpleNode<?>,
103 "Data representation of LeafList should be SimpleNode - " + child.getNodeType());
104 writeLeaf(writer, (SimpleNode<?>) child, (LeafSchemaNode) childSchema);
106 throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode, ListSchemaNode, "
107 + "LeafListSchemaNode, or LeafSchemaNode. Other types are not supported yet.");
111 for (Node<?> child : parent.getChildren()) {
112 DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
113 if (childSchema instanceof LeafListSchemaNode) {
114 foundLeafLists.remove((LeafListSchemaNode) childSchema);
115 } else if (childSchema instanceof ListSchemaNode) {
116 foundLists.remove((ListSchemaNode) childSchema);
121 private DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
122 for (DataSchemaNode dsn : dataSchemaNode) {
123 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
125 } else if (dsn instanceof ChoiceNode) {
126 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
127 DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
128 if (foundDsn != null) {
137 private void writeContainer(JsonWriter writer, CompositeNode node, ContainerSchemaNode schema) throws IOException {
138 writeName(node, schema, writer);
139 writer.beginObject();
140 writeChildrenOfParent(writer, node, schema);
144 private void writeList(JsonWriter writer, CompositeNode nodeParent, CompositeNode node, ListSchemaNode schema)
146 writeName(node, schema, writer);
149 if (nodeParent != null) {
150 List<CompositeNode> nodeLists = nodeParent.getCompositesByName(node.getNodeType());
151 for (CompositeNode nodeList : nodeLists) {
152 writer.beginObject();
153 writeChildrenOfParent(writer, nodeList, schema);
157 writer.beginObject();
158 writeChildrenOfParent(writer, node, schema);
165 private void writeLeafList(JsonWriter writer, CompositeNode nodeParent, SimpleNode<?> node,
166 LeafListSchemaNode schema) throws IOException {
167 writeName(node, schema, writer);
170 List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
171 for (SimpleNode<?> nodeLeafList : nodeLeafLists) {
172 writeValueOfNodeByType(writer, nodeLeafList, schema.getType(), schema);
177 private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
178 writeName(node, schema, writer);
179 writeValueOfNodeByType(writer, node, schema.getType(), schema);
182 private void writeValueOfNodeByType(JsonWriter writer, SimpleNode<?> node, TypeDefinition<?> type,
183 DataSchemaNode schema) throws IOException {
185 TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(type);
187 if (node.getValue() == null && !(baseType instanceof EmptyTypeDefinition)) {
188 logger.debug("While generationg JSON output null value was found for type "
189 + baseType.getClass().getSimpleName() + ".");
192 // TODO check InstanceIdentifierTypeDefinition
193 if (baseType instanceof IdentityrefTypeDefinition) {
194 if (node.getValue() instanceof QName) {
195 IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType).serialize(node.getValue());
196 IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0);
197 String moduleName = ControllerContext.getInstance().findModuleNameByNamespace(
198 URI.create(valueFromDTO.getNamespace()));
199 writer.value(moduleName + ":" + valueFromDTO.getValue());
201 logger.debug("Value of " + baseType.getQName().getNamespace() + ":"
202 + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is "
203 + node.getValue().getClass());
204 writer.value(String.valueOf(node.getValue()));
206 } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
207 || baseType instanceof UnsignedIntegerTypeDefinition) {
208 writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType).serialize(node.getValue())));
209 } else if (baseType instanceof BooleanTypeDefinition) {
210 writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType).serialize(node.getValue())));
211 } else if (baseType instanceof EmptyTypeDefinition) {
212 writeEmptyDataTypeToJson(writer);
214 String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue()));
216 value = String.valueOf(node.getValue());
218 writer.value(value.equals("null") ? "" : value);
222 private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
228 private void writeName(Node<?> node, DataSchemaNode schema, JsonWriter writer) throws IOException {
229 String nameForOutput = node.getNodeType().getLocalName();
230 if (schema.isAugmenting()) {
231 ControllerContext contContext = ControllerContext.getInstance();
232 CharSequence moduleName;
233 moduleName = contContext.toRestconfIdentifier(schema.getQName());
234 if (moduleName != null) {
235 nameForOutput = moduleName.toString();
238 writer.name(nameForOutput);
241 private static final class NumberForJsonWriter extends Number {
243 private static final long serialVersionUID = -3147729419814417666L;
244 private final String value;
246 public NumberForJsonWriter(String value) {
251 public int intValue() {
252 throw new IllegalStateException("Should not be invoked");
256 public long longValue() {
257 throw new IllegalStateException("Should not be invoked");
261 public float floatValue() {
262 throw new IllegalStateException("Should not be invoked");
266 public double doubleValue() {
267 throw new IllegalStateException("Should not be invoked");
271 public String toString() {