Search in sources :

Example 1 with DOMEntityOwnershipChange

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

the class DistributedEntityOwnershipIntegrationTest method testCloseCandidateRegistrationInQuickSuccession.

/**
 * Reproduces bug <a href="https://bugs.opendaylight.org/show_bug.cgi?id=4554">4554</a>.
 */
@Test
public void testCloseCandidateRegistrationInQuickSuccession() throws Exception {
    String name = "testCloseCandidateRegistrationInQuickSuccession";
    MemberNode leaderNode = MemberNode.builder(memberNodes).akkaConfig("Member1").testName(name).moduleShardsConfig(MODULE_SHARDS_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(leaderDatastoreContextBuilder).build();
    MemberNode follower1Node = MemberNode.builder(memberNodes).akkaConfig("Member2").testName(name).moduleShardsConfig(MODULE_SHARDS_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    MemberNode follower2Node = MemberNode.builder(memberNodes).akkaConfig("Member3").testName(name).moduleShardsConfig(MODULE_SHARDS_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    AbstractDataStore leaderDistributedDataStore = leaderNode.configDataStore();
    leaderDistributedDataStore.waitTillReady();
    follower1Node.configDataStore().waitTillReady();
    follower2Node.configDataStore().waitTillReady();
    final DOMEntityOwnershipService leaderEntityOwnershipService = newOwnershipService(leaderDistributedDataStore);
    final DOMEntityOwnershipService follower1EntityOwnershipService = newOwnershipService(follower1Node.configDataStore());
    final DOMEntityOwnershipService follower2EntityOwnershipService = newOwnershipService(follower2Node.configDataStore());
    leaderNode.kit().waitUntilLeader(leaderNode.configDataStore().getActorContext(), ENTITY_OWNERSHIP_SHARD_NAME);
    leaderEntityOwnershipService.registerListener(ENTITY_TYPE1, leaderMockListener);
    follower1EntityOwnershipService.registerListener(ENTITY_TYPE1, follower1MockListener);
    follower2EntityOwnershipService.registerListener(ENTITY_TYPE1, follower2MockListener);
    final DOMEntityOwnershipCandidateRegistration candidate1 = leaderEntityOwnershipService.registerCandidate(ENTITY1);
    verify(leaderMockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, true, true));
    final DOMEntityOwnershipCandidateRegistration candidate2 = follower1EntityOwnershipService.registerCandidate(ENTITY1);
    verify(follower1MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, false, true));
    final DOMEntityOwnershipCandidateRegistration candidate3 = follower2EntityOwnershipService.registerCandidate(ENTITY1);
    verify(follower2MockListener, timeout(5000)).ownershipChanged(ownershipChange(ENTITY1, false, false, true));
    Mockito.reset(leaderMockListener, follower1MockListener, follower2MockListener);
    ArgumentCaptor<DOMEntityOwnershipChange> leaderChangeCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
    ArgumentCaptor<DOMEntityOwnershipChange> follower1ChangeCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
    ArgumentCaptor<DOMEntityOwnershipChange> follower2ChangeCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
    doNothing().when(leaderMockListener).ownershipChanged(leaderChangeCaptor.capture());
    doNothing().when(follower1MockListener).ownershipChanged(follower1ChangeCaptor.capture());
    doNothing().when(follower2MockListener).ownershipChanged(follower2ChangeCaptor.capture());
    candidate1.close();
    candidate2.close();
    candidate3.close();
    boolean passed = false;
    for (int i = 0; i < 100; i++) {
        Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
        final Optional<EntityOwnershipState> leaderState = leaderEntityOwnershipService.getOwnershipState(ENTITY1);
        final Optional<EntityOwnershipState> follower1State = follower1EntityOwnershipService.getOwnershipState(ENTITY1);
        final Optional<EntityOwnershipState> follower2State = follower2EntityOwnershipService.getOwnershipState(ENTITY1);
        final Optional<DOMEntityOwnershipChange> leaderChange = getValueSafely(leaderChangeCaptor);
        final Optional<DOMEntityOwnershipChange> follower1Change = getValueSafely(follower1ChangeCaptor);
        final Optional<DOMEntityOwnershipChange> follower2Change = getValueSafely(follower2ChangeCaptor);
        if (!leaderState.isPresent() || leaderState.get() == EntityOwnershipState.NO_OWNER && follower1State.isPresent() && follower1State.get() == EntityOwnershipState.NO_OWNER && follower2State.isPresent() && follower2State.get() == EntityOwnershipState.NO_OWNER && leaderChange.isPresent() && !leaderChange.get().getState().hasOwner() && follower1Change.isPresent() && !follower1Change.get().getState().hasOwner() && follower2Change.isPresent() && !follower2Change.get().getState().hasOwner()) {
            passed = true;
            break;
        }
    }
    assertTrue("No ownership change message was sent with hasOwner=false", passed);
}
Also used : DOMEntityOwnershipService(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService) MemberNode(org.opendaylight.controller.cluster.datastore.MemberNode) DOMEntityOwnershipCandidateRegistration(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState) AbstractDataStore(org.opendaylight.controller.cluster.datastore.AbstractDataStore) Test(org.junit.Test)

Example 2 with DOMEntityOwnershipChange

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

the class EntityOwnershipListenerActorTest method testOnEntityOwnershipChangedWithListenerEx.

@Test
public void testOnEntityOwnershipChangedWithListenerEx() {
    DOMEntityOwnershipListener mockListener = mock(DOMEntityOwnershipListener.class);
    DOMEntity entity1 = new DOMEntity("test", YangInstanceIdentifier.of(QName.create("test", "id1")));
    doThrow(new RuntimeException("mock")).when(mockListener).ownershipChanged(ownershipChange(entity1, false, true, true));
    DOMEntity entity2 = new DOMEntity("test", YangInstanceIdentifier.of(QName.create("test", "id2")));
    doNothing().when(mockListener).ownershipChanged(ownershipChange(entity2, true, false, false));
    TestActorRef<EntityOwnershipListenerActor> listenerActor = actorFactory.createTestActor(EntityOwnershipListenerActor.props(mockListener), actorFactory.generateActorId("listener"));
    listenerActor.tell(new DOMEntityOwnershipChange(entity1, EntityOwnershipChangeState.from(false, true, true)), ActorRef.noSender());
    listenerActor.tell(new DOMEntityOwnershipChange(entity2, EntityOwnershipChangeState.from(true, false, false)), ActorRef.noSender());
    verify(mockListener, timeout(5000)).ownershipChanged(ownershipChange(entity2, true, false, false));
}
Also used : DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) Test(org.junit.Test)

