Bug 4105: Add EntityOwnerDataChangeListener 96/26796/1
authorTom Pantelis <tpanteli@brocade.com>
Wed, 12 Aug 2015 19:51:54 +0000 (15:51 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 10 Sep 2015 19:13:13 +0000 (15:13 -0400)
Added EntityOwnerDataChangeListener that responds to changes to the
entity owner leaf and notifies the EntityOwnershipListenerSupport
appropriately.

I also added an EntityOwnersModel class that defines various entity-owners
yang model constants (moved from EntityOwnershipShard) and has utilities
for creating NormalizedNodes and paths.

Change-Id: Iaa567b5cba6cf0f5cfca0dce39f0f43c38fee4bc
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnerChangeListener.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnersModel.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShard.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/AbstractEntityOwnershipTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnerChangeListenerTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShardTest.java

index fd42740..c167ec6 100644 (file)
@@ -51,13 +51,13 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     private final TipProducingDataTree dataTree;
     private SchemaContext schemaContext;
 
-    ShardDataTree(final SchemaContext schemaContext) {
+    public ShardDataTree(final SchemaContext schemaContext) {
         dataTree = InMemoryDataTreeFactory.getInstance().create();
         updateSchemaContext(schemaContext);
 
     }
 
-    TipProducingDataTree getDataTree() {
+    public TipProducingDataTree getDataTree() {
         return dataTree;
     }
 
@@ -98,7 +98,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
     }
 
-    void notifyListeners(final DataTreeCandidate candidate) {
+    public void notifyListeners(final DataTreeCandidate candidate) {
         LOG.debug("Notifying listeners on candidate {}", candidate);
 
         // DataTreeChanges first, as they are more light-weight
@@ -143,7 +143,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         return new SimpleEntry<>(reg, event);
     }
 
-    Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
+    public Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path,
             final DOMDataTreeChangeListener listener) {
         final ListenerRegistration<DOMDataTreeChangeListener> reg = treeChangePublisher.registerTreeChangeListener(path, listener);
 
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnerChangeListener.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnerChangeListener.java
new file mode 100644 (file)
index 0000000..bf26163
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.entityownership;
+
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_NODE_ID;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME;
+import com.google.common.base.Objects;
+import com.google.common.base.Optional;
+import java.util.Collection;
+import java.util.Map.Entry;
+import org.opendaylight.controller.cluster.datastore.ShardDataTree;
+import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A DataChangeListener that listeners for entity owner changes and notifies the EntityOwnershipListenerSupport
+ * appropriately.
+ *
+ * @author Thomas Pantelis
+ */
+class EntityOwnerChangeListener implements DOMDataTreeChangeListener {
+    private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerChangeListener.class);
+
+    private final String localMemberName;
+    private final EntityOwnershipListenerSupport listenerSupport;
+
+    EntityOwnerChangeListener(String localMemberName, EntityOwnershipListenerSupport listenerSupport) {
+        this.localMemberName = localMemberName;
+        this.listenerSupport = listenerSupport;
+    }
+
+    void init(ShardDataTree shardDataTree) {
+        shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).
+                node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME).build(), this);
+    }
+
+    @Override
+    public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
+        for(DataTreeCandidate change: changes) {
+            MapEntryNode entityNode = (MapEntryNode) change.getRootNode().getDataAfter().get();
+
+            LOG.debug("Entity node updated: {}", change.getRootPath());
+
+            String newOwner = extractOwner(entityNode);
+
+            String origOwner = null;
+            Optional<NormalizedNode<?, ?>> dataBefore = change.getRootNode().getDataBefore();
+            if(dataBefore.isPresent()) {
+                MapEntryNode origEntityNode = (MapEntryNode) change.getRootNode().getDataBefore().get();
+                origOwner = extractOwner(origEntityNode);
+            }
+
+            LOG.debug("New owner: {}, Original owner: {}", newOwner, origOwner);
+
+            boolean isOwner = Objects.equal(localMemberName, newOwner);
+            boolean wasOwner = Objects.equal(localMemberName, origOwner);
+            if(isOwner || wasOwner) {
+                Entity entity = createEntity(change.getRootPath());
+
+                LOG.debug("Calling notifyEntityOwnershipListeners: entity: {}, wasOwner: {}, isOwner: {}",
+                        entity, wasOwner, isOwner);
+
+                listenerSupport.notifyEntityOwnershipListeners(entity, wasOwner, isOwner);
+            }
+        }
+    }
+
+    private Entity createEntity(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 Entity(entityType, entityId);
+    }
+
+    private String extractOwner(MapEntryNode entityNode) {
+        Optional<DataContainerChild<? extends PathArgument, ?>> ownerNode = entityNode.getChild(ENTITY_OWNER_NODE_ID);
+        return ownerNode.isPresent() ? (String) ownerNode.get().getValue() : null;
+    }
+}
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnersModel.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnersModel.java
new file mode 100644 (file)
index 0000000..5795007
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.entityownership;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+
+/**
+ * Utility methods for entity-owners yang model.
+ *
+ * @author Thomas Pantelis
+ */
+final class EntityOwnersModel {
+    static final  QName ENTITY_QNAME = org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.
+            md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.Entity.QNAME;
+    static final QName CANDIDATE_NAME_QNAME = QName.create(Candidate.QNAME, "name");
+    static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id");
+    static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner");
+    static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type");
+
+    static final NodeIdentifier ENTITY_OWNERS_NODE_ID = new NodeIdentifier(EntityOwners.QNAME);
+    static final NodeIdentifier ENTITY_OWNER_NODE_ID = new NodeIdentifier(ENTITY_OWNER_QNAME);
+    static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.of(EntityOwners.QNAME);
+
+    static YangInstanceIdentifier entityPath(String entityType, YangInstanceIdentifier entityId) {
+        return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME).
+                nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME).
+                        nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
+
+    }
+
+    static NormalizedNode<?, ?> entityOwnersWithCandidate(String entityType, YangInstanceIdentifier entityId,
+            String candidateName) {
+        return entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entityType,
+                entityEntryWithCandidateEntry(entityId, candidateName)));
+    }
+
+    static ContainerNode entityOwnersWithEntityTypeEntry(MapEntryNode entityTypeNode) {
+        return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
+                ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME).
+                        addChild(entityTypeNode).build()).build();
+    }
+
+    static MapEntryNode entityTypeEntryWithEntityEntry(String entityType, MapEntryNode entityNode) {
+        return ImmutableNodes.mapEntryBuilder(EntityType.QNAME,
+                ENTITY_TYPE_QNAME, entityType).addChild(ImmutableNodes.mapNodeBuilder(
+                        ENTITY_QNAME).addChild(entityNode).build()).build();
+    }
+
+    static MapEntryNode entityEntryWithCandidateEntry(YangInstanceIdentifier entityId, String candidateName) {
+        return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
+                candidateEntry(candidateName)).build();
+    }
+
+    static MapNode candidateEntry(String candidateName) {
+        return ImmutableNodes.mapNodeBuilder(Candidate.QNAME).addChild(ImmutableNodes.mapEntry(
+                Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName)).build();
+    }
+
+    static NormalizedNode<?, ?> entityEntryWithOwner(YangInstanceIdentifier entityId, String owner) {
+        return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
+                ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner)).build();
+    }
+}
index 20a6902..3399e44 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
 import akka.actor.ActorSelection;
 import akka.actor.Props;
 import akka.dispatch.OnComplete;
