Merge "Technical debt MeterUtil, GroupUtil, FlowUtil"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImplTest.java
index 43544aff61ddbab1d37510be01b095eb378e0e41..8a89a1cc8465aa6005365b00b54ec5497cf7d239 100644 (file)
@@ -11,18 +11,23 @@ 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;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.Futures;
+import java.math.BigInteger;
+import java.util.Collections;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 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;
@@ -31,8 +36,14 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
@@ -62,7 +73,6 @@ 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);
-        when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
         deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier);
         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
         key = FlowRegistryKeyFactory.create(flowStats);
@@ -77,11 +87,81 @@ public class DeviceFlowRegistryImplTest {
     public void testFill() throws Exception {
         final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
 
-        deviceFlowRegistry.fill().get();
+        final Flow flow = new FlowBuilder()
+                .setTableId((short)1)
+                .setPriority(10)
+                .setCookie(new FlowCookie(BigInteger.TEN))
+                .setId(new FlowId("HELLO"))
+                .build();
+
+        final Table table = new TableBuilder()
+                .setFlow(Collections.singletonList(flow))
+                .build();
+
+        final FlowCapableNode flowCapableNode = new FlowCapableNodeBuilder()
+                .setTable(Collections.singletonList(table))
+                .build();
+
+        final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = testFill(path, flowCapableNode);
+        final FlowRegistryKey key = FlowRegistryKeyFactory.create(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();
+    }
+
+    @Test
+    public void testFailedFill() throws Exception {
+        final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
 
-        verify(dataBroker, times(2)).newReadOnlyTransaction();
-        verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
-        verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
+        testFill(path, null);
+
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(null)
+                .build());
+
+        testFill(path, new FlowCapableNodeBuilder()
+                .setTable(Collections.singletonList(null))
+                .build());
+
+        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);
+
+        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)));
+        deviceFlowRegistry.fill().get();
+        return deviceFlowRegistry.getAllFlowDescriptors();
     }
 
     @Test