/**
* @deprecated This class will be replaced by
- * {@link org.opendaylight.restconf.utils.patch.AbstractIdentifierAwareJaxRsProvider}
+ * {@link org.opendaylight.restconf.jersey.providers.AbstractIdentifierAwareJaxRsProvider}
*/
@Deprecated
public class AbstractIdentifierAwareJaxRsProvider {
protected boolean isPost() {
return POST.equals(this.request.getMethod());
}
+
+ Request getRequest() {
+ return this.request;
+ }
}
final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
WebApplicationException {
try {
- return readFrom(getInstanceIdentifierContext(), entityStream, isPost());
+ if(getUriInfo().getAbsolutePath().getPath().contains("restconf/16")){
+ final org.opendaylight.restconf.jersey.providers.JsonNormalizedNodeBodyReader jsonReaderNewRest =
+ new org.opendaylight.restconf.jersey.providers.JsonNormalizedNodeBodyReader();
+ jsonReaderNewRest.injectParams(getUriInfo(), getRequest());
+ return jsonReaderNewRest.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
+ } else {
+ return readFrom(getInstanceIdentifierContext(), entityStream, isPost());
+ }
} catch (final Exception e) {
propagateExceptionAs(e);
return null; // no-op
/**
* @deprecated This class will be replaced by
- * {@link org.opendaylight.restconf.utils.patch.JsonToPATCHBodyReader}
+ * {@link org.opendaylight.restconf.jersey.providers.JsonToPATCHBodyReader}
*/
@Deprecated
@Provider
/**
* @deprecated This class will be replaced by
- * {@link org.opendaylight.restconf.utils.patch.StringModuleInstanceIdentifierCodec}
+ * {@link org.opendaylight.restconf.jersey.providers.StringModuleInstanceIdentifierCodec}
*/
@Deprecated
public final class StringModuleInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec {
import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
-import org.opendaylight.restconf.Draft17;
-import org.opendaylight.restconf.utils.RestconfConstants;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
@Provider
@Consumes({ Draft02.MediaTypes.DATA + RestconfService.XML, Draft02.MediaTypes.OPERATION + RestconfService.XML,
- Draft17.MediaTypes.DATA + RestconfConstants.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
+ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public class XmlNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
private final static Logger LOG = LoggerFactory.getLogger(XmlNormalizedNodeBodyReader.class);
final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
WebApplicationException {
try {
- final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
-
- if (entityStream.available() < 1) {
- // represent empty nopayload input
- return new NormalizedNodeContext(path, null);
- }
-
- final DocumentBuilder dBuilder;
- try {
- dBuilder = BUILDERFACTORY.newDocumentBuilder();
- } catch (final ParserConfigurationException e) {
- throw new RuntimeException("Failed to parse XML document", e);
+ if (getUriInfo().getAbsolutePath().getPath().contains("restconf/16")) {
+ final org.opendaylight.restconf.jersey.providers.XmlNormalizedNodeBodyReader xmlReaderNewRest =
+ new org.opendaylight.restconf.jersey.providers.XmlNormalizedNodeBodyReader();
+ xmlReaderNewRest.injectParams(getUriInfo(), getRequest());
+ return xmlReaderNewRest.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
+ } else {
+ return readFrom(entityStream);
}
- final Document doc = dBuilder.parse(entityStream);
-
- return parse(path,doc);
} catch (final RestconfDocumentedException e){
throw e;
} catch (final Exception e) {
}
}
+ private NormalizedNodeContext readFrom(final InputStream entityStream) throws IOException, SAXException {
+ final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
+
+ if (entityStream.available() < 1) {
+ // represent empty nopayload input
+ return new NormalizedNodeContext(path, null);
+ }
+
+ final DocumentBuilder dBuilder;
+ try {
+ dBuilder = BUILDERFACTORY.newDocumentBuilder();
+ } catch (final ParserConfigurationException e) {
+ throw new RuntimeException("Failed to parse XML document", e);
+ }
+ final Document doc = dBuilder.parse(entityStream);
+
+ return parse(path,doc);
+ }
+
private NormalizedNodeContext parse(final InstanceIdentifierContext<?> pathContext,final Document doc) {
final List<Element> elements = Collections.singletonList(doc.getDocumentElement());
/**
* @deprecated This class will be replaced by
- * {@link org.opendaylight.restconf.utils.patch.XmlToPATCHBodyReader}
+ * {@link org.opendaylight.restconf.jersey.providers.XmlToPATCHBodyReader}
*/
@Deprecated
@Provider
import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
import org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl;
-import org.opendaylight.restconf.utils.patch.JsonToPATCHBodyReader;
-import org.opendaylight.restconf.utils.patch.XmlToPATCHBodyReader;
+import org.opendaylight.restconf.jersey.providers.JsonToPATCHBodyReader;
+import org.opendaylight.restconf.jersey.providers.XmlToPATCHBodyReader;
public class RestconfApplication extends Application {
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.restconf.utils.patch;
+package org.opendaylight.restconf.jersey.providers;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
protected boolean isPost() {
return POST.equals(this.request.getMethod());
}
+
+ void setUriInfo(final UriInfo uriInfo) {
+ this.uriInfo = uriInfo;
+ }
+
+ void setRequest(final Request request) {
+ this.request = request;
+ }
}
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.restconf.jersey.providers;
+
+import com.google.common.collect.Iterables;
+import com.google.gson.stream.JsonReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.Provider;
+import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
+import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.restconf.Draft17;
+import org.opendaylight.restconf.utils.RestconfConstants;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
+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.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
+import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Provider
+@Consumes({ Draft17.MediaTypes.DATA + RestconfConstants.JSON, MediaType.APPLICATION_JSON })
+public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
+
+ private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
+
+ @Override
+ public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
+ final MediaType mediaType) {
+ return true;
+ }
+
+ @Override
+ public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
+ final Annotation[] annotations, final MediaType mediaType,
+ final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
+ WebApplicationException {
+ try {
+ return readFrom(getInstanceIdentifierContext(), entityStream, isPost());
+ } catch (final Exception e) {
+ propagateExceptionAs(e);
+ return null;
+ }
+ }
+
+ private static void propagateExceptionAs(final Exception e) throws RestconfDocumentedException {
+ if(e instanceof RestconfDocumentedException) {
+ throw (RestconfDocumentedException)e;
+ }
+
+ if(e instanceof ResultAlreadySetException) {
+ LOG.debug("Error parsing json input:", e);
+
+ throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. " +
+ "Are you creating multiple resources/subresources in POST request?");
+ }
+
+ LOG.debug("Error parsing json input", e);
+
+ throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
+ ErrorTag.MALFORMED_MESSAGE, e);
+ }
+
+ private static NormalizedNodeContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream,
+ final boolean isPost) throws IOException {
+ if (entityStream.available() < 1) {
+ return new NormalizedNodeContext(path, null);
+ }
+ final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
+ final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
+
+ final SchemaNode parentSchema;
+ if(isPost) {
+ parentSchema = path.getSchemaNode();
+ } else if(path.getSchemaNode() instanceof SchemaContext) {
+ parentSchema = path.getSchemaContext();
+ } else {
+ if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
+ parentSchema = path.getSchemaContext();
+ } else {
+ parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
+ }
+ }
+
+ final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
+ final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
+ jsonParser.parse(reader);
+
+ NormalizedNode<?, ?> result = resultHolder.getResult();
+ final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
+ InstanceIdentifierContext<? extends SchemaNode> newIIContext;
+
+ while ((result instanceof AugmentationNode) || (result instanceof ChoiceNode)) {
+ final Object childNode = ((DataContainerNode) result).getValue().iterator().next();
+ if (isPost) {
+ iiToDataList.add(result.getIdentifier());
+ }
+ result = (NormalizedNode<?, ?>) childNode;
+ }
+
+ if (isPost) {
+ if (result instanceof MapEntryNode) {
+ iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
+ iiToDataList.add(result.getIdentifier());
+ } else {
+ iiToDataList.add(result.getIdentifier());
+ }
+ } else {
+ if (result instanceof MapNode) {
+ result = Iterables.getOnlyElement(((MapNode) result).getValue());
+ }
+ }
+
+ final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
+ path.getInstanceIdentifier().getPathArguments(), iiToDataList));
+
+ newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
+ path.getSchemaContext());
+
+ return new NormalizedNodeContext(newIIContext, result);
+ }
+
+ public void injectParams(final UriInfo uriInfo, final Request request) {
+ setUriInfo(uriInfo);
+ setRequest(request);
+ }
+}
+
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.restconf.utils.patch;
+package org.opendaylight.restconf.jersey.providers;
import static org.opendaylight.netconf.sal.restconf.impl.PATCHEditOperation.isPatchOperationWithValue;
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.restconf.utils.patch;
+package org.opendaylight.restconf.jersey.providers;
import com.google.common.base.Preconditions;
import java.net.URI;
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.restconf.jersey.providers;
+
+import com.google.common.collect.Iterables;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.List;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.Provider;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
+import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.restconf.Draft17;
+import org.opendaylight.restconf.utils.RestconfConstants;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
+import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
+import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+@Provider
+@Consumes({ Draft17.MediaTypes.DATA + RestconfConstants.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
+public class XmlNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
+
+ private final static Logger LOG = LoggerFactory.getLogger(XmlNormalizedNodeBodyReader.class);
+ private static final DocumentBuilderFactory BUILDERFACTORY;
+
+ static {
+ final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ try {
+ factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
+ } catch (final ParserConfigurationException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ factory.setNamespaceAware(true);
+ factory.setCoalescing(true);
+ factory.setIgnoringElementContentWhitespace(true);
+ factory.setIgnoringComments(true);
+ BUILDERFACTORY = factory;
+ }
+
+ @Override
+ public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
+ final MediaType mediaType) {
+ return true;
+ }
+
+ @Override
+ public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
+ final Annotation[] annotations, final MediaType mediaType,
+ final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
+ WebApplicationException {
+ try {
+ final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
+
+ if (entityStream.available() < 1) {
+ // represent empty nopayload input
+ return new NormalizedNodeContext(path, null);
+ }
+
+ final DocumentBuilder dBuilder;
+ try {
+ dBuilder = BUILDERFACTORY.newDocumentBuilder();
+ } catch (final ParserConfigurationException e) {
+ throw new RuntimeException("Failed to parse XML document", e);
+ }
+ final Document doc = dBuilder.parse(entityStream);
+
+ return parse(path,doc);
+ } catch (final RestconfDocumentedException e){
+ throw e;
+ } catch (final Exception e) {
+ LOG.debug("Error parsing xml input", e);
+
+ throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
+ ErrorTag.MALFORMED_MESSAGE);
+ }
+ }
+
+ private NormalizedNodeContext parse(final InstanceIdentifierContext<?> pathContext,final Document doc) {
+
+ final List<Element> elements = Collections.singletonList(doc.getDocumentElement());
+ final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
+ DataSchemaNode schemaNode;
+ boolean isRpc = false;
+ if (schemaNodeContext instanceof RpcDefinition) {
+ schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
+ isRpc = true;
+ } else if (schemaNodeContext instanceof DataSchemaNode) {
+ schemaNode = (DataSchemaNode) schemaNodeContext;
+ } else {
+ throw new IllegalStateException("Unknown SchemaNode");
+ }
+
+ final String docRootElm = doc.getDocumentElement().getLocalName();
+ final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
+ final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
+ InstanceIdentifierContext<? extends SchemaNode> outIIContext;
+
+
+ final DomToNormalizedNodeParserFactory parserFactory =
+ DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, pathContext.getSchemaContext());
+
+ if (isPost() && !isRpc) {
+ final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm, docRootNamespace);
+ if (foundSchemaNodes.isEmpty()) {
+ throw new IllegalStateException(String.format("Child \"%s\" was not found in parent schema node \"%s\"",
+ docRootElm, schemaNode.getQName()));
+ }
+ while (!foundSchemaNodes.isEmpty()) {
+ final Object child = foundSchemaNodes.pop();
+ if (child instanceof AugmentationSchema) {
+ final AugmentationSchema augmentSchemaNode = (AugmentationSchema) child;
+ iiToDataList.add(SchemaUtils.getNodeIdentifierForAugmentation(augmentSchemaNode));
+ } else if (child instanceof DataSchemaNode) {
+ schemaNode = (DataSchemaNode) child;
+ iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(schemaNode.getQName()));
+ }
+ }
+ }
+
+ NormalizedNode<?, ?> parsed = null;
+
+ if (schemaNode instanceof ContainerSchemaNode) {
+ parsed = parserFactory.getContainerNodeParser().parse(Collections.singletonList(doc.getDocumentElement()), (ContainerSchemaNode) schemaNode);
+ } else if(schemaNode instanceof ListSchemaNode) {
+ final ListSchemaNode casted = (ListSchemaNode) schemaNode;
+ parsed = parserFactory.getMapEntryNodeParser().parse(elements, casted);
+ if (isPost()) {
+ iiToDataList.add(parsed.getIdentifier());
+ }
+ }
+
+ final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
+ pathContext.getInstanceIdentifier().getPathArguments(), iiToDataList));
+
+ outIIContext = new InstanceIdentifierContext<>(fullIIToData, pathContext.getSchemaNode(), pathContext.getMountPoint(),
+ pathContext.getSchemaContext());
+
+ return new NormalizedNodeContext(outIIContext, parsed);
+ }
+
+ private static Deque<Object> findPathToSchemaNodeByName(final DataSchemaNode schemaNode, final String elementName,
+ final String namespace) {
+ final Deque<Object> result = new ArrayDeque<>();
+ final ArrayList<ChoiceSchemaNode> choiceSchemaNodes = new ArrayList<>();
+ final Collection<DataSchemaNode> children = ((DataNodeContainer) schemaNode).getChildNodes();
+ for (final DataSchemaNode child : children) {
+ if (child instanceof ChoiceSchemaNode) {
+ choiceSchemaNodes.add((ChoiceSchemaNode) child);
+ } else if (child.getQName().getLocalName().equalsIgnoreCase(elementName)
+ && child.getQName().getNamespace().toString().equalsIgnoreCase(namespace)) {
+ // add child to result
+ result.push(child);
+
+ // find augmentation
+ if (child.isAugmenting()) {
+ final AugmentationSchema augment = findCorrespondingAugment(schemaNode, child);
+ if (augment != null) {
+ result.push(augment);
+ }
+ }
+
+ // return result
+ return result;
+ }
+ }
+
+ for (final ChoiceSchemaNode choiceNode : choiceSchemaNodes) {
+ for (final ChoiceCaseNode caseNode : choiceNode.getCases()) {
+ final Deque<Object> resultFromRecursion = findPathToSchemaNodeByName(caseNode, elementName, namespace);
+ if (!resultFromRecursion.isEmpty()) {
+ resultFromRecursion.push(choiceNode);
+ if (choiceNode.isAugmenting()) {
+ final AugmentationSchema augment = findCorrespondingAugment(schemaNode, choiceNode);
+ if (augment != null) {
+ resultFromRecursion.push(augment);
+ }
+ }
+ return resultFromRecursion;
+ }
+ }
+ }
+ return result;
+ }
+
+ private static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
+ if ((parent instanceof AugmentationTarget) && !(parent instanceof ChoiceSchemaNode)) {
+ for (final AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
+ final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
+ if (childInAugmentation != null) {
+ return augmentation;
+ }
+ }
+ }
+ return null;
+ }
+
+ public void injectParams(final UriInfo uriInfo, final Request request) {
+ setRequest(request);
+ setUriInfo(uriInfo);
+ }
+}
+
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.restconf.utils.patch;
+package org.opendaylight.restconf.jersey.providers;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-
import java.lang.reflect.Field;
+import java.net.URI;
import java.util.Collections;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
requestField = AbstractIdentifierAwareJaxRsProvider.class
.getDeclaredField("request");
requestField.setAccessible(true);
- mediaType = getMediaType();
+ this.mediaType = getMediaType();
}
protected abstract MediaType getMediaType();
return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
}
- protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
- final String identifier, final T normalizedNodeProvider,
- final boolean isPost) throws NoSuchFieldException,
- SecurityException, IllegalArgumentException, IllegalAccessException {
+ protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(final String identifier,
+ final T normalizedNodeProvider, final boolean isPost) throws Exception {
final UriInfo uriInfoMock = mock(UriInfo.class);
final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
+ when(uriInfoMock.getAbsolutePath()).thenReturn(new URI("restconf"));
uriField.set(normalizedNodeProvider, uriInfoMock);
final Request request = mock(Request.class);
import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
-import org.opendaylight.restconf.utils.patch.AbstractIdentifierAwareJaxRsProvider;
+import org.opendaylight.restconf.jersey.providers.AbstractIdentifierAwareJaxRsProvider;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-public abstract class Draft11AbstractBodyReaderTest {
+public abstract class Draft17AbstractBodyReaderTest {
protected final static ControllerContext controllerContext = ControllerContext.getInstance();
protected final MediaType mediaType;
private static Field uriField;
private static Field requestField;
- public Draft11AbstractBodyReaderTest() throws NoSuchFieldException,
+ public Draft17AbstractBodyReaderTest() throws NoSuchFieldException,
SecurityException {
uriField = AbstractIdentifierAwareJaxRsProvider.class
.getDeclaredField("uriInfo");
import org.junit.Test;
import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
-import org.opendaylight.restconf.utils.patch.JsonToPATCHBodyReader;
+import org.opendaylight.restconf.jersey.providers.JsonToPATCHBodyReader;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-public class TestDraft11JsonPATCHBodyReader extends Draft11AbstractBodyReaderTest {
+public class TestDraft17JsonPATCHBodyReader extends Draft17AbstractBodyReaderTest {
private final JsonToPATCHBodyReader jsonPATCHBodyReader;
private static SchemaContext schemaContext;
- public TestDraft11JsonPATCHBodyReader() throws NoSuchFieldException, SecurityException {
+ public TestDraft17JsonPATCHBodyReader() throws NoSuchFieldException, SecurityException {
super();
jsonPATCHBodyReader = new JsonToPATCHBodyReader();
}
import org.junit.Test;
import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
-import org.opendaylight.restconf.utils.patch.XmlToPATCHBodyReader;
+import org.opendaylight.restconf.jersey.providers.XmlToPATCHBodyReader;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-public class TestDraft11XmlPATCHBodyReader extends Draft11AbstractBodyReaderTest {
+public class TestDraft17XmlPATCHBodyReader extends Draft17AbstractBodyReaderTest {
private final XmlToPATCHBodyReader xmlPATCHBodyReader;
private static SchemaContext schemaContext;
- public TestDraft11XmlPATCHBodyReader() throws NoSuchFieldException, SecurityException {
+ public TestDraft17XmlPATCHBodyReader() throws NoSuchFieldException, SecurityException {
super();
xmlPATCHBodyReader = new XmlToPATCHBodyReader();
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import java.io.IOException;
import java.io.InputStream;
-
-import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
-
import org.junit.BeforeClass;
import org.junit.Test;
-import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
+import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
public JsonIdentityrefToNnTest() throws NoSuchFieldException,
SecurityException {
super();
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
}
@BeforeClass
}
@Test
- public void jsonIdentityrefToNn() throws NoSuchFieldException,
- SecurityException, IllegalArgumentException,
- IllegalAccessException, WebApplicationException, IOException {
+ public void jsonIdentityrefToNn() throws Exception {
- String uri = "identityref-module:cont";
- mockBodyReader(uri, jsonBodyReader, false);
- InputStream inputStream = this.getClass().getResourceAsStream(
+ final String uri = "identityref-module:cont";
+ mockBodyReader(uri, this.jsonBodyReader, false);
+ final InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/identityref/json/data.json");
- NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom(
- null, null, null, mediaType, null, inputStream);
+ final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
+ null, null, null, this.mediaType, null, inputStream);
assertEquals("cont", normalizedNodeContext.getData().getNodeType()
.getLocalName());
- String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
.getData());
assertTrue(dataTree.contains("cont1"));
@Override
protected MediaType getMediaType() {
- // TODO Auto-generated method stub
return null;
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
import java.io.InputStream;
-
-import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
-
import org.junit.BeforeClass;
import org.junit.Test;
-import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
+import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
public JsonLeafrefToNnTest() throws NoSuchFieldException, SecurityException {
super();
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
}
@BeforeClass
}
@Test
- public void jsonIdentityrefToNormalizeNode() throws NoSuchFieldException,
- SecurityException, IllegalArgumentException,
- IllegalAccessException, WebApplicationException, IOException {
+ public void jsonIdentityrefToNormalizeNode() throws Exception {
- String uri = "leafref-module:cont";
- mockBodyReader(uri, jsonBodyReader, false);
- InputStream inputStream = this.getClass().getResourceAsStream(
+ final String uri = "leafref-module:cont";
+ mockBodyReader(uri, this.jsonBodyReader, false);
+ final InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/leafref/json/data.json");
- NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom(
- null, null, null, mediaType, null, inputStream);
+ final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
+ null, null, null, this.mediaType, null, inputStream);
assertEquals("cont", normalizedNodeContext.getData().getNodeType()
.getLocalName());
- String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
.getData());
assertTrue(dataTree.contains("lf2 121"));
}
@Override
protected MediaType getMediaType() {
- // TODO Auto-generated method stub
return null;
}
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-
import java.io.IOException;
import java.io.InputStream;
-
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
-
-import org.junit.Ignore;
import org.junit.Test;
-import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
+import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
controllerContext.setSchemas(schemaContext);
}
- //TODO unignore once yangtools bug is fixed
@Test
- @Ignore
- public void simpleListTest() {
+ public void simpleListTest() throws Exception {
simpleTest("/json-to-nn/simple-list.json",
"/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
}
@Test
- public void simpleContainerTest() {
+ public void simpleContainerTest() throws Exception {
simpleTest("/json-to-nn/simple-container.json",
"/json-to-nn/simple-container-yang", "cont",
"simple-container-yang");
}
@Test
- public void multipleItemsInLeafListTest() {
+ public void multipleItemsInLeafListTest() throws Exception {
- initialize("/json-to-nn/simple-list-yang/1", schemaContext);
+ initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/multiple-leaflist-items.json",
}
@Test
- public void multipleItemsInListTest() {
- initialize("/json-to-nn/simple-list-yang/3", schemaContext);
+ public void multipleItemsInListTest() throws Exception {
+ initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/multiple-items-in-list.json",
}
@Test
- public void nullArrayToSimpleNodeWithNullValueTest() {
- initialize("/json-to-nn/simple-list-yang/4", schemaContext);
+ public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
+ initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
}
@Test
- public void incorrectTopLevelElementsTest() throws WebApplicationException,
- IOException, NoSuchFieldException, SecurityException,
- IllegalArgumentException, IllegalAccessException {
+ public void incorrectTopLevelElementsTest() throws Exception {
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
- initialize("/json-to-nn/simple-list-yang/1", schemaContext);
- mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
+ mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/wrong-top-level1.json");
RestconfDocumentedException exception = null;
try {
- jsonBodyReader.readFrom(null, null, null, mediaType, null,
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
"/json-to-nn/wrong-top-level2.json");
exception = null;
try {
- jsonBodyReader.readFrom(null, null, null, mediaType, null,
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
"/json-to-nn/wrong-top-level3.json");
exception = null;
try {
- jsonBodyReader.readFrom(null, null, null, mediaType, null,
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
}
@Test
- public void emptyDataReadTest() throws WebApplicationException,
- IOException, NoSuchFieldException, SecurityException,
- IllegalArgumentException, IllegalAccessException {
+ public void emptyDataReadTest() throws Exception {
- initialize("/json-to-nn/simple-list-yang/4", schemaContext);
+ initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/empty-data.json", "array-with-null-yang:cont");
assertTrue(dataTree.contains("lflst2 45"));
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
RestconfDocumentedException exception = null;
- mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
+ mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
final InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/empty-data.json1");
try {
- jsonBodyReader.readFrom(null, null, null, mediaType, null,
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
}
@Test
- public void testJsonBlankInput() throws NoSuchFieldException,
- SecurityException, IllegalArgumentException,
- IllegalAccessException, WebApplicationException, IOException {
- initialize("/json-to-nn/simple-list-yang/4", schemaContext);
+ public void testJsonBlankInput() throws Exception {
+ initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
"array-with-null-yang:cont");
assertNull(normalizedNodeContext);
}
- //TODO unignore once yangtools bug is fixed
@Test
- @Ignore
- public void notSupplyNamespaceIfAlreadySupplied()
- throws WebApplicationException, IOException, NoSuchFieldException,
- SecurityException, IllegalArgumentException, IllegalAccessException {
+ public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
- initialize("/json-to-nn/simple-list-yang/1", schemaContext);
+ initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
final String uri = "simple-list-yang1" + ":" + "lst";
verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
- mockBodyReader("simple-list-yang2:lst", jsonBodyReader, false);
+ mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
final InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/simple-list.json");
try {
- jsonBodyReader.readFrom(null, null, null, mediaType, null,
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
inputStream);
fail("NormalizedNodeContext should not be create because of different namespace");
} catch (final RestconfDocumentedException e) {
}
@Test
- public void dataAugmentedTest() {
+ public void dataAugmentedTest() throws Exception {
- initialize("/common/augment/yang", schemaContext);
+ initialize("/common/augment/yang", this.schemaContext);
NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/common/augment/json/dataa.json", "main:cont");
}
private void simpleTest(final String jsonPath, final String yangPath,
- final String topLevelElementName, final String moduleName) {
+ final String topLevelElementName, final String moduleName) throws Exception {
- initialize(yangPath, schemaContext);
+ initialize(yangPath, this.schemaContext);
final String uri = moduleName + ":" + topLevelElementName;
verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
}
- private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) {
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
try {
- mockBodyReader(uri, jsonBodyReader, false);
+ mockBodyReader(uri, this.jsonBodyReader, false);
} catch (NoSuchFieldException | SecurityException
| IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
NormalizedNodeContext normalizedNodeContext = null;
try {
- normalizedNodeContext = jsonBodyReader.readFrom(null, null, null,
- mediaType, null, inputStream);
+ normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
+ this.mediaType, null, inputStream);
} catch (WebApplicationException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@Test
- public void unsupportedDataFormatTest() throws NoSuchFieldException,
- SecurityException, IllegalArgumentException,
- IllegalAccessException, WebApplicationException, IOException {
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
- initialize("/json-to-nn/simple-list-yang/1", schemaContext);
- mockBodyReader("simple-list-yang1:lst", jsonBodyReader, false);
+ public void unsupportedDataFormatTest() throws Exception {
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
+ mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
final InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/unsupported-json-format.json");
RestconfDocumentedException exception = null;
try {
- jsonBodyReader.readFrom(null, null, null, mediaType, null,
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
}
@Test
- public void invalidUriCharacterInValue() throws NoSuchFieldException,
- SecurityException, IllegalArgumentException,
- IllegalAccessException, WebApplicationException, IOException {
+ public void invalidUriCharacterInValue() throws Exception {
- jsonBodyReader = new JsonNormalizedNodeBodyReader();
- initialize("/json-to-nn/simple-list-yang/4", schemaContext);
- mockBodyReader("array-with-null-yang:cont", jsonBodyReader, false);
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
+ initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
+ mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
final InputStream inputStream = this.getClass().getResourceAsStream(
"/json-to-nn/invalid-uri-character-in-value.json");
- final NormalizedNodeContext normalizedNodeContext = jsonBodyReader.readFrom(
- null, null, null, mediaType, null, inputStream);
+ final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
+ null, null, null, this.mediaType, null, inputStream);
assertNotNull(normalizedNodeContext);
assertEquals("cont", normalizedNodeContext.getData().getNodeType()
@Override
protected MediaType getMediaType() {
- // TODO Auto-generated method stub
return null;
}