Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -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 | */ |
| 16 | |
| 17 | package org.onosproject.net.intent.impl.installer; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.VlanId; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigService; |
| 28 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 29 | import org.onosproject.net.NetworkResource; |
| 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.FlowRule; |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.FlowRuleOperation; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.FlowRuleOperations; |
| 36 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.intent.FlowRuleIntent; |
| 40 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.IntentData; |
| 42 | import org.onosproject.net.intent.IntentInstallationContext; |
| 43 | import org.onosproject.net.intent.IntentOperationContext; |
| 44 | import org.onosproject.net.intent.IntentState; |
| 45 | import org.onosproject.net.intent.PathIntent; |
| 46 | import org.onosproject.store.service.WallClockTimestamp; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 47 | import org.onosproject.store.trivial.SimpleIntentStore; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 48 | |
| 49 | import java.util.Collection; |
| 50 | import java.util.List; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 51 | import java.util.Map; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | import java.util.stream.Collectors; |
| 54 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 55 | import static org.easymock.EasyMock.mock; |
| 56 | import static org.junit.Assert.assertEquals; |
| 57 | import static org.junit.Assert.assertTrue; |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 58 | import static org.onosproject.net.flow.FlowRuleOperation.Type.ADD; |
| 59 | import static org.onosproject.net.flow.FlowRuleOperation.Type.REMOVE; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Tests for flow rule Intent installer. |
| 63 | */ |
| 64 | public class FlowRuleIntentInstallerTest extends AbstractIntentInstallerTest { |
| 65 | |
| 66 | private TestFlowRuleService flowRuleService; |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 67 | private final TestFlowRuleServiceNonDisruptive flowRuleServiceNonDisruptive = |
| 68 | new TestFlowRuleServiceNonDisruptive(); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 69 | private FlowRuleIntentInstaller installer; |
| 70 | |
| 71 | @Before |
| 72 | public void setup() { |
| 73 | super.setup(); |
| 74 | flowRuleService = new TestFlowRuleService(); |
| 75 | installer = new FlowRuleIntentInstaller(); |
| 76 | installer.flowRuleService = flowRuleService; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 77 | installer.store = new SimpleIntentStore(); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 78 | installer.intentExtensionService = intentExtensionService; |
| 79 | installer.intentInstallCoordinator = intentInstallCoordinator; |
| 80 | installer.trackerService = trackerService; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 81 | installer.configService = mock(ComponentConfigService.class); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 82 | |
| 83 | installer.activate(); |
| 84 | } |
| 85 | |
| 86 | @After |
| 87 | public void tearDown() { |
| 88 | super.tearDown(); |
| 89 | installer.deactivated(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Installs Intents only, no Intents to be uninstall. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testInstallOnly() { |
| 97 | List<Intent> intentsToUninstall = Lists.newArrayList(); |
| 98 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 99 | |
| 100 | IntentData toUninstall = null; |
| 101 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 102 | IntentState.INSTALLING, |
| 103 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 104 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 108 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 109 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 110 | |
| 111 | installer.apply(operationContext); |
| 112 | |
| 113 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 114 | assertEquals(successContext, operationContext); |
| 115 | |
| 116 | Set<FlowRule> expectedFlowRules = intentsToInstall.stream() |
| 117 | .map(intent -> (FlowRuleIntent) intent) |
| 118 | .map(FlowRuleIntent::flowRules) |
| 119 | .flatMap(Collection::stream) |
| 120 | .collect(Collectors.toSet()); |
| 121 | |
| 122 | assertEquals(expectedFlowRules, flowRuleService.flowRulesAdd); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Uninstalls Intents only, no Intents to be install. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testUninstallOnly() { |
| 130 | List<Intent> intentsToInstall = Lists.newArrayList(); |
| 131 | List<Intent> intentsToUninstall = createFlowRuleIntents(); |
| 132 | |
| 133 | IntentData toInstall = null; |
| 134 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 135 | IntentState.WITHDRAWING, |
| 136 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 137 | toUninstall = IntentData.compiled(toUninstall, intentsToUninstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 138 | |
| 139 | |
| 140 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 141 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 142 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 143 | |
| 144 | installer.apply(operationContext); |
| 145 | |
| 146 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 147 | assertEquals(successContext, operationContext); |
| 148 | |
| 149 | Set<FlowRule> expectedFlowRules = intentsToUninstall.stream() |
| 150 | .map(intent -> (FlowRuleIntent) intent) |
| 151 | .map(FlowRuleIntent::flowRules) |
| 152 | .flatMap(Collection::stream) |
| 153 | .collect(Collectors.toSet()); |
| 154 | |
| 155 | assertEquals(expectedFlowRules, flowRuleService.flowRulesRemove); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Do both install and uninstall Intents with different flow rules. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testUninstallAndInstall() { |
| 163 | List<Intent> intentsToInstall = createAnotherFlowRuleIntents(); |
| 164 | List<Intent> intentsToUninstall = createFlowRuleIntents(); |
| 165 | |
| 166 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 167 | IntentState.INSTALLING, |
| 168 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 169 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 170 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 171 | IntentState.INSTALLED, |
| 172 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 173 | toUninstall = IntentData.compiled(toUninstall, intentsToUninstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 174 | |
| 175 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 176 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 177 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 178 | |
| 179 | installer.apply(operationContext); |
| 180 | |
| 181 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 182 | assertEquals(successContext, operationContext); |
| 183 | |
| 184 | Set<FlowRule> expectedFlowRules = intentsToUninstall.stream() |
| 185 | .map(intent -> (FlowRuleIntent) intent) |
| 186 | .map(FlowRuleIntent::flowRules) |
| 187 | .flatMap(Collection::stream) |
| 188 | .collect(Collectors.toSet()); |
| 189 | |
| 190 | assertEquals(expectedFlowRules, flowRuleService.flowRulesRemove); |
| 191 | |
| 192 | expectedFlowRules = intentsToInstall.stream() |
| 193 | .map(intent -> (FlowRuleIntent) intent) |
| 194 | .map(FlowRuleIntent::flowRules) |
| 195 | .flatMap(Collection::stream) |
| 196 | .collect(Collectors.toSet()); |
| 197 | |
| 198 | assertEquals(expectedFlowRules, flowRuleService.flowRulesAdd); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Do both install and uninstall Intents with same flow rules. |
| 203 | */ |
| 204 | @Test |
| 205 | public void testUninstallAndInstallUnchanged() { |
| 206 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 207 | List<Intent> intentsToUninstall = createFlowRuleIntents(); |
| 208 | |
| 209 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 210 | IntentState.INSTALLING, |
| 211 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 212 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 213 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 214 | IntentState.INSTALLED, |
| 215 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 216 | toUninstall = IntentData.compiled(toUninstall, intentsToUninstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 217 | |
| 218 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 219 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 220 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 221 | |
| 222 | installer.apply(operationContext); |
| 223 | |
| 224 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 225 | assertEquals(successContext, operationContext); |
| 226 | |
| 227 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 228 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 229 | assertEquals(0, flowRuleService.flowRulesModify.size()); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Do both install and uninstall Intents with same flow rule Intent. |
| 234 | */ |
| 235 | @Test |
| 236 | public void testUninstallAndInstallSame() { |
| 237 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 238 | List<Intent> intentsToUninstall = intentsToInstall; |
| 239 | |
| 240 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 241 | IntentState.INSTALLING, |
| 242 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 243 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 244 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 245 | IntentState.INSTALLED, |
| 246 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 247 | toUninstall = IntentData.compiled(toUninstall, intentsToUninstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 248 | |
| 249 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 250 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 251 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 252 | |
| 253 | installer.apply(operationContext); |
| 254 | |
| 255 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 256 | assertEquals(successContext, operationContext); |
| 257 | |
| 258 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 259 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 260 | assertEquals(0, flowRuleService.flowRulesModify.size()); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Nothing to uninstall or install. |
| 265 | */ |
| 266 | @Test |
| 267 | public void testNoAnyIntentToApply() { |
| 268 | IntentData toInstall = null; |
| 269 | IntentData toUninstall = null; |
| 270 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 271 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 272 | operationContext = new IntentOperationContext<>(ImmutableList.of(), ImmutableList.of(), context); |
| 273 | installer.apply(operationContext); |
| 274 | |
| 275 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 276 | assertEquals(successContext, operationContext); |
| 277 | |
| 278 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 279 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 280 | assertEquals(0, flowRuleService.flowRulesModify.size()); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Test if the flow installation failed. |
| 285 | */ |
| 286 | @Test |
| 287 | public void testFailed() { |
| 288 | installer.flowRuleService = new TestFailedFlowRuleService(); |
| 289 | List<Intent> intentsToUninstall = Lists.newArrayList(); |
| 290 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 291 | |
| 292 | IntentData toUninstall = null; |
| 293 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 294 | IntentState.INSTALLING, |
| 295 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 296 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 297 | |
| 298 | |
| 299 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 300 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 301 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 302 | |
| 303 | installer.apply(operationContext); |
| 304 | |
| 305 | IntentOperationContext failedContext = intentInstallCoordinator.failedContext; |
| 306 | assertEquals(failedContext, operationContext); |
| 307 | } |
| 308 | |
| 309 | /** |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 310 | * Test intents with same match rules, should do modify instead of add. |
| 311 | */ |
| 312 | @Test |
| 313 | public void testRuleModify() { |
| 314 | List<Intent> intentsToInstall = createFlowRuleIntents(); |
| 315 | List<Intent> intentsToUninstall = createFlowRuleIntentsWithSameMatch(); |
| 316 | |
| 317 | IntentData toInstall = new IntentData(createP2PIntent(), |
| 318 | IntentState.INSTALLING, |
| 319 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 320 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 321 | IntentData toUninstall = new IntentData(createP2PIntent(), |
| 322 | IntentState.INSTALLED, |
| 323 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 324 | toUninstall = IntentData.compiled(toUninstall, intentsToUninstall); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 325 | |
| 326 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 327 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 328 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 329 | |
| 330 | installer.apply(operationContext); |
| 331 | |
| 332 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 333 | assertEquals(successContext, operationContext); |
| 334 | |
| 335 | assertEquals(0, flowRuleService.flowRulesRemove.size()); |
| 336 | assertEquals(0, flowRuleService.flowRulesAdd.size()); |
| 337 | assertEquals(1, flowRuleService.flowRulesModify.size()); |
| 338 | |
| 339 | FlowRuleIntent installedIntent = (FlowRuleIntent) intentsToInstall.get(0); |
| 340 | assertEquals(flowRuleService.flowRulesModify.size(), installedIntent.flowRules().size()); |
| 341 | assertTrue(flowRuleService.flowRulesModify.containsAll(installedIntent.flowRules())); |
| 342 | } |
| 343 | |
| 344 | /** |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 345 | * Testing the non-disruptive reallocation. |
| 346 | */ |
| 347 | @Test |
| 348 | public void testUninstallAndInstallNonDisruptive() throws InterruptedException { |
| 349 | |
| 350 | installer.flowRuleService = flowRuleServiceNonDisruptive; |
| 351 | |
| 352 | List<Intent> intentsToInstall = createAnotherFlowRuleIntentsNonDisruptive(); |
| 353 | List<Intent> intentsToUninstall = createFlowRuleIntentsNonDisruptive(); |
| 354 | |
| 355 | IntentData toInstall = new IntentData(createP2PIntentNonDisruptive(), |
| 356 | IntentState.INSTALLING, |
| 357 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 358 | toInstall = IntentData.compiled(toInstall, intentsToInstall); |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 359 | IntentData toUninstall = new IntentData(createP2PIntentNonDisruptive(), |
| 360 | IntentState.INSTALLED, |
| 361 | new WallClockTimestamp()); |
Ray Milkey | e1077f8 | 2019-02-20 09:31:12 -0800 | [diff] [blame^] | 362 | toUninstall = IntentData.compiled(toUninstall, intentsToUninstall); |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 363 | |
| 364 | IntentOperationContext<FlowRuleIntent> operationContext; |
| 365 | IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall); |
| 366 | operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context); |
| 367 | |
| 368 | installer.apply(operationContext); |
| 369 | |
| 370 | //A single FlowRule is evaluated for every non-disruptive stage |
| 371 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 372 | .matchInPhyPort(CP1.port()) |
| 373 | .build(); |
| 374 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 375 | .setOutput(CP3.port()) |
| 376 | .build(); |
| 377 | |
| 378 | FlowRule firstStageInstalledRule = DefaultFlowRule.builder() |
| 379 | .forDevice(CP1.deviceId()) |
| 380 | .withSelector(selector) |
| 381 | .withTreatment(treatment) |
| 382 | .fromApp(APP_ID) |
| 383 | .withPriority(DEFAULT_PRIORITY - 1) |
| 384 | .makePermanent() |
| 385 | .build(); |
| 386 | |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 387 | // We need to wait a bit in order to avoid |
| 388 | // race conditions and failing builds |
| 389 | synchronized (flowRuleServiceNonDisruptive) { |
| 390 | while (!verifyFlowRule(ADD, firstStageInstalledRule)) { |
| 391 | flowRuleServiceNonDisruptive.wait(); |
| 392 | } |
| 393 | } |
| 394 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 395 | assertTrue(flowRuleServiceNonDisruptive.flowRulesAdd.contains(firstStageInstalledRule)); |
| 396 | |
| 397 | selector = DefaultTrafficSelector.builder() |
| 398 | .matchInPhyPort(CP4_2.port()) |
| 399 | .build(); |
| 400 | treatment = DefaultTrafficTreatment.builder() |
| 401 | .setOutput(CP4_1.port()) |
| 402 | .build(); |
| 403 | |
| 404 | FlowRule secondStageUninstalledRule = DefaultFlowRule.builder() |
| 405 | .forDevice(CP4_1.deviceId()) |
| 406 | .withSelector(selector) |
| 407 | .withTreatment(treatment) |
| 408 | .fromApp(APP_ID) |
| 409 | .withPriority(DEFAULT_PRIORITY) |
| 410 | .makePermanent() |
| 411 | .build(); |
| 412 | |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 413 | synchronized (flowRuleServiceNonDisruptive) { |
| 414 | while (!verifyFlowRule(REMOVE, secondStageUninstalledRule)) { |
| 415 | flowRuleServiceNonDisruptive.wait(); |
| 416 | } |
| 417 | } |
| 418 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 419 | assertTrue(flowRuleServiceNonDisruptive.flowRulesRemove.contains(secondStageUninstalledRule)); |
| 420 | |
| 421 | selector = DefaultTrafficSelector.builder() |
| 422 | .matchInPhyPort(CP4_3.port()) |
| 423 | .build(); |
| 424 | treatment = DefaultTrafficTreatment.builder() |
| 425 | .setOutput(CP4_1.port()) |
| 426 | .build(); |
| 427 | |
| 428 | FlowRule thirdStageInstalledRule = DefaultFlowRule.builder() |
| 429 | .forDevice(CP4_1.deviceId()) |
| 430 | .withSelector(selector) |
| 431 | .withTreatment(treatment) |
| 432 | .fromApp(APP_ID) |
| 433 | .withPriority(DEFAULT_PRIORITY) |
| 434 | .makePermanent() |
| 435 | .build(); |
| 436 | |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 437 | synchronized (flowRuleServiceNonDisruptive) { |
| 438 | while (!verifyFlowRule(ADD, thirdStageInstalledRule)) { |
| 439 | flowRuleServiceNonDisruptive.wait(); |
| 440 | } |
| 441 | } |
| 442 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 443 | assertTrue(flowRuleServiceNonDisruptive.flowRulesAdd.contains(thirdStageInstalledRule)); |
| 444 | |
| 445 | selector = DefaultTrafficSelector.builder() |
| 446 | .matchInPhyPort(CP2_1.port()) |
| 447 | .build(); |
| 448 | treatment = DefaultTrafficTreatment.builder() |
| 449 | .setOutput(CP2_2.port()) |
| 450 | .build(); |
| 451 | |
| 452 | FlowRule lastStageUninstalledRule = DefaultFlowRule.builder() |
| 453 | .forDevice(CP2_1.deviceId()) |
| 454 | .withSelector(selector) |
| 455 | .withTreatment(treatment) |
| 456 | .fromApp(APP_ID) |
| 457 | .withPriority(DEFAULT_PRIORITY) |
| 458 | .makePermanent() |
| 459 | .build(); |
| 460 | |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 461 | synchronized (flowRuleServiceNonDisruptive) { |
| 462 | while (!verifyFlowRule(REMOVE, lastStageUninstalledRule)) { |
| 463 | flowRuleServiceNonDisruptive.wait(); |
| 464 | } |
| 465 | } |
| 466 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 467 | assertTrue(flowRuleServiceNonDisruptive.flowRulesRemove.contains(lastStageUninstalledRule)); |
| 468 | |
| 469 | IntentOperationContext successContext = intentInstallCoordinator.successContext; |
| 470 | assertEquals(successContext, operationContext); |
| 471 | } |
| 472 | |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 473 | private boolean verifyFlowRule(FlowRuleOperation.Type type, FlowRule flowRule) { |
| 474 | return type == ADD ? flowRuleServiceNonDisruptive.flowRulesAdd.contains(flowRule) : |
| 475 | flowRuleServiceNonDisruptive.flowRulesRemove.contains(flowRule); |
| 476 | } |
| 477 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 478 | /** |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 479 | * Generates FlowRuleIntents for test. |
| 480 | * |
| 481 | * @return the FlowRuleIntents for test |
| 482 | */ |
| 483 | public List<Intent> createFlowRuleIntents() { |
| 484 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 485 | .matchInPhyPort(CP1.port()) |
| 486 | .build(); |
| 487 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 488 | .setOutput(CP2.port()) |
| 489 | .build(); |
| 490 | |
| 491 | FlowRule flowRule = DefaultFlowRule.builder() |
| 492 | .forDevice(CP1.deviceId()) |
| 493 | .withSelector(selector) |
| 494 | .withTreatment(treatment) |
| 495 | .fromApp(APP_ID) |
| 496 | .withPriority(DEFAULT_PRIORITY) |
| 497 | .makePermanent() |
| 498 | .build(); |
| 499 | |
| 500 | List<NetworkResource> resources = ImmutableList.of(CP1.deviceId()); |
| 501 | |
| 502 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 503 | KEY1, |
| 504 | ImmutableList.of(flowRule), |
| 505 | resources, |
| 506 | PathIntent.ProtectionType.PRIMARY, |
| 507 | RG1); |
| 508 | |
| 509 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 510 | flowRuleIntents.add(intent); |
| 511 | |
| 512 | return flowRuleIntents; |
| 513 | } |
| 514 | |
| 515 | /** |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 516 | * Generates FlowRuleIntents for test. Flow rules in Intent should have same |
| 517 | * match as we created by createFlowRuleIntents method, but action will be |
| 518 | * different. |
| 519 | * |
| 520 | * @return the FlowRuleIntents for test |
| 521 | */ |
| 522 | public List<Intent> createFlowRuleIntentsWithSameMatch() { |
| 523 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 524 | .matchInPhyPort(CP1.port()) |
| 525 | .build(); |
| 526 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 527 | .punt() |
| 528 | .build(); |
| 529 | |
| 530 | FlowRule flowRule = DefaultFlowRule.builder() |
| 531 | .forDevice(CP1.deviceId()) |
| 532 | .withSelector(selector) |
| 533 | .withTreatment(treatment) |
| 534 | .fromApp(APP_ID) |
| 535 | .withPriority(DEFAULT_PRIORITY) |
| 536 | .makePermanent() |
| 537 | .build(); |
| 538 | |
| 539 | List<NetworkResource> resources = ImmutableList.of(CP1.deviceId()); |
| 540 | |
| 541 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 542 | KEY1, |
| 543 | ImmutableList.of(flowRule), |
| 544 | resources, |
| 545 | PathIntent.ProtectionType.PRIMARY, |
| 546 | RG1); |
| 547 | |
| 548 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 549 | flowRuleIntents.add(intent); |
| 550 | |
| 551 | return flowRuleIntents; |
| 552 | } |
| 553 | |
| 554 | /** |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 555 | * Generates another different FlowRuleIntents for test. |
| 556 | * |
| 557 | * @return the FlowRuleIntents for test |
| 558 | */ |
| 559 | public List<Intent> createAnotherFlowRuleIntents() { |
| 560 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 561 | .matchVlanId(VlanId.vlanId("100")) |
| 562 | .matchInPhyPort(CP1.port()) |
| 563 | .build(); |
| 564 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 565 | .setOutput(CP2.port()) |
| 566 | .build(); |
| 567 | |
| 568 | FlowRule flowRule = DefaultFlowRule.builder() |
| 569 | .forDevice(CP1.deviceId()) |
| 570 | .withSelector(selector) |
| 571 | .withTreatment(treatment) |
| 572 | .fromApp(APP_ID) |
| 573 | .withPriority(DEFAULT_PRIORITY) |
| 574 | .makePermanent() |
| 575 | .build(); |
| 576 | |
| 577 | List<NetworkResource> resources = ImmutableList.of(CP1.deviceId()); |
| 578 | |
| 579 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 580 | KEY1, |
| 581 | ImmutableList.of(flowRule), |
| 582 | resources, |
| 583 | PathIntent.ProtectionType.PRIMARY, |
| 584 | RG1); |
| 585 | |
| 586 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 587 | flowRuleIntents.add(intent); |
| 588 | |
| 589 | return flowRuleIntents; |
| 590 | } |
| 591 | |
| 592 | /** |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 593 | * Generates FlowRuleIntents for testing non-disruptive reallocation. |
| 594 | * |
| 595 | * @return the FlowRuleIntents for test |
| 596 | */ |
| 597 | public List<Intent> createFlowRuleIntentsNonDisruptive() { |
| 598 | |
| 599 | Map<ConnectPoint, ConnectPoint> portsAssociation = Maps.newHashMap(); |
| 600 | portsAssociation.put(CP1, CP2); |
| 601 | portsAssociation.put(CP2_1, CP2_2); |
| 602 | portsAssociation.put(CP4_2, CP4_1); |
| 603 | |
| 604 | List<FlowRule> flowRules = Lists.newArrayList(); |
| 605 | |
| 606 | for (ConnectPoint srcPoint : portsAssociation.keySet()) { |
| 607 | |
| 608 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 609 | .matchInPhyPort(srcPoint.port()) |
| 610 | .build(); |
| 611 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 612 | .setOutput(portsAssociation.get(srcPoint).port()) |
| 613 | .build(); |
| 614 | |
| 615 | FlowRule flowRule = DefaultFlowRule.builder() |
| 616 | .forDevice(srcPoint.deviceId()) |
| 617 | .withSelector(selector) |
| 618 | .withTreatment(treatment) |
| 619 | .fromApp(APP_ID) |
| 620 | .withPriority(DEFAULT_PRIORITY) |
| 621 | .makePermanent() |
| 622 | .build(); |
| 623 | flowRules.add(flowRule); |
| 624 | } |
| 625 | |
| 626 | |
| 627 | |
| 628 | List<NetworkResource> resources = ImmutableList.of(S1_S2, S2_S4); |
| 629 | |
| 630 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 631 | KEY1, |
| 632 | flowRules, |
| 633 | resources, |
| 634 | PathIntent.ProtectionType.PRIMARY, |
| 635 | RG1); |
| 636 | |
| 637 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 638 | flowRuleIntents.add(intent); |
| 639 | |
| 640 | return flowRuleIntents; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Generates another FlowRuleIntent, going through a different path, for testing non-disruptive reallocation. |
| 645 | * |
| 646 | * @return the FlowRuleIntents for test |
| 647 | */ |
| 648 | public List<Intent> createAnotherFlowRuleIntentsNonDisruptive() { |
| 649 | Map<ConnectPoint, ConnectPoint> portsAssociation = Maps.newHashMap(); |
| 650 | portsAssociation.put(CP1, CP3); |
| 651 | portsAssociation.put(CP3_1, CP3_2); |
| 652 | portsAssociation.put(CP4_3, CP4_1); |
| 653 | |
| 654 | List<FlowRule> flowRules = Lists.newArrayList(); |
| 655 | |
| 656 | for (ConnectPoint srcPoint : portsAssociation.keySet()) { |
| 657 | |
| 658 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 659 | .matchInPhyPort(srcPoint.port()) |
| 660 | .build(); |
| 661 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 662 | .setOutput(portsAssociation.get(srcPoint).port()) |
| 663 | .build(); |
| 664 | |
| 665 | FlowRule flowRule = DefaultFlowRule.builder() |
| 666 | .forDevice(srcPoint.deviceId()) |
| 667 | .withSelector(selector) |
| 668 | .withTreatment(treatment) |
| 669 | .fromApp(APP_ID) |
| 670 | .withPriority(DEFAULT_PRIORITY) |
| 671 | .makePermanent() |
| 672 | .build(); |
| 673 | flowRules.add(flowRule); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | |
| 678 | List<NetworkResource> resources = ImmutableList.of(S1_S3, S3_S4); |
| 679 | |
| 680 | FlowRuleIntent intent = new FlowRuleIntent(APP_ID, |
| 681 | KEY1, |
| 682 | flowRules, |
| 683 | resources, |
| 684 | PathIntent.ProtectionType.PRIMARY, |
| 685 | RG1); |
| 686 | |
| 687 | List<Intent> flowRuleIntents = Lists.newArrayList(); |
| 688 | flowRuleIntents.add(intent); |
| 689 | |
| 690 | return flowRuleIntents; |
| 691 | } |
| 692 | |
| 693 | /** |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 694 | * The FlowRuleService for test; always success for any flow rule operations. |
| 695 | */ |
| 696 | class TestFlowRuleService extends FlowRuleServiceAdapter { |
| 697 | |
| 698 | Set<FlowRule> flowRulesAdd = Sets.newHashSet(); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 699 | Set<FlowRule> flowRulesModify = Sets.newHashSet(); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 700 | Set<FlowRule> flowRulesRemove = Sets.newHashSet(); |
| 701 | |
| 702 | public void record(FlowRuleOperations ops) { |
| 703 | flowRulesAdd.clear(); |
| 704 | flowRulesRemove.clear(); |
| 705 | ops.stages().forEach(stage -> { |
| 706 | stage.forEach(op -> { |
| 707 | switch (op.type()) { |
| 708 | case ADD: |
| 709 | flowRulesAdd.add(op.rule()); |
| 710 | break; |
| 711 | case REMOVE: |
| 712 | flowRulesRemove.add(op.rule()); |
| 713 | break; |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 714 | case MODIFY: |
| 715 | flowRulesModify.add(op.rule()); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 716 | default: |
| 717 | break; |
| 718 | } |
| 719 | }); |
| 720 | }); |
| 721 | } |
| 722 | |
| 723 | @Override |
| 724 | public void apply(FlowRuleOperations ops) { |
| 725 | record(ops); |
| 726 | ops.callback().onSuccess(ops); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * The FlowRuleService for test; always failed for any flow rule operations. |
| 732 | */ |
| 733 | class TestFailedFlowRuleService extends TestFlowRuleService { |
| 734 | @Override |
| 735 | public void apply(FlowRuleOperations ops) { |
| 736 | record(ops); |
| 737 | ops.callback().onError(ops); |
| 738 | } |
| 739 | } |
| 740 | |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 741 | /** |
| 742 | * The FlowRuleService for testing non-disruptive reallocation. |
| 743 | * It keeps all the FlowRules installed/uninstalled. |
| 744 | */ |
| 745 | class TestFlowRuleServiceNonDisruptive extends FlowRuleServiceAdapter { |
| 746 | |
| 747 | Set<FlowRule> flowRulesAdd = Sets.newHashSet(); |
| 748 | Set<FlowRule> flowRulesRemove = Sets.newHashSet(); |
| 749 | |
| 750 | public void record(FlowRuleOperations ops) { |
| 751 | ops.stages().forEach(stage -> { |
| 752 | stage.forEach(op -> { |
| 753 | switch (op.type()) { |
| 754 | case ADD: |
| 755 | flowRulesAdd.add(op.rule()); |
| 756 | break; |
| 757 | case REMOVE: |
| 758 | flowRulesRemove.add(op.rule()); |
| 759 | break; |
| 760 | default: |
| 761 | break; |
| 762 | } |
| 763 | }); |
| 764 | }); |
| 765 | } |
| 766 | |
| 767 | @Override |
| 768 | public void apply(FlowRuleOperations ops) { |
| 769 | record(ops); |
| 770 | ops.callback().onSuccess(ops); |
Pier | 68c025b | 2018-03-26 17:56:18 -0700 | [diff] [blame] | 771 | synchronized (this) { |
| 772 | this.notify(); |
| 773 | } |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 777 | } |