@@ -26,18 +28,7 @@ import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIden
 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
 import org.opendaylight.controller.cluster.datastore.messages.SuccessReply;
 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import scala.concurrent.Future;
 
@@ -47,13 +38,6 @@ import scala.concurrent.Future;
  * @author Thomas Pantelis
  */
 class EntityOwnershipShard extends Shard {
-    static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.of(EntityOwners.QNAME);
-    static final  QName ENTITY_QNAME = org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.
-            md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.Entity.QNAME;
-    static final QName CANDIDATE_NAME = QName.create(Candidate.QNAME, "name");
-    static final QName ENTITY_ID = QName.create(ENTITY_QNAME, "id");
-    static final QName ENTITY_TYPE = QName.create(EntityType.QNAME, "type");
-
     private int transactionIDCounter = 0;
     private final String localMemberName;
     private final List<BatchedModifications> retryModifications = new ArrayList<>();
@@ -96,7 +80,8 @@ class EntityOwnershipShard extends Shard {
         modifications.setReady(true);
         modifications.setTotalMessagesSent(1);
 
-        NormalizedNode<?, ?> entityOwners = createEntityOwnersWithCandidate(registerCandidate.getEntity(), localMemberName);
+        NormalizedNode<?, ?> entityOwners = entityOwnersWithCandidate(registerCandidate.getEntity().getType(),
+                registerCandidate.getEntity().getId(), localMemberName);
         modifications.addModification(new MergeModification(ENTITY_OWNERS_PATH, entityOwners));
 
         tryCommitModifications(modifications);
@@ -104,20 +89,6 @@ class EntityOwnershipShard extends Shard {
         getSender().tell(SuccessReply.INSTANCE, getSelf());
     }
 
-    private NormalizedNode<?, ?> createEntityOwnersWithCandidate(Entity entity, String memberName) {
-        MapNode candidateNode = ImmutableNodes.mapNodeBuilder(Candidate.QNAME).addChild(
-                        ImmutableNodes.mapEntry(Candidate.QNAME, CANDIDATE_NAME, memberName)).build();
-
-        MapEntryNode entityNode = ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID, entity.getId()).
-                addChild(candidateNode).build();
-
-        MapEntryNode entityTypeNode = ImmutableNodes.mapEntryBuilder(EntityType.QNAME, ENTITY_TYPE, entity.getType()).
-                addChild(ImmutableNodes.mapNodeBuilder(ENTITY_QNAME).addChild(entityNode).build()).build();
-
-        return ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(EntityOwners.QNAME)).
-                addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME).addChild(entityTypeNode).build()).build();
-    }
-
     private void tryCommitModifications(final BatchedModifications modifications) {
         if(isLeader()) {
             if(isIsolatedLeader()) {
index 1f074ec..3d31a06 100644 (file)
@@ -10,6 +10,10 @@ package org.opendaylight.controller.cluster.datastore.entityownership;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NAME_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME;
 import com.google.common.base.Optional;
 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
@@ -42,12 +46,11 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest {
             ContainerNode entityOwnersNode = (ContainerNode) node;
 
             MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME,
-                    EntityOwnershipShard.ENTITY_TYPE, entityType);
+                    ENTITY_TYPE_QNAME, entityType);
 
-            MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, EntityOwnershipShard.ENTITY_QNAME,
-                    EntityOwnershipShard.ENTITY_ID, entityId);
+            MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME, entityId);
 
