Merge "Remove deprecated EOS services"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceContextImplTest.java
index 8f7b7c976fd7c209a1f9635599007878d775f48a..bbcc12ea4e0ab4672e4238b61c28b29c339d948c 100644 (file)
@@ -37,8 +37,7 @@ 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.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
@@ -58,9 +57,8 @@ import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
-import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
-import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
+import org.opendaylight.openflowplugin.common.txchain.TransactionChainManager;
 import org.opendaylight.openflowplugin.extension.api.ConvertorMessageFromOFJava;
 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
 import org.opendaylight.openflowplugin.impl.device.initialization.AbstractDeviceInitializer;
@@ -120,40 +118,41 @@ public class DeviceContextImplTest {
     private static final Long DUMMY_XID = 544L;
     private static final Long DUMMY_PORT_NUMBER = 159L;
     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("55");
-    Xid xid;
-    Xid xidMulti;
+    private Xid xid;
+    private Xid xidMulti;
 
-    DeviceContext deviceContext;
+    private DeviceContext deviceContext;
     @Mock
-    RequestContext<GetAsyncReply> requestContext;
+    private RequestContext<GetAsyncReply> requestContext;
     @Mock
-    RequestContext<MultipartReply> requestContextMultiReply;
+    private RequestContext<MultipartReply> requestContextMultiReply;
     @Mock
-    ConnectionContext connectionContext;
+    private ConnectionContext connectionContext;
     @Mock
-    GetFeaturesOutput featuresOutput;
+    private GetFeaturesOutput featuresOutput;
     @Mock
-    DataBroker dataBroker;
+    private DataBroker dataBroker;
     @Mock
-    WriteTransaction wTx;
+    private ReadWriteTransaction writeTx;
     @Mock
-    ReadOnlyTransaction rTx;
+    private ReadOnlyTransaction readTx;
     @Mock
-    BindingTransactionChain txChainFactory;
+    private BindingTransactionChain txChainFactory;
     @Mock
-    HashedWheelTimer timer;
+    private HashedWheelTimer timer;
     @Mock
-    OutboundQueueProvider outboundQueueProvider;
+    private OutboundQueueProvider outboundQueueProvider;
     @Mock
-    ConnectionAdapter connectionAdapter;
-    NodeId nodeId = new NodeId("h2g2:42");
-    KeyedInstanceIdentifier<Node, NodeKey> nodeKeyIdent = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
+    private ConnectionAdapter connectionAdapter;
+    private final NodeId nodeId = new NodeId("h2g2:42");
+    private final KeyedInstanceIdentifier<Node, NodeKey> nodeKeyIdent =
+            DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
     @Mock
-    TranslatorLibrary translatorLibrary;
+    private TranslatorLibrary translatorLibrary;
     @Mock
     MessageTranslator messageTranslatorPacketReceived;
     @Mock
-    MessageTranslator messageTranslatorFlowCapableNodeConnector;
+    private MessageTranslator messageTranslatorFlowCapableNodeConnector;
     @Mock
     private MessageTranslator<Object, Object> messageTranslatorFlowRemoved;
     @Mock
@@ -174,11 +173,13 @@ public class DeviceContextImplTest {
     private DeviceContext deviceContextSpy;
 
     @Before
-    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);
+    public void setUp() throws Exception {
+        final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture =
+                Futures.immediateCheckedFuture(Optional.<Node>absent());
+        Mockito.when(readTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
+        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(readTx);
+        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.ONE);
@@ -195,8 +196,8 @@ public class DeviceContextImplTest {
             settableFutureMultiReply.set((RpcResult<MultipartReply>) invocation.getArguments()[0]);
             return null;
         }).when(requestContextMultiReply).setResult(any(RpcResult.class));
-        Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
-        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
+        Mockito.when(txChainFactory.newReadWriteTransaction()).thenReturn(writeTx);
+        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(readTx);
         Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
         Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
         Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
@@ -209,18 +210,22 @@ public class DeviceContextImplTest {
         Mockito.when(featuresOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
 
         final PacketReceived packetReceived = new PacketReceivedBuilder()
-                .setMatch(new org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder()
+                .setMatch(new org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received
+                        .MatchBuilder()
                         .setInPort(new NodeConnectorId("openflow:1:LOCAL"))
                         .build())
                 .build();
 
-        Mockito.when(messageTranslatorPacketReceived.translate(any(Object.class), any(DeviceInfo.class), any(Object.class))).thenReturn(packetReceived);
-        Mockito.when(messageTranslatorFlowCapableNodeConnector.translate(any(Object.class), any(DeviceInfo.class), any(Object.class))).thenReturn(mock(FlowCapableNodeConnector.class));
-        Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PacketIn.class.getName())))).thenReturn(messageTranslatorPacketReceived);
-        Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PortGrouping.class.getName())))).thenReturn(messageTranslatorFlowCapableNodeConnector);
+        Mockito.when(messageTranslatorPacketReceived.translate(any(Object.class), any(DeviceInfo.class),
+                any(Object.class))).thenReturn(packetReceived);
+        Mockito.when(messageTranslatorFlowCapableNodeConnector.translate(any(Object.class), any(DeviceInfo.class),
+                any(Object.class))).thenReturn(mock(FlowCapableNodeConnector.class));
         Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3,
