Merge "Bug 4957 No empty transaction for every connection fix"
[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.util.concurrent.FutureCallback;
11 import java.util.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13 import javax.annotation.CheckForNull;
14 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
19 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
20 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Gets invoked from RpcManagerInitial, registers a candidate with EntityOwnershipService.
26  * On receipt of the ownership notification, makes an rpc call to SalRoleSevice.
27  *
28  * Hands over to StatisticsManager at the end.
29  */
30 public class RoleManagerImpl implements RoleManager {
31     private static final Logger LOG = LoggerFactory.getLogger(RoleManagerImpl.class);
32
33     private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
34     private EntityOwnershipService entityOwnershipService;
35     private final RpcProviderRegistry rpcProviderRegistry;
36     private final ConcurrentHashMap<DeviceContext, RoleContext> contexts = new ConcurrentHashMap<>();
37     private final OpenflowOwnershipListener openflowOwnershipListener;
38
39     public RoleManagerImpl(RpcProviderRegistry rpcProviderRegistry, EntityOwnershipService entityOwnershipService) {
40         this.entityOwnershipService = entityOwnershipService;
41         this.rpcProviderRegistry = rpcProviderRegistry;
42         this.openflowOwnershipListener = new OpenflowOwnershipListener(entityOwnershipService);
43         LOG.debug("Registering OpenflowOwnershipListener listening to all entity ownership changes");
44         openflowOwnershipListener.init();
45     }
46
47     @Override
48     public void setDeviceInitializationPhaseHandler(DeviceInitializationPhaseHandler handler) {
49         deviceInitializationPhaseHandler = handler;
50     }
51
52     @Override
53     public void onDeviceContextLevelUp(@CheckForNull final DeviceContext deviceContext) {
54         LOG.debug("RoleManager called for device:{}", deviceContext.getPrimaryConnectionContext().getNodeId());
55         if (deviceContext.getDeviceState().getFeatures().getVersion() < OFConstants.OFP_VERSION_1_3) {
56             // Roles are not supported before OF1.3, so move forward.
57             deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
58             return;
59         }
60
61         RoleContext roleContext = new RoleContextImpl(deviceContext, rpcProviderRegistry, entityOwnershipService, openflowOwnershipListener);
62         contexts.put(deviceContext, roleContext);
63         LOG.debug("Created role context");
64
65         // if the device context gets closed (mostly on connection close), we would need to cleanup
66         deviceContext.addDeviceContextClosedHandler(roleContext);
67
68         roleContext.facilitateRoleChange(new FutureCallback<Boolean>() {
69             @Override
70             public void onSuccess(Boolean aBoolean) {
71                 LOG.debug("roleChangeFuture success for device:{}. Moving to StatisticsManager", deviceContext.getDeviceState().getNodeId());
72                 deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
73             }
74
75             @Override
76             public void onFailure(Throwable throwable) {
77                 LOG.error("RoleChange on device {} was not successful after several attempts. " +
78                         "Closing the device Context, reconnect the device and start over",
79                         deviceContext.getPrimaryConnectionContext().getNodeId().getValue(), throwable);
80                 try {
81                     deviceContext.close();
82                 } catch (Exception e) {
83                     LOG.warn("Error closing device context for device:{}",
84                             deviceContext.getPrimaryConnectionContext().getNodeId().getValue(),  e);
85                 }
86             }
87         });
88     }
89
90     @Override
91     public void close() throws Exception {
92         for (Map.Entry<DeviceContext, RoleContext> roleContextEntry : contexts.entrySet()) {
93             roleContextEntry.getValue().close();
94         }
95         this.openflowOwnershipListener.close();
96     }
97 }