2 * Copyright (c) 2014 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.controller.sal.rest.impl;
10 import com.google.common.base.Throwables;
11 import com.google.common.collect.Iterables;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
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.controller.sal.rest.api.Draft02;
27 import org.opendaylight.controller.sal.rest.api.RestconfService;
28 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
45 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
46 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
47 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
49 private static final XMLOutputFactory XML_FACTORY;
52 XML_FACTORY = XMLOutputFactory.newFactory();
53 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
57 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
58 final MediaType mediaType) {
59 return type.equals(NormalizedNodeContext.class);
63 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
64 final Annotation[] annotations, final MediaType mediaType) {
69 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
70 final Annotation[] annotations, final MediaType mediaType,
71 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
72 WebApplicationException {
73 final InstanceIdentifierContext pathContext = t.getInstanceIdentifierContext();
74 if (t.getData() == null) {
78 XMLStreamWriter xmlWriter;
80 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
81 } catch (final XMLStreamException e) {
82 throw new IllegalStateException(e);
83 } catch (final FactoryConfigurationError e) {
84 throw new IllegalStateException(e);
86 NormalizedNode<?, ?> data = t.getData();
87 SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
89 // The utility method requires the path to be size of 2
90 boolean isRpc = false;
91 if(Iterables.size(schemaPath.getPathFromRoot()) > 1) {
92 isRpc = SchemaContextUtil.getRpcDataSchema(t.getInstanceIdentifierContext().getSchemaContext(), schemaPath) != null;
95 boolean isDataRoot = false;
96 if (SchemaPath.ROOT.equals(schemaPath)) {
98 // The rpc definitions required the schema path to point to the output container, not the parent (rpc itself)
101 schemaPath = schemaPath.getParent();
105 final NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
106 pathContext.getSchemaContext(), schemaPath);
107 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
109 writeRootElement(xmlWriter, nnWriter, (ContainerNode) data, SchemaContext.NAME);
111 writeRootElement(xmlWriter, nnWriter, (ContainerNode) data, schemaPath.getLastComponent());
113 if (data instanceof MapEntryNode) {
114 // Restconf allows returning one list item. We need to wrap it
115 // in map node in order to serialize it properly
116 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
118 nnWriter.write(data);
123 private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data, final QName name)
126 xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
127 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
128 nnWriter.write(child);
131 xmlWriter.writeEndElement();
133 } catch (final XMLStreamException e) {
134 Throwables.propagate(e);