Bump MRI upstreams
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManager.java
1 /*
2  * Copyright (c) 2016, 2017 Pantheon Technologies s.r.o. 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.openflowplugin.applications.frm.impl;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Iterables;
12 import com.google.common.collect.Sets;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.mdsal.binding.api.DataTreeModification;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
28 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration;
29 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeService;
30 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
31 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
32 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Manager for clustering service registrations of {@link DeviceMastership}.
46  */
47 public class DeviceMastershipManager implements ClusteredDataTreeChangeListener<FlowCapableNode>, AutoCloseable,
48         MastershipChangeService {
49     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
50     private static final InstanceIdentifier<FlowCapableNode> II_TO_FLOW_CAPABLE_NODE = InstanceIdentifier
51             .builder(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).build();
52
53     private final ClusterSingletonServiceProvider clusterSingletonService;
54     private final FlowNodeReconciliation reconcliationAgent;
55     private final DataBroker dataBroker;
56     private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap<>();
57     private final Object lockObj = new Object();
58     private final RpcProviderService rpcProviderService;
59     private final FrmReconciliationService reconcliationService;
60
61     private ListenerRegistration<DeviceMastershipManager> listenerRegistration;
62     private Set<InstanceIdentifier<FlowCapableNode>> activeNodes = Collections.emptySet();
63     private MastershipChangeRegistration mastershipChangeServiceRegistration;
64
65     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Non-final for mocking")
66     public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
67                                    final FlowNodeReconciliation reconcliationAgent,
68                                    final DataBroker dataBroker,
69                                    final MastershipChangeServiceManager mastershipChangeServiceManager,
70                                    final RpcProviderService rpcProviderService,
71                                    final FrmReconciliationService reconciliationService) {
72         this.clusterSingletonService = clusterSingletonService;
73         this.reconcliationAgent = reconcliationAgent;
74         this.rpcProviderService = rpcProviderService;
75         reconcliationService = reconciliationService;
76         this.dataBroker = dataBroker;
77         registerNodeListener();
78         mastershipChangeServiceRegistration = mastershipChangeServiceManager.register(this);
79     }
80
81     public boolean isDeviceMastered(final NodeId nodeId) {
82         return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
83     }
84
85     public boolean isNodeActive(final NodeId nodeId) {
86         final InstanceIdentifier<FlowCapableNode> flowNodeIdentifier = InstanceIdentifier.create(Nodes.class)
87                 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
88         return activeNodes.contains(flowNodeIdentifier);
89
90     }
91
92     @VisibleForTesting
93     ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
94         return deviceMasterships;
95     }
96
97     @Override
98     public void onDataTreeChanged(@NonNull final Collection<DataTreeModification<FlowCapableNode>> changes) {
99         for (DataTreeModification<FlowCapableNode> change : changes) {
100             final InstanceIdentifier<FlowCapableNode> key = change.getRootPath().getRootIdentifier();
101             final DataObjectModification<FlowCapableNode> mod = change.getRootNode();
102             final InstanceIdentifier<FlowCapableNode> nodeIdent = key.firstIdentifierOf(FlowCapableNode.class);
103
104             switch (mod.getModificationType()) {
105                 case DELETE:
106                     if (mod.getDataAfter() == null) {
107                         remove(key, mod.getDataBefore(), nodeIdent);
108                     }
109                     break;
110                 case SUBTREE_MODIFIED:
111                     // NO-OP since we do not need to reconcile on Node-updated
112                     break;
113                 case WRITE:
114                     if (mod.getDataBefore() == null) {
115                         add(key, mod.getDataAfter(), nodeIdent);
116                     }
117                     break;
118                 default:
119                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
120             }
121         }
122     }
123
124     public void remove(final InstanceIdentifier<FlowCapableNode> identifier, final FlowCapableNode del,
125             final InstanceIdentifier<FlowCapableNode> nodeIdent) {
126         if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE)) {
127             if (LOG.isDebugEnabled()) {
128                 LOG.debug("Node removed: {}", nodeIdent.firstKeyOf(Node.class).getId().getValue());
129             }
130
131             if (!nodeIdent.isWildcarded() && activeNodes.contains(nodeIdent)) {
132                 synchronized (lockObj) {
133                     if (activeNodes.contains(nodeIdent)) {
134                         Set<InstanceIdentifier<FlowCapableNode>> set = Sets.newHashSet(activeNodes);
135                         set.remove(nodeIdent);
136                         reconcliationAgent.flowNodeDisconnected(nodeIdent);
137                         activeNodes = Collections.unmodifiableSet(set);
138                         setNodeOperationalStatus(nodeIdent, false);
139                     }
140                 }
141             }
142         }
143     }
144
145     public void add(final InstanceIdentifier<FlowCapableNode> identifier, final FlowCapableNode add,
146             final InstanceIdentifier<FlowCapableNode> nodeIdent) {
147         if (compareInstanceIdentifierTail(identifier, II_TO_FLOW_CAPABLE_NODE)) {
148             if (LOG.isDebugEnabled()) {
149                 LOG.debug("Node added: {}", nodeIdent.firstKeyOf(Node.class).getId().getValue());
150             }
151
152             if (!nodeIdent.isWildcarded() && !activeNodes.contains(nodeIdent)) {
153                 synchronized (lockObj) {
154                     if (!activeNodes.contains(nodeIdent)) {
155                         Set<InstanceIdentifier<FlowCapableNode>> set = Sets.newHashSet(activeNodes);
156                         set.add(nodeIdent);
157                         activeNodes = Collections.unmodifiableSet(set);
158                         setNodeOperationalStatus(nodeIdent, true);
159                     }
160                 }
161             }
162         }
163     }
164
165     @Override
166     public void close() throws Exception {
167         if (listenerRegistration != null) {
168             listenerRegistration.close();
169             listenerRegistration = null;
170         }
171         if (mastershipChangeServiceRegistration != null) {
172             mastershipChangeServiceRegistration.close();
173             mastershipChangeServiceRegistration = null;
174         }
175     }
176
177     private static boolean compareInstanceIdentifierTail(final InstanceIdentifier<?> identifier1,
178             final InstanceIdentifier<?> identifier2) {
179         return Iterables.getLast(identifier1.getPathArguments())
180                 .equals(Iterables.getLast(identifier2.getPathArguments()));
181     }
182
183     private void setNodeOperationalStatus(final InstanceIdentifier<FlowCapableNode> nodeIid, final boolean status) {
184         NodeId nodeId = nodeIid.firstKeyOf(Node.class).getId();
185         if (nodeId != null && deviceMasterships.containsKey(nodeId)) {
186             deviceMasterships.get(nodeId).setDeviceOperationalStatus(status);
187             LOG.debug("Operational status of device {} is set to {}", nodeId, status);
188         }
189     }
190
191     @SuppressWarnings("IllegalCatch")
192     private void registerNodeListener() {
193
194         final InstanceIdentifier<FlowCapableNode> flowNodeWildCardIdentifier = InstanceIdentifier.create(Nodes.class)
195                 .child(Node.class).augmentation(FlowCapableNode.class);
196
197         final DataTreeIdentifier<FlowCapableNode> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
198                 flowNodeWildCardIdentifier);
199
200         try {
201             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
202                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
203
204             listenerRegistration = looper.loopUntilNoException(
205                 () -> dataBroker.registerDataTreeChangeListener(treeId, DeviceMastershipManager.this));
206         } catch (Exception e) {
207             LOG.warn("Data listener registration failed: {}", e.getMessage());
208             LOG.debug("Data listener registration failed ", e);
209             throw new IllegalStateException("Node listener registration failed!", e);
210         }
211     }
212
213     @Override
214     public void onBecomeOwner(@NonNull final DeviceInfo deviceInfo) {
215         LOG.debug("Mastership role notification received for device : {}", deviceInfo.getDatapathId());
216         DeviceMastership membership = deviceMasterships.computeIfAbsent(deviceInfo.getNodeId(),
217             device -> new DeviceMastership(deviceInfo.getNodeId()));
218         membership.reconcile();
219         membership.registerReconciliationRpc(rpcProviderService, reconcliationService);
220     }
221
222     @Override
223     public void onLoseOwnership(@NonNull final DeviceInfo deviceInfo) {
224         final DeviceMastership mastership = deviceMasterships.remove(deviceInfo.getNodeId());
225         if (mastership != null) {
226             mastership.deregisterReconciliationRpc();
227             mastership.close();
228             LOG.debug("Unregistered deviceMastership for device : {}", deviceInfo.getNodeId());
229         }
230     }
231 }