Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.devicemanager.test; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.Collections; |
| 5 | import java.util.Date; |
| 6 | import java.util.List; |
| 7 | import java.util.Set; |
| 8 | |
| 9 | import net.floodlightcontroller.devicemanager.IDevice; |
| 10 | import net.floodlightcontroller.devicemanager.IDeviceListener; |
| 11 | import net.floodlightcontroller.devicemanager.IEntityClass; |
| 12 | import net.floodlightcontroller.devicemanager.IEntityClassifierService; |
| 13 | import net.floodlightcontroller.devicemanager.internal.AttachmentPoint; |
| 14 | import net.floodlightcontroller.devicemanager.internal.Device; |
| 15 | import net.floodlightcontroller.devicemanager.internal.DeviceManagerImpl; |
| 16 | import net.floodlightcontroller.devicemanager.internal.Entity; |
| 17 | |
| 18 | /** |
| 19 | * Mock device manager useful for unit tests |
| 20 | * @author readams |
| 21 | */ |
| 22 | public class MockDeviceManager extends DeviceManagerImpl { |
| 23 | /** |
| 24 | * Set a new IEntityClassifier |
| 25 | * Use this as a quick way to use a particular entity classifier in a |
| 26 | * single test without having to setup the full FloodlightModuleContext |
| 27 | * again. |
| 28 | * @param ecs |
| 29 | */ |
| 30 | public void setEntityClassifier(IEntityClassifierService ecs) { |
| 31 | this.entityClassifier = ecs; |
| 32 | this.startUp(null); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Learn a device using the given characteristics. |
| 37 | * @param macAddress the MAC |
| 38 | * @param vlan the VLAN (can be null) |
| 39 | * @param ipv4Address the IP (can be null) |
| 40 | * @param switchDPID the attachment point switch DPID (can be null) |
| 41 | * @param switchPort the attachment point switch port (can be null) |
| 42 | * @param processUpdates if false, will not send updates. Note that this |
| 43 | * method is not thread safe if this is false |
| 44 | * @return the device, either new or not |
| 45 | */ |
| 46 | public IDevice learnEntity(long macAddress, Short vlan, |
| 47 | Integer ipv4Address, Long switchDPID, |
| 48 | Integer switchPort, |
| 49 | boolean processUpdates) { |
| 50 | Set<IDeviceListener> listeners = deviceListeners; |
| 51 | if (!processUpdates) { |
| 52 | deviceListeners = Collections.<IDeviceListener>emptySet(); |
| 53 | } |
| 54 | |
| 55 | if (vlan != null && vlan.shortValue() <= 0) |
| 56 | vlan = null; |
| 57 | if (ipv4Address != null && ipv4Address == 0) |
| 58 | ipv4Address = null; |
| 59 | IDevice res = learnDeviceByEntity(new Entity(macAddress, vlan, |
| 60 | ipv4Address, switchDPID, |
| 61 | switchPort, new Date())); |
| 62 | deviceListeners = listeners; |
| 63 | return res; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Learn a device using the given characteristics. |
| 68 | * @param macAddress the MAC |
| 69 | * @param vlan the VLAN (can be null) |
| 70 | * @param ipv4Address the IP (can be null) |
| 71 | * @param switchDPID the attachment point switch DPID (can be null) |
| 72 | * @param switchPort the attachment point switch port (can be null) |
| 73 | * @return the device, either new or not |
| 74 | */ |
| 75 | public IDevice learnEntity(long macAddress, Short vlan, |
| 76 | Integer ipv4Address, Long switchDPID, |
| 77 | Integer switchPort) { |
| 78 | return learnEntity(macAddress, vlan, ipv4Address, |
| 79 | switchDPID, switchPort, true); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected Device allocateDevice(Long deviceKey, |
| 84 | Entity entity, |
| 85 | IEntityClass entityClass) { |
| 86 | return new MockDevice(this, deviceKey, entity, entityClass); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected Device allocateDevice(Long deviceKey, |
| 91 | List<AttachmentPoint> aps, |
| 92 | List<AttachmentPoint> trueAPs, |
| 93 | Collection<Entity> entities, |
| 94 | IEntityClass entityClass) { |
| 95 | return new MockDevice(this, deviceKey, aps, trueAPs, entities, entityClass); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected Device allocateDevice(Device device, |
| 100 | Entity entity) { |
| 101 | return new MockDevice(device, entity); |
| 102 | } |
| 103 | } |