Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / hosttracker_new / implementation / src / test / java / org / opendaylight / controller / hosttracker / internal / DeviceManagerImplTest.java
1 package org.opendaylight.controller.hosttracker.internal;
2
3 ///*
4 // * Copyright (c) 2011,2013 Big Switch Networks, Inc.
5 // *
6 // * Licensed under the Eclipse Public License, Version 1.0 (the
7 // * "License"); you may not use this file except in compliance with the
8 // * License. You may obtain a copy of the License at
9 // *
10 // *      http://www.eclipse.org/legal/epl-v10.html
11 // *
12 // * Unless required by applicable law or agreed to in writing, software
13 // * distributed under the License is distributed on an "AS IS" BASIS,
14 // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 // * implied. See the License for the specific language governing
16 // * permissions and limitations under the License.
17 // *
18 // * This file incorporates work covered by the following copyright and
19 // * permission notice:
20 // *
21 // *    Originally created by David Erickson, Stanford University
22 // *
23 // *    Licensed under the Apache License, Version 2.0 (the "License");
24 // *    you may not use this file except in compliance with the
25 // *    License. You may obtain a copy of the License at
26 // *
27 // *         http://www.apache.org/licenses/LICENSE-2.0
28 // *
29 // *    Unless required by applicable law or agreed to in writing,
30 // *    software distributed under the License is distributed on an "AS
31 // *    IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
32 // *    express or implied. See the License for the specific language
33 // *    governing permissions and limitations under the License.
34 // */
35 //
36 //package org.opendaylight.controller.hosttracker.internal;
37 //
38 //import static org.easymock.EasyMock.anyLong;
39 //import static org.easymock.EasyMock.anyObject;
40 //import static org.easymock.EasyMock.anyShort;
41 //import static org.easymock.EasyMock.createMock;
42 //import static org.easymock.EasyMock.createNiceMock;
43 //import static org.easymock.EasyMock.eq;
44 //import static org.easymock.EasyMock.expect;
45 //import static org.easymock.EasyMock.expectLastCall;
46 //import static org.easymock.EasyMock.isA;
47 //import static org.easymock.EasyMock.or;
48 //import static org.easymock.EasyMock.replay;
49 //import static org.easymock.EasyMock.reset;
50 //import static org.easymock.EasyMock.verify;
51 //import static org.junit.Assert.*;
52 //
53 //import java.util.ArrayList;
54 //import java.util.Arrays;
55 //import java.util.Calendar;
56 //import java.util.Collection;
57 //import java.util.Collections;
58 //import java.util.Date;
59 //import java.util.EnumSet;
60 //import java.util.HashMap;
61 //import java.util.HashSet;
62 //import java.util.Iterator;
63 //import java.util.List;
64 //import java.util.Map;
65 //import java.util.Map.Entry;
66 //import java.util.Set;
67 //import java.util.concurrent.ConcurrentHashMap;
68 //
69 //import org.junit.Before;
70 //import org.junit.Test;
71 //
72 //import org.slf4j.Logger;
73 //import org.slf4j.LoggerFactory;
74 //
75 //public class DeviceManagerImplTest {
76 //
77 //    protected static Logger logger =
78 //            LoggerFactory.getLogger(DeviceManagerImplTest.class);
79 //
80 //    protected OFPacketIn packetIn_1, packetIn_2, packetIn_3;
81 //    protected IPacket testARPReplyPacket_1, testARPReplyPacket_2,
82 //    testARPReplyPacket_3;
83 //    protected IPacket testARPReqPacket_1, testARPReqPacket_2;
84 //    protected byte[] testARPReplyPacket_1_Srld, testARPReplyPacket_2_Srld;
85 //    private MockSyncService syncService;
86 //    private IStoreClient<String, DeviceSyncRepresentation> storeClient;
87 //
88 //    DeviceManagerImpl deviceManager;
89 //    MemoryStorageSource storageSource;
90 //    FlowReconcileManager flowReconcileMgr;
91 //
92 //    private IOFSwitch makeSwitchMock(long id) {
93 //        IOFSwitch mockSwitch = createMock(IOFSwitch.class);
94 //        ImmutablePort port = ImmutablePort.create("p1", (short)1);
95 //        expect(mockSwitch.getId()).andReturn(id).anyTimes();
96 //        expect(mockSwitch.getStringId())
97 //                .andReturn(HexString.toHexString(id, 6)).anyTimes();
98 //        expect(mockSwitch.getPort(anyShort()))
99 //                .andReturn(port).anyTimes();
100 //        return mockSwitch;
101 //    }
102 //
103 //    /*
104 //     * return an EasyMock ITopologyService that's setup so that it will
105 //     * answer all questions a device or device manager will ask
106 //     * (isAttachmentPointPort, etc.) in a way so that every port is a
107 //     * non-BD, attachment point port.
108 //     * The returned mock is still in record mode
109 //     */
110 //    private ITopologyService makeMockTopologyAllPortsAp() {
111 //        ITopologyService mockTopology = createMock(ITopologyService.class);
112 //        mockTopology.isAttachmentPointPort(anyLong(), anyShort());
113 //        expectLastCall().andReturn(true).anyTimes();
114 //        mockTopology.getL2DomainId(anyLong());
115 //        expectLastCall().andReturn(1L).anyTimes();
116 //        mockTopology.isBroadcastDomainPort(anyLong(), anyShort());
117 //        expectLastCall().andReturn(false).anyTimes();
118 //        mockTopology.isConsistent(anyLong(), anyShort(), anyLong(), anyShort());
119 //        expectLastCall().andReturn(false).anyTimes();
120 //        mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
121 //                                             anyLong(), anyShort());
122 //        expectLastCall().andReturn(false).anyTimes();
123 //        return mockTopology;
124 //    }
125 //
126 //    @Override
127 //    @Before
128 //    public void setUp() throws Exception {
129 //        doSetUp(Role.MASTER);
130 //    }
131 //
132 //    public void doSetUp(Role initialRole) throws Exception {
133 //        super.setUp();
134 //
135 //        this.syncService = new MockSyncService();
136 //
137 //        FloodlightModuleContext fmc = new FloodlightModuleContext();
138 //        RestApiServer restApi = new RestApiServer();
139 //        MockThreadPoolService tp = new MockThreadPoolService();
140 //        ITopologyService topology = createMock(ITopologyService.class);
141 //        fmc.addService(IThreadPoolService.class, tp);
142 //        mockFloodlightProvider = getMockFloodlightProvider();
143 //        mockFloodlightProvider.setRole(initialRole, "");
144 //
145 //        deviceManager = new DeviceManagerImpl();
146 //        flowReconcileMgr = new FlowReconcileManager();
147 //        DefaultEntityClassifier entityClassifier = new DefaultEntityClassifier();
148 //        fmc.addService(IDeviceService.class, deviceManager);
149 //        storageSource = new MemoryStorageSource();
150 //        fmc.addService(IStorageSourceService.class, storageSource);
151 //        fmc.addService(IFloodlightProviderService.class, mockFloodlightProvider);
152 //        fmc.addService(IRestApiService.class, restApi);
153 //        fmc.addService(IFlowReconcileService.class, flowReconcileMgr);
154 //        fmc.addService(IEntityClassifierService.class, entityClassifier);
155 //        fmc.addService(ITopologyService.class, topology);
156 //        fmc.addService(ISyncService.class, syncService);
157 //        tp.init(fmc);
158 //        restApi.init(fmc);
159 //        storageSource.init(fmc);
160 //        deviceManager.init(fmc);
161 //        flowReconcileMgr.init(fmc);
162 //        entityClassifier.init(fmc);
163 //        syncService.init(fmc);
164 //        storageSource.startUp(fmc);
165 //        deviceManager.startUp(fmc);
166 //        flowReconcileMgr.startUp(fmc);
167 //        tp.startUp(fmc);
168 //        entityClassifier.startUp(fmc);
169 //        syncService.startUp(fmc);
170 //
171 //        this.storeClient =
172 //                this.syncService.getStoreClient(DeviceManagerImpl.DEVICE_SYNC_STORE_NAME,
173 //                            String.class, DeviceSyncRepresentation.class);
174 //
175 //        reset(topology);
176 //        topology.addListener(deviceManager);
177 //        expectLastCall().anyTimes();
178 //        replay(topology);
179 //
180 //        IOFSwitch mockSwitch1 = makeSwitchMock(1L);
181 //        IOFSwitch mockSwitch10 = makeSwitchMock(10L);
182 //        IOFSwitch mockSwitch5 = makeSwitchMock(5L);
183 //        IOFSwitch mockSwitch50 = makeSwitchMock(50L);
184 //        Map<Long, IOFSwitch> switches = new HashMap<Long,IOFSwitch>();
185 //        switches.put(1L, mockSwitch1);
186 //        switches.put(10L, mockSwitch10);
187 //        switches.put(5L, mockSwitch5);
188 //        switches.put(50L, mockSwitch50);
189 //        mockFloodlightProvider.setSwitches(switches);
190 //
191 //        replay(mockSwitch1, mockSwitch5, mockSwitch10, mockSwitch50);
192 //
193 //        // Build our test packet
194 //        this.testARPReplyPacket_1 = new Ethernet()
195 //        .setSourceMACAddress("00:44:33:22:11:01")
196 //        .setDestinationMACAddress("00:11:22:33:44:55")
197 //        .setEtherType(Ethernet.TYPE_ARP)
198 //        .setVlanID((short)5)
199 //        .setPayload(
200 //                    new ARP()
201 //                    .setHardwareType(ARP.HW_TYPE_ETHERNET)
202 //                    .setProtocolType(ARP.PROTO_TYPE_IP)
203 //                    .setHardwareAddressLength((byte) 6)
204 //                    .setProtocolAddressLength((byte) 4)
205 //                    .setOpCode(ARP.OP_REPLY)
206 //                    .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01"))
207 //                    .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1"))
208 //                    .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55"))
209 //                    .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2")));
210 //        this.testARPReplyPacket_1_Srld = testARPReplyPacket_1.serialize();
211 //
212 //        // Another test packet with a different source IP
213 //        this.testARPReplyPacket_2 = new Ethernet()
214 //        .setSourceMACAddress("00:99:88:77:66:55")
215 //        .setDestinationMACAddress("00:11:22:33:44:55")
216 //        .setEtherType(Ethernet.TYPE_ARP)
217 //        .setVlanID((short)5)
218 //        .setPayload(
219 //                    new ARP()
220 //                    .setHardwareType(ARP.HW_TYPE_ETHERNET)
221 //                    .setProtocolType(ARP.PROTO_TYPE_IP)
222 //                    .setHardwareAddressLength((byte) 6)
223 //                    .setProtocolAddressLength((byte) 4)
224 //                    .setOpCode(ARP.OP_REPLY)
225 //                    .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:01"))
226 //                    .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1"))
227 //                    .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55"))
228 //                    .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2")));
229 //        this.testARPReplyPacket_2_Srld = testARPReplyPacket_2.serialize();
230 //
231 //        // Build the PacketIn
232 //        this.packetIn_1 = ((OFPacketIn) mockFloodlightProvider.
233 //                getOFMessageFactory().getMessage(OFType.PACKET_IN))
234 //                .setBufferId(-1)
235 //                .setInPort((short) 1)
236 //                .setPacketData(this.testARPReplyPacket_1_Srld)
237 //                .setReason(OFPacketInReason.NO_MATCH)
238 //                .setTotalLength((short) this.testARPReplyPacket_1_Srld.length);
239 //
240 //        // Build the PacketIn
241 //        this.packetIn_2 = ((OFPacketIn) mockFloodlightProvider.
242 //                getOFMessageFactory().getMessage(OFType.PACKET_IN))
243 //                .setBufferId(-1)
244 //                .setInPort((short) 1)
245 //                .setPacketData(this.testARPReplyPacket_2_Srld)
246 //                .setReason(OFPacketInReason.NO_MATCH)
247 //                .setTotalLength((short) this.testARPReplyPacket_2_Srld.length);
248 //    }
249 //
250 //
251 //
252 //
253 //
254 //    @Test
255 //    public void testLastSeen() throws Exception {
256 //        Calendar c = Calendar.getInstance();
257 //        Date d1 = c.getTime();
258 //        Entity entity1 = new Entity(1L, null, null, null, null, d1);
259 //        c.add(Calendar.SECOND, 1);
260 //        Entity entity2 = new Entity(1L, null, 1, null, null, c.getTime());
261 //
262 //        IDevice d = deviceManager.learnDeviceByEntity(entity2);
263 //        assertEquals(c.getTime(), d.getLastSeen());
264 //        d = deviceManager.learnDeviceByEntity(entity1);
265 //        assertEquals(c.getTime(), d.getLastSeen());
266 //
267 //        deviceManager.startUp(null);
268 //        d = deviceManager.learnDeviceByEntity(entity1);
269 //        assertEquals(d1, d.getLastSeen());
270 //        d = deviceManager.learnDeviceByEntity(entity2);
271 //        assertEquals(c.getTime(), d.getLastSeen());
272 //    }
273 //
274 //    @Test
275 //    public void testEntityLearning() throws Exception {
276 //        IDeviceListener mockListener =
277 //                createMock(IDeviceListener.class);
278 //        expect(mockListener.getName()).andReturn("mockListener").atLeastOnce();
279 //        expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
280 //        .andReturn(false).atLeastOnce();
281 //        expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
282 //        .andReturn(false).atLeastOnce();
283 //
284 //        replay(mockListener);
285 //        deviceManager.addListener(mockListener);
286 //        verify(mockListener);
287 //        reset(mockListener);
288 //        deviceManager.entityClassifier= new MockEntityClassifier();
289 //        deviceManager.startUp(null);
290 //
291 //        ITopologyService mockTopology = createMock(ITopologyService.class);
292 //        expect(mockTopology.getL2DomainId(anyLong())).
293 //        andReturn(1L).anyTimes();
294 //        expect(mockTopology.isBroadcastDomainPort(anyLong(), anyShort())).
295 //        andReturn(false).anyTimes();
296 //
297 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
298 //                                                  anyShort())).andReturn(true).anyTimes();
299 //        expect(mockTopology.isConsistent(10L, (short)1, 10L, (short)1)).
300 //        andReturn(true).anyTimes();
301 //        expect(mockTopology.isConsistent(1L, (short)1, 1L, (short)1)).
302 //        andReturn(true).anyTimes();
303 //        expect(mockTopology.isConsistent(50L, (short)3, 50L, (short)3)).
304 //        andReturn(true).anyTimes();
305 //
306 //        Date topologyUpdateTime = new Date();
307 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
308 //        anyTimes();
309 //
310 //        deviceManager.topology = mockTopology;
311 //
312 //        Entity entity1 = new Entity(1L, null, null, 1L, 1, new Date());
313 //        Entity entity2 = new Entity(1L, null, null, 10L, 1, new Date());
314 //        Entity entity3 = new Entity(1L, null, 1, 10L, 1, new Date());
315 //        Entity entity4 = new Entity(1L, null, 1, 1L, 1, new Date());
316 //        Entity entity5 = new Entity(2L, (short)4, 1, 5L, 2, new Date());
317 //        Entity entity6 = new Entity(2L, (short)4, 1, 50L, 3, new Date());
318 //        Entity entity7 = new Entity(2L, (short)4, 2, 50L, 3, new Date());
319 //
320 //        mockListener.deviceAdded(isA(IDevice.class));
321 //        replay(mockListener, mockTopology);
322 //
323 //        Device d1 = deviceManager.learnDeviceByEntity(entity1);
324 //        assertSame(d1, deviceManager.learnDeviceByEntity(entity1));
325 //        assertSame(d1, deviceManager.findDeviceByEntity(entity1));
326 //        assertEquals(DefaultEntityClassifier.entityClass ,
327 //                          d1.getEntityClass());
328 //        assertArrayEquals(new Short[] { -1 }, d1.getVlanId());
329 //        assertArrayEquals(new Integer[] { }, d1.getIPv4Addresses());
330 //
331 //        assertEquals(1, deviceManager.getAllDevices().size());
332 //        verify(mockListener);
333 //
334 //        reset(mockListener);
335 //        mockListener.deviceAdded(isA(IDevice.class));
336 //        replay(mockListener);
337 //
338 //        Device d2 = deviceManager.learnDeviceByEntity(entity2);
339 //        assertFalse(d1.equals(d2));
340 //        assertNotSame(d1, d2);
341 //        assertNotSame(d1.getDeviceKey(), d2.getDeviceKey());
342 //        assertEquals(MockEntityClassifier.testEC, d2.getEntityClass());
343 //        assertArrayEquals(new Short[] { -1 }, d2.getVlanId());
344 //        assertArrayEquals(new Integer[] { }, d2.getIPv4Addresses());
345 //
346 //        assertEquals(2, deviceManager.getAllDevices().size());
347 //        verify(mockListener);
348 //
349 //        reset(mockListener);
350 //        mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
351 //        replay(mockListener);
352 //
353 //        Device d3 = deviceManager.learnDeviceByEntity(entity3);
354 //        assertNotSame(d2, d3);
355 //        assertEquals(d2.getDeviceKey(), d3.getDeviceKey());
356 //        assertEquals(MockEntityClassifier.testEC, d3.getEntityClass());
357 //        assertArrayEquals(new Integer[] { 1 },
358 //                          d3.getIPv4Addresses());
359 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1) },
360 //                          d3.getAttachmentPoints());
361 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1) },
362 //                          d3.getAttachmentPoints(true));
363 //        assertArrayEquals(new Short[] { -1 },
364 //                          d3.getVlanId());
365 //
366 //        assertEquals(2, deviceManager.getAllDevices().size());
367 //        verify(mockListener);
368 //
369 //        reset(mockListener);
370 //        mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
371 //        replay(mockListener);
372 //
373 //        Device d4 = deviceManager.learnDeviceByEntity(entity4);
374 //        assertNotSame(d1, d4);
375 //        assertEquals(d1.getDeviceKey(), d4.getDeviceKey());
376 //        assertEquals(DefaultEntityClassifier.entityClass, d4.getEntityClass());
377 //        assertArrayEquals(new Integer[] { 1 },
378 //                          d4.getIPv4Addresses());
379 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
380 //                          d4.getAttachmentPoints());
381 //        assertArrayEquals(new Short[] { -1 },
382 //                          d4.getVlanId());
383 //
384 //        assertEquals(2, deviceManager.getAllDevices().size());
385 //        verify(mockListener);
386 //
387 //        reset(mockListener);
388 //        mockListener.deviceAdded((isA(IDevice.class)));
389 //        replay(mockListener);
390 //
391 //        Device d5 = deviceManager.learnDeviceByEntity(entity5);
392 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 2) },
393 //                          d5.getAttachmentPoints());
394 //        assertArrayEquals(new Short[] { (short) 4 },
395 //                          d5.getVlanId());
396 //        assertEquals(2L, d5.getMACAddress());
397 //        assertEquals("00:00:00:00:00:02", d5.getMACAddressString());
398 //        verify(mockListener);
399 //
400 //        reset(mockListener);
401 //        mockListener.deviceAdded(isA(IDevice.class));
402 //        replay(mockListener);
403 //
404 //        Device d6 = deviceManager.learnDeviceByEntity(entity6);
405 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(50L, 3) },
406 //                          d6.getAttachmentPoints());
407 //        assertArrayEquals(new Short[] { (short) 4 },
408 //                          d6.getVlanId());
409 //
410 //        assertEquals(4, deviceManager.getAllDevices().size());
411 //        verify(mockListener);
412 //
413 //        reset(mockListener);
414 //        mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
415 //        replay(mockListener);
416 //
417 //        Device d7 = deviceManager.learnDeviceByEntity(entity7);
418 //        assertNotSame(d6, d7);
419 //        assertEquals(d6.getDeviceKey(), d7.getDeviceKey());
420 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(50L, 3) },
421 //                          d7.getAttachmentPoints());
422 //        assertArrayEquals(new Short[] { (short) 4 },
423 //                          d7.getVlanId());
424 //
425 //        assertEquals(4, deviceManager.getAllDevices().size());
426 //        verify(mockListener);
427 //
428 //
429 //        reset(mockListener);
430 //        replay(mockListener);
431 //
432 //        reset(deviceManager.topology);
433 //        deviceManager.topology.addListener(deviceManager);
434 //        expectLastCall().times(1);
435 //        replay(deviceManager.topology);
436 //
437 //        deviceManager.entityClassifier = new MockEntityClassifierMac();
438 //        deviceManager.startUp(null);
439 //        Entity entityNoClass = new Entity(5L, (short)1, 5, -1L, 1, new Date());
440 //        assertEquals(null, deviceManager.learnDeviceByEntity(entityNoClass));
441 //
442 //        verify(mockListener);
443 //    }
444 //
445 //
446 //    private void doTestEntityOrdering(boolean computeInsertionPoint) throws Exception {
447 //        Entity e = new Entity(10L, null, null, null, null, null);
448 //        IEntityClass ec = createNiceMock(IEntityClass.class);
449 //        Device d = new Device(deviceManager, 1L, e, ec);
450 //
451 //        int expectedLength = 1;
452 //        Long[] macs = new Long[] {  5L,  // new first element
453 //                                   15L,  // new last element
454 //                                    7L,  // insert in middle
455 //                                   12L,  // insert in middle
456 //                                    6L,  // insert at idx 1
457 //                                   14L,  // insert at idx length-2
458 //                                    1L,
459 //                                   20L
460 //                                  };
461 //
462 //        for (Long mac: macs) {
463 //            e = new Entity(mac, null, null, null, null, null);
464 //            int insertionPoint;
465 //            if (computeInsertionPoint) {
466 //                insertionPoint = -(Arrays.binarySearch(d.entities, e)+1);
467 //            } else {
468 //                insertionPoint = -1;
469 //            }
470 //            d = deviceManager.allocateDevice(d, e, insertionPoint);
471 //            expectedLength++;
472 //            assertEquals(expectedLength, d.entities.length);
473 //            for (int i = 0; i < d.entities.length-1; i++)
474 //                assertEquals(-1, d.entities[i].compareTo(d.entities[i+1]));
475 //        }
476 //    }
477 //
478 //    @Test
479 //    public void testEntityOrderingExternal() throws Exception {
480 //        doTestEntityOrdering(true);
481 //    }
482 //
483 //    @Test
484 //    public void testEntityOrderingInternal() throws Exception {
485 //        doTestEntityOrdering(false);
486 //    }
487 //
488 //    @Test
489 //    public void testAttachmentPointLearning() throws Exception {
490 //        IDeviceListener mockListener =
491 //                createMock(IDeviceListener.class);
492 //        expect(mockListener.getName()).andReturn("mockListener").atLeastOnce();
493 //        expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
494 //        .andReturn(false).atLeastOnce();
495 //        expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
496 //        .andReturn(false).atLeastOnce();
497 //
498 //        replay(mockListener);
499 //        deviceManager.addListener(mockListener);
500 //        verify(mockListener);
501 //        reset(mockListener);
502 //
503 //        ITopologyService mockTopology = createMock(ITopologyService.class);
504 //        expect(mockTopology.getL2DomainId(1L)).
505 //        andReturn(1L).anyTimes();
506 //        expect(mockTopology.getL2DomainId(5L)).
507 //        andReturn(1L).anyTimes();
508 //        expect(mockTopology.getL2DomainId(10L)).
509 //        andReturn(10L).anyTimes();
510 //        expect(mockTopology.getL2DomainId(50L)).
511 //        andReturn(10L).anyTimes();
512 //        expect(mockTopology.isBroadcastDomainPort(anyLong(), anyShort())).
513 //        andReturn(false).anyTimes();
514 //        expect(mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
515 //                                                    anyLong(), anyShort())).andReturn(false).anyTimes();
516 //
517 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
518 //                                                  anyShort())).andReturn(true).anyTimes();
519 //        expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)1)).
520 //        andReturn(false).anyTimes();
521 //        expect(mockTopology.isConsistent(5L, (short)1, 10L, (short)1)).
522 //        andReturn(false).anyTimes();
523 //        expect(mockTopology.isConsistent(10L, (short)1, 50L, (short)1)).
524 //        andReturn(false).anyTimes();
525 //
526 //        Date topologyUpdateTime = new Date();
527 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
528 //        anyTimes();
529 //
530 //        replay(mockTopology);
531 //
532 //        deviceManager.topology = mockTopology;
533 //
534 //        Calendar c = Calendar.getInstance();
535 //        Entity entity1 = new Entity(1L, null, 1, 1L, 1, c.getTime());
536 //        Entity entity0 = new Entity(1L, null, null, null, null, c.getTime());
537 //        c.add(Calendar.SECOND, 1);
538 //        Entity entity2 = new Entity(1L, null, null, 5L, 1, c.getTime());
539 //        c.add(Calendar.SECOND, 1);
540 //        Entity entity3 = new Entity(1L, null, null, 10L, 1, c.getTime());
541 //        c.add(Calendar.SECOND, 1);
542 //        Entity entity4 = new Entity(1L, null, null, 50L, 1, c.getTime());
543 //
544 //        IDevice d;
545 //        SwitchPort[] aps;
546 //        Integer[] ips;
547 //
548 //        mockListener.deviceAdded(isA(IDevice.class));
549 //        replay(mockListener);
550 //
551 //        deviceManager.learnDeviceByEntity(entity1);
552 //        d = deviceManager.learnDeviceByEntity(entity0);
553 //        assertEquals(1, deviceManager.getAllDevices().size());
554 //        aps = d.getAttachmentPoints();
555 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) }, aps);
556 //        ips = d.getIPv4Addresses();
557 //        assertArrayEquals(new Integer[] { 1 }, ips);
558 //        verify(mockListener);
559 //
560 //        reset(mockListener);
561 //        mockListener.deviceMoved((isA(IDevice.class)));
562 //        replay(mockListener);
563 //
564 //        d = deviceManager.learnDeviceByEntity(entity2);
565 //        assertEquals(1, deviceManager.getAllDevices().size());
566 //        aps = d.getAttachmentPoints();
567 //
568 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 1) }, aps);
569 //        ips = d.getIPv4Addresses();
570 //        assertArrayEquals(new Integer[] { 1 }, ips);
571 //        verify(mockListener);
572 //
573 //        reset(mockListener);
574 //        mockListener.deviceMoved((isA(IDevice.class)));
575 //        replay(mockListener);
576 //
577 //        d = deviceManager.learnDeviceByEntity(entity3);
578 //        assertEquals(1, deviceManager.getAllDevices().size());
579 //        aps = d.getAttachmentPoints();
580 //        assertArrayEquals(new SwitchPort[] {new SwitchPort(5L, 1), new SwitchPort(10L, 1)}, aps);
581 //        ips = d.getIPv4Addresses();
582 //        assertArrayEquals(new Integer[] { 1 }, ips);
583 //        verify(mockListener);
584 //
585 //        reset(mockListener);
586 //        mockListener.deviceMoved((isA(IDevice.class)));
587 //        replay(mockListener);
588 //
589 //        d = deviceManager.learnDeviceByEntity(entity4);
590 //        assertEquals(1, deviceManager.getAllDevices().size());
591 //        aps = d.getAttachmentPoints();
592 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 1),
593 //                                             new SwitchPort(50L, 1) }, aps);
594 //        ips = d.getIPv4Addresses();
595 //        assertArrayEquals(new Integer[] { 1 }, ips);
596 //        verify(mockListener);
597 //    }
598 //
599 //    private void verifyEntityArray(Entity[] expected, Device d) {
600 //        Arrays.sort(expected);
601 //        assertArrayEquals(expected, d.entities);
602 //    }
603 //
604 //    @Test
605 //    public void testNoLearningOnInternalPorts() throws Exception {
606 //        IDeviceListener mockListener =
607 //                createMock(IDeviceListener.class);
608 //
609 //        expect(mockListener.getName()).andReturn("mockListener").anyTimes();
610 //        expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
611 //        .andReturn(false).atLeastOnce();
612 //        expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
613 //        .andReturn(false).atLeastOnce();
614 //
615 //        replay(mockListener);
616 //        deviceManager.addListener(mockListener);
617 //        verify(mockListener);
618 //        reset(mockListener);
619 //
620 //        ITopologyService mockTopology = createMock(ITopologyService.class);
621 //        expect(mockTopology.getL2DomainId(1L)).
622 //        andReturn(1L).anyTimes();
623 //        expect(mockTopology.getL2DomainId(2L)).
624 //        andReturn(1L).anyTimes();
625 //        expect(mockTopology.getL2DomainId(3L)).
626 //        andReturn(1L).anyTimes();
627 //        expect(mockTopology.getL2DomainId(4L)).
628 //        andReturn(1L).anyTimes();
629 //        expect(mockTopology.isBroadcastDomainPort(anyLong(), anyShort()))
630 //                .andReturn(false).anyTimes();
631 //        expect(mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
632 //                                                    anyLong(), anyShort()))
633 //                .andReturn(false).anyTimes();
634 //
635 //        expect(mockTopology.isAttachmentPointPort(or(eq(1L), eq(3L)), anyShort()))
636 //                .andReturn(true).anyTimes();
637 //        // Switches 2 and 4 have only internal ports
638 //        expect(mockTopology.isAttachmentPointPort(or(eq(2L), eq(4L)), anyShort()))
639 //                .andReturn(false).anyTimes();
640 //
641 //        expect(mockTopology.isConsistent(1L, (short)1, 3L, (short)1))
642 //                .andReturn(false).once();
643 //
644 //        Date topologyUpdateTime = new Date();
645 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
646 //        anyTimes();
647 //
648 //        replay(mockTopology);
649 //
650 //        deviceManager.topology = mockTopology;
651 //
652 //        Calendar c = Calendar.getInstance();
653 //        Entity entity1 = new Entity(1L, null, 1, 1L, 1, c.getTime());
654 //        c.add(Calendar.SECOND, 1);
655 //        Entity entity2 = new Entity(1L, null, 2, 2L, 1, c.getTime());
656 //        c.add(Calendar.SECOND, 1);
657 //        Entity entity3 = new Entity(1L, null, 3, 3L, 1, c.getTime());
658 //        c.add(Calendar.SECOND, 1);
659 //        Entity entity4 = new Entity(1L, null, 4, 4L, 1, c.getTime());
660 //
661 //        IDevice d;
662 //        SwitchPort[] aps;
663 //        Integer[] ips;
664 //
665 //        mockListener.deviceAdded(isA(IDevice.class));
666 //        expectLastCall().once();
667 //        replay(mockListener);
668 //
669 //        // cannot learn device internal ports
670 //        d = deviceManager.learnDeviceByEntity(entity2);
671 //        assertNull(d);
672 //        d = deviceManager.learnDeviceByEntity(entity4);
673 //        assertNull(d);
674 //
675 //        d = deviceManager.learnDeviceByEntity(entity1);
676 //        assertEquals(1, deviceManager.getAllDevices().size());
677 //        aps = d.getAttachmentPoints();
678 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) }, aps);
679 //        verifyEntityArray(new Entity[] { entity1 } , (Device)d);
680 //        ips = d.getIPv4Addresses();
681 //        assertArrayEquals(new Integer[] { 1 }, ips);
682 //        verify(mockListener);
683 //
684 //        reset(mockListener);
685 //        replay(mockListener);
686 //
687 //        // don't learn
688 //        d = deviceManager.learnDeviceByEntity(entity2);
689 //        assertEquals(1, deviceManager.getAllDevices().size());
690 //        aps = d.getAttachmentPoints();
691 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) }, aps);
692 //        verifyEntityArray(new Entity[] { entity1 } , (Device)d);
693 //        ips = d.getIPv4Addresses();
694 //        assertArrayEquals(new Integer[] { 1 }, ips);
695 //        verify(mockListener);
696 //
697 //        reset(mockListener);
698 //        mockListener.deviceMoved(isA(IDevice.class));
699 //        mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
700 //        replay(mockListener);
701 //
702 //        // learn
703 //        d = deviceManager.learnDeviceByEntity(entity3);
704 //        assertEquals(1, deviceManager.getAllDevices().size());
705 //        aps = d.getAttachmentPoints();
706 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(3L, 1) }, aps);
707 //        verifyEntityArray(new Entity[] { entity1, entity3 } , (Device)d);
708 //        ips = d.getIPv4Addresses();
709 //        Arrays.sort(ips);
710 //        assertArrayEquals(new Integer[] { 1, 3 }, ips);
711 //        verify(mockListener);
712 //
713 //        reset(mockListener);
714 //        replay(mockListener);
715 //
716 //        // don't learn
717 //        d = deviceManager.learnDeviceByEntity(entity4);
718 //        assertEquals(1, deviceManager.getAllDevices().size());
719 //        aps = d.getAttachmentPoints();
720 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(3L, 1) }, aps);
721 //        verifyEntityArray(new Entity[] { entity1, entity3 } , (Device)d);
722 //        ips = d.getIPv4Addresses();
723 //        Arrays.sort(ips);
724 //        assertArrayEquals(new Integer[] { 1, 3 }, ips);
725 //        verify(mockListener);
726 //    }
727 //
728 //    @Test
729 //    public void testAttachmentPointSuppression() throws Exception {
730 //        IDeviceListener mockListener =
731 //                createMock(IDeviceListener.class);
732 //
733 //        expect(mockListener.getName()).andReturn("mockListener").anyTimes();
734 //        expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
735 //        .andReturn(false).atLeastOnce();
736 //        expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
737 //        .andReturn(false).atLeastOnce();
738 //
739 //        replay(mockListener);
740 //        deviceManager.addListener(mockListener);
741 //        verify(mockListener);
742 //        reset(mockListener);
743 //
744 //        ITopologyService mockTopology = createMock(ITopologyService.class);
745 //        expect(mockTopology.getL2DomainId(1L)).
746 //        andReturn(1L).anyTimes();
747 //        expect(mockTopology.getL2DomainId(5L)).
748 //        andReturn(1L).anyTimes();
749 //        expect(mockTopology.getL2DomainId(10L)).
750 //        andReturn(10L).anyTimes();
751 //        expect(mockTopology.getL2DomainId(50L)).
752 //        andReturn(10L).anyTimes();
753 //        expect(mockTopology.isBroadcastDomainPort(anyLong(), anyShort()))
754 //                .andReturn(false).anyTimes();
755 //        expect(mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
756 //                                                    anyLong(), anyShort()))
757 //                .andReturn(false).anyTimes();
758 //
759 //        expect(mockTopology.isAttachmentPointPort(anyLong(), anyShort()))
760 //                .andReturn(true).anyTimes();
761 //        expect(mockTopology.isConsistent(5L, (short)1, 50L, (short)1))
762 //                .andReturn(false).anyTimes();
763 //
764 //        Date topologyUpdateTime = new Date();
765 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
766 //        anyTimes();
767 //
768 //        replay(mockTopology);
769 //
770 //        deviceManager.topology = mockTopology;
771 //        // suppress (1L, 1) and (10L, 1)
772 //        deviceManager.addSuppressAPs(1L, (short)1);
773 //        deviceManager.addSuppressAPs(10L, (short)1);
774 //
775 //        Calendar c = Calendar.getInstance();
776 //        Entity entity0 = new Entity(1L, null, null, null, null, c.getTime());
777 //        // No attachment point should be learnt on 1L, 1
778 //        Entity entity1 = new Entity(1L, null, 1, 1L, 1, c.getTime());
779 //        c.add(Calendar.SECOND, 1);
780 //        Entity entity2 = new Entity(1L, null, 1, 5L, 1, c.getTime());
781 //        c.add(Calendar.SECOND, 1);
782 //        Entity entity3 = new Entity(1L, null, null, 10L, 1, c.getTime());
783 //        c.add(Calendar.SECOND, 1);
784 //        Entity entity4 = new Entity(1L, null, null, 50L, 1, c.getTime());
785 //
786 //        IDevice d;
787 //        SwitchPort[] aps;
788 //        Integer[] ips;
789 //
790 //        mockListener.deviceAdded(isA(IDevice.class));
791 //        mockListener.deviceIPV4AddrChanged((isA(IDevice.class)));
792 //        replay(mockListener);
793 //
794 //        // TODO: we currently do learn entities on suppressed APs
795 //        // // cannot learn device on suppressed AP
796 //        // d = deviceManager.learnDeviceByEntity(entity1);
797 //        // assertNull(d);
798 //
799 //        deviceManager.learnDeviceByEntity(entity0);
800 //        d = deviceManager.learnDeviceByEntity(entity1);
801 //        assertEquals(1, deviceManager.getAllDevices().size());
802 //        aps = d.getAttachmentPoints();
803 //        assertEquals(aps.length, 0);
804 //        verifyEntityArray(new Entity[] { entity0, entity1} , (Device)d);
805 //        ips = d.getIPv4Addresses();
806 //        assertArrayEquals(new Integer[] { 1 }, ips);
807 //        verify(mockListener);
808 //
809 //        reset(mockListener);
810 //        mockListener.deviceMoved((isA(IDevice.class)));
811 //        //mockListener.deviceIPV4AddrChanged((isA(IDevice.class)));
812 //        replay(mockListener);
813 //        d = deviceManager.learnDeviceByEntity(entity2);
814 //        assertEquals(1, deviceManager.getAllDevices().size());
815 //        aps = d.getAttachmentPoints();
816 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 1) }, aps);
817 //        verifyEntityArray(new Entity[] { entity0, entity1, entity2 } , (Device)d);
818 //        ips = d.getIPv4Addresses();
819 //        assertArrayEquals(new Integer[] { 1 }, ips);
820 //        verify(mockListener);
821 //
822 //        reset(mockListener);
823 //        replay(mockListener);
824 //
825 //        d = deviceManager.learnDeviceByEntity(entity3);
826 //        assertEquals(1, deviceManager.getAllDevices().size());
827 //        aps = d.getAttachmentPoints();
828 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 1) }, aps);
829 //        verifyEntityArray(new Entity[] { entity0, entity1, entity2, entity3 } , (Device)d);
830 //        ips = d.getIPv4Addresses();
831 //        assertArrayEquals(new Integer[] { 1 }, ips);
832 //        verify(mockListener);
833 //
834 //        reset(mockListener);
835 //        mockListener.deviceMoved((isA(IDevice.class)));
836 //        replay(mockListener);
837 //
838 //        d = deviceManager.learnDeviceByEntity(entity4);
839 //        assertEquals(1, deviceManager.getAllDevices().size());
840 //        aps = d.getAttachmentPoints();
841 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 1),
842 //                                             new SwitchPort(50L, 1) }, aps);
843 //        verifyEntityArray(new Entity[] { entity0, entity1, entity2, entity3, entity4} , (Device)d);
844 //        ips = d.getIPv4Addresses();
845 //        assertArrayEquals(new Integer[] { 1 }, ips);
846 //        verify(mockListener);
847 //    }
848 //
849 //    @Test
850 //    public void testBDAttachmentPointLearning() throws Exception {
851 //        ITopologyService mockTopology = createMock(ITopologyService.class);
852 //        expect(mockTopology.getL2DomainId(anyLong())).
853 //        andReturn(1L).anyTimes();
854 //        expect(mockTopology.isAttachmentPointPort(anyLong(), anyShort())).
855 //        andReturn(true).anyTimes();
856 //        expect(mockTopology.isBroadcastDomainPort(1L, (short)1)).
857 //        andReturn(false).anyTimes();
858 //        expect(mockTopology.isBroadcastDomainPort(1L, (short)2)).
859 //        andReturn(true).anyTimes();
860 //        expect(mockTopology.isInSameBroadcastDomain(1L, (short)1,
861 //                                                    1L, (short)2)).andReturn(true).anyTimes();
862 //        expect(mockTopology.isInSameBroadcastDomain(1L, (short)2,
863 //                                                    1L, (short)1)).andReturn(true).anyTimes();
864 //        expect(mockTopology.isConsistent(anyLong(), anyShort(), anyLong(), anyShort())).andReturn(false).anyTimes();
865 //
866 //        Date topologyUpdateTime = new Date();
867 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
868 //        anyTimes();
869 //
870 //        replay(mockTopology);
871 //
872 //        deviceManager.topology = mockTopology;
873 //
874 //        Calendar c = Calendar.getInstance();
875 //        Entity entity1 = new Entity(1L, null, 1, 1L, 1, c.getTime());
876 //        c.add(Calendar.MILLISECOND,
877 //              (int)AttachmentPoint.OPENFLOW_TO_EXTERNAL_TIMEOUT/ 2);
878 //        Entity entity2 = new Entity(1L, null, null, 1L, 2, c.getTime());
879 //        c.add(Calendar.MILLISECOND,
880 //              (int)AttachmentPoint.OPENFLOW_TO_EXTERNAL_TIMEOUT / 2 + 1);
881 //        Entity entity3 = new Entity(1L, null, null, 1L, 2, c.getTime());
882 //
883 //        IDevice d;
884 //        SwitchPort[] aps;
885 //
886 //        d = deviceManager.learnDeviceByEntity(entity1);
887 //        assertEquals(1, deviceManager.getAllDevices().size());
888 //        aps = d.getAttachmentPoints();
889 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) }, aps);
890 //
891 //        // this timestamp is too soon; don't switch
892 //        d = deviceManager.learnDeviceByEntity(entity2);
893 //        assertEquals(1, deviceManager.getAllDevices().size());
894 //        aps = d.getAttachmentPoints();
895 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) }, aps);
896 //
897 //        // it should switch when we learn with a timestamp after the
898 //        // timeout
899 //        d = deviceManager.learnDeviceByEntity(entity3);
900 //        assertEquals(1, deviceManager.getAllDevices().size());
901 //        aps = d.getAttachmentPoints();
902 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 2) }, aps);
903 //    }
904 //
905 //    /**
906 //     * This test verifies that the learning behavior on OFPP_LOCAL ports.
907 //     * Once a host is learned on OFPP_LOCAL, it is allowed to move only from
908 //     * one OFPP_LOCAL to another OFPP_LOCAL port.
909 //     * @throws Exception
910 //     */
911 //    @Test
912 //    public void testLOCALAttachmentPointLearning() throws Exception {
913 //        ITopologyService mockTopology = createMock(ITopologyService.class);
914 //        expect(mockTopology.getL2DomainId(anyLong())).
915 //        andReturn(1L).anyTimes();
916 //        expect(mockTopology.isAttachmentPointPort(anyLong(), anyShort())).
917 //        andReturn(true).anyTimes();
918 //        expect(mockTopology.isBroadcastDomainPort(1L, (short)1)).
919 //        andReturn(false).anyTimes();
920 //        expect(mockTopology.isBroadcastDomainPort(1L, OFPort.OFPP_LOCAL.getValue())).
921 //        andReturn(false).anyTimes();
922 //        expect(mockTopology.isBroadcastDomainPort(1L, (short)2)).
923 //        andReturn(true).anyTimes();
924 //        expect(mockTopology.isInSameBroadcastDomain(1L, (short)1,
925 //                                                    1L, OFPort.OFPP_LOCAL.getValue())).andReturn(true).anyTimes();
926 //        expect(mockTopology.isInSameBroadcastDomain(1L, OFPort.OFPP_LOCAL.getValue(),
927 //                                                    1L, (short)2)).andReturn(true).anyTimes();
928 //        expect(mockTopology.isInSameBroadcastDomain(1L, (short)2,
929 //                                                    1L, OFPort.OFPP_LOCAL.getValue())).andReturn(true).anyTimes();
930 //        expect(mockTopology.isConsistent(anyLong(), anyShort(), anyLong(), anyShort())).andReturn(false).anyTimes();
931 //
932 //        Date topologyUpdateTime = new Date();
933 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
934 //        anyTimes();
935 //
936 //        replay(mockTopology);
937 //
938 //        deviceManager.topology = mockTopology;
939 //
940 //        Calendar c = Calendar.getInstance();
941 //        Entity entity1 = new Entity(1L, null, 1, 1L, 1, c.getTime());
942 //        c.add(Calendar.MILLISECOND,
943 //              (int)AttachmentPoint.OPENFLOW_TO_EXTERNAL_TIMEOUT/ 2);
944 //        Entity entity2 = new Entity(1L, null, null, 1L, (int)OFPort.OFPP_LOCAL.getValue(), c.getTime());
945 //        c.add(Calendar.MILLISECOND,
946 //              (int)AttachmentPoint.OPENFLOW_TO_EXTERNAL_TIMEOUT + 1);
947 //        Entity entity3 = new Entity(1L, null, null, 1L, 2, c.getTime());
948 //
949 //        IDevice d;
950 //        SwitchPort[] aps;
951 //
952 //        d = deviceManager.learnDeviceByEntity(entity1);
953 //        assertEquals(1, deviceManager.getAllDevices().size());
954 //        aps = d.getAttachmentPoints();
955 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) }, aps);
956 //
957 //        // Ensure that the attachment point changes to OFPP_LOCAL
958 //        d = deviceManager.learnDeviceByEntity(entity2);
959 //        assertEquals(1, deviceManager.getAllDevices().size());
960 //        aps = d.getAttachmentPoints();
961 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, OFPort.OFPP_LOCAL.getValue()) }, aps);
962 //
963 //        // Even though the new attachment point is consistent with old
964 //        // and the time has elapsed, OFPP_LOCAL attachment point should
965 //        // be maintained.
966 //        d = deviceManager.learnDeviceByEntity(entity3);
967 //        assertEquals(1, deviceManager.getAllDevices().size());
968 //        aps = d.getAttachmentPoints();
969 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, OFPort.OFPP_LOCAL.getValue()) }, aps);
970 //    }
971 //
972 //    @Test
973 //    public void testPacketInBasic(byte[] deviceMac, OFPacketIn packetIn) {
974 //        // Mock up our expected behavior
975 //        ITopologyService mockTopology = createMock(ITopologyService.class);
976 //        deviceManager.topology = mockTopology;
977 //        expect(mockTopology.isAttachmentPointPort(EasyMock.anyLong(),
978 //                EasyMock.anyShort())).
979 //                andReturn(true).anyTimes();
980 //        expect(mockTopology.isConsistent(EasyMock.anyLong(),
981 //                EasyMock.anyShort(),
982 //                EasyMock.anyLong(),
983 //                EasyMock.anyShort())).andReturn(false).
984 //                anyTimes();
985 //        expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
986 //        replay(mockTopology);
987 //
988 //        Date currentDate = new Date();
989 //
990 //        // build our expected Device
991 //        Integer ipaddr = IPv4.toIPv4Address("192.168.1.1");
992 //        Device device =
993 //                new Device(deviceManager,
994 //                        new Long(deviceManager.deviceKeyCounter),
995 //                        new Entity(Ethernet.toLong(deviceMac),
996 //                                (short)5,
997 //                                ipaddr,
998 //                                1L,
999 //                                1,
1000 //                                currentDate),
1001 //                                DefaultEntityClassifier.entityClass);
1002 //
1003 //        // Get the listener and trigger the packet in
1004 //        IOFSwitch switch1 = mockFloodlightProvider.getSwitch(1L);
1005 //        mockFloodlightProvider.dispatchMessage(switch1, packetIn);
1006 //
1007 //        // Verify the replay matched our expectations
1008 //        // verify(mockTopology);
1009 //
1010 //        // Verify the device
1011 //        Device rdevice = (Device)
1012 //                deviceManager.findDevice(Ethernet.toLong(deviceMac),
1013 //                        (short)5, null, null, null);
1014 //
1015 //        assertEquals(device, rdevice);
1016 //        assertEquals(new Short((short)5), rdevice.getVlanId()[0]);
1017 //
1018 //        Device result = null;
1019 //        Iterator<? extends IDevice> dstiter =
1020 //                deviceManager.queryClassDevices(device.getEntityClass(),
1021 //                        null, null, ipaddr,
1022 //                        null, null);
1023 //        if (dstiter.hasNext()) {
1024 //            result = (Device)dstiter.next();
1025 //        }
1026 //
1027 //        assertEquals(device, result);
1028 //
1029 //        device =
1030 //                new Device(device,
1031 //                        new Entity(Ethernet.toLong(deviceMac),
1032 //                                (short)5,
1033 //                                ipaddr,
1034 //                                5L,
1035 //                                2,
1036 //                                currentDate),
1037 //                                -1);
1038 //
1039 //        reset(mockTopology);
1040 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1041 //                anyShort())).
1042 //                andReturn(true).
1043 //                anyTimes();
1044 //        expect(mockTopology.isConsistent(EasyMock.anyLong(),
1045 //                EasyMock.anyShort(),
1046 //                EasyMock.anyLong(),
1047 //                EasyMock.anyShort())).andReturn(false).
1048 //                anyTimes();
1049 //        expect(mockTopology.isBroadcastDomainPort(EasyMock.anyLong(),
1050 //                EasyMock.anyShort()))
1051 //                .andReturn(false)
1052 //                .anyTimes();
1053 //        expect(mockTopology.getL2DomainId(1L)).andReturn(1L).anyTimes();
1054 //        expect(mockTopology.getL2DomainId(5L)).andReturn(1L).anyTimes();
1055 //        expect(mockTopology.isInSameBroadcastDomain(1L, (short)1, 5L, (short)2)).
1056 //        andReturn(false).anyTimes();
1057 //
1058 //        // Start recording the replay on the mocks
1059 //        replay(mockTopology);
1060 //        // Get the listener and trigger the packet in
1061 //        IOFSwitch switch5 = mockFloodlightProvider.getSwitch(5L);
1062 //        mockFloodlightProvider.
1063 //        dispatchMessage(switch5, this.packetIn_1.setInPort((short)2));
1064 //
1065 //        // Verify the replay matched our expectations
1066 //        verify(mockTopology);
1067 //
1068 //        // Verify the device
1069 //        rdevice = (Device)
1070 //                deviceManager.findDevice(Ethernet.toLong(deviceMac),
1071 //                        (short)5, null, null, null);
1072 //        assertEquals(device, rdevice);
1073 //    }
1074 //
1075 //    @Test
1076 //    public void testPacketIn() throws Exception {
1077 //        byte[] deviceMac1 =
1078 //                ((Ethernet)this.testARPReplyPacket_1).getSourceMACAddress();
1079 //        testPacketInBasic(deviceMac1, packetIn_1);
1080 //    }
1081 //
1082 //    /**
1083 //     * This test ensures the device manager learns the source device
1084 //     * corresponding to the senderHardwareAddress and senderProtocolAddress
1085 //     * in an ARP response whenever the senderHardwareAddress is different
1086 //     * from the source MAC address of the Ethernet frame.
1087 //     *
1088 //     * This test is the same as testPacketIn method, except for the
1089 //     * packet-in that's used.
1090 //     * @throws Exception
1091 //     */
1092 //    @Test
1093 //    public void testDeviceLearningFromArpResponseData() throws Exception {
1094 //        ARP arp = (ARP)((Ethernet)this.testARPReplyPacket_2).getPayload();
1095 //        byte[] deviceMac2 = arp.getSenderHardwareAddress();
1096 //
1097 //        testPacketInBasic(deviceMac2, packetIn_2);
1098 //    }
1099 //
1100 //    /**
1101 //     * Note: Entity expiration does not result in device moved notification.
1102 //     * @throws Exception
1103 //     */
1104 //    public void doTestEntityExpiration() throws Exception {
1105 //        IDeviceListener mockListener =
1106 //                createMock(IDeviceListener.class);
1107 //        expect(mockListener.getName()).andReturn("mockListener").anyTimes();
1108 //        expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
1109 //        .andReturn(false).atLeastOnce();
1110 //        expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
1111 //        .andReturn(false).atLeastOnce();
1112 //
1113 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1114 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1115 //                                                  anyShort())).
1116 //                                                  andReturn(true).anyTimes();
1117 //
1118 //        expect(mockTopology.isBroadcastDomainPort(1L, (short)1)).andReturn(false).anyTimes();
1119 //        expect(mockTopology.isBroadcastDomainPort(5L, (short)1)).andReturn(false).anyTimes();
1120 //        expect(mockTopology.getL2DomainId(1L)).andReturn(1L).anyTimes();
1121 //        expect(mockTopology.getL2DomainId(5L)).andReturn(5L).anyTimes();
1122 //        expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)1)).
1123 //        andReturn(false).anyTimes();
1124 //
1125 //        Date topologyUpdateTime = new Date();
1126 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
1127 //        anyTimes();
1128 //
1129 //        replay(mockTopology);
1130 //        deviceManager.topology = mockTopology;
1131 //
1132 //        Calendar c = Calendar.getInstance();
1133 //        Entity entity1 = new Entity(1L, null, 2, 1L, 1, c.getTime());
1134 //        c.add(Calendar.MILLISECOND, -DeviceManagerImpl.ENTITY_TIMEOUT-1);
1135 //        Entity entity2 = new Entity(1L, null, 1, 5L, 1, c.getTime());
1136 //
1137 //        deviceManager.learnDeviceByEntity(entity1);
1138 //        IDevice d = deviceManager.learnDeviceByEntity(entity2);
1139 //        assertArrayEquals(new Integer[] { 1, 2 }, d.getIPv4Addresses());
1140 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1141 //                                             new SwitchPort(5L, 1)},
1142 //                                             d.getAttachmentPoints());
1143 //        Iterator<? extends IDevice> diter =
1144 //                deviceManager.queryClassDevices(d.getEntityClass(),
1145 //                                                null, null, 1, null, null);
1146 //        assertTrue(diter.hasNext());
1147 //        assertEquals(d.getDeviceKey(), diter.next().getDeviceKey());
1148 //        diter = deviceManager.queryClassDevices(d.getEntityClass(),
1149 //                                                null, null, 2, null, null);
1150 //        assertTrue(diter.hasNext());
1151 //        assertEquals(d.getDeviceKey(), diter.next().getDeviceKey());
1152 //
1153 //        replay(mockListener);
1154 //        deviceManager.addListener(mockListener);
1155 //        verify(mockListener);
1156 //        reset(mockListener);
1157 //
1158 //        mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
1159 //        replay(mockListener);
1160 //        deviceManager.entityCleanupTask.reschedule(0, null);
1161 //
1162 //        d = deviceManager.getDevice(d.getDeviceKey());
1163 //        assertArrayEquals(new Integer[] { 2 }, d.getIPv4Addresses());
1164 //
1165 //        // Attachment points are not removed, previous ones are still valid.
1166 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1167 //                                             new SwitchPort(5L, 1) },
1168 //                          d.getAttachmentPoints());
1169 //        diter = deviceManager.queryClassDevices(d.getEntityClass(),
1170 //                                                null, null, 2, null, null);
1171 //        assertTrue(diter.hasNext());
1172 //        assertEquals(d.getDeviceKey(), diter.next().getDeviceKey());
1173 //        diter = deviceManager.queryClassDevices(d.getEntityClass(),
1174 //                                                null, null, 1, null, null);
1175 //        assertFalse(diter.hasNext());
1176 //
1177 //        d = deviceManager.findDevice(1L, null, null, null, null);
1178 //        assertArrayEquals(new Integer[] { 2 }, d.getIPv4Addresses());
1179 //
1180 //        // Attachment points are not removed, previous ones are still valid.
1181 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1182 //                                             new SwitchPort(5L, 1) },
1183 //                          d.getAttachmentPoints());
1184 //
1185 //        verify(mockListener);
1186 //    }
1187 //
1188 //    public void doTestDeviceExpiration() throws Exception {
1189 //        IDeviceListener mockListener =
1190 //                createMock(IDeviceListener.class);
1191 //        expect(mockListener.getName()).andReturn("mockListener").anyTimes();
1192 //        expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
1193 //        .andReturn(false).atLeastOnce();
1194 //        expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
1195 //        .andReturn(false).atLeastOnce();
1196 //
1197 //        Calendar c = Calendar.getInstance();
1198 //        c.add(Calendar.MILLISECOND, -DeviceManagerImpl.ENTITY_TIMEOUT-1);
1199 //        Entity entity1 = new Entity(1L, null, 1, 1L, 1, c.getTime());
1200 //        Entity entity2 = new Entity(1L, null, 2, 5L, 1, c.getTime());
1201 //
1202 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1203 //        deviceManager.topology = mockTopology;
1204 //
1205 //        expect(mockTopology.isAttachmentPointPort(EasyMock.anyLong(),
1206 //                                           EasyMock.anyShort())).
1207 //                                           andReturn(true).
1208 //                                           anyTimes();
1209 //        expect(mockTopology.getL2DomainId(1L)).andReturn(1L).anyTimes();
1210 //        expect(mockTopology.getL2DomainId(5L)).andReturn(1L).anyTimes();
1211 //        expect(mockTopology.isConsistent(EasyMock.anyLong(),
1212 //                                         EasyMock.anyShort(),
1213 //                                         EasyMock.anyLong(),
1214 //                                         EasyMock.anyShort())).andReturn(false).
1215 //                                         anyTimes();
1216 //        expect(mockTopology.isBroadcastDomainPort(EasyMock.anyLong(),
1217 //                                                  EasyMock.anyShort())).
1218 //                                                  andReturn(false).anyTimes();
1219 //        replay(mockTopology);
1220 //
1221 //        IDevice d = deviceManager.learnDeviceByEntity(entity2);
1222 //        d = deviceManager.learnDeviceByEntity(entity1);
1223 //        assertArrayEquals(new Integer[] { 1, 2 }, d.getIPv4Addresses());
1224 //
1225 //        replay(mockListener);
1226 //        deviceManager.addListener(mockListener);
1227 //        verify(mockListener);
1228 //        reset(mockListener);
1229 //
1230 //        mockListener.deviceRemoved(isA(IDevice.class));
1231 //        replay(mockListener);
1232 //        deviceManager.entityCleanupTask.reschedule(0, null);
1233 //
1234 //        IDevice r = deviceManager.getDevice(d.getDeviceKey());
1235 //        assertNull(r);
1236 //        Iterator<? extends IDevice> diter =
1237 //                deviceManager.queryClassDevices(d.getEntityClass(),
1238 //                                                null, null, 1, null, null);
1239 //        assertFalse(diter.hasNext());
1240 //
1241 //        r = deviceManager.findDevice(1L, null, null, null, null);
1242 //        assertNull(r);
1243 //
1244 //        verify(mockListener);
1245 //    }
1246 //
1247 //    /*
1248 //     * A ConcurrentHashMap for devices (deviceMap) that can be used to test
1249 //     * code that specially handles concurrent modification situations. In
1250 //     * particular, we overwrite values() and will replace / remove all the
1251 //     * elements returned by values.
1252 //     *
1253 //     * The remove flag in the constructor specifies if devices returned by
1254 //     * values() should be removed or replaced.
1255 //     */
1256 //    protected static class ConcurrentlyModifiedDeviceMap
1257 //                            extends ConcurrentHashMap<Long, Device> {
1258 //        private static final long serialVersionUID = 7784938535441180562L;
1259 //        protected boolean remove;
1260 //        public ConcurrentlyModifiedDeviceMap(boolean remove) {
1261 //            super();
1262 //            this.remove = remove;
1263 //        }
1264 //
1265 //        @Override
1266 //        public Collection<Device> values() {
1267 //            // Get the values from the real map and copy them since
1268 //            // the collection returned by values can reflect changed
1269 //            Collection<Device> devs = new ArrayList<Device>(super.values());
1270 //            for (Device d: devs) {
1271 //                if (remove) {
1272 //                    // We remove the device from the underlying map
1273 //                    super.remove(d.getDeviceKey());
1274 //                } else {
1275 //                    super.remove(d.getDeviceKey());
1276 //                    // We add a different Device instance with the same
1277 //                    // key to the map. We'll do some hackery so the device
1278 //                    // is different enough to compare differently in equals
1279 //                    // but otherwise looks the same.
1280 //                    // It's ugly but it works.
1281 //                    // clone entities
1282 //                    Device newDevice = d;
1283 //                    for (Entity e: d.getEntities()) {
1284 //                        Entity newEntity = new Entity (e.macAddress,
1285 //                                                       e.vlan,
1286 //                                                       e.ipv4Address,
1287 //                                                       e.switchDPID,
1288 //                                                       e.switchPort,
1289 //                                                       e.lastSeenTimestamp);
1290 //                        if (e.vlan == null)
1291 //                            newEntity.vlan = (short)1;
1292 //                        else
1293 //                             newEntity.vlan = (short)((e.vlan + 1 % 4095)+1);
1294 //                        newDevice = new Device(newDevice, newEntity, -1);
1295 //                    }
1296 //                    assertEquals(false, newDevice.equals(d));
1297 //                    super.put(newDevice.getDeviceKey(), newDevice);
1298 //                }
1299 //            }
1300 //            return devs;
1301 //        }
1302 //    }
1303 //
1304 //    @Test
1305 //    public void testEntityExpiration() throws Exception {
1306 //        doTestEntityExpiration();
1307 //    }
1308 //
1309 //    @Test
1310 //    public void testDeviceExpiration() throws Exception {
1311 //        doTestDeviceExpiration();
1312 //    }
1313 //
1314 //    /* Test correct entity cleanup behavior when a concurrent modification
1315 //     * occurs.
1316 //     */
1317 //    @Test
1318 //    public void testEntityExpirationConcurrentModification() throws Exception {
1319 //        deviceManager.deviceMap = new ConcurrentlyModifiedDeviceMap(false);
1320 //        doTestEntityExpiration();
1321 //    }
1322 //
1323 //    /* Test correct entity cleanup behavior when a concurrent remove
1324 //     * occurs.
1325 //     */
1326 //    @Test
1327 //    public void testDeviceExpirationConcurrentRemove() throws Exception {
1328 //        deviceManager.deviceMap = new ConcurrentlyModifiedDeviceMap(true);
1329 //        doTestDeviceExpiration();
1330 //    }
1331 //
1332 //    /* Test correct entity cleanup behavior when a concurrent modification
1333 //     * occurs.
1334 //     */
1335 //    @Test
1336 //    public void testDeviceExpirationConcurrentModification() throws Exception {
1337 //        deviceManager.deviceMap = new ConcurrentlyModifiedDeviceMap(false);
1338 //        doTestDeviceExpiration();
1339 //    }
1340 //
1341 //
1342 //    @Test
1343 //    public void testAttachmentPointFlapping() throws Exception {
1344 //        Calendar c = Calendar.getInstance();
1345 //
1346 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1347 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1348 //                                                  anyShort())).andReturn(true).anyTimes();
1349 //        expect(mockTopology.isBroadcastDomainPort(anyLong(),
1350 //                                                  anyShort())).
1351 //                                                  andReturn(false).anyTimes();
1352 //        expect(mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
1353 //                                                    anyLong(), anyShort())).andReturn(false).anyTimes();
1354 //        expect(mockTopology.getL2DomainId(anyLong())).
1355 //        andReturn(1L).anyTimes();
1356 //        expect(mockTopology.isConsistent(1L, (short)1, 1L, (short)1)).
1357 //        andReturn(true).anyTimes();
1358 //        expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)1)).
1359 //        andReturn(false).anyTimes();
1360 //        expect(mockTopology.isConsistent(1L, (short)1, 10L, (short)1)).
1361 //        andReturn(false).anyTimes();
1362 //        expect(mockTopology.isConsistent(5L, (short)1, 10L, (short)1)).
1363 //        andReturn(false).anyTimes();
1364 //        expect(mockTopology.isConsistent(10L, (short)1, 1L, (short)1)).
1365 //        andReturn(false).anyTimes();
1366 //        expect(mockTopology.isConsistent(5L, (short)1, 1L, (short)1)).
1367 //        andReturn(false).anyTimes();
1368 //        expect(mockTopology.isConsistent(10L, (short)1, 5L, (short)1)).
1369 //        andReturn(false).anyTimes();
1370 //
1371 //        Date topologyUpdateTime = new Date();
1372 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
1373 //        anyTimes();
1374 //
1375 //
1376 //        replay(mockTopology);
1377 //        deviceManager.topology = mockTopology;
1378 //
1379 //        Entity entity1 = new Entity(1L, null, null, 1L, 1, c.getTime());
1380 //        Entity entity1a = new Entity(1L, null, 1, 1L, 1, c.getTime());
1381 //        Entity entity2 = new Entity(1L, null, null, 5L, 1, c.getTime());
1382 //        Entity entity3 = new Entity(1L, null, null, 10L, 1, c.getTime());
1383 //        entity1.setLastSeenTimestamp(c.getTime());
1384 //        c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT/2);
1385 //        entity1a.setLastSeenTimestamp(c.getTime());
1386 //        c.add(Calendar.MILLISECOND, 1);
1387 //        entity2.setLastSeenTimestamp(c.getTime());
1388 //        c.add(Calendar.MILLISECOND, 1);
1389 //        entity3.setLastSeenTimestamp(c.getTime());
1390 //
1391 //
1392 //
1393 //        IDevice d;
1394 //        d = deviceManager.learnDeviceByEntity(entity1);
1395 //        d = deviceManager.learnDeviceByEntity(entity1a);
1396 //        d = deviceManager.learnDeviceByEntity(entity2);
1397 //        d = deviceManager.learnDeviceByEntity(entity3);
1398 //
1399 //        // all entities are active, so entity3 should win
1400 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1) },
1401 //                          d.getAttachmentPoints());
1402 //
1403 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1),},
1404 //                              d.getAttachmentPoints(true));
1405 //
1406 //        c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT/4);
1407 //        entity1.setLastSeenTimestamp(c.getTime());
1408 //        d = deviceManager.learnDeviceByEntity(entity1);
1409 //
1410 //        // all are still active; entity3 should still win
1411 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
1412 //                          d.getAttachmentPoints());
1413 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1414 //                                             new SwitchPort(5L, 1,
1415 //                                                            ErrorStatus.DUPLICATE_DEVICE),
1416 //                                                            new SwitchPort(10L, 1,
1417 //                                                                           ErrorStatus.DUPLICATE_DEVICE) },
1418 //                                                                           d.getAttachmentPoints(true));
1419 //
1420 //        c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT+2000);
1421 //        entity1.setLastSeenTimestamp(c.getTime());
1422 //        d = deviceManager.learnDeviceByEntity(entity1);
1423 //
1424 //        assertEquals(entity1.getActiveSince(), entity1.getLastSeenTimestamp());
1425 //        // entity1 should now be the only active entity
1426 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
1427 //                          d.getAttachmentPoints());
1428 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
1429 //                          d.getAttachmentPoints(true));
1430 //    }
1431 //
1432 //
1433 //    @Test
1434 //    public void testAttachmentPointFlappingTwoCluster() throws Exception {
1435 //        Calendar c = Calendar.getInstance();
1436 //
1437 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1438 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1439 //                                                  anyShort())).andReturn(true).anyTimes();
1440 //        expect(mockTopology.isBroadcastDomainPort(anyLong(),
1441 //                                                  anyShort())).
1442 //                                                  andReturn(false).anyTimes();
1443 //        expect(mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
1444 //                                                    anyLong(), anyShort())).andReturn(false).anyTimes();
1445 //        expect(mockTopology.getL2DomainId(1L)).
1446 //        andReturn(1L).anyTimes();
1447 //        expect(mockTopology.getL2DomainId(5L)).
1448 //        andReturn(5L).anyTimes();
1449 //        expect(mockTopology.isConsistent(1L, (short)1, 1L, (short)2)).
1450 //        andReturn(false).anyTimes();
1451 //        expect(mockTopology.isConsistent(1L, (short)2, 5L, (short)1)).
1452 //        andReturn(false).anyTimes();
1453 //        expect(mockTopology.isConsistent(5L, (short)1, 5L, (short)2)).
1454 //        andReturn(false).anyTimes();
1455 //        expect(mockTopology.isConsistent(1L, (short)2, 1L, (short)1)).
1456 //        andReturn(false).anyTimes();
1457 //        expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)1)).
1458 //        andReturn(false).anyTimes();
1459 //        expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)2)).
1460 //        andReturn(false).anyTimes();
1461 //        expect(mockTopology.isConsistent(5L, (short)2, 5L, (short)1)).
1462 //        andReturn(false).anyTimes();
1463 //
1464 //        Date topologyUpdateTime = new Date();
1465 //        expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
1466 //        anyTimes();
1467 //
1468 //        replay(mockTopology);
1469 //        deviceManager.topology = mockTopology;
1470 //
1471 //        Entity entity1 = new Entity(1L, null, null, 1L, 1, c.getTime());
1472 //        Entity entity2 = new Entity(1L, null, null, 1L, 2, c.getTime());
1473 //        Entity entity3 = new Entity(1L, null, null, 5L, 1, c.getTime());
1474 //        Entity entity4 = new Entity(1L, null, null, 5L, 2, c.getTime());
1475 //        entity1.setLastSeenTimestamp(c.getTime());
1476 //        c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT/2);
1477 //        c.add(Calendar.MILLISECOND, 1);
1478 //        entity2.setLastSeenTimestamp(c.getTime());
1479 //        c.add(Calendar.MILLISECOND, 1);
1480 //        entity3.setLastSeenTimestamp(c.getTime());
1481 //        c.add(Calendar.MILLISECOND, 1);
1482 //        entity4.setLastSeenTimestamp(c.getTime());
1483 //
1484 //        deviceManager.learnDeviceByEntity(entity1);
1485 //        deviceManager.learnDeviceByEntity(entity2);
1486 //        deviceManager.learnDeviceByEntity(entity3);
1487 //        IDevice d = deviceManager.learnDeviceByEntity(entity4);
1488 //
1489 //        // all entities are active, so entities 2,4 should win
1490 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 2),
1491 //                                             new SwitchPort(5L, 2) },
1492 //                                             d.getAttachmentPoints());
1493 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 2),
1494 //                                             new SwitchPort(5L, 2)},
1495 //                                             d.getAttachmentPoints(true));
1496 //
1497 //        c.add(Calendar.MILLISECOND, 1);
1498 //        entity1.setLastSeenTimestamp(c.getTime());
1499 //        d = deviceManager.learnDeviceByEntity(entity1);
1500 //
1501 //        // all entities are active, so entities 2,4 should win
1502 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1503 //                                             new SwitchPort(5L, 2) },
1504 //                                             d.getAttachmentPoints());
1505 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1506 //                                             new SwitchPort(5L, 2),
1507 //                                             new SwitchPort(1L, 2, ErrorStatus.DUPLICATE_DEVICE)},
1508 //                                             d.getAttachmentPoints(true));
1509 //
1510 //        c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT+1);
1511 //        entity1.setLastSeenTimestamp(c.getTime());
1512 //        d = deviceManager.learnDeviceByEntity(entity1);
1513 //
1514 //        // entities 3,4 are still in conflict, but 1 should be resolved
1515 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1516 //                                             new SwitchPort(5L, 2) },
1517 //                                             d.getAttachmentPoints());
1518 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1519 //                                             new SwitchPort(5L, 2)},
1520 //                                             d.getAttachmentPoints(true));
1521 //
1522 //        entity3.setLastSeenTimestamp(c.getTime());
1523 //        d = deviceManager.learnDeviceByEntity(entity3);
1524 //
1525 //        // no conflicts, 1 and 3 will win
1526 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1527 //                                             new SwitchPort(5L, 1) },
1528 //                                             d.getAttachmentPoints());
1529 //        assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
1530 //                                             new SwitchPort(5L, 1) },
1531 //                                             d.getAttachmentPoints(true));
1532 //
1533 //    }
1534 //
1535 //    protected void doTestDeviceQuery() throws Exception {
1536 //        Entity entity1 = new Entity(1L, (short)1, 1, 1L, 1, new Date());
1537 //        Entity entity2 = new Entity(2L, (short)2, 2, 1L, 2, new Date());
1538 //        Entity entity3 = new Entity(3L, (short)3, 3, 5L, 1, new Date());
1539 //        Entity entity4 = new Entity(4L, (short)4, 3, 5L, 2, new Date());
1540 //        Entity entity5 = new Entity(1L, (short)4, 3, 5L, 2, new Date());
1541 //
1542 //        Device d1 = deviceManager.learnDeviceByEntity(entity1);
1543 //        deviceManager.learnDeviceByEntity(entity2);
1544 //        Device d3 = deviceManager.learnDeviceByEntity(entity3);
1545 //        Device d4 = deviceManager.learnDeviceByEntity(entity4);
1546 //
1547 //        IDevice d;
1548 //
1549 //        Iterator<? extends IDevice> iter =
1550 //                deviceManager.queryDevices(null, (short)1, 1, null, null);
1551 //        int count = 0;
1552 //        while (iter.hasNext()) {
1553 //            count += 1;
1554 //            d = iter.next();
1555 //            assertEquals(d1.getDeviceKey(), d.getDeviceKey());
1556 //        }
1557 //        assertEquals(1, count);
1558 //
1559 //        iter = deviceManager.queryDevices(null, (short)3, 3, null, null);
1560 //        count = 0;
1561 //        while (iter.hasNext()) {
1562 //            count += 1;
1563 //            d = iter.next();
1564 //            assertEquals(d3.getDeviceKey(), d.getDeviceKey());
1565 //        }
1566 //        assertEquals(1, count);
1567 //
1568 //        iter = deviceManager.queryDevices(null, (short)1, 3, null, null);
1569 //        count = 0;
1570 //        while (iter.hasNext()) {
1571 //            count += 1;
1572 //            iter.next();
1573 //        }
1574 //        assertEquals(0, count);
1575 //
1576 //        Device d5 = deviceManager.learnDeviceByEntity(entity5);
1577 //        iter = deviceManager.queryDevices(null, (short)4, 3, null, null);
1578 //        count = 0;
1579 //        Set<Long> deviceKeysFromIterator = new HashSet<Long>();
1580 //        while (iter.hasNext()) {
1581 //            count += 1;
1582 //            d = iter.next();
1583 //            deviceKeysFromIterator.add(d.getDeviceKey());
1584 //        }
1585 //        Set<Long> expectedDeviceKeys = new HashSet<Long>();
1586 //        expectedDeviceKeys.add(d4.getDeviceKey());
1587 //        expectedDeviceKeys.add(d5.getDeviceKey());
1588 //        assertEquals(expectedDeviceKeys, deviceKeysFromIterator);
1589 //        assertEquals(2, count);
1590 //
1591 //
1592 //        iter = deviceManager.queryDevices(1L, null, null, null, null);
1593 //        count = 0;
1594 //        deviceKeysFromIterator = new HashSet<Long>();
1595 //        while (iter.hasNext()) {
1596 //            count += 1;
1597 //            d = iter.next();
1598 //            deviceKeysFromIterator.add(d.getDeviceKey());
1599 //        }
1600 //        expectedDeviceKeys = new HashSet<Long>();
1601 //        expectedDeviceKeys.add(d1.getDeviceKey());
1602 //        expectedDeviceKeys.add(d5.getDeviceKey());
1603 //        assertEquals(expectedDeviceKeys, deviceKeysFromIterator);
1604 //        assertEquals(2, count);
1605 //    }
1606 //
1607 //    @Test
1608 //    public void testDeviceIndex() throws Exception {
1609 //        EnumSet<IDeviceService.DeviceField> indexFields =
1610 //                EnumSet.noneOf(IDeviceService.DeviceField.class);
1611 //        indexFields.add(IDeviceService.DeviceField.IPV4);
1612 //        indexFields.add(IDeviceService.DeviceField.VLAN);
1613 //        deviceManager.addIndex(false, indexFields);
1614 //
1615 //        indexFields = EnumSet.noneOf(IDeviceService.DeviceField.class);
1616 //        deviceManager.addIndex(false, indexFields);
1617 //
1618 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1619 //        deviceManager.topology = mockTopology;
1620 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1621 //                                                  anyShort())).
1622 //                                                  andReturn(true).anyTimes();
1623 //        expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
1624 //        replay(mockTopology);
1625 //        doTestDeviceQuery();
1626 //    }
1627 //
1628 //    @Test
1629 //    public void testDeviceQuery() throws Exception {
1630 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1631 //        deviceManager.topology = mockTopology;
1632 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1633 //                                                  anyShort())).
1634 //                                                  andReturn(true).anyTimes();
1635 //        expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
1636 //        replay(mockTopology);
1637 //
1638 //        doTestDeviceQuery();
1639 //    }
1640 //
1641 //    protected void doTestDeviceClassQuery() throws Exception {
1642 //        Entity entity1 = new Entity(1L, (short)1, 1, 1L, 1, new Date());
1643 //        Entity entity2 = new Entity(2L, (short)2, 2, 1L, 2, new Date());
1644 //        Entity entity3 = new Entity(3L, (short)3, 3, 5L, 1, new Date());
1645 //        Entity entity4 = new Entity(4L, (short)4, 3, 5L, 2, new Date());
1646 //        Entity entity5 = new Entity(1L, (short)4, 3, 5L, 2, new Date());
1647 //
1648 //        IDevice d1 = deviceManager.learnDeviceByEntity(entity1);
1649 //        IDevice d2 = deviceManager.learnDeviceByEntity(entity2);
1650 //        IDevice d3 = deviceManager.learnDeviceByEntity(entity3);
1651 //        IDevice d4 = deviceManager.learnDeviceByEntity(entity4);
1652 //        assertEquals(d1.getEntityClass(), d2.getEntityClass());
1653 //        assertEquals(d1.getEntityClass(), d3.getEntityClass());
1654 //        assertEquals(d1.getEntityClass(), d4.getEntityClass());
1655 //
1656 //        IDevice d;
1657 //
1658 //        Iterator<? extends IDevice> iter =
1659 //                deviceManager.queryClassDevices(d1.getEntityClass(), null,
1660 //                                                (short)1, 1, null, null);
1661 //        int count = 0;
1662 //        while (iter.hasNext()) {
1663 //            count += 1;
1664 //            d = iter.next();
1665 //            assertEquals(d1.getDeviceKey(), d.getDeviceKey());
1666 //        }
1667 //        assertEquals(1, count);
1668 //
1669 //        iter = deviceManager.queryClassDevices(d1.getEntityClass(), null,
1670 //                                               (short)3, 3, null, null);
1671 //        count = 0;
1672 //        while (iter.hasNext()) {
1673 //            count += 1;
1674 //            d = iter.next();
1675 //            assertEquals(d3.getDeviceKey(), d.getDeviceKey());
1676 //
1677 //        }
1678 //        assertEquals(1, count);
1679 //
1680 //        iter = deviceManager.queryClassDevices(d1.getEntityClass(), null,
1681 //                                               (short)1, 3, null, null);
1682 //        count = 0;
1683 //        while (iter.hasNext()) {
1684 //            count += 1;
1685 //            iter.next();
1686 //        }
1687 //        assertEquals(0, count);
1688 //
1689 //        IDevice d5 = deviceManager.learnDeviceByEntity(entity5);
1690 //        assertEquals(d1.getEntityClass(), d5.getEntityClass());
1691 //        iter = deviceManager.queryClassDevices(d1.getEntityClass(), null,
1692 //                                               (short)4, 3, null, null);
1693 //        count = 0;
1694 //        Set<Long> deviceKeysFromIterator = new HashSet<Long>();
1695 //        while (iter.hasNext()) {
1696 //            count += 1;
1697 //            d = iter.next();
1698 //            deviceKeysFromIterator.add(d.getDeviceKey());
1699 //        }
1700 //        Set<Long> expectedDeviceKeys = new HashSet<Long>();
1701 //        expectedDeviceKeys.add(d4.getDeviceKey());
1702 //        expectedDeviceKeys.add(d5.getDeviceKey());
1703 //        assertEquals(expectedDeviceKeys, deviceKeysFromIterator);
1704 //        assertEquals(2, count);
1705 //    }
1706 //
1707 //    @Test
1708 //    public void testDeviceClassIndex() throws Exception {
1709 //        EnumSet<IDeviceService.DeviceField> indexFields =
1710 //                EnumSet.noneOf(IDeviceService.DeviceField.class);
1711 //        indexFields.add(IDeviceService.DeviceField.IPV4);
1712 //        indexFields.add(IDeviceService.DeviceField.VLAN);
1713 //        deviceManager.addIndex(true, indexFields);
1714 //
1715 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1716 //        deviceManager.topology = mockTopology;
1717 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1718 //                                                  anyShort())).
1719 //                                                  andReturn(true).anyTimes();
1720 //        expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
1721 //        replay(mockTopology);
1722 //
1723 //        doTestDeviceClassQuery();
1724 //    }
1725 //
1726 //    @Test
1727 //    public void testDeviceClassQuery() throws Exception {
1728 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1729 //        deviceManager.topology = mockTopology;
1730 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1731 //                                                  anyShort())).
1732 //                                                  andReturn(true).anyTimes();
1733 //        expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
1734 //        replay(mockTopology);
1735 //
1736 //        doTestDeviceClassQuery();
1737 //    }
1738 //
1739 //    @Test
1740 //    public void testFindDevice() throws FloodlightModuleException {
1741 //        boolean exceptionCaught;
1742 //        deviceManager.entityClassifier= new MockEntityClassifierMac();
1743 //        deviceManager.startUp(null);
1744 //
1745 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1746 //        deviceManager.topology = mockTopology;
1747 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1748 //                                                  anyShort())).
1749 //                                                  andReturn(true).anyTimes();
1750 //        expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
1751 //        replay(mockTopology);
1752 //
1753 //        Entity entity1 = new Entity(1L, (short)1, 1, 1L, 1, new Date());
1754 //        Entity entity2 = new Entity(2L, (short)2, 2, 1L, 2, new Date());
1755 //        Entity entity2b = new Entity(22L, (short)2, 2, 1L, 2, new Date());
1756 //
1757 //        Entity entity3 = new Entity(3L, (short)1, 3, 2L, 1, new Date());
1758 //        Entity entity4 = new Entity(4L, (short)2, 4, 2L, 2, new Date());
1759 //
1760 //        Entity entity5 = new Entity(5L, (short)1, 5, 3L, 1, new Date());
1761 //
1762 //
1763 //        IDevice d1 = deviceManager.learnDeviceByEntity(entity1);
1764 //        IDevice d2 = deviceManager.learnDeviceByEntity(entity2);
1765 //        IDevice d3 = deviceManager.learnDeviceByEntity(entity3);
1766 //        IDevice d4 = deviceManager.learnDeviceByEntity(entity4);
1767 //        IDevice d5 = deviceManager.learnDeviceByEntity(entity5);
1768 //
1769 //        // Make sure the entity classifier worked as expected
1770 //        assertEquals(MockEntityClassifierMac.testECMac1, d1.getEntityClass());
1771 //        assertEquals(MockEntityClassifierMac.testECMac1, d2.getEntityClass());
1772 //        assertEquals(MockEntityClassifierMac.testECMac2, d3.getEntityClass());
1773 //        assertEquals(MockEntityClassifierMac.testECMac2, d4.getEntityClass());
1774 //        assertEquals(DefaultEntityClassifier.entityClass,
1775 //                     d5.getEntityClass());
1776 //
1777 //        // Look up the device using findDevice() which uses only the primary
1778 //        // index
1779 //        assertEquals(d1, deviceManager.findDevice(entity1.getMacAddress(),
1780 //                                                  entity1.getVlan(),
1781 //                                                  entity1.getIpv4Address(),
1782 //                                                  entity1.getSwitchDPID(),
1783 //                                                  entity1.getSwitchPort()));
1784 //        // port changed. Device will be found through class index
1785 //        assertEquals(d1, deviceManager.findDevice(entity1.getMacAddress(),
1786 //                                                  entity1.getVlan(),
1787 //                                                  entity1.getIpv4Address(),
1788 //                                                  entity1.getSwitchDPID(),
1789 //                                                  entity1.getSwitchPort()+1));
1790 //        // VLAN changed. No device matches
1791 //        assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(),
1792 //                                                  (short)42,
1793 //                                                  entity1.getIpv4Address(),
1794 //                                                  entity1.getSwitchDPID(),
1795 //                                                  entity1.getSwitchPort()));
1796 //        assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(),
1797 //                                                  null,
1798 //                                                  entity1.getIpv4Address(),
1799 //                                                  entity1.getSwitchDPID(),
1800 //                                                  entity1.getSwitchPort()));
1801 //        assertEquals(d2, deviceManager.findDeviceByEntity(entity2));
1802 //        assertEquals(null, deviceManager.findDeviceByEntity(entity2b));
1803 //        assertEquals(d3, deviceManager.findDevice(entity3.getMacAddress(),
1804 //                                                  entity3.getVlan(),
1805 //                                                  entity3.getIpv4Address(),
1806 //                                                  entity3.getSwitchDPID(),
1807 //                                                  entity3.getSwitchPort()));
1808 //        // switch and port not set. throws exception
1809 //        exceptionCaught = false;
1810 //        try {
1811 //            assertEquals(null, deviceManager.findDevice(entity3.getMacAddress(),
1812 //                                                        entity3.getVlan(),
1813 //                                                        entity3.getIpv4Address(),
1814 //                                                        null,
1815 //                                                        null));
1816 //        }
1817 //        catch (IllegalArgumentException e) {
1818 //            exceptionCaught = true;
1819 //        }
1820 //        if (!exceptionCaught)
1821 //            fail("findDevice() did not throw IllegalArgumentException");
1822 //        assertEquals(d4, deviceManager.findDeviceByEntity(entity4));
1823 //        assertEquals(d5, deviceManager.findDevice(entity5.getMacAddress(),
1824 //                                                  entity5.getVlan(),
1825 //                                                  entity5.getIpv4Address(),
1826 //                                                  entity5.getSwitchDPID(),
1827 //                                                  entity5.getSwitchPort()));
1828 //        // switch and port not set. throws exception (swith/port are key
1829 //        // fields of IEntityClassifier but not d5.entityClass
1830 //        exceptionCaught = false;
1831 //        try {
1832 //            assertEquals(d5, deviceManager.findDevice(entity5.getMacAddress(),
1833 //                                                      entity5.getVlan(),
1834 //                                                      entity5.getIpv4Address(),
1835 //                                                      null,
1836 //                                                      null));
1837 //        }
1838 //        catch (IllegalArgumentException e) {
1839 //            exceptionCaught = true;
1840 //        }
1841 //        if (!exceptionCaught)
1842 //            fail("findDevice() did not throw IllegalArgumentException");
1843 //
1844 //
1845 //        Entity entityNoClass = new Entity(5L, (short)1, 5, -1L, 1, new Date());
1846 //        assertEquals(null, deviceManager.findDeviceByEntity(entityNoClass));
1847 //
1848 //
1849 //        // Now look up destination devices
1850 //        assertEquals(d1, deviceManager.findClassDevice(d2.getEntityClass(),
1851 //                                                  entity1.getMacAddress(),
1852 //                                                  entity1.getVlan(),
1853 //                                                  entity1.getIpv4Address()));
1854 //        assertEquals(d1, deviceManager.findClassDevice(d2.getEntityClass(),
1855 //                                                  entity1.getMacAddress(),
1856 //                                                  entity1.getVlan(),
1857 //                                                  null));
1858 //        assertEquals(null, deviceManager.findClassDevice(d2.getEntityClass(),
1859 //                                                  entity1.getMacAddress(),
1860 //                                                  (short) -1,
1861 //                                                  0));
1862 //    }
1863 //
1864 //
1865 //
1866 //    @Test
1867 //    public void testGetIPv4Addresses() {
1868 //        // Looks like Date is only 1s granularity
1869 //
1870 //        ITopologyService mockTopology = createMock(ITopologyService.class);
1871 //        deviceManager.topology = mockTopology;
1872 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
1873 //                                                  anyShort())).
1874 //                                                  andReturn(true).anyTimes();
1875 //        expect(mockTopology.getL2DomainId(anyLong())).andReturn(1L).anyTimes();
1876 //        expect(mockTopology.isConsistent(EasyMock.anyLong(),
1877 //                                         EasyMock.anyShort(),
1878 //                                         EasyMock.anyLong(),
1879 //                                         EasyMock.anyShort()))
1880 //                                         .andReturn(false)
1881 //                                         .anyTimes();
1882 //        expect(mockTopology.isBroadcastDomainPort(EasyMock.anyLong(),
1883 //                                                  EasyMock.anyShort()))
1884 //                                                  .andReturn(false)
1885 //                                                  .anyTimes();
1886 //        expect(mockTopology.isInSameBroadcastDomain(EasyMock.anyLong(),
1887 //                                                    EasyMock.anyShort(),
1888 //                                                    EasyMock.anyLong(),
1889 //                                                    EasyMock.anyShort())).
1890 //                                                    andReturn(false).anyTimes();
1891 //        replay(mockTopology);
1892 //
1893 //        Entity e1 = new Entity(1L, (short)1, null, null, null, new Date(2000));
1894 //        Device d1 = deviceManager.learnDeviceByEntity(e1);
1895 //        assertArrayEquals(new Integer[0], d1.getIPv4Addresses());
1896 //
1897 //
1898 //        Entity e2 = new Entity(2L, (short)2, 2, null, null, new Date(2000));
1899 //        Device d2 = deviceManager.learnDeviceByEntity(e2);
1900 //        d2 = deviceManager.learnDeviceByEntity(e2);
1901 //        assertArrayEquals(new Integer[] { 2 }, d2.getIPv4Addresses());
1902 //        // More than one entity
1903 //        Entity e2b = new Entity(2L, (short)2, null, 2L, 2, new Date(3000));
1904 //        d2 = deviceManager.learnDeviceByEntity(e2b);
1905 //        assertEquals(2, d2.entities.length);
1906 //        assertArrayEquals(new Integer[] { 2 }, d2.getIPv4Addresses());
1907 //        // and now add an entity with an IP
1908 //        Entity e2c = new Entity(2L, (short)2, 2, 2L, 3, new Date(3000));
1909 //        d2 = deviceManager.learnDeviceByEntity(e2c);
1910 //        assertArrayEquals(new Integer[] { 2 }, d2.getIPv4Addresses());
1911 //        assertEquals(3, d2.entities.length);
1912 //
1913 //        // Other devices with different IPs shouldn't interfere
1914 //        Entity e3 = new Entity(3L, (short)3, 3, null, null, new Date(4000));
1915 //        Entity e3b = new Entity(3L, (short)3, 3, 3L, 3, new Date(4400));
1916 //        Device d3 = deviceManager.learnDeviceByEntity(e3);
1917 //        d3 = deviceManager.learnDeviceByEntity(e3b);
1918 //        assertArrayEquals(new Integer[] { 2 }, d2.getIPv4Addresses());
1919 //        assertArrayEquals(new Integer[] { 3 }, d3.getIPv4Addresses());
1920 //
1921 //        // Add another IP to d3
1922 //        Entity e3c = new Entity(3L, (short)3, 33, 3L, 3, new Date(4400));
1923 //        d3 = deviceManager.learnDeviceByEntity(e3c);
1924 //        Integer[] ips = d3.getIPv4Addresses();
1925 //        Arrays.sort(ips);
1926 //        assertArrayEquals(new Integer[] { 3, 33 }, ips);
1927 //
1928 //        // Add another device that also claims IP2 but is older than e2
1929 //        Entity e4 = new Entity(4L, (short)4, 2, null, null, new Date(1000));
1930 //        Entity e4b = new Entity(4L, (short)4, null, 4L, 4, new Date(1000));
1931 //        Device d4 = deviceManager.learnDeviceByEntity(e4);
1932 //        assertArrayEquals(new Integer[] { 2 }, d2.getIPv4Addresses());
1933 //        assertArrayEquals(new Integer[0],  d4.getIPv4Addresses());
1934 //        // add another entity to d4
1935 //        d4 = deviceManager.learnDeviceByEntity(e4b);
1936 //        assertArrayEquals(new Integer[0], d4.getIPv4Addresses());
1937 //
1938 //        // Make e4 and e4a newer
1939 //        Entity e4c = new Entity(4L, (short)4, 2, null, null, new Date(5000));
1940 //        Entity e4d = new Entity(4L, (short)4, null, 4L, 5, new Date(5000));
1941 //        d4 = deviceManager.learnDeviceByEntity(e4c);
1942 //        d4 = deviceManager.learnDeviceByEntity(e4d);
1943 //        assertArrayEquals(new Integer[0], d2.getIPv4Addresses());
1944 //        // FIXME: d4 should not return IP4
1945 //        assertArrayEquals(new Integer[] { 2 }, d4.getIPv4Addresses());
1946 //
1947 //        // Add another newer entity to d2 but with different IP
1948 //        Entity e2d = new Entity(2L, (short)2, 22, 4L, 6, new Date(6000));
1949 //        d2 = deviceManager.learnDeviceByEntity(e2d);
1950 //        assertArrayEquals(new Integer[] { 22 }, d2.getIPv4Addresses());
1951 //        assertArrayEquals(new Integer[] { 2 }, d4.getIPv4Addresses());
1952 //
1953 //        // new IP for d2,d4 but with same timestamp. Both devices get the IP
1954 //        Entity e2e = new Entity(2L, (short)2, 42, 2L, 4, new Date(7000));
1955 //        d2 = deviceManager.learnDeviceByEntity(e2e);
1956 //        ips= d2.getIPv4Addresses();
1957 //        Arrays.sort(ips);
1958 //        assertArrayEquals(new Integer[] { 22, 42 }, ips);
1959 //        Entity e4e = new Entity(4L, (short)4, 42, 4L, 7, new Date(7000));
1960 //        d4 = deviceManager.learnDeviceByEntity(e4e);
1961 //        ips= d4.getIPv4Addresses();
1962 //        Arrays.sort(ips);
1963 //        assertArrayEquals(new Integer[] { 2, 42 }, ips);
1964 //
1965 //        // add a couple more IPs
1966 //        Entity e2f = new Entity(2L, (short)2, 4242, 2L, 5, new Date(8000));
1967 //        d2 = deviceManager.learnDeviceByEntity(e2f);
1968 //        ips= d2.getIPv4Addresses();
1969 //        Arrays.sort(ips);
1970 //        assertArrayEquals(new Integer[] { 22, 42, 4242 }, ips);
1971 //        Entity e4f = new Entity(4L, (short)4, 4242, 4L, 8, new Date(9000));
1972 //        d4 = deviceManager.learnDeviceByEntity(e4f);
1973 //        ips= d4.getIPv4Addresses();
1974 //        Arrays.sort(ips);
1975 //        assertArrayEquals(new Integer[] { 2, 42, 4242 }, ips);
1976 //    }
1977 //
1978 //    // TODO: this test should really go into a separate class that collects
1979 //    // unit tests for Device
1980 //    @Test
1981 //    public void testGetSwitchPortVlanId() {
1982 //            Entity entity1 = new Entity(1L, (short)1, null, 10L, 1, new Date());
1983 //            Entity entity2 = new Entity(1L, null, null, 10L, 1, new Date());
1984 //            Entity entity3 = new Entity(1L, (short)3, null,  1L, 1, new Date());
1985 //            Entity entity4 = new Entity(1L, (short)42, null,  1L, 1, new Date());
1986 //            Entity[] entities = new Entity[] { entity1, entity2,
1987 //                                               entity3, entity4
1988 //                                             };
1989 //            Device d = new Device(null,1L, null, null, null,
1990 //                                  Arrays.asList(entities), null);
1991 //            SwitchPort swp1x1 = new SwitchPort(1L, 1);
1992 //            SwitchPort swp1x2 = new SwitchPort(1L, 2);
1993 //            SwitchPort swp2x1 = new SwitchPort(2L, 1);
1994 //            SwitchPort swp10x1 = new SwitchPort(10L, 1);
1995 //            assertArrayEquals(new Short[] { -1, 1},
1996 //                              d.getSwitchPortVlanIds(swp10x1));
1997 //            assertArrayEquals(new Short[] { 3, 42},
1998 //                              d.getSwitchPortVlanIds(swp1x1));
1999 //            assertArrayEquals(new Short[0],
2000 //                              d.getSwitchPortVlanIds(swp1x2));
2001 //            assertArrayEquals(new Short[0],
2002 //                              d.getSwitchPortVlanIds(swp2x1));
2003 //    }
2004 //
2005 //    @Test
2006 //    public void testReclassifyDevice() throws FloodlightModuleException {
2007 //        MockFlexEntityClassifier flexClassifier =
2008 //                new MockFlexEntityClassifier();
2009 //        deviceManager.entityClassifier= flexClassifier;
2010 //        deviceManager.startUp(null);
2011 //
2012 //        ITopologyService mockTopology = createMock(ITopologyService.class);
2013 //        deviceManager.topology = mockTopology;
2014 //        expect(mockTopology.isAttachmentPointPort(anyLong(),
2015 //                                                  anyShort())).
2016 //                                                  andReturn(true).anyTimes();
2017 //        expect(mockTopology.getL2DomainId(anyLong())).andReturn(1L).anyTimes();
2018 //        expect(mockTopology.isConsistent(EasyMock.anyLong(),
2019 //                                         EasyMock.anyShort(),
2020 //                                         EasyMock.anyLong(),
2021 //                                         EasyMock.anyShort()))
2022 //                                         .andReturn(false)
2023 //                                         .anyTimes();
2024 //        expect(mockTopology.isBroadcastDomainPort(EasyMock.anyLong(),
2025 //                                                  EasyMock.anyShort()))
2026 //                                                  .andReturn(false)
2027 //                                                  .anyTimes();
2028 //        replay(mockTopology);
2029 //
2030 //        //flexClassifier.createTestEntityClass("Class1");
2031 //
2032 //        Entity entity1 = new Entity(1L, (short)1, 1, 1L, 1, new Date());
2033 //        Entity entity1b = new Entity(1L, (short)2, 1, 1L, 1, new Date());
2034 //        Entity entity2 = new Entity(2L, (short)1, 2, 2L, 2, new Date());
2035 //        Entity entity2b = new Entity(2L, (short)2, 2, 2L, 2, new Date());
2036 //
2037 //
2038 //        Device d1 = deviceManager.learnDeviceByEntity(entity1);
2039 //        Device d2 = deviceManager.learnDeviceByEntity(entity2);
2040 //        Device d1b = deviceManager.learnDeviceByEntity(entity1b);
2041 //        Device d2b = deviceManager.learnDeviceByEntity(entity2b);
2042 //
2043 //        d1 = deviceManager.getDeviceIteratorForQuery(entity1.getMacAddress(),
2044 //                        entity1.getVlan(), entity1.getIpv4Address(),
2045 //                        entity1.getSwitchDPID(), entity1.getSwitchPort())
2046 //                        .next();
2047 //        d1b = deviceManager.getDeviceIteratorForQuery(entity1b.getMacAddress(),
2048 //                entity1b.getVlan(), entity1b.getIpv4Address(),
2049 //                entity1b.getSwitchDPID(), entity1b.getSwitchPort()).next();
2050 //
2051 //        assertEquals(d1, d1b);
2052 //
2053 //        d2 = deviceManager.getDeviceIteratorForQuery(entity2.getMacAddress(),
2054 //                entity2.getVlan(), entity2.getIpv4Address(),
2055 //                entity2.getSwitchDPID(), entity2.getSwitchPort()).next();
2056 //        d2b = deviceManager.getDeviceIteratorForQuery(entity2b.getMacAddress(),
2057 //                entity2b.getVlan(), entity2b.getIpv4Address(),
2058 //                entity2b.getSwitchDPID(), entity2b.getSwitchPort()).next();
2059 //        assertEquals(d2, d2b);
2060 //
2061 //        IEntityClass eC1 = flexClassifier.createTestEntityClass("C1");
2062 //        IEntityClass eC2 = flexClassifier.createTestEntityClass("C2");
2063 //
2064 //        flexClassifier.addVlanEntities((short)1, eC1);
2065 //        flexClassifier.addVlanEntities((short)2, eC1);
2066 //
2067 //        deviceManager.reclassifyDevice(d1);
2068 //        deviceManager.reclassifyDevice(d2);
2069 //
2070 //        d1 = deviceManager.deviceMap.get(
2071 //                deviceManager.primaryIndex.findByEntity(entity1));
2072 //        d1b = deviceManager.deviceMap.get(
2073 //                deviceManager.primaryIndex.findByEntity(entity1b));
2074 //
2075 //        assertEquals(d1, d1b);
2076 //
2077 //        d2 = deviceManager.deviceMap.get(
2078 //                deviceManager.primaryIndex.findByEntity(entity2));
2079 //        d2b = deviceManager.deviceMap.get(
2080 //                deviceManager.primaryIndex.findByEntity(entity2b));
2081 //
2082 //        assertEquals(d2, d2b);
2083 //
2084 //        flexClassifier.addVlanEntities((short)1, eC2);
2085 //
2086 //        deviceManager.reclassifyDevice(d1);
2087 //        deviceManager.reclassifyDevice(d2);
2088 //        d1 = deviceManager.deviceMap.get(
2089 //                deviceManager.primaryIndex.findByEntity(entity1));
2090 //        d1b = deviceManager.deviceMap.get(
2091 //                deviceManager.primaryIndex.findByEntity(entity1b));
2092 //        d2 = deviceManager.deviceMap.get(
2093 //                deviceManager.primaryIndex.findByEntity(entity2));
2094 //        d2b = deviceManager.deviceMap.get(
2095 //                deviceManager.primaryIndex.findByEntity(entity2b));
2096 //
2097 //        assertNotSame(d1, d1b);
2098 //
2099 //        assertNotSame(d2, d2b);
2100 //
2101 //        flexClassifier.addVlanEntities((short)1, eC1);
2102 //        deviceManager.reclassifyDevice(d1);
2103 //        deviceManager.reclassifyDevice(d2);
2104 //        ClassState classState = deviceManager.classStateMap.get(eC1.getName());
2105 //
2106 //        Long deviceKey1 = null;
2107 //        Long deviceKey1b = null;
2108 //        Long deviceKey2 = null;
2109 //        Long deviceKey2b = null;
2110 //
2111 //        deviceKey1 =
2112 //                classState.classIndex.findByEntity(entity1);
2113 //        deviceKey1b =
2114 //                classState.classIndex.findByEntity(entity1b);
2115 //        deviceKey2 =
2116 //                classState.classIndex.findByEntity(entity2);
2117 //        deviceKey2b =
2118 //                classState.classIndex.findByEntity(entity2b);
2119 //
2120 //        assertEquals(deviceKey1, deviceKey1b);
2121 //
2122 //        assertEquals(deviceKey2, deviceKey2b);
2123 //    }
2124 //
2125 //    @Test
2126 //    public void testSyncEntity() {
2127 //        Date d1 = new Date();
2128 //        Date d2 = new Date(0);
2129 //        Entity e1 = new Entity(1L, (short)2, 3, 4L, 5, d1);
2130 //        e1.setActiveSince(d2);
2131 //        SyncEntity se1 = new SyncEntity(e1);
2132 //        assertEntityEquals(e1, se1);
2133 //        assertEquals(1L, se1.macAddress);
2134 //        assertEquals(Short.valueOf((short)2), se1.vlan);
2135 //        assertEquals(Integer.valueOf(3), se1.ipv4Address);
2136 //        assertEquals(Long.valueOf(4L), se1.switchDPID);
2137 //        assertEquals(Integer.valueOf(5), se1.switchPort);
2138 //        assertEquals(d1, se1.lastSeenTimestamp);
2139 //        assertEquals(d2, se1.activeSince);
2140 //        assertNotSame(d1, se1.lastSeenTimestamp);
2141 //        assertNotSame(d2, se1.activeSince);
2142 //
2143 //        Entity e2 = new Entity(42L, null, null, null, null, null);
2144 //        SyncEntity se2 = new SyncEntity(e2);
2145 //        assertEntityEquals(e2, se2);
2146 //
2147 //        SyncEntity se3 = new SyncEntity();
2148 //        SyncEntity se4 = new SyncEntity();
2149 //        se3.lastSeenTimestamp = new Date(1000);
2150 //        se4.lastSeenTimestamp = new Date(2000);
2151 //        assertTrue("", se3.compareTo(se4) < 0);
2152 //        assertTrue("", se4.compareTo(se3) > 0);
2153 //        se4.lastSeenTimestamp = new Date(1000);
2154 //        assertTrue("", se3.compareTo(se4) == 0);
2155 //        assertTrue("", se4.compareTo(se3) == 0);
2156 //        se4.lastSeenTimestamp = new Date(500);
2157 //        assertTrue("", se3.compareTo(se4) > 0);
2158 //        assertTrue("", se4.compareTo(se3) < 0);
2159 //    }
2160 //
2161 //    /* Test basic DeviceSyncRepresentation behavior */
2162 //    @Test
2163 //    public void testDeviceSyncRepresentationBasics() {
2164 //        DeviceSyncRepresentation dsr = new DeviceSyncRepresentation();
2165 //        assertNull(dsr.getKey());
2166 //        assertNull(dsr.getEntities());
2167 //        dsr.setKey("MyKey");
2168 //        assertEquals("MyKey", dsr.getKey());
2169 //        assertEquals("MyKey", dsr.toString());
2170 //
2171 //        List<SyncEntity> entities = new ArrayList<SyncEntity>();
2172 //        Entity e1a = new Entity(1L, (short)2, 3, 4L, 5, new Date(1000));
2173 //        Entity e1b = new Entity(1L, (short)2, null, 4L, 5, new Date(0));
2174 //        entities.add(new SyncEntity(e1a));
2175 //        entities.add(new SyncEntity(e1b));
2176 //        // e1b comes before e1 (lastSeen) but we add it after it to test
2177 //        // sorting
2178 //        dsr.setEntities(entities);
2179 //
2180 //        assertEquals(2, dsr.getEntities().size());
2181 //        // e1b has earlier time
2182 //        assertEquals(e1b, dsr.getEntities().get(0).asEntity());
2183 //        assertEquals(e1a, dsr.getEntities().get(1).asEntity());
2184 //
2185 //        dsr.setKey(null);
2186 //        dsr.setEntities(null);
2187 //        assertNull(dsr.getKey());
2188 //        assertNull(dsr.getEntities());
2189 //    }
2190 //
2191 //    @Test
2192 //    public void testDeviceSyncRepresentationFromDevice() {
2193 //        ITopologyService mockTopology = makeMockTopologyAllPortsAp();
2194 //        replay(mockTopology);
2195 //        deviceManager.topology = mockTopology;
2196 //
2197 //        deviceManager.entityClassifier = new MockEntityClassifier();
2198 //
2199 //        //**************************************
2200 //        // Test 1: a single entity
2201 //        Entity e1 = new Entity(1L, (short)2, 3, 4L, 5, new Date(1000));
2202 //        Device d1 = deviceManager.learnDeviceByEntity(e1);
2203 //        assertEquals("Sanity check failed. Device doesn't have the expected " +
2204 //                     "entity class. Something with the test setup is strange",
2205 //                     "DefaultEntityClass", d1.getEntityClass().getName());
2206 //        assertEquals("Sanity check failed. Device doesn't have the expected " +
2207 //                     "entity class. Something with the test setup is strange",
2208 //                     EnumSet.of(DeviceField.MAC, DeviceField.VLAN),
2209 //                     d1.getEntityClass().getKeyFields());
2210 //
2211 //        Long deviceKey = d1.getDeviceKey();
2212 //        DeviceSyncRepresentation dsr1 = new DeviceSyncRepresentation(d1);
2213 //        assertEquals("DefaultEntityClass::00:00:00:00:00:01::[2]::",
2214 //                     dsr1.getKey());
2215 //        assertEquals(1, dsr1.getEntities().size());
2216 //        assertEquals(e1, dsr1.getEntities().get(0).asEntity());
2217 //
2218 //        //**************************************
2219 //        // Test 1b: same device, now with a second entity (no IP).
2220 //        // this second entity has a lastSeen time that is earlier than the
2221 //        // first entity
2222 //        Entity e1b = new Entity(1L, (short)2, null, 4L, 5, new Date(0));
2223 //        d1 = deviceManager.learnDeviceByEntity(e1b);
2224 //        assertEquals("Sanity check failed. Should still be same device but " +
2225 //                     "deviceKeys differs", deviceKey, d1.getDeviceKey());
2226 //        dsr1 = new DeviceSyncRepresentation(d1);
2227 //        assertEquals("DefaultEntityClass::00:00:00:00:00:01::[2]::",
2228 //                     dsr1.getKey());
2229 //        assertEquals(2, dsr1.getEntities().size());
2230 //        // Entities are ordered by their lastSeen time. e1b should come
2231 //        // before e1.
2232 //        assertEquals(e1, dsr1.getEntities().get(1).asEntity());
2233 //        assertEquals(e1b, dsr1.getEntities().get(0).asEntity());
2234 //
2235 //        //**************************************
2236 //        // Test 1c: same device with a third entity that does not have a
2237 //        // switch port. It should be added to the DeviceSyncRepresentation
2238 //        Entity e1c = new Entity(1L, (short)2, 33, null, null, new Date(2000));
2239 //        d1 = deviceManager.learnDeviceByEntity(e1c);
2240 //        assertEquals("Sanity check failed. Should still be same device but " +
2241 //                     "deviceKeys differs", deviceKey, d1.getDeviceKey());
2242 //        dsr1 = new DeviceSyncRepresentation(d1);
2243 //        assertEquals("DefaultEntityClass::00:00:00:00:00:01::[2]::",
2244 //                     dsr1.getKey());
2245 //        assertEquals(3, dsr1.getEntities().size());
2246 //        // Entities are ordered by their lastSeen time
2247 //        assertEquals(e1c, dsr1.getEntities().get(2).asEntity());
2248 //        assertEquals(e1, dsr1.getEntities().get(1).asEntity());
2249 //        assertEquals(e1b, dsr1.getEntities().get(0).asEntity());
2250 //
2251 //        //**************************************
2252 //        // Test 1d: same device with a fourth entity that has a different
2253 //        // attachment point and that is newer. Device should move and
2254 //        // non-attachment point entities should be removed (e1b). Although
2255 //        // e1 is non-attachment point it will remain because it has an IP
2256 //        Entity e1d = new Entity(1L, (short)2, 33, 4L, 6, new Date(3000));
2257 //        d1 = deviceManager.learnDeviceByEntity(e1d);
2258 //        assertEquals("Sanity check failed. Should still be same device but " +
2259 //                     "deviceKeys differs", deviceKey, d1.getDeviceKey());
2260 //        dsr1 = new DeviceSyncRepresentation(d1);
2261 //        assertEquals("DefaultEntityClass::00:00:00:00:00:01::[2]::",
2262 //                     dsr1.getKey());
2263 //        assertEquals(3, dsr1.getEntities().size());
2264 //        assertEquals(e1, dsr1.getEntities().get(0).asEntity());
2265 //        assertEquals(e1c, dsr1.getEntities().get(1).asEntity());
2266 //        assertEquals(e1d, dsr1.getEntities().get(2).asEntity());
2267 //
2268 //        d1 = null;
2269 //
2270 //
2271 //        //**************************************
2272 //        // Test 2: a second device with a different entity class. The
2273 //        // mock entity classifier will return an entity class where all
2274 //        // fields are keys if the DPID is > 10L
2275 //        Entity e2 = new Entity(2L, (short)23, 24, 11L, 1, new Date(0));
2276 //        Device d2 = deviceManager.learnDeviceByEntity(e2);
2277 //        DeviceSyncRepresentation dsr2 = new DeviceSyncRepresentation(d2);
2278 //        assertEquals("Sanity check failed. Device doesn't have the expected " +
2279 //                     "entity class. Something with the test setup is strange",
2280 //                     "TestEntityClass", d2.getEntityClass().getName());
2281 //        assertEquals("Sanity check failed. Device doesn't have the expected " +
2282 //                     "entity class. Something with the test setup is strange",
2283 //                     EnumSet.of(DeviceField.MAC, DeviceField.VLAN,
2284 //                                DeviceField.SWITCH, DeviceField.PORT),
2285 //                     d2.getEntityClass().getKeyFields());
2286 //        SwitchPort swp = new SwitchPort(11L, 1, null);
2287 //        assertEquals("TestEntityClass::00:00:00:00:00:02::[23]::[" +
2288 //                     swp.toString() + "]::",
2289 //                     dsr2.getKey());
2290 //    }
2291 //
2292 //    /* interate through all entries in the sync store and return them as
2293 //     * list. We don't return the key from the store however, we assert
2294 //     * that the key from the store matches the key in the representation.
2295 //     * If we have a null value (tombstone) we simply add the null value to
2296 //     * the list to return.
2297 //     */
2298 //    private List<DeviceSyncRepresentation> getEntriesFromStore()
2299 //            throws Exception {
2300 //        List<DeviceSyncRepresentation> entries =
2301 //                new ArrayList<DeviceSyncRepresentation>();
2302 //        IClosableIterator<Entry<String, Versioned<DeviceSyncRepresentation>>> iter =
2303 //                storeClient.entries();
2304 //        try {
2305 //            while(iter.hasNext()) {
2306 //                Entry<String, Versioned<DeviceSyncRepresentation>> entry =
2307 //                        iter.next();
2308 //                DeviceSyncRepresentation dsr = entry.getValue().getValue();
2309 //                if (dsr != null)
2310 //                    assertEquals(entry.getKey(), dsr.getKey());
2311 //                entries.add(dsr);
2312 //            }
2313 //        } finally {
2314 //            if (iter != null)
2315 //                iter.close();
2316 //        }
2317 //        return entries;
2318 //    }
2319 //
2320 //    /*
2321 //     * assert whether the given Entity expected is equals to the given
2322 //     * SyncEntity actual. This method also compares the times (lastSeen,
2323 //     * activeSince). Entity.equals will not do that!
2324 //     */
2325 //    private static void assertEntityEquals(Entity expected, SyncEntity actual) {
2326 //        assertNotNull(actual);
2327 //        assertNotNull(expected);
2328 //        Entity actualEntity = actual.asEntity();
2329 //        assertEquals("entityFields", expected, actualEntity);
2330 //        assertEquals("lastSeenTimestamp",
2331 //                     expected.getLastSeenTimestamp(),
2332 //                     actualEntity.getLastSeenTimestamp());
2333 //        assertEquals("activeSince",
2334 //                     expected.getActiveSince(), actualEntity.getActiveSince());
2335 //    }
2336 //
2337 //    /* This test tests the normal operation as master when we write to the sync
2338 //     * store or delete from the store.
2339 //     */
2340 //    @Test
2341 //    public void testWriteToSyncStore() throws Exception {
2342 //        int syncStoreIntervalMs = 50;
2343 //        ITopologyService mockTopology = makeMockTopologyAllPortsAp();
2344 //        replay(mockTopology);
2345 //        deviceManager.topology = mockTopology;
2346 //        deviceManager.setSyncStoreWriteInterval(syncStoreIntervalMs);
2347 //
2348 //        Entity e1a = new Entity(1L, (short)2, 3, 4L, 5, new Date(1000));
2349 //        e1a.setActiveSince(new Date(0));
2350 //        deviceManager.learnDeviceByEntity(e1a);
2351 //
2352 //        //storeClient.put("FooBar", new DeviceSyncRepresentation());
2353 //
2354 //        List<DeviceSyncRepresentation> entries = getEntriesFromStore();
2355 //        assertEquals(1, entries.size());
2356 //        DeviceSyncRepresentation dsr1 = entries.get(0);
2357 //        assertEquals(1, dsr1.getEntities().size());
2358 //        assertEntityEquals(e1a, dsr1.getEntities().get(0));
2359 //
2360 //        // Same entity but newer timestamp. Since the device hasn't changed,
2361 //        // only the timestamp is updated and the write should be throttled.
2362 //        Entity e1b = new Entity(1L, (short)2, 3, 4L, 5, new Date(2000));
2363 //        e1b.setActiveSince(new Date(0));
2364 //        deviceManager.learnDeviceByEntity(e1a);
2365 //        entries = getEntriesFromStore();
2366 //        assertEquals(1, entries.size());
2367 //        dsr1 = entries.get(0);
2368 //        assertEquals(1, dsr1.getEntities().size());
2369 //        assertEntityEquals(e1a, dsr1.getEntities().get(0)); //e1a not e1b !!!
2370 //
2371 //        // Wait for the write interval to expire then write again.
2372 //        Thread.sleep(syncStoreIntervalMs+5);
2373 //        Entity e1c = new Entity(1L, (short)2, 3, 4L, 5, new Date(3000));
2374 //        e1c.setActiveSince(new Date(0));
2375 //        deviceManager.learnDeviceByEntity(e1c);
2376 //        entries = getEntriesFromStore();
2377 //        assertEquals(1, entries.size());
2378 //        dsr1 = entries.get(0);
2379 //        assertEquals(1, dsr1.getEntities().size());
2380 //        assertEntityEquals(e1c, dsr1.getEntities().get(0)); // e1c !!
2381 //
2382 //        // Entity for same device but with different IP. should be added
2383 //        // immediately
2384 //        Entity e1d = new Entity(1L, (short)2, 33, 4L, 5, new Date(4000));
2385 //        e1d.setActiveSince(new Date(0));
2386 //        deviceManager.learnDeviceByEntity(e1d);
2387 //        entries = getEntriesFromStore();
2388 //        assertEquals(1, entries.size());
2389 //        dsr1 = entries.get(0);
2390 //        assertEquals(2, dsr1.getEntities().size());
2391 //        assertEntityEquals(e1c, dsr1.getEntities().get(0)); // e1c !!
2392 //        assertEntityEquals(e1d, dsr1.getEntities().get(1)); // e1d !!
2393 //
2394 //        // Entity for same device with new switch port ==> moved ==> write
2395 //        // update immediately without throttle.
2396 //        // Note: the previous entities will still be there because they have
2397 //        // IPs (even though they aren't for the current attachment point)
2398 //        Entity e1e = new Entity(1L, (short)2, 33, 4L, 6, new Date(5000));
2399 //        e1e.setActiveSince(new Date(0));
2400 //        deviceManager.learnDeviceByEntity(e1e);
2401 //        entries = getEntriesFromStore();
2402 //        assertEquals(1, entries.size());
2403 //        dsr1 = entries.get(0);
2404 //        assertEquals(3, dsr1.getEntities().size());
2405 //        assertEntityEquals(e1c, dsr1.getEntities().get(0));
2406 //        assertEntityEquals(e1d, dsr1.getEntities().get(1));
2407 //        assertEntityEquals(e1e, dsr1.getEntities().get(2));
2408 //
2409 //        // Add a second device
2410 //        Entity e2 = new Entity(2L, null, null, 5L, 5, new Date());
2411 //        deviceManager.learnDeviceByEntity(e2);
2412 //        entries = getEntriesFromStore();
2413 //        assertEquals(2, entries.size());
2414 //        for (DeviceSyncRepresentation dsr: entries) {
2415 //            // This is a kinda ugly way to ensure we have the two
2416 //            // devices we need..... but it will work for now
2417 //            if (dsr.getKey().contains("::00:00:00:00:00:01::")) {
2418 //                assertEquals(3, dsr.getEntities().size());
2419 //                assertEntityEquals(e1c, dsr.getEntities().get(0));
2420 //                assertEntityEquals(e1d, dsr.getEntities().get(1));
2421 //                assertEntityEquals(e1e, dsr.getEntities().get(2));
2422 //            } else if (dsr.getKey().contains("::00:00:00:00:00:02::")) {
2423 //                assertEquals(1, dsr.getEntities().size());
2424 //                assertEntityEquals(e2, dsr.getEntities().get(0));
2425 //            } else {
2426 //                fail("Unknown entry in store: " + dsr);
2427 //            }
2428 //        }
2429 //
2430 //
2431 //        // Run entity cleanup. Since we've used phony time stamps for
2432 //        // device 1 its entities should be cleared and the device should be
2433 //        // removed from the store. Device 2 should remain in the store.
2434 //        deviceManager.cleanupEntities();
2435 //        entries = getEntriesFromStore();
2436 //        assertEquals(2, entries.size());
2437 //        for (DeviceSyncRepresentation dsr: entries) {
2438 //            if (dsr == null) {
2439 //                // pass
2440 //            } else if (dsr.getKey().contains("::00:00:00:00:00:02::")) {
2441 //                assertEquals(1, dsr.getEntities().size());
2442 //                assertEntityEquals(e2, dsr.getEntities().get(0));
2443 //            } else {
2444 //                fail("Unknown entry in store: " + dsr);
2445 //            }
2446 //        }
2447 //    }
2448 //
2449 //
2450 //    private void assertDeviceIps(Integer[] expected, IDevice d) {
2451 //        List<Integer> expectedList = Arrays.asList(expected);
2452 //        Collections.sort(expectedList);
2453 //        List<Integer> actualList = Arrays.asList(d.getIPv4Addresses());
2454 //        Collections.sort(actualList);
2455 //        assertEquals(expectedList, actualList);
2456 //    }
2457 //
2458 //    private IDevice getSingleDeviceFromDeviceManager(long mac) {
2459 //        Iterator<? extends IDevice> diter =
2460 //                deviceManager.queryDevices(mac, null, null, null, null);
2461 //        assertTrue("Query didn't return a device", diter.hasNext());
2462 //        IDevice d = diter.next();
2463 //        assertFalse("Query returned more than one device", diter.hasNext());
2464 //        return d;
2465 //    }
2466 //
2467 //    @Test
2468 //    public void testToMaster() throws Exception {
2469 //        int syncStoreWriteIntervalMs = 0;
2470 //        int initialSyncStoreConsolidateIntervalMs = 50;
2471 //        ITopologyService mockTopology = makeMockTopologyAllPortsAp();
2472 //        replay(mockTopology);
2473 //        deviceManager.topology = mockTopology;
2474 //        // We want an EntityClassifier that has switch/port as key fields
2475 //        deviceManager.entityClassifier = new MockEntityClassifier();
2476 //        deviceManager.setSyncStoreWriteInterval(syncStoreWriteIntervalMs);
2477 //        deviceManager.setInitialSyncStoreConsolidateMs(initialSyncStoreConsolidateIntervalMs);
2478 //
2479 //        // Add Device1 with two entities with two different IPs
2480 //        Entity e1a = new Entity(1L, null, 3, 4L, 5, new Date(1000));
2481 //        Entity e1b = new Entity(1L, null, 33,  4L, 5, new Date(2000));
2482 //        Device d1 = deviceManager.allocateDevice(1L, e1a,
2483 //                                                 DefaultEntityClassifier.entityClass);
2484 //        d1 = deviceManager.allocateDevice(d1, e1b, -1);
2485 //        DeviceSyncRepresentation dsr = new DeviceSyncRepresentation(d1);
2486 //        storeClient.put(dsr.getKey(), dsr);
2487 //
2488 //        // Add Device2 with different switch-ports. Only the most recent
2489 //        // one should be the attachment point
2490 //        Entity e2a = new Entity(2L, null, null, 4L, 4, new Date(1000));
2491 //        Entity e2b = new Entity(2L, null, null, 4L, 5, new Date(2000));
2492 //        Device d2 = deviceManager.allocateDevice(2L, e2a,
2493 //                                                 DefaultEntityClassifier.entityClass);
2494 //        d2 = deviceManager.allocateDevice(d2, e2b, -1);
2495 //        d2.updateAttachmentPoint(4L, (short)5,
2496 //                                 e2b.getLastSeenTimestamp().getTime());
2497 //        SwitchPort swp = new SwitchPort(4L, 5);
2498 //        SwitchPort[] aps = d2.getAttachmentPoints();
2499 //        // sanity check
2500 //        assertArrayEquals("Sanity check: should only have AP(4L,5)",
2501 //                          new SwitchPort[] {swp}, aps);
2502 //        dsr = new DeviceSyncRepresentation(d2);
2503 //        storeClient.put(dsr.getKey(), dsr);
2504 //
2505 //        // Add a tombstone entry to the store to make sure we don't trip a
2506 //        // NPE
2507 //        dsr = null;
2508 //        Versioned<DeviceSyncRepresentation> versionedDsr =
2509 //                storeClient.get("FooBar");
2510 //        storeClient.put("FooBar", versionedDsr);
2511 //
2512 //        deviceManager.getHAListener().transitionToMaster();
2513 //
2514 //        // Query for the Device1. Make sure we have the two IPs we stored.
2515 //        IDevice d = getSingleDeviceFromDeviceManager(1L);
2516 //        assertDeviceIps(new Integer[] {3, 33}, d);
2517 //        assertArrayEquals(new Short[] { Ethernet.VLAN_UNTAGGED }, d.getVlanId());
2518 //        swp = new SwitchPort(4L, 5);
2519 //        assertArrayEquals(new SwitchPort[] { swp }, d.getAttachmentPoints());
2520 //
2521 //        // Query for Device2. Make sure we only have the more recent AP
2522 //        // Query for the Device1. Make sure we have the two IPs we stored.
2523 //        d = getSingleDeviceFromDeviceManager(2L);
2524 //        assertArrayEquals(new Integer[0], d.getIPv4Addresses());
2525 //        assertArrayEquals(new Short[] { Ethernet.VLAN_UNTAGGED }, d.getVlanId());
2526 //        swp = new SwitchPort(4L, 5);
2527 //        assertArrayEquals(new SwitchPort[] { swp }, d.getAttachmentPoints());
2528 //
2529 //        //----------------------------
2530 //        // add another entry device to the store. since device manager is
2531 //        // already master we won't read this device and it should be
2532 //        // removed from the store by the consolidate task
2533 //        Entity e3 = new Entity(3L, null, null, 1L, 1, null);
2534 //        dsr = new DeviceSyncRepresentation();
2535 //        dsr.setKey("Device3");
2536 //        dsr.setEntities(Collections.singletonList(new SyncEntity(e3)));
2537 //        storeClient.put(dsr.getKey(), dsr);
2538 //
2539 //        // make sure it's in the store
2540 //        List<DeviceSyncRepresentation> entries = getEntriesFromStore();
2541 //        boolean found = false;
2542 //        for (DeviceSyncRepresentation entry: entries) {
2543 //            if (entry!=null && entry.getKey().equals("Device3"))
2544 //                found = true;
2545 //        }
2546 //        assertTrue("Device3 not in store. Entries in store: " + entries, found);
2547 //        // make sure it's not in DevManager
2548 //        Iterator<? extends IDevice> diter =
2549 //                deviceManager.queryDevices(3L, null, null, null, null);
2550 //        assertFalse("Device3 found in DeviceManager. Should be there",
2551 //                    diter.hasNext());
2552 //
2553 //        // Wait for consolidate
2554 //        Thread.sleep(initialSyncStoreConsolidateIntervalMs + 5);
2555 //        // make sure it's in NOT the store
2556 //        entries = getEntriesFromStore();
2557 //        found = false;
2558 //        for (DeviceSyncRepresentation entry: entries) {
2559 //            if (entry!=null && entry.getKey().equals("Device3"))
2560 //                found = true;
2561 //        }
2562 //        assertFalse("Device3 not is still in the store. Entries in store: "
2563 //                    + entries, found);
2564 //        // make sure it's not in DevManager
2565 //        diter = deviceManager.queryDevices(3L, null, null, null, null);
2566 //        assertFalse("Device3 found in DeviceManager. Should be there",
2567 //                    diter.hasNext());
2568 //    }
2569 //
2570 //
2571 //    @Test
2572 //    public void testConsolitateStore() throws Exception {
2573 //        int syncStoreInternalMs = 0;
2574 //        ITopologyService mockTopology = makeMockTopologyAllPortsAp();
2575 //        replay(mockTopology);
2576 //        deviceManager.topology = mockTopology;
2577 //        // We want an EntityClassifier that has switch/port as key fields
2578 //        deviceManager.entityClassifier = new MockEntityClassifier();
2579 //        deviceManager.setSyncStoreWriteInterval(syncStoreInternalMs);
2580 //
2581 //        // Add Device1 with two entities to store and let device manager
2582 //        // learn
2583 //        Entity e1a = new Entity(1L, null, null, 4L, 5, new Date(1000));
2584 //        Entity e1b = new Entity(1L, null, 3,  4L, 5, new Date(2000));
2585 //        Device d1 = deviceManager.learnDeviceByEntity(e1a);
2586 //        deviceManager.learnDeviceByEntity(e1b);
2587 //        String dev1Key = DeviceSyncRepresentation.computeKey(d1);
2588 //
2589 //
2590 //        // Add a second device to the store but do NOT add to device manager
2591 //        Entity e2 = new Entity(2L, null, null, 5L, 5, new Date());
2592 //        Device d2 = deviceManager.allocateDevice(42L, e2,
2593 //                                                 DefaultEntityClassifier.entityClass);
2594 //        DeviceSyncRepresentation dsr = new DeviceSyncRepresentation(d2);
2595 //        storeClient.put(dsr.getKey(), dsr);
2596 //        String dev2Key = DeviceSyncRepresentation.computeKey(d2);
2597 //
2598 //        // Make sure we have two devices in the store
2599 //        List<DeviceSyncRepresentation> entries = getEntriesFromStore();
2600 //        assertEquals(2, entries.size());
2601 //
2602 //        deviceManager.scheduleConsolidateStoreNow();
2603 //        Thread.sleep(25); // give the scheduler time to run the task
2604 //
2605 //        // We should still have two entries, however one of them will be a
2606 //        // tombstone
2607 //        entries = getEntriesFromStore();
2608 //        assertEquals(2, entries.size());
2609 //
2610 //        // Device 1 should still be in store
2611 //        Versioned<DeviceSyncRepresentation> versioned =
2612 //                storeClient.get(dev1Key);
2613 //        dsr = versioned.getValue();
2614 //        assertNotNull(dsr);
2615 //        assertEquals(2, dsr.getEntities().size());
2616 //        assertEntityEquals(e1a, dsr.getEntities().get(0));
2617 //        assertEntityEquals(e1b, dsr.getEntities().get(1));
2618 //
2619 //        // Device2 should be gone
2620 //        versioned = storeClient.get(dev2Key);
2621 //        assertNull(versioned.getValue());
2622 //
2623 //        // Run consolitate again. This time we check that tombstones in
2624 //        // the store are handled correctly
2625 //        deviceManager.scheduleConsolidateStoreNow();
2626 //        Thread.sleep(25); // give the scheduler time to run the task
2627 //
2628 //        // Now write a device to the store that doesn't have any switch-port
2629 //        // it should be removed
2630 //        Entity e3 = new Entity(3L, null, null, null, null, null);
2631 //        dsr.setKey("Device3");
2632 //        dsr.setEntities(Collections.singletonList(new SyncEntity(e3)));
2633 //        storeClient.put(dsr.getKey(), dsr);
2634 //
2635 //        // Run consolitate again. This time we check that tombstones in
2636 //        // the store are handled correctly
2637 //        deviceManager.scheduleConsolidateStoreNow();
2638 //        Thread.sleep(25); // give the scheduler time to run the task
2639 //        versioned = storeClient.get("Device3");
2640 //        assertNull(versioned.getValue());
2641 //
2642 //    }
2643 //
2644 // }