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