2 * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.netconf.sal.rest.impl;
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Throwables;
14 import com.google.common.collect.Iterables;
15 import com.google.gson.stream.JsonWriter;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStreamWriter;
20 import java.util.Iterator;
21 import java.util.List;
22 import javax.ws.rs.core.Context;
23 import javax.ws.rs.core.HttpHeaders;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.ext.ExceptionMapper;
27 import javax.ws.rs.ext.Provider;
28 import javax.xml.XMLConstants;
29 import javax.xml.stream.FactoryConfigurationError;
30 import javax.xml.stream.XMLOutputFactory;
31 import javax.xml.stream.XMLStreamException;
32 import javax.xml.stream.XMLStreamWriter;
33 import org.opendaylight.netconf.sal.rest.api.Draft02;
34 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
36 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
49 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
50 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
51 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
52 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
53 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
54 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
55 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
59 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
61 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
62 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
64 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
65 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
70 * This class defines an ExceptionMapper that handles RestconfDocumentedExceptions thrown by resource implementations
71 * and translates appropriately to restconf error response as defined in the RESTCONF RFC draft.
73 * @author Thomas Pantelis
76 public class RestconfDocumentedExceptionMapper implements ExceptionMapper<RestconfDocumentedException> {
78 private final static Logger LOG = LoggerFactory.getLogger(RestconfDocumentedExceptionMapper.class);
80 private static final XMLOutputFactory XML_FACTORY;
83 XML_FACTORY = XMLOutputFactory.newFactory();
84 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
88 private HttpHeaders headers;
91 public Response toResponse(final RestconfDocumentedException exception) {
93 LOG.debug("In toResponse: {}", exception.getMessage());
95 final List<MediaType> accepts = headers.getAcceptableMediaTypes();
96 accepts.remove(MediaType.WILDCARD_TYPE);
98 LOG.debug("Accept headers: {}", accepts);
100 final MediaType mediaType;
101 if (accepts != null && accepts.size() > 0) {
102 mediaType = accepts.get(0); // just pick the first one
104 // Default to the content type if there's no Accept header
105 mediaType = MediaType.APPLICATION_JSON_TYPE;
108 LOG.debug("Using MediaType: {}", mediaType);
110 final List<RestconfError> errors = exception.getErrors();
111 if (errors.isEmpty()) {
112 // We don't actually want to send any content but, if we don't set any content here,
113 // the tomcat front-end will send back an html error report. To prevent that, set a
114 // single space char in the entity.
116 return Response.status(exception.getStatus()).type(MediaType.TEXT_PLAIN_TYPE).entity(" ").build();
119 final int status = errors.iterator().next().getErrorTag().getStatusCode();
121 final ControllerContext context = ControllerContext.getInstance();
122 final DataNodeContainer errorsSchemaNode = (DataNodeContainer) context.getRestconfModuleErrorsSchemaNode();
124 if (errorsSchemaNode == null) {
125 return Response.status(status).type(MediaType.TEXT_PLAIN_TYPE).entity(exception.getMessage()).build();
128 Preconditions.checkState(errorsSchemaNode instanceof ContainerSchemaNode,
129 "Found Errors SchemaNode isn't ContainerNode");
130 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> errContBuild =
131 Builders.containerBuilder((ContainerSchemaNode) errorsSchemaNode);
133 final List<DataSchemaNode> schemaList = ControllerContext.findInstanceDataChildrenByName(errorsSchemaNode,
134 Draft02.RestConfModule.ERROR_LIST_SCHEMA_NODE);
135 final DataSchemaNode errListSchemaNode = Iterables.getFirst(schemaList, null);
136 Preconditions.checkState(errListSchemaNode instanceof ListSchemaNode, "Found Error SchemaNode isn't ListSchemaNode");
137 final CollectionNodeBuilder<MapEntryNode, MapNode> listErorsBuilder = Builders
138 .mapBuilder((ListSchemaNode) errListSchemaNode);
141 for (final RestconfError error : errors) {
142 listErorsBuilder.withChild(toErrorEntryNode(error, errListSchemaNode));
144 errContBuild.withChild(listErorsBuilder.build());
146 final NormalizedNodeContext errContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(null,
147 (DataSchemaNode) errorsSchemaNode, null, context.getGlobalSchema()), errContBuild.build());
150 if (mediaType.getSubtype().endsWith("json")) {
151 responseBody = toJsonResponseBody(errContext, errorsSchemaNode);
153 responseBody = toXMLResponseBody(errContext, errorsSchemaNode);
156 return Response.status(status).type(mediaType).entity(responseBody).build();
159 private static MapEntryNode toErrorEntryNode(final RestconfError error, final DataSchemaNode errListSchemaNode) {
160 Preconditions.checkArgument(errListSchemaNode instanceof ListSchemaNode,
161 "errListSchemaNode has to be of type ListSchemaNode");
162 final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) errListSchemaNode;
163 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> errNodeValues = Builders
164 .mapEntryBuilder(listStreamSchemaNode);
166 List<DataSchemaNode> lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
167 (listStreamSchemaNode), "error-type");
168 final DataSchemaNode errTypSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
169 Preconditions.checkState(errTypSchemaNode instanceof LeafSchemaNode);
170 errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errTypSchemaNode)
171 .withValue(error.getErrorType().getErrorTypeTag()).build());
173 lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
174 (listStreamSchemaNode), "error-tag");
175 final DataSchemaNode errTagSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
176 Preconditions.checkState(errTagSchemaNode instanceof LeafSchemaNode);
177 errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errTagSchemaNode)
178 .withValue(error.getErrorTag().getTagValue()).build());
180 if (error.getErrorAppTag() != null) {
181 lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
182 (listStreamSchemaNode), "error-app-tag");
183 final DataSchemaNode errAppTagSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
184 Preconditions.checkState(errAppTagSchemaNode instanceof LeafSchemaNode);
185 errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errAppTagSchemaNode)
186 .withValue(error.getErrorAppTag()).build());
189 lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
190 (listStreamSchemaNode), "error-message");
191 final DataSchemaNode errMsgSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
192 Preconditions.checkState(errMsgSchemaNode instanceof LeafSchemaNode);
193 errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errMsgSchemaNode)
194 .withValue(error.getErrorMessage()).build());
196 if(error.getErrorInfo() != null) {
197 // Oddly, error-info is defined as an empty container in the restconf yang. Apparently the
198 // intention is for implementors to define their own data content so we'll just treat it as a leaf
200 errNodeValues.withChild(ImmutableNodes.leafNode(Draft02.RestConfModule.ERROR_INFO_QNAME,
201 error.getErrorInfo()));
204 // TODO : find how could we add possible "error-path"
206 return errNodeValues.build();
209 private static Object toJsonResponseBody(final NormalizedNodeContext errorsNode, final DataNodeContainer errorsSchemaNode) {
210 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
211 NormalizedNode<?, ?> data = errorsNode.getData();
212 final InstanceIdentifierContext<?> context = errorsNode.getInstanceIdentifierContext();
213 final DataSchemaNode schema = (DataSchemaNode) context.getSchemaNode();
215 SchemaPath path = context.getSchemaNode().getPath();
216 final OutputStreamWriter outputWriter = new OutputStreamWriter(outStream, Charsets.UTF_8);
218 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
221 boolean isDataRoot = false;
222 URI initialNs = null;
223 if (SchemaPath.ROOT.equals(path)) {
226 path = path.getParent();
227 // FIXME: Add proper handling of reading root.
229 if (!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
230 initialNs = schema.getQName().getNamespace();
233 final JsonWriter jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
234 final NormalizedNodeStreamWriter jsonStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
235 JSONCodecFactory.create(context.getSchemaContext()), path, initialNs, jsonWriter);
237 // We create a delegating writer to special-case error-info as error-info is defined as an empty
238 // container in the restconf yang schema but we create a leaf node so we can output it. The delegate
239 // stream writer validates the node type against the schema and thus will expect a LeafSchemaNode but
240 // the schema has a ContainerSchemaNode so, to avoid an error, we override the leafNode behavior
242 final NormalizedNodeStreamWriter streamWriter = new ForwardingNormalizedNodeStreamWriter() {
244 protected NormalizedNodeStreamWriter delegate() {
245 return jsonStreamWriter;
249 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
250 if(name.getNodeType().equals(Draft02.RestConfModule.ERROR_INFO_QNAME)) {
251 jsonWriter.name(Draft02.RestConfModule.ERROR_INFO_QNAME.getLocalName());
252 jsonWriter.value(value.toString());
254 super.leafNode(name, value);
259 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(streamWriter);
262 writeDataRoot(outputWriter,nnWriter,(ContainerNode) data);
264 if (data instanceof MapEntryNode) {
265 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
267 nnWriter.write(data);
270 outputWriter.flush();
271 } catch (final IOException e) {
272 LOG.warn("Error writing error response body", e);
275 return outStream.toString();
279 private static Object toXMLResponseBody(final NormalizedNodeContext errorsNode, final DataNodeContainer errorsSchemaNode) {
280 final InstanceIdentifierContext<?> pathContext = errorsNode.getInstanceIdentifierContext();
281 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
283 final XMLStreamWriter xmlWriter;
285 xmlWriter = XML_FACTORY.createXMLStreamWriter(outStream, "UTF-8");
286 } catch (final XMLStreamException e) {
287 throw new IllegalStateException(e);
288 } catch (final FactoryConfigurationError e) {
289 throw new IllegalStateException(e);
291 NormalizedNode<?, ?> data = errorsNode.getData();
292 SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
294 boolean isDataRoot = false;
295 if (SchemaPath.ROOT.equals(schemaPath)) {
298 schemaPath = schemaPath.getParent();
301 final NormalizedNodeStreamWriter xmlStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
302 pathContext.getSchemaContext(), schemaPath);
304 // We create a delegating writer to special-case error-info as error-info is defined as an empty
305 // container in the restconf yang schema but we create a leaf node so we can output it. The delegate
306 // stream writer validates the node type against the schema and thus will expect a LeafSchemaNode but
307 // the schema has a ContainerSchemaNode so, to avoid an error, we override the leafNode behavior
309 final NormalizedNodeStreamWriter streamWriter = new ForwardingNormalizedNodeStreamWriter() {
311 protected NormalizedNodeStreamWriter delegate() {
312 return xmlStreamWriter;
316 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
317 if(name.getNodeType().equals(Draft02.RestConfModule.ERROR_INFO_QNAME)) {
318 String ns = Draft02.RestConfModule.ERROR_INFO_QNAME.getNamespace().toString();
320 xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,
321 Draft02.RestConfModule.ERROR_INFO_QNAME.getLocalName(), ns);
322 xmlWriter.writeCharacters(value.toString());
323 xmlWriter.writeEndElement();
324 } catch (XMLStreamException e) {
325 throw new IOException("Error writing error-info", e);
328 super.leafNode(name, value);
333 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(streamWriter);
336 writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
338 if (data instanceof MapEntryNode) {
339 // Restconf allows returning one list item. We need to wrap it
340 // in map node in order to serialize it properly
341 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
343 nnWriter.write(data);
347 catch (final IOException e) {
348 LOG.warn("Error writing error response body.", e);
351 return outStream.toString();
354 private static void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data)
357 final QName name = SchemaContext.NAME;
358 xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
359 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
360 nnWriter.write(child);
363 xmlWriter.writeEndElement();
365 } catch (final XMLStreamException e) {
366 Throwables.propagate(e);
370 private static void writeDataRoot(final OutputStreamWriter outputWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
371 final Iterator<DataContainerChild<? extends PathArgument, ?>> iterator = data.getValue().iterator();
372 while (iterator.hasNext()) {
373 final DataContainerChild<? extends PathArgument, ?> child = iterator.next();
374 nnWriter.write(child);