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