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