Fix warnings/javadocs in sal-distributed-datastore
[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
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;
24 import java.util.Map;
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;
41
42 /**
43  * Listens for candidate entries added/removed and notifies the EntityOwnershipShard appropriately.
44  *
45  * @author Moiz Raja
46  * @author Thomas Pantelis
47  */
48 class CandidateListChangeListener implements DOMDataTreeChangeListener {
49     private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
50
51     private final String logId;
52     private final ActorRef shard;
53     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
54
55     CandidateListChangeListener(ActorRef shard, String logId) {
56         this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
57         this.logId = logId;
58     }
59
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);
64     }
65
66     @Override
67     public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
68         for (DataTreeCandidate change: changes) {
69             DataTreeCandidateNode changeRoot = change.getRootNode();
70             ModificationType type = changeRoot.getModificationType();
71
72             LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
73
74             NodeIdentifierWithPredicates candidateKey =
75                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
76             String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
77
78             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
79
80             if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
81                 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
82
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);
87
88                 Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
89                 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
90             }
91         }
92     }
93
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);
99         }
100
101         candidates.add(newCandidate);
102         return candidates;
103     }
104
105     private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
106         Collection<String> candidates = currentCandidates.get(entityId);
107         if (candidates != null) {
108             candidates.remove(candidateToRemove);
109             return candidates;
110         }
111
112         // Shouldn't happen
113         return Collections.emptyList();
114     }
115
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())) {
124                     break;
125                 }
126             }
127         }
128
129         return YangInstanceIdentifier.create(newPathArgs);
130     }
131 }