Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | |
Sho SHIMIZU | e098172 | 2016-01-14 16:02:48 -0800 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 19 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.core.CoreService; |
| 21 | import org.onosproject.core.IdGenerator; |
Sho SHIMIZU | 98c0a39 | 2016-01-14 18:40:53 -0800 | [diff] [blame] | 22 | import org.onosproject.event.AbstractListenerManager; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Andreas Papazois | 0554896 | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 24 | import org.onosproject.net.config.NetworkConfigService; |
Andreas Papazois | f00087c | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 25 | import org.onosproject.net.domain.DomainIntentService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.FlowRuleService; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 28 | import org.onosproject.net.group.GroupKey; |
| 29 | import org.onosproject.net.group.GroupService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.IntentExtensionService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentInstallCoordinator; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 37 | import org.onosproject.net.intent.IntentInstaller; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.IntentListener; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentOperationContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.IntentService; |
| 41 | import org.onosproject.net.intent.IntentState; |
| 42 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.Key; |
Yi Tseng | 24d9be7 | 2017-05-12 11:28:13 -0700 | [diff] [blame] | 45 | import org.onosproject.net.intent.ObjectiveTrackerService; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 46 | import org.onosproject.net.intent.PointToPointIntent; |
Yi Tseng | 24d9be7 | 2017-05-12 11:28:13 -0700 | [diff] [blame] | 47 | import org.onosproject.net.intent.TopologyChangeDelegate; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 48 | import org.onosproject.net.intent.impl.compiler.PointToPointIntentCompiler; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 50 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 51 | import org.onosproject.net.intent.impl.phase.Skipped; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 52 | import org.onosproject.net.resource.ResourceConsumer; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 53 | import org.onosproject.net.resource.ResourceService; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 54 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | import org.osgi.service.component.annotations.Activate; |
| 56 | import org.osgi.service.component.annotations.Component; |
| 57 | import org.osgi.service.component.annotations.Deactivate; |
| 58 | import org.osgi.service.component.annotations.Modified; |
| 59 | import org.osgi.service.component.annotations.Reference; |
| 60 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 63 | import java.util.Collection; |
| 64 | import java.util.EnumSet; |
| 65 | import java.util.List; |
| 66 | import java.util.Map; |
Sho SHIMIZU | ab541a5 | 2016-01-13 23:29:32 -0800 | [diff] [blame] | 67 | import java.util.Objects; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 68 | import java.util.Optional; |
Sho SHIMIZU | ad8ab27 | 2016-01-13 22:08:58 -0800 | [diff] [blame] | 69 | import java.util.concurrent.CompletableFuture; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 70 | import java.util.concurrent.ExecutorService; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 71 | import java.util.stream.Collectors; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 72 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 73 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 74 | import static com.google.common.base.Strings.isNullOrEmpty; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 75 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 76 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 77 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 78 | import static org.onosproject.net.OsgiPropertyConstants.IM_NUM_THREADS; |
| 79 | import static org.onosproject.net.OsgiPropertyConstants.IM_NUM_THREADS_DEFAULT; |
| 80 | import static org.onosproject.net.OsgiPropertyConstants.IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL; |
| 81 | import static org.onosproject.net.OsgiPropertyConstants.IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL_DEFAULT; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 83 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 84 | import static org.onosproject.net.intent.IntentState.WITHDRAWING; |
| 85 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 86 | import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 87 | import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 88 | import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 89 | import static org.onosproject.security.AppGuard.checkPermission; |
Sho SHIMIZU | 98c0a39 | 2016-01-14 18:40:53 -0800 | [diff] [blame] | 90 | import static org.onosproject.security.AppPermission.Type.INTENT_READ; |
| 91 | import static org.onosproject.security.AppPermission.Type.INTENT_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 92 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 93 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 94 | /** |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 95 | * An implementation of intent service. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 96 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 97 | @Component( |
| 98 | immediate = true, |
| 99 | service = { |
| 100 | IntentService.class, |
| 101 | IntentExtensionService.class, |
| 102 | IntentInstallCoordinator.class |
| 103 | }, |
| 104 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 105 | IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL + ":Boolean=" + IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL_DEFAULT, |
| 106 | IM_NUM_THREADS + ":Integer=" + IM_NUM_THREADS_DEFAULT |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 107 | } |
| 108 | ) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 109 | public class IntentManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 110 | extends AbstractListenerManager<IntentEvent, IntentListener> |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 111 | implements IntentService, IntentExtensionService, IntentInstallCoordinator { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 112 | |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 113 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 114 | |
Sho SHIMIZU | 17c09da | 2016-03-31 12:54:17 -0700 | [diff] [blame] | 115 | private static final String INTENT_NULL = "Intent cannot be null"; |
| 116 | private static final String INTENT_ID_NULL = "Intent key cannot be null"; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 117 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 118 | private static final EnumSet<IntentState> RECOMPILE |
| 119 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 120 | private static final EnumSet<IntentState> WITHDRAW |
| 121 | = EnumSet.of(WITHDRAW_REQ, WITHDRAWING, WITHDRAWN); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 122 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 123 | /** Indicates whether skipping resource releases on withdrawal is enabled or not. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 124 | private boolean skipReleaseResourcesOnWithdrawal = IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL_DEFAULT; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 125 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 126 | /** Number of worker threads. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 127 | private int numThreads = IM_NUM_THREADS_DEFAULT; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 128 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 130 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 131 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 133 | protected IntentStore store; |
| 134 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 136 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 137 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sho SHIMIZU | 214ac32 | 2015-02-23 19:30:15 -0800 | [diff] [blame] | 139 | protected FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 140 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 142 | protected FlowObjectiveService flowObjectiveService; |
| 143 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andreas Papazois | f00087c | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 145 | protected DomainIntentService domainIntentService; |
| 146 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 148 | protected ResourceService resourceService; |
| 149 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 151 | protected ComponentConfigService configService; |
| 152 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 154 | protected GroupService groupService; |
| 155 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andreas Papazois | 0554896 | 2016-11-23 11:36:55 +0200 | [diff] [blame] | 157 | private NetworkConfigService networkConfigService; |
| 158 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 159 | private ExecutorService batchExecutor; |
| 160 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 161 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 162 | private final CompilerRegistry compilerRegistry = new CompilerRegistry(); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 163 | private final InstallerRegistry installerRegistry = new InstallerRegistry(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 164 | private final InternalIntentProcessor processor = new InternalIntentProcessor(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 165 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 166 | private final IntentStoreDelegate testOnlyDelegate = new TestOnlyIntentStoreDelegate(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 167 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 168 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 169 | private InstallCoordinator installCoordinator; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 170 | private IdGenerator idGenerator; |
| 171 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 172 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 173 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 174 | @Activate |
| 175 | public void activate() { |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 176 | configService.registerProperties(getClass()); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 177 | if (skipReleaseResourcesOnWithdrawal) { |
| 178 | store.setDelegate(testOnlyDelegate); |
| 179 | } else { |
| 180 | store.setDelegate(delegate); |
| 181 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 182 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 183 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 184 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch", log)); |
| 185 | workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log)); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 186 | idGenerator = coreService.getIdGenerator("intent-ids"); |
Thomas Vachuska | 2323596 | 2017-02-03 11:44:15 -0800 | [diff] [blame] | 187 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 188 | Intent.bindIdGenerator(idGenerator); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 189 | installCoordinator = new InstallCoordinator(installerRegistry, store); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 190 | log.info("Started"); |
| 191 | } |
| 192 | |
| 193 | @Deactivate |
| 194 | public void deactivate() { |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 195 | if (skipReleaseResourcesOnWithdrawal) { |
| 196 | store.unsetDelegate(testOnlyDelegate); |
| 197 | } else { |
| 198 | store.unsetDelegate(delegate); |
| 199 | } |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame] | 200 | configService.unregisterProperties(getClass(), false); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 201 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 202 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 203 | batchExecutor.shutdown(); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 204 | workerExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 205 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 206 | log.info("Stopped"); |
| 207 | } |
| 208 | |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 209 | @Modified |
| 210 | public void modified(ComponentContext context) { |
| 211 | if (context == null) { |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 212 | skipReleaseResourcesOnWithdrawal = IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL_DEFAULT; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 213 | logConfig("Default config"); |
| 214 | return; |
| 215 | } |
| 216 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 217 | String s = Tools.get(context.getProperties(), IM_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 218 | boolean newTestEnabled = isNullOrEmpty(s) ? skipReleaseResourcesOnWithdrawal : Boolean.parseBoolean(s.trim()); |
| 219 | if (skipReleaseResourcesOnWithdrawal && !newTestEnabled) { |
| 220 | store.unsetDelegate(testOnlyDelegate); |
| 221 | store.setDelegate(delegate); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 222 | skipReleaseResourcesOnWithdrawal = false; |
| 223 | logConfig("Reconfigured skip release resources on withdrawal"); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 224 | } else if (!skipReleaseResourcesOnWithdrawal && newTestEnabled) { |
| 225 | store.unsetDelegate(delegate); |
| 226 | store.setDelegate(testOnlyDelegate); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 227 | skipReleaseResourcesOnWithdrawal = true; |
| 228 | logConfig("Reconfigured skip release resources on withdrawal"); |
| 229 | } |
| 230 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 231 | s = Tools.get(context.getProperties(), IM_NUM_THREADS); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 232 | int newNumThreads = isNullOrEmpty(s) ? numThreads : Integer.parseInt(s); |
| 233 | if (newNumThreads != numThreads) { |
| 234 | numThreads = newNumThreads; |
| 235 | ExecutorService oldWorkerExecutor = workerExecutor; |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 236 | workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log)); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 237 | if (oldWorkerExecutor != null) { |
| 238 | oldWorkerExecutor.shutdown(); |
| 239 | } |
| 240 | logConfig("Reconfigured number of worker threads"); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | private void logConfig(String prefix) { |
| 245 | log.info("{} with skipReleaseResourcesOnWithdrawal = {}", prefix, skipReleaseResourcesOnWithdrawal); |
| 246 | } |
| 247 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 248 | @Override |
| 249 | public void submit(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 250 | checkPermission(INTENT_WRITE); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 251 | checkNotNull(intent, INTENT_NULL); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 252 | IntentData data = IntentData.submit(intent); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 253 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void withdraw(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 258 | checkPermission(INTENT_WRITE); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 259 | checkNotNull(intent, INTENT_NULL); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 260 | IntentData data = IntentData.withdraw(intent); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 261 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 264 | @Override |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 265 | public void purge(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 266 | checkPermission(INTENT_WRITE); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 267 | checkNotNull(intent, INTENT_NULL); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 268 | IntentData data = IntentData.purge(intent); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 269 | store.addPending(data); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 270 | |
| 271 | // remove associated group if there is one |
| 272 | if (intent instanceof PointToPointIntent) { |
| 273 | PointToPointIntent pointIntent = (PointToPointIntent) intent; |
Ray Milkey | a2cf3a1 | 2018-02-15 16:13:56 -0800 | [diff] [blame] | 274 | DeviceId deviceId = pointIntent.filteredIngressPoint().connectPoint().deviceId(); |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 275 | GroupKey groupKey = PointToPointIntentCompiler.makeGroupKey(intent.id()); |
| 276 | groupService.removeGroup(deviceId, groupKey, |
| 277 | intent.appId()); |
| 278 | } |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 282 | public Intent getIntent(Key key) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 283 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 284 | return store.getIntent(key); |
| 285 | } |
| 286 | |
| 287 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 288 | public Iterable<Intent> getIntents() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 289 | checkPermission(INTENT_READ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 290 | return store.getIntents(); |
| 291 | } |
| 292 | |
| 293 | @Override |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 294 | public void addPending(IntentData intentData) { |
| 295 | checkPermission(INTENT_WRITE); |
| 296 | checkNotNull(intentData, INTENT_NULL); |
| 297 | //TODO we might consider further checking / assertions |
| 298 | store.addPending(intentData); |
| 299 | } |
| 300 | |
| 301 | @Override |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 302 | public Iterable<IntentData> getIntentData() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 303 | checkPermission(INTENT_READ); |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 304 | return store.getIntentData(false, 0); |
| 305 | } |
| 306 | |
| 307 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 308 | public long getIntentCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 309 | checkPermission(INTENT_READ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 310 | return store.getIntentCount(); |
| 311 | } |
| 312 | |
| 313 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 314 | public IntentState getIntentState(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 315 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 316 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 317 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 321 | public List<Intent> getInstallableIntents(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 322 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 323 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 324 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 328 | public boolean isLocal(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 329 | checkPermission(INTENT_READ); |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 330 | return store.isMaster(intentKey); |
| 331 | } |
| 332 | |
| 333 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 334 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 335 | checkPermission(INTENT_WRITE); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 336 | compilerRegistry.registerCompiler(cls, compiler); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 341 | checkPermission(INTENT_WRITE); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 342 | compilerRegistry.unregisterCompiler(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 347 | checkPermission(INTENT_READ); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 348 | return compilerRegistry.getCompilers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 352 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { |
| 353 | installerRegistry.registerInstaller(cls, installer); |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
| 358 | installerRegistry.unregisterInstaller(cls); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
| 363 | return installerRegistry.getInstallers(); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | @SuppressWarnings("unchecked") |
| 368 | public <T extends Intent> IntentInstaller<T> getInstaller(Class<T> cls) { |
| 369 | return (IntentInstaller<T>) installerRegistry.getInstallers().get(cls); |
| 370 | } |
| 371 | |
| 372 | @Override |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 373 | public Iterable<Intent> getPending() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 374 | checkPermission(INTENT_READ); |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 375 | return store.getPending(); |
| 376 | } |
| 377 | |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 378 | @Override |
| 379 | public void intentInstallSuccess(IntentOperationContext context) { |
| 380 | installCoordinator.success(context); |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public void intentInstallFailed(IntentOperationContext context) { |
| 385 | installCoordinator.failed(context); |
| 386 | } |
| 387 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 388 | // Store delegate to re-post events emitted from the store. |
| 389 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 390 | @Override |
| 391 | public void notify(IntentEvent event) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 392 | post(event); |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 393 | switch (event.type()) { |
| 394 | case WITHDRAWN: |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 395 | if (!skipReleaseResourcesOnWithdrawal) { |
| 396 | releaseResources(event.subject()); |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 397 | } |
| 398 | break; |
| 399 | default: |
| 400 | break; |
| 401 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 402 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 403 | |
| 404 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 405 | public void process(IntentData data) { |
| 406 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 407 | } |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 408 | |
| 409 | @Override |
| 410 | public void onUpdate(IntentData intentData) { |
| 411 | trackerService.trackIntent(intentData); |
| 412 | } |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 413 | |
| 414 | private void releaseResources(Intent intent) { |
| 415 | // If a resource group is set on the intent, the resource consumer is |
| 416 | // set equal to it. Otherwise it's set to the intent key |
| 417 | ResourceConsumer resourceConsumer = |
| 418 | intent.resourceGroup() != null ? intent.resourceGroup() : intent.key(); |
| 419 | |
| 420 | // By default the resource doesn't get released |
| 421 | boolean removeResource = false; |
| 422 | |
| 423 | if (intent.resourceGroup() == null) { |
| 424 | // If the intent doesn't have a resource group, it means the |
| 425 | // resource was registered using the intent key, so it can be |
| 426 | // released |
| 427 | removeResource = true; |
| 428 | } else { |
| 429 | // When a resource group is set, we make sure there are no other |
| 430 | // intents using the same resource group, before deleting the |
| 431 | // related resources. |
| 432 | Long remainingIntents = |
| 433 | Tools.stream(store.getIntents()) |
Yi Tseng | 2ef8014 | 2017-02-17 15:56:32 -0800 | [diff] [blame] | 434 | .filter(i -> { |
| 435 | return i.resourceGroup() != null |
| 436 | && i.resourceGroup().equals(intent.resourceGroup()); |
| 437 | }) |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 438 | .count(); |
| 439 | if (remainingIntents == 0) { |
| 440 | removeResource = true; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (removeResource) { |
| 445 | // Release resources allocated to withdrawn intent |
| 446 | if (!resourceService.release(resourceConsumer)) { |
| 447 | log.error("Failed to release resources allocated to {}", resourceConsumer); |
| 448 | } |
| 449 | } |
| 450 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 453 | // Store delegate enabled only when performing intent throughput tests |
| 454 | private class TestOnlyIntentStoreDelegate implements IntentStoreDelegate { |
| 455 | @Override |
| 456 | public void process(IntentData data) { |
| 457 | accumulator.add(data); |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public void onUpdate(IntentData data) { |
| 462 | trackerService.trackIntent(data); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public void notify(IntentEvent event) { |
| 467 | post(event); |
| 468 | } |
| 469 | } |
| 470 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 471 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 472 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 473 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 474 | for (Key key : intentKeys) { |
Yi Tseng | 49a2b2e | 2017-05-11 14:32:16 -0700 | [diff] [blame] | 475 | if (!store.isMaster(key)) { |
| 476 | continue; |
| 477 | } |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 478 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 479 | if (intent == null) { |
| 480 | continue; |
| 481 | } |
jaegonkim | b914fcb | 2018-05-20 16:04:06 +0900 | [diff] [blame] | 482 | if (store.getPendingData(key) != null) { |
| 483 | continue; |
| 484 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 485 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Jonathan Hart | 0cca3e8 | 2015-09-23 17:54:11 -0700 | [diff] [blame] | 488 | if (compileAllFailed) { |
| 489 | // If required, compile all currently failed intents. |
| 490 | for (Intent intent : getIntents()) { |
Yi Tseng | 49a2b2e | 2017-05-11 14:32:16 -0700 | [diff] [blame] | 491 | if (!store.isMaster(intent.key())) { |
| 492 | continue; |
| 493 | } |
jaegonkim | b914fcb | 2018-05-20 16:04:06 +0900 | [diff] [blame] | 494 | if (store.getPendingData(intent.key()) != null) { |
| 495 | continue; |
| 496 | } |
Jonathan Hart | 0cca3e8 | 2015-09-23 17:54:11 -0700 | [diff] [blame] | 497 | IntentState state = getIntentState(intent.key()); |
| 498 | if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) { |
| 499 | if (WITHDRAW.contains(state)) { |
| 500 | withdraw(intent); |
| 501 | } else { |
| 502 | submit(intent); |
| 503 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 507 | } |
| 508 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 509 | // Topology change delegate |
| 510 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 511 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 512 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 513 | boolean compileAllFailed) { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 514 | // TODO figure out who is making excessive calls? |
Jon Hall | 274cecb | 2017-08-09 12:15:48 -0700 | [diff] [blame] | 515 | log.trace("submitting {} + all?:{} for compilation", intentKeys, compileAllFailed); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 516 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 517 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 518 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 519 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 520 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 521 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 522 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 523 | log.debug("Execute {} operation(s).", operations.size()); |
| 524 | log.trace("Execute operations: {}", operations); |
Sho SHIMIZU | 94b03b1 | 2015-04-10 14:53:13 -0700 | [diff] [blame] | 525 | |
| 526 | // batchExecutor is single-threaded, so only one batch is in flight at a time |
Sho SHIMIZU | 489aa9b | 2016-01-14 17:19:32 -0800 | [diff] [blame] | 527 | CompletableFuture.runAsync(() -> { |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 528 | // process intent until the phase reaches one of the final phases |
| 529 | List<CompletableFuture<IntentData>> futures = operations.stream() |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 530 | .map(data -> { |
| 531 | log.debug("Start processing of {} {}@{}", data.request(), data.key(), data.version()); |
| 532 | return data; |
| 533 | }) |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 534 | .map(x -> CompletableFuture.completedFuture(x) |
| 535 | .thenApply(IntentManager.this::createInitialPhase) |
Sho SHIMIZU | 4a14185 | 2016-01-14 18:55:40 -0800 | [diff] [blame] | 536 | .thenApplyAsync(IntentProcessPhase::process, workerExecutor) |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 537 | .thenApply(FinalIntentProcessPhase::data) |
| 538 | .exceptionally(e -> { |
Pier Luigi | 13b287f | 2017-01-10 15:07:52 -0800 | [diff] [blame] | 539 | // When the future fails, we update the Intent to simulate the failure of |
| 540 | // the installation/withdrawal phase and we save in the current map. In |
| 541 | // the next round the CleanUp Thread will pick this Intent again. |
| 542 | log.warn("Future failed", e); |
| 543 | log.warn("Intent {} - state {} - request {}", |
| 544 | x.key(), x.state(), x.request()); |
| 545 | switch (x.state()) { |
| 546 | case INSTALL_REQ: |
| 547 | case INSTALLING: |
| 548 | case WITHDRAW_REQ: |
| 549 | case WITHDRAWING: |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 550 | // TODO should we swtich based on current |
Pier Luigi | 13b287f | 2017-01-10 15:07:52 -0800 | [diff] [blame] | 551 | IntentData current = store.getIntentData(x.key()); |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 552 | return IntentData.nextState(current, FAILED); |
Pier Luigi | 13b287f | 2017-01-10 15:07:52 -0800 | [diff] [blame] | 553 | default: |
| 554 | return null; |
| 555 | } |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 556 | })) |
| 557 | .collect(Collectors.toList()); |
Sho SHIMIZU | 62bbc60 | 2016-01-13 16:54:35 -0800 | [diff] [blame] | 558 | |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 559 | // write multiple data to store in order |
| 560 | store.batchWrite(Tools.allOf(futures).join().stream() |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 561 | .filter(Objects::nonNull) |
| 562 | .collect(Collectors.toList())); |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 563 | }, batchExecutor).exceptionally(e -> { |
| 564 | log.error("Error submitting batches:", e); |
| 565 | // FIXME incomplete Intents should be cleaned up |
| 566 | // (transition to FAILED, etc.) |
| 567 | |
| 568 | // the batch has failed |
| 569 | // TODO: maybe we should do more? |
| 570 | log.error("Walk the plank, matey..."); |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 571 | return null; |
| 572 | }).thenRun(accumulator::ready); |
| 573 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 574 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 575 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 576 | |
Sho SHIMIZU | c88b85e | 2016-01-14 18:45:14 -0800 | [diff] [blame] | 577 | private IntentProcessPhase createInitialPhase(IntentData data) { |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 578 | IntentData pending = store.getPendingData(data.key()); |
| 579 | if (pending == null || pending.version().isNewerThan(data.version())) { |
| 580 | /* |
| 581 | If the pending map is null, then this intent was compiled by a |
| 582 | previous batch iteration, so we can skip it. |
| 583 | If the pending map has a newer request, it will get compiled as |
| 584 | part of the next batch, so we can skip it. |
| 585 | */ |
| 586 | return Skipped.getPhase(); |
| 587 | } |
Sho SHIMIZU | c88b85e | 2016-01-14 18:45:14 -0800 | [diff] [blame] | 588 | IntentData current = store.getIntentData(data.key()); |
| 589 | return newInitialPhase(processor, data, current); |
| 590 | } |
| 591 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 592 | private class InternalIntentProcessor implements IntentProcessor { |
| 593 | @Override |
| 594 | public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { |
| 595 | return compilerRegistry.compile(intent, previousInstallables); |
| 596 | } |
| 597 | |
| 598 | @Override |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 599 | public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) { |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 600 | installCoordinator.installIntents(toUninstall, toInstall); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 601 | } |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 602 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 603 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 604 | } |