Merge "BUG 1595 - Clustering : NPE on startup"
[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 java.io.IOException;
12 import java.io.OutputStream;
13 import java.io.OutputStreamWriter;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import java.net.URI;
17 import java.util.Iterator;
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.JSONNormalizedNodeStreamWriter;
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.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42
43 @Provider
44 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
45     Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
46 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
47
48     @Override
49     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
50         return type.equals(NormalizedNodeContext.class);
51     }
52
53     @Override
54     public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
55         return -1;
56     }
57
58     @Override
59     public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations,
60             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
61                     throws IOException, WebApplicationException {
62         NormalizedNode<?, ?> data = t.getData();
63         InstanceIdentifierContext context = t.getInstanceIdentifierContext();
64         DataSchemaNode schema = context.getSchemaNode();
65         SchemaPath path = context.getSchemaNode().getPath();
66         OutputStreamWriter outputWriter = new OutputStreamWriter(entityStream, Charsets.UTF_8);
67         if (data == null) {
68             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
69         }
70
71         boolean isDataRoot = false;
72         URI initialNs = null;
73         outputWriter.write('{');
74         if (SchemaPath.ROOT.equals(path)) {
75             isDataRoot = true;
76         } else {
77             path = path.getParent();
78             // FIXME: Add proper handling of reading root.
79         }
80         if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
81             initialNs = schema.getQName().getNamespace();
82         }
83         NormalizedNodeStreamWriter jsonWriter = JSONNormalizedNodeStreamWriter.create(context.getSchemaContext(),path,initialNs,outputWriter);
84         NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
85         if(isDataRoot) {
86             writeDataRoot(outputWriter,nnWriter,(ContainerNode) data);
87         } else {
88             if(data instanceof MapEntryNode) {
89                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
90             }
91             nnWriter.write(data);
92         }
93         nnWriter.flush();
94         outputWriter.write('}');
95         outputWriter.flush();
96     }
97
98     private void writeDataRoot(OutputStreamWriter outputWriter, NormalizedNodeWriter nnWriter, ContainerNode data) throws IOException {
99         Iterator<DataContainerChild<? extends PathArgument, ?>> iterator = data.getValue().iterator();
100         while(iterator.hasNext()) {
101             DataContainerChild<? extends PathArgument, ?> child = iterator.next();
102             nnWriter.write(child);
103             nnWriter.flush();
104             if(iterator.hasNext()) {
105                 outputWriter.write(",");
106             }
107         }
108     }
109
110 }