2cf01f1d5adc24701b5ee7a240d4009f70c6b0d0
[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.common.errors.RestconfError;
24 import org.opendaylight.restconf.common.patch.PatchStatusContext;
25 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.status.YangPatchStatus;
27 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29
30 @Provider
31 @Produces(MediaTypes.APPLICATION_YANG_DATA_XML)
32 public class XmlPatchStatusBodyWriter extends AbstractPatchStatusBodyWriter {
33     private static final String XML_NAMESPACE = YangPatchStatus.QNAME.getNamespace().toString();
34     private static final XMLOutputFactory XML_FACTORY;
35
36     static {
37         XML_FACTORY = XMLOutputFactory.newFactory();
38         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
39     }
40
41     @Override
42     public void writeTo(final PatchStatusContext patchStatusContext, final Class<?> type, final Type genericType,
43                         final Annotation[] annotations, final MediaType mediaType,
44                         final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
45             throws IOException {
46         try {
47             final XMLStreamWriter xmlWriter =
48                     XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
49             writeDocument(xmlWriter, patchStatusContext);
50         } catch (final XMLStreamException e) {
51             throw new IOException("Failed to write body", e);
52         }
53     }
54
55     private static void writeDocument(final XMLStreamWriter writer, final PatchStatusContext context)
56             throws XMLStreamException {
57         writer.writeStartElement("", "yang-patch-status", XML_NAMESPACE);
58         writer.writeStartElement("patch-id");
59         writer.writeCharacters(context.patchId());
60         writer.writeEndElement();
61
62         if (context.ok()) {
63             writer.writeEmptyElement("ok");
64         } else {
65             final var globalErrors = context.globalErrors();
66             if (globalErrors != null) {
67                 reportErrors(context.context(), globalErrors, writer);
68             } else {
69                 writer.writeStartElement("edit-status");
70                 for (var patchStatusEntity : context.editCollection()) {
71                     writer.writeStartElement("edit");
72                     writer.writeStartElement("edit-id");
73                     writer.writeCharacters(patchStatusEntity.getEditId());
74                     writer.writeEndElement();
75
76                     final var editErrors = patchStatusEntity.getEditErrors();
77                     if (editErrors != null) {
78                         reportErrors(context.context(), editErrors, writer);
79                     } else if (patchStatusEntity.isOk()) {
80                         writer.writeEmptyElement("ok");
81                     }
82                     writer.writeEndElement();
83                 }
84                 writer.writeEndElement();
85             }
86         }
87         writer.writeEndElement();
88         writer.flush();
89     }
90
91     private static void reportErrors(final EffectiveModelContext modelContext, final List<RestconfError> errors,
92             final XMLStreamWriter writer) throws XMLStreamException {
93         writer.writeStartElement("errors");
94
95         for (var restconfError : errors) {
96             writer.writeStartElement("error-type");
97             writer.writeCharacters(restconfError.getErrorType().elementBody());
98             writer.writeEndElement();
99
100             writer.writeStartElement("error-tag");
101             writer.writeCharacters(restconfError.getErrorTag().elementBody());
102             writer.writeEndElement();
103
104             // optional node
105             final var errorPath = restconfError.getErrorPath();
106             if (errorPath != null) {
107                 writer.writeStartElement("error-path");
108                 XmlCodecFactory.create(modelContext).instanceIdentifierCodec()
109                     .writeValue(writer, errorPath);
110                 writer.writeEndElement();
111             }
112
113             // optional node
114             final var errorMessage = restconfError.getErrorMessage();
115             if (errorMessage != null) {
116                 writer.writeStartElement("error-message");
117                 writer.writeCharacters(errorMessage);
118                 writer.writeEndElement();
119             }
120
121             // optional node
122             final var errorInfo = restconfError.getErrorInfo();
123             if (errorInfo != null) {
124                 writer.writeStartElement("error-info");
125                 writer.writeCharacters(errorInfo);
126                 writer.writeEndElement();
127             }
128         }
129
130         writer.writeEndElement();
131     }
132 }