Merge "Fix statistics race condition on big flows"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImplTest.java
index 8a89a1cc8465aa6005365b00b54ec5497cf7d239..d4f5906426ca986ed02b9246a3a46e1e26d2395d 100644 (file)
@@ -33,6 +33,7 @@ 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;
@@ -73,13 +74,13 @@ 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());
-        deviceFlowRegistry.store(key, descriptor);
+        deviceFlowRegistry.storeDescriptor(key, descriptor);
         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
     }
 
@@ -103,7 +104,7 @@ public class DeviceFlowRegistryImplTest {
                 .build();
 
         final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = testFill(path, flowCapableNode);
-        final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
+        final FlowRegistryKey key = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flow);
 
         InOrder order = inOrder(dataBroker, readOnlyTransaction);
         order.verify(dataBroker).newReadOnlyTransaction();
@@ -112,8 +113,7 @@ public class DeviceFlowRegistryImplTest {
         order.verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
         assertTrue(allFlowDescriptors.containsKey(key));
 
-        deviceFlowRegistry.markToBeremoved(key);
-        deviceFlowRegistry.removeMarked();
+        deviceFlowRegistry.addMark(key);
     }
 
     @Test
@@ -166,23 +166,23 @@ public class DeviceFlowRegistryImplTest {
 
     @Test
     public void testRetrieveIdForFlow() throws Exception {
-        Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
+        Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveDescriptor(key));
     }
 
     @Test
     public void testStore() throws Exception {
         //store the same key with different value
         final FlowDescriptor descriptor2 = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:2"));
-        deviceFlowRegistry.store(key, descriptor2);
+        deviceFlowRegistry.storeDescriptor(key, descriptor2);
         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
-        Assert.assertEquals("ut:2", deviceFlowRegistry.retrieveIdForFlow(key).getFlowId().getValue());
+        Assert.assertEquals("ut:2", deviceFlowRegistry.retrieveDescriptor(key).getFlowId().getValue());
 
         // store new key with old value
         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
-        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
-        deviceFlowRegistry.store(key2, descriptor);
+        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flowStats);
+        deviceFlowRegistry.storeDescriptor(key2, descriptor);
         Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
-        Assert.assertEquals("ut:1", deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue());
+        Assert.assertEquals("ut:1", deviceFlowRegistry.retrieveDescriptor(key2).getFlowId().getValue());
     }
 
     @Test
@@ -190,48 +190,34 @@ public class DeviceFlowRegistryImplTest {
         FlowId newFlowId;
 
         //store existing key
-        newFlowId = deviceFlowRegistry.storeIfNecessary(key);
+        deviceFlowRegistry.store(key);
+        newFlowId = deviceFlowRegistry.retrieveDescriptor(key).getFlowId();
 
         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
-        Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
+        Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveDescriptor(key));
         Assert.assertEquals(descriptor.getFlowId(), newFlowId);
 
         //store new key
         final String alienPrefix = "#UF$TABLE*2-";
-        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
-        newFlowId = deviceFlowRegistry.storeIfNecessary(key2);
+        final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
+        deviceFlowRegistry.store(key2);
+        newFlowId = deviceFlowRegistry.retrieveDescriptor(key2).getFlowId();
 
         Assert.assertTrue(newFlowId.getValue().startsWith(alienPrefix));
-        Assert.assertTrue(deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue().startsWith(alienPrefix));
+        Assert.assertTrue(deviceFlowRegistry.retrieveDescriptor(key2).getFlowId().getValue().startsWith(alienPrefix));
         Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
     }
 
     @Test
-    public void testRemoveMarked() throws Exception {
-        deviceFlowRegistry.markToBeremoved(key);
-        deviceFlowRegistry.removeMarked();
-        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();
+    public void testRemoveDescriptor() throws Exception {
+        deviceFlowRegistry.addMark(key);
         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
@@ -255,4 +241,4 @@ public class DeviceFlowRegistryImplTest {
 
         return null;
     }
-}
\ No newline at end of file
+}