2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.role;
10 import com.google.common.util.concurrent.FutureCallback;
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;
25 * Gets invoked from RpcManagerInitial, registers a candidate with EntityOwnershipService.
26 * On receipt of the ownership notification, makes an rpc call to SalRoleSevice.
28 * Hands over to StatisticsManager at the end.
30 public class RoleManagerImpl implements RoleManager {
31 private static final Logger LOG = LoggerFactory.getLogger(RoleManagerImpl.class);
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;
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();
48 public void setDeviceInitializationPhaseHandler(DeviceInitializationPhaseHandler handler) {
49 deviceInitializationPhaseHandler = handler;
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);
61 RoleContext roleContext = new RoleContextImpl(deviceContext, rpcProviderRegistry, entityOwnershipService, openflowOwnershipListener);
62 contexts.put(deviceContext, roleContext);
63 LOG.debug("Created role context");
65 // if the device context gets closed (mostly on connection close), we would need to cleanup
66 deviceContext.addDeviceContextClosedHandler(roleContext);
68 roleContext.facilitateRoleChange(new FutureCallback<Boolean>() {
70 public void onSuccess(Boolean aBoolean) {
71 LOG.debug("roleChangeFuture success for device:{}. Moving to StatisticsManager", deviceContext.getDeviceState().getNodeId());
72 deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
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);
81 deviceContext.close();
82 } catch (Exception e) {
83 LOG.warn("Error closing device context for device:{}",
84 deviceContext.getPrimaryConnectionContext().getNodeId().getValue(), e);
91 public void close() throws Exception {
92 for (Map.Entry<DeviceContext, RoleContext> roleContextEntry : contexts.entrySet()) {
93 roleContextEntry.getValue().close();
95 this.openflowOwnershipListener.close();