Refactor AbstractClientHandle a bit
[controller.git] / opendaylight / md-sal / sal-distributed-eos / src / main / java / org / opendaylight / controller / cluster / 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.entityownership;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.CANDIDATE_NAME_QNAME;
12 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
13 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
14 import static org.opendaylight.controller.cluster.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.Optional;
25 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
26 import org.opendaylight.controller.cluster.entityownership.messages.CandidateAdded;
27 import org.opendaylight.controller.cluster.entityownership.messages.CandidateRemoved;
28 import org.opendaylight.mdsal.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.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Listens for candidate entries added/removed and notifies the EntityOwnershipShard appropriately.
42  *
43  * @author Moiz Raja
44  * @author Thomas Pantelis
45  */
46 class CandidateListChangeListener implements DOMDataTreeChangeListener {
47     private static final Logger LOG = LoggerFactory.getLogger(CandidateListChangeListener.class);
48
49     private final String logId;
50     private final ActorRef shard;
51     private final Map<YangInstanceIdentifier, Collection<String>> currentCandidates = new HashMap<>();
52
53     CandidateListChangeListener(final ActorRef shard, final String logId) {
54         this.shard = requireNonNull(shard, "shard should not be null");
55         this.logId = logId;
56     }
57
58     void init(final ShardDataTree shardDataTree) {
59         shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH)
60             .node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME)
61                 .node(Candidate.QNAME).node(Candidate.QNAME).build(), this, Optional.empty(), noop -> { /* NOOP */ });
62     }
63
64     @Override
65     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
66         for (DataTreeCandidate change: changes) {
67             DataTreeCandidateNode changeRoot = change.getRootNode();
68             ModificationType type = changeRoot.getModificationType();
69
70             LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
71
72             NodeIdentifierWithPredicates candidateKey =
73                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
74             String candidate = candidateKey.getValue(CANDIDATE_NAME_QNAME).toString();
75
76             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
77
78             if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
79                 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
80
81                 Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
82                 shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
83             } else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
84                 LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
85
86                 Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
87                 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
88             }
89         }
90     }
91
92     private Collection<String> addToCurrentCandidates(final YangInstanceIdentifier entityId,
93             final String newCandidate) {
94         Collection<String> candidates = currentCandidates.computeIfAbsent(entityId, k -> new LinkedHashSet<>());
95         candidates.add(newCandidate);
96         return candidates;
97     }
98
99     private Collection<String> removeFromCurrentCandidates(final YangInstanceIdentifier entityId,
100             final String candidateToRemove) {
101         Collection<String> candidates = currentCandidates.get(entityId);
102         if (candidates != null) {
103             candidates.remove(candidateToRemove);
104             return candidates;
105         }
106
107         // Shouldn't happen
108         return Collections.emptyList();
109     }
110
111     private static YangInstanceIdentifier extractEntityPath(final YangInstanceIdentifier candidatePath) {
112         List<PathArgument> newPathArgs = new ArrayList<>();
113         for (PathArgument pathArg: candidatePath.getPathArguments()) {
114             newPathArgs.add(pathArg);
115             if (pathArg instanceof NodeIdentifierWithPredicates
116                     && ENTITY_ID_QNAME.equals(((NodeIdentifierWithPredicates) pathArg).keySet().iterator().next())) {
117                 break;
118             }
119         }
120
121         return YangInstanceIdentifier.create(newPathArgs);
122     }
123 }