2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
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;
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;
36 XML_FACTORY = XMLOutputFactory.newFactory();
37 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
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)
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);
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();
62 writer.writeEmptyElement("ok");
64 final var globalErrors = context.globalErrors();
65 if (globalErrors != null) {
66 reportErrors(context.databind(), globalErrors, writer);
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();
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");
81 writer.writeEndElement();
83 writer.writeEndElement();
86 writer.writeEndElement();
90 private static void reportErrors(final DatabindContext databind, final List<RestconfError> errors,
91 final XMLStreamWriter writer) throws XMLStreamException {
92 writer.writeStartElement("errors");
94 for (var restconfError : errors) {
95 writer.writeStartElement("error-type");
96 writer.writeCharacters(restconfError.getErrorType().elementBody());
97 writer.writeEndElement();
99 writer.writeStartElement("error-tag");
100 writer.writeCharacters(restconfError.getErrorTag().elementBody());
101 writer.writeEndElement();
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();
112 final var errorMessage = restconfError.getErrorMessage();
113 if (errorMessage != null) {
114 writer.writeStartElement("error-message");
115 writer.writeCharacters(errorMessage);
116 writer.writeEndElement();
120 final var errorInfo = restconfError.getErrorInfo();
121 if (errorInfo != null) {
122 writer.writeStartElement("error-info");
123 writer.writeCharacters(errorInfo);
124 writer.writeEndElement();
128 writer.writeEndElement();