2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore.entityownership;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NAME_QNAME;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
13 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
14 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
16 import akka.actor.ActorRef;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.LinkedHashSet;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
28 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
29 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
32 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;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Listens for candidate entries added/removed and notifies the EntityOwnershipShard appropriately.
47 * @author Thomas Pantelis
49 class CandidateListChangeListener implements DOMDataTreeChangeListener {
50 private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
52 private final String logId;
53 private final ActorRef shard;
54 private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
56 CandidateListChangeListener(ActorRef shard, String logId) {
57 this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
61 void init(ShardDataTree shardDataTree) {
62 shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH)
63 .node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME)
64 .node(Candidate.QNAME).node(Candidate.QNAME).build(), this, Optional.absent(), noop -> { /* NOOP */ });
68 public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
69 for (DataTreeCandidate change: changes) {
70 DataTreeCandidateNode changeRoot = change.getRootNode();
71 ModificationType type = changeRoot.getModificationType();
73 LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
75 NodeIdentifierWithPredicates candidateKey =
76 (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
77 String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
79 YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
81 if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
82 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
84 Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
85 shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
86 } else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
87 LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
89 Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
90 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
95 private Collection<String> addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) {
96 Collection<String> candidates = currentCandidates.computeIfAbsent(entityId, k -> new LinkedHashSet<>());
98 candidates.add(newCandidate);
102 private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
103 Collection<String> candidates = currentCandidates.get(entityId);
104 if (candidates != null) {
105 candidates.remove(candidateToRemove);
110 return Collections.emptyList();
113 private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
114 List<PathArgument> newPathArgs = new ArrayList<>();
115 for (PathArgument pathArg: candidatePath.getPathArguments()) {
116 newPathArgs.add(pathArg);
117 if (pathArg instanceof NodeIdentifierWithPredicates) {
118 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
119 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
120 if (ENTITY_ID_QNAME.equals(key.getKey())) {
126 return YangInstanceIdentifier.create(newPathArgs);