Delete netconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / NormalizedNodeJsonBodyWriter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.rest.impl;
9
10 import com.google.common.base.Charsets;
11 import com.google.common.base.Optional;
12 import com.google.gson.stream.JsonWriter;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.io.OutputStreamWriter;
16 import java.lang.annotation.Annotation;
17 import java.lang.reflect.Type;
18 import java.net.URI;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyWriter;
24 import javax.ws.rs.ext.Provider;
25 import org.opendaylight.controller.sal.rest.api.Draft02;
26 import org.opendaylight.controller.sal.rest.api.RestconfNormalizedNodeWriter;
27 import org.opendaylight.controller.sal.rest.api.RestconfService;
28 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
37 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45
46 @Provider
47 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
48     Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
49 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
50
51     private static final int DEFAULT_INDENT_SPACES_NUM = 2;
52
53     @Override
54     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
55         return type.equals(NormalizedNodeContext.class);
56     }
57
58     @Override
59     public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
60         return -1;
61     }
62
63     @Override
64     public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations,
65             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
66                     throws IOException, WebApplicationException {
67         NormalizedNode<?, ?> data = t.getData();
68         if (data == null) {
69             return;
70         }
71
72         @SuppressWarnings("unchecked")
73         final InstanceIdentifierContext<SchemaNode> context = (InstanceIdentifierContext<SchemaNode>) t.getInstanceIdentifierContext();
74
75         SchemaPath path = context.getSchemaNode().getPath();
76         final JsonWriter jsonWriter = createJsonWriter(entityStream, t.getWriterParameters().isPrettyPrint());
77         jsonWriter.beginObject();
78         writeNormalizedNode(jsonWriter,path,context,data, t.getWriterParameters().getDepth());
79         jsonWriter.endObject();
80         jsonWriter.flush();
81     }
82
83     private void writeNormalizedNode(JsonWriter jsonWriter, SchemaPath path,
84             InstanceIdentifierContext<SchemaNode> context, NormalizedNode<?, ?> data, Optional<Integer> depth) throws
85             IOException {
86         final RestconfNormalizedNodeWriter nnWriter;
87         if (SchemaPath.ROOT.equals(path)) {
88             /*
89              *  Creates writer without initialNs and we write children of root data container
90              *  which is not visible in restconf
91              */
92             nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
93             writeChildren(nnWriter,(ContainerNode) data);
94         } else if (context.getSchemaNode() instanceof RpcDefinition) {
95             /*
96              *  RpcDefinition is not supported as initial codec in JSONStreamWriter,
97              *  so we need to emit initial output declaratation..
98              */
99             path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
100             nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
101             jsonWriter.name("output");
102             jsonWriter.beginObject();
103             writeChildren(nnWriter, (ContainerNode) data);
104             jsonWriter.endObject();
105         } else {
106             path = path.getParent();
107
108             if(data instanceof MapEntryNode) {
109                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
110             }
111             nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
112             nnWriter.write(data);
113         }
114         nnWriter.flush();
115     }
116
117     private void writeChildren(final RestconfNormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
118         for(final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
119             nnWriter.write(child);
120         }
121     }
122
123     private RestconfNormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext<SchemaNode> context,
124             final SchemaPath path, final JsonWriter jsonWriter, Optional<Integer> depth) {
125
126         final SchemaNode schema = context.getSchemaNode();
127         final JSONCodecFactory codecs = getCodecFactory(context);
128
129         final URI initialNs;
130         if ((schema instanceof DataSchemaNode)
131                 && !((DataSchemaNode)schema).isAugmenting()
132                 && !(schema instanceof SchemaContext)) {
133             initialNs = schema.getQName().getNamespace();
134         } else if (schema instanceof RpcDefinition) {
135             initialNs = schema.getQName().getNamespace();
136         } else {
137             initialNs = null;
138         }
139         final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
140         if (depth.isPresent()) {
141             return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth.get());
142         } else {
143             return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(streamWriter);
144         }
145     }
146
147     private JsonWriter createJsonWriter(final OutputStream entityStream, boolean prettyPrint) {
148         if (prettyPrint) {
149             return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8),
150                     DEFAULT_INDENT_SPACES_NUM);
151         } else {
152             return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
153         }
154     }
155
156     private JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
157         // TODO: Performance: Cache JSON Codec factory and schema context
158         return JSONCodecFactory.create(context.getSchemaContext());
159     }
160
161 }