DeviceManagerImpl implements IDeviceService, IOFMessageListener, ITopologyListener, IFloodlightModule, IEntityClassListener, IInfoProvider { @Override public IDevice findDevice(@Nonnull MacAddress macAddress, VlanVid vlan, @Nonnull IPv4Address ipv4Address, @Nonnull IPv6Address ipv6Address, @Nonnull DatapathId switchDPID, @Nonnull OFPort switchPort) throws IllegalArgumentException { if (macAddress == null) { throw new IllegalArgumentException("MAC address cannot be null. Try MacAddress.NONE if intention is 'no MAC'"); } if (ipv4Address == null) { throw new IllegalArgumentException("IPv4 address cannot be null. Try IPv4Address.NONE if intention is 'no IPv4'"); } if (ipv6Address == null) { throw new IllegalArgumentException("IPv6 address cannot be null. Try IPv6Address.NONE if intention is 'no IPv6'"); } if (vlan == null) { throw new IllegalArgumentException("VLAN cannot be null. Try VlanVid.ZERO if intention is 'no VLAN / untagged'"); } if (switchDPID == null) { throw new IllegalArgumentException("Switch DPID cannot be null. Try DatapathId.NONE if intention is 'no DPID'"); } if (switchPort == null) { throw new IllegalArgumentException("Switch port cannot be null. Try OFPort.ZERO if intention is 'no port'"); } Entity e = new Entity(macAddress, vlan, ipv4Address, ipv6Address, switchDPID, switchPort, Entity.NO_DATE); if (!allKeyFieldsPresent(e, entityClassifier.getKeyFields())) { throw new IllegalArgumentException("Not all key fields specified." + " Required fields: " + entityClassifier.getKeyFields()); } return findDeviceByEntity(e); }  @Override IDevice getDevice(Long deviceKey); @Override IDevice findDevice(@Nonnull MacAddress macAddress, VlanVid vlan,
			@Nonnull IPv4Address ipv4Address, @Nonnull IPv6Address ipv6Address,
			@Nonnull DatapathId switchDPID, @Nonnull OFPort switchPort); @Override IDevice findClassDevice(@Nonnull IEntityClass entityClass, @Nonnull MacAddress macAddress,
			@Nonnull VlanVid vlan, @Nonnull IPv4Address ipv4Address, @Nonnull IPv6Address ipv6Address); @Override Collection<? extends IDevice> getAllDevices(); @Override void addIndex(boolean perClass,
			EnumSet<DeviceField> keyFields); @Override Iterator<? extends IDevice> queryDevices(@Nonnull MacAddress macAddress,
			VlanVid vlan,
			@Nonnull IPv4Address ipv4Address,
			@Nonnull IPv6Address ipv6Address,
			@Nonnull DatapathId switchDPID,
			@Nonnull OFPort switchPort); @Override Iterator<? extends IDevice> queryClassDevices(@Nonnull IEntityClass entityClass,
			@Nonnull MacAddress macAddress,
			@Nonnull VlanVid vlan,
			@Nonnull IPv4Address ipv4Address,
			@Nonnull IPv6Address ipv6Address,
			@Nonnull DatapathId switchDPID,
			@Nonnull OFPort switchPort); @Override void addListener(IDeviceListener listener); @Override void addSuppressAPs(DatapathId swId, OFPort port); @Override void removeSuppressAPs(DatapathId swId, OFPort port); @Override Set<SwitchPort> getSuppressAPs(); @Override Map<String, Object> getInfo(String type); @Override String getName(); @Override boolean isCallbackOrderingPrereq(OFType type, String name); @Override boolean isCallbackOrderingPostreq(OFType type, String name); @Override Command receive(IOFSwitch sw, OFMessage msg,
			FloodlightContext cntx); @Override Collection<Class<? extends IFloodlightService>> getModuleServices(); @Override Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls(); @Override Collection<Class<? extends IFloodlightService>> getModuleDependencies(); @Override void init(FloodlightModuleContext fmc); @Override void startUp(FloodlightModuleContext fmc); boolean isValidAttachmentPoint(DatapathId switchDPID,
			OFPort switchPort); @Override void topologyChanged(List<LDUpdate> updateList); @Override void entityClassChanged(Set<String> entityClassNames); static final String MODULE_NAME; static final String PACKAGE; public IDebugCounter cntIncoming; public IDebugCounter cntReconcileRequest; public IDebugCounter cntReconcileNoSource; public IDebugCounter cntReconcileNoDest; public IDebugCounter cntInvalidSource; public IDebugCounter cntInvalidDest; public IDebugCounter cntNoSource; public IDebugCounter cntNoDest; public IDebugCounter cntDhcpClientNameSnooped; public IDebugCounter cntDeviceOnInternalPortNotLearned; public IDebugCounter cntPacketNotAllowed; public IDebugCounter cntNewDevice; public IDebugCounter cntPacketOnInternalPortForKnownDevice; public IDebugCounter cntNewEntity; public IDebugCounter cntDeviceChanged; public IDebugCounter cntDeviceMoved; public IDebugCounter cntCleanupEntitiesRuns; public IDebugCounter cntEntityRemovedTimeout; public IDebugCounter cntDeviceDeleted; public IDebugCounter cntDeviceReclassifyDelete; public IDebugCounter cntDeviceStrored; public IDebugCounter cntDeviceStoreThrottled; public IDebugCounter cntDeviceRemovedFromStore; public IDebugCounter cntSyncException; public IDebugCounter cntDevicesFromStore; public IDebugCounter cntConsolidateStoreRuns; public IDebugCounter cntConsolidateStoreDevicesRemoved; public IDebugCounter cntTransitionToMaster; public AttachmentPointComparator apComparator; public SingletonTask entityCleanupTask; }
