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