Fix modernization issues
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnershipShard.java
index 6f4c8dbbd29f5afa875825943cf213debd71a843..cd97b0bcdd01504b2dad32b11ba6a1d94743d5bc 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
+import static java.util.Objects.requireNonNull;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NAME_NODE_ID;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NODE_ID;
 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_NODE_ID;
@@ -30,9 +31,8 @@ import akka.cluster.ClusterEvent.CurrentClusterState;
 import akka.cluster.Member;
 import akka.cluster.MemberStatus;
 import akka.pattern.Patterns;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -40,6 +40,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
@@ -199,13 +200,13 @@ class EntityOwnershipShard extends Shard {
         getSender().tell(SuccessReply.INSTANCE, getSelf());
 
         searchForEntities((entityTypeNode, entityNode) -> {
-            java.util.Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
+            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);
+                Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
                 if (possibleOwner.isPresent()) {
                     isOwner = localMemberName.getName().equals(possibleOwner.get().getValue().toString());
                     hasOwner = true;
@@ -299,12 +300,12 @@ class EntityOwnershipShard extends Shard {
 
     private void notifyAllListeners() {
         searchForEntities((entityTypeNode, entityNode) -> {
-            java.util.Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
+            Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
             if (possibleType.isPresent()) {
                 final boolean hasOwner;
                 final boolean isOwner;
 
-                java.util.Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
+                Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
                 if (possibleOwner.isPresent()) {
                     isOwner = localMemberName.getName().equals(possibleOwner.get().getValue().toString());
                     hasOwner = true;
@@ -388,7 +389,7 @@ class EntityOwnershipShard extends Shard {
                     .node(entityTypeNode.getIdentifier()).node(ENTITY_NODE_ID).node(entityNode.getIdentifier())
                     .node(ENTITY_OWNER_NODE_ID).build();
 
-            java.util.Optional<String> possibleOwner =
+            Optional<String> possibleOwner =
                     entityNode.getChild(ENTITY_OWNER_NODE_ID).map(node -> node.getValue().toString());
             String newOwner = newOwner(possibleOwner.orElse(null), getCandidateNames(entityNode),
                     getEntityOwnerElectionStrategy(entityPath));
@@ -403,7 +404,7 @@ class EntityOwnershipShard extends Shard {
     }
 
     private void initializeDownPeerMemberNamesFromClusterState() {
-        java.util.Optional<Cluster> cluster = getRaftActorContext().getCluster();
+        Optional<Cluster> cluster = getRaftActorContext().getCluster();
         if (!cluster.isPresent()) {
             return;
         }
@@ -513,7 +514,7 @@ class EntityOwnershipShard extends Shard {
 
             } else {
                 LOG.debug("{}: Found entity {} but no other candidates - not clearing owner", persistenceId(),
-                        entityPath, newOwner);
+                        entityPath);
             }
         });
 
@@ -543,20 +544,21 @@ class EntityOwnershipShard extends Shard {
     }
 
     private static Collection<String> getCandidateNames(final MapEntryNode entity) {
-        Collection<MapEntryNode> candidates = ((MapNode)entity.getChild(CANDIDATE_NODE_ID).get()).getValue();
-        Collection<String> candidateNames = new ArrayList<>(candidates.size());
-        for (MapEntryNode candidate: candidates) {
-            candidateNames.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
-        }
-
-        return candidateNames;
+        return entity.getChild(CANDIDATE_NODE_ID).map(child -> {
+            Collection<MapEntryNode> candidates = ((MapNode) child).getValue();
+            Collection<String> candidateNames = new ArrayList<>(candidates.size());
+            for (MapEntryNode candidate: candidates) {
+                candidateNames.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
+            }
+            return candidateNames;
+        }).orElse(ImmutableList.of());
     }
 
     private void searchForEntitiesOwnedBy(final Set<String> ownedBy, final EntityWalker walker) {
         LOG.debug("{}: Searching for entities owned by {}", persistenceId(), ownedBy);
 
         searchForEntities((entityTypeNode, entityNode) -> {
-            java.util.Optional<DataContainerChild<? extends PathArgument, ?>> possibleOwner =
+            Optional<DataContainerChild<? extends PathArgument, ?>> possibleOwner =
                     entityNode.getChild(ENTITY_OWNER_NODE_ID);
             String currentOwner = possibleOwner.isPresent() ? possibleOwner.get().getValue().toString() : "";
             if (ownedBy.contains(currentOwner)) {
@@ -569,11 +571,10 @@ class EntityOwnershipShard extends Shard {
         final List<Modification> modifications = new ArrayList<>();
         searchForEntities((entityTypeNode, entityNode) -> {
             if (hasCandidate(entityNode, member)) {
-                YangInstanceIdentifier entityId =
-                        (YangInstanceIdentifier) entityNode.getIdentifier().getKeyValues().get(ENTITY_ID_QNAME);
-                YangInstanceIdentifier candidatePath = candidatePath(
-                        entityTypeNode.getIdentifier().getKeyValues().get(ENTITY_TYPE_QNAME).toString(),
-                        entityId, member.getName());
+                YangInstanceIdentifier entityId = (YangInstanceIdentifier) entityNode.getIdentifier()
+                        .getValue(ENTITY_ID_QNAME);
+                YangInstanceIdentifier candidatePath = candidatePath(entityTypeNode.getIdentifier()
+                    .getValue(ENTITY_TYPE_QNAME).toString(), entityId, member.getName());
 
                 LOG.info("{}: Found entity {}, removing candidate {}, path {}", persistenceId(), entityId,
                         member, candidatePath);
@@ -586,7 +587,8 @@ class EntityOwnershipShard extends Shard {
     }
 
     private static boolean hasCandidate(final MapEntryNode entity, final MemberName candidateName) {
-        return ((MapNode)entity.getChild(CANDIDATE_NODE_ID).get()).getChild(candidateNodeKey(candidateName.getName()))
+        return entity.getChild(CANDIDATE_NODE_ID)
+                .flatMap(child -> ((MapNode)child).getChild(candidateNodeKey(candidateName.getName())))
                 .isPresent();
     }
 
@@ -596,8 +598,8 @@ class EntityOwnershipShard extends Shard {
             return;
         }
 
-        for (MapEntryNode entityType ((MapNode) possibleEntityTypes.get()).getValue()) {
-            java.util.Optional<DataContainerChild<?, ?>> possibleEntities = entityType.getChild(ENTITY_NODE_ID);
+        for (MapEntryNode entityType : ((MapNode) possibleEntityTypes.get()).getValue()) {
+            Optional<DataContainerChild<?, ?>> possibleEntities = entityType.getChild(ENTITY_NODE_ID);
             if (!possibleEntities.isPresent()) {
                 // shouldn't happen but handle anyway
                 continue;
@@ -666,11 +668,9 @@ class EntityOwnershipShard extends Shard {
     }
 
     private String getCurrentOwner(final YangInstanceIdentifier entityId) {
-        Optional<NormalizedNode<?, ?>> optionalEntityOwner = getDataStore().readNode(entityId.node(ENTITY_OWNER_QNAME));
-        if (optionalEntityOwner.isPresent()) {
-            return optionalEntityOwner.get().getValue().toString();
-        }
-        return null;
+        return getDataStore().readNode(entityId.node(ENTITY_OWNER_QNAME))
+                .map(owner -> owner.getValue().toString())
+                .orElse(null);
     }
 
     @FunctionalInterface
@@ -705,8 +705,8 @@ class EntityOwnershipShard extends Shard {
         @Override
         protected void verify() {
             super.verify();
-            Preconditions.checkNotNull(localMemberName, "localMemberName should not be null");
-            Preconditions.checkNotNull(ownerSelectionStrategyConfig, "ownerSelectionStrategyConfig should not be null");
+            requireNonNull(localMemberName, "localMemberName should not be null");
+            requireNonNull(ownerSelectionStrategyConfig, "ownerSelectionStrategyConfig should not be null");
         }
     }
 }