Search in sources :

Example 11 with DOMEntity

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

the class EntityOwnershipShard method onRegisterListenerLocal.

private void onRegisterListenerLocal(final RegisterListenerLocal registerListener) {
    LOG.debug("{}: onRegisterListenerLocal: {}", persistenceId(), registerListener);
    listenerSupport.addEntityOwnershipListener(registerListener.getEntityType(), registerListener.getListener());
    getSender().tell(SuccessReply.INSTANCE, getSelf());
    searchForEntities((entityTypeNode, entityNode) -> {
        java.util.Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
        String entityType = possibleType.isPresent() ? possibleType.get().getValue().toString() : null;
        if (registerListener.getEntityType().equals(entityType)) {
            final boolean hasOwner;
            final boolean isOwner;
            java.util.Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
            if (possibleOwner.isPresent()) {
                isOwner = localMemberName.getName().equals(possibleOwner.get().getValue().toString());
                hasOwner = true;
            } else {
                isOwner = false;
                hasOwner = false;
            }
            DOMEntity entity = new DOMEntity(entityType, (YangInstanceIdentifier) entityNode.getChild(ENTITY_ID_NODE_ID).get().getValue());
            listenerSupport.notifyEntityOwnershipListener(entity, false, isOwner, hasOwner, registerListener.getListener());
        }
    });
}
Also used : DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity)

Example 12 with DOMEntity

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

the class EntityOwnerChangeListener method onDataTreeChanged.

@Override
public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
    for (DataTreeCandidate change : changes) {
        DataTreeCandidateNode changeRoot = change.getRootNode();
        LeafNode<?> ownerLeaf = (LeafNode<?>) changeRoot.getDataAfter().get();
        LOG.debug("{}: Entity node changed: {}, {}", logId(), changeRoot.getModificationType(), change.getRootPath());
        String newOwner = extractOwner(ownerLeaf);
        String origOwner = null;
        Optional<NormalizedNode<?, ?>> dataBefore = changeRoot.getDataBefore();
        if (dataBefore.isPresent()) {
            origOwner = extractOwner((LeafNode<?>) changeRoot.getDataBefore().get());
        }
        LOG.debug("{}: New owner: {}, Original owner: {}", logId(), newOwner, origOwner);
        if (!Objects.equals(origOwner, newOwner)) {
            boolean isOwner = localMemberName.equals(newOwner);
            boolean wasOwner = localMemberName.equals(origOwner);
            boolean hasOwner = !Strings.isNullOrEmpty(newOwner);
            DOMEntity entity = createEntity(change.getRootPath());
            LOG.debug("{}: Calling notifyEntityOwnershipListeners: entity: {}, wasOwner: {}, isOwner: {}, hasOwner: {}", logId(), entity, wasOwner, isOwner, hasOwner);
            publisher.notifyEntityOwnershipListeners(entity, wasOwner, isOwner, hasOwner);
        }
    }
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) DataTreeCandidateNode(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 13 with DOMEntity

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

the class EntityOwnersModel method createEntity.

static DOMEntity createEntity(final YangInstanceIdentifier entityPath) {
    String entityType = null;
    YangInstanceIdentifier entityId = null;
    for (PathArgument pathArg : entityPath.getPathArguments()) {
        if (pathArg instanceof NodeIdentifierWithPredicates) {
            NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
            Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
            if (ENTITY_TYPE_QNAME.equals(key.getKey())) {
                entityType = key.getValue().toString();
            } else if (ENTITY_ID_QNAME.equals(key.getKey())) {
                entityId = (YangInstanceIdentifier) key.getValue();
            }
        }
    }
    return new DOMEntity(entityType, entityId);
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 14 with DOMEntity

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

the class DistributedEntityOwnershipServiceTest method testCloseCandidateRegistration.

@Test
public void testCloseCandidateRegistration() throws Exception {
    DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
    DOMEntity entity = new DOMEntity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
    DOMEntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
    verifyEntityOwnershipCandidateRegistration(entity, reg);
    verifyRegisterCandidateLocal(service, entity);
    reset(service);
    reg.close();
    UnregisterCandidateLocal unregCandidate = verifyMessage(service, UnregisterCandidateLocal.class);
    assertEquals("getEntity", entity, unregCandidate.getEntity());
    // Re-register - should succeed.
    reset(service);
    service.registerCandidate(entity);
    verifyRegisterCandidateLocal(service, entity);
    service.close();
}
Also used : UnregisterCandidateLocal(org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal) DOMEntityOwnershipCandidateRegistration(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) Test(org.junit.Test)

Example 15 with DOMEntity

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

the class DistributedEntityOwnershipServiceTest method testGetOwnershipState.

@Test
public void testGetOwnershipState() throws Exception {
    DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
    final Shard mockShard = Mockito.mock(Shard.class);
    ShardDataTree shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL);
    when(service.getLocalEntityOwnershipShardDataTree()).thenReturn(shardDataTree.getDataTree());
    DOMEntity entity1 = new DOMEntity(ENTITY_TYPE, "one");
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getIdentifier(), "member-1"), shardDataTree);
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(), entityEntryWithOwner(entity1.getIdentifier(), "member-1"))), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.IS_OWNER);
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getIdentifier(), "member-2"), shardDataTree);
    writeNode(entityPath(entity1.getType(), entity1.getIdentifier()), entityEntryWithOwner(entity1.getIdentifier(), "member-2"), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.OWNED_BY_OTHER);
    writeNode(entityPath(entity1.getType(), entity1.getIdentifier()), entityEntryWithOwner(entity1.getIdentifier(), ""), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.NO_OWNER);
    DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, "two");
    Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
    assertEquals("getOwnershipState present", false, state.isPresent());
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getIdentifier(), "member-1"), shardDataTree);
    writeNode(entityPath(entity2.getType(), entity2.getIdentifier()), ImmutableNodes.mapEntry(ENTITY_QNAME, ENTITY_ID_QNAME, entity2.getIdentifier()), shardDataTree);
    verifyGetOwnershipState(service, entity2, EntityOwnershipState.NO_OWNER);
    deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getIdentifier()), "member-1"), shardDataTree);
    Optional<EntityOwnershipState> state2 = service.getOwnershipState(entity2);
    assertEquals("getOwnershipState present", false, state2.isPresent());
    service.close();
}
Also used : EntityOwnershipState(org.opendaylight.mdsal.eos.common.api.EntityOwnershipState) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) Shard(org.opendaylight.controller.cluster.datastore.Shard) ShardDataTree(org.opendaylight.controller.cluster.datastore.ShardDataTree) Test(org.junit.Test)

Aggregations

DOMEntity (org.opendaylight.mdsal.eos.dom.api.DOMEntity)26 Test (org.junit.Test)21 ShardTestKit (org.opendaylight.controller.cluster.datastore.ShardTestKit)13 RegisterCandidateLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal)13 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)11 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)9 DOMEntityOwnershipListener (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener)7 RegisterListenerLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal)4 UnregisterCandidateLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal)4 DOMEntityOwnershipChange (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange)4 PeerDown (org.opendaylight.controller.cluster.datastore.messages.PeerDown)3 ArrayList (java.util.ArrayList)2 UnregisterListenerLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal)2 EntityOwnerSelectionStrategyConfig (org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig)2 PeerUp (org.opendaylight.controller.cluster.datastore.messages.PeerUp)2 DOMEntityOwnershipCandidateRegistration (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration)2 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)2 ActorRef (akka.actor.ActorRef)1 TestActorRef (akka.testkit.TestActorRef)1 TestKit (akka.testkit.javadsl.TestKit)1