Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [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; |
| 18 | |
Ray Milkey | fd72440 | 2016-05-26 14:45:46 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
| 21 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.FlowRule; |
| 23 | import org.onosproject.net.flow.FlowRuleOperations; |
| 24 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 25 | import org.onosproject.net.flow.FlowRuleService; |
| 26 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flowobjective.Objective; |
| 28 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 29 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 30 | import org.onosproject.net.intent.FlowObjectiveIntent; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.FlowRuleIntent; |
| 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.IntentData; |
| 34 | import org.onosproject.net.intent.IntentStore; |
| 35 | import org.slf4j.Logger; |
| 36 | |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 38 | import java.util.Collection; |
Brian O'Connor | a78f060 | 2016-09-22 10:56:08 -0700 | [diff] [blame] | 39 | import java.util.Collections; |
helenyrwu | e6aaa33 | 2016-08-05 15:41:42 -0700 | [diff] [blame] | 40 | import java.util.Iterator; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.Optional; |
| 43 | import java.util.Set; |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 44 | import java.util.function.Consumer; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 45 | import java.util.stream.Collectors; |
| 46 | |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 47 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 48 | import static org.onosproject.net.intent.IntentState.*; |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
| 52 | * Auxiliary entity responsible for installing the intents into the environment. |
| 53 | */ |
| 54 | class IntentInstaller { |
| 55 | |
| 56 | private static final Logger log = getLogger(IntentManager.class); |
| 57 | |
| 58 | private IntentStore store; |
| 59 | private ObjectiveTrackerService trackerService; |
| 60 | private FlowRuleService flowRuleService; |
| 61 | private FlowObjectiveService flowObjectiveService; |
| 62 | |
| 63 | private enum Direction { |
| 64 | ADD, |
| 65 | REMOVE |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Initializes the installer with references to required services. |
| 70 | * |
| 71 | * @param intentStore intent store |
| 72 | * @param trackerService objective tracking service |
| 73 | * @param flowRuleService flow rule service |
| 74 | * @param flowObjectiveService flow objective service |
| 75 | */ |
| 76 | void init(IntentStore intentStore, ObjectiveTrackerService trackerService, |
| 77 | FlowRuleService flowRuleService, FlowObjectiveService flowObjectiveService) { |
| 78 | this.store = intentStore; |
| 79 | this.trackerService = trackerService; |
| 80 | this.flowRuleService = flowRuleService; |
| 81 | this.flowObjectiveService = flowObjectiveService; |
| 82 | } |
| 83 | |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 84 | |
| 85 | // FIXME: Refactor to accept both FlowObjectiveIntent and FlowRuleIntents |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 86 | // FIXME: Intent Manager should have never become dependent on a specific intent type(s). |
| 87 | // This will be addressed in intent domains work; not now. |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Applies the specified intent updates to the environment by uninstalling |
| 91 | * and installing the intents and updating the store references appropriately. |
| 92 | * |
| 93 | * @param toUninstall optional intent to uninstall |
| 94 | * @param toInstall optional intent to install |
| 95 | */ |
| 96 | void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) { |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 97 | // Hook for handling success |
| 98 | Consumer<OperationContext> successConsumer = (ctx) -> { |
| 99 | if (toInstall.isPresent()) { |
| 100 | IntentData installData = toInstall.get(); |
| 101 | log.debug("Completed installing: {}", installData.key()); |
| 102 | installData.setState(INSTALLED); |
| 103 | store.write(installData); |
| 104 | } else if (toUninstall.isPresent()) { |
| 105 | IntentData uninstallData = toUninstall.get(); |
| 106 | log.debug("Completed withdrawing: {}", uninstallData.key()); |
| 107 | switch (uninstallData.request()) { |
| 108 | case INSTALL_REQ: |
| 109 | uninstallData.setState(FAILED); |
| 110 | break; |
| 111 | case WITHDRAW_REQ: |
| 112 | default: //TODO "default" case should not happen |
| 113 | uninstallData.setState(WITHDRAWN); |
| 114 | break; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 115 | } |
Brian O'Connor | a78f060 | 2016-09-22 10:56:08 -0700 | [diff] [blame] | 116 | // Intent has been withdrawn; we can clear the installables |
| 117 | store.write(new IntentData(uninstallData, Collections.emptyList())); |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 118 | } |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 119 | }; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 120 | |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 121 | // Hook for handling errors |
| 122 | Consumer<OperationContext> errorConsumer = (ctx) -> { |
| 123 | // if toInstall was cause of error, then recompile (manage/increment counter, when exceeded -> CORRUPT) |
| 124 | if (toInstall.isPresent()) { |
| 125 | IntentData installData = toInstall.get(); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 126 | log.warn("Failed installation: {} {} due to {}", |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 127 | installData.key(), installData.intent(), ctx.error()); |
| 128 | installData.setState(CORRUPT); |
| 129 | installData.incrementErrorCount(); |
| 130 | store.write(installData); |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 131 | } |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 132 | // if toUninstall was cause of error, then CORRUPT (another job will clean this up) |
| 133 | if (toUninstall.isPresent()) { |
| 134 | IntentData uninstallData = toUninstall.get(); |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 135 | log.warn("Failed withdrawal: {} {} due to {}", |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 136 | uninstallData.key(), uninstallData.intent(), ctx.error()); |
| 137 | uninstallData.setState(CORRUPT); |
| 138 | uninstallData.incrementErrorCount(); |
| 139 | store.write(uninstallData); |
| 140 | } |
| 141 | }; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 142 | |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 143 | // Create a context for tracking the backing operations for applying |
| 144 | // the intents to the environment. |
| 145 | OperationContext context = createContext(toUninstall, toInstall); |
| 146 | |
| 147 | context.prepare(toUninstall, toInstall, successConsumer, errorConsumer); |
| 148 | context.apply(); |
| 149 | } |
| 150 | |
| 151 | // ------ Utilities to support FlowRule vs. FlowObjective behavior ------- |
| 152 | |
| 153 | // Creates the context appropriate for tracking operations of the |
| 154 | // the specified intents. |
| 155 | private OperationContext createContext(Optional<IntentData> toUninstall, |
| 156 | Optional<IntentData> toInstall) { |
| 157 | if (isInstallable(toUninstall, toInstall, FlowRuleIntent.class)) { |
| 158 | return new FlowRuleOperationContext(); |
| 159 | } |
| 160 | if (isInstallable(toUninstall, toInstall, FlowObjectiveIntent.class)) { |
| 161 | return new FlowObjectiveOperationContext(); |
| 162 | } |
| 163 | return new ErrorContext(); |
| 164 | } |
| 165 | |
| 166 | private boolean isInstallable(Optional<IntentData> toUninstall, Optional<IntentData> toInstall, |
| 167 | Class<? extends Intent> intentClass) { |
| 168 | boolean notBothNull = false; |
| 169 | if (toInstall.isPresent()) { |
| 170 | notBothNull = true; |
| 171 | if (!toInstall.get().installables().stream() |
| 172 | .allMatch(i -> intentClass.isAssignableFrom(i.getClass()))) { |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | if (toUninstall.isPresent()) { |
| 177 | notBothNull = true; |
| 178 | if (!toUninstall.get().installables().stream() |
| 179 | .allMatch(i -> intentClass.isAssignableFrom(i.getClass()))) { |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | return notBothNull; |
| 184 | } |
| 185 | |
| 186 | // Base context for applying and tracking operations related to installable intents. |
| 187 | private abstract class OperationContext { |
| 188 | protected Optional<IntentData> toUninstall; |
| 189 | protected Optional<IntentData> toInstall; |
Yuta HIGUCHI | 4d19ab9 | 2016-10-27 16:27:15 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Implementation of {@link OperationContext} should call this on success. |
| 192 | */ |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 193 | protected Consumer<OperationContext> successConsumer; |
Yuta HIGUCHI | 4d19ab9 | 2016-10-27 16:27:15 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Implementation of {@link OperationContext} should call this on error. |
| 196 | */ |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 197 | protected Consumer<OperationContext> errorConsumer; |
| 198 | |
Yuta HIGUCHI | 4d19ab9 | 2016-10-27 16:27:15 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Applies the Intents specified by |
| 201 | * {@link #prepareIntents(List, Direction)} call(s) prior to this call. |
| 202 | */ |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 203 | abstract void apply(); |
| 204 | |
Yuta HIGUCHI | 4d19ab9 | 2016-10-27 16:27:15 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Returns error state of the context. |
| 207 | * <p> |
| 208 | * Used for error logging purpose. |
| 209 | * Returned Object should have reasonable toString() implementation. |
| 210 | * @return context state, describing current error state |
| 211 | */ |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 212 | abstract Object error(); |
| 213 | |
Yuta HIGUCHI | 4d19ab9 | 2016-10-27 16:27:15 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Prepares Intent(s) to {@link #apply() apply} in this operation. |
| 216 | * <p> |
| 217 | * Intents specified by {@code intentsToApply} in a single call |
| 218 | * can be applied to the Devices in arbitrary order. |
| 219 | * But group of Intents specified in consecutive {@link #prepareIntents(List, Direction)} |
| 220 | * calls must be applied in order. (e.g., guarded by barrier) |
| 221 | * |
| 222 | * @param intentsToApply {@link Intent}s to apply |
| 223 | * @param direction of operation |
| 224 | */ |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 225 | abstract void prepareIntents(List<Intent> intentsToApply, Direction direction); |
| 226 | |
| 227 | void prepare(Optional<IntentData> toUninstall, Optional<IntentData> toInstall, |
| 228 | Consumer<OperationContext> successConsumer, |
| 229 | Consumer<OperationContext> errorConsumer) { |
| 230 | this.toUninstall = toUninstall; |
| 231 | this.toInstall = toInstall; |
| 232 | this.successConsumer = successConsumer; |
| 233 | this.errorConsumer = errorConsumer; |
helenyrwu | e6aaa33 | 2016-08-05 15:41:42 -0700 | [diff] [blame] | 234 | prepareIntentData(toUninstall, toInstall); |
| 235 | } |
| 236 | |
| 237 | private void prepareIntentData(Optional<IntentData> uninstallData, |
| 238 | Optional<IntentData> installData) { |
| 239 | if (!installData.isPresent() && !uninstallData.isPresent()) { |
| 240 | return; |
| 241 | } else if (!installData.isPresent()) { |
| 242 | prepareIntentData(uninstallData, Direction.REMOVE); |
| 243 | } else if (!uninstallData.isPresent()) { |
| 244 | prepareIntentData(installData, Direction.ADD); |
| 245 | } else { |
| 246 | IntentData uninstall = uninstallData.get(); |
| 247 | IntentData install = installData.get(); |
Brian O'Connor | 09d90f0 | 2016-09-13 11:06:14 -0700 | [diff] [blame] | 248 | List<Intent> uninstallIntents = Lists.newArrayList(uninstall.installables()); |
| 249 | List<Intent> installIntents = Lists.newArrayList(install.installables()); |
helenyrwu | e6aaa33 | 2016-08-05 15:41:42 -0700 | [diff] [blame] | 250 | |
| 251 | checkState(uninstallIntents.stream().allMatch(this::isSupported), |
| 252 | "Unsupported installable intents detected"); |
| 253 | checkState(installIntents.stream().allMatch(this::isSupported), |
| 254 | "Unsupported installable intents detected"); |
| 255 | |
| 256 | //TODO: Filter FlowObjective intents |
| 257 | // Filter out same intents and intents with same flow rules |
| 258 | Iterator<Intent> iterator = installIntents.iterator(); |
| 259 | while (iterator.hasNext()) { |
| 260 | Intent installIntent = iterator.next(); |
| 261 | uninstallIntents.stream().filter(uIntent -> { |
| 262 | if (uIntent.equals(installIntent)) { |
| 263 | return true; |
| 264 | } else if (uIntent instanceof FlowRuleIntent && installIntent instanceof FlowRuleIntent) { |
Brian O'Connor | 09d90f0 | 2016-09-13 11:06:14 -0700 | [diff] [blame] | 265 | //FIXME we can further optimize this by doing the filtering on a flow-by-flow basis |
| 266 | // (direction can be implied from intent state) |
helenyrwu | e6aaa33 | 2016-08-05 15:41:42 -0700 | [diff] [blame] | 267 | return ((FlowRuleIntent) uIntent).flowRules() |
| 268 | .containsAll(((FlowRuleIntent) installIntent).flowRules()); |
| 269 | } else { |
| 270 | return false; |
| 271 | } |
| 272 | }).findFirst().ifPresent(common -> { |
| 273 | uninstallIntents.remove(common); |
Brian O'Connor | 09d90f0 | 2016-09-13 11:06:14 -0700 | [diff] [blame] | 274 | if (INSTALLED.equals(uninstall.state())) { |
| 275 | // only remove the install intent if the existing |
| 276 | // intent (i.e. the uninstall one) is already |
| 277 | // installed or installing |
| 278 | iterator.remove(); |
| 279 | } |
helenyrwu | e6aaa33 | 2016-08-05 15:41:42 -0700 | [diff] [blame] | 280 | }); |
| 281 | } |
| 282 | |
| 283 | final IntentData newUninstall = new IntentData(uninstall, uninstallIntents); |
| 284 | final IntentData newInstall = new IntentData(install, installIntents); |
| 285 | |
| 286 | trackerService.removeTrackedResources(newUninstall.key(), newUninstall.intent().resources()); |
| 287 | uninstallIntents.forEach(installable -> |
| 288 | trackerService.removeTrackedResources(newUninstall.intent().key(), |
| 289 | installable.resources())); |
| 290 | trackerService.addTrackedResources(newInstall.key(), newInstall.intent().resources()); |
| 291 | installIntents.forEach(installable -> |
| 292 | trackerService.addTrackedResources(newInstall.key(), |
| 293 | installable.resources())); |
| 294 | prepareIntents(uninstallIntents, Direction.REMOVE); |
| 295 | prepareIntents(installIntents, Direction.ADD); |
| 296 | } |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 299 | /** |
| 300 | * Applies the specified intent data, if present, to the network using the |
| 301 | * specified context. |
| 302 | * |
| 303 | * @param intentData optional intent data; no-op if not present |
| 304 | * @param direction indicates adding or removal |
| 305 | */ |
| 306 | private void prepareIntentData(Optional<IntentData> intentData, Direction direction) { |
| 307 | if (!intentData.isPresent()) { |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | IntentData data = intentData.get(); |
| 312 | List<Intent> intentsToApply = data.installables(); |
| 313 | checkState(intentsToApply.stream().allMatch(this::isSupported), |
| 314 | "Unsupported installable intents detected"); |
| 315 | |
| 316 | if (direction == Direction.ADD) { |
| 317 | trackerService.addTrackedResources(data.key(), data.intent().resources()); |
| 318 | intentsToApply.forEach(installable -> |
| 319 | trackerService.addTrackedResources(data.key(), |
| 320 | installable.resources())); |
| 321 | } else { |
| 322 | trackerService.removeTrackedResources(data.key(), data.intent().resources()); |
| 323 | intentsToApply.forEach(installable -> |
| 324 | trackerService.removeTrackedResources(data.intent().key(), |
| 325 | installable.resources())); |
| 326 | } |
| 327 | |
| 328 | prepareIntents(intentsToApply, direction); |
| 329 | } |
| 330 | |
| 331 | private boolean isSupported(Intent intent) { |
| 332 | return intent instanceof FlowRuleIntent || intent instanceof FlowObjectiveIntent; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | |
| 337 | // Context for applying and tracking operations related to flow rule intent. |
| 338 | private class FlowRuleOperationContext extends OperationContext { |
| 339 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 340 | FlowRuleOperationsContext flowRuleOperationsContext; |
| 341 | |
Yuta HIGUCHI | 4d19ab9 | 2016-10-27 16:27:15 -0700 | [diff] [blame] | 342 | @Override |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 343 | void apply() { |
| 344 | flowRuleOperationsContext = new FlowRuleOperationsContext() { |
| 345 | @Override |
| 346 | public void onSuccess(FlowRuleOperations ops) { |
| 347 | successConsumer.accept(FlowRuleOperationContext.this); |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void onError(FlowRuleOperations ops) { |
| 352 | errorConsumer.accept(FlowRuleOperationContext.this); |
| 353 | } |
| 354 | }; |
| 355 | FlowRuleOperations operations = builder.build(flowRuleOperationsContext); |
| 356 | |
| 357 | if (log.isTraceEnabled()) { |
| 358 | log.trace("applying intent {} -> {} with {} rules: {}", |
| 359 | toUninstall.map(x -> x.key().toString()).orElse("<empty>"), |
| 360 | toInstall.map(x -> x.key().toString()).orElse("<empty>"), |
| 361 | operations.stages().stream().mapToLong(Set::size).sum(), |
| 362 | operations.stages()); |
| 363 | } |
| 364 | |
| 365 | flowRuleService.apply(operations); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public void prepareIntents(List<Intent> intentsToApply, Direction direction) { |
| 370 | // FIXME do FlowRuleIntents have stages??? Can we do uninstall work in parallel? I think so. |
| 371 | builder.newStage(); |
| 372 | |
| 373 | List<Collection<FlowRule>> stages = intentsToApply.stream() |
| 374 | .map(x -> (FlowRuleIntent) x) |
| 375 | .map(FlowRuleIntent::flowRules) |
| 376 | .collect(Collectors.toList()); |
| 377 | |
| 378 | for (Collection<FlowRule> rules : stages) { |
| 379 | if (direction == Direction.ADD) { |
| 380 | rules.forEach(builder::add); |
| 381 | } else { |
| 382 | rules.forEach(builder::remove); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public Object error() { |
| 390 | return flowRuleOperationsContext; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Context for applying and tracking operations related to flow objective intents. |
| 395 | private class FlowObjectiveOperationContext extends OperationContext { |
Ray Milkey | fd72440 | 2016-05-26 14:45:46 -0700 | [diff] [blame] | 396 | List<FlowObjectiveInstallationContext> contexts = Lists.newLinkedList(); |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 397 | final Set<ObjectiveContext> pendingContexts = Sets.newHashSet(); |
| 398 | final Set<ObjectiveContext> errorContexts = Sets.newConcurrentHashSet(); |
| 399 | |
| 400 | @Override |
| 401 | public void prepareIntents(List<Intent> intentsToApply, Direction direction) { |
Ray Milkey | fd72440 | 2016-05-26 14:45:46 -0700 | [diff] [blame] | 402 | intentsToApply.stream() |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 403 | .flatMap(x -> buildObjectiveContexts((FlowObjectiveIntent) x, direction).stream()) |
Ray Milkey | fd72440 | 2016-05-26 14:45:46 -0700 | [diff] [blame] | 404 | .forEach(contexts::add); |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // Builds the specified objective in the appropriate direction |
| 408 | private List<FlowObjectiveInstallationContext> buildObjectiveContexts(FlowObjectiveIntent intent, |
| 409 | Direction direction) { |
| 410 | int size = intent.objectives().size(); |
| 411 | List<FlowObjectiveInstallationContext> contexts = new ArrayList<>(size); |
| 412 | for (int i = 0; i < size; i++) { |
| 413 | DeviceId deviceId = intent.devices().get(i); |
| 414 | Objective.Builder builder = intent.objectives().get(i).copy(); |
| 415 | FlowObjectiveInstallationContext context = new FlowObjectiveInstallationContext(); |
| 416 | |
| 417 | final Objective objective; |
| 418 | switch (direction) { |
| 419 | case ADD: |
| 420 | objective = builder.add(context); |
| 421 | break; |
| 422 | case REMOVE: |
| 423 | objective = builder.remove(context); |
| 424 | break; |
| 425 | default: |
| 426 | throw new UnsupportedOperationException("Unsupported direction " + direction); |
| 427 | } |
| 428 | context.setObjective(objective, deviceId); |
| 429 | contexts.add(context); |
| 430 | } |
| 431 | return contexts; |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | void apply() { |
| 436 | contexts.forEach(objectiveContext -> { |
| 437 | pendingContexts.add(objectiveContext); |
| 438 | flowObjectiveService.apply(objectiveContext.deviceId, |
| 439 | objectiveContext.objective); |
| 440 | }); |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public Object error() { |
| 445 | return errorContexts; |
| 446 | } |
| 447 | |
| 448 | private class FlowObjectiveInstallationContext implements ObjectiveContext { |
| 449 | Objective objective; |
| 450 | DeviceId deviceId; |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 451 | ObjectiveError error; |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 452 | |
| 453 | void setObjective(Objective objective, DeviceId deviceId) { |
| 454 | this.objective = objective; |
| 455 | this.deviceId = deviceId; |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public void onSuccess(Objective objective) { |
| 460 | finish(); |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void onError(Objective objective, ObjectiveError error) { |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 465 | this.error = error; |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 466 | errorContexts.add(this); |
| 467 | finish(); |
| 468 | } |
| 469 | |
| 470 | private void finish() { |
| 471 | synchronized (pendingContexts) { |
| 472 | pendingContexts.remove(this); |
| 473 | if (pendingContexts.isEmpty()) { |
| 474 | if (errorContexts.isEmpty()) { |
| 475 | successConsumer.accept(FlowObjectiveOperationContext.this); |
| 476 | } else { |
| 477 | errorConsumer.accept(FlowObjectiveOperationContext.this); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public String toString() { |
Thomas Vachuska | d27097c | 2016-06-14 19:10:41 -0700 | [diff] [blame] | 485 | return String.format("(%s on %s for %s)", error, deviceId, objective); |
Thomas Vachuska | 2980c97 | 2016-02-23 20:58:49 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | private class ErrorContext extends OperationContext { |
| 491 | @Override |
| 492 | void apply() { |
| 493 | throw new UnsupportedOperationException("Unsupported installable intent"); |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | Object error() { |
| 498 | return null; |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | void prepareIntents(List<Intent> intentsToApply, Direction direction) { |
| 503 | } |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 504 | } |
| 505 | } |