Merge "SONAR TD - DeviceFlowRegistryImpl, FlowDescriptorFactory, FlowRegistryKeyFactory"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImplTest.java
index e80ce3e55e2ad9ad3798c46013cf3c35c6715a36..43544aff61ddbab1d37510be01b095eb378e0e41 100644 (file)
@@ -8,6 +8,8 @@
 
 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.times;
 import static org.mockito.Mockito.verify;
@@ -15,6 +17,8 @@ import static org.mockito.Mockito.when;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.Futures;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -42,9 +46,13 @@ import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 @RunWith(MockitoJUnitRunner.class)
 public class DeviceFlowRegistryImplTest {
     private static final String NODE_ID = "openflow:1";
+    private static final Pattern INDEX_PATTERN = Pattern.compile("^#UF\\$TABLE\\*1-([0-9]+)$");
+    private static final Short DUMMY_TABLE_ID = 1;
+
     private DeviceFlowRegistryImpl deviceFlowRegistry;
     private FlowRegistryKey key;
     private FlowDescriptor descriptor;
+    private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
     @Mock
     private DataBroker dataBroker;
     @Mock
@@ -52,9 +60,10 @@ public class DeviceFlowRegistryImplTest {
 
     @Before
     public void setUp() throws Exception {
+        nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
         when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
-        deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker);
+        deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier);
         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
         key = FlowRegistryKeyFactory.create(flowStats);
         descriptor = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:1"));
@@ -66,10 +75,9 @@ public class DeviceFlowRegistryImplTest {
 
     @Test
     public void testFill() throws Exception {
-        final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
         final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
 
-        deviceFlowRegistry.fill(nodeInstanceIdentifier).get();
+        deviceFlowRegistry.fill().get();
 
         verify(dataBroker, times(2)).newReadOnlyTransaction();
         verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
@@ -145,4 +153,26 @@ public class DeviceFlowRegistryImplTest {
         deviceFlowRegistry.removeMarked();
         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
     }
+
+    @Test
+    public void createAlienFlowIdTest() throws Exception {
+        final String alienFlowId1 = DeviceFlowRegistryImpl.createAlienFlowId(DUMMY_TABLE_ID).getValue();
+        final Integer index1 = parseIndex(alienFlowId1);
+        final String alienFlowId2 = DeviceFlowRegistryImpl.createAlienFlowId(DUMMY_TABLE_ID).getValue();
+        final Integer index2 = parseIndex(alienFlowId2);
+
+        assertNotNull("index1 parsing failed: " + alienFlowId1, index1);
+        assertNotNull("index2 parsing failed: " + alienFlowId2, index2);
+        assertTrue(index1 < index2);
+    }
+
+    private static Integer parseIndex(String alienFlowIdValue) {
+        final Matcher mach = INDEX_PATTERN.matcher(alienFlowIdValue);
+
+        if (mach.find()) {
+            return Integer.valueOf(mach.group(1));
+        }
+
+        return null;
+    }
 }
\ No newline at end of file