Merge "Fixed discard-changes for mdsal netconf, mapping code cleanup."
[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.core.Response;
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.RestconfService;
27 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
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.api.schema.stream.NormalizedNodeWriter;
37 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
38 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
67         }
68
69         final InstanceIdentifierContext context = t.getInstanceIdentifierContext();
70
71         SchemaPath path = context.getSchemaNode().getPath();
72         boolean isDataRoot = false;
73         if (SchemaPath.ROOT.equals(path)) {
74             isDataRoot = true;
75         } else {
76             path = path.getParent();
77             // FIXME: Add proper handling of reading root.
78         }
79
80         JsonWriter jsonWriter = createJsonWriter(entityStream);
81         NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
82
83         jsonWriter.beginObject();
84         if(isDataRoot) {
85             writeDataRoot(nnWriter,(ContainerNode) data);
86         } else {
87             if(data instanceof MapEntryNode) {
88                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
89             }
90             nnWriter.write(data);
91         }
92
93         nnWriter.flush();
94         jsonWriter.endObject();
95         jsonWriter.flush();
96     }
97
98     private NormalizedNodeWriter createNormalizedNodeWriter(InstanceIdentifierContext context, SchemaPath path, JsonWriter jsonWriter) {
99
100         final DataSchemaNode schema = context.getSchemaNode();
101         final JSONCodecFactory codecs = getCodecFactory(context);
102
103         URI initialNs = null;
104         if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
105             initialNs = schema.getQName().getNamespace();
106         }
107         final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
108         return NormalizedNodeWriter.forStreamWriter(streamWriter);
109     }
110
111     private JsonWriter createJsonWriter(OutputStream entityStream) {
112         // FIXME BUG-2153: Add pretty print support
113         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
114
115     }
116
117     private JSONCodecFactory getCodecFactory(InstanceIdentifierContext context) {
118         // TODO: Performance: Cache JSON Codec factory and schema context
119         return JSONCodecFactory.create(context.getSchemaContext());
120     }
121
122     private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
123         for(DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
124             nnWriter.write(child);
125         }
126     }
127
128 }