Search in sources :

Example 16 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class RestconfImpl method getOperations.

@Override
@Deprecated
public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
    if (!identifier.contains(ControllerContext.MOUNT)) {
        final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to " + " end with " + ControllerContext.MOUNT;
        LOG.debug("{} for {}", errMsg, identifier);
        throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final InstanceIdentifierContext<?> mountPointIdentifier = controllerContext.toMountPointIdentifier(identifier);
    final DOMMountPoint mountPoint = mountPointIdentifier.getMountPoint();
    final var entry = OperationsResourceUtils.contextForModelContext(modelContext(mountPoint), mountPoint);
    return new NormalizedNodeContext(entry.getKey(), entry.getValue());
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) NormalizedNodeContext(org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)

Example 17 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class BrokerFacade method patchConfigurationDataWithinTransaction.

public PatchStatusContext patchConfigurationDataWithinTransaction(final PatchContext patchContext) throws Exception {
    final DOMMountPoint mountPoint = patchContext.getInstanceIdentifierContext().getMountPoint();
    // get new transaction and schema context on server or on mounted device
    final EffectiveModelContext schemaContext;
    final DOMDataTreeReadWriteTransaction patchTransaction;
    if (mountPoint == null) {
        schemaContext = patchContext.getInstanceIdentifierContext().getSchemaContext();
        patchTransaction = this.domDataBroker.newReadWriteTransaction();
    } else {
        schemaContext = modelContext(mountPoint);
        final Optional<DOMDataBroker> optional = mountPoint.getService(DOMDataBroker.class);
        if (optional.isPresent()) {
            patchTransaction = optional.get().newReadWriteTransaction();
        } else {
            // if mount point does not have broker it is not possible to continue and global error is reported
            LOG.error("Http Patch {} has failed - device {} does not support broker service", patchContext.getPatchId(), mountPoint.getIdentifier());
            return new PatchStatusContext(patchContext.getPatchId(), null, false, ImmutableList.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier())));
        }
    }
    final List<PatchStatusEntity> editCollection = new ArrayList<>();
    List<RestconfError> editErrors;
    boolean withoutError = true;
    for (final PatchEntity patchEntity : patchContext.getData()) {
        final PatchEditOperation operation = patchEntity.getOperation();
        switch(operation) {
            case CREATE:
                if (withoutError) {
                    try {
                        postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            case REPLACE:
                if (withoutError) {
                    try {
                        putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            case DELETE:
            case REMOVE:
                if (withoutError) {
                    try {
                        deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            case MERGE:
                if (withoutError) {
                    try {
                        mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            default:
                LOG.error("Unsupported http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                break;
        }
    }
    // if errors then cancel transaction and return error status
    if (!withoutError) {
        patchTransaction.cancel();
        return new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), false, null);
    }
    // if no errors commit transaction
    final CountDownLatch waiter = new CountDownLatch(1);
    final FluentFuture<? extends CommitInfo> future = patchTransaction.commit();
    final PatchStatusContextHelper status = new PatchStatusContextHelper();
    future.addCallback(new FutureCallback<CommitInfo>() {

        @Override
        public void onSuccess(final CommitInfo result) {
            status.setStatus(new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), true, null));
            waiter.countDown();
        }

        @Override
        public void onFailure(final Throwable throwable) {
            // if commit failed it is global error
            LOG.error("Http Patch {} transaction commit has failed", patchContext.getPatchId());
            status.setStatus(new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), false, ImmutableList.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, throwable.getMessage()))));
            waiter.countDown();
        }
    }, MoreExecutors.directExecutor());
    waiter.await();
    return status.getStatus();
}
Also used : PatchEditOperation(org.opendaylight.restconf.common.patch.PatchEditOperation) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) DOMDataTreeReadWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction) PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) PatchStatusEntity(org.opendaylight.restconf.common.patch.PatchStatusEntity) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo) DOMDataBroker(org.opendaylight.mdsal.dom.api.DOMDataBroker) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 18 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class TestXmlBodyReaderMountPoint method checkExpectValueNormalizeNodeContext.

