Fix sonar warnings in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / CandidateListChangeListener.java
index 35100cb5b9a49d3e5c72be4d5c98fc7d3a0810ee..729b7d8f82202ebf9cc253980df143c2494297ac 100644 (file)
@@ -12,7 +12,9 @@ import static org.opendaylight.controller.cluster.datastore.entityownership.Enti
 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_QNAME;
+
 import akka.actor.ActorRef;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -44,26 +46,31 @@ import org.slf4j.LoggerFactory;
  * @author Moiz Raja
  * @author Thomas Pantelis
  */
-public class CandidateListChangeListener implements DOMDataTreeChangeListener {
+class CandidateListChangeListener implements DOMDataTreeChangeListener {
     private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
 
+    private final String logId;
     private final ActorRef shard;
     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
 
-    public CandidateListChangeListener(ActorRef shard, ShardDataTree shardDataTree) {
+    CandidateListChangeListener(ActorRef shard, String logId) {
         this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
+        this.logId = logId;
+    }
 
-        shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).
-                node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME).
-                        node(Candidate.QNAME).node(Candidate.QNAME).build(), this);
+    void init(ShardDataTree shardDataTree) {
+        shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH)
+            .node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME)
+                .node(Candidate.QNAME).node(Candidate.QNAME).build(), this, Optional.absent(), noop -> { /* NOOP */ });
     }
 
     @Override
     public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
-        for(DataTreeCandidate change: changes) {
+        for (DataTreeCandidate change: changes) {
             DataTreeCandidateNode changeRoot = change.getRootNode();
+            ModificationType type = changeRoot.getModificationType();
 
-            LOG.debug("Candidate node changed: {}, {}", changeRoot.getModificationType(), change.getRootPath());
+            LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
 
             NodeIdentifierWithPredicates candidateKey =
                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
@@ -71,23 +78,23 @@ public class CandidateListChangeListener implements DOMDataTreeChangeListener {
 
             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
 
-            if(changeRoot.getModificationType() == ModificationType.WRITE) {
-                LOG.debug("Candidate {} was added for entity {}", candidate, entityId);
+            if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
+                LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
 
-                Collection<String> currentCandidates = addToCurrentCandidates(entityId, candidate);
-                shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(currentCandidates)), shard);
-            } else if(changeRoot.getModificationType() == ModificationType.DELETE) {
-                LOG.debug("Candidate {} was removed for entity {}", 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> currentCandidates = removeFromCurrentCandidates(entityId, candidate);
-                shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(currentCandidates)), shard);
+                Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
+                shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
             }
         }
     }
 
     private Collection<String> addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) {
         Collection<String> candidates = currentCandidates.get(entityId);
-        if(candidates == null) {
+        if (candidates == null) {
             candidates = new LinkedHashSet<>();
             currentCandidates.put(entityId, candidates);
         }
@@ -98,7 +105,7 @@ public class CandidateListChangeListener implements DOMDataTreeChangeListener {
 
     private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
         Collection<String> candidates = currentCandidates.get(entityId);
-        if(candidates != null) {
+        if (candidates != null) {
             candidates.remove(candidateToRemove);
             return candidates;
         }
@@ -107,14 +114,14 @@ public class CandidateListChangeListener implements DOMDataTreeChangeListener {
         return Collections.emptyList();
     }
 
-    private YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
+    private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
         List<PathArgument> newPathArgs = new ArrayList<>();
-        for(PathArgument pathArg: candidatePath.getPathArguments()) {
+        for (PathArgument pathArg: candidatePath.getPathArguments()) {
             newPathArgs.add(pathArg);
-            if(pathArg instanceof NodeIdentifierWithPredicates) {
+            if (pathArg instanceof NodeIdentifierWithPredicates) {
                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
                 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
-                if(ENTITY_ID_QNAME.equals(key.getKey())) {
+                if (ENTITY_ID_QNAME.equals(key.getKey())) {
                     break;
                 }
             }
@@ -122,4 +129,4 @@ public class CandidateListChangeListener implements DOMDataTreeChangeListener {
 
         return YangInstanceIdentifier.create(newPathArgs);
     }
-}
\ No newline at end of file
+}