Fix sonar warnings 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.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;
25 import java.util.Map;
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.controller.md.sal.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;
42
43 /**
44  * Listens for candidate entries added/removed and notifies the EntityOwnershipShard appropriately.
45  *
46  * @author Moiz Raja
47  * @author Thomas Pantelis
48  */
49 class CandidateListChangeListener implements DOMDataTreeChangeListener {
50     private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
51
52     private final String logId;
53     private final ActorRef shard;
54     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
55
56     CandidateListChangeListener(ActorRef shard, String logId) {
57         this.shard = Preconditions.checkNotNull(shard, "shard should not be null");
58         this.logId = logId;
59     }
60
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 */ });
65     }
66
67     @Override
68     public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
69         for (DataTreeCandidate change: changes) {
70             DataTreeCandidateNode changeRoot = change.getRootNode();
71             ModificationType type = changeRoot.getModificationType();
72
73             LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
74
75             NodeIdentifierWithPredicates candidateKey =
76                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
77             String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
78
79             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
80
81             if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
82                 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
83
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);
88
89                 Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
90                 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
91             }
92         }
93     }
94
95     private Collection<String> addToCurrentCandidates(YangInstanceIdentifier entityId, String newCandidate) {
96         Collection<String> candidates = currentCandidates.get(entityId);
97         if (candidates == null) {
98             candidates = new LinkedHashSet<>();
99             currentCandidates.put(entityId, candidates);
100         }
101
102         candidates.add(newCandidate);
103         return candidates;
104     }
105
106     private Collection<String> removeFromCurrentCandidates(YangInstanceIdentifier entityId, String candidateToRemove) {
107         Collection<String> candidates = currentCandidates.get(entityId);
108         if (candidates != null) {
109             candidates.remove(candidateToRemove);
110             return candidates;
111         }
112
113         // Shouldn't happen
114         return Collections.emptyList();
115     }
116
117     private static YangInstanceIdentifier extractEntityPath(YangInstanceIdentifier candidatePath) {
118         List<PathArgument> newPathArgs = new ArrayList<>();
119         for (PathArgument pathArg: candidatePath.getPathArguments()) {
120             newPathArgs.add(pathArg);
121             if (pathArg instanceof NodeIdentifierWithPredicates) {
122                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
123                 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
124                 if (ENTITY_ID_QNAME.equals(key.getKey())) {
125                     break;
126                 }
127             }
128         }
129
130         return YangInstanceIdentifier.create(newPathArgs);
131     }
132 }