Bug 6554 Fix rejecting connections
[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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Iterators;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import io.netty.util.HashedWheelTimer;
18 import java.util.Iterator;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21 import javax.annotation.CheckForNull;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
30 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
32 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
33 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
34 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
35 import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Gets invoked from RpcManagerInitial, registers a candidate with EntityOwnershipService.
43  * On receipt of the ownership notification, makes an rpc call to SalRoleService.
44  *
45  * Hands over to StatisticsManager at the end.
46  */
47 public class RoleManagerImpl implements RoleManager {
48     private static final Logger LOG = LoggerFactory.getLogger(RoleManagerImpl.class);
49
50     // Maximum limit of timeout retries when cleaning DS, to prevent infinite recursive loops
51     private static final int MAX_CLEAN_DS_RETRIES = 3;
52
53     private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
54     private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
55     private final DataBroker dataBroker;
56     private final ConcurrentMap<DeviceInfo, RoleContext> contexts = new ConcurrentHashMap<>();
57     private final HashedWheelTimer hashedWheelTimer;
58
59     public RoleManagerImpl(final DataBroker dataBroker, final HashedWheelTimer hashedWheelTimer) {
60         this.dataBroker = Preconditions.checkNotNull(dataBroker);
61         this.hashedWheelTimer = hashedWheelTimer;
62     }
63
64     @Override
65     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
66         deviceInitializationPhaseHandler = handler;
67     }
68
69     @Override
70     public void onDeviceContextLevelUp(@CheckForNull final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
71         final DeviceContext deviceContext = Preconditions.checkNotNull(lifecycleService.getDeviceContext());
72         final RoleContext roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, this, lifecycleService);
73         roleContext.setSalRoleService(new SalRoleServiceImpl(roleContext, deviceContext));
74         Verify.verify(contexts.putIfAbsent(deviceInfo, roleContext) == null, "Role context for master Node %s is still not closed.", deviceInfo.getLOGValue());
75         Futures.addCallback(roleContext.makeDeviceSlave(), new FutureCallback<RpcResult<SetRoleOutput>>() {
76                     @Override
77                     public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
78                         if (LOG.isDebugEnabled()) {
79                             LOG.debug("Role SLAVE was successfully propagated on device, node {}", deviceInfo.getLOGValue());
80                         }
81                     }
82
83                     @Override
84                     public void onFailure(Throwable throwable) {
85                         LOG.warn("Was not able to set role SLAVE to device on node {} ",deviceInfo.getLOGValue());
86                         lifecycleService.closeConnection();
87                     }
88                 });
89         lifecycleService.setRoleContext(roleContext);
90         deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
91     }
92
93     @Override
94     public void close() {
95         LOG.debug("Close method on role manager was called.");
96         for (final Iterator<RoleContext> iterator = Iterators.consumingIterator(contexts.values().iterator()); iterator.hasNext();) {
97             // got here because last known role is LEADER and DS might need clearing up
98             final RoleContext roleContext = iterator.next();
99             contexts.remove(roleContext.getDeviceInfo());
100             removeDeviceFromOperationalDS(roleContext.getDeviceInfo());
101         }
102     }
103
104     @Override
105     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
106         contexts.remove(deviceInfo);
107         deviceTerminationPhaseHandler.onDeviceContextLevelDown(deviceInfo);
108     }
109
110     @Override
111     public CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperationalDS(final DeviceInfo deviceInfo) {
112         final WriteTransaction delWtx = dataBroker.newWriteOnlyTransaction();
113         delWtx.delete(LogicalDatastoreType.OPERATIONAL, deviceInfo.getNodeInstanceIdentifier());
114         final CheckedFuture<Void, TransactionCommitFailedException> delFuture = delWtx.submit();
115
116         Futures.addCallback(delFuture, new FutureCallback<Void>() {
117             @Override
118             public void onSuccess(final Void result) {
119                 if (LOG.isDebugEnabled()) {
120                     LOG.debug("Delete Node {} was successful", deviceInfo.getLOGValue());
121                 }
122             }
123
124             @Override
125             public void onFailure(@Nonnull final Throwable t) {
126                 LOG.warn("Delete node {} failed with exception {}", deviceInfo.getLOGValue(), t);
127             }
128         });
129
130         return delFuture;
131     }
132
133     @Override
134     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
135         deviceTerminationPhaseHandler = handler;
136     }
137
138     @VisibleForTesting
139     RoleContext getRoleContext(final DeviceInfo deviceInfo){
140         return contexts.get(deviceInfo);
141     }
142
143 }