bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / test / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepDataChangeListenerTest.java
index 1b787c785bd4c53f2d63d9e16dab0212470fe4f4..f4cd1017e19058a96be9fcfbad3d1352b86b96fb 100644 (file)
@@ -9,10 +9,14 @@
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Matchers;
-import org.opendaylight.ovsdb.lib.notation.UUID;
+import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependencyQueue;
 import org.opendaylight.ovsdb.lib.operations.Operations;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
@@ -22,6 +26,7 @@ import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -29,6 +34,8 @@ import org.powermock.modules.junit4.PowerMockRunner;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.util.Iterator;
 import java.util.List;
 
@@ -73,6 +80,45 @@ public class HwvtepDataChangeListenerTest extends DataChangeListenerTestBase {
             {"FF:FF:FF:FF:FF:FF", "ls1", "192.168.122.10", "192.168.122.30"}
     };
 
+    String[][] mcastMac2 = new String[][]{
+            {"FF:FF:FF:FF:FF:FF", "ls0", "192.168.122.20", "192.168.122.10"},
+            {"FF:FF:FF:FF:FF:FF", "ls1", "192.168.122.10", "192.168.122.20"}
+    };
+
+    String[][] mcastMac3WithZeroLocators = new String[][]{
+            {"FF:FF:FF:FF:FF:FF", "ls0"},
+            {"FF:FF:FF:FF:FF:FF", "ls1"}
+    };
+
+    HwvtepOperationalDataChangeListener opDataChangeListener;
+
+    @Before
+    public void setupListener() throws Exception {
+        setFinalStatic(DependencyQueue.class, "executorService", MoreExecutors.sameThreadExecutor());
+        opDataChangeListener = new HwvtepOperationalDataChangeListener(dataBroker, hwvtepConnectionManager, connectionInstance);
+    }
+
+    @After
+    public void cleanupListener() {
+        try {
+            opDataChangeListener.close();
+        } catch (Exception e) {
+        }
+    }
+
+    void setFinalStatic(Class cls, String fieldName, Object newValue) throws Exception {
+        Field fields[] = FieldUtils.getAllFields(cls);
+        for (Field field : fields) {
+            if (fieldName.equals(field.getName())) {
+                field.setAccessible(true);
+                Field modifiersField = Field.class.getDeclaredField("modifiers");
+                modifiersField.setAccessible(true);
+                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+                field.set(null, newValue);
+                break;
+            }
+        }
+    }
 
     @Test
     public <T extends DataObject> void testLogicalSwitchAdd() throws Exception {
@@ -80,12 +126,19 @@ public class HwvtepDataChangeListenerTest extends DataChangeListenerTestBase {
         verifyThatLogicalSwitchCreated();
     }
 
+    @Test
+    public <T extends DataObject> void testLogicalSwitchDelete() throws Exception {
+        addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        resetOperations();
+        deleteData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        verify(Operations.op,  times(2)).delete(any());
+    }
+
     @Test
     public <T extends DataObject> void testUcastMacAdd() throws Exception {
         addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
         addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
-        connectionInstance.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, ls0Iid, new UUID("ls0"), "ls0");
-        connectionInstance.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, ls1Iid, new UUID("ls1"), "ls1");
         resetOperations();
         addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
         addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
@@ -94,12 +147,36 @@ public class HwvtepDataChangeListenerTest extends DataChangeListenerTestBase {
         //TODO add finer grained validation
     }
 
+    @Test
+    public <T extends DataObject> void testUcastMacAddWithoutConfigTep() throws Exception {
+        addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        resetOperations();
+        addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
+        //4 ucast macs + 2 termination points
+        verify(Operations.op,  times(6)).insert(any(UcastMacsRemote.class));
+        //TODO add finer grained validation
+    }
+
+    @Test
+    public <T extends DataObject> void testUcastMacDelete() throws Exception {
+        addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
+        addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
+        addData(OPERATIONAL, RemoteUcastMacs.class, ucastMacs);
+        addData(OPERATIONAL, TerminationPoint.class, terminationPoints);
+
+        resetOperations();
+        deleteData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
+        verify(Operations.op,  times(4)).delete(any());
+        //TODO add finer grained validation
+    }
+
     @Test
     public <T extends DataObject> void testMcastMacAdd() throws Exception {
         addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
         addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
-        connectionInstance.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, ls0Iid, new UUID("ls0"), "ls0");
-        connectionInstance.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, ls1Iid, new UUID("ls1"), "ls0");
         resetOperations();
         addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
         addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
@@ -108,11 +185,33 @@ public class HwvtepDataChangeListenerTest extends DataChangeListenerTestBase {
     }
 
     @Test
