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