/** * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowplugin.impl.role; import javax.annotation.Nullable; import java.util.concurrent.Future; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Verify; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.JdkFutureAdapters; import com.google.common.util.concurrent.ListenableFuture; import io.netty.util.Timeout; import io.netty.util.TimerTask; import java.util.concurrent.Future; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException; import org.opendaylight.controller.md.sal.common.api.clustering.Entity; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService; import org.opendaylight.openflowplugin.api.OFConstants; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceState; import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; import org.opendaylight.openflowplugin.api.openflow.role.RoleContext; import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext; import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef; import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole; import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService; import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by kramesha on 9/12/15. */ public class RoleContextImpl implements RoleContext { private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class); private final EntityOwnershipService entityOwnershipService; private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration; private EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration; private final DeviceContext deviceContext; private final Entity entity; private final Entity txEntity; private SalRoleService salRoleService; private final Semaphore mainCandidateGuard = new Semaphore(1, true); private final Semaphore txCandidateGuard = new Semaphore(1, true); public RoleContextImpl(final DeviceContext deviceContext, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity) { this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService); this.deviceContext = Preconditions.checkNotNull(deviceContext); this.entity = Preconditions.checkNotNull(entity); this.txEntity = Preconditions.checkNotNull(txEntity); salRoleService = new SalRoleServiceImpl(this, deviceContext); } @Override public void initialization() throws CandidateAlreadyRegisteredException { LOG.debug("Initialization RoleContext for Node {}", deviceContext.getDeviceState().getNodeId()); final AsyncFunction, Void> initFunction = new AsyncFunction, Void>() { @Override public ListenableFuture apply(final RpcResult input) throws Exception { LOG.debug("Initialization request OpenflowEntityOwnership for entity {}", entity); getDeviceState().setRole(OfpRole.BECOMESLAVE); entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity); LOG.debug("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext .getPrimaryConnectionContext().getNodeId().getValue()); return Futures.immediateFuture(null); } }; final ListenableFuture roleChange = sendRoleChangeToDevice(OfpRole.BECOMESLAVE, initFunction); Futures.addCallback(roleChange, new FutureCallback() { @Override public void onSuccess(final Void result) { LOG.debug("Initial RoleContext for Node {} is successful", deviceContext.getDeviceState().getNodeId()); } @Override public void onFailure(final Throwable t) { LOG.warn("Initial RoleContext for Node {} fail", deviceContext.getDeviceState().getNodeId(), t); deviceContext.close(); } }); } @Override public ListenableFuture onRoleChanged(final OfpRole oldRole, final OfpRole newRole) { LOG.trace("onRoleChanged method call for Entity {}", entity); if (!isDeviceConnected()) { // this can happen as after the disconnect, we still get a last message from EntityOwnershipService. LOG.info("Device {} is disconnected from this node. Hence not attempting a role change.", deviceContext.getPrimaryConnectionContext().getNodeId()); LOG.debug("SetRole cancelled for entity [{}], reason = device disconnected.", entity); return Futures.immediateFailedFuture(new Exception( "Device disconnected - stopped by setRole: " + deviceContext .getPrimaryConnectionContext().getNodeId())); } LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole, deviceContext.getPrimaryConnectionContext().getNodeId()); final AsyncFunction, Void> roleChangeFunction = new AsyncFunction, Void>() { @Override public ListenableFuture apply(final RpcResult setRoleOutputRpcResult) throws Exception { LOG.debug("Role change {} successful made on switch :{}", newRole, deviceContext.getDeviceState() .getNodeId()); getDeviceState().setRole(newRole); return deviceContext.onClusterRoleChange(oldRole, newRole); } }; return sendRoleChangeToDevice(newRole, roleChangeFunction); } @Override public void setupTxCandidate() throws CandidateAlreadyRegisteredException { LOG.debug("setupTxCandidate for entity {} and Transaction entity {}", entity, txEntity); Verify.verify(txEntity != null); txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(txEntity); } @Override public void close() { if (entityOwnershipCandidateRegistration != null) { LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity); entityOwnershipCandidateRegistration.close(); } } @Override public Entity getEntity() { return entity; } @Override public Entity getTxEntity() { return txEntity; } private boolean isDeviceConnected() { return ConnectionContext.CONNECTION_STATE.WORKING.equals( deviceContext.getPrimaryConnectionContext().getConnectionState()); } @Nullable @Override public RequestContext createRequestContext() { final AbstractRequestContext ret = new AbstractRequestContext(deviceContext.reservedXidForDeviceMessage()) { @Override public void close() { } }; return ret; } @VisibleForTesting void setSalRoleService(final SalRoleService salRoleService) { this.salRoleService = salRoleService; } @Override public DeviceState getDeviceState() { return deviceContext.getDeviceState(); } @Override public void suspendTxCandidate() { if (txEntityOwnershipCandidateRegistration != null) { txEntityOwnershipCandidateRegistration.close(); txEntityOwnershipCandidateRegistration = null; } } @Override public DeviceContext getDeviceContext() { return deviceContext; } @Override public Semaphore getMainCandidateGuard() { return mainCandidateGuard; } @Override public Semaphore getTxCandidateGuard() { return txCandidateGuard; } private ListenableFuture sendRoleChangeToDevice(final OfpRole newRole, final AsyncFunction, Void> function) { LOG.debug("Send new role {} to device {}", newRole, deviceContext.getDeviceState().getNodeId()); final Future> setRoleOutputFuture; if (deviceContext.getDeviceState().getFeatures().getVersion() < OFConstants.OFP_VERSION_1_3) { LOG.debug("Device OF version {} not support ROLE", deviceContext.getDeviceState().getFeatures().getVersion()); setRoleOutputFuture = Futures.immediateFuture(RpcResultBuilder. success().build()); } else { final SetRoleInput setRoleInput = (new SetRoleInputBuilder()).setControllerRole(newRole) .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier())).build(); setRoleOutputFuture = salRoleService.setRole(setRoleInput); final TimerTask timerTask = new TimerTask() { @Override public void run(final Timeout timeout) throws Exception { if (!setRoleOutputFuture.isDone()) { LOG.info("New role {} was not propagated to device {} during 10 sec. Close connection immediately.", newRole, deviceContext.getDeviceState().getNodeId()); deviceContext.close(); } } }; deviceContext.getTimer().newTimeout(timerTask, 10, TimeUnit.SECONDS); } return Futures.transform(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), function); } }