Merge "OFPGC_ADD_OR_MOD support in openflowplugin"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImplTest.java
index 250d13615c08833ec81a3b51801357d4ef807639..b9be54d89420ed90961601e35de37e8f015d5698 100644 (file)
@@ -21,6 +21,7 @@ import com.google.common.util.concurrent.Futures;
 import java.math.BigInteger;
 import java.util.Collections;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.junit.Assert;
@@ -72,15 +73,17 @@ public class DeviceFlowRegistryImplTest {
 
     @Before
     public void setUp() throws Exception {
-        nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
+        nodeInstanceIdentifier =
+                InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
-        deviceFlowRegistry = new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodeInstanceIdentifier);
+        deviceFlowRegistry =
+                new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodeInstanceIdentifier);
         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
         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,8 +106,8 @@ public class DeviceFlowRegistryImplTest {
                 .setTable(Collections.singletonList(table))
                 .build();
 
-        final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = testFill(path, flowCapableNode);
-        final FlowRegistryKey key = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flow);
+        final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = fillRegistry(path, flowCapableNode);
+        key = FlowRegistryKeyFactory.create(OFConstants.OFP_VERSION_1_3, flow);
 
         InOrder order = inOrder(dataBroker, readOnlyTransaction);
         order.verify(dataBroker).newReadOnlyTransaction();
@@ -113,36 +116,36 @@ public class DeviceFlowRegistryImplTest {
         order.verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
         assertTrue(allFlowDescriptors.containsKey(key));
 
-        deviceFlowRegistry.removeDescriptor(key);
+        deviceFlowRegistry.addMark(key);
     }
 
     @Test
     public void testFailedFill() throws Exception {
         final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
 
-        testFill(path, null);
+        fillRegistry(path, null);
 
-        testFill(path, new FlowCapableNodeBuilder()
+        fillRegistry(path, new FlowCapableNodeBuilder()
                 .setTable(null)
                 .build());
 
-        testFill(path, new FlowCapableNodeBuilder()
+        fillRegistry(path, new FlowCapableNodeBuilder()
                 .setTable(Collections.singletonList(null))
                 .build());
 
-        testFill(path, new FlowCapableNodeBuilder()
+        fillRegistry(path, new FlowCapableNodeBuilder()
                 .setTable(Collections.singletonList(new TableBuilder()
                         .setFlow(null)
                         .build()))
                 .build());
 
-        testFill(path, new FlowCapableNodeBuilder()
+        fillRegistry(path, new FlowCapableNodeBuilder()
                 .setTable(Collections.singletonList(new TableBuilder()
                         .setFlow(Collections.singletonList(null))
                         .build()))
                 .build());
 
-        testFill(path, new FlowCapableNodeBuilder()
+        fillRegistry(path, new FlowCapableNodeBuilder()
                 .setTable(Collections.singletonList(new TableBuilder()
                         .setFlow(Collections.singletonList(new FlowBuilder()
                                 .setId(null)
@@ -157,32 +160,33 @@ public class DeviceFlowRegistryImplTest {
         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().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)));
+    private Map<FlowRegistryKey, FlowDescriptor> fillRegistry(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
     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(OFConstants.OFP_VERSION_1_3, flowStats);
-        deviceFlowRegistry.store(key2, descriptor);
+        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,25 +194,28 @@ 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(OFConstants.OFP_VERSION_1_3, 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 testRemoveDescriptor() throws Exception {
-        deviceFlowRegistry.removeDescriptor(key);
+        deviceFlowRegistry.addMark(key);
         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
     }
 
@@ -230,6 +237,13 @@ public class DeviceFlowRegistryImplTest {
         assertTrue(index1 < index2);
     }
 
+    @Test
+    public void testForEach() throws Exception {
+        final AtomicInteger counter = new AtomicInteger(0);
+        deviceFlowRegistry.forEach(k -> counter.incrementAndGet());
+        Assert.assertEquals(1, counter.get());
+    }
+
     private static Integer parseIndex(String alienFlowIdValue) {
         final Matcher mach = INDEX_PATTERN.matcher(alienFlowIdValue);