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 | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.EnumSet; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.ExecutionException; |
| 23 | import java.util.concurrent.ExecutorService; |
| 24 | import java.util.concurrent.Future; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.core.IdGenerator; |
| 35 | import org.onosproject.event.AbstractListenerRegistry; |
| 36 | import org.onosproject.event.EventDeliveryService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowRuleOperations; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.FlowRuleIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.Intent; |
| 43 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.IntentCompiler; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentData; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.IntentEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.IntentExtensionService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.IntentService; |
| 50 | import org.onosproject.net.intent.IntentState; |
| 51 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.intent.IntentStoreDelegate; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 53 | import org.onosproject.net.intent.Key; |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 54 | import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 55 | import org.onosproject.net.intent.impl.phase.IntentProcessPhase; |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 56 | import org.onosproject.net.intent.impl.phase.IntentWorker; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 59 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 60 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 61 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 62 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 63 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 64 | import static org.onlab.util.Tools.groupedThreads; |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 65 | import static org.onosproject.net.intent.IntentState.FAILED; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 66 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 67 | import static org.onosproject.net.intent.IntentState.INSTALL_REQ; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 69 | import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 70 | import static org.onosproject.net.intent.impl.phase.IntentProcessPhase.newInitialPhase; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 71 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * An implementation of Intent Manager. |
| 75 | */ |
| 76 | @Component(immediate = true) |
| 77 | @Service |
| 78 | public class IntentManager |
| 79 | implements IntentService, IntentExtensionService { |
Sho SHIMIZU | 8b5051d | 2014-11-05 11:24:13 -0800 | [diff] [blame] | 80 | private static final Logger log = getLogger(IntentManager.class); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 81 | |
| 82 | public static final String INTENT_NULL = "Intent cannot be null"; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 83 | 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] | 84 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 85 | private static final int NUM_THREADS = 12; |
| 86 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 87 | private static final EnumSet<IntentState> RECOMPILE |
| 88 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 89 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 90 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 91 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 92 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected CoreService coreService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected IntentStore store; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 100 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 103 | protected EventDeliveryService eventDispatcher; |
| 104 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 214ac32 | 2015-02-23 19:30:15 -0800 | [diff] [blame] | 106 | protected FlowRuleService flowRuleService; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 107 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 108 | |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 109 | private ExecutorService batchExecutor; |
| 110 | private ExecutorService workerExecutor; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 111 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 112 | private final CompilerRegistry compilerRegistry = new CompilerRegistry(); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 113 | private final InternalIntentProcessor processor = new InternalIntentProcessor(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 114 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 115 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 116 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| 117 | private IdGenerator idGenerator; |
| 118 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 119 | private final IntentAccumulator accumulator = new IntentAccumulator(batchDelegate); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 120 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 121 | @Activate |
| 122 | public void activate() { |
| 123 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 124 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 125 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | bdc7f00 | 2015-02-18 20:49:41 -0800 | [diff] [blame] | 126 | batchExecutor = newSingleThreadExecutor(groupedThreads("onos/intent", "batch")); |
| 127 | workerExecutor = newFixedThreadPool(NUM_THREADS, groupedThreads("onos/intent", "worker-%d")); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 128 | idGenerator = coreService.getIdGenerator("intent-ids"); |
| 129 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 130 | log.info("Started"); |
| 131 | } |
| 132 | |
| 133 | @Deactivate |
| 134 | public void deactivate() { |
| 135 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 136 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 137 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 138 | batchExecutor.shutdown(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 139 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 140 | log.info("Stopped"); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void submit(Intent intent) { |
| 145 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 146 | IntentData data = new IntentData(intent, IntentState.INSTALL_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 147 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void withdraw(Intent intent) { |
| 152 | checkNotNull(intent, INTENT_NULL); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 153 | IntentData data = new IntentData(intent, IntentState.WITHDRAW_REQ, null); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 154 | store.addPending(data); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 157 | @Override |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 158 | public void purge(Intent intent) { |
| 159 | checkNotNull(intent, INTENT_NULL); |
| 160 | IntentData data = new IntentData(intent, IntentState.PURGE_REQ, null); |
| 161 | store.addPending(data); |
| 162 | } |
| 163 | |
| 164 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 165 | public Intent getIntent(Key key) { |
| 166 | return store.getIntent(key); |
| 167 | } |
| 168 | |
| 169 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 170 | public Iterable<Intent> getIntents() { |
| 171 | return store.getIntents(); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public long getIntentCount() { |
| 176 | return store.getIntentCount(); |
| 177 | } |
| 178 | |
| 179 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 180 | public IntentState getIntentState(Key intentKey) { |
| 181 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 182 | return store.getIntentState(intentKey); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 186 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 187 | checkNotNull(intentKey, INTENT_ID_NULL); |
| 188 | return store.getInstallableIntents(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 192 | public boolean isLocal(Key intentKey) { |
| 193 | return store.isMaster(intentKey); |
| 194 | } |
| 195 | |
| 196 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 197 | public void addListener(IntentListener listener) { |
| 198 | listenerRegistry.addListener(listener); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void removeListener(IntentListener listener) { |
| 203 | listenerRegistry.removeListener(listener); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 208 | compilerRegistry.registerCompiler(cls, compiler); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 213 | compilerRegistry.unregisterCompiler(cls); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 218 | return compilerRegistry.getCompilers(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 222 | public Iterable<Intent> getPending() { |
| 223 | return store.getPending(); |
| 224 | } |
| 225 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 226 | // Store delegate to re-post events emitted from the store. |
| 227 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 228 | @Override |
| 229 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 230 | eventDispatcher.post(event); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 231 | } |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 232 | |
| 233 | @Override |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 234 | public void process(IntentData data) { |
| 235 | accumulator.add(data); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 236 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 239 | private void buildAndSubmitBatches(Iterable<Key> intentKeys, |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 240 | boolean compileAllFailed) { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 241 | // Attempt recompilation of the specified intents first. |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 242 | for (Key key : intentKeys) { |
| 243 | Intent intent = store.getIntent(key); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 244 | if (intent == null) { |
| 245 | continue; |
| 246 | } |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 247 | submit(intent); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | if (compileAllFailed) { |
| 251 | // If required, compile all currently failed intents. |
| 252 | for (Intent intent : getIntents()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 253 | IntentState state = getIntentState(intent.key()); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 254 | if (RECOMPILE.contains(state)) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 255 | if (state == WITHDRAW_REQ) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 256 | withdraw(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 257 | } else { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 258 | submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 259 | } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 264 | //FIXME |
| 265 | // for (ApplicationId appId : batches.keySet()) { |
| 266 | // if (batchService.isLocalLeader(appId)) { |
| 267 | // execute(batches.get(appId).build()); |
| 268 | // } |
| 269 | // } |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 270 | } |
| 271 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 272 | // Topology change delegate |
| 273 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 274 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 275 | public void triggerCompile(Iterable<Key> intentKeys, |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 276 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 277 | buildAndSubmitBatches(intentKeys, compileAllFailed); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 278 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 279 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 280 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 281 | private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) { |
Sho SHIMIZU | 0cb6fe6 | 2015-02-23 16:39:57 -0800 | [diff] [blame] | 282 | IntentData current = store.getIntentData(data.key()); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 283 | IntentProcessPhase initial = newInitialPhase(processor, data, current); |
| 284 | return workerExecutor.submit(new IntentWorker(initial)); |
Sho SHIMIZU | 8d9d136 | 2015-02-04 12:28:15 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Sho SHIMIZU | d42058e | 2015-02-23 16:16:20 -0800 | [diff] [blame] | 287 | private class IntentBatchProcess implements Runnable { |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 288 | |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 289 | protected final Collection<IntentData> data; |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 290 | |
Sho SHIMIZU | d42058e | 2015-02-23 16:16:20 -0800 | [diff] [blame] | 291 | IntentBatchProcess(Collection<IntentData> data) { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 292 | this.data = checkNotNull(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void run() { |
| 297 | try { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 298 | /* |
| 299 | 1. wrap each intentdata in a runnable and submit |
| 300 | 2. wait for completion of all the work |
| 301 | 3. accumulate results and submit batch write of IntentData to store |
| 302 | (we can also try to update these individually) |
| 303 | */ |
| 304 | submitUpdates(waitForFutures(createIntentUpdates())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 305 | } catch (Exception e) { |
| 306 | log.error("Error submitting batches:", e); |
| 307 | // FIXME incomplete Intents should be cleaned up |
| 308 | // (transition to FAILED, etc.) |
| 309 | |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 310 | // the batch has failed |
| 311 | // TODO: maybe we should do more? |
| 312 | log.error("Walk the plank, matey..."); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 313 | //FIXME |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 314 | // batchService.removeIntentOperations(data); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 315 | } |
Brian O'Connor | 3c4b00d | 2015-02-22 20:42:26 -0800 | [diff] [blame] | 316 | accumulator.ready(); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 319 | private List<Future<FinalIntentProcessPhase>> createIntentUpdates() { |
Sho SHIMIZU | 5f281a4 | 2015-02-04 15:29:11 -0800 | [diff] [blame] | 320 | return data.stream() |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 321 | .map(IntentManager.this::submitIntentData) |
| 322 | .collect(Collectors.toList()); |
| 323 | } |
| 324 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 325 | private List<FinalIntentProcessPhase> waitForFutures(List<Future<FinalIntentProcessPhase>> futures) { |
| 326 | ImmutableList.Builder<FinalIntentProcessPhase> updateBuilder = ImmutableList.builder(); |
| 327 | for (Future<FinalIntentProcessPhase> future : futures) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 328 | try { |
| 329 | updateBuilder.add(future.get()); |
| 330 | } catch (InterruptedException | ExecutionException e) { |
| 331 | //FIXME |
| 332 | log.warn("Future failed: {}", e); |
| 333 | } |
| 334 | } |
| 335 | return updateBuilder.build(); |
| 336 | } |
| 337 | |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 338 | private void submitUpdates(List<FinalIntentProcessPhase> updates) { |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 339 | store.batchWrite(updates.stream() |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 340 | .map(FinalIntentProcessPhase::data) |
| 341 | .collect(Collectors.toList())); |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 342 | } |
Sho SHIMIZU | adf8c48 | 2014-12-12 18:23:29 -0800 | [diff] [blame] | 343 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 344 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 345 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| 346 | @Override |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 347 | public void execute(Collection<IntentData> operations) { |
Brian O'Connor | ab8ef82 | 2015-02-17 18:08:54 -0800 | [diff] [blame] | 348 | log.debug("Execute {} operation(s).", operations.size()); |
| 349 | log.trace("Execute operations: {}", operations); |
Sho SHIMIZU | d42058e | 2015-02-23 16:16:20 -0800 | [diff] [blame] | 350 | batchExecutor.execute(new IntentBatchProcess(operations)); |
Brian O'Connor | db15b04 | 2015-02-04 14:59:28 -0800 | [diff] [blame] | 351 | // 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] | 352 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 353 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 354 | |
| 355 | private class InternalIntentProcessor implements IntentProcessor { |
| 356 | @Override |
| 357 | public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { |
| 358 | return compilerRegistry.compile(intent, previousInstallables); |
| 359 | } |
| 360 | |
| 361 | @Override |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 362 | public void apply(IntentData toUninstall, IntentData toInstall) { |
| 363 | IntentManager.this.apply(toUninstall, toInstall); |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 364 | } |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 365 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 366 | |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 367 | private enum Direction { |
| 368 | ADD, |
| 369 | REMOVE |
| 370 | } |
| 371 | |
| 372 | private void applyIntentData(IntentData data, |
| 373 | FlowRuleOperations.Builder builder, |
| 374 | Direction direction) { |
| 375 | if (data != null) { |
| 376 | List<Intent> intentsToApply = data.installables(); |
| 377 | if (!intentsToApply.stream().allMatch(x -> x instanceof FlowRuleIntent)) { |
| 378 | throw new IllegalStateException("installable intents must be FlowRuleIntent"); |
| 379 | } |
| 380 | |
| 381 | if (direction == Direction.ADD) { |
| 382 | trackerService.addTrackedResources(data.key(), data.intent().resources()); |
| 383 | intentsToApply.forEach(installable -> |
| 384 | trackerService.addTrackedResources(data.key(), installable.resources())); |
| 385 | } else { |
| 386 | trackerService.removeTrackedResources(data.key(), data.intent().resources()); |
| 387 | intentsToApply.forEach(installable -> |
| 388 | trackerService.removeTrackedResources(data.intent().key(), |
| 389 | installable.resources())); |
| 390 | } |
| 391 | |
| 392 | // FIXME do FlowRuleIntents have stages??? Can we do uninstall work in parallel? I think so. |
| 393 | builder.newStage(); |
| 394 | |
| 395 | List<Collection<FlowRule>> stages = intentsToApply.stream() |
| 396 | .map(x -> (FlowRuleIntent) x) |
| 397 | .map(FlowRuleIntent::flowRules) |
| 398 | .collect(Collectors.toList()); |
| 399 | |
| 400 | for (Collection<FlowRule> rules : stages) { |
| 401 | if (direction == Direction.ADD) { |
| 402 | rules.forEach(builder::add); |
| 403 | } else { |
| 404 | rules.forEach(builder::remove); |
| 405 | } |
| 406 | } |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 411 | private void apply(IntentData toUninstall, IntentData toInstall) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 412 | // need to consider if FlowRuleIntent is only one as installable intent or not |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 413 | |
| 414 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 415 | applyIntentData(toUninstall, builder, Direction.REMOVE); |
| 416 | applyIntentData(toInstall, builder, Direction.ADD); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 417 | |
| 418 | FlowRuleOperations operations = builder.build(new FlowRuleOperationsContext() { |
| 419 | @Override |
| 420 | public void onSuccess(FlowRuleOperations ops) { |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 421 | if (toInstall != null) { |
| 422 | log.debug("Completed installing: {}", toInstall.key()); |
| 423 | //FIXME state depends on install.... we might want to pass this in. |
| 424 | toInstall.setState(INSTALLED); |
| 425 | store.write(toInstall); |
| 426 | } else if (toUninstall != null) { |
| 427 | log.debug("Completed withdrawing: {}", toUninstall.key()); |
| 428 | //FIXME state depends on install.... we might want to pass this in. |
| 429 | toUninstall.setState(WITHDRAWN); |
| 430 | store.write(toUninstall); |
| 431 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void onError(FlowRuleOperations ops) { |
Ray Milkey | fd7931d | 2015-03-30 13:58:38 -0700 | [diff] [blame] | 436 | if (toInstall != null) { |
| 437 | log.warn("Failed installation: {} {} on {}", toInstall.key(), toInstall.intent(), ops); |
| 438 | //FIXME |
| 439 | toInstall.setState(FAILED); |
| 440 | store.write(toInstall); |
| 441 | } |
| 442 | // if toInstall was cause of error, then recompile (manage/increment counter, when exceeded -> CORRUPT) |
Sho SHIMIZU | d493625 | 2015-04-10 15:03:57 -0700 | [diff] [blame^] | 443 | // if toUninstall was cause of error, then CORRUPT (another job will clean this up) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 444 | } |
| 445 | }); |
| 446 | |
| 447 | flowRuleService.apply(operations); |
| 448 | } |
| 449 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 450 | } |