blob: 68304ebc83adeca763d26e42f579c6643ad25c50 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.FlowRuleService;
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -080034import org.onosproject.net.flowobjective.FlowObjectiveService;
helenyrwu2a674902016-07-20 09:48:04 -070035import org.onosproject.net.group.GroupKey;
36import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.intent.Intent;
38import org.onosproject.net.intent.IntentBatchDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.IntentCompiler;
Brian O'Connorcff03322015-02-03 15:28:59 -080040import org.onosproject.net.intent.IntentData;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.intent.IntentEvent;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.intent.IntentExtensionService;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.intent.IntentService;
45import org.onosproject.net.intent.IntentState;
46import org.onosproject.net.intent.IntentStore;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.intent.IntentStoreDelegate;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080048import org.onosproject.net.intent.Key;
helenyrwu2a674902016-07-20 09:48:04 -070049import org.onosproject.net.intent.PointToPointIntent;
50import org.onosproject.net.intent.impl.compiler.PointToPointIntentCompiler;
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080051import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080052import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
Brian O'Connorc590ebb2016-12-08 18:16:41 -080053import org.onosproject.net.intent.impl.phase.Skipped;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080054import org.osgi.service.component.ComponentContext;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080055import org.onosproject.net.resource.ResourceService;
Brian O'Connor66630c82014-10-02 21:08:19 -070056import org.slf4j.Logger;
57
Brian O'Connorf0c5a052015-04-27 00:34:53 -070058import java.util.Collection;
59import java.util.EnumSet;
60import java.util.List;
61import java.util.Map;
Sho SHIMIZUab541a52016-01-13 23:29:32 -080062import java.util.Objects;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070063import java.util.Optional;
Sho SHIMIZUad8ab272016-01-13 22:08:58 -080064import java.util.concurrent.CompletableFuture;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070065import java.util.concurrent.ExecutorService;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070066import java.util.stream.Collectors;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070067
Brian O'Connorfa81eae2014-10-30 13:20:05 -070068import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080069import static com.google.common.base.Strings.isNullOrEmpty;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080070import static java.util.concurrent.Executors.newFixedThreadPool;
Brian O'Connordb15b042015-02-04 14:59:28 -080071import static java.util.concurrent.Executors.newSingleThreadExecutor;
Brian O'Connorbdc7f002015-02-18 20:49:41 -080072import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -080073import static org.onosproject.net.intent.IntentState.*;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070074import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080075import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase;
Changhoon Yoon541ef712015-05-23 17:18:34 +090076import static org.onosproject.security.AppGuard.checkPermission;
Sho SHIMIZU98c0a392016-01-14 18:40:53 -080077import static org.onosproject.security.AppPermission.Type.INTENT_READ;
78import static org.onosproject.security.AppPermission.Type.INTENT_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070079import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090080
Brian O'Connor66630c82014-10-02 21:08:19 -070081/**
Brian O'Connorce2d8b52015-07-29 16:24:13 -070082 * An implementation of intent service.
Brian O'Connor66630c82014-10-02 21:08:19 -070083 */
84@Component(immediate = true)
85@Service
86public class IntentManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070087 extends AbstractListenerManager<IntentEvent, IntentListener>
Brian O'Connor66630c82014-10-02 21:08:19 -070088 implements IntentService, IntentExtensionService {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070089
Sho SHIMIZU8b5051d2014-11-05 11:24:13 -080090 private static final Logger log = getLogger(IntentManager.class);
Brian O'Connor66630c82014-10-02 21:08:19 -070091
Sho SHIMIZU17c09da2016-03-31 12:54:17 -070092 private static final String INTENT_NULL = "Intent cannot be null";
93 private static final String INTENT_ID_NULL = "Intent key cannot be null";
Brian O'Connor66630c82014-10-02 21:08:19 -070094
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080095 private static final EnumSet<IntentState> RECOMPILE
96 = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ);
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070097 private static final EnumSet<IntentState> WITHDRAW
98 = EnumSet.of(WITHDRAW_REQ, WITHDRAWING, WITHDRAWN);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -080099
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800100 private static final boolean DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL = false;
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800101 @Property(name = "skipReleaseResourcesOnWithdrawal",
102 boolValue = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL,
103 label = "Indicates whether skipping resource releases on withdrawal is enabled or not")
104 private boolean skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800105
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800106 private static final int DEFAULT_NUM_THREADS = 12;
107 @Property(name = "numThreads",
108 intValue = DEFAULT_NUM_THREADS,
109 label = "Number of worker threads")
110 private int numThreads = DEFAULT_NUM_THREADS;
111
Brian O'Connor520c0522014-11-23 23:50:47 -0800112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected CoreService coreService;
Brian O'Connor66630c82014-10-02 21:08:19 -0700114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected IntentStore store;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom85258ee2014-10-07 00:10:02 -0700119 protected ObjectiveTrackerService trackerService;
tom95329eb2014-10-06 08:40:06 -0700120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU214ac322015-02-23 19:30:15 -0800122 protected FlowRuleService flowRuleService;
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700123
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800125 protected FlowObjectiveService flowObjectiveService;
126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800128 protected ResourceService resourceService;
129
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected ComponentConfigService configService;
132
helenyrwu2a674902016-07-20 09:48:04 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected GroupService groupService;
135
Andreas Papazois05548962016-11-23 11:36:55 +0200136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 private NetworkConfigService networkConfigService;
138
139
Brian O'Connordb15b042015-02-04 14:59:28 -0800140 private ExecutorService batchExecutor;
141 private ExecutorService workerExecutor;
Brian O'Connor520c0522014-11-23 23:50:47 -0800142
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800143 private final IntentInstaller intentInstaller = new IntentInstaller();
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800144 private final CompilerRegistry compilerRegistry = new CompilerRegistry();
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800145 private final InternalIntentProcessor processor = new InternalIntentProcessor();
Brian O'Connor520c0522014-11-23 23:50:47 -0800146 private final IntentStoreDelegate delegate = new InternalStoreDelegate();
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800147 private final IntentStoreDelegate testOnlyDelegate = new TestOnlyIntentStoreDelegate();
Brian O'Connor520c0522014-11-23 23:50:47 -0800148 private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate();
149 private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate();
150 private IdGenerator idGenerator;
151
Brian O'Connorb499b352015-02-03 16:46:15 -0800152 private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate);
Brian O'Connorcff03322015-02-03 15:28:59 -0800153
Brian O'Connor66630c82014-10-02 21:08:19 -0700154 @Activate
155 public void activate() {
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800156 configService.registerProperties(getClass());
157
Andreas Papazois05548962016-11-23 11:36:55 +0200158 intentInstaller.init(store, trackerService, flowRuleService, flowObjectiveService,
159 networkConfigService);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800160 if (skipReleaseResourcesOnWithdrawal) {
161 store.setDelegate(testOnlyDelegate);
162 } else {
163 store.setDelegate(delegate);
164 }
tom95329eb2014-10-06 08:40:06 -0700165 trackerService.setDelegate(topoDelegate);
Brian O'Connor66630c82014-10-02 21:08:19 -0700166 eventDispatcher.addSink(IntentEvent.class, listenerRegistry);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700167 batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch", log));
168 workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log));
Brian O'Connor520c0522014-11-23 23:50:47 -0800169 idGenerator = coreService.getIdGenerator("intent-ids");
170 Intent.bindIdGenerator(idGenerator);
Brian O'Connor66630c82014-10-02 21:08:19 -0700171 log.info("Started");
172 }
173
174 @Deactivate
175 public void deactivate() {
Andreas Papazois05548962016-11-23 11:36:55 +0200176 intentInstaller.init(null, null, null, null, null);
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800177 if (skipReleaseResourcesOnWithdrawal) {
178 store.unsetDelegate(testOnlyDelegate);
179 } else {
180 store.unsetDelegate(delegate);
181 }
Sho SHIMIZUd5bf1062016-02-25 10:26:02 -0800182 configService.unregisterProperties(getClass(), false);
tom95329eb2014-10-06 08:40:06 -0700183 trackerService.unsetDelegate(topoDelegate);
Brian O'Connor66630c82014-10-02 21:08:19 -0700184 eventDispatcher.removeSink(IntentEvent.class);
Brian O'Connordb15b042015-02-04 14:59:28 -0800185 batchExecutor.shutdown();
Brian O'Connor3c58e962015-04-28 23:21:51 -0700186 workerExecutor.shutdown();
Brian O'Connor520c0522014-11-23 23:50:47 -0800187 Intent.unbindIdGenerator(idGenerator);
Brian O'Connor66630c82014-10-02 21:08:19 -0700188 log.info("Stopped");
189 }
190
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800191 @Modified
192 public void modified(ComponentContext context) {
193 if (context == null) {
194 skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL;
195 logConfig("Default config");
196 return;
197 }
198
199 String s = Tools.get(context.getProperties(), "skipReleaseResourcesOnWithdrawal");
200 boolean newTestEnabled = isNullOrEmpty(s) ? skipReleaseResourcesOnWithdrawal : Boolean.parseBoolean(s.trim());
201 if (skipReleaseResourcesOnWithdrawal && !newTestEnabled) {
202 store.unsetDelegate(testOnlyDelegate);
203 store.setDelegate(delegate);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800204 skipReleaseResourcesOnWithdrawal = false;
205 logConfig("Reconfigured skip release resources on withdrawal");
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800206 } else if (!skipReleaseResourcesOnWithdrawal && newTestEnabled) {
207 store.unsetDelegate(delegate);
208 store.setDelegate(testOnlyDelegate);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800209 skipReleaseResourcesOnWithdrawal = true;
210 logConfig("Reconfigured skip release resources on withdrawal");
211 }
212
213 s = Tools.get(context.getProperties(), "numThreads");
214 int newNumThreads = isNullOrEmpty(s) ? numThreads : Integer.parseInt(s);
215 if (newNumThreads != numThreads) {
216 numThreads = newNumThreads;
217 ExecutorService oldWorkerExecutor = workerExecutor;
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800218 workerExecutor = newFixedThreadPool(numThreads, groupedThreads("onos/intent", "worker-%d", log));
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800219 if (oldWorkerExecutor != null) {
220 oldWorkerExecutor.shutdown();
221 }
222 logConfig("Reconfigured number of worker threads");
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800223 }
224 }
225
226 private void logConfig(String prefix) {
227 log.info("{} with skipReleaseResourcesOnWithdrawal = {}", prefix, skipReleaseResourcesOnWithdrawal);
228 }
229
Brian O'Connor66630c82014-10-02 21:08:19 -0700230 @Override
231 public void submit(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900232 checkPermission(INTENT_WRITE);
Brian O'Connor66630c82014-10-02 21:08:19 -0700233 checkNotNull(intent, INTENT_NULL);
Brian O'Connorcff03322015-02-03 15:28:59 -0800234 IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null);
Brian O'Connorcff03322015-02-03 15:28:59 -0800235 store.addPending(data);
Brian O'Connor66630c82014-10-02 21:08:19 -0700236 }
237
238 @Override
239 public void withdraw(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900240 checkPermission(INTENT_WRITE);
Brian O'Connor66630c82014-10-02 21:08:19 -0700241 checkNotNull(intent, INTENT_NULL);
Brian O'Connorcff03322015-02-03 15:28:59 -0800242 IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null);
Brian O'Connorcff03322015-02-03 15:28:59 -0800243 store.addPending(data);
Brian O'Connor66630c82014-10-02 21:08:19 -0700244 }
245
Brian O'Connor66630c82014-10-02 21:08:19 -0700246 @Override
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700247 public void purge(Intent intent) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900248 checkPermission(INTENT_WRITE);
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700249 checkNotNull(intent, INTENT_NULL);
250 IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null);
251 store.addPending(data);
helenyrwu2a674902016-07-20 09:48:04 -0700252
253 // remove associated group if there is one
254 if (intent instanceof PointToPointIntent) {
255 PointToPointIntent pointIntent = (PointToPointIntent) intent;
256 DeviceId deviceId = pointIntent.ingressPoint().deviceId();
257 GroupKey groupKey = PointToPointIntentCompiler.makeGroupKey(intent.id());
258 groupService.removeGroup(deviceId, groupKey,
259 intent.appId());
260 }
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700261 }
262
263 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800264 public Intent getIntent(Key key) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900265 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800266 return store.getIntent(key);
267 }
268
269 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700270 public Iterable<Intent> getIntents() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900271 checkPermission(INTENT_READ);
Brian O'Connor66630c82014-10-02 21:08:19 -0700272 return store.getIntents();
273 }
274
275 @Override
Brian O'Connor38224302016-08-02 22:03:01 -0700276 public void addPending(IntentData intentData) {
277 checkPermission(INTENT_WRITE);
278 checkNotNull(intentData, INTENT_NULL);
279 //TODO we might consider further checking / assertions
280 store.addPending(intentData);
281 }
282
283 @Override
Thomas Vachuskac46af202015-06-03 16:43:27 -0700284 public Iterable<IntentData> getIntentData() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900285 checkPermission(INTENT_READ);
Thomas Vachuskac46af202015-06-03 16:43:27 -0700286 return store.getIntentData(false, 0);
287 }
288
289 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700290 public long getIntentCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900291 checkPermission(INTENT_READ);
Brian O'Connor66630c82014-10-02 21:08:19 -0700292 return store.getIntentCount();
293 }
294
295 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800296 public IntentState getIntentState(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900297 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800298 checkNotNull(intentKey, INTENT_ID_NULL);
299 return store.getIntentState(intentKey);
Brian O'Connor66630c82014-10-02 21:08:19 -0700300 }
301
302 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800303 public List<Intent> getInstallableIntents(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900304 checkPermission(INTENT_READ);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800305 checkNotNull(intentKey, INTENT_ID_NULL);
306 return store.getInstallableIntents(intentKey);
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700307 }
308
309 @Override
Brian O'Connorbe28a872015-02-19 21:44:37 -0800310 public boolean isLocal(Key intentKey) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900311 checkPermission(INTENT_READ);
Brian O'Connorbe28a872015-02-19 21:44:37 -0800312 return store.isMaster(intentKey);
313 }
314
315 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700316 public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900317 checkPermission(INTENT_WRITE);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800318 compilerRegistry.registerCompiler(cls, compiler);
Brian O'Connor66630c82014-10-02 21:08:19 -0700319 }
320
321 @Override
322 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900323 checkPermission(INTENT_WRITE);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800324 compilerRegistry.unregisterCompiler(cls);
Brian O'Connor66630c82014-10-02 21:08:19 -0700325 }
326
327 @Override
328 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900329 checkPermission(INTENT_READ);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800330 return compilerRegistry.getCompilers();
Brian O'Connor66630c82014-10-02 21:08:19 -0700331 }
332
333 @Override
Jonathan Hart34f1e382015-02-24 16:52:23 -0800334 public Iterable<Intent> getPending() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900335 checkPermission(INTENT_READ);
Jonathan Hart34f1e382015-02-24 16:52:23 -0800336 return store.getPending();
337 }
338
Brian O'Connor66630c82014-10-02 21:08:19 -0700339 // Store delegate to re-post events emitted from the store.
340 private class InternalStoreDelegate implements IntentStoreDelegate {
341 @Override
342 public void notify(IntentEvent event) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700343 post(event);
Naoki Shiotabbc7ead2016-01-20 14:10:38 -0800344 switch (event.type()) {
345 case WITHDRAWN:
346 // release resources allocated to withdrawn intent
347 if (!resourceService.release(event.subject().id())) {
348 log.error("Failed to release resources allocated to {}", event.subject().id());
349 }
350 break;
351 default:
352 break;
353 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700354 }
Brian O'Connorea4d7d12015-01-28 16:37:46 -0800355
356 @Override
Brian O'Connorcff03322015-02-03 15:28:59 -0800357 public void process(IntentData data) {
358 accumulator.add(data);
Brian O'Connorea4d7d12015-01-28 16:37:46 -0800359 }
Thomas Vachuskac46af202015-06-03 16:43:27 -0700360
361 @Override
362 public void onUpdate(IntentData intentData) {
363 trackerService.trackIntent(intentData);
364 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700365 }
366
Sho SHIMIZU4cee50f2016-02-23 16:24:42 -0800367 // Store delegate enabled only when performing intent throughput tests
368 private class TestOnlyIntentStoreDelegate implements IntentStoreDelegate {
369 @Override
370 public void process(IntentData data) {
371 accumulator.add(data);
372 }
373
374 @Override
375 public void onUpdate(IntentData data) {
376 trackerService.trackIntent(data);
377 }
378
379 @Override
380 public void notify(IntentEvent event) {
381 post(event);
382 }
383 }
384
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800385 private void buildAndSubmitBatches(Iterable<Key> intentKeys,
Brian O'Connor72a034c2014-11-26 18:24:23 -0800386 boolean compileAllFailed) {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800387 // Attempt recompilation of the specified intents first.
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800388 for (Key key : intentKeys) {
389 Intent intent = store.getIntent(key);
Brian O'Connor72a034c2014-11-26 18:24:23 -0800390 if (intent == null) {
391 continue;
392 }
Brian O'Connor03406a42015-02-03 17:28:57 -0800393 submit(intent);
Brian O'Connor72a034c2014-11-26 18:24:23 -0800394 }
395
Jonathan Hart0cca3e82015-09-23 17:54:11 -0700396 if (compileAllFailed) {
397 // If required, compile all currently failed intents.
398 for (Intent intent : getIntents()) {
399 IntentState state = getIntentState(intent.key());
400 if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) {
401 if (WITHDRAW.contains(state)) {
402 withdraw(intent);
403 } else {
404 submit(intent);
405 }
Brian O'Connor72a034c2014-11-26 18:24:23 -0800406 }
407 }
408 }
Brian O'Connor72a034c2014-11-26 18:24:23 -0800409 }
410
tom95329eb2014-10-06 08:40:06 -0700411 // Topology change delegate
412 private class InternalTopoChangeDelegate implements TopologyChangeDelegate {
413 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800414 public void triggerCompile(Iterable<Key> intentKeys,
tom85258ee2014-10-07 00:10:02 -0700415 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800416 buildAndSubmitBatches(intentKeys, compileAllFailed);
tom95329eb2014-10-06 08:40:06 -0700417 }
tom95329eb2014-10-06 08:40:06 -0700418 }
tom85258ee2014-10-07 00:10:02 -0700419
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700420 private class InternalBatchDelegate implements IntentBatchDelegate {
421 @Override
Brian O'Connorb499b352015-02-03 16:46:15 -0800422 public void execute(Collection<IntentData> operations) {
Brian O'Connorab8ef822015-02-17 18:08:54 -0800423 log.debug("Execute {} operation(s).", operations.size());
424 log.trace("Execute operations: {}", operations);
Sho SHIMIZU94b03b12015-04-10 14:53:13 -0700425
426 // batchExecutor is single-threaded, so only one batch is in flight at a time
Sho SHIMIZU489aa9b2016-01-14 17:19:32 -0800427 CompletableFuture.runAsync(() -> {
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800428 // process intent until the phase reaches one of the final phases
429 List<CompletableFuture<IntentData>> futures = operations.stream()
430 .map(x -> CompletableFuture.completedFuture(x)
431 .thenApply(IntentManager.this::createInitialPhase)
Sho SHIMIZU4a141852016-01-14 18:55:40 -0800432 .thenApplyAsync(IntentProcessPhase::process, workerExecutor)
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800433 .thenApply(FinalIntentProcessPhase::data)
434 .exceptionally(e -> {
435 //FIXME
436 log.warn("Future failed: {}", e);
437 return null;
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800438 }))
439 .collect(Collectors.toList());
Sho SHIMIZU62bbc602016-01-13 16:54:35 -0800440
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800441 // write multiple data to store in order
442 store.batchWrite(Tools.allOf(futures).join().stream()
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800443 .filter(Objects::nonNull)
444 .collect(Collectors.toList()));
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800445 }, batchExecutor).exceptionally(e -> {
446 log.error("Error submitting batches:", e);
447 // FIXME incomplete Intents should be cleaned up
448 // (transition to FAILED, etc.)
449
450 // the batch has failed
451 // TODO: maybe we should do more?
452 log.error("Walk the plank, matey...");
Sho SHIMIZU8f2b7772016-01-14 18:17:44 -0800453 return null;
454 }).thenRun(accumulator::ready);
455
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700456 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700457 }
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800458
Sho SHIMIZUc88b85e2016-01-14 18:45:14 -0800459 private IntentProcessPhase createInitialPhase(IntentData data) {
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800460 IntentData pending = store.getPendingData(data.key());
461 if (pending == null || pending.version().isNewerThan(data.version())) {
462 /*
463 If the pending map is null, then this intent was compiled by a
464 previous batch iteration, so we can skip it.
465 If the pending map has a newer request, it will get compiled as
466 part of the next batch, so we can skip it.
467 */
468 return Skipped.getPhase();
469 }
Sho SHIMIZUc88b85e2016-01-14 18:45:14 -0800470 IntentData current = store.getIntentData(data.key());
471 return newInitialPhase(processor, data, current);
472 }
473
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800474 private class InternalIntentProcessor implements IntentProcessor {
475 @Override
476 public List<Intent> compile(Intent intent, List<Intent> previousInstallables) {
477 return compilerRegistry.compile(intent, previousInstallables);
478 }
479
480 @Override
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700481 public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) {
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -0800482 intentInstaller.apply(toUninstall, toInstall);
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800483 }
Ray Milkeyfd7931d2015-03-30 13:58:38 -0700484 }
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -0800485
Brian O'Connor66630c82014-10-02 21:08:19 -0700486}