Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.device.impl; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNotNull; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.onlab.onos.net.Device.Type.*; |
| 23 | import static org.onlab.onos.net.MastershipRole.*; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.HashMap; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import org.junit.After; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.onlab.onos.net.DefaultDevice; |
| 36 | import org.onlab.onos.net.Device; |
| 37 | import org.onlab.onos.net.DeviceId; |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.MastershipRole; |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.device.DeviceDescription; |
| 40 | import org.onlab.onos.net.device.DeviceProvider; |
| 41 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
| 42 | import org.onlab.onos.net.device.DeviceProviderService; |
| 43 | import org.onlab.onos.net.device.PortDescription; |
| 44 | import org.onlab.onos.net.provider.ProviderId; |
| 45 | import org.onlab.onos.openflow.controller.Dpid; |
| 46 | import org.onlab.onos.openflow.controller.OpenFlowController; |
| 47 | import org.onlab.onos.openflow.controller.OpenFlowEventListener; |
| 48 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 49 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
| 50 | import org.onlab.onos.openflow.controller.PacketListener; |
| 51 | import org.onlab.onos.openflow.controller.RoleState; |
| 52 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 53 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 54 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 55 | import org.projectfloodlight.openflow.protocol.OFPortReason; |
| 56 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 57 | import org.projectfloodlight.openflow.protocol.ver10.OFFactoryVer10; |
| 58 | import org.projectfloodlight.openflow.types.OFPort; |
| 59 | |
| 60 | import com.google.common.collect.HashMultimap; |
| 61 | import com.google.common.collect.Lists; |
| 62 | import com.google.common.collect.Multimap; |
| 63 | |
| 64 | public class OpenFlowDeviceProviderTest { |
| 65 | |
| 66 | private static final ProviderId PID = new ProviderId("of", "test"); |
| 67 | private static final DeviceId DID1 = DeviceId.deviceId("of:0000000000000001"); |
| 68 | private static final Dpid DPID1 = Dpid.dpid(DID1.uri()); |
| 69 | |
| 70 | private static final OFPortDesc PD1 = portDesc(1); |
| 71 | private static final OFPortDesc PD2 = portDesc(2); |
| 72 | private static final OFPortDesc PD3 = portDesc(3); |
| 73 | |
| 74 | private static final List<OFPortDesc> PLIST = Lists.newArrayList(PD1, PD2); |
| 75 | |
| 76 | private static final Device DEV1 = |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 77 | new DefaultDevice(PID, DID1, SWITCH, "", "", "", "", null); |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 78 | |
| 79 | private static final TestOpenFlowSwitch SW1 = new TestOpenFlowSwitch(); |
| 80 | |
| 81 | private final OpenFlowDeviceProvider provider = new OpenFlowDeviceProvider(); |
| 82 | private final TestDeviceRegistry registry = new TestDeviceRegistry(); |
| 83 | private final TestController controller = new TestController(); |
| 84 | |
| 85 | @Before |
| 86 | public void startUp() { |
| 87 | provider.providerRegistry = registry; |
| 88 | provider.controller = controller; |
| 89 | controller.switchMap.put(DPID1, SW1); |
| 90 | provider.activate(); |
| 91 | assertNotNull("provider should be registered", registry.provider); |
| 92 | assertNotNull("listener should be registered", controller.listener); |
| 93 | assertEquals("devices not added", 1, registry.connected.size()); |
| 94 | assertEquals("ports not added", 2, registry.ports.get(DID1).size()); |
| 95 | } |
| 96 | |
| 97 | @After |
| 98 | public void tearDown() { |
| 99 | provider.deactivate(); |
| 100 | assertTrue("devices should be removed", registry.connected.isEmpty()); |
| 101 | assertNull("listener should be removed", controller.listener); |
| 102 | provider.controller = null; |
| 103 | provider.providerRegistry = null; |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void roleChanged() { |
| 108 | provider.roleChanged(DEV1, MASTER); |
| 109 | assertEquals("Should be MASTER", RoleState.MASTER, controller.roleMap.get(DPID1)); |
| 110 | provider.roleChanged(DEV1, STANDBY); |
| 111 | assertEquals("Should be EQUAL", RoleState.EQUAL, controller.roleMap.get(DPID1)); |
| 112 | provider.roleChanged(DEV1, NONE); |
| 113 | assertEquals("Should be SLAVE", RoleState.SLAVE, controller.roleMap.get(DPID1)); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void triggerProbe() { |
| 118 | // TODO |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void switchRemoved() { |
| 123 | controller.listener.switchRemoved(DPID1); |
| 124 | assertTrue("device not removed", registry.connected.isEmpty()); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void portChanged() { |
| 129 | OFPortStatus stat = SW1.factory().buildPortStatus() |
| 130 | .setReason(OFPortReason.ADD) |
| 131 | .setDesc(PD3) |
| 132 | .build(); |
| 133 | controller.listener.portChanged(DPID1, stat); |
Ayaka Koshibe | 39a0480 | 2014-09-22 10:23:54 -0700 | [diff] [blame] | 134 | assertNotNull("never went throught the provider service", registry.descr); |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 135 | assertEquals("port status unhandled", 3, registry.ports.get(DID1).size()); |
| 136 | } |
| 137 | |
Ayaka Koshibe | 32402f3 | 2014-09-25 11:08:54 -0700 | [diff] [blame] | 138 | @Test |
| 139 | public void roleAssertFailed() { |
| 140 | controller.listener.roleAssertFailed(DPID1, RoleState.MASTER); |
| 141 | assertEquals("wrong role reported", DPID1, registry.roles.get(MASTER)); |
| 142 | controller.listener.roleAssertFailed(DPID1, RoleState.EQUAL); |
| 143 | assertEquals("wrong role reported", DPID1, registry.roles.get(STANDBY)); |
| 144 | controller.listener.roleAssertFailed(DPID1, RoleState.SLAVE); |
| 145 | assertEquals("wrong role reported", DPID1, registry.roles.get(NONE)); |
| 146 | } |
| 147 | |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 148 | private static OFPortDesc portDesc(int port) { |
| 149 | OFPortDesc.Builder builder = OFFactoryVer10.INSTANCE.buildPortDesc(); |
| 150 | builder.setPortNo(OFPort.of(port)); |
| 151 | |
| 152 | return builder.build(); |
| 153 | } |
| 154 | |
| 155 | private class TestDeviceRegistry implements DeviceProviderRegistry { |
| 156 | DeviceProvider provider; |
Ayaka Koshibe | 32402f3 | 2014-09-25 11:08:54 -0700 | [diff] [blame] | 157 | |
| 158 | Set<DeviceId> connected = new HashSet<>(); |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 159 | Multimap<DeviceId, PortDescription> ports = HashMultimap.create(); |
Ayaka Koshibe | 39a0480 | 2014-09-22 10:23:54 -0700 | [diff] [blame] | 160 | PortDescription descr = null; |
Ayaka Koshibe | 32402f3 | 2014-09-25 11:08:54 -0700 | [diff] [blame] | 161 | Map<MastershipRole, Dpid> roles = new HashMap<>(); |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 162 | |
| 163 | @Override |
| 164 | public DeviceProviderService register(DeviceProvider provider) { |
| 165 | this.provider = provider; |
| 166 | return new TestProviderService(); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void unregister(DeviceProvider provider) { |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Set<ProviderId> getProviders() { |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | private class TestProviderService implements DeviceProviderService { |
| 179 | |
| 180 | @Override |
| 181 | public DeviceProvider provider() { |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void deviceConnected(DeviceId deviceId, |
| 187 | DeviceDescription deviceDescription) { |
| 188 | connected.add(deviceId); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void deviceDisconnected(DeviceId deviceId) { |
| 193 | connected.remove(deviceId); |
| 194 | ports.removeAll(deviceId); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void updatePorts(DeviceId deviceId, |
| 199 | List<PortDescription> portDescriptions) { |
| 200 | for (PortDescription p : portDescriptions) { |
| 201 | ports.put(deviceId, p); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void portStatusChanged(DeviceId deviceId, |
| 207 | PortDescription portDescription) { |
Ayaka Koshibe | 39a0480 | 2014-09-22 10:23:54 -0700 | [diff] [blame] | 208 | ports.put(deviceId, portDescription); |
| 209 | descr = portDescription; |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 210 | } |
| 211 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 212 | @Override |
| 213 | public void unableToAssertRole(DeviceId deviceId, MastershipRole role) { |
Ayaka Koshibe | 32402f3 | 2014-09-25 11:08:54 -0700 | [diff] [blame] | 214 | roles.put(role, Dpid.dpid(deviceId.uri())); |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | private class TestController implements OpenFlowController { |
| 221 | OpenFlowSwitchListener listener = null; |
| 222 | Map<Dpid, RoleState> roleMap = new HashMap<Dpid, RoleState>(); |
| 223 | Map<Dpid, OpenFlowSwitch> switchMap = new HashMap<Dpid, OpenFlowSwitch>(); |
| 224 | |
| 225 | @Override |
| 226 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 227 | return switchMap.values(); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 242 | return switchMap.get(dpid); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 252 | |
| 253 | return null; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void addListener(OpenFlowSwitchListener listener) { |
| 258 | this.listener = listener; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void removeListener(OpenFlowSwitchListener listener) { |
| 263 | this.listener = null; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void addPacketListener(int priority, PacketListener listener) { |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void removePacketListener(PacketListener listener) { |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void addEventListener(OpenFlowEventListener listener) { |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void removeEventListener(OpenFlowEventListener listener) { |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void write(Dpid dpid, OFMessage msg) { |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void processPacket(Dpid dpid, OFMessage msg) { |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void setRole(Dpid dpid, RoleState role) { |
| 292 | roleMap.put(dpid, role); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | private static class TestOpenFlowSwitch implements OpenFlowSwitch { |
| 297 | |
| 298 | RoleState state; |
| 299 | List<OFMessage> sent = new ArrayList<OFMessage>(); |
| 300 | OFFactory factory = OFFactoryVer10.INSTANCE; |
| 301 | |
| 302 | @Override |
| 303 | public void sendMsg(OFMessage msg) { |
| 304 | sent.add(msg); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void sendMsg(List<OFMessage> msgs) { |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void handleMessage(OFMessage fromSwitch) { |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void setRole(RoleState role) { |
| 317 | state = role; |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public RoleState getRole() { |
| 322 | return state; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public List<OFPortDesc> getPorts() { |
| 327 | return PLIST; |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public OFFactory factory() { |
| 332 | return factory; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public String getStringId() { |
| 337 | return null; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public long getId() { |
| 342 | return DPID1.value(); |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public String manfacturerDescription() { |
| 347 | return null; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public String datapathDescription() { |
| 352 | return null; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public String hardwareDescription() { |
| 357 | return null; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public String softwareDescription() { |
| 362 | return null; |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public String serialNumber() { |
| 367 | return null; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void disconnectSwitch() { |
| 372 | } |
| 373 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 374 | @Override |
| 375 | public void returnRoleAssertFailure(RoleState role) { |
| 376 | } |
| 377 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 378 | @Override |
| 379 | public boolean isOptical() { |
| 380 | return false; |
| 381 | } |
| 382 | |
Ayaka Koshibe | 0380d4c | 2014-09-19 16:03:07 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | } |