Bug 1664: Fixed JSON/XML serialization of reading mount point root data.
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / NormalizedNodeXmlBodyWriter.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.Throwables;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.ext.MessageBodyWriter;
21 import javax.ws.rs.ext.Provider;
22 import javax.xml.stream.FactoryConfigurationError;
23 import javax.xml.stream.XMLOutputFactory;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26 import org.opendaylight.controller.sal.rest.api.Draft02;
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.controller.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
39 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43
44 @Provider
45 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
46         Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
47 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
48
49     private static final XMLOutputFactory XML_FACTORY;
50
51     static {
52         XML_FACTORY = XMLOutputFactory.newFactory();
53         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
54     }
55
56     @Override
57     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
58             final MediaType mediaType) {
59         return type.equals(NormalizedNodeContext.class);
60     }
61
62     @Override
63     public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
64             final Annotation[] annotations, final MediaType mediaType) {
65         return -1;
66     }
67
68     @Override
69     public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
70             final Annotation[] annotations, final MediaType mediaType,
71             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
72             WebApplicationException {
73         InstanceIdentifierContext pathContext = t.getInstanceIdentifierContext();
74         if (t.getData() == null) {
75             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
76         }
77
78         XMLStreamWriter xmlWriter;
79         try {
80             xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
81         } catch (XMLStreamException e) {
82             throw new IllegalStateException(e);
83         } catch (FactoryConfigurationError e) {
84             throw new IllegalStateException(e);
85         }
86         NormalizedNode<?, ?> data = t.getData();
87         SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
88
89         boolean isDataRoot = false;
90         if (SchemaPath.ROOT.equals(schemaPath)) {
91             isDataRoot = true;
92         } else {
93             schemaPath = schemaPath.getParent();
94         }
95
96         NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
97                 pathContext.getSchemaContext(), schemaPath);
98         NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
99         if (isDataRoot) {
100             writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
101         } else {
102             if (data instanceof MapEntryNode) {
103                 // Restconf allows returning one list item. We need to wrap it
104                 // in map node in order to serialize it properly
105                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
106             }
107             nnWriter.write(data);
108             nnWriter.flush();
109         }
110     }
111
112     private void writeRootElement(XMLStreamWriter xmlWriter, NormalizedNodeWriter nnWriter, ContainerNode data)
113             throws IOException {
114         try {
115             QName name = SchemaContext.NAME;
116             xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
117             for (DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
118                 nnWriter.write(child);
119             }
120             nnWriter.flush();
121             xmlWriter.writeEndElement();
122             xmlWriter.flush();
123         } catch (XMLStreamException e) {
124             Throwables.propagate(e);
125         }
126     }
127 }