Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.net.intent.impl.installer; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.net.NetworkResource; |
| 27 | import org.onosproject.net.flow.DefaultFlowRule; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.FlowRuleOperations; |
| 32 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.intent.FlowRuleIntent; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentData; |
| 38 | import org.onosproject.net.intent.IntentInstallationContext; |
| 39 | import org.onosproject.net.intent.IntentOperationContext; |
| 40 | import org.onosproject.net.intent.IntentState; |
| 41 | import org.onosproject.net.intent.PathIntent; |
| 42 | import org.onosproject.store.service.WallClockTimestamp; |
| 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 47 | import java.util.stream.Collectors; |
| 48 | |
| 49 | import static org.junit.Assert.*; |
| 50 | |
| 51 | /** |
| 52 | * Tests for flow rule Intent installer. |
| 53 | */ |
| 54 | public class FlowRuleIntentInstallerTest extends AbstractIntentInstallerTest { |
| 55 | |
| 56 | private TestFlowRuleService flowRuleService; |
| 57 | private FlowRuleIntentInstaller installer; |
| 58 | |
| 59 | @Before |
| 60 | public void setup() { |
| 61 | super.setup(); |
| 62 | flowRuleService = new TestFlowRuleService(); |
| 63 | installer = new FlowRuleIntentInstaller(); |
| 64 | installer.flowRuleService = flowRuleService; |
| 65 | installer.intentExtensionService = intentExtensionService; |
| 66 | installer.intentInstallCoordinator = intentInstallCoordinator; |
| 67 | installer.trackerService = trackerService; |
| 68 | |
| 69 | installer.activate(); |
| 70 | } |
| 71 | |
| 72 | @After |
| 73 | public void tearDown() { |
| 74 | super.tearDown(); |
| 75 | installer.deactivated(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Installs Intents only, no Intents to be uninstall. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testInstallOnly() { |
| 83 | List<Intent> intentsToUninstall = Lists.newArrayList(); |
| 84 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 85 | |
| 86 | IntentData toUninstall = null; |
| 87 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 88 | IntentState.INSTALLING, |
| 89 | new WallClockTimestamp()); |
| 90 | toInstall = new IntentData(toInstall, intentsToInstall); |
| 91 | |
| 92 | |
| 93 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 94 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 95 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 96 | |
| 97 | installer.apply(operationContext); |
| 98 | |
| 99 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 100 | assertEquals(successContext, operationContext); |
| 101 | |
| 102 | Set<FlowRule> expectedFlowRules = intentsToInstall.stream() |
| 103 | .map(intent -> (FlowRuleIntent) intent) |
| 104 | .map(FlowRuleIntent::flowRules) |
| 105 | .flatMap(Collection::stream) |
| 106 | .collect(Collectors.toSet()); |
| 107 | |
| 108 | assertEquals(expectedFlowRules, flowRuleService.flowRulesAdd); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Uninstalls Intents only, no Intents to be install. |
| 113 | */ |
| 114 | @Test |
| 115 | public void testUninstallOnly() { |
| 116 | List<Intent> intentsToInstall = Lists.newArrayList(); |
| 117 | List<Intent> intentsToUninstall = createFlowRuleIntents(); |
| 118 | |
| 119 | IntentData toInstall = null; |
| 120 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 121 | IntentState.WITHDRAWING, |
| 122 | new WallClockTimestamp()); |
| 123 | toUninstall = new IntentData(toUninstall, intentsToUninstall); |
| 124 | |
| 125 | |
| 126 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 127 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 128 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 129 | |
| 130 | installer.apply(operationContext); |
| 131 | |
| 132 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 133 | assertEquals(successContext, operationContext); |
| 134 | |
| 135 | Set<FlowRule> expectedFlowRules = intentsToUninstall.stream() |
| 136 | .map(intent -> (FlowRuleIntent) intent) |
| 137 | .map(FlowRuleIntent::flowRules) |
| 138 | .flatMap(Collection::stream) |
| 139 | .collect(Collectors.toSet()); |
| 140 | |
| 141 | assertEquals(expectedFlowRules, flowRuleService.flowRulesRemove); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Do both install and uninstall Intents with different flow rules. |
| 146 | */ |
| 147 | @Test |
| 148 | public void testUninstallAndInstall() { |
| 149 | List<Intent> intentsToInstall = createAnotherFlowRuleIntents(); |
| 150 | List<Intent> intentsToUninstall = createFlowRuleIntents(); |
| 151 | |
| 152 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 153 | IntentState.INSTALLING, |
| 154 | new WallClockTimestamp()); |
| 155 | toInstall = new IntentData(toInstall, intentsToInstall); |
| 156 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 157 | IntentState.INSTALLED, |
| 158 | new WallClockTimestamp()); |
| 159 | toUninstall = new IntentData(toUninstall, intentsToUninstall); |
| 160 | |
| 161 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 162 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 163 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 164 | |
| 165 | installer.apply(operationContext); |
| 166 | |
| 167 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 168 | assertEquals(successContext, operationContext); |
| 169 | |
| 170 | Set<FlowRule> expectedFlowRules = intentsToUninstall.stream() |
| 171 | .map(intent -> (FlowRuleIntent) intent) |
| 172 | .map(FlowRuleIntent::flowRules) |
| 173 | .flatMap(Collection::stream) |
| 174 | .collect(Collectors.toSet()); |
| 175 | |
| 176 | assertEquals(expectedFlowRules, flowRuleService.flowRulesRemove); |
| 177 | |
| 178 | expectedFlowRules = intentsToInstall.stream() |
| 179 | .map(intent -> (FlowRuleIntent) intent) |
| 180 | .map(FlowRuleIntent::flowRules) |
| 181 | .flatMap(Collection::stream) |
| 182 | .collect(Collectors.toSet()); |
| 183 | |
| 184 | assertEquals(expectedFlowRules, flowRuleService.flowRulesAdd); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Do both install and uninstall Intents with same flow rules. |
| 189 | */ |
| 190 | @Test |
| 191 | public void testUninstallAndInstallUnchanged() { |
| 192 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 193 | List<Intent> intentsToUninstall = createFlowRuleIntents(); |
| 194 | |
| 195 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 196 | IntentState.INSTALLING, |
| 197 | new WallClockTimestamp()); |
| 198 | toInstall = new IntentData(toInstall, intentsToInstall); |
| 199 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 200 | IntentState.INSTALLED, |
| 201 | new WallClockTimestamp()); |
| 202 | toUninstall = new IntentData(toUninstall, intentsToUninstall); |
| 203 | |
| 204 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 205 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 206 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 207 | |
| 208 | installer.apply(operationContext); |
| 209 | |
| 210 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 211 | assertEquals(successContext, operationContext); |
| 212 | |
| 213 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 214 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Do both install and uninstall Intents with same flow rule Intent. |
| 219 | */ |
| 220 | @Test |
| 221 | public void testUninstallAndInstallSame() { |
| 222 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 223 | List<Intent> intentsToUninstall = intentsToInstall; |
| 224 | |
| 225 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 226 | IntentState.INSTALLING, |
| 227 | new WallClockTimestamp()); |
| 228 | toInstall = new IntentData(toInstall, intentsToInstall); |
| 229 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 230 | IntentState.INSTALLED, |
| 231 | new WallClockTimestamp()); |
| 232 | toUninstall = new IntentData(toUninstall, intentsToUninstall); |
| 233 | |
| 234 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 235 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 236 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 237 | |
| 238 | installer.apply(operationContext); |
| 239 | |
| 240 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 241 | assertEquals(successContext, operationContext); |
| 242 | |
| 243 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 244 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Nothing to uninstall or install. |
| 249 | */ |
| 250 | @Test |
| 251 | public void testNoAnyIntentToApply() { |
| 252 | IntentData toInstall = null; |
| 253 | IntentData toUninstall = null; |
| 254 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 255 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 256 | operationContext = new IntentOperationContext<>(ImmutableList.of(), ImmutableList.of(), context); |
| 257 | installer.apply(operationContext); |
| 258 | |
| 259 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 260 | assertEquals(successContext, operationContext); |
| 261 | |
| 262 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 263 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Test if the flow installation failed. |
| 268 | */ |
| 269 | @Test |
| 270 | public void testFailed() { |
| 271 | installer.flowRuleService = new TestFailedFlowRuleService(); |
| 272 | List<Intent> intentsToUninstall = Lists.newArrayList(); |
| 273 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 274 | |
| 275 | IntentData toUninstall = null; |
| 276 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 277 | IntentState.INSTALLING, |
| 278 | new WallClockTimestamp()); |
| 279 | toInstall = new IntentData(toInstall, intentsToInstall); |
| 280 | |
| 281 | |
| 282 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 283 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 284 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 285 | |
| 286 | installer.apply(operationContext); |
| 287 | |
| 288 | IntentOperationContext failedContext = intentInstallCoordinator.failedContext; |
| 289 | assertEquals(failedContext, operationContext); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Generates FlowRuleIntents for test. |
| 294 | * |
| 295 | * @return the FlowRuleIntents for test |
| 296 | */ |
| 297 | public List<Intent> createFlowRuleIntents() { |
| 298 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 299 | .matchInPhyPort(CP1.port()) |
| 300 | .build(); |
| 301 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 302 | .setOutput(CP2.port()) |
| 303 | .build(); |
| 304 | |
| 305 | FlowRule flowRule = DefaultFlowRule.builder() |
| 306 | .forDevice(CP1.deviceId()) |
| 307 | .withSelector(selector) |
| 308 | .withTreatment(treatment) |
| 309 | .fromApp(APP_ID) |
| 310 | .withPriority(DEFAULT_PRIORITY) |
| 311 | .makePermanent() |
| 312 | .build(); |
| 313 | |
| 314 | List<NetworkResource> resources = ImmutableList.of(CP1.deviceId()); |
| 315 | |
| 316 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 317 | KEY1, |
| 318 | ImmutableList.of(flowRule), |
| 319 | resources, |
| 320 | PathIntent.ProtectionType.PRIMARY, |
| 321 | RG1); |
| 322 | |
| 323 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 324 | flowRuleIntents.add(intent); |
| 325 | |
| 326 | return flowRuleIntents; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Generates another different FlowRuleIntents for test. |
| 331 | * |
| 332 | * @return the FlowRuleIntents for test |
| 333 | */ |
| 334 | public List<Intent> createAnotherFlowRuleIntents() { |
| 335 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 336 | .matchVlanId(VlanId.vlanId("100")) |
| 337 | .matchInPhyPort(CP1.port()) |
| 338 | .build(); |
| 339 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 340 | .setOutput(CP2.port()) |
| 341 | .build(); |
| 342 | |
| 343 | FlowRule flowRule = DefaultFlowRule.builder() |
| 344 | .forDevice(CP1.deviceId()) |
| 345 | .withSelector(selector) |
| 346 | .withTreatment(treatment) |
| 347 | .fromApp(APP_ID) |
| 348 | .withPriority(DEFAULT_PRIORITY) |
| 349 | .makePermanent() |
| 350 | .build(); |
| 351 | |
| 352 | List<NetworkResource> resources = ImmutableList.of(CP1.deviceId()); |
| 353 | |
| 354 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 355 | KEY1, |
| 356 | ImmutableList.of(flowRule), |
| 357 | resources, |
| 358 | PathIntent.ProtectionType.PRIMARY, |
| 359 | RG1); |
| 360 | |
| 361 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 362 | flowRuleIntents.add(intent); |
| 363 | |
| 364 | return flowRuleIntents; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * The FlowRuleService for test; always success for any flow rule operations. |
| 369 | */ |
| 370 | class TestFlowRuleService extends FlowRuleServiceAdapter { |
| 371 | |
| 372 | Set<FlowRule> flowRulesAdd = Sets.newHashSet(); |
| 373 | Set<FlowRule> flowRulesRemove = Sets.newHashSet(); |
| 374 | |
| 375 | public void record(FlowRuleOperations ops) { |
| 376 | flowRulesAdd.clear(); |
| 377 | flowRulesRemove.clear(); |
| 378 | ops.stages().forEach(stage -> { |
| 379 | stage.forEach(op -> { |
| 380 | switch (op.type()) { |
| 381 | case ADD: |
| 382 | flowRulesAdd.add(op.rule()); |
| 383 | break; |
| 384 | case REMOVE: |
| 385 | flowRulesRemove.add(op.rule()); |
| 386 | break; |
| 387 | default: |
| 388 | break; |
| 389 | } |
| 390 | }); |
| 391 | }); |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public void apply(FlowRuleOperations ops) { |
| 396 | record(ops); |
| 397 | ops.callback().onSuccess(ops); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * The FlowRuleService for test; always failed for any flow rule operations. |
| 403 | */ |
| 404 | class TestFailedFlowRuleService extends TestFlowRuleService { |
| 405 | @Override |
| 406 | public void apply(FlowRuleOperations ops) { |
| 407 | record(ops); |
| 408 | ops.callback().onError(ops); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | } |