8b279360bf546f40e71664a67d9dd6556e0ff8a0
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchStatusBodyWriter.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 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import java.nio.charset.StandardCharsets;
15 import java.util.List;
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.Provider;
20 import javax.xml.stream.XMLOutputFactory;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamWriter;
23 import org.opendaylight.restconf.common.errors.RestconfError;
24 import org.opendaylight.restconf.common.patch.PatchStatusContext;
25 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
26 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.status.YangPatchStatus;
28
29 @Provider
30 @Produces(MediaTypes.APPLICATION_YANG_DATA_XML)
31 public class XmlPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
32     private static final String XML_NAMESPACE = YangPatchStatus.QNAME.getNamespace().toString();
33     private static final XMLOutputFactory XML_FACTORY;
34
35     static {
36         XML_FACTORY = XMLOutputFactory.newFactory();
37         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
38     }
39
40     @Override
41     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
42                         final Annotation[] annotations, final MediaType mediaType,
43                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
44             throws IOException {
45         try {
46             final XMLStreamWriter xmlWriter =
47                     XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
48             writeDocument(xmlWriter, patchStatusContext);
49         } catch (final XMLStreamException e) {
50             throw new IOException("Failed to write body", e);
51         }
52     }
53
54     private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
55             throws XMLStreamException {
56         writer.writeStartElement("", "yang-patch-status", XML_NAMESPACE);
57         writer.writeStartElement("patch-id");
58         writer.writeCharacters(context.getPatchId());
59         writer.writeEndElement();
60
61         if (context.isOk()) {
62             writer.writeEmptyElement("ok");
63         } else {
64             if (context.getGlobalErrors() != null) {
65                 reportErrors(context.getGlobalErrors(), writer);
66             }
67             writer.writeStartElement("edit-status");
68             for (final PatchStatusEntity patchStatusEntity : context.getEditCollection()) {
69                 writer.writeStartElement("edit");
70                 writer.writeStartElement("edit-id");
71                 writer.writeCharacters(patchStatusEntity.getEditId());
72                 writer.writeEndElement();
73                 if (patchStatusEntity.getEditErrors() != null) {
74                     reportErrors(patchStatusEntity.getEditErrors(), writer);
75                 } else {
76                     if (patchStatusEntity.isOk()) {
77                         writer.writeEmptyElement("ok");
78                     }
79                 }
80                 writer.writeEndElement();
81             }
82             writer.writeEndElement();
83
84         }
85         writer.writeEndElement();
86
87         writer.flush();
88     }
89
90     private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer)
91             throws XMLStreamException {
92         writer.writeStartElement("errors");
93
94         for (final RestconfError restconfError : errors) {
95             writer.writeStartElement("error-type");
96             writer.writeCharacters(restconfError.getErrorType().elementBody());
97             writer.writeEndElement();
98
99             writer.writeStartElement("error-tag");
100             writer.writeCharacters(restconfError.getErrorTag().getTagValue());
101             writer.writeEndElement();
102
103             // optional node
104             if (restconfError.getErrorPath() != null) {
105                 writer.writeStartElement("error-path");
106                 writer.writeCharacters(restconfError.getErrorPath().toString());
107                 writer.writeEndElement();
108             }
109
110             // optional node
111             if (restconfError.getErrorMessage() != null) {
112                 writer.writeStartElement("error-message");
113                 writer.writeCharacters(restconfError.getErrorMessage());
114                 writer.writeEndElement();
115             }
116
117             // optional node
118             if (restconfError.getErrorInfo() != null) {
119                 writer.writeStartElement("error-info");
120                 writer.writeCharacters(restconfError.getErrorInfo());
121                 writer.writeEndElement();
122             }
123         }
124
125         writer.writeEndElement();
126     }
127 }