Deleted device manager's inside global cache and changed to use the topo cache.
The reason is it caused some unmatched condition with the topo cache when link and port status was changed.
The DeviceEvent timing is changed.
The old one sends it when it is appeared, or attachment point or IP is changed, but now it sends every packet-in to update last-time-seen.

Note:
Looks like there is some vulnerability for changing the mastership of switches.
During mastership changes, the intent reroute is occured, and for a short period of time links or ports haven't found,
so the intent cann't calcurate its path.

Change-Id: I311fa5ad368ca833915999ad112274f1a7979387
diff --git a/src/test/java/net/onrc/onos/core/devicemanager/OnosDeviceManagerTest.java b/src/test/java/net/onrc/onos/core/devicemanager/OnosDeviceManagerTest.java
index 0b9defb..22900cc 100644
--- a/src/test/java/net/onrc/onos/core/devicemanager/OnosDeviceManagerTest.java
+++ b/src/test/java/net/onrc/onos/core/devicemanager/OnosDeviceManagerTest.java
@@ -7,7 +7,6 @@
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
 import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
 
 import java.util.Date;
 
@@ -16,12 +15,12 @@
 import net.floodlightcontroller.core.IOFSwitch;
 import net.floodlightcontroller.core.IUpdate;
 import net.floodlightcontroller.core.module.FloodlightModuleContext;
+import net.floodlightcontroller.core.module.FloodlightModuleException;
 import net.floodlightcontroller.test.FloodlightTestCase;
 import net.floodlightcontroller.util.MACAddress;
 import net.onrc.onos.core.datagrid.IDatagridService;
 import net.onrc.onos.core.datagrid.IEventChannel;
 import net.onrc.onos.core.datagrid.IEventChannelListener;
-import net.onrc.onos.core.intent.MockTopology;
 import net.onrc.onos.core.packet.ARP;
 import net.onrc.onos.core.packet.DHCP;
 import net.onrc.onos.core.packet.Data;
@@ -32,6 +31,7 @@
 import net.onrc.onos.core.registry.IControllerRegistryService;
 import net.onrc.onos.core.topology.ITopologyListener;
 import net.onrc.onos.core.topology.ITopologyService;
+import net.onrc.onos.core.topology.MockTopology;
 
 import org.easymock.EasyMock;
 import org.junit.After;
@@ -51,9 +51,9 @@
     private IPacket pkt0, pkt1, pkt2, pkt3, pkt4;
     private IOFSwitch sw1;
     private long sw1Dpid;
-    private short sw1DevPort;
+    private long sw1DevPort, sw1DevPort2;
     private OnosDeviceManager odm;
-    private OFPacketIn pktIn;
+    private OFPacketIn pktIn, pktIn2;
     private FloodlightModuleContext modContext;
     private ITopologyService networkGraphService;
     private IEventChannel<Long, OnosDevice> eventChannel;
@@ -69,7 +69,7 @@
         IDatagridService datagridService;
         IControllerRegistryService controllerRegistryService;
 
-        topology.createSampleTopology1();
+        topology.createSampleTopology2();
         modContext = new FloodlightModuleContext();
 
         floodLightProvider = createMock(IFloodlightProviderService.class);
@@ -104,6 +104,7 @@
         replay(sw1);
 
         sw1DevPort = 100;
+        sw1DevPort2 = 12L;
 
         odm = new OnosDeviceManager();
         /*
@@ -201,13 +202,11 @@
                 .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2")));
 
 
-        this.pktIn = new OFPacketIn()
-        .setInPort(sw1DevPort);
+        this.pktIn = new OFPacketIn().setInPort((short) sw1DevPort);
+
+        this.pktIn2 = new OFPacketIn().setInPort((short) sw1DevPort2);
 
         lastSeenTimestamp = new Date(1);
-
-        odm.init(modContext);
-        odm.startUp(modContext);
     }
 
     @Override
@@ -264,30 +263,46 @@
 
     /**
      * This test will invoke addOnosDevice to add a new device through Packet pkt1.
+     * @throws FloodlightModuleException
      */
     @Test
