blob: 14d2beaec693b94d2d4b165821d10e9644c53559 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Brian O'Connor66630c82014-10-02 21:08:19 -070017
Brian O'Connor66630c82014-10-02 21:08:19 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
Brian O'Connor66630c82014-10-02 21:08:19 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Sho SHIMIZUe0981722016-01-14 16:02:48 -080026import org.onlab.util.Tools;
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -080027import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.core.CoreService;
29import org.onosproject.core.IdGenerator;
Sho SHIMIZU98c0a392016-01-14 18:40:53 -080030import org.onosproject.event.AbstractListenerManager;
helenyrwu2a674902016-07-20 09:48:04 -070031import org.onosproject.net.DeviceId;
Andreas Papazois05548962016-11-23 11:36:55 +020032import org.onosproject.net.config.NetworkConfigService;
Andreas Papazoisf00087c2016-11-23 11:36:55 +020033import org.onosproject.net.domain.DomainIntentService;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.FlowRuleService;
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -080035import org.onosproject.net.flowobjective.FlowObjectiveService;
helenyrwu2a674902016-07-20 09:48:04 -070036import org.onosproject.net.group.GroupKey;
37import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentBatchDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.intent.IntentCompiler;
Yi Tsengc927a062017-05-02 15:02:37 -070041import org.onosproject.net.intent.IntentInstallCoordinator;
Brian O'Connorcff03322015-02-03 15:28:59 -080042import org.onosproject.net.intent.IntentData;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.intent.IntentEvent;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.intent.IntentExtensionService;
Yi Tsengc927a062017-05-02 15:02:37 -070045import org.onosproject.net.intent.IntentOperationContext;
46import org.onosproject.net.intent.IntentInstaller;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.intent.IntentService;
49import org.onosproject.net.intent.IntentState;
50import org.onosproject.net.intent.IntentStore;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.intent.IntentStoreDelegate;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080052import org.onosproject.net.intent.Key;
helenyrwu2a674902016-07-20 09:48:04 -070053import org.onosproject.net.intent.PointToPointIntent;
54import org.onosproject.net.intent.impl.compiler.PointToPointIntentCompiler;
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080055import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080056import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
Brian O'Connorc590ebb2016-12-08 18:16:41 -080057import org.onosproject.net.intent.impl.phase.Skipped;
Luca Pretede10c782017-01-05 17:23:08 -080058import org.onosproject.net.resource.ResourceConsumer;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080059import org.onosproject.net.resource.ResourceService;
Luca Pretede10c782017-01-05 17:23:08 -080060import org.osgi.service.component.ComponentContext;
Brian O'Connor66630c82014-10-02 21:08:19 -070061import org.slf4j.Logger;
62
Brian O'Connorf0c5a052015-04-27 00:34:53 -070063import java.util.Collection;
64import java.util.EnumSet;
65import java.util.List;
66import java.util.Map;
Sho SHIMIZUab541a52016-01-13 23:29:32 -080067import java.util.Objects;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070068import java.util.Optional;
Sho SHIMIZUad8ab272016-01-13 22:08:58 -080069import java.util.concurrent.CompletableFuture;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070070import java.util.concurrent.ExecutorService;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070071import java.util.stream.Collectors;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070072
Brian O'Connorfa81eae2014-10-30 13:20:05 -070073import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080074import static com.google.common.base.Strings.isNullOrEmpty;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080075import static java.util.concurrent.Executors.newFixedThreadPool;
Brian O'Connordb15b042015-02-04 14:59:28 -080076import static java.util.concurrent.Executors.newSingleThreadExecutor;
Brian O'Connorbdc7f002015-02-18 20:49:41 -080077import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -080078import static org.onosproject.net.intent.IntentState.*;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070079import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080080import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase;
Changhoon Yoon541ef712015-05-23 17:18:34 +090081import static org.onosproject.security.AppGuard.checkPermission;
Sho SHIMIZU98c0a392016-01-14 18:40:53 -080082import static org.onosproject.security.AppPermission.Type.INTENT_READ;
83import static org.onosproject.security.AppPermission.Type.INTENT_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070084import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090085
Brian O'Connor66630c82014-10-02 21:08:19 -070086/**
Brian O'Connorce2d8b52015-07-29 16:24:13 -070087 * An implementation of intent service.
Brian O'Connor66630c82014-10-02 21:08:19 -070088 */
89@Component(immediate = true)
90@Service
91public class IntentManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070092 extends AbstractListenerManager<IntentEvent, IntentListener>
Yi Tsengc927a062017-05-02 15:02:37 -070093 implements IntentService, IntentExtensionService, IntentInstallCoordinator {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070094
Sho SHIMIZU8b5051d2014-11-05 11:24:13 -080095 private static final Logger log = getLogger(IntentManager.class);
Brian O'Connor66630c82014-10-02 21:08:19 -070096
Sho SHIMIZU17c09da2016-03-31 12:54:17 -070097 private static final String INTENT_NULL = "Intent cannot be null";
98 private static final String INTENT_ID_NULL = "Intent key cannot be null";
Brian O'Connor66630c82014-10-02 21:08:19 -070099
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800100 private static final EnumSet<IntentState> RECOMPILE
101 = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ);
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700102 private static final EnumSet<IntentState> WITHDRAW
103 = EnumSet.of(WITHDRAW_REQ, WITHDRAWING, WITHDRAWN);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800104
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800105 private static final boolean DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL = false;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800106 @Property(name = "skipReleaseResourcesOnWithdrawal",
107 boolValue = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL,
108 label = "Indicates whether skipping resource releases on withdrawal is enabled or not")
109 private boolean skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800110
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800111 private static final int DEFAULT_NUM_THREADS = 12;
112 @Property(name = "numThreads",
113 intValue = DEFAULT_NUM_THREADS,
114 label = "Number of worker threads")
115 private int numThreads = DEFAULT_NUM_THREADS;
116
Brian O'Connor520c0522014-11-23 23:50:47 -0800117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected CoreService coreService;
Brian O'Connor66630c82014-10-02 21:08:19 -0700119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected IntentStore store;
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom85258ee2014-10-07 00:10:02 -0700124 protected ObjectiveTrackerService trackerService;
tom95329eb2014-10-06 08:40:06 -0700125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU214ac322015-02-23 19:30:15 -0800127 protected FlowRuleService flowRuleService;
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700128
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800130 protected FlowObjectiveService flowObjectiveService;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andreas Papazoisf00087c2016-11-23 11:36:55 +0200133 protected DomainIntentService domainIntentService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800136 protected ResourceService resourceService;
137
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected ComponentConfigService configService;
140
helenyrwu2a674902016-07-20 09:48:04 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected GroupService groupService;
143
Andreas Papazois05548962016-11-23 11:36:55 +0200144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
145 private NetworkConfigService networkConfigService;
146
Brian O'Connordb15b042015-02-04 14:59:28 -0800147 private ExecutorService batchExecutor;
148 private ExecutorService workerExecutor;
Brian O'Connor520c0522014-11-23 23:50:47 -0800149
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800150 private final CompilerRegistry compilerRegistry = new CompilerRegistry();
Yi Tsengc927a062017-05-02 15:02:37 -0700151 private final InstallerRegistry installerRegistry = new InstallerRegistry();
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800152 private final InternalIntentProcessor processor = new InternalIntentProcessor();
Brian O'Connor520c0522014-11-23 23:50:47 -0800153 private final IntentStoreDelegate delegate = new InternalStoreDelegate();
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800154 private final IntentStoreDelegate testOnlyDelegate = new TestOnlyIntentStoreDelegate();
Brian O'Connor520c0522014-11-23 23:50:47 -0800155 private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate();
156 private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate();
Yi Tsengc927a062017-05-02 15:02:37 -0700157 private InstallCoordinator installCoordinator;
Brian O'Connor520c0522014-11-23 23:50:47 -0800158 private IdGenerator idGenerator;
159
Brian O'Connorb499b352015-02-03 16:46:15 -0800160 private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate);
Brian O'Connorcff03322015-02-03 15:28:59 -0800161
Brian O'Connor66630c82014-10-02 21:08:19 -0700162 @Activate
163 public void activate() {
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800164 configService.registerProperties(getClass());
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800165 if (skipReleaseResourcesOnWithdrawal) {
166 store.setDelegate(testOnlyDelegate);
167 } else {
168 store.setDelegate(delegate);
169 }
tom95329eb2014-10-06 08:40:06 -0700170 trackerService.setDelegate(topoDelegate);
Brian O'Connor66630c82014-10-02 21:08:19 -0700171 eventDispatcher.addSink(IntentEvent.class, listenerRegistry);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700172 batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch", log));
173 workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log));
Brian O'Connor520c0522014-11-23 23:50:47 -0800174 idGenerator = coreService.getIdGenerator("intent-ids");
Thomas Vachuska23235962017-02-03 11:44:15 -0800175 Intent.unbindIdGenerator(idGenerator);
Brian O'Connor520c0522014-11-23 23:50:47 -0800176 Intent.bindIdGenerator(idGenerator);
Yi Tsengc927a062017-05-02 15:02:37 -0700177 installCoordinator = new InstallCoordinator(installerRegistry, store);
Brian O'Connor66630c82014-10-02 21:08:19 -0700178 log.info("Started");
179 }
180
181 @Deactivate
182 public void deactivate() {
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800183 if (skipReleaseResourcesOnWithdrawal) {
184 store.unsetDelegate(testOnlyDelegate);
185 } else {
186 store.unsetDelegate(delegate);
187 }
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800188 configService.unregisterProperties(getClass(), false);
tom95329eb2014-10-06 08:40:06 -0700189 trackerService.unsetDelegate(topoDelegate);
Brian O'Connor66630c82014-10-02 21:08:19 -0700190 eventDispatcher.removeSink(IntentEvent.class);
Brian O'Connordb15b042015-02-04 14:59:28 -0800191 batchExecutor.shutdown();
Brian O'Connor3c58e962015-04-28 23:21:51 -0700192 workerExecutor.shutdown();
Brian O'Connor520c0522014-11-23 23:50:47 -0800193 Intent.unbindIdGenerator(idGenerator);
Brian O'Connor66630c82014-10-02 21:08:19 -0700194 log.info("Stopped");
195 }
196
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800197 @Modified
198 public void modified(ComponentContext context) {
199 if (context == null) {
200 skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL;
201 logConfig("Default config");
202 return;
203 }
204
205 String s = Tools.get(context.getProperties(), "skipReleaseResourcesOnWithdrawal");
206 boolean newTestEnabled = isNullOrEmpty(s) ? skipReleaseResourcesOnWithdrawal : Boolean.parseBoolean(s.trim());
207 if (skipReleaseResourcesOnWithdrawal && !newTestEnabled) {
208 store.unsetDelegate(testOnlyDelegate);
209 store.setDelegate(delegate);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800210 skipReleaseResourcesOnWithdrawal = false;
211 logConfig("Reconfigured skip release resources on withdrawal");
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800212 } else if (!skipReleaseResourcesOnWithdrawal && newTestEnabled) {
213 store.unsetDelegate(delegate);
214 store.setDelegate(testOnlyDelegate);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800215 skipReleaseResourcesOnWithdrawal = true;
216 logConfig("Reconfigured skip release resources on withdrawal");
217 }
218
219 s = Tools.get(context.getProperties(), "numThreads");
220 int newNumThreads = isNullOrEmpty(s) ? numThreads : Integer.parseInt(s);
221 if (newNumThreads != numThreads) {
222 numThreads = newNumThreads;
223 ExecutorService oldWorkerExecutor = workerExecutor;
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800224 workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log));
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800225 if (oldWorkerExecutor != null) {
226 oldWorkerExecutor.shutdown();
227 }
228 logConfig("Reconfigured number of worker threads");
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800229 }
230 }
231
232 private void logConfig(String prefix) {
233 log.info("{} with skipReleaseResourcesOnWithdrawal = {}", prefix, skipReleaseResourcesOnWithdrawal);
234 }
235
Brian O'Connor66630c82014-10-02 21:08:19 -0700236 @Override
237 public void submit(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900238 checkPermission(INTENT_WRITE);
Brian O'Connor66630c82014-10-02 21:08:19 -0700239 checkNotNull(intent, INTENT_NULL);
Brian O'Connorcff03322015-02-03 15:28:59 -0800240 IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null);
Brian O'Connorcff03322015-02-03 15:28:59 -0800241 store.addPending(data);
Brian O'Connor66630c82014-10-02 21:08:19 -0700242 }
243
244 @Override
245 public void withdraw(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900246 checkPermission(INTENT_WRITE);
Brian O'Connor66630c82014-10-02 21:08:19 -0700247 checkNotNull(intent, INTENT_NULL);
Brian O'Connorcff03322015-02-03 15:28:59 -0800248 IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null);
Brian O'Connorcff03322015-02-03 15:28:59 -0800249 store.addPending(data);
Brian O'Connor66630c82014-10-02 21:08:19 -0700250 }
251
Brian O'Connor66630c82014-10-02 21:08:19 -0700252 @Override
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700253 public void purge(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900254 checkPermission(INTENT_WRITE);
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700255 checkNotNull(intent, INTENT_NULL);
256 IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null);
257 store.addPending(data);
helenyrwu2a674902016-07-20 09:48:04 -0700258
259 // remove associated group if there is one
260 if (intent instanceof PointToPointIntent) {
261 PointToPointIntent pointIntent = (PointToPointIntent) intent;
262 DeviceId deviceId = pointIntent.ingressPoint().deviceId();
263 GroupKey groupKey = PointToPointIntentCompiler.makeGroupKey(intent.id());
264 groupService.removeGroup(deviceId, groupKey,
265 intent.appId());
266 }
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700267 }
268
269 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800270 public Intent getIntent(Key key) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900271 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800272 return store.getIntent(key);
273 }
274
275 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700276 public Iterable<Intent> getIntents() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900277 checkPermission(INTENT_READ);
Brian O'Connor66630c82014-10-02 21:08:19 -0700278 return store.getIntents();
279 }
280
281 @Override
Brian O'Connor38224302016-08-02 22:03:01 -0700282 public void addPending(IntentData intentData) {
283 checkPermission(INTENT_WRITE);
284 checkNotNull(intentData, INTENT_NULL);
285 //TODO we might consider further checking / assertions
286 store.addPending(intentData);
287 }
288
289 @Override
Thomas Vachuskac46af202015-06-03 16:43:27 -0700290 public Iterable<IntentData> getIntentData() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900291 checkPermission(INTENT_READ);
Thomas Vachuskac46af202015-06-03 16:43:27 -0700292 return store.getIntentData(false, 0);
293 }
294
295 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700296 public long getIntentCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900297 checkPermission(INTENT_READ);
Brian O'Connor66630c82014-10-02 21:08:19 -0700298 return store.getIntentCount();
299 }
300
301 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800302 public IntentState getIntentState(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900303 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800304 checkNotNull(intentKey, INTENT_ID_NULL);
305 return store.getIntentState(intentKey);
Brian O'Connor66630c82014-10-02 21:08:19 -0700306 }
307
308 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800309 public List<Intent> getInstallableIntents(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900310 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800311 checkNotNull(intentKey, INTENT_ID_NULL);
312 return store.getInstallableIntents(intentKey);
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700313 }
314
315 @Override
Brian O'Connorbe28a872015-02-19 21:44:37 -0800316 public boolean isLocal(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900317 checkPermission(INTENT_READ);
Brian O'Connorbe28a872015-02-19 21:44:37 -0800318 return store.isMaster(intentKey);
319 }
320
321 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700322 public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900323 checkPermission(INTENT_WRITE);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800324 compilerRegistry.registerCompiler(cls, compiler);
Brian O'Connor66630c82014-10-02 21:08:19 -0700325 }
326
327 @Override
328 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900329 checkPermission(INTENT_WRITE);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800330 compilerRegistry.unregisterCompiler(cls);
Brian O'Connor66630c82014-10-02 21:08:19 -0700331 }
332
333 @Override
334 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900335 checkPermission(INTENT_READ);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800336 return compilerRegistry.getCompilers();
Brian O'Connor66630c82014-10-02 21:08:19 -0700337 }
338
339 @Override
Yi Tsengc927a062017-05-02 15:02:37 -0700340 public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) {
341 installerRegistry.registerInstaller(cls, installer);
342 }
343
344 @Override
345 public <T extends Intent> void unregisterInstaller(Class<T> cls) {
346 installerRegistry.unregisterInstaller(cls);
347 }
348
349 @Override
350 public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() {
351 return installerRegistry.getInstallers();
352 }
353
354 @Override
355 @SuppressWarnings("unchecked")
356 public <T extends Intent> IntentInstaller<T> getInstaller(Class<T> cls) {
357 return (IntentInstaller<T>) installerRegistry.getInstallers().get(cls);
358 }
359
360 @Override
Jonathan Hart34f1e382015-02-24 16:52:23 -0800361 public Iterable<Intent> getPending() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900362 checkPermission(INTENT_READ);
Jonathan Hart34f1e382015-02-24 16:52:23 -0800363 return store.getPending();
364 }
365
Yi Tsengc927a062017-05-02 15:02:37 -0700366 @Override
367 public void intentInstallSuccess(IntentOperationContext context) {
368 installCoordinator.success(context);
369 }
370
371 @Override
372 public void intentInstallFailed(IntentOperationContext context) {
373 installCoordinator.failed(context);
374 }
375
Brian O'Connor66630c82014-10-02 21:08:19 -0700376 // Store delegate to re-post events emitted from the store.
377 private class InternalStoreDelegate implements IntentStoreDelegate {
378 @Override
379 public void notify(IntentEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700380 post(event);
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800381 switch (event.type()) {
382 case WITHDRAWN:
Luca Pretede10c782017-01-05 17:23:08 -0800383 if (!skipReleaseResourcesOnWithdrawal) {
384 releaseResources(event.subject());
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800385 }
386 break;
387 default:
388 break;
389 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700390 }
Brian O'Connorea4d7d12015-01-28 16:37:46 -0800391
392 @Override
Brian O'Connorcff03322015-02-03 15:28:59 -0800393 public void process(IntentData data) {
394 accumulator.add(data);
Brian O'Connorea4d7d12015-01-28 16:37:46 -0800395 }
Thomas Vachuskac46af202015-06-03 16:43:27 -0700396
397 @Override
398 public void onUpdate(IntentData intentData) {
399 trackerService.trackIntent(intentData);
400 }
Luca Pretede10c782017-01-05 17:23:08 -0800401
402 private void releaseResources(Intent intent) {
403 // If a resource group is set on the intent, the resource consumer is
404 // set equal to it. Otherwise it's set to the intent key
405 ResourceConsumer resourceConsumer =
406 intent.resourceGroup() != null ? intent.resourceGroup() : intent.key();
407
408 // By default the resource doesn't get released
409 boolean removeResource = false;
410
411 if (intent.resourceGroup() == null) {
412 // If the intent doesn't have a resource group, it means the
413 // resource was registered using the intent key, so it can be
414 // released
415 removeResource = true;
416 } else {
417 // When a resource group is set, we make sure there are no other
418 // intents using the same resource group, before deleting the
419 // related resources.
420 Long remainingIntents =
421 Tools.stream(store.getIntents())
Yi Tseng2ef80142017-02-17 15:56:32 -0800422 .filter(i -> {
423 return i.resourceGroup() != null
424 && i.resourceGroup().equals(intent.resourceGroup());
425 })
Luca Pretede10c782017-01-05 17:23:08 -0800426 .count();
427 if (remainingIntents == 0) {
428 removeResource = true;
429 }
430 }
431
432 if (removeResource) {
433 // Release resources allocated to withdrawn intent
434 if (!resourceService.release(resourceConsumer)) {
435 log.error("Failed to release resources allocated to {}", resourceConsumer);
436 }
437 }
438 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700439 }
440
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800441 // Store delegate enabled only when performing intent throughput tests
442 private class TestOnlyIntentStoreDelegate implements IntentStoreDelegate {
443 @Override
444 public void process(IntentData data) {
445 accumulator.add(data);
446 }
447
448 @Override
449 public void onUpdate(IntentData data) {
450 trackerService.trackIntent(data);
451 }
452
453 @Override
454 public void notify(IntentEvent event) {
455 post(event);
456 }
457 }
458
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800459 private void buildAndSubmitBatches(Iterable<Key> intentKeys,
Brian O'Connor72a034c2014-11-26 18:24:23 -0800460 boolean compileAllFailed) {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800461 // Attempt recompilation of the specified intents first.
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800462 for (Key key : intentKeys) {
463 Intent intent = store.getIntent(key);
Brian O'Connor72a034c2014-11-26 18:24:23 -0800464 if (intent == null) {
465 continue;
466 }
Brian O'Connor03406a42015-02-03 17:28:57 -0800467 submit(intent);
Brian O'Connor72a034c2014-11-26 18:24:23 -0800468 }
469
Jonathan Hart0cca3e82015-09-23 17:54:11 -0700470 if (compileAllFailed) {
471 // If required, compile all currently failed intents.
472 for (Intent intent : getIntents()) {
473 IntentState state = getIntentState(intent.key());
474 if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) {
475 if (WITHDRAW.contains(state)) {
476 withdraw(intent);
477 } else {
478 submit(intent);
479 }
Brian O'Connor72a034c2014-11-26 18:24:23 -0800480 }
481 }
482 }
Brian O'Connor72a034c2014-11-26 18:24:23 -0800483 }
484
tom95329eb2014-10-06 08:40:06 -0700485 // Topology change delegate
486 private class InternalTopoChangeDelegate implements TopologyChangeDelegate {
487 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800488 public void triggerCompile(Iterable<Key> intentKeys,
tom85258ee2014-10-07 00:10:02 -0700489 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800490 buildAndSubmitBatches(intentKeys, compileAllFailed);
tom95329eb2014-10-06 08:40:06 -0700491 }
tom95329eb2014-10-06 08:40:06 -0700492 }
tom85258ee2014-10-07 00:10:02 -0700493
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700494 private class InternalBatchDelegate implements IntentBatchDelegate {
495 @Override
Brian O'Connorb499b352015-02-03 16:46:15 -0800496 public void execute(Collection<IntentData> operations) {
Brian O'Connorab8ef822015-02-17 18:08:54 -0800497 log.debug("Execute {} operation(s).", operations.size());
498 log.trace("Execute operations: {}", operations);
Sho SHIMIZU94b03b12015-04-10 14:53:13 -0700499
500 // batchExecutor is single-threaded, so only one batch is in flight at a time
Sho SHIMIZU489aa9b2016-01-14 17:19:32 -0800501 CompletableFuture.runAsync(() -> {
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800502 // process intent until the phase reaches one of the final phases
503 List<CompletableFuture<IntentData>> futures = operations.stream()
504 .map(x -> CompletableFuture.completedFuture(x)
505 .thenApply(IntentManager.this::createInitialPhase)
Sho SHIMIZU4a141852016-01-14 18:55:40 -0800506 .thenApplyAsync(IntentProcessPhase::process, workerExecutor)
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800507 .thenApply(FinalIntentProcessPhase::data)
508 .exceptionally(e -> {
Pier Luigi13b287f2017-01-10 15:07:52 -0800509 // When the future fails, we update the Intent to simulate the failure of
510 // the installation/withdrawal phase and we save in the current map. In
511 // the next round the CleanUp Thread will pick this Intent again.
512 log.warn("Future failed", e);
513 log.warn("Intent {} - state {} - request {}",
514 x.key(), x.state(), x.request());
515 switch (x.state()) {
516 case INSTALL_REQ:
517 case INSTALLING:
518 case WITHDRAW_REQ:
519 case WITHDRAWING:
520 x.setState(FAILED);
521 IntentData current = store.getIntentData(x.key());
522 return new IntentData(x, current.installables());
523 default:
524 return null;
525 }
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800526 }))
527 .collect(Collectors.toList());
Sho SHIMIZU62bbc602016-01-13 16:54:35 -0800528
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800529 // write multiple data to store in order
530 store.batchWrite(Tools.allOf(futures).join().stream()
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800531 .filter(Objects::nonNull)
532 .collect(Collectors.toList()));
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800533 }, batchExecutor).exceptionally(e -> {
534 log.error("Error submitting batches:", e);
535 // FIXME incomplete Intents should be cleaned up
536 // (transition to FAILED, etc.)
537
538 // the batch has failed
539 // TODO: maybe we should do more?
540 log.error("Walk the plank, matey...");
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800541 return null;
542 }).thenRun(accumulator::ready);
543
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700544 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700545 }
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800546
Sho SHIMIZUc88b85e2016-01-14 18:45:14 -0800547 private IntentProcessPhase createInitialPhase(IntentData data) {
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800548 IntentData pending = store.getPendingData(data.key());
549 if (pending == null || pending.version().isNewerThan(data.version())) {
550 /*
551 If the pending map is null, then this intent was compiled by a
552 previous batch iteration, so we can skip it.
553 If the pending map has a newer request, it will get compiled as
554 part of the next batch, so we can skip it.
555 */
556 return Skipped.getPhase();
557 }
Sho SHIMIZUc88b85e2016-01-14 18:45:14 -0800558 IntentData current = store.getIntentData(data.key());
559 return newInitialPhase(processor, data, current);
560 }
561
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800562 private class InternalIntentProcessor implements IntentProcessor {
563 @Override
564 public List<Intent> compile(Intent intent, List<Intent> previousInstallables) {
565 return compilerRegistry.compile(intent, previousInstallables);
566 }
567
568 @Override
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700569 public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) {
Yi Tsengc927a062017-05-02 15:02:37 -0700570 installCoordinator.installIntents(toUninstall, toInstall);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800571 }
Ray Milkeyfd7931d2015-03-30 13:58:38 -0700572 }
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800573
Brian O'Connor66630c82014-10-02 21:08:19 -0700574}