4944d96b546933c49637fbb66b4b91ea8add05d1
[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.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import java.net.URI;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.ext.MessageBodyWriter;
23 import javax.ws.rs.ext.Provider;
24 import org.opendaylight.controller.sal.rest.api.Draft02;
25 import org.opendaylight.controller.sal.rest.api.RestconfService;
26 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
27 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
35 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
36 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44
45 @Provider
46 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
47     Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
48 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
49
50     @Override
51     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
52         return type.equals(NormalizedNodeContext.class);
53     }
54
55     @Override
56     public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
57         return -1;
58     }
59
60     @Override
61     public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations,
62             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
63                     throws IOException, WebApplicationException {
64         NormalizedNode<?, ?> data = t.getData();
65         if (data == null) {
66             return;
67         }
68
69         final InstanceIdentifierContext<SchemaNode> context = (InstanceIdentifierContext<SchemaNode>) t.getInstanceIdentifierContext();
70
71         SchemaPath path = context.getSchemaNode().getPath();
72         boolean isDataRoot = false;
73         if (SchemaPath.ROOT.equals(path)) {
74             isDataRoot = true;
75         } else if (context.getSchemaNode() instanceof RpcDefinition) {
76             isDataRoot = true;
77             path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
78         } else {
79             path = path.getParent();
80             // FIXME: Add proper handling of reading root.
81         }
82
83         final JsonWriter jsonWriter = createJsonWriter(entityStream);
84         final NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
85
86         jsonWriter.beginObject();
87         if(isDataRoot) {
88             writeDataRoot(nnWriter,(ContainerNode) data);
89         } else {
90             if(data instanceof MapEntryNode) {
91                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
92             }
93             nnWriter.write(data);
94         }
95
96         nnWriter.flush();
97         jsonWriter.endObject();
98         jsonWriter.flush();
99     }
100
101     private NormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext<SchemaNode> context,
102             final SchemaPath path, final JsonWriter jsonWriter) {
103
104         final SchemaNode schema = context.getSchemaNode();
105         final JSONCodecFactory codecs = getCodecFactory(context);
106
107         URI initialNs = null;
108         if ( ! (schema instanceof RpcDefinition) && (!((DataSchemaNode)schema).isAugmenting() && !(schema instanceof SchemaContext))) {
109             initialNs = schema.getQName().getNamespace();
110         }
111         final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
112         return NormalizedNodeWriter.forStreamWriter(streamWriter);
113     }
114
115     private JsonWriter createJsonWriter(final OutputStream entityStream) {
116         // FIXME BUG-2153: Add pretty print support
117         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
118
119     }
120
121     private JSONCodecFactory getCodecFactory(final InstanceIdentifierContext context) {
122         // TODO: Performance: Cache JSON Codec factory and schema context
123         return JSONCodecFactory.create(context.getSchemaContext());
124     }
125
126     private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
127         for(final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
128             nnWriter.write(child);
129         }
130     }
131
132 }