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 java.io.IOException;
12 import java.io.OutputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.MessageBodyWriter;
20 import javax.ws.rs.ext.Provider;
21 import javax.xml.stream.FactoryConfigurationError;
22 import javax.xml.stream.XMLOutputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import org.opendaylight.controller.sal.rest.api.Draft02;
26 import org.opendaylight.controller.sal.rest.api.RestconfService;
27 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
37 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
45 Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
46 public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
48 private static final XMLOutputFactory XML_FACTORY;
51 XML_FACTORY = XMLOutputFactory.newFactory();
52 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
56 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
57 final MediaType mediaType) {
58 return type.equals(NormalizedNodeContext.class);
62 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
63 final Annotation[] annotations, final MediaType mediaType) {
68 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType,
69 final Annotation[] annotations, final MediaType mediaType,
70 final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
71 WebApplicationException {
72 final InstanceIdentifierContext<?> pathContext = t.getInstanceIdentifierContext();
73 if (t.getData() == null) {
77 XMLStreamWriter xmlWriter;
79 xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream);
80 } catch (final XMLStreamException e) {
81 throw new IllegalStateException(e);
82 } catch (final FactoryConfigurationError e) {
83 throw new IllegalStateException(e);
85 NormalizedNode<?, ?> data = t.getData();
86 SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
88 boolean isDataRoot = false;
89 if (SchemaPath.ROOT.equals(schemaPath)) {
91 } else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
93 schemaPath = ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath();
95 schemaPath = schemaPath.getParent();
98 final NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
99 pathContext.getSchemaContext(), schemaPath);
100 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
102 writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
104 if (data instanceof MapEntryNode) {
105 // Restconf allows returning one list item. We need to wrap it
106 // in map node in order to serialize it properly
107 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
109 nnWriter.write(data);
114 private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data)
117 final QName name = SchemaContext.NAME;
118 xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
119 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
120 nnWriter.write(child);
123 xmlWriter.writeEndElement();
125 } catch (final XMLStreamException e) {
126 Throwables.propagate(e);