-            getMapEntryNodeChild(entityEntry, Candidate.QNAME, EntityOwnershipShard.CANDIDATE_NAME, candidateName);
+            getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
         } catch(AssertionError e) {
             throw new AssertionError("Verification of enitity candidate failed - returned data was: " + node, e);
         }
index c2d997f..3e9a51e 100644 (file)
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.mock;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
 import akka.actor.ActorRef;
 import akka.actor.PoisonPill;
 import akka.actor.Props;
@@ -215,7 +216,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
         Stopwatch sw = Stopwatch.createStarted();
         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
             DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
-            Optional<NormalizedNode<?, ?>> optional = readTx.read(EntityOwnershipShard.ENTITY_OWNERS_PATH).
+            Optional<NormalizedNode<?, ?>> optional = readTx.read(ENTITY_OWNERS_PATH).
                     checkedGet(5, TimeUnit.SECONDS);
             if(optional.isPresent()) {
                 return optional.get();
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnerChangeListenerTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnerChangeListenerTest.java
new file mode 100644 (file)
index 0000000..24913d2
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.entityownership;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.verify;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.cluster.datastore.ShardDataTree;
+import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
+import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
+
+/**
+ * Unit tests for EntityOwnerChangeListener.
+ *
+ * @author Thomas Pantelis
+ */
+public class EntityOwnerChangeListenerTest {
+    private static final String LOCAL_MEMBER_NAME = "member-1";
+    private static final String REMOTE_MEMBER_NAME1 = "member-2";
+    private static final String REMOTE_MEMBER_NAME2 = "member-3";
+    private static final String ENTITY_TYPE = "test";
+    private static final YangInstanceIdentifier ENTITY_ID1 =
+            YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
+    private static final YangInstanceIdentifier ENTITY_ID2 =
+            YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
+    private static final Entity ENTITY1 = new Entity(ENTITY_TYPE, ENTITY_ID1);
+    private static final Entity ENTITY2 = new Entity(ENTITY_TYPE, ENTITY_ID2);
+
+    private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
+    private final EntityOwnershipListenerSupport mockListenerSupport = mock(EntityOwnershipListenerSupport.class);
+    private EntityOwnerChangeListener listener;
+
+    @Before
+    public void setup() {
+        listener = new EntityOwnerChangeListener(LOCAL_MEMBER_NAME, mockListenerSupport);
+        listener.init(shardDataTree);
+    }
+
+    @Test
+    public void testOnDataChanged() throws Exception {
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
+
+        verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
+
+        writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
+
+        verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
+
+        reset(mockListenerSupport);
+        writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
+
+        verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, true, false);
+
+        reset(mockListenerSupport);
+        writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
+
+        verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
+
+        writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
+
+        verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
+
+        reset(mockListenerSupport);
+        writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
+
+        verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
+
+        reset(mockListenerSupport);
+        writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
+
+        verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, true);
+    }
+
+    private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
+        DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
+        modification.merge(path, node);
+        modification.ready();
+
+        shardDataTree.getDataTree().validate(modification);
+        DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
+        shardDataTree.getDataTree().commit(candidate);
+        shardDataTree.notifyListeners(candidate);
+    }
+}
index 7a251b9..e4aaaa1 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.cluster.datastore.entityownership;
 
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
 import akka.actor.ActorRef;
 import akka.actor.Props;
 import akka.actor.UntypedActor;
@@ -251,7 +252,7 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
     private NormalizedNode<?, ?> readEntityOwners(TestActorRef<EntityOwnershipShard> shard) throws Exception {
         Stopwatch sw = Stopwatch.createStarted();
         while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
-            NormalizedNode<?, ?> node = AbstractShardTest.readStore(shard, EntityOwnershipShard.ENTITY_OWNERS_PATH);
+            NormalizedNode<?, ?> node = AbstractShardTest.readStore(shard, ENTITY_OWNERS_PATH);
             if(node != null) {
                 return node;
             }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.