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