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 // TODO check InstanceIdentifierTypeDefinition
188 if (baseType instanceof IdentityrefTypeDefinition) {
189 if (node.getValue() instanceof QName) {
190 IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType).serialize(node.getValue());
191 IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0);
192 String moduleName = ControllerContext.getInstance().findModuleByNamespace(URI.create(valueFromDTO.getNamespace()));
193 writer.value(moduleName + ":" + valueFromDTO.getValue());
195 logger.debug("Value of " + baseType.getQName().getNamespace() + ":"
196 + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is " + node.getValue().getClass());
197 writer.value(String.valueOf(node.getValue()));
199 } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition
200 || baseType instanceof UnsignedIntegerTypeDefinition) {
201 writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType).serialize(node.getValue())));
202 } else if (baseType instanceof BooleanTypeDefinition) {
203 writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType).serialize(node.getValue())));
204 } else if (baseType instanceof EmptyTypeDefinition) {
205 writeEmptyDataTypeToJson(writer);
207 String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue()));
209 value = String.valueOf(node.getValue());
211 writer.value(value.equals("null") ? "" : value);
215 private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException {
221 private void writeName(Node<?> node, DataSchemaNode schema, JsonWriter writer) throws IOException {
222 String nameForOutput = node.getNodeType().getLocalName();
223 if (schema.isAugmenting()) {
224 ControllerContext contContext = ControllerContext.getInstance();
225 CharSequence moduleName;
226 moduleName = contContext.toRestconfIdentifier(schema.getQName());
227 if (moduleName != null) {
228 nameForOutput = moduleName.toString();
231 writer.name(nameForOutput);
234 private static final class NumberForJsonWriter extends Number {
236 private static final long serialVersionUID = -3147729419814417666L;
237 private final String value;
239 public NumberForJsonWriter(String value) {
244 public int intValue() {
245 throw new IllegalStateException("Should not be invoked");
249 public long longValue() {
250 throw new IllegalStateException("Should not be invoked");
254 public float floatValue() {
255 throw new IllegalStateException("Should not be invoked");
259 public double doubleValue() {
260 throw new IllegalStateException("Should not be invoked");
264 public String toString() {