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 javax.annotation.CheckForNull;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
31 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
32 import org.opendaylight.openflowplugin.api.OFConstants;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
35 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
36 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
37 import org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtils;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Gets invoked from RpcManagerInitial, registers a candidate with EntityOwnershipService.
44 * On receipt of the ownership notification, makes an rpc call to SalRoleSevice.
46 * Hands over to StatisticsManager at the end.
48 public class RoleManagerImpl implements RoleManager {
49 private static final Logger LOG = LoggerFactory.getLogger(RoleManagerImpl.class);
51 private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
52 private final EntityOwnershipService entityOwnershipService;
53 private final RpcProviderRegistry rpcProviderRegistry;
54 private final ConcurrentHashMap<DeviceContext, RoleContext> contexts = new ConcurrentHashMap<>();
55 private final OpenflowOwnershipListener openflowOwnershipListener;
56 private final boolean switchFeaturesMandatory;
58 public RoleManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
59 final EntityOwnershipService entityOwnershipService, final boolean switchFeaturesMandatory) {
60 this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
61 this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
62 this.switchFeaturesMandatory = switchFeaturesMandatory;
63 this.openflowOwnershipListener = new OpenflowOwnershipListener(entityOwnershipService);
64 LOG.debug("Registering OpenflowOwnershipListener listening to all entity ownership changes");
65 openflowOwnershipListener.init();
69 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
70 deviceInitializationPhaseHandler = handler;
74 public void onDeviceContextLevelUp(@CheckForNull final DeviceContext deviceContext) {
75 LOG.debug("RoleManager called for device:{}", deviceContext.getPrimaryConnectionContext().getNodeId());
76 if (deviceContext.getDeviceState().getFeatures().getVersion() < OFConstants.OFP_VERSION_1_3) {
77 // Roles are not supported before OF1.3, so move forward.
78 deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
82 final RoleContext roleContext = new RoleContextImpl(deviceContext, rpcProviderRegistry, entityOwnershipService, openflowOwnershipListener);
83 // if the device context gets closed (mostly on connection close), we would need to cleanup
84 deviceContext.addDeviceContextClosedHandler(roleContext);
85 Verify.verify(contexts.putIfAbsent(deviceContext, roleContext) == null);
88 role = roleContext.initialization().get(5, TimeUnit.SECONDS);
89 } catch (InterruptedException | ExecutionException | TimeoutException | CandidateAlreadyRegisteredException e) {
90 LOG.warn("Unexpected exception by DeviceConection {}. Connection has to close.", deviceContext.getDeviceState().getNodeId(), e);
91 final Optional<EntityOwnershipState> entityOwnershipStateOptional = entityOwnershipService.getOwnershipState(roleContext.getEntity());
92 if (entityOwnershipStateOptional.isPresent()) {
93 // TODO : check again who will call RoleCtx.onRoleChanged
94 role = entityOwnershipStateOptional.get().isOwner() ? OfpRole.BECOMEMASTER : OfpRole.BECOMESLAVE;
97 deviceContext.close();
98 } catch (Exception e1) {
99 LOG.warn("Exception during device context close. ", e);
104 if (OfpRole.BECOMEMASTER.equals(role)) {
105 final ListenableFuture<Void> initNodeFuture = DeviceInitializationUtils.initializeNodeInformation(deviceContext, switchFeaturesMandatory);
106 Futures.addCallback(initNodeFuture, new FutureCallback<Void>() {
108 public void onSuccess(final Void result) {
109 LOG.trace("Node {} was initialized", deviceContext.getDeviceState().getNodeId());
110 getRoleContextLevelUp(deviceContext);
114 public void onFailure(final Throwable t) {
115 LOG.warn("Node {} Initialization fail", deviceContext.getDeviceState().getNodeId(), t);
117 deviceContext.close();
118 } catch (Exception e) {
119 LOG.warn("Exception during device context close. ", e);
124 getRoleContextLevelUp(deviceContext);
129 void getRoleContextLevelUp(final DeviceContext deviceContext) {
130 LOG.debug("Created role context for node {}", deviceContext.getDeviceState().getNodeId());
131 LOG.debug("roleChangeFuture success for device:{}. Moving to StatisticsManager", deviceContext.getDeviceState().getNodeId());
132 deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceContext);
136 public void close() throws Exception {
137 for (final Map.Entry<DeviceContext, RoleContext> roleContextEntry : contexts.entrySet()) {
138 if (roleContextEntry.getValue() != null) {
139 roleContextEntry.getValue().close();
142 this.openflowOwnershipListener.close();
146 public void onDeviceContextClosed(final DeviceContext deviceContext) {
147 LOG.debug("onDeviceContextClosed for node {}", deviceContext.getDeviceState().getNodeId());
148 final RoleContext removedContext = contexts.remove(deviceContext.getDeviceState().getNodeId());
149 if (removedContext != null) {
151 LOG.info("Unregistering rpcs for device context closure");
152 removedContext.close();
153 } catch (final Exception e) {
155 "Exception while unregistering rpcs onDeviceContextClosed handler for node:{}. But continuing.",
156 deviceContext.getDeviceState().getNodeId(), e);
159 LOG.info("No RpcContext to close , for device:{}", deviceContext.getDeviceState().getNodeId());