2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.Timeout;
17 import io.netty.util.TimerTask;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.concurrent.CancellationException;
21 import java.util.concurrent.Future;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.AtomicReference;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
26 import org.opendaylight.openflowplugin.api.OFConstants;
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.lifecycle.ContextChainMastershipState;
30 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
31 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
32 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
33 import org.opendaylight.openflowplugin.impl.services.util.RequestContextUtil;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class RoleContextImpl implements RoleContext {
46 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
48 // Timeout after what we will give up on propagating role
49 private static final long SET_ROLE_TIMEOUT = 10000;
51 private final DeviceInfo deviceInfo;
52 private final HashedWheelTimer timer;
53 private final AtomicReference<ListenableFuture<RpcResult<SetRoleOutput>>> lastRoleFuture = new AtomicReference<>();
54 private final Collection<RequestContext<?>> requestContexts = new HashSet<>();
55 private final Timeout slaveTask;
56 private final OpenflowProviderConfig config;
57 private ContextChainMastershipWatcher contextChainMastershipWatcher;
58 private SalRoleService roleService;
60 RoleContextImpl(@Nonnull final DeviceInfo deviceInfo,
61 @Nonnull final HashedWheelTimer timer,
62 final long checkRoleMasterTimeout,
63 final OpenflowProviderConfig config) {
64 this.deviceInfo = deviceInfo;
67 slaveTask = timer.newTimeout((timerTask) -> makeDeviceSlave(), checkRoleMasterTimeout, TimeUnit.MILLISECONDS);
69 LOG.info("Started timer for setting SLAVE role on device {} if no role will be set in {}s.",
71 checkRoleMasterTimeout / 1000L);
75 public DeviceInfo getDeviceInfo() {
80 public void setRoleService(final SalRoleService salRoleService) {
81 roleService = salRoleService;
85 public void registerMastershipWatcher(@Nonnull final ContextChainMastershipWatcher newWatcher) {
86 this.contextChainMastershipWatcher = newWatcher;
91 changeLastRoleFuture(null);
92 requestContexts.forEach(requestContext -> RequestContextUtil
93 .closeRequestContextWithRpcError(requestContext, "Connection closed."));
94 requestContexts.clear();
98 public void instantiateServiceInstance() {
99 final ListenableFuture<RpcResult<SetRoleOutput>> future = sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
100 changeLastRoleFuture(future);
101 Futures.addCallback(future, new MasterRoleCallback(), MoreExecutors.directExecutor());
105 public ListenableFuture<Void> closeServiceInstance() {
106 changeLastRoleFuture(null);
107 return Futures.immediateFuture(null);
111 public <T> RequestContext<T> createRequestContext() {
112 final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceInfo.reserveXidForDeviceMessage()) {
114 public void close() {
115 requestContexts.remove(this);
119 requestContexts.add(ret);
125 public ServiceGroupIdentifier getIdentifier() {
126 return deviceInfo.getServiceIdentifier();
129 private void changeLastRoleFuture(final ListenableFuture<RpcResult<SetRoleOutput>> newFuture) {
131 lastRoleFuture.getAndUpdate(lastFuture -> {
132 if (lastFuture != null && !lastFuture.isCancelled() && !lastFuture.isDone()) {
133 lastFuture.cancel(true);
140 private ListenableFuture<RpcResult<SetRoleOutput>> makeDeviceSlave() {
141 final ListenableFuture<RpcResult<SetRoleOutput>> future = sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
142 changeLastRoleFuture(future);
143 Futures.addCallback(future, new SlaveRoleCallback(), MoreExecutors.directExecutor());
147 private ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole) {
148 final Boolean isEqualRole = config.isEnableEqualRole();
150 LOG.warn("Skip sending role change request to device {} as user enabled"
151 + " equal role for controller", deviceInfo);
152 return Futures.immediateFuture(null);
154 LOG.debug("Sending new role {} to device {}", newRole, deviceInfo);
156 if (deviceInfo.getVersion() >= OFConstants.OFP_VERSION_1_3) {
157 final SetRoleInput setRoleInput = new SetRoleInputBuilder()
158 .setControllerRole(newRole)
159 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
162 final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture = roleService.setRole(setRoleInput);
164 final TimerTask timerTask = timeout -> {
165 if (!setRoleOutputFuture.isDone()) {
166 LOG.warn("New role {} was not propagated to device {} during {} sec", newRole,
167 deviceInfo, SET_ROLE_TIMEOUT);
168 setRoleOutputFuture.cancel(true);
172 timer.newTimeout(timerTask, SET_ROLE_TIMEOUT, TimeUnit.MILLISECONDS);
173 return JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture);
176 LOG.info("Device: {} with version: {} does not support role {}", deviceInfo, deviceInfo.getVersion(), newRole);
177 return Futures.immediateFuture(null);
180 private final class MasterRoleCallback implements FutureCallback<RpcResult<SetRoleOutput>> {
182 public void onSuccess(RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
183 contextChainMastershipWatcher.onMasterRoleAcquired(
185 ContextChainMastershipState.MASTER_ON_DEVICE);
186 LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo);
190 public void onFailure(final Throwable throwable) {
191 if (!(throwable instanceof CancellationException)) {
192 contextChainMastershipWatcher.onNotAbleToStartMastershipMandatory(
194 "Was not able to propagate MASTER role on device. Error: " + throwable.toString());
199 private final class SlaveRoleCallback implements FutureCallback<RpcResult<SetRoleOutput>> {
201 public void onSuccess(final RpcResult<SetRoleOutput> result) {
202 contextChainMastershipWatcher.onSlaveRoleAcquired(deviceInfo);
203 LOG.debug("Role SLAVE was successfully set on device, node {}", deviceInfo);
207 public void onFailure(final Throwable throwable) {
208 if (!(throwable instanceof CancellationException)) {
209 contextChainMastershipWatcher.onSlaveRoleNotAcquired(deviceInfo,
210 "Was not able to propagate SLAVE role on device. Error: " + throwable.toString());