Search in sources :

Example 1 with DOMDataTreeCommitCohort

use of org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort in project controller by opendaylight.

the class ConcurrentDOMDataBrokerTest method testExtensions.

@Test
public void testExtensions() {
    DistributedDataStore mockConfigStore = mock(DistributedDataStore.class);
    DistributedDataStore mockOperStore = mock(DistributedDataStore.class);
    try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(LogicalDatastoreType.OPERATIONAL, mockOperStore, LogicalDatastoreType.CONFIGURATION, mockConfigStore), futureExecutor)) {
        Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> supportedExtensions = dataBroker.getSupportedExtensions();
        assertNotNull(supportedExtensions.get(DOMDataTreeChangeService.class));
        DOMDataTreeCommitCohortRegistry cohortRegistry = (DOMDataTreeCommitCohortRegistry) supportedExtensions.get(DOMDataTreeCommitCohortRegistry.class);
        assertNotNull(cohortRegistry);
        DOMDataTreeCommitCohort mockCohort = mock(DOMDataTreeCommitCohort.class);
        DOMDataTreeIdentifier path = new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
        cohortRegistry.registerCommitCohort(path, mockCohort);
        verify(mockConfigStore).registerCommitCohort(path, mockCohort);
    }
}
Also used : DOMDataTreeChangeService(org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService) DistributedDataStore(org.opendaylight.controller.cluster.datastore.DistributedDataStore) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) DOMDataBrokerExtension(org.opendaylight.mdsal.dom.api.DOMDataBrokerExtension) DOMDataTreeCommitCohortRegistry(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) Test(org.junit.Test)

Example 2 with DOMDataTreeCommitCohort

use of org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort in project controller by opendaylight.

the class DataTreeCohortIntegrationTest method testCanCommitWithListEntries.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCanCommitWithListEntries() throws Exception {
    final DOMDataTreeCommitCohort cohort = mock(DOMDataTreeCommitCohort.class);
    doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testCanCommitWithMultipleListEntries", "cars-1")) {
        final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME)), cohort);
        assertNotNull(cohortReg);
        IntegrationTestKit.verifyShardState(dataStore, "cars-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        // First write an empty base container and verify the cohort isn't invoked.
        DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
        writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
        kit.doCommit(writeTx.ready());
        verifyNoMoreInteractions(cohort);
        // Write a single car entry and verify the cohort is invoked.
        writeTx = dataStore.newWriteOnlyTransaction();
        final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
        final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
        writeTx.write(optimaPath, optimaNode);
        kit.doCommit(writeTx.ready());
        ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
        verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
        assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE, Optional.of(optimaNode), Optional.absent());
        // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
        // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
        // car entry (DELETE mod).
        reset(cohort);
        doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
        writeTx = dataStore.newWriteOnlyTransaction();
        final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
        final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", BigInteger.valueOf(20000));
        final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
        final MapEntryNode soulNode = CarsModel.newCarEntry("soul", BigInteger.valueOf(20000));
        writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode, soulNode)));
        kit.doCommit(writeTx.ready());
        candidateCapture = ArgumentCaptor.forClass(Collection.class);
        verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
        assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE, Optional.of(sportageNode), Optional.absent());
        assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE, Optional.of(soulNode), Optional.absent());
        assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE, Optional.absent(), Optional.of(optimaNode));
        // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
        reset(cohort);
        doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
        writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.delete(CarsModel.BASE_PATH);
        kit.doCommit(writeTx.ready());
        candidateCapture = ArgumentCaptor.forClass(Collection.class);
        verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
        assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE, Optional.absent(), Optional.of(sportageNode));
        assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE, Optional.absent(), Optional.of(soulNode));
    }
}
Also used : DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Collection(java.util.Collection) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 3 with DOMDataTreeCommitCohort

use of org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort in project controller by opendaylight.

the class DataTreeCohortIntegrationTest method testFailedCanCommit.

