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.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;
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;
37 XML_FACTORY = XMLOutputFactory.newFactory();
38 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
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)
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);
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();
63 writer.writeEmptyElement("ok");
65 final var globalErrors = context.globalErrors();
66 if (globalErrors != null) {
67 reportErrors(context.context(), globalErrors, writer);
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();
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");
82 writer.writeEndElement();
84 writer.writeEndElement();
87 writer.writeEndElement();
91 private static void reportErrors(final EffectiveModelContext modelContext, final List<RestconfError> errors,
92 final XMLStreamWriter writer) throws XMLStreamException {
93 writer.writeStartElement("errors");
95 for (var restconfError : errors) {
96 writer.writeStartElement("error-type");
97 writer.writeCharacters(restconfError.getErrorType().elementBody());
98 writer.writeEndElement();
100 writer.writeStartElement("error-tag");
101 writer.writeCharacters(restconfError.getErrorTag().elementBody());
102 writer.writeEndElement();
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();
114 final var errorMessage = restconfError.getErrorMessage();
115 if (errorMessage != null) {
116 writer.writeStartElement("error-message");
117 writer.writeCharacters(errorMessage);
118 writer.writeEndElement();
122 final var errorInfo = restconfError.getErrorInfo();
123 if (errorInfo != null) {
124 writer.writeStartElement("error-info");
125 writer.writeCharacters(errorInfo);
126 writer.writeEndElement();
130 writer.writeEndElement();