Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Sho SHIMIZU | 8992524 | 2016-01-13 14:20:34 -0800 | [diff] [blame] | 2 | * Copyright 2014-2016 Open Networking Laboratory |
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 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | e098172 | 2016-01-14 16:02:48 -0800 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame^] | 27 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.IdGenerator; |
Sho SHIMIZU | 98c0a39 | 2016-01-14 18:40:53 -0800 | [diff] [blame] | 30 | import org.onosproject.event.AbstractListenerManager; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.FlowRuleService; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Intent; |
| 34 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.IntentExtensionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentListener; |
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; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 47 | import org.onosproject.net.newresource.ResourceService; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 48 | import org.osgi.service.component.ComponentContext; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 51 | import java.util.Collection; |
| 52 | import java.util.EnumSet; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
Sho SHIMIZU | ab541a5 | 2016-01-13 23:29:32 -0800 | [diff] [blame] | 55 | import java.util.Objects; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 56 | import java.util.Optional; |
Sho SHIMIZU | ad8ab27 | 2016-01-13 22:08:58 -0800 | [diff] [blame] | 57 | import java.util.concurrent.CompletableFuture; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 59 | import java.util.stream.Collectors; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 60 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 61 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 62 | import static com.google.common.base.Strings.isNullOrEmpty; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 63 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 64 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 65 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 66 | import static org.onosproject.net.intent.IntentState.*; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 67 | import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 69 | import static org.onosproject.security.AppGuard.checkPermission; |
Sho SHIMIZU | 98c0a39 | 2016-01-14 18:40:53 -0800 | [diff] [blame] | 70 | import static org.onosproject.security.AppPermission.Type.INTENT_READ; |
| 71 | import static org.onosproject.security.AppPermission.Type.INTENT_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 72 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 73 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 74 | /** |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 75 | * An implementation of intent service. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 76 | */ |
| 77 | @Component(immediate = true) |
| 78 | @Service |
| 79 | public class IntentManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 80 | extends AbstractListenerManager<IntentEvent, IntentListener> |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 81 | implements IntentService, IntentExtensionService { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 82 | |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 83 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 84 | |
| 85 | public static final String INTENT_NULL = "Intent cannot be null"; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 86 | public static final String INTENT_ID_NULL = "Intent key cannot be null"; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 87 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 88 | private static final int NUM_THREADS = 12; |
| 89 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 90 | private static final EnumSet<IntentState> RECOMPILE |
| 91 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 92 | private static final EnumSet<IntentState> WITHDRAW |
| 93 | = EnumSet.of(WITHDRAW_REQ, WITHDRAWING, WITHDRAWN); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 94 | private static final boolean DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL = false; |
| 95 | |
| 96 | @Property(name = "skipReleaseResourcesOnWithdrawal", |
| 97 | boolValue = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL, |
| 98 | label = "Indicates whether skipping resource releases on withdrawal is enabled or not") |
| 99 | private boolean skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 100 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected IntentStore store; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 108 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 214ac32 | 2015-02-23 19:30:15 -0800 | [diff] [blame] | 111 | protected FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 112 | |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 114 | protected FlowObjectiveService flowObjectiveService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 117 | protected ResourceService resourceService; |
| 118 | |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame^] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected ComponentConfigService configService; |
| 121 | |
| 122 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 123 | private ExecutorService batchExecutor; |
| 124 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 125 | |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 126 | private final IntentInstaller intentInstaller = new IntentInstaller(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 127 | private final CompilerRegistry compilerRegistry = new CompilerRegistry(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 128 | private final InternalIntentProcessor processor = new InternalIntentProcessor(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 129 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 130 | private final IntentStoreDelegate testOnlyDelegate = new TestOnlyIntentStoreDelegate(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 131 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 132 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 133 | private IdGenerator idGenerator; |
| 134 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 135 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 136 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 137 | @Activate |
| 138 | public void activate() { |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame^] | 139 | configService.registerProperties(getClass()); |
| 140 | |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 141 | intentInstaller.init(store, trackerService, flowRuleService, flowObjectiveService); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 142 | if (skipReleaseResourcesOnWithdrawal) { |
| 143 | store.setDelegate(testOnlyDelegate); |
| 144 | } else { |
| 145 | store.setDelegate(delegate); |
| 146 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 147 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 148 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 149 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch")); |
| 150 | workerExecutor = newFixedThreadPool(NUM_THREADS, groupedThreads("onos/intent", "worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 151 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 152 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 153 | log.info("Started"); |
| 154 | } |
| 155 | |
| 156 | @Deactivate |
| 157 | public void deactivate() { |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 158 | intentInstaller.init(null, null, null, null); |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 159 | if (skipReleaseResourcesOnWithdrawal) { |
| 160 | store.unsetDelegate(testOnlyDelegate); |
| 161 | } else { |
| 162 | store.unsetDelegate(delegate); |
| 163 | } |
Sho SHIMIZU | d5bf106 | 2016-02-25 10:26:02 -0800 | [diff] [blame^] | 164 | configService.unregisterProperties(getClass(), false); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 165 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 166 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 167 | batchExecutor.shutdown(); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 168 | workerExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 169 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 170 | log.info("Stopped"); |
| 171 | } |
| 172 | |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 173 | @Modified |
| 174 | public void modified(ComponentContext context) { |
| 175 | if (context == null) { |
| 176 | skipReleaseResourcesOnWithdrawal = DEFAULT_SKIP_RELEASE_RESOURCES_ON_WITHDRAWAL; |
| 177 | logConfig("Default config"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | String s = Tools.get(context.getProperties(), "skipReleaseResourcesOnWithdrawal"); |
| 182 | boolean newTestEnabled = isNullOrEmpty(s) ? skipReleaseResourcesOnWithdrawal : Boolean.parseBoolean(s.trim()); |
| 183 | if (skipReleaseResourcesOnWithdrawal && !newTestEnabled) { |
| 184 | store.unsetDelegate(testOnlyDelegate); |
| 185 | store.setDelegate(delegate); |
| 186 | logConfig("Reconfigured"); |
| 187 | } else if (!skipReleaseResourcesOnWithdrawal && newTestEnabled) { |
| 188 | store.unsetDelegate(delegate); |
| 189 | store.setDelegate(testOnlyDelegate); |
| 190 | logConfig("Reconfigured"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private void logConfig(String prefix) { |
| 195 | log.info("{} with skipReleaseResourcesOnWithdrawal = {}", prefix, skipReleaseResourcesOnWithdrawal); |
| 196 | } |
| 197 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 198 | @Override |
| 199 | public void submit(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 200 | checkPermission(INTENT_WRITE); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 201 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 202 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 203 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void withdraw(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 208 | checkPermission(INTENT_WRITE); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 209 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 210 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 211 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 214 | @Override |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 215 | public void purge(Intent intent) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 216 | checkPermission(INTENT_WRITE); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 217 | checkNotNull(intent, INTENT_NULL); |
| 218 | IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null); |
| 219 | store.addPending(data); |
| 220 | } |
| 221 | |
| 222 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 223 | public Intent getIntent(Key key) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 224 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 225 | return store.getIntent(key); |
| 226 | } |
| 227 | |
| 228 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 229 | public Iterable<Intent> getIntents() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 230 | checkPermission(INTENT_READ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 231 | return store.getIntents(); |
| 232 | } |
| 233 | |
| 234 | @Override |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 235 | public Iterable<IntentData> getIntentData() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 236 | checkPermission(INTENT_READ); |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 237 | return store.getIntentData(false, 0); |
| 238 | } |
| 239 | |
| 240 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 241 | public long getIntentCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 242 | checkPermission(INTENT_READ); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 243 | return store.getIntentCount(); |
| 244 | } |
| 245 | |
| 246 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 247 | public IntentState getIntentState(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 248 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 249 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 250 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 254 | public List<Intent> getInstallableIntents(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 255 | checkPermission(INTENT_READ); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 256 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 257 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 261 | public boolean isLocal(Key intentKey) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 262 | checkPermission(INTENT_READ); |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 263 | return store.isMaster(intentKey); |
| 264 | } |
| 265 | |
| 266 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 267 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 268 | compilerRegistry.registerCompiler(cls, compiler); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 273 | compilerRegistry.unregisterCompiler(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 278 | return compilerRegistry.getCompilers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 282 | public Iterable<Intent> getPending() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 283 | checkPermission(INTENT_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 284 | |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 285 | return store.getPending(); |
| 286 | } |
| 287 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 288 | // Store delegate to re-post events emitted from the store. |
| 289 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 290 | @Override |
| 291 | public void notify(IntentEvent event) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 292 | post(event); |
Naoki Shiota | bbc7ead | 2016-01-20 14:10:38 -0800 | [diff] [blame] | 293 | switch (event.type()) { |
| 294 | case WITHDRAWN: |
| 295 | // release resources allocated to withdrawn intent |
| 296 | if (!resourceService.release(event.subject().id())) { |
| 297 | log.error("Failed to release resources allocated to {}", event.subject().id()); |
| 298 | } |
| 299 | break; |
| 300 | default: |
| 301 | break; |
| 302 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 303 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 304 | |
| 305 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 306 | public void process(IntentData data) { |
| 307 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 308 | } |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 309 | |
| 310 | @Override |
| 311 | public void onUpdate(IntentData intentData) { |
| 312 | trackerService.trackIntent(intentData); |
| 313 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Sho SHIMIZU | 4cee50f | 2016-02-23 16:24:42 -0800 | [diff] [blame] | 316 | // Store delegate enabled only when performing intent throughput tests |
| 317 | private class TestOnlyIntentStoreDelegate implements IntentStoreDelegate { |
| 318 | @Override |
| 319 | public void process(IntentData data) { |
| 320 | accumulator.add(data); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void onUpdate(IntentData data) { |
| 325 | trackerService.trackIntent(data); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void notify(IntentEvent event) { |
| 330 | post(event); |
| 331 | } |
| 332 | } |
| 333 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 334 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 335 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 336 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 337 | for (Key key : intentKeys) { |
| 338 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 339 | if (intent == null) { |
| 340 | continue; |
| 341 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 342 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Jonathan Hart | 0cca3e8 | 2015-09-23 17:54:11 -0700 | [diff] [blame] | 345 | if (compileAllFailed) { |
| 346 | // If required, compile all currently failed intents. |
| 347 | for (Intent intent : getIntents()) { |
| 348 | IntentState state = getIntentState(intent.key()); |
| 349 | if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) { |
| 350 | if (WITHDRAW.contains(state)) { |
| 351 | withdraw(intent); |
| 352 | } else { |
| 353 | submit(intent); |
| 354 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 358 | } |
| 359 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 360 | // Topology change delegate |
| 361 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 362 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 363 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 364 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 365 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 366 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 367 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 368 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 369 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 370 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 371 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 372 | log.debug("Execute {} operation(s).", operations.size()); |
| 373 | log.trace("Execute operations: {}", operations); |
Sho SHIMIZU | 94b03b1 | 2015-04-10 14:53:13 -0700 | [diff] [blame] | 374 | |
| 375 | // 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] | 376 | CompletableFuture.runAsync(() -> { |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 377 | // process intent until the phase reaches one of the final phases |
| 378 | List<CompletableFuture<IntentData>> futures = operations.stream() |
| 379 | .map(x -> CompletableFuture.completedFuture(x) |
| 380 | .thenApply(IntentManager.this::createInitialPhase) |
Sho SHIMIZU | 4a14185 | 2016-01-14 18:55:40 -0800 | [diff] [blame] | 381 | .thenApplyAsync(IntentProcessPhase::process, workerExecutor) |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 382 | .thenApply(FinalIntentProcessPhase::data) |
| 383 | .exceptionally(e -> { |
| 384 | //FIXME |
| 385 | log.warn("Future failed: {}", e); |
| 386 | return null; |
| 387 | })).collect(Collectors.toList()); |
Sho SHIMIZU | 62bbc60 | 2016-01-13 16:54:35 -0800 | [diff] [blame] | 388 | |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 389 | // write multiple data to store in order |
| 390 | store.batchWrite(Tools.allOf(futures).join().stream() |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 391 | .filter(Objects::nonNull) |
| 392 | .collect(Collectors.toList())); |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 393 | }, batchExecutor).exceptionally(e -> { |
| 394 | log.error("Error submitting batches:", e); |
| 395 | // FIXME incomplete Intents should be cleaned up |
| 396 | // (transition to FAILED, etc.) |
| 397 | |
| 398 | // the batch has failed |
| 399 | // TODO: maybe we should do more? |
| 400 | log.error("Walk the plank, matey..."); |
Sho SHIMIZU | 8f2b777 | 2016-01-14 18:17:44 -0800 | [diff] [blame] | 401 | return null; |
| 402 | }).thenRun(accumulator::ready); |
| 403 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 404 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 405 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 406 | |
Sho SHIMIZU | c88b85e | 2016-01-14 18:45:14 -0800 | [diff] [blame] | 407 | private IntentProcessPhase createInitialPhase(IntentData data) { |
| 408 | IntentData current = store.getIntentData(data.key()); |
| 409 | return newInitialPhase(processor, data, current); |
| 410 | } |
| 411 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 412 | private class InternalIntentProcessor implements IntentProcessor { |
| 413 | @Override |
| 414 | public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { |
| 415 | return compilerRegistry.compile(intent, previousInstallables); |
| 416 | } |
| 417 | |
| 418 | @Override |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 419 | public void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) { |
Thomas Vachuska | f6ec97b | 2016-02-22 10:59:23 -0800 | [diff] [blame] | 420 | intentInstaller.apply(toUninstall, toInstall); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 421 | } |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 422 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 423 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 424 | } |