-    public <T extends DataObject> void testAddMacs() throws Exception {
+    public <T extends DataObject> void testMcastMacAddWithoutConfigTep() throws Exception {
         addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
         addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
-        connectionInstance.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, ls0Iid, new UUID("ls0"), "ls0");
-        connectionInstance.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, ls1Iid, new UUID("ls1"), "ls0");
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
+        //2 mcast macs + 2 locator sets + 3 termination points
+        verify(Operations.op,  times(7)).insert(Matchers.<McastMacsRemote>any());
+    }
+
+    @Test
+    public <T extends DataObject> void testMcastMacDelete() throws Exception {
+        addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
+        addData(OPERATIONAL, TerminationPoint.class, terminationPoints);
+        addData(OPERATIONAL, RemoteMcastMacs.class, mcastMacs);
+
+        resetOperations();
+        deleteData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
+        verify(Operations.op,  times(2)).delete(Matchers.any());
+    }
+
+    @Test
+    public <T extends DataObject> void testAddMacs() throws Exception {
+        Node node = addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
         resetOperations();
         addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
         addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
@@ -120,11 +219,83 @@ public class HwvtepDataChangeListenerTest extends DataChangeListenerTestBase {
 
         addData(OPERATIONAL, TerminationPoint.class, terminationPoints);
         addData(OPERATIONAL, RemoteUcastMacs.class, ucastMacs);
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
+        //2 mcast mac + 2 locator sets ( termination point already added )
+        verify(Operations.op,  times(4)).insert(Matchers.<McastMacsRemote>any());
+    }
+
+    @Test
+    public <T extends DataObject> void testUpdateMacs() throws Exception {
+        Node node = addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        resetOperations();
+        addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
+        addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
+        verify(Operations.op,  times(6)).insert(any(UcastMacsRemote.class));
+
+        addData(OPERATIONAL, TerminationPoint.class, terminationPoints);
+        addData(OPERATIONAL, RemoteUcastMacs.class, ucastMacs);
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
+        verify(Operations.op,  times(4)).insert(Matchers.<McastMacsRemote>any());
+        addData(OPERATIONAL, RemoteMcastMacs.class, mcastMacs);
+
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMac2);
+        verify(Operations.op,  times(2)).insert(Matchers.<McastMacsRemote>any());
+        verify(Operations.op,  times(2)).update(Matchers.<McastMacsRemote>any());
+        verify(Operations.op,  times(0)).delete(Matchers.any());
+    }
+
+    @Test
+    public <T extends DataObject> void testUpdateMacsWithZeroLocators() throws Exception {
+        Node node = addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        resetOperations();
+        addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
+        addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
+        verify(Operations.op,  times(6)).insert(any(UcastMacsRemote.class));
+
+        addData(OPERATIONAL, TerminationPoint.class, terminationPoints);
+        addData(OPERATIONAL, RemoteUcastMacs.class, ucastMacs);
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
+        verify(Operations.op,  times(4)).insert(Matchers.<McastMacsRemote>any());
+        addData(OPERATIONAL, RemoteMcastMacs.class, mcastMacs);
+
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMac3WithZeroLocators);
+        verify(Operations.op,  times(2)).delete(Matchers.any());
+    }
+
+    @Test
+    public <T extends DataObject> void testBackToBackMacsUpdate() throws Exception {
+        Node node = addData(CONFIGURATION, LogicalSwitches.class, logicalSwitches);
+        addData(OPERATIONAL, LogicalSwitches.class, logicalSwitches);
+        resetOperations();
+        addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
+        addData(CONFIGURATION, RemoteUcastMacs.class, ucastMacs);
+        verify(Operations.op,  times(6)).insert(any(UcastMacsRemote.class));
 
         resetOperations();
         addData(CONFIGURATION, RemoteMcastMacs.class, mcastMacs);
         //2 mcast mac + 2 locator sets ( termination point already added )
+        verify(Operations.op,  times(0)).insert(Matchers.<McastMacsRemote>any());
+        resetOperations();
+        addData(OPERATIONAL, TerminationPoint.class, terminationPoints);
+        addData(OPERATIONAL, RemoteUcastMacs.class, ucastMacs);
+        connectionInstance.getDeviceInfo().onOpDataAvailable();
+        //2 mcast mac + 2 locator sets ( termination point already added )
         verify(Operations.op,  times(4)).insert(Matchers.<McastMacsRemote>any());
+
+        resetOperations();
+        addData(CONFIGURATION, RemoteMcastMacs.class, mcastMac2);
+        verify(Operations.op,  times(0)).insert(Matchers.<McastMacsRemote>any());
+        addData(OPERATIONAL, RemoteMcastMacs.class, mcastMacs);
+        connectionInstance.getDeviceInfo().onOpDataAvailable();
+        verify(Operations.op,  times(2)).insert(Matchers.<McastMacsRemote>any());
+        verify(Operations.op,  times(2)).update(Matchers.<McastMacsRemote>any());
     }
 
     private void verifyThatLogicalSwitchCreated() {