-                FlowRemoved.class.getName()))))
-                .thenReturn(messageTranslatorFlowRemoved);
+                PacketIn.class.getName())))).thenReturn(messageTranslatorPacketReceived);
+        Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3,
+                PortGrouping.class.getName())))).thenReturn(messageTranslatorFlowCapableNodeConnector);
+        Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3,
+                FlowRemoved.class.getName())))).thenReturn(messageTranslatorFlowRemoved);
 
         Mockito.when(abstractDeviceInitializer.initialize(any(), anyBoolean(), anyBoolean(), any(), any()))
                 .thenReturn(Futures.immediateFuture(null));
@@ -253,23 +258,20 @@ public class DeviceContextImplTest {
 
     @Test
     public void testGetReadTransaction() {
-        final ReadTransaction readTx = deviceContext.getReadTransaction();
+        readTx = deviceContext.getReadTransaction();
         assertNotNull(readTx);
-        assertEquals(rTx, readTx);
+        assertEquals(this.readTx, readTx);
     }
 
-    /**
-     * @throws Exception
-     */
     @Test
     public void testInitialSubmitTransaction() throws Exception {
-        Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
+        Mockito.when(writeTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
         final InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
         ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
-        ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().initialSubmitWriteTransaction();
         deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
         deviceContext.initialSubmitTransaction();
-        verify(wTx).submit();
+        verify(writeTx).submit();
     }
 
     private ConnectionContext prepareConnectionContext() {
@@ -280,25 +282,19 @@ public class DeviceContextImplTest {
         return mockedConnectionContext;
     }
 
-    /**
-     * @throws Exception
-     */
     @Test
-    public void testAddDeleteToTxChain() throws Exception{
+    public void testAddDeleteToTxChain() throws Exception {
         final InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
         ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
-        ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().initialSubmitWriteTransaction();
         deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
-        verify(wTx).delete(eq(LogicalDatastoreType.CONFIGURATION), eq(dummyII));
+        verify(writeTx).delete(eq(LogicalDatastoreType.CONFIGURATION), eq(dummyII));
     }
 
-    /**
-     * @throws Exception
-     */
     @Test
     public void testSubmitTransaction() throws Exception {
         ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
-        ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().initialSubmitWriteTransaction();
         assertTrue(deviceContext.submitTransaction());
     }
 
@@ -353,9 +349,10 @@ public class DeviceContextImplTest {
     public void testProcessPacketInMessageFutureSuccess() {
         final PacketInMessage mockedPacketInMessage = mock(PacketInMessage.class);
         final NotificationPublishService mockedNotificationPublishService = mock(NotificationPublishService.class);
-        final ListenableFuture stringListenableFuture = Futures.immediateFuture(new String("dummy value"));
+        final ListenableFuture stringListenableFuture = Futures.immediateFuture("dummy value");
 
-        when(mockedNotificationPublishService.offerNotification(any(PacketReceived.class))).thenReturn(stringListenableFuture);
+        when(mockedNotificationPublishService.offerNotification(any(PacketReceived.class)))
+                .thenReturn(stringListenableFuture);
         deviceContext.setNotificationPublishService(mockedNotificationPublishService);
         deviceContext.processPacketInMessage(mockedPacketInMessage);
         verify(messageSpy).spyMessage(any(Class.class), eq(MessageSpy.StatisticsGroup.FROM_SWITCH));
@@ -371,7 +368,8 @@ public class DeviceContextImplTest {
         when(mockedNotificationPublishService.offerNotification(any(PacketReceived.class))).thenReturn(dummyFuture);
         deviceContext.setNotificationPublishService(mockedNotificationPublishService);
         deviceContext.processPacketInMessage(mockedPacketInMessage);
-        verify(messageSpy).spyMessage(any(Class.class), eq(MessageSpy.StatisticsGroup.FROM_SWITCH_NOTIFICATION_REJECTED));
+        verify(messageSpy).spyMessage(any(Class.class),
+                eq(MessageSpy.StatisticsGroup.FROM_SWITCH_NOTIFICATION_REJECTED));
     }
 
     @Test
@@ -396,7 +394,7 @@ public class DeviceContextImplTest {
     }
 
     @Test
-    public void testPortStatusMessage() throws Exception{
+    public void testPortStatusMessage() throws Exception {
         final PortStatusMessage mockedPortStatusMessage = mock(PortStatusMessage.class);
         final Class dummyClass = Class.class;
         when(mockedPortStatusMessage.getImplementedInterface()).thenReturn(dummyClass);
@@ -423,20 +421,16 @@ public class DeviceContextImplTest {
                 .setMatch(new MatchBuilder().build());
         final NotificationPublishService mockedNotificationPublishService = mock(NotificationPublishService.class);
 
-        Mockito.when(messageTranslatorFlowRemoved.translate(any(Object.class), any(DeviceInfo.class), any(Object.class)))
+        Mockito.when(messageTranslatorFlowRemoved
+                .translate(any(Object.class), any(DeviceInfo.class), any(Object.class)))
                 .thenReturn(flowRemovedMdsalBld.build());
 
         // insert flow+flowId into local registry
-        final FlowRegistryKey flowRegKey = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowRemovedMdsalBld.build());
+        final FlowRegistryKey flowRegKey =
+                FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowRemovedMdsalBld.build());
         final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create((short) 0, new FlowId("ut-ofp:f456"));
         deviceContext.getDeviceFlowRegistry().storeDescriptor(flowRegKey, flowDescriptor);
 
-        // plug in lifecycleListener
-        final ItemLifecycleListener itemLifecycleListener = Mockito.mock(ItemLifecycleListener.class);
-        for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
-            lifeCycleSource.setItemLifecycleListener(itemLifecycleListener);
-        }
-
         // prepare empty input message
         final FlowRemovedMessageBuilder flowRemovedBld = new FlowRemovedMessageBuilder();
 
@@ -448,15 +442,14 @@ public class DeviceContextImplTest {
 
         deviceContext.setNotificationPublishService(mockedNotificationPublishService);
         deviceContext.processFlowRemovedMessage(flowRemovedBld.build());
-
-        Mockito.verify(itemLifecycleListener).onRemoved(flowToBeRemovedPath);
     }
 
     @Test
     public void testProcessExperimenterMessage() {
         final ConvertorMessageFromOFJava mockedMessageConverter = mock(ConvertorMessageFromOFJava.class);
         final ExtensionConverterProvider mockedExtensionConverterProvider = mock(ExtensionConverterProvider.class);
-        when(mockedExtensionConverterProvider.getMessageConverter(any(MessageTypeKey.class))).thenReturn(mockedMessageConverter);
+        when(mockedExtensionConverterProvider.getMessageConverter(any(MessageTypeKey.class)))
+                .thenReturn(mockedMessageConverter);
 
         final ExperimenterDataOfChoice mockedExperimenterDataOfChoice = mock(ExperimenterDataOfChoice.class);
         final ExperimenterMessage experimenterMessage = new ExperimenterMessageBuilder()
@@ -486,4 +479,4 @@ public class DeviceContextImplTest {
         deviceContext.closeServiceInstance();
     }
 
-}
\ No newline at end of file
+}