Search in sources :

Example 1 with CandidateRemoved

use of org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved in project controller by opendaylight.

the class CandidateListChangeListener method onDataTreeChanged.

@Override
public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
    for (DataTreeCandidate change : changes) {
        DataTreeCandidateNode changeRoot = change.getRootNode();
        ModificationType type = changeRoot.getModificationType();
        LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
        NodeIdentifierWithPredicates candidateKey = (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
        String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
        YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
        if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
            LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
            Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
            shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
        } else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
            LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
            Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
            shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
        }
    }
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) ModificationType(org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType) CandidateAdded(org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CandidateRemoved(org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 2 with CandidateRemoved

use of org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved in project controller by opendaylight.

the class CandidateListChangeListenerTest method testOnDataTreeChanged.

@Test
public void testOnDataTreeChanged() throws Exception {
    TestKit kit = new TestKit(getSystem());
    new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
    String memberName1 = "member-1";
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
    CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
    assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
    assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
    assertEquals("getAllCandidates", ImmutableSet.of(memberName1), ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
    kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
    String memberName2 = "member-2";
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
    candidateAdded = kit.expectMsgClass(CandidateAdded.class);
    assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
    assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
    assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2), ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
    candidateAdded = kit.expectMsgClass(CandidateAdded.class);
    assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
    assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
    assertEquals("getAllCandidates", ImmutableSet.of(memberName1), ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
    deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
    CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
    assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
    assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
    assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2), ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
    deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
    candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
    assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
    assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
    assertEquals("getRemainingCandidates", ImmutableSet.of(), ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
}
Also used : CandidateAdded(org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded) CandidateRemoved(org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved) TestKit(akka.testkit.javadsl.TestKit) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Aggregations

CandidateAdded (org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded)2 CandidateRemoved (org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved)2 TestKit (akka.testkit.javadsl.TestKit)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Test (org.junit.Test)1 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)1 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)1 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)1 DataTreeCandidate (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate)1 DataTreeCandidateNode (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode)1 ModificationType (org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType)1