private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
private final DataBroker dataBroker;
private final EntityOwnershipService entityOwnershipService;
- private final ConcurrentMap<NodeId, RoleContext> contexts = new ConcurrentHashMap<>();
+ private final ConcurrentMap<DeviceInfo, RoleContext> contexts = new ConcurrentHashMap<>();
private final ConcurrentMap<Entity, RoleContext> watchingEntities = new ConcurrentHashMap<>();
private final EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
private final EntityOwnershipListenerRegistration txEntityOwnershipListenerRegistration;
final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo));
final RoleContext roleContext = new RoleContextImpl(deviceInfo, entityOwnershipService, makeEntity(deviceInfo.getNodeId()), makeTxEntity(deviceInfo.getNodeId()), conductor);
roleContext.setSalRoleService(new SalRoleServiceImpl(roleContext, deviceContext));
- Verify.verify(contexts.putIfAbsent(deviceInfo.getNodeId(), roleContext) == null, "Role context for master Node %s is still not closed.", deviceInfo.getNodeId());
+ Verify.verify(contexts.putIfAbsent(deviceInfo, roleContext) == null, "Role context for master Node %s is still not closed.", deviceInfo.getNodeId());
makeDeviceRoleChange(OfpRole.BECOMESLAVE, roleContext, true);
/* First start to watch entity so we don't miss any notification, and then try to register in EOS */
notifyListenersRoleInitializationDone(roleContext.getDeviceInfo(), roleContext.initialization());
final RoleContext roleContext = iterator.next();
watchingEntities.remove(roleContext.getEntity());
watchingEntities.remove(roleContext.getTxEntity());
- contexts.remove(roleContext.getDeviceInfo().getNodeId());
+ contexts.remove(roleContext.getDeviceInfo());
if (roleContext.isTxCandidateRegistered()) {
LOG.info("Node {} was holder txEntity, so trying to remove device from operational DS.");
- removeDeviceFromOperationalDS(roleContext.getDeviceInfo().getNodeId());
+ removeDeviceFromOperationalDS(roleContext.getDeviceInfo());
} else {
roleContext.close();
}
@Override
public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
LOG.trace("onDeviceContextLevelDown for node {}", deviceInfo.getNodeId());
- final RoleContext roleContext = contexts.get(deviceInfo.getNodeId());
+ final RoleContext roleContext = contexts.get(deviceInfo);
if (roleContext != null) {
LOG.debug("Found roleContext associated to deviceContext: {}, now trying close the roleContext", deviceInfo.getNodeId());
if (roleContext.isMainCandidateRegistered()) {
roleContext.unregisterCandidate(roleContext.getTxEntity());
if (ownershipChange.wasOwner() && !ownershipChange.isOwner() && !ownershipChange.hasOwner()) {
LOG.debug("Trying to remove from operational node: {}", roleContext.getDeviceInfo().getNodeId());
- removeDeviceFromOperationalDS(roleContext.getDeviceInfo().getNodeId());
+ removeDeviceFromOperationalDS(roleContext.getDeviceInfo());
}
} else {
- final NodeId nodeId = roleContext.getDeviceInfo().getNodeId();
- contexts.remove(nodeId, roleContext);
+ contexts.remove(roleContext.getDeviceInfo(), roleContext);
roleContext.close();
conductor.closeConnection(roleContext.getDeviceInfo());
}
roleContext.unregisterCandidate(roleContext.getTxEntity());
if (!ownershipChange.hasOwner()) {
LOG.debug("Trying to remove from operational node: {}", roleContext.getDeviceInfo().getNodeId());
- removeDeviceFromOperationalDS(roleContext.getDeviceInfo().getNodeId());
+ removeDeviceFromOperationalDS(roleContext.getDeviceInfo());
} else {
final NodeId nodeId = roleContext.getDeviceInfo().getNodeId();
- contexts.remove(nodeId, roleContext);
+ contexts.remove(roleContext.getDeviceInfo(), roleContext);
roleContext.close();
conductor.closeConnection(roleContext.getDeviceInfo());
}
} else {
LOG.debug("Tx-EntityOwnershipRegistration is not active for entity {}", ownershipChange.getEntity().getType());
watchingEntities.remove(roleContext.getTxEntity(), roleContext);
- final NodeId nodeId = roleContext.getDeviceInfo().getNodeId();
- contexts.remove(nodeId, roleContext);
+ contexts.remove(roleContext.getDeviceInfo(), roleContext);
roleContext.close();
conductor.closeConnection(roleContext.getDeviceInfo());
}
ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole, final RoleContext roleContext) {
LOG.debug("Sending new role {} to device {}", newRole, roleContext.getDeviceInfo().getNodeId());
final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture;
- final Short version = conductor.gainVersionSafely(roleContext.getDeviceInfo());
+ final Short version = roleContext.getDeviceInfo().getVersion();
if (null == version) {
LOG.debug("Device version is null");
return Futures.immediateFuture(null);
}
@VisibleForTesting
- CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperationalDS(final NodeId nodeId) {
+ CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperationalDS(final DeviceInfo deviceInfo) {
final WriteTransaction delWtx = dataBroker.newWriteOnlyTransaction();
- delWtx.delete(LogicalDatastoreType.OPERATIONAL, DeviceStateUtil.createNodeInstanceIdentifier(nodeId));
+ delWtx.delete(LogicalDatastoreType.OPERATIONAL, DeviceStateUtil.createNodeInstanceIdentifier(deviceInfo.getNodeId()));
final CheckedFuture<Void, TransactionCommitFailedException> delFuture = delWtx.submit();
Futures.addCallback(delFuture, new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
- LOG.debug("Delete Node {} was successful", nodeId);
- final RoleContext roleContext = contexts.remove(nodeId);
+ LOG.debug("Delete Node {} was successful", deviceInfo);
+ final RoleContext roleContext = contexts.remove(deviceInfo);
if (roleContext != null) {
roleContext.close();
}
@Override
public void onFailure(@Nonnull final Throwable t) {
- LOG.warn("Delete Node {} failed. {}", nodeId, t);
- contexts.remove(nodeId);
- final RoleContext roleContext = contexts.remove(nodeId);
+ LOG.warn("Delete Node {} failed. {}", deviceInfo, t);
+ contexts.remove(deviceInfo);
+ final RoleContext roleContext = contexts.remove(deviceInfo);
if (roleContext != null) {
roleContext.close();
}
@Override
public void servicesChangeDone(final DeviceInfo deviceInfo, final boolean success) {
LOG.debug("Services stopping done for node {} as " + (success ? "successful" : "unsuccessful"), deviceInfo);
- final RoleContext roleContext = contexts.get(deviceInfo.getNodeId());
+ final RoleContext roleContext = contexts.get(deviceInfo);
if (null != roleContext) {
/* Services stopped or failure */
roleContext.unregisterCandidate(roleContext.getTxEntity());
}
@VisibleForTesting
- RoleContext getRoleContext(final NodeId nodeId){
- return contexts.get(nodeId);
+ RoleContext getRoleContext(final DeviceInfo deviceInfo){
+ return contexts.get(deviceInfo);
}
/**
* This method is only for testing
*/
@VisibleForTesting
- void setRoleContext(NodeId nodeId, RoleContext roleContext){
- if(!contexts.containsKey(nodeId)) {
- contexts.put(nodeId, roleContext);
+ void setRoleContext(DeviceInfo deviceInfo, RoleContext roleContext){
+ if(!contexts.containsKey(deviceInfo)) {
+ contexts.put(deviceInfo, roleContext);
}
}
@RunWith(MockitoJUnitRunner.class)
public class DeviceStateImplTest {
- @Mock
- private FeaturesReply featuresReply;
private DeviceStateImpl deviceState;
- private final short version = 13;
- private final long portNr = 10L;
- private final Long portBandwidth = 1024L;
- private final List<PhyPort> pPort = Arrays.asList(new PhyPortBuilder()
- .setPortNo(portNr).setMaxSpeed(portBandwidth).build());
-
@Before
public void initialization() {
- Mockito.when(featuresReply.getVersion()).thenReturn(version);
- Mockito.when(featuresReply.getPhyPort()).thenReturn(pPort);
deviceState = new DeviceStateImpl();
}
import com.google.common.base.VerifyException;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
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.lifecycle.LifecycleConductor;
import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
+import org.opendaylight.yangtools.yang.common.RpcResult;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
@Mock
DeviceInfo deviceInfo2;
+ @Mock
+ MessageSpy messageSpy;
+
+ @Mock
+ OutboundQueue outboundQueue;
@Mock
GetFeaturesOutput featuresOutput;
Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
+ Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
+ Mockito.when(deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider()).thenReturn(outboundQueue);
Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
- Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
- Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
+ Mockito.when(deviceInfo.getDatapathId()).thenReturn(new BigInteger("1"));
+ Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
+ Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<DeviceInfo>any());
Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceInfo>any());
Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
Mockito.when(conductor.getDeviceContext(deviceInfo)).thenReturn(deviceContext);
roleManagerSpy = Mockito.spy(roleManager);
roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
- roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
+ Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
+ roleContextSpy = Mockito.spy(roleManager.getRoleContext(deviceInfo));
+ Mockito.when(roleContextSpy.getDeviceInfo()).thenReturn(deviceInfo);
Mockito.when(roleContextSpy.getDeviceInfo().getNodeId()).thenReturn(nodeId);
+// Mockito.when(roleManagerSpy.sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any())).thenReturn(rpcFuture);
inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
}
roleManagerSpy.ownershipChanged(masterTxEntity);
roleManagerSpy.close();
inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
- inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
+ inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(deviceInfo);
inOrder.verifyNoMoreInteractions();
}
roleManagerSpy.ownershipChanged(slaveEntity);
roleManagerSpy.close();
inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
- inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
+ inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(deviceInfo);
inOrder.verifyNoMoreInteractions();
}
inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
inOrder.verify(roleContextSpy, Mockito.never()).close();
- inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
+ inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
}
@Test
@Override
public void roleChangeOnDevice(final DeviceInfo deviceInfo_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
- Assert.assertTrue(deviceInfo.equals(deviceInfo_));
+ Assert.assertTrue(RoleManagerImplTest.this.deviceInfo.equals(deviceInfo_));
Assert.assertTrue(success);
Assert.assertFalse(initializationPhase);
Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
roleManager.notifyListenersRoleInitializationDone(deviceInfo, true);
roleManager.notifyListenersRoleChangeOnDevice(deviceInfo, true, OfpRole.BECOMEMASTER, false);
}
-
- @Test
- public void testMakeDeviceRoleChange() throws Exception{
- roleManagerSpy.makeDeviceRoleChange(OfpRole.BECOMEMASTER, roleContextSpy, true);
- verify(roleManagerSpy, atLeastOnce()).sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any());
- verify(roleManagerSpy, atLeastOnce()).notifyListenersRoleChangeOnDevice(Mockito.<DeviceInfo>any(), eq(true), Mockito.<OfpRole>any(), eq(true));
- }
-
+
@Test
public void testServicesChangeDone() throws Exception {
- roleManagerSpy.setRoleContext(nodeId2, roleContextSpy);
+ roleManagerSpy.setRoleContext(deviceInfo2, roleContextSpy);
roleManagerSpy.servicesChangeDone(deviceInfo2, true);
verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
}
@Test
public void testServicesChangeDoneContextIsNull() throws Exception {
- roleManagerSpy.setRoleContext(nodeId, roleContextSpy);
+ roleManagerSpy.setRoleContext(deviceInfo, roleContextSpy);
roleManagerSpy.servicesChangeDone(deviceInfo2, true);
verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());
}
@Mock
private DeviceState deviceState;
@Mock
- private DeviceInfo deviceInfo;
- @Mock
private XidSequencer xidSequencer;
@Mock
private MessageSpy messageSpy;
private NotificationPublishService notificationPublishService;
@Mock
private TestRpcService serviceInstance;
+ @Mock
+ private DeviceInfo deviceInfo;
private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
@RunWith(MockitoJUnitRunner.class)
public class StatisticsGatheringUtilsTest {
- public static final String DUMMY_NODE_ID_VALUE = "1";
- public static final NodeId DUMMY_NODE_ID = new NodeId(DUMMY_NODE_ID_VALUE);
+ static final String DUMMY_NODE_ID_VALUE = "1";
+ static final NodeId DUMMY_NODE_ID = new NodeId(DUMMY_NODE_ID_VALUE);
private final KeyedInstanceIdentifier<Node, NodeKey> dummyNodePath = InstanceIdentifier.create(Nodes.class)
.child(Node.class, new NodeKey(DUMMY_NODE_ID));
@Mock
DeviceState mockedDeviceState;
@Mock
+ DeviceInfo mockedDeviceInfo;
+ @Mock
DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
@Mock
DeviceTerminationPhaseHandler mockedTerminationPhaseHandler;
statisticsGatheringService = new StatisticsGatheringOnTheFlyService(mockedRequestContextStack, mockedDeviceContext);
Mockito.doReturn(NODE_ID).when(mockedPrimConnectionContext).getNodeId();
Mockito.when(mockedDeviceInfo.getNodeId()).thenReturn(NODE_ID);
+ Mockito.when(mockedDeviceContext.getDeviceInfo().getNodeId()).thenReturn(NODE_ID);
}
@Test
protected DeviceState deviceState;
@Mock
protected DeviceInfo deviceInfo;
+ @Mock
+ protected GetFeaturesOutput getFeaturesOutput;
protected NodeConnectorId nodeConnectorId;
protected KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
when(deviceInfo.getNodeId()).thenReturn(new NodeId(NODE_ID));
when(deviceInfo.getVersion()).thenReturn(OF_VERSION);
when(deviceInfo.getDatapathId()).thenReturn(DATAPATH_ID);
+ when(getFeaturesOutput.getVersion()).thenReturn(OF_VERSION);
+ when(getFeaturesOutput.getDatapathId()).thenReturn(DATAPATH_ID);
when(connectionContext.getFeatures()).thenReturn(features);
when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
when(features.getVersion()).thenReturn(OF_VERSION);
private AggregatedFlowStatisticsTranslator translator;
@Mock
- private DeviceState deviceState;
- @Mock
private DeviceInfo deviceInfo;
@Before
builder.setMatchV10(new MatchV10Builder().setWildcards(flowWildcards).build());
} else {
builder.setMatch(new MatchBuilder().setMatchEntry(Collections.<MatchEntry>emptyList()).build())
- .setTableId(new TableId(42l));
+ .setTableId(new TableId(42L));
}
return builder.build();
private DeviceInfo mockedDeviceInfo;
@Mock
private DeviceInitializationUtils deviceInitializationUtils;
+ @Mock
+ private DeviceInfo deviceInfo;
@Before
public void setUp() throws Exception {
final ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
final DeviceState mockedDeviceState = mock(DeviceState.class);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-
final DeviceInfo mockedDeviceInfo = mock(DeviceInfo.class);
+ when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeatures);
- final GetFeaturesOutput mockedFeaturesOutput = mock(GetFeaturesOutput.class);
-
final BigInteger mockedDataPathId = mock(BigInteger.class);
when(mockedFeatures.getDatapathId()).thenReturn(mockedDataPathId);
- when(mockedFeaturesOutput.getDatapathId()).thenReturn(mockedDataPathId);
when(mockedDeviceInfo.getDatapathId()).thenReturn(mockedDataPathId);
when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedConnectionContext);