X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Frestconf-nb-bierman02%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Frest%2Fimpl%2FRestconfDocumentedExceptionMapper.java;h=1803a4b3ba20bedb649d3e1c6e5f6ac016db1309;hb=07315276858f9eeff0760b14ca87a25dc81041ed;hp=3828abd6a70a20bb1e4114553473bc8caee9f2ec;hpb=956285fc6f3ffeabd828f164700e7f997eda2faa;p=netconf.git diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java index 3828abd6a7..1803a4b3ba 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java @@ -5,18 +5,17 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netconf.sal.rest.impl; -import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.Iterables; import com.google.gson.stream.JsonWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; -import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -24,6 +23,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; import javax.xml.XMLConstants; @@ -33,19 +33,21 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.restconf.common.ErrorTags; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; -import org.opendaylight.restconf.common.context.NormalizedNodeContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.errors.RestconfError; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; -import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode; +import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder; +import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; @@ -53,10 +55,8 @@ import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; @@ -91,7 +91,7 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper errContBuild = - Builders.containerBuilder((ContainerSchemaNode) errorsSchemaNode); + checkState(errorsSchemaNode instanceof ContainerSchemaNode, "Found Errors SchemaNode isn't ContainerNode"); + final DataContainerNodeBuilder errContBuild = + SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) errorsSchemaNode); final List schemaList = ControllerContext.findInstanceDataChildrenByName(errorsSchemaNode, Draft02.RestConfModule.ERROR_LIST_SCHEMA_NODE); final DataSchemaNode errListSchemaNode = Iterables.getFirst(schemaList, null); - Preconditions.checkState( - errListSchemaNode instanceof ListSchemaNode, "Found Error SchemaNode isn't ListSchemaNode"); - final CollectionNodeBuilder listErorsBuilder = Builders + checkState(errListSchemaNode instanceof ListSchemaNode, "Found Error SchemaNode isn't ListSchemaNode"); + final CollectionNodeBuilder listErorsBuilder = SchemaAwareBuilders .mapBuilder((ListSchemaNode) errListSchemaNode); @@ -147,8 +143,8 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper(null, - (DataSchemaNode) errorsSchemaNode, null, controllerContext.getGlobalSchema()), errContBuild.build()); + final NormalizedNodeContext errContext = new NormalizedNodeContext(InstanceIdentifierContext.ofDataSchemaNode( + controllerContext.getGlobalSchema(), (DataSchemaNode) errorsSchemaNode), errContBuild.build()); Object responseBody; if (mediaType.getSubtype().endsWith("json")) { @@ -161,40 +157,40 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper errNodeValues = Builders + final DataContainerNodeBuilder errNodeValues = SchemaAwareBuilders .mapEntryBuilder(listStreamSchemaNode); List lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName( listStreamSchemaNode, "error-type"); final DataSchemaNode errTypSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null); - Preconditions.checkState(errTypSchemaNode instanceof LeafSchemaNode); - errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errTypSchemaNode) - .withValue(error.getErrorType().getErrorTypeTag()).build()); + checkState(errTypSchemaNode instanceof LeafSchemaNode); + errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errTypSchemaNode) + .withValue(error.getErrorType().elementBody()).build()); lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName( listStreamSchemaNode, "error-tag"); final DataSchemaNode errTagSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null); - Preconditions.checkState(errTagSchemaNode instanceof LeafSchemaNode); - errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errTagSchemaNode) - .withValue(error.getErrorTag().getTagValue()).build()); + checkState(errTagSchemaNode instanceof LeafSchemaNode); + errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errTagSchemaNode) + .withValue(error.getErrorTag().elementBody()).build()); if (error.getErrorAppTag() != null) { lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName( listStreamSchemaNode, "error-app-tag"); final DataSchemaNode errAppTagSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null); - Preconditions.checkState(errAppTagSchemaNode instanceof LeafSchemaNode); - errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errAppTagSchemaNode) + checkState(errAppTagSchemaNode instanceof LeafSchemaNode); + errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errAppTagSchemaNode) .withValue(error.getErrorAppTag()).build()); } lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName( listStreamSchemaNode, "error-message"); final DataSchemaNode errMsgSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null); - Preconditions.checkState(errMsgSchemaNode instanceof LeafSchemaNode); - errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errMsgSchemaNode) + checkState(errMsgSchemaNode instanceof LeafSchemaNode); + errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errMsgSchemaNode) .withValue(error.getErrorMessage()).build()); if (error.getErrorInfo() != null) { @@ -213,8 +209,8 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper data = errorsNode.getData(); - final InstanceIdentifierContext context = errorsNode.getInstanceIdentifierContext(); + NormalizedNode data = errorsNode.getData(); + final InstanceIdentifierContext context = errorsNode.getInstanceIdentifierContext(); final DataSchemaNode schema = (DataSchemaNode) context.getSchemaNode(); SchemaPath path = context.getSchemaNode().getPath(); @@ -224,7 +220,7 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper pathContext = errorsNode.getInstanceIdentifierContext(); + final InstanceIdentifierContext pathContext = errorsNode.getInstanceIdentifierContext(); final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); final XMLStreamWriter xmlWriter; try { xmlWriter = XML_FACTORY.createXMLStreamWriter(outStream, StandardCharsets.UTF_8.name()); - } catch (final XMLStreamException e) { - throw new IllegalStateException(e); - } catch (final FactoryConfigurationError e) { + } catch (final XMLStreamException | FactoryConfigurationError e) { throw new IllegalStateException(e); } - NormalizedNode data = errorsNode.getData(); + NormalizedNode data = errorsNode.getData(); SchemaPath schemaPath = pathContext.getSchemaNode().getPath(); boolean isDataRoot = false; @@ -318,25 +335,53 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper child : data.getValue()) { + for (final DataContainerChild child : data.body()) { nnWriter.write(child); } nnWriter.flush(); xmlWriter.writeEndElement(); xmlWriter.flush(); } catch (final XMLStreamException e) { - Throwables.propagate(e); + throw new IOException("Failed to write elements", e); } } private static void writeDataRoot(final OutputStreamWriter outputWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException { - for (final DataContainerChild child : data.getValue()) { + for (final DataContainerChild child : data.body()) { nnWriter.write(child); nnWriter.flush(); }