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