X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fimpl%2FNormalizedNodeXmlBodyWriter.java;h=c8c702295d845059aa1498cb68e1d66f5ee4427f;hp=bab26dfc012b1a620f2e0a619ef3d93d10b0bb2e;hb=874a18a9ce5dc09bc49922754bf8fb3e981fffb9;hpb=3d7c2897555bff941853f29093ba5ab8c811a421 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java index bab26dfc01..c8c702295d 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java @@ -8,6 +8,7 @@ package org.opendaylight.controller.sal.rest.impl; import com.google.common.base.Throwables; +import com.google.common.collect.Iterables; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; @@ -26,9 +27,6 @@ import org.opendaylight.controller.sal.rest.api.Draft02; import org.opendaylight.controller.sal.rest.api.RestconfService; import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext; import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext; -import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException; -import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag; -import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; @@ -41,6 +39,7 @@ import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNo import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; @Provider @Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML, @@ -71,36 +70,45 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException { - InstanceIdentifierContext pathContext = t.getInstanceIdentifierContext(); + final InstanceIdentifierContext pathContext = t.getInstanceIdentifierContext(); if (t.getData() == null) { - throw new RestconfDocumentedException( - "Request could not be completed because the relevant data model content does not exist.", - ErrorType.APPLICATION, ErrorTag.DATA_MISSING); + return; } XMLStreamWriter xmlWriter; try { xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream); - } catch (XMLStreamException e) { + } catch (final XMLStreamException e) { throw new IllegalStateException(e); - } catch (FactoryConfigurationError e) { + } catch (final FactoryConfigurationError e) { throw new IllegalStateException(e); } NormalizedNode data = t.getData(); SchemaPath schemaPath = pathContext.getSchemaNode().getPath(); + // The utility method requires the path to be size of 2 + boolean isRpc = false; + if(Iterables.size(schemaPath.getPathFromRoot()) > 1) { + isRpc = SchemaContextUtil.getRpcDataSchema(t.getInstanceIdentifierContext().getSchemaContext(), schemaPath) != null; + } + boolean isDataRoot = false; if (SchemaPath.ROOT.equals(schemaPath)) { isDataRoot = true; + // The rpc definitions required the schema path to point to the output container, not the parent (rpc itself) } else { - schemaPath = schemaPath.getParent(); + if(!isRpc) { + schemaPath = schemaPath.getParent(); + } } - NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, + final NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, pathContext.getSchemaContext(), schemaPath); - NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter); + final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter); if (isDataRoot) { - writeRootElement(xmlWriter, nnWriter, (ContainerNode) data); + writeRootElement(xmlWriter, nnWriter, (ContainerNode) data, SchemaContext.NAME); + } else if(isRpc) { + writeRootElement(xmlWriter, nnWriter, (ContainerNode) data, schemaPath.getLastComponent()); } else { if (data instanceof MapEntryNode) { // Restconf allows returning one list item. We need to wrap it @@ -112,18 +120,17 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter child : data.getValue()) { + for (final DataContainerChild child : data.getValue()) { nnWriter.write(child); } nnWriter.flush(); xmlWriter.writeEndElement(); xmlWriter.flush(); - } catch (XMLStreamException e) { + } catch (final XMLStreamException e) { Throwables.propagate(e); } }