Fix modernization issues
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / CandidateListChangeListener.java
index 6b6717c7da16ac0432f8ffc69381a2df010fc1ad..f96d15588bd8005708f8c6eab04a964bd845b148 100644 (file)
@@ -5,17 +5,15 @@
  * 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 java.util.Objects.requireNonNull;
 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_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;
 import java.util.Collections;
@@ -23,14 +21,13 @@ import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
+import java.util.Optional;
 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.mdsal.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.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.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -53,19 +50,19 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener {
     private final ActorRef shard;
     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
 
-    CandidateListChangeListener(ActorRef shard, String logId) {
-        this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
+    CandidateListChangeListener(final ActorRef shard, final String logId) {
+        this.shard = requireNonNull(shard, "shard should not be null");
         this.logId = logId;
     }
 
-    void init(ShardDataTree shardDataTree) {
+    void init(final 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 -> { });
+            .node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME)
+                .node(Candidate.QNAME).node(Candidate.QNAME).build(), this, Optional.empty(), noop -> { /* NOOP */ });
     }
 
     @Override
-    public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
+    public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
         for (DataTreeCandidate change: changes) {
             DataTreeCandidateNode changeRoot = change.getRootNode();
             ModificationType type = changeRoot.getModificationType();
@@ -74,7 +71,7 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener {
 
             NodeIdentifierWithPredicates candidateKey =
                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
-            String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
+            String candidate = candidateKey.getValue(CANDIDATE_NAME_QNAME).toString();
 
             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
 
@@ -92,18 +89,15 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener {
         }
     }
 
-    private Collection<String> addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) {
-        Collection<String> candidates = currentCandidates.get(entityId);
-        if (candidates == null) {
-            candidates = new LinkedHashSet<>();
-            currentCandidates.put(entityId, candidates);
-        }
-
+    private Collection<String> addToCurrentCandidates(final YangInstanceIdentifier entityId,
+            final String newCandidate) {
+        Collection<String> candidates = currentCandidates.computeIfAbsent(entityId, k -> new LinkedHashSet<>());
         candidates.add(newCandidate);
         return candidates;
     }
 
-    private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
+    private Collection<String> removeFromCurrentCandidates(final YangInstanceIdentifier entityId,
+            final String candidateToRemove) {
         Collection<String> candidates = currentCandidates.get(entityId);
         if (candidates != null) {
             candidates.remove(candidateToRemove);
@@ -114,16 +108,13 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener {
         return Collections.emptyList();
     }
 
-    private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
+    private static YangInstanceIdentifier extractEntityPath(final YangInstanceIdentifier candidatePath) {
         List<PathArgument> newPathArgs = new ArrayList<>();
         for (PathArgument pathArg: candidatePath.getPathArguments()) {
             newPathArgs.add(pathArg);
-            if (pathArg instanceof NodeIdentifierWithPredicates) {
-                NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
-                Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
-                if (ENTITY_ID_QNAME.equals(key.getKey())) {
-                    break;
-                }
+            if (pathArg instanceof NodeIdentifierWithPredicates
+                    && ENTITY_ID_QNAME.equals(((NodeIdentifierWithPredicates) pathArg).keySet().iterator().next())) {
+                break;
             }
         }