@Test public void testFindDevice() throws FloodlightModuleException { boolean exceptionCaught; deviceManager.entityClassifier= new MockEntityClassifierMac(); deviceManager.startUp(null); ITopologyService mockTopology = createMock(ITopologyService.class); deviceManager.topology = mockTopology; expect(mockTopology.isAttachmentPointPort(DatapathId.of(anyLong()), OFPort.of(anyShort()))). andReturn(true).anyTimes(); expect(mockTopology.getOpenflowDomainId(DatapathId.of(EasyMock.anyLong()))).andReturn(DatapathId.of(1L)).anyTimes(); replay(mockTopology); Entity entity1 = new Entity(MacAddress.of(1L), VlanVid.ofVlan(1), IPv4Address.of(1), IPv6Address.NONE, DatapathId.of(1L), OFPort.of(1), new Date()); Entity entity2 = new Entity(MacAddress.of(2L), VlanVid.ofVlan(2), IPv4Address.of(2), IPv6Address.NONE, DatapathId.of(1L), OFPort.of(2), new Date()); Entity entity2b = new Entity(MacAddress.of(22L), VlanVid.ofVlan(2), IPv4Address.of(2), IPv6Address.NONE, DatapathId.of(1L), OFPort.of(2), new Date()); Entity entity3 = new Entity(MacAddress.of(3L), VlanVid.ofVlan(1), IPv4Address.of(3), IPv6Address.NONE, DatapathId.of(2L), OFPort.of(1), new Date()); Entity entity4 = new Entity(MacAddress.of(4L), VlanVid.ofVlan(2), IPv4Address.of(4), IPv6Address.NONE, DatapathId.of(2L), OFPort.of(2), new Date()); Entity entity5 = new Entity(MacAddress.of(5L), VlanVid.ofVlan(1), IPv4Address.of(5), IPv6Address.NONE, DatapathId.of(3L), OFPort.of(1), new Date()); IDevice d1 = deviceManager.learnDeviceByEntity(entity1); IDevice d2 = deviceManager.learnDeviceByEntity(entity2); IDevice d3 = deviceManager.learnDeviceByEntity(entity3); IDevice d4 = deviceManager.learnDeviceByEntity(entity4); IDevice d5 = deviceManager.learnDeviceByEntity(entity5); assertEquals(MockEntityClassifierMac.testECMac1, d1.getEntityClass()); assertEquals(MockEntityClassifierMac.testECMac1, d2.getEntityClass()); assertEquals(MockEntityClassifierMac.testECMac2, d3.getEntityClass()); assertEquals(MockEntityClassifierMac.testECMac2, d4.getEntityClass()); assertEquals(DefaultEntityClassifier.entityClass, d5.getEntityClass()); assertEquals(d1, deviceManager.findDevice(entity1.getMacAddress(), entity1.getVlan(), entity1.getIpv4Address(), entity1.getIpv6Address(), entity1.getSwitchDPID(), entity1.getSwitchPort())); assertEquals(d1, deviceManager.findDevice(entity1.getMacAddress(), entity1.getVlan(), entity1.getIpv4Address(), entity1.getIpv6Address(), entity1.getSwitchDPID(), OFPort.of(entity1.getSwitchPort().getPortNumber()+1))); assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(), VlanVid.ofVlan(42), entity1.getIpv4Address(), entity1.getIpv6Address(), entity1.getSwitchDPID(), entity1.getSwitchPort())); assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(), VlanVid.ZERO, entity1.getIpv4Address(), entity1.getIpv6Address(), entity1.getSwitchDPID(), entity1.getSwitchPort())); assertEquals(d2, deviceManager.findDeviceByEntity(entity2)); assertEquals(null, deviceManager.findDeviceByEntity(entity2b)); assertEquals(d3, deviceManager.findDevice(entity3.getMacAddress(), entity3.getVlan(), entity3.getIpv4Address(), entity3.getIpv6Address(), entity3.getSwitchDPID(), entity3.getSwitchPort())); exceptionCaught = false; try { assertEquals(null, deviceManager.findDevice(entity3.getMacAddress(), entity3.getVlan(), entity3.getIpv4Address(), entity3.getIpv6Address(), DatapathId.NONE, OFPort.ZERO)); } catch (IllegalArgumentException e) { exceptionCaught = true; } if (!exceptionCaught) fail("findDevice() did not throw IllegalArgumentException"); assertEquals(d4, deviceManager.findDeviceByEntity(entity4)); assertEquals(d5, deviceManager.findDevice(entity5.getMacAddress(), entity5.getVlan(), entity5.getIpv4Address(), entity5.getIpv6Address(), entity5.getSwitchDPID(), entity5.getSwitchPort())); exceptionCaught = false; try { assertEquals(d5, deviceManager.findDevice(entity5.getMacAddress(), entity5.getVlan(), entity5.getIpv4Address(), entity5.getIpv6Address(), DatapathId.NONE, OFPort.ZERO)); } catch (IllegalArgumentException e) { exceptionCaught = true; } if (!exceptionCaught) fail("findDevice() did not throw IllegalArgumentException"); Entity entityNoClass = new Entity(MacAddress.of(5L), VlanVid.ofVlan(1), IPv4Address.of(5), IPv6Address.NONE, DatapathId.of(-1L), OFPort.of(1), new Date()); assertEquals(null, deviceManager.findDeviceByEntity(entityNoClass)); assertEquals(d1, deviceManager.findClassDevice(d2.getEntityClass(), entity1.getMacAddress(), entity1.getVlan(), entity1.getIpv4Address(), entity1.getIpv6Address())); assertEquals(d1, deviceManager.findClassDevice(d2.getEntityClass(), entity1.getMacAddress(), entity1.getVlan(), IPv4Address.NONE, entity1.getIpv6Address())); assertEquals(null, deviceManager.findClassDevice(d2.getEntityClass(), entity1.getMacAddress(), VlanVid.ZERO, IPv4Address.NONE, entity1.getIpv6Address())); }