1c695ac2d70ae4a325ca8b8c60adfc5ae03da963
[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.restconf.common.errors.RestconfError;
32
33 @Provider
34 @Produces({ Draft02.MediaTypes.PATCH_STATUS + RestconfService.XML })
35 public class PatchXmlBodyWriter implements MessageBodyWriter<PatchStatusContext> {
36
37     private static final XMLOutputFactory XML_FACTORY;
38
39     static {
40         XML_FACTORY = XMLOutputFactory.newFactory();
41         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
42     }
43
44     @Override
45     public boolean isWriteable(final Class<?> type, final Type genericType,
46                                final Annotation[] annotations, final MediaType mediaType) {
47         return type.equals(PatchStatusContext.class);
48     }
49
50     @Override
51     public long getSize(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
52                         final Annotation[] annotations, final MediaType mediaType) {
53         return -1;
54     }
55
56     @Override
57     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
58                         final Annotation[] annotations, final MediaType mediaType,
59                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
60             throws IOException, WebApplicationException {
61
62         try {
63             final XMLStreamWriter xmlWriter =
64                     XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
65             writeDocument(xmlWriter, patchStatusContext);
66         } catch (final XMLStreamException e) {
67             throw new IllegalStateException(e);
68         } catch (final FactoryConfigurationError e) {
69             throw new IllegalStateException(e);
70         }
71     }
72
73     private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
74             throws XMLStreamException, IOException {
75         writer.writeStartElement("", "yang-patch-status", "urn:ietf:params:xml:ns:yang:ietf-yang-patch");
76         writer.writeStartElement("patch-id");
77         writer.writeCharacters(context.getPatchId());
78         writer.writeEndElement();
79
80         if (context.isOk()) {
81             writer.writeEmptyElement("ok");
82         } else {
83             if (context.getGlobalErrors() != null) {
84                 reportErrors(context.getGlobalErrors(), writer);
85             }
86             writer.writeStartElement("edit-status");
87             for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
88                 writer.writeStartElement("edit");
89                 writer.writeStartElement("edit-id");
90                 writer.writeCharacters(patchStatusEntity.getEditId());
91                 writer.writeEndElement();
92                 if (patchStatusEntity.getEditErrors() != null) {
93                     reportErrors(patchStatusEntity.getEditErrors(), writer);
94                 } else {
95                     if (patchStatusEntity.isOk()) {
96                         writer.writeEmptyElement("ok");
97                     }
98                 }
99                 writer.writeEndElement();
100             }
101             writer.writeEndElement();
102
103         }
104         writer.writeEndElement();
105
106         writer.flush();
107     }
108
109     private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
110             throws IOException, XMLStreamException {
111         writer.writeStartElement("errors");
112
113         for (final RestconfError restconfError : errors) {
114             writer.writeStartElement("error-type");
115             writer.writeCharacters(restconfError.getErrorType().getErrorTypeTag());
116             writer.writeEndElement();
117
118             writer.writeStartElement("error-tag");
119             writer.writeCharacters(restconfError.getErrorTag().getTagValue());
120             writer.writeEndElement();
121
122             // optional node
123             if (restconfError.getErrorPath() != null) {
124                 writer.writeStartElement("error-path");
125                 writer.writeCharacters(restconfError.getErrorPath().toString());
126                 writer.writeEndElement();
127             }
128
129             // optional node
130             if (restconfError.getErrorMessage() != null) {
131                 writer.writeStartElement("error-message");
132                 writer.writeCharacters(restconfError.getErrorMessage());
133                 writer.writeEndElement();
134             }
135
136             // optional node
137             if (restconfError.getErrorInfo() != null) {
138                 writer.writeStartElement("error-info");
139                 writer.writeCharacters(restconfError.getErrorInfo());
140                 writer.writeEndElement();
141             }
142         }
143
144         writer.writeEndElement();
145     }
146 }