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