Search in sources :

Example 31 with PatchContext

use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.

the class XmlPatchBodyReaderMountPointTest method moduleDataTest.

@Test
public void moduleDataTest() throws Exception {
    final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
    mockBodyReader(uri, xmlToPatchBodyReader, false);
    final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
    final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
    checkPatchContextMountPoint(returnValue);
}
Also used : PatchContext(org.opendaylight.restconf.common.patch.PatchContext) InputStream(java.io.InputStream) AbstractBodyReaderTest(org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest) XmlBodyReaderTest(org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest) Test(org.junit.Test)

Example 32 with PatchContext

use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.

the class BrokerFacadeTest method testPatchConfigurationDataWithinTransactionServer.

/**
 * Test Patch method on the server with no data.
 */
@Test
public void testPatchConfigurationDataWithinTransactionServer() throws Exception {
    final PatchContext patchContext = mock(PatchContext.class);
    final InstanceIdentifierContext<?> identifierContext = mock(InstanceIdentifierContext.class);
    when(patchContext.getData()).thenReturn(new ArrayList<>());
    doReturn(identifierContext).when(patchContext).getInstanceIdentifierContext();
    // no mount point
    when(identifierContext.getMountPoint()).thenReturn(null);
    doReturn(CommitInfo.emptyFluentFuture()).when(this.rwTransaction).commit();
    final PatchStatusContext status = this.brokerFacade.patchConfigurationDataWithinTransaction(patchContext);
    // assert success
    assertTrue("Patch operation should be successful on server", status.isOk());
}
Also used : PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) Test(org.junit.Test)

Example 33 with PatchContext

use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.

the class XmlPatchBodyReader method parse.

private static PatchContext parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
    final List<PatchEntity> resultCollection = new ArrayList<>();
    final String patchId = doc.getElementsByTagName("patch-id").item(0).getFirstChild().getNodeValue();
    final NodeList editNodes = doc.getElementsByTagName("edit");
    for (int i = 0; i < editNodes.getLength(); i++) {
        DataSchemaNode schemaNode = (DataSchemaNode) pathContext.getSchemaNode();
        final Element element = (Element) editNodes.item(i);
        final String operation = element.getElementsByTagName("operation").item(0).getFirstChild().getNodeValue();
        final PatchEditOperation oper = PatchEditOperation.valueOf(operation.toUpperCase(Locale.ROOT));
        final String editId = element.getElementsByTagName("edit-id").item(0).getFirstChild().getNodeValue();
        final String target = element.getElementsByTagName("target").item(0).getFirstChild().getNodeValue();
        final List<Element> values = readValueNodes(element, oper);
        final Element firstValueElement = values != null ? values.get(0) : null;
        // find complete path to target and target schema node
        // target can be also empty (only slash)
        YangInstanceIdentifier targetII;
        final SchemaNode targetNode;
        final Inference inference;
        if (target.equals("/")) {
            targetII = pathContext.getInstanceIdentifier();
            targetNode = pathContext.getSchemaContext();
            inference = Inference.ofDataTreePath(pathContext.getSchemaContext(), schemaNode.getQName());
        } else {
            // interpret as simple context
            targetII = ParserIdentifier.parserPatchTarget(pathContext, target);
            // move schema node
            schemaNode = verifyNotNull(DataSchemaContextTree.from(pathContext.getSchemaContext()).findChild(targetII).orElseThrow().getDataSchemaNode());
            final SchemaInferenceStack stack = SchemaInferenceStack.of(pathContext.getSchemaContext());
            targetII.getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
            final EffectiveStatement<?, ?> parentStmt = stack.exit();
            verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt);
            targetNode = (SchemaNode) parentStmt;
            inference = stack.toInference();
        }
        if (targetNode == null) {
            LOG.debug("Target node {} not found in path {} ", target, pathContext.getSchemaNode());
            throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
        }
        if (oper.isWithValue()) {
            final NormalizedNode parsed;
            if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode) {
                final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
                final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
                final XmlParserStream xmlParser = XmlParserStream.create(writer, inference);
                xmlParser.traverse(new DOMSource(firstValueElement));
                parsed = resultHolder.getResult();
            } else {
                parsed = null;
            }
            // for lists allow to manipulate with list items through their parent
            if (targetII.getLastPathArgument() instanceof NodeIdentifierWithPredicates) {
                targetII = targetII.getParent();
            }
            resultCollection.add(new PatchEntity(editId, oper, targetII, parsed));
        } else {
            resultCollection.add(new PatchEntity(editId, oper, targetII));
        }
    }
    return new PatchContext(pathContext, ImmutableList.copyOf(resultCollection), patchId);
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) Provider(javax.ws.rs.ext.Provider) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) UntrustedXML(org.opendaylight.yangtools.util.xml.UntrustedXML) Consumes(javax.ws.rs.Consumes) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) Locale(java.util.Locale) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) Inference(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference) MediaTypes(org.opendaylight.restconf.nb.rfc8040.MediaTypes) ParserIdentifier(org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier) List(java.util.List) SAXException(org.xml.sax.SAXException) WebApplicationException(javax.ws.rs.WebApplicationException) NonNull(org.eclipse.jdt.annotation.NonNull) Verify.verifyNotNull(com.google.common.base.Verify.verifyNotNull) DOMSource(javax.xml.transform.dom.DOMSource) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) DOMMountPointService(org.opendaylight.mdsal.dom.api.DOMMountPointService) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) ArrayList(java.util.ArrayList) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) SchemaContextHandler(org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler) XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) Node(org.w3c.dom.Node) EffectiveStatement(org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) IOException(java.io.IOException) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) DataSchemaContextTree(org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStream(java.io.InputStream) PatchEditOperation(org.opendaylight.restconf.common.patch.PatchEditOperation) XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMSource(javax.xml.transform.dom.DOMSource) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) PatchEditOperation(org.opendaylight.restconf.common.patch.PatchEditOperation) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) NodeList(org.w3c.dom.NodeList) Inference(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)

Example 34 with PatchContext

use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.

the class TestJsonPatchBodyReader method modulePatchDataTest.

@Test
public void modulePatchDataTest() throws Exception {
    final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
    mockBodyReader(uri, jsonToPatchBodyReader, false);
    final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
    final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
    checkPatchContext(returnValue);
}
Also used : PatchContext(org.opendaylight.restconf.common.patch.PatchContext) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 35 with PatchContext

use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.

the class TestJsonPatchBodyReader method modulePatchCreateAndDeleteTest.

/**
 * Test of successful Patch consisting of create and delete Patch operations.
 */
@Test
public void modulePatchCreateAndDeleteTest() throws Exception {
    final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
    mockBodyReader(uri, jsonToPatchBodyReader, false);
    final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
    final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
    checkPatchContext(returnValue);
}
Also used : PatchContext(org.opendaylight.restconf.common.patch.PatchContext) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

PatchContext (org.opendaylight.restconf.common.patch.PatchContext)42 Test (org.junit.Test)36 InputStream (java.io.InputStream)31 PatchEntity (org.opendaylight.restconf.common.patch.PatchEntity)11 ArrayList (java.util.ArrayList)9 InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)9 PatchStatusContext (org.opendaylight.restconf.common.patch.PatchStatusContext)7 AbstractBodyReaderTest (org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest)7 JsonBodyReaderTest (org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest)6 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)5 IOException (java.io.IOException)4 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)4 MdsalRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy)4 NetconfRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy)4 URISyntaxException (java.net.URISyntaxException)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 DefaultDOMRpcResult (org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult)3 Verify.verify (com.google.common.base.Verify.verify)2