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());
}
});
}
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);
}
}
}
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);
}
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();
}
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();
}
Aggregations