caf1ced48fe81d28ea909f0fda936b8e71cee218
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleManagerImpl.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.openflowplugin.impl.role;
9
10 import javax.annotation.CheckForNull;
11 import javax.annotation.Nullable;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.base.Verify;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import com.google.common.util.concurrent.FutureCallback;
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
29 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.openflowplugin.api.OFConstants;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
41 import org.opendaylight.openflowplugin.api.openflow.role.RoleChangeListener;
42 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
43 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
44 import org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtils;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Gets invoked from RpcManagerInitial, registers a candidate with EntityOwnershipService.
52  * On receipt of the ownership notification, makes an rpc call to SalRoleSevice.
53  *
54  * Hands over to StatisticsManager at the end.
55  */
56 public class RoleManagerImpl implements RoleManager, EntityOwnershipListener {
57     private static final Logger LOG = LoggerFactory.getLogger(RoleManagerImpl.class);
58
59     private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
60     private final DataBroker dataBroker;
61     private final EntityOwnershipService entityOwnershipService;
62     private final ConcurrentMap<Entity, RoleContext> contexts = new ConcurrentHashMap<>();
63     private final EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
64     private final boolean switchFeaturesMandatory;
65
66     public RoleManagerImpl(final EntityOwnershipService entityOwnershipService, final DataBroker dataBroker, final boolean switchFeaturesMandatory) {
67         this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
68         this.dataBroker = Preconditions.checkNotNull(dataBroker);
69         this.switchFeaturesMandatory = switchFeaturesMandatory;
70         this.entityOwnershipListenerRegistration = Preconditions.checkNotNull(entityOwnershipService.registerListener(RoleManager.ENTITY_TYPE, this));
71         LOG.debug("Registering OpenflowOwnershipListener listening to all entity ownership changes");
72     }
73
74     @Override
75     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
76         deviceInitializationPhaseHandler = handler;
77     }
78
79     @Override
80     public void onDeviceContextLevelUp(@CheckForNull final DeviceContext deviceContext) {
81         LOG.debug("RoleManager called for device:{}", deviceContext.getPrimaryConnectionContext().getNodeId());
82         if (deviceContext.getDeviceState().getFeatures().getVersion() < OFConstants.OFP_VERSION_1_3) {
83             // Roles are not supported before OF1.3, so move forward.
84             deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
85             return;
86         }
87
88         final RoleContext roleContext = new RoleContextImpl(deviceContext, entityOwnershipService,
89                 makeEntity(deviceContext.getDeviceState().getNodeId()));
90         // if the device context gets closed (mostly on connection close), we would need to cleanup
91         deviceContext.addDeviceContextClosedHandler(this);
92         Verify.verify(contexts.putIfAbsent(roleContext.getEntity(), roleContext) == null,
93                 "RoleCtx for master Node {} is still not close.", deviceContext.getDeviceState().getNodeId());
94         OfpRole role = null;
95         try {
96             role = roleContext.initialization().get(5, TimeUnit.SECONDS);
97         } catch (InterruptedException | ExecutionException | TimeoutException | CandidateAlreadyRegisteredException e) {
98             LOG.warn("Unexpected exception by DeviceConection {}. Connection has to close.", deviceContext.getDeviceState().getNodeId(), e);
99             final Optional<EntityOwnershipState> entityOwnershipStateOptional = entityOwnershipService.getOwnershipState(roleContext.getEntity());
100             if (entityOwnershipStateOptional.isPresent()) {
101                 // TODO : check again who will call RoleCtx.onRoleChanged but who will call DeviceCtx#onClusterRoleChange
102                 role = entityOwnershipStateOptional.get().isOwner() ? OfpRole.BECOMEMASTER : OfpRole.BECOMESLAVE;
103             } else {
104                 try {
105                     deviceContext.close();
106                 } catch (Exception e1) {
107                     LOG.warn("Exception during device context close. ", e);
108                 }
109                 return;
110             }
111         }
112         if (OfpRole.BECOMEMASTER.equals(role)) {
113             final ListenableFuture<Void> initNodeFuture = DeviceInitializationUtils.initializeNodeInformation(deviceContext, switchFeaturesMandatory);
114             Futures.addCallback(initNodeFuture, new FutureCallback<Void>() {
115                 @Override
116                 public void onSuccess(final Void result) {
117                     LOG.trace("Node {} was initialized", deviceContext.getDeviceState().getNodeId());
118                     getRoleContextLevelUp(deviceContext);
119                 }
120
121                 @Override
122                 public void onFailure(final Throwable t) {
123                     LOG.warn("Node {} Initialization fail", deviceContext.getDeviceState().getNodeId(), t);
124                     try {
125                         deviceContext.close();
126                     } catch (Exception e) {
127                         LOG.warn("Exception during device context close. ", e);
128                     }
129                 }
130             });
131         } else {
132             getRoleContextLevelUp(deviceContext);
133         }
134
135     }
136
137     void getRoleContextLevelUp(final DeviceContext deviceContext) {
138         LOG.debug("Created role context for node {}", deviceContext.getDeviceState().getNodeId());
139         LOG.debug("roleChangeFuture success for device:{}. Moving to StatisticsManager", deviceContext.getDeviceState().getNodeId());
140         deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
141     }
142
143     @Override
144     public void close() throws Exception {
145         entityOwnershipListenerRegistration.close();
146         for (final Map.Entry<Entity, RoleContext> roleContextEntry : contexts.entrySet()) {
147             // got here because last known role is LEADER and DS might need clearing up
148             final Entity entity = roleContextEntry.getKey();
149             final Optional<EntityOwnershipState> ownershipState = entityOwnershipService.getOwnershipState(entity);
150             final NodeId nodeId = roleContextEntry.getValue().getDeviceState().getNodeId();
151             if (ownershipState.isPresent())
152                 if ((!ownershipState.get().hasOwner())) {
153                     LOG.trace("Last role is LEADER and ownershipService returned hasOwner=false for node: {}; " +
154                             "cleaning DS as being probably the last owner", nodeId);
155                     removeDeviceFromOperDS(roleContextEntry.getValue());
156                 } else {
157                     // NOOP - there is another owner
158                     LOG.trace("Last role is LEADER and ownershipService returned hasOwner=true for node: {}; " +
159                             "leaving DS untouched", nodeId);
160                 }
161             else {
162                 // TODO: is this safe? When could this happen?
163                 LOG.warn("Last role is LEADER but ownershipService returned empty ownership info for node: {}; " +
164                         "cleaning DS ANYWAY!", nodeId);
165                 removeDeviceFromOperDS(roleContextEntry.getValue());
166             }
167         }
168         contexts.clear();
169     }
170
171     @Override
172     public void onDeviceContextClosed(final DeviceContext deviceContext) {
173         final NodeId nodeId = deviceContext.getDeviceState().getNodeId();
174         final OfpRole role = deviceContext.getDeviceState().getRole();
175         LOG.debug("onDeviceContextClosed for node {}", nodeId);
176
177         final Entity entity = makeEntity(nodeId);
178         final RoleContext roleContext = contexts.get(entity);
179         if (roleContext != null) {
180             LOG.debug("Found roleContext associated to deviceContext: {}, now closing the roleContext", nodeId);
181             roleContext.close();
182             if (role == null || OfpRole.BECOMESLAVE.equals(role)) {
183                 LOG.debug("No DS commitment for device {} - LEADER is somewhere else", nodeId);
184                 contexts.remove(entity, roleContext);
185             }
186         }
187     }
188
189     private static Entity makeEntity(final NodeId nodeId) {
190         return new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
191     }
192
193     private CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperDS(final RoleChangeListener roleChangeListener) {
194         Preconditions.checkArgument(roleChangeListener != null);
195         final DeviceState deviceState = roleChangeListener.getDeviceState();
196         final WriteTransaction delWtx = dataBroker.newWriteOnlyTransaction();
197         delWtx.delete(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier());
198         final CheckedFuture<Void, TransactionCommitFailedException> delFuture = delWtx.submit();
199         Futures.addCallback(delFuture, new FutureCallback<Void>() {
200
201             @Override
202             public void onSuccess(final Void result) {
203                 LOG.debug("Delete Node {} was successful", deviceState.getNodeId());
204             }
205
206             @Override
207             public void onFailure(final Throwable t) {
208                 LOG.warn("Delete Node {} fail.", deviceState.getNodeId(), t);
209             }
210         });
211         return delFuture;
212     }
213
214     @Override
215     public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
216         Preconditions.checkArgument(ownershipChange != null);
217         final RoleChangeListener roleChangeListener = contexts.get(ownershipChange.getEntity());
218
219         LOG.info("Received EntityOwnershipChange:{}, roleChangeListener-present={}", ownershipChange, (roleChangeListener != null));
220
221         if (roleChangeListener != null) {
222             LOG.debug("Found roleChangeListener for local entity:{}", ownershipChange.getEntity());
223             // if this was the master and entity does not have a master
224             if (ownershipChange.wasOwner() && !ownershipChange.isOwner() && !ownershipChange.hasOwner()) {
225                 LOG.info("Initiate removal from operational. Possibly the last node to be disconnected for :{}. ", ownershipChange);
226                 Futures.addCallback(removeDeviceFromOperDS(roleChangeListener),
227                         new FutureCallback<Void>() {
228                             @Override
229                             public void onSuccess(@Nullable final Void aVoid) {
230                                 LOG.debug("Freeing roleContext slot for device: {}", roleChangeListener.getDeviceState().getNodeId());
231                                 contexts.remove(ownershipChange.getEntity(), roleChangeListener);
232                             }
233
234                             @Override
235                             public void onFailure(final Throwable throwable) {
236                                 LOG.warn("NOT freeing roleContext slot for device: {}, {}",
237                                         roleChangeListener.getDeviceState().getNodeId(), throwable.getMessage());
238                             }
239                         });
240             } else {
241                 final OfpRole newRole = ownershipChange.isOwner() ? OfpRole.BECOMEMASTER : OfpRole.BECOMESLAVE;
242                 final OfpRole oldRole = ownershipChange.wasOwner() ? OfpRole.BECOMEMASTER : OfpRole.BECOMESLAVE;
243                 // send even if they are same. we do the check for duplicates in SalRoleService and maintain a lastKnownRole
244                 roleChangeListener.onRoleChanged(oldRole, newRole);
245             }
246         }
247     }
248 }