Example 3 with DOMEntityOwnershipChange

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

the class LegacyEntityOwnershipServiceAdapterTest method testRegisterListener.

@Test
public void testRegisterListener() {
    DOMEntityOwnershipListenerRegistration mockDOMReg = mock(DOMEntityOwnershipListenerRegistration.class);
    doNothing().when(mockDOMReg).close();
    doReturn(mockDOMReg).when(mockDOMService).registerListener(eq(DOM_ENTITY.getType()), any(DOMEntityOwnershipListener.class));
    EntityOwnershipListener mockListener = mock(EntityOwnershipListener.class);
    doNothing().when(mockListener).ownershipChanged(any(EntityOwnershipChange.class));
    EntityOwnershipListenerRegistration reg = adapter.registerListener(LEGACY_ENTITY.getType(), mockListener);
    assertNotNull("registerListener returned null", reg);
    assertEquals("getInstance", mockListener, reg.getInstance());
    assertEquals("getEntityType", LEGACY_ENTITY.getType(), reg.getEntityType());
    ArgumentCaptor<DOMEntityOwnershipListener> domListenerCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipListener.class);
    verify(mockDOMService).registerListener(eq(DOM_ENTITY.getType()), domListenerCaptor.capture());
    DOMEntityOwnershipChange domOwnershipChange = new DOMEntityOwnershipChange(DOM_ENTITY, EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
    domListenerCaptor.getValue().ownershipChanged(domOwnershipChange);
    ArgumentCaptor<EntityOwnershipChange> ownershipChangeCaptor = ArgumentCaptor.forClass(EntityOwnershipChange.class);
    verify(mockListener).ownershipChanged(ownershipChangeCaptor.capture());
    EntityOwnershipChange change = ownershipChangeCaptor.getValue();
    assertEquals("getEntity", LEGACY_ENTITY, change.getEntity());
    assertEquals("wasOwner", false, change.wasOwner());
    assertEquals("isOwner", true, change.isOwner());
    assertEquals("hasOwner", true, change.hasOwner());
    reg.close();
    verify(mockDOMReg).close();
}
Also used : DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) DOMEntityOwnershipListenerRegistration(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) EntityOwnershipChange(org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange) EntityOwnershipListener(org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener) DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) EntityOwnershipListenerRegistration(org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration) DOMEntityOwnershipListenerRegistration(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration) Test(org.junit.Test)

