3e4ef118b861e77db808b3efb5dfcf370a732be0
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / 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;
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.api.MediaTypes;
24 import org.opendaylight.restconf.common.errors.RestconfError;
25 import org.opendaylight.restconf.server.api.DatabindContext;
26 import org.opendaylight.restconf.server.api.PatchStatusContext;
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.patchId());
59         writer.writeEndElement();
60
61         if (context.ok()) {
62             writer.writeEmptyElement("ok");
63         } else {
64             final var globalErrors = context.globalErrors();
65             if (globalErrors != null) {
66                 reportErrors(context.databind(), globalErrors, writer);
67             } else {
68                 writer.writeStartElement("edit-status");
69                 for (var patchStatusEntity : context.editCollection()) {
70                     writer.writeStartElement("edit");
71                     writer.writeStartElement("edit-id");
72                     writer.writeCharacters(patchStatusEntity.getEditId());
73                     writer.writeEndElement();
74
75                     final var editErrors = patchStatusEntity.getEditErrors();
76                     if (editErrors != null) {
77                         reportErrors(context.databind(), editErrors, writer);
78                     } else if (patchStatusEntity.isOk()) {
79                         writer.writeEmptyElement("ok");
80                     }
81                     writer.writeEndElement();
82                 }
83                 writer.writeEndElement();
84             }
85         }
86         writer.writeEndElement();
87         writer.flush();
88     }
89
90     private static void reportErrors(final DatabindContext databind, final List<RestconfError> errors,
91             final XMLStreamWriter writer) throws XMLStreamException {
92         writer.writeStartElement("errors");
93
94         for (var 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().elementBody());
101             writer.writeEndElement();
102
103             // optional node
104             final var errorPath = restconfError.getErrorPath();
105             if (errorPath != null) {
106                 writer.writeStartElement("error-path");
107                 databind.xmlCodecs().instanceIdentifierCodec().writeValue(writer, errorPath);
108                 writer.writeEndElement();
109             }
110
111             // optional node
112             final var errorMessage = restconfError.getErrorMessage();
113             if (errorMessage != null) {
114                 writer.writeStartElement("error-message");
115                 writer.writeCharacters(errorMessage);
116                 writer.writeEndElement();
117             }
118
119             // optional node
120             final var errorInfo = restconfError.getErrorInfo();
121             if (errorInfo != null) {
122                 writer.writeStartElement("error-info");
123                 writer.writeCharacters(errorInfo);
124                 writer.writeEndElement();
125             }
126         }
127
128         writer.writeEndElement();
129     }
130 }