@SuppressWarnings("unchecked")
@Test
public void testFailedCanCommit() throws Exception {
    final DOMDataTreeCommitCohort failedCohort = mock(DOMDataTreeCommitCohort.class);
    doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testFailedCanCommit", "test-1")) {
        dataStore.registerCommitCohort(TEST_ID, failedCohort);
        IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
        DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
        try {
            dsCohort.canCommit().get(5, TimeUnit.SECONDS);
            fail("Exception should be raised.");
        } catch (ExecutionException e) {
            assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
        }
    }
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Collection(java.util.Collection) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) ExecutionException(java.util.concurrent.ExecutionException) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Example 4 with DOMDataTreeCommitCohort

use of org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort in project controller by opendaylight.

the class DataTreeCohortIntegrationTest method testAbortAfterCanCommit.

/**
 * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
 * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
 * we can't actually test abort after canCommit.
 */
@SuppressWarnings("unchecked")
@Test
@Ignore
public void testAbortAfterCanCommit() throws Exception {
    final DOMDataTreeCommitCohort cohortToAbort = mock(DOMDataTreeCommitCohort.class);
    final PostCanCommitStep stepToAbort = mock(PostCanCommitStep.class);
    doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
    doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
    doReturn(Futures.immediateCheckedFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testAbortAfterCanCommit", "test-1", "cars-1")) {
        dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
        IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
        writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
        DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
        dsCohort.canCommit().get(5, TimeUnit.SECONDS);
        dsCohort.preCommit().get(5, TimeUnit.SECONDS);
        dsCohort.abort().get(5, TimeUnit.SECONDS);
        verify(stepToAbort).abort();
    }
}
Also used : PostCanCommitStep(org.opendaylight.mdsal.common.api.PostCanCommitStep) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Collection(java.util.Collection) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with DOMDataTreeCommitCohort

use of org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort in project controller by opendaylight.

the class DataTreeCohortIntegrationTest method testSuccessfulCanCommitWithNoopPostStep.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSuccessfulCanCommitWithNoopPostStep() throws Exception {
    final DOMDataTreeCommitCohort cohort = mock(DOMDataTreeCommitCohort.class);
    doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    ArgumentCaptor<Collection> candidateCapt = ArgumentCaptor.forClass(Collection.class);
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testSuccessfulCanCommitWithNoopPostStep", "test-1")) {
        final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(TEST_ID, cohort);
        assertNotNull(cohortReg);
        IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        final ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
        kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
        verify(cohort).canCommit(any(Object.class), candidateCapt.capture(), any(SchemaContext.class));
        assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapt.getValue().iterator().next(), TEST_ID, ModificationType.WRITE, Optional.of(node), Optional.absent());
        reset(cohort);
        doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
        kit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
        verify(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
        cohortReg.close();
        IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("Cohort registrations", 0, state.getCommitCohortActors().size()));
        kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
        verifyNoMoreInteractions(cohort);
    }
}
Also used : Collection(java.util.Collection) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 DOMDataTreeCommitCohort (org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort)6 Collection (java.util.Collection)4 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)4 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)3 DOMDataTreeIdentifier (org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier)2 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)2 ExecutionException (java.util.concurrent.ExecutionException)1 Ignore (org.junit.Ignore)1 DistributedDataStore (org.opendaylight.controller.cluster.datastore.DistributedDataStore)1 DOMDataTreeCommitCohortRegistry (org.opendaylight.controller.md.sal.dom.api.DOMDataTreeCommitCohortRegistry)1 DOMDataTreeIdentifier (org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier)1 PostCanCommitStep (org.opendaylight.mdsal.common.api.PostCanCommitStep)1 DOMDataBrokerExtension (org.opendaylight.mdsal.dom.api.DOMDataBrokerExtension)1 DOMDataTreeChangeService (org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService)1 DOMDataTreeCommitCohortRegistry (org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry)1 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)1 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)1 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)1