BUG-2218: Keep existing link augmentations during discovery process
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / test / java / org / opendaylight / controller / hosttracker / test / MockFlexEntityClassifier.java
1 /*
2  * Copyright (c) 2013 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 package org.opendaylight.controller.hosttracker.test;
18
19 import static org.opendaylight.controller.hosttracker.IDeviceService.DeviceField.MAC;
20 import static org.opendaylight.controller.hosttracker.IDeviceService.DeviceField.SWITCHPORT;
21 import static org.opendaylight.controller.hosttracker.IDeviceService.DeviceField.VLAN;
22
23 import java.util.EnumSet;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.opendaylight.controller.hosttracker.Entity;
28 import org.opendaylight.controller.hosttracker.IDeviceService;
29 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
30 import org.opendaylight.controller.hosttracker.IEntityClass;
31 import org.opendaylight.controller.hosttracker.internal.DefaultEntityClassifier;
32
33 /**
34  * Extension to simple entity classifier to help in unit tests to provide table
35  * based multiple entity classification mock for reclassification tests
36  *
37  */
38 public class MockFlexEntityClassifier extends DefaultEntityClassifier {
39     Map<Long, IEntityClass> switchEntities;
40     Map<Short, IEntityClass> vlanEntities;
41
42     public static class TestEntityClass implements IEntityClass {
43         String name;
44
45         public TestEntityClass(String name) {
46             this.name = name;
47         }
48
49         @Override
50         public EnumSet<DeviceField> getKeyFields() {
51             return EnumSet.of(MAC);
52         }
53
54         @Override
55         public String getName() {
56             return name;
57         }
58     }
59
60     public static IEntityClass defaultClass = new TestEntityClass("default");
61
62     public MockFlexEntityClassifier() {
63         switchEntities = new HashMap<Long, IEntityClass>();
64         vlanEntities = new HashMap<Short, IEntityClass>();
65     }
66
67     public IEntityClass createTestEntityClass(String name) {
68         return new TestEntityClass(name);
69     }
70
71     public void addSwitchEntity(Long dpid, IEntityClass entityClass) {
72         switchEntities.put(dpid, entityClass);
73     }
74
75     public void removeSwitchEntity(Long dpid) {
76         switchEntities.remove(dpid);
77     }
78
79     public void addVlanEntities(Short vlan, IEntityClass entityClass) {
80         vlanEntities.put(vlan, entityClass);
81     }
82
83     public void removeVlanEntities(Short vlan) {
84         vlanEntities.remove(vlan);
85     }
86
87     @Override
88     public IEntityClass classifyEntity(Entity entity) {
89         if (switchEntities.containsKey((Long) entity.getPort().getNode()
90                 .getID()))
91             return switchEntities
92                     .get((Long) entity.getPort().getNode().getID());
93         if (vlanEntities.containsKey(entity.getVlan()))
94             return vlanEntities.get(entity.getVlan());
95         return defaultClass;
96     }
97
98     @Override
99     public EnumSet<IDeviceService.DeviceField> getKeyFields() {
100         return EnumSet.of(MAC, VLAN, SWITCHPORT);
101     }
102 }