blob: 5524d633ddfcacada351ac429ea37674e67219e9 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.FlowRuleService;
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -080033import org.onosproject.net.flowobjective.FlowObjectiveService;
helenyrwu2a674902016-07-20 09:48:04 -070034import org.onosproject.net.group.GroupKey;
35import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentBatchDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.IntentCompiler;
Brian O'Connorcff03322015-02-03 15:28:59 -080039import org.onosproject.net.intent.IntentData;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.intent.IntentEvent;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.intent.IntentExtensionService;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.intent.IntentService;
44import org.onosproject.net.intent.IntentState;
45import org.onosproject.net.intent.IntentStore;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.intent.IntentStoreDelegate;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080047import org.onosproject.net.intent.Key;
helenyrwu2a674902016-07-20 09:48:04 -070048import org.onosproject.net.intent.PointToPointIntent;
49import org.onosproject.net.intent.impl.compiler.PointToPointIntentCompiler;
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080050import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080051import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
Brian O'Connorc590ebb2016-12-08 18:16:41 -080052import org.onosproject.net.intent.impl.phase.Skipped;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080053import org.osgi.service.component.ComponentContext;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080054import org.onosproject.net.resource.ResourceService;
Brian O'Connor66630c82014-10-02 21:08:19 -070055import org.slf4j.Logger;
56
Brian O'Connorf0c5a052015-04-27 00:34:53 -070057import java.util.Collection;
58import java.util.EnumSet;
59import java.util.List;
60import java.util.Map;
Sho SHIMIZUab541a52016-01-13 23:29:32 -080061import java.util.Objects;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070062import java.util.Optional;
Sho SHIMIZUad8ab272016-01-13 22:08:58 -080063import java.util.concurrent.CompletableFuture;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070064import java.util.concurrent.ExecutorService;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070065import java.util.stream.Collectors;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070066
Brian O'Connorfa81eae2014-10-30 13:20:05 -070067import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080068import static com.google.common.base.Strings.isNullOrEmpty;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080069import static java.util.concurrent.Executors.newFixedThreadPool;
Brian O'Connordb15b042015-02-04 14:59:28 -080070import static java.util.concurrent.Executors.newSingleThreadExecutor;
Brian O'Connorbdc7f002015-02-18 20:49:41 -080071import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -080072import static org.onosproject.net.intent.IntentState.*;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070073import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080074import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase;
Changhoon Yoon541ef712015-05-23 17:18:34 +090075import static org.onosproject.security.AppGuard.checkPermission;
Sho SHIMIZU98c0a392016-01-14 18:40:53 -080076import static org.onosproject.security.AppPermission.Type.INTENT_READ;
77import static org.onosproject.security.AppPermission.Type.INTENT_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070078import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090079
Brian O'Connor66630c82014-10-02 21:08:19 -070080/**
Brian O'Connorce2d8b52015-07-29 16:24:13 -070081 * An implementation of intent service.
Brian O'Connor66630c82014-10-02 21:08:19 -070082 */
83@Component(immediate = true)
84@Service
85public class IntentManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070086 extends AbstractListenerManager<IntentEvent, IntentListener>
Brian O'Connor66630c82014-10-02 21:08:19 -070087 implements IntentService, IntentExtensionService {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070088
Sho SHIMIZU8b5051d2014-11-05 11:24:13 -080089 private static final Logger log = getLogger(IntentManager.class);
Brian O'Connor66630c82014-10-02 21:08:19 -070090
Sho SHIMIZU17c09da2016-03-31 12:54:17 -070091 private static final String INTENT_NULL = "Intent cannot be null";
92 private static final String INTENT_ID_NULL = "Intent key cannot be null";
Brian O'Connor66630c82014-10-02 21:08:19 -070093
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080094 private static final EnumSet<IntentState> RECOMPILE
95 = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ);
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070096 private static final EnumSet<IntentState> WITHDRAW
97 = EnumSet.of(WITHDRAW_REQ, WITHDRAWING, WITHDRAWN);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080098
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080099 private static final boolean DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL = false;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800100 @Property(name = "skipReleaseResourcesOnWithdrawal",
101 boolValue = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL,
102 label = "Indicates whether skipping resource releases on withdrawal is enabled or not")
103 private boolean skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800104
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800105 private static final int DEFAULT_NUM_THREADS = 12;
106 @Property(name = "numThreads",
107 intValue = DEFAULT_NUM_THREADS,
108 label = "Number of worker threads")
109 private int numThreads = DEFAULT_NUM_THREADS;
110
Brian O'Connor520c0522014-11-23 23:50:47 -0800111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected CoreService coreService;
Brian O'Connor66630c82014-10-02 21:08:19 -0700113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected IntentStore store;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom85258ee2014-10-07 00:10:02 -0700118 protected ObjectiveTrackerService trackerService;
tom95329eb2014-10-06 08:40:06 -0700119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU214ac322015-02-23 19:30:15 -0800121 protected FlowRuleService flowRuleService;
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700122
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800124 protected FlowObjectiveService flowObjectiveService;
125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800127 protected ResourceService resourceService;
128
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected ComponentConfigService configService;
131
helenyrwu2a674902016-07-20 09:48:04 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected GroupService groupService;
134
Brian O'Connordb15b042015-02-04 14:59:28 -0800135 private ExecutorService batchExecutor;
136 private ExecutorService workerExecutor;
Brian O'Connor520c0522014-11-23 23:50:47 -0800137
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800138 private final IntentInstaller intentInstaller = new IntentInstaller();
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800139 private final CompilerRegistry compilerRegistry = new CompilerRegistry();
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800140 private final InternalIntentProcessor processor = new InternalIntentProcessor();
Brian O'Connor520c0522014-11-23 23:50:47 -0800141 private final IntentStoreDelegate delegate = new InternalStoreDelegate();
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800142 private final IntentStoreDelegate testOnlyDelegate = new TestOnlyIntentStoreDelegate();
Brian O'Connor520c0522014-11-23 23:50:47 -0800143 private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate();
144 private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate();
145 private IdGenerator idGenerator;
146
Brian O'Connorb499b352015-02-03 16:46:15 -0800147 private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate);
Brian O'Connorcff03322015-02-03 15:28:59 -0800148
Brian O'Connor66630c82014-10-02 21:08:19 -0700149 @Activate
150 public void activate() {
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800151 configService.registerProperties(getClass());
152
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800153 intentInstaller.init(store, trackerService, flowRuleService, flowObjectiveService);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800154 if (skipReleaseResourcesOnWithdrawal) {
155 store.setDelegate(testOnlyDelegate);
156 } else {
157 store.setDelegate(delegate);
158 }
tom95329eb2014-10-06 08:40:06 -0700159 trackerService.setDelegate(topoDelegate);
Brian O'Connor66630c82014-10-02 21:08:19 -0700160 eventDispatcher.addSink(IntentEvent.class, listenerRegistry);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700161 batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch", log));
162 workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log));
Brian O'Connor520c0522014-11-23 23:50:47 -0800163 idGenerator = coreService.getIdGenerator("intent-ids");
164 Intent.bindIdGenerator(idGenerator);
Brian O'Connor66630c82014-10-02 21:08:19 -0700165 log.info("Started");
166 }
167
168 @Deactivate
169 public void deactivate() {
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800170 intentInstaller.init(null, null, null, null);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800171 if (skipReleaseResourcesOnWithdrawal) {
172 store.unsetDelegate(testOnlyDelegate);
173 } else {
174 store.unsetDelegate(delegate);
175 }
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800176 configService.unregisterProperties(getClass(), false);
tom95329eb2014-10-06 08:40:06 -0700177 trackerService.unsetDelegate(topoDelegate);
Brian O'Connor66630c82014-10-02 21:08:19 -0700178 eventDispatcher.removeSink(IntentEvent.class);
Brian O'Connordb15b042015-02-04 14:59:28 -0800179 batchExecutor.shutdown();
Brian O'Connor3c58e962015-04-28 23:21:51 -0700180 workerExecutor.shutdown();
Brian O'Connor520c0522014-11-23 23:50:47 -0800181 Intent.unbindIdGenerator(idGenerator);
Brian O'Connor66630c82014-10-02 21:08:19 -0700182 log.info("Stopped");
183 }
184
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800185 @Modified
186 public void modified(ComponentContext context) {
187 if (context == null) {
188 skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL;
189 logConfig("Default config");
190 return;
191 }
192
193 String s = Tools.get(context.getProperties(), "skipReleaseResourcesOnWithdrawal");
194 boolean newTestEnabled = isNullOrEmpty(s) ? skipReleaseResourcesOnWithdrawal : Boolean.parseBoolean(s.trim());
195 if (skipReleaseResourcesOnWithdrawal && !newTestEnabled) {
196 store.unsetDelegate(testOnlyDelegate);
197 store.setDelegate(delegate);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800198 skipReleaseResourcesOnWithdrawal = false;
199 logConfig("Reconfigured skip release resources on withdrawal");
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800200 } else if (!skipReleaseResourcesOnWithdrawal && newTestEnabled) {
201 store.unsetDelegate(delegate);
202 store.setDelegate(testOnlyDelegate);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800203 skipReleaseResourcesOnWithdrawal = true;
204 logConfig("Reconfigured skip release resources on withdrawal");
205 }
206
207 s = Tools.get(context.getProperties(), "numThreads");
208 int newNumThreads = isNullOrEmpty(s) ? numThreads : Integer.parseInt(s);
209 if (newNumThreads != numThreads) {
210 numThreads = newNumThreads;
211 ExecutorService oldWorkerExecutor = workerExecutor;
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800212 workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log));
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800213 if (oldWorkerExecutor != null) {
214 oldWorkerExecutor.shutdown();
215 }
216 logConfig("Reconfigured number of worker threads");
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800217 }
218 }
219
220 private void logConfig(String prefix) {
221 log.info("{} with skipReleaseResourcesOnWithdrawal = {}", prefix, skipReleaseResourcesOnWithdrawal);
222 }
223
Brian O'Connor66630c82014-10-02 21:08:19 -0700224 @Override
225 public void submit(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900226 checkPermission(INTENT_WRITE);
Brian O'Connor66630c82014-10-02 21:08:19 -0700227 checkNotNull(intent, INTENT_NULL);
Brian O'Connorcff03322015-02-03 15:28:59 -0800228 IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null);
Brian O'Connorcff03322015-02-03 15:28:59 -0800229 store.addPending(data);
Brian O'Connor66630c82014-10-02 21:08:19 -0700230 }
231
232 @Override
233 public void withdraw(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900234 checkPermission(INTENT_WRITE);
Brian O'Connor66630c82014-10-02 21:08:19 -0700235 checkNotNull(intent, INTENT_NULL);
Brian O'Connorcff03322015-02-03 15:28:59 -0800236 IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null);
Brian O'Connorcff03322015-02-03 15:28:59 -0800237 store.addPending(data);
Brian O'Connor66630c82014-10-02 21:08:19 -0700238 }
239
Brian O'Connor66630c82014-10-02 21:08:19 -0700240 @Override
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700241 public void purge(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900242 checkPermission(INTENT_WRITE);
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700243 checkNotNull(intent, INTENT_NULL);
244 IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null);
245 store.addPending(data);
helenyrwu2a674902016-07-20 09:48:04 -0700246
247 // remove associated group if there is one
248 if (intent instanceof PointToPointIntent) {
249 PointToPointIntent pointIntent = (PointToPointIntent) intent;
250 DeviceId deviceId = pointIntent.ingressPoint().deviceId();
251 GroupKey groupKey = PointToPointIntentCompiler.makeGroupKey(intent.id());
252 groupService.removeGroup(deviceId, groupKey,
253 intent.appId());
254 }
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700255 }
256
257 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800258 public Intent getIntent(Key key) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900259 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800260 return store.getIntent(key);
261 }
262
263 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700264 public Iterable<Intent> getIntents() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900265 checkPermission(INTENT_READ);
Brian O'Connor66630c82014-10-02 21:08:19 -0700266 return store.getIntents();
267 }
268
269 @Override
Brian O'Connor38224302016-08-02 22:03:01 -0700270 public void addPending(IntentData intentData) {
271 checkPermission(INTENT_WRITE);
272 checkNotNull(intentData, INTENT_NULL);
273 //TODO we might consider further checking / assertions
274 store.addPending(intentData);
275 }
276
277 @Override
Thomas Vachuskac46af202015-06-03 16:43:27 -0700278 public Iterable<IntentData> getIntentData() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900279 checkPermission(INTENT_READ);
Thomas Vachuskac46af202015-06-03 16:43:27 -0700280 return store.getIntentData(false, 0);
281 }
282
283 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700284 public long getIntentCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900285 checkPermission(INTENT_READ);
Brian O'Connor66630c82014-10-02 21:08:19 -0700286 return store.getIntentCount();
287 }
288
289 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800290 public IntentState getIntentState(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900291 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800292 checkNotNull(intentKey, INTENT_ID_NULL);
293 return store.getIntentState(intentKey);
Brian O'Connor66630c82014-10-02 21:08:19 -0700294 }
295
296 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800297 public List<Intent> getInstallableIntents(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900298 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800299 checkNotNull(intentKey, INTENT_ID_NULL);
300 return store.getInstallableIntents(intentKey);
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700301 }
302
303 @Override
Brian O'Connorbe28a872015-02-19 21:44:37 -0800304 public boolean isLocal(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900305 checkPermission(INTENT_READ);
Brian O'Connorbe28a872015-02-19 21:44:37 -0800306 return store.isMaster(intentKey);
307 }
308
309 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700310 public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900311 checkPermission(INTENT_WRITE);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800312 compilerRegistry.registerCompiler(cls, compiler);
Brian O'Connor66630c82014-10-02 21:08:19 -0700313 }
314
315 @Override
316 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900317 checkPermission(INTENT_WRITE);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800318 compilerRegistry.unregisterCompiler(cls);
Brian O'Connor66630c82014-10-02 21:08:19 -0700319 }
320
321 @Override
322 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900323 checkPermission(INTENT_READ);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800324 return compilerRegistry.getCompilers();
Brian O'Connor66630c82014-10-02 21:08:19 -0700325 }
326
327 @Override
Jonathan Hart34f1e382015-02-24 16:52:23 -0800328 public Iterable<Intent> getPending() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900329 checkPermission(INTENT_READ);
Jonathan Hart34f1e382015-02-24 16:52:23 -0800330 return store.getPending();
331 }
332
Brian O'Connor66630c82014-10-02 21:08:19 -0700333 // Store delegate to re-post events emitted from the store.
334 private class InternalStoreDelegate implements IntentStoreDelegate {
335 @Override
336 public void notify(IntentEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700337 post(event);
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800338 switch (event.type()) {
339 case WITHDRAWN:
340 // release resources allocated to withdrawn intent
341 if (!resourceService.release(event.subject().id())) {
342 log.error("Failed to release resources allocated to {}", event.subject().id());
343 }
344 break;
345 default:
346 break;
347 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700348 }
Brian O'Connorea4d7d12015-01-28 16:37:46 -0800349
350 @Override
Brian O'Connorcff03322015-02-03 15:28:59 -0800351 public void process(IntentData data) {
352 accumulator.add(data);
Brian O'Connorea4d7d12015-01-28 16:37:46 -0800353 }
Thomas Vachuskac46af202015-06-03 16:43:27 -0700354
355 @Override
356 public void onUpdate(IntentData intentData) {
357 trackerService.trackIntent(intentData);
358 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700359 }
360
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800361 // Store delegate enabled only when performing intent throughput tests
362 private class TestOnlyIntentStoreDelegate implements IntentStoreDelegate {
363 @Override
364 public void process(IntentData data) {
365 accumulator.add(data);
366 }
367
368 @Override
369 public void onUpdate(IntentData data) {
370 trackerService.trackIntent(data);
371 }
372
373 @Override
374 public void notify(IntentEvent event) {
375 post(event);
376 }
377 }
378
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800379 private void buildAndSubmitBatches(Iterable<Key> intentKeys,
Brian O'Connor72a034c2014-11-26 18:24:23 -0800380 boolean compileAllFailed) {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800381 // Attempt recompilation of the specified intents first.
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800382 for (Key key : intentKeys) {
383 Intent intent = store.getIntent(key);
Brian O'Connor72a034c2014-11-26 18:24:23 -0800384 if (intent == null) {
385 continue;
386 }
Brian O'Connor03406a42015-02-03 17:28:57 -0800387 submit(intent);
Brian O'Connor72a034c2014-11-26 18:24:23 -0800388 }
389
Jonathan Hart0cca3e82015-09-23 17:54:11 -0700390 if (compileAllFailed) {
391 // If required, compile all currently failed intents.
392 for (Intent intent : getIntents()) {
393 IntentState state = getIntentState(intent.key());
394 if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) {
395 if (WITHDRAW.contains(state)) {
396 withdraw(intent);
397 } else {
398 submit(intent);
399 }
Brian O'Connor72a034c2014-11-26 18:24:23 -0800400 }
401 }
402 }
Brian O'Connor72a034c2014-11-26 18:24:23 -0800403 }
404
tom95329eb2014-10-06 08:40:06 -0700405 // Topology change delegate
406 private class InternalTopoChangeDelegate implements TopologyChangeDelegate {
407 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800408 public void triggerCompile(Iterable<Key> intentKeys,
tom85258ee2014-10-07 00:10:02 -0700409 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800410 buildAndSubmitBatches(intentKeys, compileAllFailed);
tom95329eb2014-10-06 08:40:06 -0700411 }
tom95329eb2014-10-06 08:40:06 -0700412 }
tom85258ee2014-10-07 00:10:02 -0700413
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700414 private class InternalBatchDelegate implements IntentBatchDelegate {
415 @Override
Brian O'Connorb499b352015-02-03 16:46:15 -0800416 public void execute(Collection<IntentData> operations) {
Brian O'Connorab8ef822015-02-17 18:08:54 -0800417 log.debug("Execute {} operation(s).", operations.size());
418 log.trace("Execute operations: {}", operations);
Sho SHIMIZU94b03b12015-04-10 14:53:13 -0700419
420 // batchExecutor is single-threaded, so only one batch is in flight at a time
Sho SHIMIZU489aa9b2016-01-14 17:19:32 -0800421 CompletableFuture.runAsync(() -> {
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800422 // process intent until the phase reaches one of the final phases
423 List<CompletableFuture<IntentData>> futures = operations.stream()
424 .map(x -> CompletableFuture.completedFuture(x)
425 .thenApply(IntentManager.this::createInitialPhase)
Sho SHIMIZU4a141852016-01-14 18:55:40 -0800426 .thenApplyAsync(IntentProcessPhase::process, workerExecutor)
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800427 .thenApply(FinalIntentProcessPhase::data)
428 .exceptionally(e -> {
429 //FIXME
430 log.warn("Future failed: {}", e);
431 return null;
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800432 }))
433 .collect(Collectors.toList());
Sho SHIMIZU62bbc602016-01-13 16:54:35 -0800434
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800435 // write multiple data to store in order
436 store.batchWrite(Tools.allOf(futures).join().stream()
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800437 .filter(Objects::nonNull)
438 .collect(Collectors.toList()));
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800439 }, batchExecutor).exceptionally(e -> {
440 log.error("Error submitting batches:", e);
441 // FIXME incomplete Intents should be cleaned up
442 // (transition to FAILED, etc.)
443
444 // the batch has failed
445 // TODO: maybe we should do more?
446 log.error("Walk the plank, matey...");
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800447 return null;
448 }).thenRun(accumulator::ready);
449
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700450 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700451 }
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800452
Sho SHIMIZUc88b85e2016-01-14 18:45:14 -0800453 private IntentProcessPhase createInitialPhase(IntentData data) {
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800454 IntentData pending = store.getPendingData(data.key());
455 if (pending == null || pending.version().isNewerThan(data.version())) {
456 /*
457 If the pending map is null, then this intent was compiled by a
458 previous batch iteration, so we can skip it.
459 If the pending map has a newer request, it will get compiled as
460 part of the next batch, so we can skip it.
461 */
462 return Skipped.getPhase();
463 }
Sho SHIMIZUc88b85e2016-01-14 18:45:14 -0800464 IntentData current = store.getIntentData(data.key());
465 return newInitialPhase(processor, data, current);
466 }
467
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800468 private class InternalIntentProcessor implements IntentProcessor {
469 @Override
470 public List<Intent> compile(Intent intent, List<Intent> previousInstallables) {
471 return compilerRegistry.compile(intent, previousInstallables);
472 }
473
474 @Override
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700475 public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) {
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800476 intentInstaller.apply(toUninstall, toInstall);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800477 }
Ray Milkeyfd7931d2015-03-30 13:58:38 -0700478 }
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800479
Brian O'Connor66630c82014-10-02 21:08:19 -0700480}