155fde41a94c239a29eaefcdbd12a6b7462d8f40
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / PatchXmlBodyWriter.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.netconf.sal.rest.impl;
10
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import java.nio.charset.StandardCharsets;
16 import java.util.List;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.MessageBodyWriter;
22 import javax.ws.rs.ext.Provider;
23 import javax.xml.stream.FactoryConfigurationError;
24 import javax.xml.stream.XMLOutputFactory;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.XMLStreamWriter;
27 import org.opendaylight.netconf.sal.rest.api.Draft02;
28 import org.opendaylight.netconf.sal.rest.api.RestconfService;
29 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusContext;
30 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusEntity;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
32 import org.opendaylight.restconf.Rfc8040;
33 import org.opendaylight.restconf.utils.RestconfConstants;
34
35 @Provider
36 @Produces({Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML,
37         Rfc8040.MediaTypes.PATCH_STATUS + RestconfConstants.XML})
38 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
39
40     private static final XMLOutputFactory XML_FACTORY;
41
42     static {
43         XML_FACTORY = XMLOutputFactory.newFactory();
44         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
45     }
46
47     @Override
48     public boolean isWriteable(final Class<?> type, final Type genericType,
49                                final Annotation[] annotations, final MediaType mediaType) {
50         return type.equals(PatchStatusContext.class);
51     }
52
53     @Override
54     public long getSize(final PatchStatusContext patchStatusContext, Class<?> type, final Type genericType,
55                         final Annotation[] annotations, final MediaType mediaType) {
56         return -1;
57     }
58
59     @Override
60     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
61                         final Annotation[] annotations, final MediaType mediaType,
62                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
63             throws IOException, WebApplicationException {
64
65         try {
66             final XMLStreamWriter xmlWriter =
67                     XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
68             writeDocument(xmlWriter, patchStatusContext);
69         } catch (final XMLStreamException e) {
70             throw new IllegalStateException(e);
71         } catch (final FactoryConfigurationError e) {
72             throw new IllegalStateException(e);
73         }
74     }
75
76     private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
77             throws XMLStreamException, IOException {
78         writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
79         writer.writeStartElement("patch-id");
80         writer.writeCharacters(context.getPatchId());
81         writer.writeEndElement();
82
83         if (context.isOk()) {
84             writer.writeEmptyElement("ok");
85         } else {
86             if (context.getGlobalErrors() != null) {
87                 reportErrors(context.getGlobalErrors(), writer);
88             }
89             writer.writeStartElement("edit-status");
90             for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
91                 writer.writeStartElement("edit");
92                 writer.writeStartElement("edit-id");
93                 writer.writeCharacters(patchStatusEntity.getEditId());
94                 writer.writeEndElement();
95                 if (patchStatusEntity.getEditErrors() != null) {
96                     reportErrors(patchStatusEntity.getEditErrors(), writer);
97                 } else {
98                     if (patchStatusEntity.isOk()) {
99                         writer.writeEmptyElement("ok");
100                     }
101                 }
102                 writer.writeEndElement();
103             }
104             writer.writeEndElement();
105
106         }
107         writer.writeEndElement();
108
109         writer.flush();
110     }
111
112     private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
113             throws IOException, XMLStreamException {
114         writer.writeStartElement("errors");
115
116         for (final RestconfError restconfError : errors) {
117             writer.writeStartElement("error-type");
118             writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
119             writer.writeEndElement();
120
121             writer.writeStartElement("error-tag");
122             writer.writeCharacters(restconfError.getErrorTag().getTagValue());
123             writer.writeEndElement();
124
125             // optional node
126             if (restconfError.getErrorPath() != null) {
127                 writer.writeStartElement("error-path");
128                 writer.writeCharacters(restconfError.getErrorPath().toString());
129                 writer.writeEndElement();
130             }
131
132             // optional node
133             if (restconfError.getErrorMessage() != null) {
134                 writer.writeStartElement("error-message");
135                 writer.writeCharacters(restconfError.getErrorMessage());
136                 writer.writeEndElement();
137             }
138
139             // optional node
140             if (restconfError.getErrorInfo() != null) {
141                 writer.writeStartElement("error-info");
142                 writer.writeCharacters(restconfError.getErrorInfo());
143                 writer.writeEndElement();
144             }
145         }
146
147         writer.writeEndElement();
148     }
149 }