Merge "Move adsal into its own subdirectory."
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / test / java / org / opendaylight / controller / hosttracker / test / MockDeviceManager.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 java.util.Collection;
20 import java.util.Date;
21 import java.util.List;
22
23 import org.opendaylight.controller.hosttracker.Entity;
24 import org.opendaylight.controller.hosttracker.IDevice;
25 import org.opendaylight.controller.hosttracker.IDeviceListener;
26 import org.opendaylight.controller.hosttracker.IEntityClass;
27 import org.opendaylight.controller.hosttracker.IEntityClassifierService;
28 import org.opendaylight.controller.hosttracker.internal.AttachmentPoint;
29 import org.opendaylight.controller.hosttracker.internal.Device;
30 import org.opendaylight.controller.hosttracker.internal.DeviceManagerImpl;
31 import org.opendaylight.controller.sal.core.NodeConnector;
32
33 /**
34  * Mock device manager useful for unit tests
35  *
36  * @author readams
37  */
38 public class MockDeviceManager extends DeviceManagerImpl {
39     /**
40      * Set a new IEntityClassifier Use this as a quick way to use a particular
41      * entity classifier in a single test without having to setup the full
42      * FloodlightModuleContext again.
43      *
44      * @param ecs
45      */
46     public void setEntityClassifier(IEntityClassifierService ecs) {
47         this.entityClassifier = ecs;
48         // setSyncServiceIfNotSet(new MockSyncService());
49         this.start();
50     }
51
52     /**
53      * Learn a device using the given characteristics.
54      *
55      * @param macAddress
56      *            the MAC
57      * @param vlan
58      *            the VLAN (can be null)
59      * @param ipv4Address
60      *            the IP (can be null)
61      * @param switchDPID
62      *            the attachment point switch DPID (can be null)
63      * @param switchPort
64      *            the attachment point switch port (can be null)
65      * @param processUpdates
66      *            if false, will not send updates. Note that this method is not
67      *            thread safe if this is false
68      * @return the device, either new or not
69      */
70     public IDevice learnEntity(long macAddress, Short vlan,
71             Integer ipv4Address, NodeConnector port, boolean processUpdates) {
72         List<IDeviceListener> listeners = deviceListeners.getOrderedListeners();
73         if (!processUpdates) {
74             deviceListeners.clearListeners();
75         }
76
77         if (vlan != null && vlan.shortValue() <= 0)
78             vlan = null;
79         if (ipv4Address != null && ipv4Address == 0)
80             ipv4Address = null;
81         IDevice res = learnDeviceByEntity(new Entity(macAddress, vlan,
82                 ipv4Address, port, new Date()));
83         // Restore listeners
84         if (listeners != null) {
85             for (IDeviceListener listener : listeners) {
86                 deviceListeners.addListener("device", listener);
87             }
88         }
89         return res;
90     }
91
92     @Override
93     public void deleteDevice(Device device) {
94         super.deleteDevice(device);
95     }
96
97     /**
98      * Learn a device using the given characteristics.
99      *
100      * @param macAddress
101      *            the MAC
102      * @param vlan
103      *            the VLAN (can be null)
104      * @param ipv4Address
105      *            the IP (can be null)
106      * @param switchDPID
107      *            the attachment point switch DPID (can be null)
108      * @param switchPort
109      *            the attachment point switch port (can be null)
110      * @return the device, either new or not
111      */
112     public IDevice learnEntity(long macAddress, Short vlan,
113             Integer ipv4Address, NodeConnector port) {
114         return learnEntity(macAddress, vlan, ipv4Address, port, true);
115     }
116
117     @Override
118     protected Device allocateDevice(Long deviceKey, Entity entity,
119             IEntityClass entityClass) {
120         return new MockDevice(this, deviceKey, entity, entityClass);
121     }
122
123     @Override
124     protected Device allocateDevice(Long deviceKey, String dhcpClientName,
125             List<AttachmentPoint> aps, List<AttachmentPoint> trueAPs,
126             Collection<Entity> entities, IEntityClass entityClass) {
127         return new MockDevice(this, deviceKey, aps, trueAPs, entities,
128                 entityClass);
129     }
130
131     @Override
132     protected Device allocateDevice(Device device, Entity entity,
133             int insertionpoint) {
134         return new MockDevice(device, entity, insertionpoint);
135     }
136 }