protected void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode, final NormalizedNodeContext nnContext, final QName qualifiedName) {
    YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
    final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
    final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint).getDataChildByName(dataSchemaNode.getQName());
    assertNotNull(mountDataSchemaNode);
    if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
        final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
        dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
        assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
    } else {
        assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
    }
    assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 19 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class BrokerFacadeTest method testPatchConfigurationDataWithinTransactionMountFail.

/**
 * Negative test for Patch operation when mounted device does not support {@link DOMDataBroker service.}
 * Patch operation should fail with global error.
 */
@Test
public void testPatchConfigurationDataWithinTransactionMountFail() throws Exception {
    final PatchContext patchContext = mock(PatchContext.class);
    final InstanceIdentifierContext<?> identifierContext = mock(InstanceIdentifierContext.class);
    final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
    final DOMDataBroker mountDataBroker = mock(DOMDataBroker.class);
    final DOMDataTreeReadWriteTransaction transaction = mock(DOMDataTreeReadWriteTransaction.class);
    doReturn(identifierContext).when(patchContext).getInstanceIdentifierContext();
    when(identifierContext.getMountPoint()).thenReturn(mountPoint);
    // missing broker on mounted device
    when(mountPoint.getService(DOMDataBroker.class)).thenReturn(Optional.empty());
    when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.empty());
    final PatchStatusContext status = this.brokerFacade.patchConfigurationDataWithinTransaction(patchContext);
    // assert not successful operation with error
    assertNotNull(status.getGlobalErrors());
    assertEquals(1, status.getGlobalErrors().size());
    assertEquals(ErrorType.APPLICATION, status.getGlobalErrors().get(0).getErrorType());
    assertEquals(ErrorTag.OPERATION_FAILED, status.getGlobalErrors().get(0).getErrorTag());
    assertFalse("Patch operation should fail on mounted device without Broker", status.isOk());
}
Also used : DOMDataTreeReadWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction) PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) DOMDataBroker(org.opendaylight.mdsal.dom.api.DOMDataBroker) Test(org.junit.Test)

Example 20 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class BrokerFacadeTest method testPatchConfigurationDataWithinTransactionMount.

/**
 * Test Patch method on mounted device with no data.
 */
@Test
public void testPatchConfigurationDataWithinTransactionMount() throws Exception {
    final PatchContext patchContext = mock(PatchContext.class);
    final InstanceIdentifierContext<?> identifierContext = mock(InstanceIdentifierContext.class);
    final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
    final DOMDataBroker mountDataBroker = mock(DOMDataBroker.class);
    final DOMDataTreeReadWriteTransaction transaction = mock(DOMDataTreeReadWriteTransaction.class);
    when(patchContext.getData()).thenReturn(new ArrayList<>());
    doReturn(identifierContext).when(patchContext).getInstanceIdentifierContext();
    // return mount point with broker
    when(identifierContext.getMountPoint()).thenReturn(mountPoint);
    when(mountPoint.getService(DOMDataBroker.class)).thenReturn(Optional.of(mountDataBroker));
    when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.empty());
    when(mountDataBroker.newReadWriteTransaction()).thenReturn(transaction);
    doReturn(CommitInfo.emptyFluentFuture()).when(transaction).commit();
    final PatchStatusContext status = this.brokerFacade.patchConfigurationDataWithinTransaction(patchContext);
    // assert success
    assertTrue("Patch operation should be successful on mounted device", status.isOk());
}
Also used : DOMDataTreeReadWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction) PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) PatchContext(org.opendaylight.restconf.common.patch.PatchContext) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) DOMDataBroker(org.opendaylight.mdsal.dom.api.DOMDataBroker) Test(org.junit.Test)

Aggregations

DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)38 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)16 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)15 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)13 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)9 NormalizedNodeContext (org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)8 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)8 MdsalRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy)7 RestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy)7 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)6 Test (org.junit.Test)5 DOMRpcService (org.opendaylight.mdsal.dom.api.DOMRpcService)5 QName (org.opendaylight.yangtools.yang.common.QName)5 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ExecutionException (java.util.concurrent.ExecutionException)4 DOMDataTreeReadWriteTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction)4 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)4 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)4