import java.math.BigInteger;
import java.util.List;
import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
+
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.openflowplugin.api.openflow.OFPContext;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
* newRole is {@link OfpRole#BECOMESLAVE}.
* Parameters are used as marker to be sure it is change to SLAVE from MASTER or from
* MASTER to SLAVE and the last parameter "cleanDataStore" is used for validation only.
- * @param oldRole - old role for quick validation for needed processing
* @param role - NewRole expect to be {@link OfpRole#BECOMESLAVE} or {@link OfpRole#BECOMEMASTER}
* @return RoleChangeTxChainManager future for activation/deactivation
* @deprecated replaced by method onDeviceTakeClusterLeadership and onDevicLostClusterLeadership
*/
@Deprecated
- ListenableFuture<Void> onClusterRoleChange(@Nullable OfpRole oldRole, @CheckForNull OfpRole role);
+ ListenableFuture<Void> onClusterRoleChange(@CheckForNull OfpRole role);
/**
* Method has to activate TransactionChainManager and prepare all Contexts from Device Contects suite
*/
ListenableFuture<Void> onDeviceTakeClusterLeadership();
- /**
- * Method has to deactivate TransactionChainManager and prepare all Contexts from Device Contects suite
- * to Lost ClusterLeadership role {@link OfpRole#BECOMESLAVE} (e.g. Stop RPC rounting, stop StatPolling ...)
- * @return RoleChangeTxChainManager future for deactivation
- */
- ListenableFuture<Void> onDeviceLostClusterLeadership();
-
/**
* Method has to close TxManager ASAP we are notified about Closed Connection
* @return sync. future for Slave and MD-SAL completition for Master
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.openflowplugin.impl.util.MdSalRegistrationUtils;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
LOG.info("Role change to {} in role context for node {} was successful, starting/stopping services.", newRole, deviceInfo);
+ final String logText;
+
if (OfpRole.BECOMEMASTER.equals(newRole)) {
- statisticsManager.startScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
+ logText = "Start";
+ statisticsManager.startScheduling(deviceInfo);
+ MdSalRegistrationUtils.registerMasterServices(
+ rpcManager.gainContext(deviceInfo),
+ deviceContext,
+ OfpRole.BECOMEMASTER);
} else {
- statisticsManager.stopScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
+ logText = "Stopp";
+ statisticsManager.stopScheduling(deviceInfo);
+ MdSalRegistrationUtils.registerSlaveServices(
+ rpcManager.gainContext(deviceInfo),
+ OfpRole.BECOMESLAVE);
}
- final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(null, newRole);
+ final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(newRole);
Futures.addCallback(onClusterRoleChange, new FutureCallback<Void>() {
@Override
public void onSuccess(@Nullable final Void aVoid) {
- LOG.info("Starting/Stopping services for node {} was successful", deviceInfo);
+ LOG.info("{}ing services for node {} was successful", logText, deviceInfo);
if (newRole.equals(OfpRole.BECOMESLAVE)) notifyServiceChangeListeners(deviceInfo, true);
}
@Override
public void onFailure(final Throwable throwable) {
- LOG.warn("Starting/Stopping services for node {} was NOT successful, closing connection", deviceInfo);
+ LOG.warn("ing services for node {} was NOT successful, closing connection", logText, deviceInfo);
closeConnection(deviceInfo);
}
});
}
@Override
- public ListenableFuture<Void> onClusterRoleChange(final OfpRole oldRole, @CheckForNull final OfpRole role) {
+ public ListenableFuture<Void> onClusterRoleChange(@CheckForNull final OfpRole role) {
LOG.trace("onClusterRoleChange {} for node:", role, deviceInfo.getNodeId());
- Preconditions.checkArgument(role != null);
- if (role.equals(oldRole)) {
- LOG.debug("Demanded role change for device {} is not changed. OldRole: {}, NewRole {}", deviceInfo.getNodeId(), oldRole, role);
- return Futures.immediateFuture(null);
- }
if (OfpRole.BECOMEMASTER.equals(role)) {
return onDeviceTakeClusterLeadership();
- } else if (OfpRole.BECOMESLAVE.equals(role)) {
- return onDeviceLostClusterLeadership();
- } else {
- LOG.warn("Unknown OFCluster Role {} for Node {}", role, deviceInfo.getNodeId());
- if (null != rpcContext) {
- MdSalRegistrationUtils.unregisterServices(rpcContext);
- }
- return transactionChainManager.deactivateTransactionManager();
- }
- }
-
- @Override
- public ListenableFuture<Void> onDeviceLostClusterLeadership() {
- LOG.trace("onDeviceLostClusterLeadership for node: {}", deviceInfo.getNodeId());
- if (null != rpcContext) {
- MdSalRegistrationUtils.registerSlaveServices(rpcContext, OfpRole.BECOMESLAVE);
}
return transactionChainManager.deactivateTransactionManager();
}
LOG.warn(errMsg);
return Futures.immediateFailedFuture(new IllegalStateException(errMsg));
}
- /* Routed RPC registration */
- MdSalRegistrationUtils.registerMasterServices(getRpcContext(), DeviceContextImpl.this, OfpRole.BECOMEMASTER);
if (isStatisticsRpcEnabled) {
MdSalRegistrationUtils.registerStatCompatibilityServices(getRpcContext(), this,
import com.google.common.util.concurrent.ListenableFuture;
import io.netty.util.HashedWheelTimer;
import io.netty.util.TimerTask;
+
+import java.math.BigInteger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
+import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@Mock
private StatisticsManager statisticsManager;
@Mock
+ private RpcManager rpcManager;
+ @Mock
+ private RpcContext rpcContext;
+ @Mock
private DeviceInfo deviceInfo;
private NodeId nodeId = new NodeId("openflow-junit:1");
lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
lifecycleConductor.setSafelyManager(deviceManager);
lifecycleConductor.setSafelyManager(statisticsManager);
+ lifecycleConductor.setSafelyManager(rpcManager);
when(connectionContext.getFeatures()).thenReturn(featuresReply);
when(deviceInfo.getNodeId()).thenReturn(nodeId);
+ when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+ when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
+ when(rpcManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(rpcContext);
}
public void roleChangeOnDeviceTest4() {
when(deviceContext.getDeviceState()).thenReturn(deviceState);
when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
+ when(deviceContext.onClusterRoleChange(OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMEMASTER,false);
verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
}
public void roleChangeOnDeviceTest5() {
when(deviceContext.getDeviceState()).thenReturn(deviceState);
when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
+ when(deviceContext.onClusterRoleChange(OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMESLAVE,false);
verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
}
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import com.google.common.base.Optional;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
+import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
+import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
import org.opendaylight.openflowplugin.api.openflow.device.Xid;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("55");
Xid xid;
Xid xidMulti;
- DeviceContextImpl deviceContext;
+
+ DeviceContext deviceContext;
@Mock
TransactionChainManager txChainManager;
@Mock
RequestContext<GetAsyncReply> requestContext;
@Mock
RequestContext<MultipartReply> requestContextMultiReply;
-
@Mock
ConnectionContext connectionContext;
@Mock
private DeviceContext deviceContextSpy;
@Before
- public void setUp() {
+ public void setUp() throws Exception{
final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent());
Mockito.when(rTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class))).thenReturn(txChainFactory);
Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent);
Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
- Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+ Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.ONE);
final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply = SettableFuture.create();
Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
Mockito.when(lifecycleConductor.getMessageIntelligenceAgency()).thenReturn(messageIntelligenceAgency);
deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, lifecycleConductor, outboundQueueProvider, translatorLibrary, false);
-
deviceContextSpy = Mockito.spy(deviceContext);
xid = new Xid(atomicLong.incrementAndGet());
xidMulti = new Xid(atomicLong.incrementAndGet());
+
+ Mockito.doNothing().when(deviceContextSpy).writeToTransaction(Mockito.<LogicalDatastoreType>any(), Mockito.<InstanceIdentifier>any(), any());
}
@Test(expected = NullPointerException.class)
public void testInitialSubmitTransaction() throws Exception {
Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
final InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
- deviceContext.getTransactionChainManager().activateTransactionManager() ;
- deviceContext.getTransactionChainManager().enableSubmit();
+ ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
+ ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
- deviceContext.initialSubmitTransaction();
+ ((DeviceContextImpl) deviceContext).initialSubmitTransaction();
verify(wTx).submit();
}
@Test
public void testAddDeleteToTxChain() throws Exception{
final InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
- deviceContext.getTransactionChainManager().activateTransactionManager() ;
- deviceContext.getTransactionChainManager().enableSubmit();
+ ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
+ ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
verify(wTx).delete(eq(LogicalDatastoreType.CONFIGURATION), eq(dummyII));
}
*/
@Test
public void testSubmitTransaction() throws Exception {
- deviceContext.getTransactionChainManager().activateTransactionManager() ;
- deviceContext.getTransactionChainManager().enableSubmit();
+ ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
+ ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
assertTrue(deviceContext.submitTransaction());
}
when(mockedPortStatusMessage.getPortNo()).thenReturn(42L);
OpenflowPortsUtil.init();
- deviceContext.processPortStatusMessage(mockedPortStatusMessage);
-// verify(txChainManager).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(DataObject.class));
+ deviceContextSpy.processPortStatusMessage(mockedPortStatusMessage);
+ verify(deviceContextSpy).writeToTransaction(Mockito.<LogicalDatastoreType>any(), Mockito.<InstanceIdentifier>any(), any());
+ verify(deviceContextSpy).submitTransaction();
}
@Test
final NotificationPublishService mockedNotificationPublishService = mock(NotificationPublishService.class);
deviceContext.setNotificationPublishService(mockedNotificationPublishService);
- deviceContext.setExtensionConverterProvider(mockedExtensionConverterProvider);
+ ((DeviceContextImpl) deviceContext).setExtensionConverterProvider(mockedExtensionConverterProvider);
deviceContext.processExperimenterMessage(experimenterMessage);
verify(mockedNotificationPublishService).offerNotification(any(ExperimenterMessageFromDev.class));
@Test
public void testOnClusterRoleChange() throws Exception {
- // test role.equals(oldRole)
- Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.BECOMEMASTER, OfpRole.BECOMEMASTER).get());
// test call transactionChainManager.deactivateTransactionManager()
- Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE, OfpRole.NOCHANGE).get());
+ Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE).get());
// test call MdSalRegistrationUtils.unregisterServices(rpcContext)
final RpcContext rpcContext = mock(RpcContext.class);
deviceContextSpy.setRpcContext(rpcContext);
- Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE, OfpRole.NOCHANGE).get());
+ Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE).get());
final StatisticsContext statisticsContext = mock(StatisticsContext.class);
deviceContextSpy.setStatisticsContext(statisticsContext);
- deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE, OfpRole.BECOMEMASTER);
+ deviceContextSpy.onClusterRoleChange(OfpRole.BECOMEMASTER);
verify(deviceContextSpy).onDeviceTakeClusterLeadership();
Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
- deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE, OfpRole.BECOMESLAVE);
- verify(deviceContextSpy).onDeviceLostClusterLeadership();
+ deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE);
}
}