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 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -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.Iterator; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Optional; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.Callable; |
| 28 | import java.util.concurrent.ConcurrentHashMap; |
| 29 | import java.util.concurrent.ConcurrentMap; |
| 30 | import java.util.concurrent.ExecutionException; |
| 31 | import java.util.concurrent.ExecutorService; |
| 32 | import java.util.concurrent.Future; |
| 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; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.FlowRuleOperation; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.FlowRuleOperations; |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowRuleService; |
| 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 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; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 62 | import org.onosproject.net.intent.Key; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 63 | import org.onosproject.net.intent.impl.phase.CompilingFailed; |
| 64 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
| 65 | import org.onosproject.net.intent.impl.phase.InstallRequest; |
| 66 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
| 67 | import org.onosproject.net.intent.impl.phase.WithdrawRequest; |
| 68 | import org.onosproject.net.intent.impl.phase.Withdrawn; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 71 | import com.google.common.collect.ImmutableList; |
| 72 | import com.google.common.collect.ImmutableMap; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 73 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 74 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 75 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 76 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 77 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 78 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 79 | import static org.onlab.util.Tools.isNullOrEmpty; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 80 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 81 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
| 82 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
| 83 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 84 | import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 85 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * An implementation of Intent Manager. |
| 89 | */ |
| 90 | @Component(immediate = true) |
| 91 | @Service |
| 92 | public class IntentManager |
| 93 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 94 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 95 | |
| 96 | public static final String INTENT_NULL = "Intent cannot be null"; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 97 | public static final String INTENT_ID_NULL = "Intent key cannot be null"; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 98 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 99 | private static final int NUM_THREADS = 12; |
| 100 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 101 | private static final EnumSet<IntentState> RECOMPILE |
| 102 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 103 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 104 | // Collections for compiler, installer, and listener are ONOS instance local |
| 105 | private final ConcurrentMap<Class<? extends Intent>, |
| 106 | IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 107 | private final ConcurrentMap<Class<? extends Intent>, |
| 108 | IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 109 | |
| 110 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 111 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 112 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected IntentStore store; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 120 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 123 | protected EventDeliveryService eventDispatcher; |
| 124 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 125 | // TODO: make this protected due to short term hack for ONOS-1051 |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 127 | public FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 128 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 129 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 130 | private ExecutorService batchExecutor; |
| 131 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 132 | |
| 133 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 134 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 135 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 136 | private IdGenerator idGenerator; |
| 137 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 138 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 139 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 140 | @Activate |
| 141 | public void activate() { |
| 142 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 143 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 144 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 145 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch")); |
| 146 | workerExecutor = newFixedThreadPool(NUM_THREADS, groupedThreads("onos/intent", "worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 147 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 148 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 149 | log.info("Started"); |
| 150 | } |
| 151 | |
| 152 | @Deactivate |
| 153 | public void deactivate() { |
| 154 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 155 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 156 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 157 | batchExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 158 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 159 | log.info("Stopped"); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void submit(Intent intent) { |
| 164 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 165 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 166 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void withdraw(Intent intent) { |
| 171 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 172 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 173 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 176 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 177 | public Intent getIntent(Key key) { |
| 178 | return store.getIntent(key); |
| 179 | } |
| 180 | |
| 181 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 182 | public Iterable<Intent> getIntents() { |
| 183 | return store.getIntents(); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public long getIntentCount() { |
| 188 | return store.getIntentCount(); |
| 189 | } |
| 190 | |
| 191 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 192 | public IntentState getIntentState(Key intentKey) { |
| 193 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 194 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 198 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 199 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 200 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame^] | 204 | public boolean isLocal(Key intentKey) { |
| 205 | return store.isMaster(intentKey); |
| 206 | } |
| 207 | |
| 208 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 209 | public void addListener(IntentListener listener) { |
| 210 | listenerRegistry.addListener(listener); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void removeListener(IntentListener listener) { |
| 215 | listenerRegistry.removeListener(listener); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 220 | compilers.put(cls, compiler); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 225 | compilers.remove(cls); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 230 | return ImmutableMap.copyOf(compilers); |
| 231 | } |
| 232 | |
| 233 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 234 | 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] | 235 | installers.put(cls, installer); |
| 236 | } |
| 237 | |
| 238 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 239 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 240 | installers.remove(cls); |
| 241 | } |
| 242 | |
| 243 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 244 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 245 | return ImmutableMap.copyOf(installers); |
| 246 | } |
| 247 | |
| 248 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 249 | * Returns the corresponding intent compiler to the specified intent. |
| 250 | * |
| 251 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 252 | * @param <T> the type of intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 253 | * @return intent compiler corresponding to the specified intent |
| 254 | */ |
| 255 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 256 | @SuppressWarnings("unchecked") |
| 257 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 258 | if (compiler == null) { |
| 259 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 260 | } |
| 261 | return compiler; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Returns the corresponding intent installer to the specified installable intent. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 266 | * |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 267 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 268 | * @param <T> the type of installable intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 269 | * @return intent installer corresponding to the specified installable intent |
| 270 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 271 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 272 | @SuppressWarnings("unchecked") |
| 273 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); |
| 274 | if (installer == null) { |
| 275 | throw new IntentException("no installer for class " + intent.getClass()); |
| 276 | } |
| 277 | return installer; |
| 278 | } |
| 279 | |
| 280 | /** |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 281 | * Compiles an intent recursively. |
| 282 | * |
| 283 | * @param intent intent |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 284 | * @param previousInstallables previous intent installables |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 285 | * @return result of compilation |
| 286 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 287 | // TODO: make this non-public due to short term hack for ONOS-1051 |
| 288 | public List<Intent> compileIntent(Intent intent, List<Intent> previousInstallables) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 289 | if (intent.isInstallable()) { |
| 290 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 291 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 292 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 293 | registerSubclassCompilerIfNeeded(intent); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 294 | // FIXME: get previous resources |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 295 | List<Intent> installable = new ArrayList<>(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 296 | for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { |
| 297 | installable.addAll(compileIntent(compiled, previousInstallables)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 298 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 299 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 302 | //TODO javadoc |
| 303 | //FIXME |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 304 | // TODO: make this non-public due to short term hack for ONOS-1051 |
| 305 | public FlowRuleOperations coordinate(IntentData current, IntentData pending) { |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 306 | List<Intent> oldInstallables = (current != null) ? current.installables() : null; |
| 307 | List<Intent> newInstallables = pending.installables(); |
| 308 | |
| 309 | checkState(isNullOrEmpty(oldInstallables) || |
| 310 | oldInstallables.size() == newInstallables.size(), |
| 311 | "Old and New Intent must have equivalent installable intents."); |
| 312 | |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 313 | List<List<Set<FlowRuleOperation>>> plans = new ArrayList<>(); |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 314 | for (int i = 0; i < newInstallables.size(); i++) { |
| 315 | Intent newInstallable = newInstallables.get(i); |
| 316 | registerSubclassInstallerIfNeeded(newInstallable); |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 317 | //TODO consider migrating installers to FlowRuleOperations |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 318 | /* FIXME |
| 319 | - we need to do another pass on this method about that doesn't |
| 320 | require the length of installables to be equal, and also doesn't |
| 321 | depend on ordering |
| 322 | - we should also reconsider when to start/stop tracking resources |
| 323 | */ |
| 324 | if (isNullOrEmpty(oldInstallables)) { |
| 325 | plans.add(getInstaller(newInstallable).install(newInstallable)); |
| 326 | } else { |
| 327 | Intent oldInstallable = oldInstallables.get(i); |
| 328 | checkState(oldInstallable.getClass().equals(newInstallable.getClass()), |
| 329 | "Installable Intent type mismatch."); |
| 330 | trackerService.removeTrackedResources(pending.key(), oldInstallable.resources()); |
| 331 | plans.add(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); |
| 332 | } |
| 333 | trackerService.addTrackedResources(pending.key(), newInstallable.resources()); |
| 334 | // } catch (IntentException e) { |
| 335 | // log.warn("Unable to update intent {} due to:", oldIntent.id(), e); |
| 336 | // //FIXME... we failed. need to uninstall (if same) or revert (if different) |
| 337 | // trackerService.removeTrackedResources(newIntent.id(), newInstallable.resources()); |
| 338 | // exception = e; |
| 339 | // batches = uninstallIntent(oldIntent, oldInstallables); |
| 340 | // } |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 341 | } |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 342 | |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 343 | return merge(plans).build(new FlowRuleOperationsContext() { // TODO move this out |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 344 | @Override |
| 345 | public void onSuccess(FlowRuleOperations ops) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 346 | log.debug("Completed installing: {}", pending.key()); |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 347 | pending.setState(INSTALLED); |
| 348 | store.write(pending); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void onError(FlowRuleOperations ops) { |
Brian O'Connor | ba1abbe | 2015-02-06 15:21:48 -0800 | [diff] [blame] | 353 | log.warn("Failed installation: {} {} on {}", pending.key(), |
| 354 | pending.intent(), ops); |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 355 | //TODO store.write(pending.setState(BROKEN)); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 356 | pending.setState(FAILED); |
| 357 | store.write(pending); |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 358 | } |
| 359 | }); |
| 360 | } |
| 361 | |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 362 | /** |
| 363 | * Generate a {@link FlowRuleOperations} instance from the specified intent data. |
| 364 | * |
| 365 | * @param current intent data stored in the store |
| 366 | * @return flow rule operations |
| 367 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 368 | // TODO: make this non-public due to short term hack for ONOS-1051 |
| 369 | public FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending) { |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 370 | List<Intent> installables = current.installables(); |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 371 | List<List<Set<FlowRuleOperation>>> plans = new ArrayList<>(); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 372 | for (Intent installable : installables) { |
Sho SHIMIZU | b5cd581 | 2015-02-09 14:25:13 -0800 | [diff] [blame] | 373 | plans.add(getInstaller(installable).uninstall(installable)); |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 374 | trackerService.removeTrackedResources(pending.key(), installable.resources()); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return merge(plans).build(new FlowRuleOperationsContext() { |
| 378 | @Override |
| 379 | public void onSuccess(FlowRuleOperations ops) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 380 | log.debug("Completed withdrawing: {}", pending.key()); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 381 | pending.setState(WITHDRAWN); |
| 382 | pending.setInstallables(Collections.emptyList()); |
| 383 | store.write(pending); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public void onError(FlowRuleOperations ops) { |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 388 | log.warn("Failed withdraw: {}", pending.key()); |
| 389 | pending.setState(FAILED); |
| 390 | store.write(pending); |
Sho SHIMIZU | 37a24a8 | 2015-02-09 09:12:21 -0800 | [diff] [blame] | 391 | } |
| 392 | }); |
| 393 | } |
| 394 | |
| 395 | |
Brian O'Connor | ea2ba3e | 2015-02-12 11:27:55 -0800 | [diff] [blame] | 396 | // TODO needs tests... or maybe it's just perfect |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 397 | private FlowRuleOperations.Builder merge(List<List<Set<FlowRuleOperation>>> plans) { |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 398 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 399 | // Build a batch one stage at a time |
| 400 | for (int stageNumber = 0;; stageNumber++) { |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 401 | // Get the sub-stage from each plan (List<Set<FlowRuleOperation>) |
| 402 | for (Iterator<List<Set<FlowRuleOperation>>> itr = plans.iterator(); itr.hasNext();) { |
| 403 | List<Set<FlowRuleOperation>> plan = itr.next(); |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 404 | if (plan.size() <= stageNumber) { |
| 405 | // we have consumed all stages from this plan, so remove it |
| 406 | itr.remove(); |
| 407 | continue; |
| 408 | } |
| 409 | // write operations from this sub-stage into the builder |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 410 | Set<FlowRuleOperation> stage = plan.get(stageNumber); |
| 411 | for (FlowRuleOperation entry : stage) { |
| 412 | builder.operation(entry); |
Brian O'Connor | 7775bda | 2015-02-06 15:01:18 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | // we are done with the stage, start the next one... |
| 416 | if (plans.isEmpty()) { |
| 417 | break; // we don't need to start a new stage, we are done. |
| 418 | } |
| 419 | builder.newStage(); |
| 420 | } |
| 421 | return builder; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 424 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 425 | * Registers an intent compiler of the specified intent if an intent compiler |
| 426 | * for the intent is not registered. This method traverses the class hierarchy of |
| 427 | * the intent. Once an intent compiler for a parent type is found, this method |
| 428 | * registers the found intent compiler. |
| 429 | * |
| 430 | * @param intent intent |
| 431 | */ |
| 432 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 433 | if (!compilers.containsKey(intent.getClass())) { |
| 434 | Class<?> cls = intent.getClass(); |
| 435 | while (cls != Object.class) { |
| 436 | // As long as we're within the Intent class descendants |
| 437 | if (Intent.class.isAssignableFrom(cls)) { |
| 438 | IntentCompiler<?> compiler = compilers.get(cls); |
| 439 | if (compiler != null) { |
| 440 | compilers.put(intent.getClass(), compiler); |
| 441 | return; |
| 442 | } |
| 443 | } |
| 444 | cls = cls.getSuperclass(); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Registers an intent installer of the specified intent if an intent installer |
| 451 | * for the intent is not registered. This method traverses the class hierarchy of |
| 452 | * the intent. Once an intent installer for a parent type is found, this method |
| 453 | * registers the found intent installer. |
| 454 | * |
| 455 | * @param intent intent |
| 456 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 457 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 458 | if (!installers.containsKey(intent.getClass())) { |
| 459 | Class<?> cls = intent.getClass(); |
| 460 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 461 | // As long as we're within the Intent class descendants |
| 462 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 463 | IntentInstaller<?> installer = installers.get(cls); |
| 464 | if (installer != null) { |
| 465 | installers.put(intent.getClass(), installer); |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | cls = cls.getSuperclass(); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 474 | // Store delegate to re-post events emitted from the store. |
| 475 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 476 | @Override |
| 477 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 478 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 479 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 480 | |
| 481 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 482 | public void process(IntentData data) { |
| 483 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 484 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 487 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 488 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 489 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 490 | for (Key key : intentKeys) { |
| 491 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 492 | if (intent == null) { |
| 493 | continue; |
| 494 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 495 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if (compileAllFailed) { |
| 499 | // If required, compile all currently failed intents. |
| 500 | for (Intent intent : getIntents()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 501 | IntentState state = getIntentState(intent.key()); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 502 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 503 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 504 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 505 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 506 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 507 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 512 | //FIXME |
| 513 | // for (ApplicationId appId : batches.keySet()) { |
| 514 | // if (batchService.isLocalLeader(appId)) { |
| 515 | // execute(batches.get(appId).build()); |
| 516 | // } |
| 517 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 518 | } |
| 519 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 520 | // Topology change delegate |
| 521 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 522 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 523 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 524 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 525 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 526 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 527 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 528 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 529 | private IntentProcessPhase createIntentUpdate(IntentData intentData) { |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 530 | IntentData current = store.getIntentData(intentData.key()); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 531 | switch (intentData.state()) { |
| 532 | case INSTALL_REQ: |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 533 | return new InstallRequest(this, intentData, Optional.ofNullable(current)); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 534 | case WITHDRAW_REQ: |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 535 | if (current == null || isNullOrEmpty(current.installables())) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 536 | return new Withdrawn(intentData, WITHDRAWN); |
Sho SHIMIZU | 95a7baf | 2015-02-12 09:15:57 -0800 | [diff] [blame] | 537 | } else { |
| 538 | return new WithdrawRequest(this, intentData, current); |
| 539 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 540 | default: |
| 541 | // illegal state |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 542 | return new CompilingFailed(intentData); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 543 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 546 | private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 547 | return workerExecutor.submit(new IntentWorker(data)); |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 550 | private class IntentBatchPreprocess implements Runnable { |
| 551 | |
| 552 | // TODO make this configurable |
| 553 | private static final int TIMEOUT_PER_OP = 500; // ms |
| 554 | protected static final int MAX_ATTEMPTS = 3; |
| 555 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 556 | protected final Collection<IntentData> data; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 557 | |
| 558 | // future holding current FlowRuleBatch installation result |
| 559 | protected final long startTime = System.currentTimeMillis(); |
| 560 | protected final long endTime; |
| 561 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 562 | private IntentBatchPreprocess(Collection<IntentData> data, long endTime) { |
| 563 | this.data = checkNotNull(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 564 | this.endTime = endTime; |
| 565 | } |
| 566 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 567 | public IntentBatchPreprocess(Collection<IntentData> data) { |
| 568 | this(data, System.currentTimeMillis() + data.size() * TIMEOUT_PER_OP); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // FIXME compute reasonable timeouts |
| 572 | protected long calculateTimeoutLimit() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 573 | return System.currentTimeMillis() + data.size() * TIMEOUT_PER_OP; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void run() { |
| 578 | try { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 579 | /* |
| 580 | 1. wrap each intentdata in a runnable and submit |
| 581 | 2. wait for completion of all the work |
| 582 | 3. accumulate results and submit batch write of IntentData to store |
| 583 | (we can also try to update these individually) |
| 584 | */ |
| 585 | submitUpdates(waitForFutures(createIntentUpdates())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 586 | } catch (Exception e) { |
| 587 | log.error("Error submitting batches:", e); |
| 588 | // FIXME incomplete Intents should be cleaned up |
| 589 | // (transition to FAILED, etc.) |
| 590 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 591 | // the batch has failed |
| 592 | // TODO: maybe we should do more? |
| 593 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 594 | //FIXME |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 595 | // batchService.removeIntentOperations(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 599 | private List<Future<FinalIntentProcessPhase>> createIntentUpdates() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 600 | return data.stream() |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 601 | .map(IntentManager.this::submitIntentData) |
| 602 | .collect(Collectors.toList()); |
| 603 | } |
| 604 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 605 | private List<FinalIntentProcessPhase> waitForFutures(List<Future<FinalIntentProcessPhase>> futures) { |
| 606 | ImmutableList.Builder<FinalIntentProcessPhase> updateBuilder = ImmutableList.builder(); |
| 607 | for (Future<FinalIntentProcessPhase> future : futures) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 608 | try { |
| 609 | updateBuilder.add(future.get()); |
| 610 | } catch (InterruptedException | ExecutionException e) { |
| 611 | //FIXME |
| 612 | log.warn("Future failed: {}", e); |
| 613 | } |
| 614 | } |
| 615 | return updateBuilder.build(); |
| 616 | } |
| 617 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 618 | private void submitUpdates(List<FinalIntentProcessPhase> updates) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 619 | store.batchWrite(updates.stream() |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 620 | .map(FinalIntentProcessPhase::data) |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 621 | .collect(Collectors.toList())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 622 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 623 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 624 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 625 | private final class IntentWorker implements Callable<FinalIntentProcessPhase> { |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 626 | |
| 627 | private final IntentData data; |
| 628 | |
| 629 | private IntentWorker(IntentData data) { |
| 630 | this.data = data; |
| 631 | } |
| 632 | |
| 633 | @Override |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 634 | public FinalIntentProcessPhase call() throws Exception { |
| 635 | IntentProcessPhase update = createIntentUpdate(data); |
| 636 | Optional<IntentProcessPhase> currentPhase = Optional.of(update); |
| 637 | IntentProcessPhase previousPhase = update; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 638 | |
| 639 | while (currentPhase.isPresent()) { |
| 640 | previousPhase = currentPhase.get(); |
| 641 | currentPhase = previousPhase.execute(); |
| 642 | } |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 643 | return (FinalIntentProcessPhase) previousPhase; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 644 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 645 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 646 | |
| 647 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 648 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 649 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 650 | log.debug("Execute {} operation(s).", operations.size()); |
| 651 | log.trace("Execute operations: {}", operations); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 652 | batchExecutor.execute(new IntentBatchPreprocess(operations)); |
| 653 | // TODO ensure that only one batch is in flight at a time |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 654 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 655 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 656 | } |