Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.EnumSet; |
| 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Optional; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | import java.util.concurrent.ConcurrentMap; |
| 28 | import java.util.concurrent.ExecutionException; |
| 29 | import java.util.concurrent.ExecutorService; |
| 30 | import java.util.concurrent.Future; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | import java.util.concurrent.TimeoutException; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 35 | import org.apache.felix.scr.annotations.Activate; |
| 36 | import org.apache.felix.scr.annotations.Component; |
| 37 | import org.apache.felix.scr.annotations.Deactivate; |
| 38 | import org.apache.felix.scr.annotations.Reference; |
| 39 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 40 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.core.CoreService; |
| 42 | import org.onosproject.core.IdGenerator; |
| 43 | import org.onosproject.event.AbstractListenerRegistry; |
| 44 | import org.onosproject.event.EventDeliveryService; |
| 45 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 46 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 47 | import org.onosproject.net.flow.FlowRuleService; |
| 48 | import org.onosproject.net.intent.Intent; |
| 49 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 51 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.intent.IntentEvent; |
| 53 | import org.onosproject.net.intent.IntentException; |
| 54 | import org.onosproject.net.intent.IntentExtensionService; |
| 55 | import org.onosproject.net.intent.IntentId; |
| 56 | import org.onosproject.net.intent.IntentInstaller; |
| 57 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import org.onosproject.net.intent.IntentService; |
| 59 | import org.onosproject.net.intent.IntentState; |
| 60 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.intent.IntentStoreDelegate; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 64 | import com.google.common.collect.ImmutableList; |
| 65 | import com.google.common.collect.ImmutableMap; |
| 66 | import com.google.common.collect.Lists; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 67 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 68 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 69 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 70 | import static org.onlab.util.Tools.namedThreads; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 71 | import static org.onosproject.net.intent.IntentState.FAILED; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 72 | import static org.onosproject.net.intent.IntentState.INSTALLING; |
| 73 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 74 | import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 75 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * An implementation of Intent Manager. |
| 79 | */ |
| 80 | @Component(immediate = true) |
| 81 | @Service |
| 82 | public class IntentManager |
| 83 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 84 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 85 | |
| 86 | public static final String INTENT_NULL = "Intent cannot be null"; |
| 87 | public static final String INTENT_ID_NULL = "Intent ID cannot be null"; |
| 88 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 89 | private static final int NUM_THREADS = 12; |
| 90 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 91 | private static final EnumSet<IntentState> RECOMPILE |
| 92 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 93 | |
| 94 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 95 | // Collections for compiler, installer, and listener are ONOS instance local |
| 96 | private final ConcurrentMap<Class<? extends Intent>, |
| 97 | IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 98 | private final ConcurrentMap<Class<? extends Intent>, |
| 99 | IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 100 | |
| 101 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 102 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 103 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected IntentStore store; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 111 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 114 | protected EventDeliveryService eventDispatcher; |
| 115 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected FlowRuleService flowRuleService; |
| 118 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 119 | |
| 120 | private ExecutorService executor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 121 | |
| 122 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 123 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 124 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 125 | private IdGenerator idGenerator; |
| 126 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 127 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 128 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 129 | @Activate |
| 130 | public void activate() { |
| 131 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 132 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 133 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Yuta HIGUCHI | 7a9fddd | 2014-12-03 15:30:25 -0800 | [diff] [blame] | 134 | executor = newFixedThreadPool(NUM_THREADS, namedThreads("onos-intent-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 135 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 136 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 137 | log.info("Started"); |
| 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
| 142 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 143 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 144 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 145 | executor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 146 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 147 | log.info("Stopped"); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void submit(Intent intent) { |
| 152 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 153 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
| 154 | //FIXME timestamp? |
| 155 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void withdraw(Intent intent) { |
| 160 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 161 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
| 162 | //FIXME timestamp? |
| 163 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 166 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 167 | public Iterable<Intent> getIntents() { |
| 168 | return store.getIntents(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public long getIntentCount() { |
| 173 | return store.getIntentCount(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Intent getIntent(IntentId id) { |
| 178 | checkNotNull(id, INTENT_ID_NULL); |
| 179 | return store.getIntent(id); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public IntentState getIntentState(IntentId id) { |
| 184 | checkNotNull(id, INTENT_ID_NULL); |
| 185 | return store.getIntentState(id); |
| 186 | } |
| 187 | |
| 188 | @Override |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 189 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 190 | checkNotNull(intentId, INTENT_ID_NULL); |
| 191 | return store.getInstallableIntents(intentId); |
| 192 | } |
| 193 | |
| 194 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 195 | public void addListener(IntentListener listener) { |
| 196 | listenerRegistry.addListener(listener); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void removeListener(IntentListener listener) { |
| 201 | listenerRegistry.removeListener(listener); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 206 | compilers.put(cls, compiler); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 211 | compilers.remove(cls); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 216 | return ImmutableMap.copyOf(compilers); |
| 217 | } |
| 218 | |
| 219 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 220 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 221 | installers.put(cls, installer); |
| 222 | } |
| 223 | |
| 224 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 225 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 226 | installers.remove(cls); |
| 227 | } |
| 228 | |
| 229 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 230 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 231 | return ImmutableMap.copyOf(installers); |
| 232 | } |
| 233 | |
| 234 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 235 | * Returns the corresponding intent compiler to the specified intent. |
| 236 | * |
| 237 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 238 | * @param <T> the type of intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 239 | * @return intent compiler corresponding to the specified intent |
| 240 | */ |
| 241 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 242 | @SuppressWarnings("unchecked") |
| 243 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 244 | if (compiler == null) { |
| 245 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 246 | } |
| 247 | return compiler; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns the corresponding intent installer to the specified installable intent. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 252 | * |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 253 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 254 | * @param <T> the type of installable intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 255 | * @return intent installer corresponding to the specified installable intent |
| 256 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 257 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 258 | @SuppressWarnings("unchecked") |
| 259 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); |
| 260 | if (installer == null) { |
| 261 | throw new IntentException("no installer for class " + intent.getClass()); |
| 262 | } |
| 263 | return installer; |
| 264 | } |
| 265 | |
| 266 | /** |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 267 | * Compiles an intent recursively. |
| 268 | * |
| 269 | * @param intent intent |
| 270 | * @return result of compilation |
| 271 | */ |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 272 | private List<Intent> compileIntent(Intent intent, List<Intent> previousInstallables) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 273 | if (intent.isInstallable()) { |
| 274 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 275 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 276 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 277 | registerSubclassCompilerIfNeeded(intent); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 278 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 279 | List<Intent> installable = new ArrayList<>(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 280 | for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { |
| 281 | installable.addAll(compileIntent(compiled, previousInstallables)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 282 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 283 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 287 | * Uninstalls all installable intents associated with the given intent. |
| 288 | * |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 289 | * @param intent intent |
| 290 | * @param installables installable intents |
| 291 | * @return list of batches to uninstall intent |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 292 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 293 | private List<FlowRuleBatchOperation> uninstallIntent(Intent intent, List<Intent> installables) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 294 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 295 | for (Intent installable : installables) { |
| 296 | trackerService.removeTrackedResources(intent.id(), |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 297 | installable.resources()); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 298 | try { |
| 299 | batches.addAll(getInstaller(installable).uninstall(installable)); |
| 300 | } catch (IntentException e) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 301 | log.warn("Unable to uninstall intent {} due to:", intent.id(), e); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 302 | // TODO: this should never happen. but what if it does? |
| 303 | } |
| 304 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 305 | return batches; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 309 | * Registers an intent compiler of the specified intent if an intent compiler |
| 310 | * for the intent is not registered. This method traverses the class hierarchy of |
| 311 | * the intent. Once an intent compiler for a parent type is found, this method |
| 312 | * registers the found intent compiler. |
| 313 | * |
| 314 | * @param intent intent |
| 315 | */ |
| 316 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 317 | if (!compilers.containsKey(intent.getClass())) { |
| 318 | Class<?> cls = intent.getClass(); |
| 319 | while (cls != Object.class) { |
| 320 | // As long as we're within the Intent class descendants |
| 321 | if (Intent.class.isAssignableFrom(cls)) { |
| 322 | IntentCompiler<?> compiler = compilers.get(cls); |
| 323 | if (compiler != null) { |
| 324 | compilers.put(intent.getClass(), compiler); |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | cls = cls.getSuperclass(); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Registers an intent installer of the specified intent if an intent installer |
| 335 | * for the intent is not registered. This method traverses the class hierarchy of |
| 336 | * the intent. Once an intent installer for a parent type is found, this method |
| 337 | * registers the found intent installer. |
| 338 | * |
| 339 | * @param intent intent |
| 340 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 341 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 342 | if (!installers.containsKey(intent.getClass())) { |
| 343 | Class<?> cls = intent.getClass(); |
| 344 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 345 | // As long as we're within the Intent class descendants |
| 346 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 347 | IntentInstaller<?> installer = installers.get(cls); |
| 348 | if (installer != null) { |
| 349 | installers.put(intent.getClass(), installer); |
| 350 | return; |
| 351 | } |
| 352 | } |
| 353 | cls = cls.getSuperclass(); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 358 | // Store delegate to re-post events emitted from the store. |
| 359 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 360 | @Override |
| 361 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 362 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 363 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 364 | |
| 365 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 366 | public void process(IntentData data) { |
| 367 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 368 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 371 | private void buildAndSubmitBatches(Iterable<IntentId> intentIds, |
| 372 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 373 | // Attempt recompilation of the specified intents first. |
| 374 | for (IntentId id : intentIds) { |
| 375 | Intent intent = store.getIntent(id); |
| 376 | if (intent == null) { |
| 377 | continue; |
| 378 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 379 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (compileAllFailed) { |
| 383 | // If required, compile all currently failed intents. |
| 384 | for (Intent intent : getIntents()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 385 | IntentState state = getIntentState(intent.id()); |
| 386 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 387 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 388 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 389 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 390 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 391 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 396 | //FIXME |
| 397 | // for (ApplicationId appId : batches.keySet()) { |
| 398 | // if (batchService.isLocalLeader(appId)) { |
| 399 | // execute(batches.get(appId).build()); |
| 400 | // } |
| 401 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 402 | } |
| 403 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 404 | // Topology change delegate |
| 405 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 406 | @Override |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 407 | public void triggerCompile(Iterable<IntentId> intentIds, |
| 408 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 409 | buildAndSubmitBatches(intentIds, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 410 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 411 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 412 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 413 | // TODO: simplify the branching statements |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 414 | private IntentUpdate createIntentUpdate(IntentData intentData) { |
| 415 | IntentData currentState = store.getIntentData(intentData.key()); |
| 416 | switch (intentData.state()) { |
| 417 | case INSTALL_REQ: |
| 418 | return new InstallRequest(intentData.intent(), currentState); |
| 419 | case WITHDRAW_REQ: |
| 420 | return new WithdrawRequest(intentData.intent(), currentState); |
| 421 | // fallthrough |
| 422 | case COMPILING: |
| 423 | case INSTALLING: |
| 424 | case INSTALLED: |
| 425 | case RECOMPILING: |
| 426 | case WITHDRAWING: |
| 427 | case WITHDRAWN: |
| 428 | case FAILED: |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 429 | default: |
| 430 | // illegal state |
| 431 | return new DoNothing(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 432 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 435 | // TODO pull out the IntentUpdate inner classes |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 436 | private class InstallRequest implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 437 | |
| 438 | private final Intent intent; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 439 | private final IntentData currentState; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 440 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 441 | InstallRequest(Intent intent, IntentData currentState) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 442 | this.intent = checkNotNull(intent); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 443 | this.currentState = currentState; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Override |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 447 | public Optional<IntentUpdate> execute() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 448 | return Optional.of(new Compiling(intent)); //FIXME |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 452 | private class WithdrawRequest implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 453 | |
| 454 | private final Intent intent; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 455 | private final IntentData currentState; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 456 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 457 | WithdrawRequest(Intent intent, IntentData currentState) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 458 | this.intent = checkNotNull(intent); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 459 | this.currentState = currentState; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | @Override |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 463 | public Optional<IntentUpdate> execute() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 464 | return Optional.of(new Withdrawing(intent, currentState.installables())); //FIXME |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 468 | private class Compiling implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 469 | |
| 470 | private final Intent intent; |
| 471 | |
| 472 | Compiling(Intent intent) { |
| 473 | this.intent = checkNotNull(intent); |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public Optional<IntentUpdate> execute() { |
| 478 | try { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 479 | return Optional.of(new Installing(intent, compileIntent(intent, null))); |
| 480 | } catch (PathNotFoundException e) { |
Sho SHIMIZU | fd0cd8c | 2015-01-28 08:16:58 -0800 | [diff] [blame] | 481 | log.debug("Path not found for intent {}", intent); |
Sho SHIMIZU | c708f3b | 2015-02-04 09:19:01 -0800 | [diff] [blame] | 482 | // TODO: revisit to implement failure handling |
| 483 | return Optional.of(new DoNothing()); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 484 | } catch (IntentException e) { |
Sho SHIMIZU | fd0cd8c | 2015-01-28 08:16:58 -0800 | [diff] [blame] | 485 | log.warn("Unable to compile intent {} due to:", intent.id(), e); |
Sho SHIMIZU | c708f3b | 2015-02-04 09:19:01 -0800 | [diff] [blame] | 486 | // TODO: revisit to implement failure handling |
| 487 | return Optional.of(new DoNothing()); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // TODO: better naming because install() method actually generate FlowRuleBatchOperations |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 493 | private class Installing implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 494 | |
| 495 | private final Intent intent; |
| 496 | private final List<Intent> installables; |
| 497 | |
| 498 | Installing(Intent intent, List<Intent> installables) { |
| 499 | this.intent = checkNotNull(intent); |
| 500 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public Optional<IntentUpdate> execute() { |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame^] | 505 | try { |
| 506 | List<FlowRuleBatchOperation> converted = convert(installables); |
| 507 | // TODO: call FlowRuleService API to push FlowRules and track resources, |
| 508 | // which the submitted intent will use. |
| 509 | return Optional.of(new Installed(intent, installables, converted)); |
| 510 | } catch (FlowRuleBatchOperationConvertionException e) { |
| 511 | log.warn("Unable to install intent {} due to:", intent.id(), e.getCause()); |
| 512 | return Optional.of(new InstallingFailed(intent, installables, e.converted())); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 513 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame^] | 517 | private List<FlowRuleBatchOperation> convert(List<Intent> installables) { |
| 518 | List<FlowRuleBatchOperation> batches = new ArrayList<>(installables.size()); |
| 519 | for (Intent installable : installables) { |
| 520 | try { |
| 521 | registerSubclassInstallerIfNeeded(installable); |
| 522 | batches.addAll(getInstaller(installable).install(installable)); |
| 523 | } catch (Exception e) { // TODO this should be IntentException |
| 524 | throw new FlowRuleBatchOperationConvertionException(batches, e); |
| 525 | } |
| 526 | } |
| 527 | return batches; |
| 528 | } |
| 529 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 530 | private class Withdrawing implements IntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 531 | |
| 532 | private final Intent intent; |
| 533 | private final List<Intent> installables; |
| 534 | |
| 535 | Withdrawing(Intent intent, List<Intent> installables) { |
| 536 | this.intent = checkNotNull(intent); |
| 537 | this.installables = ImmutableList.copyOf(installables); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 540 | @Override |
| 541 | public Optional<IntentUpdate> execute() { |
| 542 | List<FlowRuleBatchOperation> batches = uninstallIntent(intent, installables); |
| 543 | |
| 544 | return Optional.of(new Withdrawn(intent, installables, batches)); |
| 545 | } |
| 546 | } |
| 547 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 548 | private class Installed implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 549 | |
| 550 | private final Intent intent; |
| 551 | private final List<Intent> installables; |
| 552 | private IntentState intentState; |
| 553 | private final List<FlowRuleBatchOperation> batches; |
| 554 | private int currentBatch = 0; |
| 555 | |
| 556 | Installed(Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) { |
| 557 | this.intent = checkNotNull(intent); |
| 558 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 559 | this.batches = new LinkedList<>(checkNotNull(batches)); |
| 560 | this.intentState = INSTALLING; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 563 | @Override |
| 564 | public void batchSuccess() { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 565 | currentBatch++; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 568 | @Override |
| 569 | public List<Intent> allInstallables() { |
| 570 | return installables; |
| 571 | } |
| 572 | |
| 573 | @Override |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 574 | public FlowRuleBatchOperation currentBatch() { |
| 575 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public void batchFailed() { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 580 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 581 | batches.remove(i); |
| 582 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 583 | intentState = FAILED; |
| 584 | batches.addAll(uninstallIntent(intent, installables)); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 585 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 586 | // TODO we might want to try to recompile the new intent |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 587 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 588 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 589 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 590 | private class Withdrawn implements CompletedIntentUpdate { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 591 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 592 | private final Intent intent; |
| 593 | private final List<Intent> installables; |
| 594 | private final List<FlowRuleBatchOperation> batches; |
| 595 | private int currentBatch; |
| 596 | |
| 597 | Withdrawn(Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) { |
| 598 | this.intent = checkNotNull(intent); |
| 599 | this.installables = ImmutableList.copyOf(installables); |
| 600 | this.batches = new LinkedList<>(batches); |
| 601 | this.currentBatch = 0; |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public List<Intent> allInstallables() { |
| 606 | return installables; |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public void batchSuccess() { |
| 611 | currentBatch++; |
| 612 | } |
| 613 | |
| 614 | @Override |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 615 | public FlowRuleBatchOperation currentBatch() { |
| 616 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public void batchFailed() { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 621 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
| 622 | batches.remove(i); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 623 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 624 | batches.addAll(uninstallIntent(intent, installables)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Sho SHIMIZU | 28e7271 | 2015-01-27 17:03:41 -0800 | [diff] [blame] | 628 | private class InstallingFailed implements CompletedIntentUpdate { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 629 | |
| 630 | private final Intent intent; |
| 631 | private final List<Intent> installables; |
| 632 | private final List<FlowRuleBatchOperation> batches; |
| 633 | private int currentBatch = 0; |
| 634 | |
| 635 | InstallingFailed(Intent intent, List<Intent> installables, List<FlowRuleBatchOperation> batches) { |
| 636 | this.intent = checkNotNull(intent); |
| 637 | this.installables = ImmutableList.copyOf(checkNotNull(installables)); |
| 638 | this.batches = new LinkedList<>(checkNotNull(batches)); |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public List<Intent> allInstallables() { |
| 643 | return installables; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public void batchSuccess() { |
| 648 | currentBatch++; |
| 649 | } |
| 650 | |
| 651 | @Override |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 652 | public FlowRuleBatchOperation currentBatch() { |
| 653 | return currentBatch < batches.size() ? batches.get(currentBatch) : null; |
| 654 | } |
| 655 | |
| 656 | @Override |
| 657 | public void batchFailed() { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 658 | for (int i = batches.size() - 1; i >= currentBatch; i--) { |
| 659 | batches.remove(i); |
| 660 | } |
| 661 | batches.addAll(uninstallIntent(intent, installables)); |
| 662 | |
| 663 | // TODO we might want to try to recompile the new intent |
| 664 | } |
| 665 | } |
| 666 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 667 | private class IntentBatchPreprocess implements Runnable { |
| 668 | |
| 669 | // TODO make this configurable |
| 670 | private static final int TIMEOUT_PER_OP = 500; // ms |
| 671 | protected static final int MAX_ATTEMPTS = 3; |
| 672 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 673 | protected final Collection<IntentData> ops; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 674 | |
| 675 | // future holding current FlowRuleBatch installation result |
| 676 | protected final long startTime = System.currentTimeMillis(); |
| 677 | protected final long endTime; |
| 678 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 679 | private IntentBatchPreprocess(Collection<IntentData> ops, long endTime) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 680 | this.ops = checkNotNull(ops); |
| 681 | this.endTime = endTime; |
| 682 | } |
| 683 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 684 | public IntentBatchPreprocess(Collection<IntentData> ops) { |
| 685 | this(ops, System.currentTimeMillis() + ops.size() * TIMEOUT_PER_OP); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // FIXME compute reasonable timeouts |
| 689 | protected long calculateTimeoutLimit() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 690 | return System.currentTimeMillis() + ops.size() * TIMEOUT_PER_OP; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | @Override |
| 694 | public void run() { |
| 695 | try { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 696 | List<IntentUpdate> updates = createIntentUpdates(); |
| 697 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 698 | new IntentBatchApplyFirst(ops, processIntentUpdates(updates), endTime, 0, null).run(); |
| 699 | } catch (Exception e) { |
| 700 | log.error("Error submitting batches:", e); |
| 701 | // FIXME incomplete Intents should be cleaned up |
| 702 | // (transition to FAILED, etc.) |
| 703 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 704 | // the batch has failed |
| 705 | // TODO: maybe we should do more? |
| 706 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 707 | //FIXME |
| 708 | // batchService.removeIntentOperations(ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | private List<IntentUpdate> createIntentUpdates() { |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 713 | return ops.stream() |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 714 | .map(IntentManager.this::createIntentUpdate) |
| 715 | .collect(Collectors.toList()); |
| 716 | } |
| 717 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 718 | private List<CompletedIntentUpdate> processIntentUpdates(List<IntentUpdate> updates) { |
| 719 | // start processing each Intents |
| 720 | List<CompletedIntentUpdate> completed = new ArrayList<>(); |
| 721 | for (IntentUpdate update : updates) { |
| 722 | Optional<IntentUpdate> phase = Optional.of(update); |
| 723 | IntentUpdate previous = update; |
| 724 | while (true) { |
| 725 | if (!phase.isPresent()) { |
| 726 | // FIXME: not type safe cast |
| 727 | completed.add((CompletedIntentUpdate) previous); |
| 728 | break; |
| 729 | } |
| 730 | previous = phase.get(); |
| 731 | phase = previous.execute(); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | return completed; |
| 736 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 737 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 738 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 739 | // TODO: better naming |
| 740 | private class IntentBatchApplyFirst extends IntentBatchPreprocess { |
| 741 | |
| 742 | protected final List<CompletedIntentUpdate> intentUpdates; |
| 743 | protected final int installAttempt; |
| 744 | protected Future<CompletedBatchOperation> future; |
| 745 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 746 | IntentBatchApplyFirst(Collection<IntentData> operations, List<CompletedIntentUpdate> intentUpdates, |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 747 | long endTime, int installAttempt, Future<CompletedBatchOperation> future) { |
| 748 | super(operations, endTime); |
| 749 | this.intentUpdates = ImmutableList.copyOf(intentUpdates); |
| 750 | this.future = future; |
| 751 | this.installAttempt = installAttempt; |
| 752 | } |
| 753 | |
| 754 | @Override |
| 755 | public void run() { |
| 756 | Future<CompletedBatchOperation> future = applyNextBatch(intentUpdates); |
| 757 | new IntentBatchProcessFutures(ops, intentUpdates, endTime, installAttempt, future).run(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 760 | /** |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 761 | * Builds and applies the next batch, and returns the future. |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 762 | * |
| 763 | * @return Future for next batch |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 764 | */ |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 765 | protected Future<CompletedBatchOperation> applyNextBatch(List<CompletedIntentUpdate> updates) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 766 | //TODO test this. (also, maybe save this batch) |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 767 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 768 | FlowRuleBatchOperation batch = createFlowRuleBatchOperation(updates); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 769 | if (batch.size() > 0) { |
| 770 | //FIXME apply batch might throw an exception |
| 771 | return flowRuleService.applyBatch(batch); |
| 772 | } else { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 773 | return null; |
| 774 | } |
| 775 | } |
| 776 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 777 | private FlowRuleBatchOperation createFlowRuleBatchOperation(List<CompletedIntentUpdate> intentUpdates) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 778 | FlowRuleBatchOperation batch = new FlowRuleBatchOperation(Collections.emptyList(), null, 0); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 779 | for (CompletedIntentUpdate update : intentUpdates) { |
| 780 | FlowRuleBatchOperation currentBatch = update.currentBatch(); |
| 781 | if (currentBatch != null) { |
| 782 | batch.addAll(currentBatch); |
| 783 | } |
| 784 | } |
| 785 | return batch; |
| 786 | } |
| 787 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 788 | protected void abandonShip() { |
| 789 | // the batch has failed |
| 790 | // TODO: maybe we should do more? |
| 791 | log.error("Walk the plank, matey..."); |
| 792 | future = null; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 793 | //FIXME |
| 794 | // batchService.removeIntentOperations(ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | // TODO: better naming |
| 799 | private class IntentBatchProcessFutures extends IntentBatchApplyFirst { |
| 800 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 801 | IntentBatchProcessFutures(Collection<IntentData> operations, List<CompletedIntentUpdate> intentUpdates, |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 802 | long endTime, int installAttempt, Future<CompletedBatchOperation> future) { |
| 803 | super(operations, intentUpdates, endTime, installAttempt, future); |
| 804 | } |
| 805 | |
| 806 | @Override |
| 807 | public void run() { |
| 808 | try { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 809 | Future<CompletedBatchOperation> future = processFutures(); |
| 810 | if (future == null) { |
| 811 | // there are no outstanding batches; we are done |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 812 | //FIXME |
| 813 | return; //? |
| 814 | // batchService.removeIntentOperations(ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 815 | } else if (System.currentTimeMillis() > endTime) { |
| 816 | // - cancel current FlowRuleBatch and resubmit again |
| 817 | retry(); |
| 818 | } else { |
| 819 | // we are not done yet, yield the thread by resubmitting ourselves |
| 820 | executor.submit(new IntentBatchProcessFutures(ops, intentUpdates, endTime, installAttempt, future)); |
| 821 | } |
| 822 | } catch (Exception e) { |
| 823 | log.error("Error submitting batches:", e); |
| 824 | // FIXME incomplete Intents should be cleaned up |
| 825 | // (transition to FAILED, etc.) |
| 826 | abandonShip(); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Iterate through the pending futures, and remove them when they have completed. |
| 832 | */ |
| 833 | private Future<CompletedBatchOperation> processFutures() { |
| 834 | try { |
| 835 | CompletedBatchOperation completed = future.get(100, TimeUnit.NANOSECONDS); |
| 836 | updateBatches(completed); |
| 837 | return applyNextBatch(intentUpdates); |
| 838 | } catch (TimeoutException | InterruptedException te) { |
| 839 | log.trace("Installation of intents are still pending: {}", ops); |
| 840 | return future; |
| 841 | } catch (ExecutionException e) { |
| 842 | log.warn("Execution of batch failed: {}", ops, e); |
| 843 | abandonShip(); |
| 844 | return future; |
| 845 | } |
| 846 | } |
| 847 | |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 848 | private void updateBatches(CompletedBatchOperation completed) { |
| 849 | if (completed.isSuccess()) { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 850 | for (CompletedIntentUpdate update : intentUpdates) { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 851 | update.batchSuccess(); |
| 852 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 853 | } else { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 854 | // entire batch has been reverted... |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 855 | log.debug("Failed items: {}", completed.failedItems()); |
| 856 | log.debug("Failed ids: {}", completed.failedIds()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 857 | |
| 858 | for (Long id : completed.failedIds()) { |
| 859 | IntentId targetId = IntentId.valueOf(id); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 860 | for (CompletedIntentUpdate update : intentUpdates) { |
| 861 | for (Intent intent : update.allInstallables()) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 862 | if (intent.id().equals(targetId)) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 863 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | // don't increment the non-failed items, as they have been reverted. |
| 869 | } |
| 870 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 873 | private void retry() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 874 | log.debug("Execution timed out, retrying."); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 875 | if (future.cancel(true)) { // cancel success; batch is reverted |
| 876 | // reset the timer |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 877 | long timeLimit = calculateTimeoutLimit(); |
| 878 | int attempts = installAttempt + 1; |
| 879 | if (attempts == MAX_ATTEMPTS) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 880 | log.warn("Install request timed out: {}", ops); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 881 | for (CompletedIntentUpdate update : intentUpdates) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 882 | update.batchFailed(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 883 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 884 | } else if (attempts > MAX_ATTEMPTS) { |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 885 | abandonShip(); |
| 886 | return; |
Sho SHIMIZU | 55a5518 | 2015-02-03 22:32:56 -0800 | [diff] [blame] | 887 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 888 | Future<CompletedBatchOperation> future = applyNextBatch(intentUpdates); |
| 889 | executor.submit(new IntentBatchProcessFutures(ops, intentUpdates, timeLimit, attempts, future)); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 890 | } else { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 891 | log.error("Cancelling FlowRuleBatch failed."); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 892 | abandonShip(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 893 | } |
| 894 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 895 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 896 | |
| 897 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 898 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 899 | public void execute(Collection<IntentData> operations) { |
| 900 | log.info("Execute {} operation(s).", operations.size()); |
| 901 | log.debug("Execute operations: {}", operations); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 902 | executor.execute(new IntentBatchPreprocess(operations)); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 903 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 904 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 905 | } |