Refactor AbstractBrokerAwareActivator
[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 package org.opendaylight.controller.cluster.datastore.entityownership;
9
10 import static java.util.Objects.requireNonNull;
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 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 java.util.Optional;
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.mdsal.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(final ActorRef shard, final String logId) {
56         this.shard = requireNonNull(shard, "shard should not be null");
57         this.logId = logId;
58     }
59
60     void init(final 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, Optional.empty(), noop -> { /* NOOP */ });
64     }
65
66     @Override
67     public void onDataTreeChanged(final 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(final YangInstanceIdentifier entityId,
95             final String newCandidate) {
96         Collection<String> candidates = currentCandidates.computeIfAbsent(entityId, k -> new LinkedHashSet<>());
97         candidates.add(newCandidate);
98         return candidates;
99     }
100
101     private Collection<String> removeFromCurrentCandidates(final YangInstanceIdentifier entityId,
102             final String candidateToRemove) {
103         Collection<String> candidates = currentCandidates.get(entityId);
104         if (candidates != null) {
105             candidates.remove(candidateToRemove);
106             return candidates;
107         }
108
109         // Shouldn't happen
110         return Collections.emptyList();
111     }
112
113     private static YangInstanceIdentifier extractEntityPath(final 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())) {
121                     break;
122                 }
123             }
124         }
125
126         return YangInstanceIdentifier.create(newPathArgs);
127     }
128 }