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