X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2FCandidateListChangeListener.java;h=a7be8c50d2cf949244143d8e5fad4f0569d45c90;hb=a5e45c08c64f450aef9f3bf2d1f98714d01db1ed;hp=833ca30e31412f8ac159933a02d40e27e50eb05d;hpb=ecccb6d5b43dd73aef0d2d19349d19ee9b4728f7;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/CandidateListChangeListener.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/CandidateListChangeListener.java index 833ca30e31..a7be8c50d2 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/CandidateListChangeListener.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/CandidateListChangeListener.java @@ -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; @@ -25,7 +27,7 @@ import java.util.Map.Entry; 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; @@ -57,14 +59,14 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener { } 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); + 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 changes) { - for(DataTreeCandidate change: changes) { + for (DataTreeCandidate change: changes) { DataTreeCandidateNode changeRoot = change.getRootNode(); ModificationType type = changeRoot.getModificationType(); @@ -76,26 +78,22 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener { YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath()); - if(type == ModificationType.WRITE || type == ModificationType.APPEARED) { + if (type == ModificationType.WRITE || type == ModificationType.APPEARED) { LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId); - Collection currentCandidates = addToCurrentCandidates(entityId, candidate); - shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(currentCandidates)), shard); - } else if(type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) { + Collection 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 currentCandidates = removeFromCurrentCandidates(entityId, candidate); - shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(currentCandidates)), shard); + Collection newCandidates = removeFromCurrentCandidates(entityId, candidate); + shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard); } } } private Collection addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) { - Collection candidates = currentCandidates.get(entityId); - if(candidates == null) { - candidates = new LinkedHashSet<>(); - currentCandidates.put(entityId, candidates); - } + Collection candidates = currentCandidates.computeIfAbsent(entityId, k -> new LinkedHashSet<>()); candidates.add(newCandidate); return candidates; @@ -103,7 +101,7 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener { private Collection removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) { Collection candidates = currentCandidates.get(entityId); - if(candidates != null) { + if (candidates != null) { candidates.remove(candidateToRemove); return candidates; } @@ -114,12 +112,12 @@ class CandidateListChangeListener implements DOMDataTreeChangeListener { private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) { List 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 key = nodeKey.getKeyValues().entrySet().iterator().next(); - if(ENTITY_ID_QNAME.equals(key.getKey())) { + if (ENTITY_ID_QNAME.equals(key.getKey())) { break; } }