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.Preconditions;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.LinkedHashSet;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
27 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
28 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
31 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;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Listens for candidate entries added/removed and notifies the EntityOwnershipShard appropriately.
46 * @author Thomas Pantelis
48 class CandidateListChangeListener implements DOMDataTreeChangeListener {
49 private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
51 private final String logId;
52 private final ActorRef shard;
53 private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
55 CandidateListChangeListener(ActorRef shard, String logId) {
56 this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
60 void init(ShardDataTree shardDataTree) {
61 shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH)
62 .node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME)
63 .node(Candidate.QNAME).node(Candidate.QNAME).build(), this);
67 public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
68 for (DataTreeCandidate change: changes) {
69 DataTreeCandidateNode changeRoot = change.getRootNode();
70 ModificationType type = changeRoot.getModificationType();
72 LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
74 NodeIdentifierWithPredicates candidateKey =
75 (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
76 String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
78 YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
80 if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
81 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
83 Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
84 shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
85 } else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
86 LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
88 Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
89 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
94 private Collection<String> addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) {
95 Collection<String> candidates = currentCandidates.get(entityId);
96 if (candidates == null) {
97 candidates = new LinkedHashSet<>();
98 currentCandidates.put(entityId, candidates);
101 candidates.add(newCandidate);
105 private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
106 Collection<String> candidates = currentCandidates.get(entityId);
107 if (candidates != null) {
108 candidates.remove(candidateToRemove);
113 return Collections.emptyList();
116 private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
117 List<PathArgument> newPathArgs = new ArrayList<>();
118 for (PathArgument pathArg: candidatePath.getPathArguments()) {
119 newPathArgs.add(pathArg);
120 if (pathArg instanceof NodeIdentifierWithPredicates) {
121 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
122 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
123 if (ENTITY_ID_QNAME.equals(key.getKey())) {
129 return YangInstanceIdentifier.create(newPathArgs);