Make private methods static
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / CandidateListChangeListener.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.entityownership;
10
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;
15 import akka.actor.ActorRef;
16 import com.google.common.base.Preconditions;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.LinkedHashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
26 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
27 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
30 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;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Listens for candidate entries added/removed and notifies the EntityOwnershipShard appropriately.
43  *
44  * @author Moiz Raja
45  * @author Thomas Pantelis
46  */
47 class CandidateListChangeListener implements DOMDataTreeChangeListener {
48     private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
49
50     private final String logId;
51     private final ActorRef shard;
52     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
53
54     CandidateListChangeListener(ActorRef shard, String logId) {
55         this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
56         this.logId = logId;
57     }
58
59     void init(ShardDataTree shardDataTree) {
60         shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).
61                 node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME).
62                         node(Candidate.QNAME).node(Candidate.QNAME).build(), this);
63     }
64
65     @Override
66     public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
67         for(DataTreeCandidate change: changes) {
68             DataTreeCandidateNode changeRoot = change.getRootNode();
69             ModificationType type = changeRoot.getModificationType();
70
71             LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
72
73             NodeIdentifierWithPredicates candidateKey =
74                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
75             String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
76
77             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
78
79             if(type == ModificationType.WRITE || type == ModificationType.APPEARED) {
80                 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
81
82                 Collection<String> currentCandidates = addToCurrentCandidates(entityId, candidate);
83                 shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(currentCandidates)), shard);
84             } else if(type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
85                 LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
86
87                 Collection<String> currentCandidates = removeFromCurrentCandidates(entityId, candidate);
88                 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(currentCandidates)), shard);
89             }
90         }
91     }
92
93     private Collection<String> addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) {
94         Collection<String> candidates = currentCandidates.get(entityId);
95         if(candidates == null) {
96             candidates = new LinkedHashSet<>();
97             currentCandidates.put(entityId, candidates);
98         }
99
100         candidates.add(newCandidate);
101         return candidates;
102     }
103
104     private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
105         Collection<String> candidates = currentCandidates.get(entityId);
106         if(candidates != null) {
107             candidates.remove(candidateToRemove);
108             return candidates;
109         }
110
111         // Shouldn't happen
112         return Collections.emptyList();
113     }
114
115     private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
116         List<PathArgument> newPathArgs = new ArrayList<>();
117         for(PathArgument pathArg: candidatePath.getPathArguments()) {
118             newPathArgs.add(pathArg);
119             if(pathArg instanceof NodeIdentifierWithPredicates) {
120                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
121                 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
122                 if(ENTITY_ID_QNAME.equals(key.getKey())) {
123                     break;
124                 }
125             }
126         }
127
128         return YangInstanceIdentifier.create(newPathArgs);
129     }
130 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.