Fix comparison between port numbers in match
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImplTest.java
index 79554b8f8f868f8d3a4812b2b14551a049fe9205..250d13615c08833ec81a3b51801357d4ef807639 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.openflowplugin.impl.registry.flow;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -26,11 +27,13 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.InOrder;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
@@ -71,9 +74,9 @@ public class DeviceFlowRegistryImplTest {
     public void setUp() throws Exception {
         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
-        deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier);
+        deviceFlowRegistry = new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodeInstanceIdentifier);
         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
-        key = FlowRegistryKeyFactory.create(flowStats);
+        key = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flowStats);
         descriptor = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:1"));
 
         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
@@ -100,44 +103,65 @@ public class DeviceFlowRegistryImplTest {
                 .setTable(Collections.singletonList(table))
                 .build();
 
-        when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.of(flowCapableNode)));
-
-        deviceFlowRegistry.fill().get();
-        verify(dataBroker, times(2)).newReadOnlyTransaction();
-        verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
-        verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
-
-        final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = deviceFlowRegistry.getAllFlowDescriptors();
-        final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
+        final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = testFill(path, flowCapableNode);
+        final FlowRegistryKey key = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flow);
 
+        InOrder order = inOrder(dataBroker, readOnlyTransaction);
+        order.verify(dataBroker).newReadOnlyTransaction();
+        order.verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
+        order.verify(dataBroker).newReadOnlyTransaction();
+        order.verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
         assertTrue(allFlowDescriptors.containsKey(key));
 
-        deviceFlowRegistry.markToBeremoved(key);
-        deviceFlowRegistry.removeMarked();
+        deviceFlowRegistry.removeDescriptor(key);
     }
 
     @Test
     public void testFailedFill() throws Exception {
         final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
 
-        final Table table = new TableBuilder()
-                .setFlow(null)
-                .build();
+        testFill(path, null);
 
-        final FlowCapableNode flowCapableNode = new FlowCapableNodeBuilder()
-                .setTable(Collections.singletonList(table))
-                .build();
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(null)
+                .build());
 
-        when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.of(flowCapableNode)));
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(Collections.singletonList(null))
+                .build());
 
-        deviceFlowRegistry.fill().get();
-        verify(dataBroker, times(2)).newReadOnlyTransaction();
-        verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
-        verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(Collections.singletonList(new TableBuilder()
+                        .setFlow(null)
+                        .build()))
+                .build());
+
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(Collections.singletonList(new TableBuilder()
+                        .setFlow(Collections.singletonList(null))
+                        .build()))
+                .build());
+
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(Collections.singletonList(new TableBuilder()
+                        .setFlow(Collections.singletonList(new FlowBuilder()
+                                .setId(null)
+                                .build()))
+                        .build()))
+                .build());
+
+        verify(dataBroker, times(12)).newReadOnlyTransaction();
+        verify(readOnlyTransaction, times(6)).read(LogicalDatastoreType.CONFIGURATION, path);
+        verify(readOnlyTransaction, times(6)).read(LogicalDatastoreType.OPERATIONAL, path);
 
-        final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = deviceFlowRegistry.getAllFlowDescriptors();
+        Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
+    }
 
-        Assert.assertEquals(1, allFlowDescriptors.size());
+    private Map<FlowRegistryKey, FlowDescriptor> testFill(final InstanceIdentifier<FlowCapableNode> path,
+                                                          final FlowCapableNode flowCapableNode) throws Exception {
+        when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.fromNullable(flowCapableNode)));
+        deviceFlowRegistry.fill().get();
+        return deviceFlowRegistry.getAllFlowDescriptors();
     }
 
     @Test
@@ -155,7 +179,7 @@ public class DeviceFlowRegistryImplTest {
 
         // store new key with old value
         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
-        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
+        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flowStats);
         deviceFlowRegistry.store(key2, descriptor);
         Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
         Assert.assertEquals("ut:1", deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue());
@@ -174,7 +198,7 @@ public class DeviceFlowRegistryImplTest {
 
         //store new key
         final String alienPrefix = "#UF$TABLE*2-";
-        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
+        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
         newFlowId = deviceFlowRegistry.storeIfNecessary(key2);
 
         Assert.assertTrue(newFlowId.getValue().startsWith(alienPrefix));
@@ -183,31 +207,15 @@ public class DeviceFlowRegistryImplTest {
     }
 
     @Test
-    public void testRemoveMarked() throws Exception {
-        deviceFlowRegistry.markToBeremoved(key);
-        deviceFlowRegistry.removeMarked();
+    public void testRemoveDescriptor() throws Exception {
+        deviceFlowRegistry.removeDescriptor(key);
         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
     }
 
-    @Test
-    public void testRemoveMarkedNegative() throws Exception {
-        final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
-        FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
-        deviceFlowRegistry.markToBeremoved(key2);
-        deviceFlowRegistry.removeMarked();
-        Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
-    }
-
     @Test
     public void testClose() throws Exception {
-        deviceFlowRegistry.markToBeremoved(key);
         deviceFlowRegistry.close();
         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
-
-        deviceFlowRegistry.store(key, descriptor);
-        Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
-        deviceFlowRegistry.removeMarked();
-        Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
     }
 
     @Test
@@ -231,4 +239,4 @@ public class DeviceFlowRegistryImplTest {
 
         return null;
     }
-}
\ No newline at end of file
+}