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