Adjust to DOMDataTreeChangeListener update
[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 onInitialData() {
66         // No-op
67     }
68
69     @Override
70     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
71         for (DataTreeCandidate change: changes) {
72             DataTreeCandidateNode changeRoot = change.getRootNode();
73             ModificationType type = changeRoot.getModificationType();
74
75             LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
76
77             NodeIdentifierWithPredicates candidateKey =
78                     (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
79             String candidate = candidateKey.getValue(CANDIDATE_NAME_QNAME).toString();
80
81             YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
82
83             if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
84                 LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
85
86                 Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
87                 shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
88             } else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
89                 LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
90
91                 Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
92                 shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
93             }
94         }
95     }
96
97     private Collection<String> addToCurrentCandidates(final YangInstanceIdentifier entityId,
98             final String newCandidate) {
99         Collection<String> candidates = currentCandidates.computeIfAbsent(entityId, k -> new LinkedHashSet<>());
100         candidates.add(newCandidate);
101         return candidates;
102     }
103
104     private Collection<String> removeFromCurrentCandidates(final YangInstanceIdentifier entityId,
105             final 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(final YangInstanceIdentifier candidatePath) {
117         List<PathArgument> newPathArgs = new ArrayList<>();
118         for (PathArgument pathArg: candidatePath.getPathArguments()) {
119             newPathArgs.add(pathArg);
120             if (pathArg instanceof NodeIdentifierWithPredicates
121                     && ENTITY_ID_QNAME.equals(((NodeIdentifierWithPredicates) pathArg).keySet().iterator().next())) {
122                 break;
123             }
124         }
125
126         return YangInstanceIdentifier.create(newPathArgs);
127     }
128 }