Merge "Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to...
[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 com.google.common.collect.Iterables;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedMap;
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.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
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         final InstanceIdentifierContext pathContext = t.getInstanceIdentifierContext();
74         if (t.getData() == null) {
75             return;
76         }
77
78         XMLStreamWriter xmlWriter;
79         try {
80             xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
81         } catch (final XMLStreamException e) {
82             throw new IllegalStateException(e);
83         } catch (final FactoryConfigurationError e) {
84             throw new IllegalStateException(e);
85         }
86         NormalizedNode<?, ?> data = t.getData();
87         SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
88
89         // The utility method requires the path to be size of 2
90         boolean isRpc = false;
91         if(Iterables.size(schemaPath.getPathFromRoot()) > 1) {
92             isRpc = SchemaContextUtil.getRpcDataSchema(t.getInstanceIdentifierContext().getSchemaContext(), schemaPath) != null;
93         }
94
95         boolean isDataRoot = false;
96         if (SchemaPath.ROOT.equals(schemaPath)) {
97             isDataRoot = true;
98         // The rpc definitions required the schema path to point to the output container, not the parent (rpc itself)
99         } else {
100             if(!isRpc) {
101                 schemaPath = schemaPath.getParent();
102             }
103         }
104
105         final NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
106                 pathContext.getSchemaContext(), schemaPath);
107         final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
108         if (isDataRoot) {
109             writeRootElement(xmlWriter, nnWriter, (ContainerNode) data, SchemaContext.NAME);
110         } else if(isRpc) {
111             writeRootElement(xmlWriter, nnWriter, (ContainerNode) data, schemaPath.getLastComponent());
112         } else {
113             if (data instanceof MapEntryNode) {
114                 // Restconf allows returning one list item. We need to wrap it
115                 // in map node in order to serialize it properly
116                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
117             }
118             nnWriter.write(data);
119             nnWriter.flush();
120         }
121     }
122
123     private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data, final QName name)
124             throws IOException {
125         try {
126             xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
127             for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
128                 nnWriter.write(child);
129             }
130             nnWriter.flush();
131             xmlWriter.writeEndElement();
132             xmlWriter.flush();
133         } catch (final XMLStreamException e) {
134             Throwables.propagate(e);
135         }
136     }
137 }