Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.group.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.core.DefaultGroupId; |
| 23 | import org.onosproject.core.GroupId; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Device; |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.group.DefaultGroupBucket; |
| 30 | import org.onosproject.net.group.Group; |
| 31 | import org.onosproject.net.group.GroupBucket; |
| 32 | import org.onosproject.net.group.GroupBuckets; |
| 33 | import org.onosproject.net.group.GroupDescription; |
| 34 | import org.onosproject.net.group.GroupOperation; |
| 35 | import org.onosproject.net.group.GroupOperations; |
| 36 | import org.onosproject.net.group.GroupProvider; |
| 37 | import org.onosproject.net.group.GroupProviderRegistry; |
| 38 | import org.onosproject.net.group.GroupProviderService; |
| 39 | import org.onosproject.net.provider.AbstractProviderService; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | import org.onosproject.openflow.controller.Dpid; |
| 42 | import org.onosproject.openflow.controller.OpenFlowController; |
| 43 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 44 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 45 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 46 | import org.onosproject.openflow.controller.PacketListener; |
| 47 | import org.onosproject.openflow.controller.RoleState; |
| 48 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 49 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 50 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 51 | import org.projectfloodlight.openflow.protocol.OFGroupMod; |
| 52 | import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode; |
| 53 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
| 54 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 55 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 56 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 57 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 58 | import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg; |
| 59 | import org.projectfloodlight.openflow.types.OFGroup; |
| 60 | |
| 61 | import java.util.Collection; |
| 62 | import java.util.List; |
| 63 | import java.util.Set; |
| 64 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 65 | import static org.junit.Assert.*; |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 66 | |
| 67 | public class OpenFlowGroupProviderTest { |
| 68 | |
| 69 | OpenFlowGroupProvider provider = new OpenFlowGroupProvider(); |
| 70 | private final OpenFlowController controller = new TestController(); |
| 71 | GroupProviderRegistry providerRegistry = new TestGroupProviderRegistry(); |
| 72 | GroupProviderService providerService; |
| 73 | |
| 74 | private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001"); |
| 75 | private Dpid dpid1 = Dpid.dpid(deviceId.uri()); |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() { |
| 79 | provider.controller = controller; |
| 80 | provider.providerRegistry = providerRegistry; |
| 81 | provider.activate(); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void basics() { |
| 86 | assertNotNull("registration expected", providerService); |
| 87 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void addGroup() { |
| 92 | |
| 93 | GroupId groupId = new DefaultGroupId(1); |
| 94 | |
| 95 | List<GroupBucket> bucketList = Lists.newArrayList(); |
| 96 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 97 | builder.setOutput(PortNumber.portNumber(1)); |
| 98 | GroupBucket bucket = |
| 99 | DefaultGroupBucket.createSelectGroupBucket(builder.build()); |
| 100 | bucketList.add(bucket); |
| 101 | GroupBuckets buckets = new GroupBuckets(bucketList); |
| 102 | |
| 103 | List<GroupOperation> operationList = Lists.newArrayList(); |
| 104 | GroupOperation operation = GroupOperation.createAddGroupOperation(groupId, |
| 105 | GroupDescription.Type.SELECT, buckets); |
| 106 | operationList.add(operation); |
| 107 | GroupOperations operations = new GroupOperations(operationList); |
| 108 | |
| 109 | provider.performGroupOperation(deviceId, operations); |
| 110 | |
| 111 | final Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 112 | TestOpenFlowSwitch sw = (TestOpenFlowSwitch) controller.getSwitch(dpid); |
| 113 | assertNotNull("Switch should not be nul", sw); |
| 114 | assertNotNull("OFGroupMsg should not be null", sw.msg); |
| 115 | |
| 116 | } |
| 117 | |
| 118 | |
| 119 | @Test |
| 120 | public void groupModFailure() { |
| 121 | TestOpenFlowGroupProviderService testProviderService = |
| 122 | (TestOpenFlowGroupProviderService) providerService; |
| 123 | |
| 124 | GroupId groupId = new DefaultGroupId(1); |
| 125 | List<GroupBucket> bucketList = Lists.newArrayList(); |
| 126 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 127 | builder.setOutput(PortNumber.portNumber(1)); |
| 128 | GroupBucket bucket = |
| 129 | DefaultGroupBucket.createSelectGroupBucket(builder.build()); |
| 130 | bucketList.add(bucket); |
| 131 | GroupBuckets buckets = new GroupBuckets(bucketList); |
| 132 | List<GroupOperation> operationList = Lists.newArrayList(); |
| 133 | GroupOperation operation = GroupOperation.createAddGroupOperation(groupId, |
| 134 | GroupDescription.Type.SELECT, buckets); |
| 135 | operationList.add(operation); |
| 136 | GroupOperations operations = new GroupOperations(operationList); |
| 137 | |
| 138 | provider.performGroupOperation(deviceId, operations); |
| 139 | |
| 140 | OFGroupModFailedErrorMsg.Builder errorBuilder = |
| 141 | OFFactories.getFactory(OFVersion.OF_13).errorMsgs().buildGroupModFailedErrorMsg(); |
| 142 | OFGroupMod.Builder groupBuilder = OFFactories.getFactory(OFVersion.OF_13).buildGroupModify(); |
| 143 | groupBuilder.setGroupType(OFGroupType.ALL); |
| 144 | groupBuilder.setGroup(OFGroup.of(1)); |
| 145 | errorBuilder.setCode(OFGroupModFailedCode.GROUP_EXISTS); |
| 146 | errorBuilder.setXid(provider.getXidAndAdd(0) - 1); |
| 147 | |
| 148 | controller.processPacket(dpid1, errorBuilder.build()); |
| 149 | |
| 150 | assertNotNull("Operation failed should not be null", |
| 151 | testProviderService.failedOperation); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | @Test |
| 156 | public void groupStatsEvent() { |
| 157 | TestOpenFlowGroupProviderService testProviderService = |
| 158 | (TestOpenFlowGroupProviderService) providerService; |
| 159 | |
| 160 | OFGroupStatsReply.Builder rep1 = |
| 161 | OFFactories.getFactory(OFVersion.OF_13).buildGroupStatsReply(); |
| 162 | rep1.setXid(1); |
| 163 | controller.processPacket(dpid1, rep1.build()); |
| 164 | OFGroupDescStatsReply.Builder rep2 = |
| 165 | OFFactories.getFactory(OFVersion.OF_13).buildGroupDescStatsReply(); |
| 166 | assertNull("group entries is not set yet", testProviderService.getGroupEntries()); |
| 167 | |
| 168 | rep2.setXid(2); |
| 169 | controller.processPacket(dpid1, rep2.build()); |
| 170 | assertNotNull("group entries should be set", testProviderService.getGroupEntries()); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | @After |
| 176 | public void tearDown() { |
| 177 | provider.deactivate(); |
| 178 | provider.providerRegistry = null; |
| 179 | provider.controller = null; |
| 180 | } |
| 181 | |
| 182 | private class TestOpenFlowGroupProviderService |
| 183 | extends AbstractProviderService<GroupProvider> |
| 184 | implements GroupProviderService { |
| 185 | |
| 186 | Collection<Group> groups = null; |
| 187 | GroupOperation failedOperation = null; |
| 188 | |
| 189 | protected TestOpenFlowGroupProviderService(GroupProvider provider) { |
| 190 | super(provider); |
| 191 | } |
| 192 | |
| 193 | @Override |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 194 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 195 | this.failedOperation = operation; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void pushGroupMetrics(DeviceId deviceId, Collection<Group> groupEntries) { |
| 200 | this.groups = groupEntries; |
| 201 | } |
| 202 | |
| 203 | public Collection<Group> getGroupEntries() { |
| 204 | return groups; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private class TestController implements OpenFlowController { |
| 209 | |
| 210 | OpenFlowEventListener eventListener = null; |
| 211 | List<OpenFlowSwitch> switches = Lists.newArrayList(); |
| 212 | |
| 213 | public TestController() { |
| 214 | OpenFlowSwitch testSwitch = new TestOpenFlowSwitch(); |
| 215 | switches.add(testSwitch); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void addListener(OpenFlowSwitchListener listener) { |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void removeListener(OpenFlowSwitchListener listener) { |
| 224 | |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void addPacketListener(int priority, PacketListener listener) { |
| 229 | |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void removePacketListener(PacketListener listener) { |
| 234 | |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void addEventListener(OpenFlowEventListener listener) { |
| 239 | this.eventListener = listener; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void removeEventListener(OpenFlowEventListener listener) { |
| 244 | |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void write(Dpid dpid, OFMessage msg) { |
| 249 | |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void processPacket(Dpid dpid, OFMessage msg) { |
| 254 | eventListener.handleMessage(dpid, msg); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void setRole(Dpid dpid, RoleState role) { |
| 259 | |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 264 | return switches; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 269 | return null; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 274 | return null; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 279 | return switches.get(0); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 284 | return null; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 289 | return null; |
| 290 | } |
| 291 | |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 292 | @Override |
| 293 | public void monitorAllEvents(boolean monitor) { |
| 294 | } |
| 295 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private class TestGroupProviderRegistry implements GroupProviderRegistry { |
| 299 | |
| 300 | @Override |
| 301 | public GroupProviderService register(GroupProvider provider) { |
| 302 | providerService = new TestOpenFlowGroupProviderService(provider); |
| 303 | return providerService; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void unregister(GroupProvider provider) { |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public Set<ProviderId> getProviders() { |
| 312 | return null; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private class TestOpenFlowSwitch implements OpenFlowSwitch { |
| 317 | |
| 318 | OFMessage msg = null; |
| 319 | |
| 320 | @Override |
| 321 | public void sendMsg(OFMessage msg) { |
| 322 | this.msg = msg; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void sendMsg(List<OFMessage> msgs) { |
| 327 | |
| 328 | } |
| 329 | |
| 330 | @Override |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 331 | public void handleMessage(OFMessage fromSwitch) { |
| 332 | |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public void setRole(RoleState role) { |
| 337 | |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public RoleState getRole() { |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public List<OFPortDesc> getPorts() { |
| 347 | return null; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public OFFactory factory() { |
| 352 | return OFFactories.getFactory(OFVersion.OF_13); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public String getStringId() { |
| 357 | return null; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public long getId() { |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public String manufacturerDescription() { |
| 367 | return null; |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public String datapathDescription() { |
| 372 | return null; |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public String hardwareDescription() { |
| 377 | return null; |
| 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public String softwareDescription() { |
| 382 | return null; |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public String serialNumber() { |
| 387 | return null; |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public boolean isConnected() { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void disconnectSwitch() { |
| 397 | |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public void returnRoleReply(RoleState requested, RoleState response) { |
| 402 | |
| 403 | } |
| 404 | |
| 405 | @Override |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 406 | public Device.Type deviceType() { |
| 407 | return Device.Type.SWITCH; |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public String channelId() { |
| 412 | return null; |
| 413 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 414 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 415 | @Override |
| 416 | public void addEventListener(OpenFlowEventListener listener) { |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void removeEventListener(OpenFlowEventListener listener) { |
| 421 | } |
| 422 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 423 | } |
| 424 | } |