Example 4 with DOMEntityOwnershipChange

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

the class EntityOwnershipListenerSupport method notifyListeners.

@GuardedBy("listenerLock")
private void notifyListeners(final DOMEntity entity, final boolean wasOwner, final boolean isOwner, final boolean hasOwner, final Collection<ListenerActorRefEntry> listenerEntries) {
    DOMEntityOwnershipChange changed = new DOMEntityOwnershipChange(entity, EntityOwnershipChangeState.from(wasOwner, isOwner, hasOwner), inJeopardy);
    for (ListenerActorRefEntry entry : listenerEntries) {
        ActorRef listenerActor = entry.actorFor();
        LOG.debug("{}: Notifying EntityOwnershipListenerActor {} with {}", logId, listenerActor, changed);
        listenerActor.tell(changed, ActorRef.noSender());
    }
}
Also used : DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) ActorRef(akka.actor.ActorRef) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 5 with DOMEntityOwnershipChange

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

the class EntityOwnershipListenerActorTest method testOnEntityOwnershipChanged.

@Test
public void testOnEntityOwnershipChanged() {
    DOMEntityOwnershipListener mockListener = mock(DOMEntityOwnershipListener.class);
    TestActorRef<EntityOwnershipListenerActor> listenerActor = actorFactory.createTestActor(EntityOwnershipListenerActor.props(mockListener), actorFactory.generateActorId("listener"));
    DOMEntity entity = new DOMEntity("test", YangInstanceIdentifier.of(QName.create("test", "id1")));
    boolean wasOwner = false;
    boolean isOwner = true;
    boolean hasOwner = true;
    listenerActor.tell(new DOMEntityOwnershipChange(entity, EntityOwnershipChangeState.from(wasOwner, isOwner, hasOwner)), ActorRef.noSender());
    verify(mockListener, timeout(5000)).ownershipChanged(ownershipChange(entity, wasOwner, isOwner, hasOwner));
}
Also used : DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) Test(org.junit.Test)

Aggregations

DOMEntityOwnershipChange (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange)5 Test (org.junit.Test)4 DOMEntityOwnershipListener (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener)3 DOMEntity (org.opendaylight.mdsal.eos.dom.api.DOMEntity)2 ActorRef (akka.actor.ActorRef)1 GuardedBy (javax.annotation.concurrent.GuardedBy)1 AbstractDataStore (org.opendaylight.controller.cluster.datastore.AbstractDataStore)1 MemberNode (org.opendaylight.controller.cluster.datastore.MemberNode)1 EntityOwnershipChange (org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange)1 EntityOwnershipListener (org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener)1 EntityOwnershipListenerRegistration (org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration)1 EntityOwnershipState (org.opendaylight.mdsal.eos.common.api.EntityOwnershipState)1 DOMEntityOwnershipCandidateRegistration (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration)1 DOMEntityOwnershipListenerRegistration (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration)1 DOMEntityOwnershipService (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService)1