Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package net.floodlightcontroller.devicemanager.test; |
| 5 | |
| 6 | import static net.floodlightcontroller.devicemanager.IDeviceService.DeviceField.MAC; |
| 7 | import static net.floodlightcontroller.devicemanager.IDeviceService.DeviceField.PORT; |
| 8 | import static net.floodlightcontroller.devicemanager.IDeviceService.DeviceField.SWITCH; |
| 9 | import static net.floodlightcontroller.devicemanager.IDeviceService.DeviceField.VLAN; |
| 10 | |
| 11 | import java.util.EnumSet; |
| 12 | import java.util.HashMap; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | import net.floodlightcontroller.devicemanager.IDeviceService; |
| 16 | import net.floodlightcontroller.devicemanager.IEntityClass; |
| 17 | import net.floodlightcontroller.devicemanager.IDeviceService.DeviceField; |
| 18 | import net.floodlightcontroller.devicemanager.internal.DefaultEntityClassifier; |
| 19 | import net.floodlightcontroller.devicemanager.internal.Entity; |
| 20 | |
| 21 | /** |
| 22 | * Extension to simple entity classifier to help in unit tests to provide table |
| 23 | * based multiple entity classification mock for reclassification tests |
| 24 | * |
| 25 | */ |
| 26 | public class MockFlexEntityClassifier extends DefaultEntityClassifier { |
| 27 | Map <Long, IEntityClass> switchEntities; |
| 28 | Map <Short, IEntityClass> vlanEntities; |
| 29 | |
| 30 | public static class TestEntityClass implements IEntityClass { |
| 31 | String name; |
| 32 | public TestEntityClass(String name) { |
| 33 | this.name = name; |
| 34 | } |
| 35 | @Override |
| 36 | public EnumSet<DeviceField> getKeyFields() { |
| 37 | return EnumSet.of(MAC); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String getName() { |
| 42 | return name; |
| 43 | } |
| 44 | } |
| 45 | public static IEntityClass defaultClass = new TestEntityClass("default"); |
| 46 | public MockFlexEntityClassifier() { |
| 47 | switchEntities = new HashMap<Long, IEntityClass> (); |
| 48 | vlanEntities = new HashMap<Short, IEntityClass> (); |
| 49 | } |
| 50 | public IEntityClass createTestEntityClass(String name) { |
| 51 | return new TestEntityClass(name); |
| 52 | } |
| 53 | |
| 54 | public void addSwitchEntity(Long dpid, IEntityClass entityClass) { |
| 55 | switchEntities.put(dpid, entityClass); |
| 56 | } |
| 57 | public void removeSwitchEntity(Long dpid) { |
| 58 | switchEntities.remove(dpid); |
| 59 | } |
| 60 | public void addVlanEntities(Short vlan, IEntityClass entityClass) { |
| 61 | vlanEntities.put(vlan, entityClass); |
| 62 | } |
| 63 | public void removeVlanEntities(Short vlan) { |
| 64 | vlanEntities.remove(vlan); |
| 65 | } |
| 66 | @Override |
| 67 | public IEntityClass classifyEntity(Entity entity) { |
| 68 | if (switchEntities.containsKey(entity.getSwitchDPID())) |
| 69 | return switchEntities.get(entity.getSwitchDPID()); |
| 70 | if (vlanEntities.containsKey(entity.getVlan())) |
| 71 | return vlanEntities.get(entity.getVlan()); |
| 72 | return defaultClass; |
| 73 | } |
| 74 | @Override |
| 75 | public EnumSet<IDeviceService.DeviceField> getKeyFields() { |
| 76 | return EnumSet.of(MAC, VLAN, SWITCH, PORT); |
| 77 | } |
| 78 | } |