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