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.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.JdkFutureAdapters;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import io.netty.util.HashedWheelTimer;
18 import io.netty.util.TimerTask;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
30 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
31 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
32 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
33 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Role context try to make change device role on device
47 class RoleContextImpl implements RoleContext {
49 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
50 // Maximum limit of timeout retries when cleaning DS, to prevent infinite recursive loops
51 private static final int MAX_CLEAN_DS_RETRIES = 3;
53 private SalRoleService salRoleService = null;
54 private final HashedWheelTimer hashedWheelTimer;
55 private final DeviceInfo deviceInfo;
56 private CONTEXT_STATE state;
57 private final RoleManager myManager;
58 private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
60 RoleContextImpl(final DeviceInfo deviceInfo,
61 final HashedWheelTimer hashedWheelTimer,
62 final RoleManager myManager) {
63 this.deviceInfo = deviceInfo;
64 state = CONTEXT_STATE.WORKING;
65 this.myManager = myManager;
66 this.hashedWheelTimer = hashedWheelTimer;
71 public <T> RequestContext<T> createRequestContext() {
72 return new AbstractRequestContext<T>(deviceInfo.reserveXidForDeviceMessage()) {
80 public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
81 Preconditions.checkNotNull(salRoleService);
82 this.salRoleService = salRoleService;
86 public CONTEXT_STATE getState() {
91 public void setState(CONTEXT_STATE state) {
96 public ServiceGroupIdentifier getServiceIdentifier() {
97 return this.deviceInfo.getServiceIdentifier();
101 public DeviceInfo getDeviceInfo() {
102 return this.deviceInfo;
105 public void startupClusterServices() throws ExecutionException, InterruptedException {
106 Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new FutureCallback<RpcResult<SetRoleOutput>>() {
108 public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getLOGValue());
115 public void onFailure(final Throwable throwable) {
116 LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
122 public ListenableFuture<Void> stopClusterServices(final boolean deviceDisconnected) {
124 if (!deviceDisconnected) {
125 ListenableFuture<Void> future = Futures.transform(makeDeviceSlave(), new Function<RpcResult<SetRoleOutput>, Void>() {
128 public Void apply(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
133 Futures.addCallback(future, new FutureCallback<Void>() {
135 public void onSuccess(@Nullable Void aVoid) {
136 if (LOG.isDebugEnabled()) {
137 LOG.debug("Role SLAVE was successfully propagated on device, node {}", deviceInfo.getLOGValue());
142 public void onFailure(final Throwable throwable) {
143 LOG.warn("Was not able to set role SLAVE to device on node {} ", deviceInfo.getLOGValue());
144 LOG.trace("Error occurred on device role setting, probably connection loss: ", throwable);
145 myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
151 return myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
156 public ListenableFuture<RpcResult<SetRoleOutput>> makeDeviceSlave(){
157 return sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
161 ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole) {
162 LOG.debug("Sending new role {} to device {}", newRole, deviceInfo.getNodeId());
163 final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture;
164 final Short version = deviceInfo.getVersion();
165 if (null == version) {
166 LOG.debug("Device version is null");
167 return Futures.immediateFuture(null);
169 if (version < OFConstants.OFP_VERSION_1_3) {
170 LOG.debug("Device version not support ROLE");
171 return Futures.immediateFuture(null);
173 final SetRoleInput setRoleInput = (new SetRoleInputBuilder()).setControllerRole(newRole)
174 .setNode(new NodeRef(DeviceStateUtil.createNodeInstanceIdentifier(deviceInfo.getNodeId()))).build();
175 setRoleOutputFuture = this.salRoleService.setRole(setRoleInput);
176 final TimerTask timerTask = timeout -> {
177 if (!setRoleOutputFuture.isDone()) {
178 LOG.warn("New role {} was not propagated to device {} during 10 sec", newRole, deviceInfo.getLOGValue());
179 setRoleOutputFuture.cancel(true);
182 hashedWheelTimer.newTimeout(timerTask, 10, TimeUnit.SECONDS);
184 return JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture);
188 public void setLifecycleInitializationPhaseHandler(final ClusterInitializationPhaseHandler handler) {
189 this.clusterInitializationPhaseHandler = handler;
193 public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
195 if (connectionContext.getConnectionState().equals(ConnectionContext.CONNECTION_STATE.RIP)) {
196 LOG.warn("Connection on device {} was interrupted, will stop starting master services.", deviceInfo.getLOGValue());
200 Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new FutureCallback<RpcResult<SetRoleOutput>>() {
202 public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
203 if (LOG.isDebugEnabled()) {
204 LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getLOGValue());
209 public void onFailure(final Throwable throwable) {
210 LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
214 return this.clusterInitializationPhaseHandler.onContextInstantiateService(connectionContext);