-    public void testProcessPacketInAddNewDevice() {
-        Ethernet eth = (Ethernet) pkt1;
-        Long longmac = eth.getSourceMAC().toLong();
-        OnosDevice srcDevice = odm.getSourceDeviceFromPacket(eth, sw1Dpid, sw1DevPort);
-
-        floodLightProvider.addOFMessageListener(EasyMock.eq(OFType.PACKET_IN), EasyMock.isA(OnosDeviceManager.class));
-        EasyMock.expectLastCall();
-        eventChannel.addEntry(longmac, srcDevice);
+    public void testProcessPacketInAddNewDevice() throws FloodlightModuleException {
+        floodLightProvider.addOFMessageListener(EasyMock.eq(OFType.PACKET_IN),
+                EasyMock.isA(OnosDeviceManager.class));
         EasyMock.expectLastCall();
         floodLightProvider.publishUpdate(EasyMock.isA(IUpdate.class));
         EasyMock.expectLastCall();
-        replay(floodLightProvider, eventChannel);
+        replay(floodLightProvider);
 
+        odm.init(modContext);
+        odm.startUp(modContext);
         Command cmd = odm.processPacketIn(sw1, pktIn, (Ethernet) pkt1);
         assertEquals(Command.CONTINUE, cmd);
 
-        verify(eventChannel);
+        EasyMock.verify(floodLightProvider);
     }
 
     /**
-     * This test will test return Command.STOP path in processPacketIn method
-     * by injecting a broadcast packet.
+     * Test ProcessPacket function.
+     * @throws FloodlightModuleException
+     */
+    @Test
+    public void testProcessPacketInHasLink() throws FloodlightModuleException {
+        floodLightProvider.addOFMessageListener(EasyMock.eq(OFType.PACKET_IN),
+                EasyMock.isA(OnosDeviceManager.class));
+        EasyMock.expectLastCall();
+        replay(floodLightProvider);
+
+        odm.init(modContext);
+        odm.startUp(modContext);
+        Command cmd = odm.processPacketIn(sw1, pktIn2, (Ethernet) pkt1);
+        assertEquals(Command.CONTINUE, cmd);
+
+        EasyMock.verify(floodLightProvider);
+    }
+
+    /**
+     * Test return Command.STOP path in processPacketIn method by injecting a broadcast packet.
      */
     @Test
     public void testProcessPacketInStop() {
@@ -296,99 +311,69 @@
     }
 
     /**
-     * This tests same packet received case.
-     */
-    @Test
-    public void testProcessPacketInSamePacket() {
-        Ethernet eth = (Ethernet) pkt2;
-        OnosDevice srcDevice = odm.getSourceDeviceFromPacket(eth, sw1Dpid, sw1DevPort);
-        odm.entryAdded(srcDevice);
-        srcDevice.setLastSeenTimestamp(lastSeenTimestamp);
-
-        Command cmd = odm.processPacketIn(sw1, pktIn, (Ethernet) pkt2);
-        assertEquals(Command.CONTINUE, cmd);
-        assertTrue(lastSeenTimestamp.before(srcDevice.getLastSeenTimestamp()));
-    }
-
-    /**
-     * This tests the packet with the same MAC but the second one without IP address.
-     */
-    @Test
-    public void testProcessPacketInNoIpAddress() {
-        Ethernet eth = (Ethernet) pkt3;
-        OnosDevice srcDevice = odm.getSourceDeviceFromPacket(eth, sw1Dpid, sw1DevPort);
-        odm.entryAdded(srcDevice);
-        srcDevice.setLastSeenTimestamp(lastSeenTimestamp);
-
-        Command cmd = odm.processPacketIn(sw1, pktIn, (Ethernet) pkt2);
-        assertEquals(Command.CONTINUE, cmd);
-        assertTrue(lastSeenTimestamp.before(srcDevice.getLastSeenTimestamp()));
-    }
-
-    /**
      * Test add a device from the information from packet.
+     * @throws FloodlightModuleException
      */
     @Test
-    public void testAddOnosDevice() {
+    public void testAddOnosDevice() throws FloodlightModuleException {
         Ethernet eth = (Ethernet) pkt1;
         Long longmac = eth.getSourceMAC().toLong();
         OnosDevice srcDevice = odm.getSourceDeviceFromPacket(eth, sw1Dpid, sw1DevPort);
 
         floodLightProvider.addOFMessageListener(EasyMock.eq(OFType.PACKET_IN), EasyMock.isA(OnosDeviceManager.class));
         EasyMock.expectLastCall();
-        eventChannel.addEntry(longmac, srcDevice);
-        EasyMock.expectLastCall();
         floodLightProvider.publishUpdate(EasyMock.isA(IUpdate.class));
         EasyMock.expectLastCall();
-        replay(floodLightProvider, eventChannel);
+        replay(floodLightProvider);
 
+        odm.init(modContext);
+        odm.startUp(modContext);
         odm.addOnosDevice(longmac, srcDevice);
 
-        verify(eventChannel);
+        EasyMock.verify(floodLightProvider);
     }
 
     /**
      * Test delete a device.
+     * @throws FloodlightModuleException
      */
     @Test
-    public void testDeleteOnosDevice() {
+    public void testDeleteOnosDevice() throws FloodlightModuleException {
         Ethernet eth = (Ethernet) pkt1;
-        Long longmac = eth.getSourceMAC().toLong();
         OnosDevice srcDevice = odm.getSourceDeviceFromPacket(eth, sw1Dpid, sw1DevPort);
 
         floodLightProvider.addOFMessageListener(EasyMock.eq(OFType.PACKET_IN), EasyMock.isA(OnosDeviceManager.class));
         EasyMock.expectLastCall();
-        eventChannel.removeEntry(longmac);
-        EasyMock.expectLastCall();
         floodLightProvider.publishUpdate(EasyMock.isA(IUpdate.class));
         EasyMock.expectLastCall();
-        replay(floodLightProvider, eventChannel);
+        replay(floodLightProvider);
 
+        odm.init(modContext);
+        odm.startUp(modContext);
         odm.deleteOnosDevice(srcDevice);
 
-        verify(eventChannel);
+        EasyMock.verify(floodLightProvider);
     }
 
     /**
      * Test delete a device by using its source mac address.
+     * @throws FloodlightModuleException
      */
     @Test
-    public void testDeleteOnosDeviceByMac() {
+    public void testDeleteOnosDeviceByMac() throws FloodlightModuleException {
         Ethernet eth = (Ethernet) pkt1;
         MACAddress mac = eth.getSourceMAC();
-        Long longmac = mac.toLong();
-        OnosDevice srcDevice = odm.getSourceDeviceFromPacket(eth, sw1Dpid, sw1DevPort);
 
         floodLightProvider.addOFMessageListener(EasyMock.eq(OFType.PACKET_IN), EasyMock.isA(OnosDeviceManager.class));
         EasyMock.expectLastCall();
-        eventChannel.removeEntry(longmac);
-        EasyMock.expectLastCall();
         floodLightProvider.publishUpdate(EasyMock.isA(IUpdate.class));
         EasyMock.expectLastCall();
-        replay(floodLightProvider, eventChannel);
+        replay(floodLightProvider);
 
-        odm.entryAdded(srcDevice);
+        odm.init(modContext);
+        odm.startUp(modContext);
         odm.deleteOnosDeviceByMac(mac);
-        verify(eventChannel);
+
+        EasyMock.verify(floodLightProvider);
     }
 }