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 | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Brian O'Connor | 9cfe829 | 2014-10-24 14:14:08 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 20 | import static org.onlab.onos.net.intent.IntentState.COMPILING; |
| 21 | import static org.onlab.onos.net.intent.IntentState.FAILED; |
| 22 | import static org.onlab.onos.net.intent.IntentState.INSTALLED; |
| 23 | import static org.onlab.onos.net.intent.IntentState.INSTALLING; |
| 24 | import static org.onlab.onos.net.intent.IntentState.RECOMPILING; |
| 25 | import static org.onlab.onos.net.intent.IntentState.WITHDRAWING; |
| 26 | import static org.onlab.onos.net.intent.IntentState.WITHDRAWN; |
| 27 | import static org.onlab.util.Tools.namedThreads; |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Objects; |
| 35 | import java.util.concurrent.ConcurrentHashMap; |
| 36 | import java.util.concurrent.ConcurrentMap; |
| 37 | import java.util.concurrent.ExecutionException; |
| 38 | import java.util.concurrent.ExecutorService; |
| 39 | import java.util.concurrent.Future; |
| 40 | import java.util.concurrent.TimeUnit; |
| 41 | import java.util.concurrent.TimeoutException; |
| 42 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 43 | import org.apache.felix.scr.annotations.Activate; |
| 44 | import org.apache.felix.scr.annotations.Component; |
| 45 | import org.apache.felix.scr.annotations.Deactivate; |
| 46 | import org.apache.felix.scr.annotations.Reference; |
| 47 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 48 | import org.apache.felix.scr.annotations.Service; |
| 49 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 50 | import org.onlab.onos.event.EventDeliveryService; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 51 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 53 | import org.onlab.onos.net.flow.FlowRuleService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 54 | import org.onlab.onos.net.intent.Intent; |
| 55 | import org.onlab.onos.net.intent.IntentCompiler; |
| 56 | import org.onlab.onos.net.intent.IntentEvent; |
| 57 | import org.onlab.onos.net.intent.IntentException; |
| 58 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 59 | import org.onlab.onos.net.intent.IntentId; |
| 60 | import org.onlab.onos.net.intent.IntentInstaller; |
| 61 | import org.onlab.onos.net.intent.IntentListener; |
| 62 | import org.onlab.onos.net.intent.IntentOperations; |
| 63 | import org.onlab.onos.net.intent.IntentService; |
| 64 | import org.onlab.onos.net.intent.IntentState; |
| 65 | import org.onlab.onos.net.intent.IntentStore; |
| 66 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 67 | import org.slf4j.Logger; |
| 68 | |
Brian O'Connor | 9cfe829 | 2014-10-24 14:14:08 -0700 | [diff] [blame] | 69 | import com.google.common.collect.ImmutableList; |
| 70 | import com.google.common.collect.ImmutableMap; |
| 71 | import com.google.common.collect.Lists; |
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"; |
| 83 | public static final String INTENT_ID_NULL = "Intent ID cannot be null"; |
| 84 | |
| 85 | // Collections for compiler, installer, and listener are ONOS instance local |
| 86 | private final ConcurrentMap<Class<? extends Intent>, |
| 87 | IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 88 | private final ConcurrentMap<Class<? extends Intent>, |
| 89 | IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 90 | |
| 91 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 92 | listenerRegistry = new AbstractListenerRegistry<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 93 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 94 | private ExecutorService executor; |
| 95 | private ExecutorService monitorExecutor; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 96 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 97 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 98 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected IntentStore store; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 104 | protected ObjectiveTrackerService trackerService; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 107 | protected EventDeliveryService eventDispatcher; |
| 108 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected FlowRuleService flowRuleService; |
| 111 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 112 | @Activate |
| 113 | public void activate() { |
| 114 | store.setDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 115 | trackerService.setDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 116 | eventDispatcher.addSink(IntentEvent.class, listenerRegistry); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 117 | executor = newSingleThreadExecutor(namedThreads("onos-intents")); |
| 118 | monitorExecutor = newSingleThreadExecutor(namedThreads("onos-intent-monitor")); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 119 | log.info("Started"); |
| 120 | } |
| 121 | |
| 122 | @Deactivate |
| 123 | public void deactivate() { |
| 124 | store.unsetDelegate(delegate); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 125 | trackerService.unsetDelegate(topoDelegate); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 126 | eventDispatcher.removeSink(IntentEvent.class); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 127 | executor.shutdown(); |
| 128 | monitorExecutor.shutdown(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 129 | log.info("Stopped"); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void submit(Intent intent) { |
| 134 | checkNotNull(intent, INTENT_NULL); |
| 135 | registerSubclassCompilerIfNeeded(intent); |
| 136 | IntentEvent event = store.createIntent(intent); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 137 | if (event != null) { |
| 138 | eventDispatcher.post(event); |
| 139 | executor.execute(new IntentTask(COMPILING, intent)); |
| 140 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void withdraw(Intent intent) { |
| 145 | checkNotNull(intent, INTENT_NULL); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 146 | executor.execute(new IntentTask(WITHDRAWING, intent)); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // FIXME: implement this method |
| 150 | @Override |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 151 | public void replace(IntentId oldIntentId, Intent newIntent) { |
| 152 | throw new UnsupportedOperationException("execute() is not implemented yet"); |
| 153 | } |
| 154 | |
| 155 | // FIXME: implement this method |
| 156 | @Override |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 157 | public Future<IntentOperations> execute(IntentOperations operations) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 158 | throw new UnsupportedOperationException("execute() is not implemented yet"); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Iterable<Intent> getIntents() { |
| 163 | return store.getIntents(); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public long getIntentCount() { |
| 168 | return store.getIntentCount(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Intent getIntent(IntentId id) { |
| 173 | checkNotNull(id, INTENT_ID_NULL); |
| 174 | return store.getIntent(id); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public IntentState getIntentState(IntentId id) { |
| 179 | checkNotNull(id, INTENT_ID_NULL); |
| 180 | return store.getIntentState(id); |
| 181 | } |
| 182 | |
| 183 | @Override |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 184 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 185 | checkNotNull(intentId, INTENT_ID_NULL); |
| 186 | return store.getInstallableIntents(intentId); |
| 187 | } |
| 188 | |
| 189 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 190 | public void addListener(IntentListener listener) { |
| 191 | listenerRegistry.addListener(listener); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void removeListener(IntentListener listener) { |
| 196 | listenerRegistry.removeListener(listener); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 201 | compilers.put(cls, compiler); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 206 | compilers.remove(cls); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 211 | return ImmutableMap.copyOf(compilers); |
| 212 | } |
| 213 | |
| 214 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 215 | 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] | 216 | installers.put(cls, installer); |
| 217 | } |
| 218 | |
| 219 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 220 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 221 | installers.remove(cls); |
| 222 | } |
| 223 | |
| 224 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 225 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 226 | return ImmutableMap.copyOf(installers); |
| 227 | } |
| 228 | |
| 229 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 230 | * Returns the corresponding intent compiler to the specified intent. |
| 231 | * |
| 232 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 233 | * @param <T> the type of intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 234 | * @return intent compiler corresponding to the specified intent |
| 235 | */ |
| 236 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 237 | @SuppressWarnings("unchecked") |
| 238 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 239 | if (compiler == null) { |
| 240 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 241 | } |
| 242 | return compiler; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns the corresponding intent installer to the specified installable intent. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 247 | * |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 248 | * @param intent intent |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 249 | * @param <T> the type of installable intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 250 | * @return intent installer corresponding to the specified installable intent |
| 251 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 252 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 253 | @SuppressWarnings("unchecked") |
| 254 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); |
| 255 | if (installer == null) { |
| 256 | throw new IntentException("no installer for class " + intent.getClass()); |
| 257 | } |
| 258 | return installer; |
| 259 | } |
| 260 | |
| 261 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 262 | * Compiles the specified intent. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 263 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 264 | * @param intent intent to be compiled |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 265 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 266 | private void executeCompilingPhase(Intent intent) { |
| 267 | // Indicate that the intent is entering the compiling phase. |
| 268 | store.setState(intent, COMPILING); |
| 269 | |
| 270 | try { |
| 271 | // Compile the intent into installable derivatives. |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 272 | List<Intent> installable = compileIntent(intent); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 273 | |
| 274 | // If all went well, associate the resulting list of installable |
| 275 | // intents with the top-level intent and proceed to install. |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 276 | store.setInstallableIntents(intent.id(), installable); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 277 | executeInstallingPhase(intent); |
| 278 | |
| 279 | } catch (Exception e) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 280 | log.warn("Unable to compile intent {} due to:", intent.id(), e); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 281 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 282 | // If compilation failed, mark the intent as failed. |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 283 | IntentEvent event = store.setState(intent, FAILED); |
| 284 | if (event != null) { |
| 285 | eventDispatcher.post(event); |
| 286 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Compiles an intent recursively. |
| 292 | * |
| 293 | * @param intent intent |
| 294 | * @return result of compilation |
| 295 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 296 | private List<Intent> compileIntent(Intent intent) { |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 297 | if (intent.isInstallable()) { |
| 298 | return ImmutableList.of(intent); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 299 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 300 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 301 | List<Intent> installable = new ArrayList<>(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 302 | // TODO do we need to registerSubclassCompiler? |
| 303 | for (Intent compiled : getCompiler(intent).compile(intent)) { |
| 304 | installable.addAll(compileIntent(compiled)); |
| 305 | } |
| 306 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 307 | return installable; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 311 | * Installs all installable intents associated with the specified top-level |
| 312 | * intent. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 313 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 314 | * @param intent intent to be installed |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 315 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 316 | private void executeInstallingPhase(Intent intent) { |
| 317 | // Indicate that the intent is entering the installing phase. |
| 318 | store.setState(intent, INSTALLING); |
| 319 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 320 | List<FlowRuleBatchOperation> installWork = Lists.newArrayList(); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 321 | try { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 322 | List<Intent> installables = store.getInstallableIntents(intent.id()); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 323 | if (installables != null) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 324 | for (Intent installable : installables) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 325 | registerSubclassInstallerIfNeeded(installable); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 326 | trackerService.addTrackedResources(intent.id(), |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 327 | installable.resources()); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 328 | List<FlowRuleBatchOperation> batch = getInstaller(installable).install(installable); |
| 329 | installWork.addAll(batch); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 330 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 331 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 332 | // FIXME we have to wait for the installable intents |
| 333 | //eventDispatcher.post(store.setState(intent, INSTALLED)); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 334 | monitorExecutor.execute(new IntentInstallMonitor(intent, installWork, INSTALLED)); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 335 | } catch (Exception e) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 336 | log.warn("Unable to install intent {} due to:", intent.id(), e); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 337 | uninstallIntent(intent, RECOMPILING); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 338 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 339 | // If compilation failed, kick off the recompiling phase. |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 340 | // FIXME |
| 341 | //executeRecompilingPhase(intent); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 342 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 346 | * Recompiles the specified intent. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 347 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 348 | * @param intent intent to be recompiled |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 349 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 350 | private void executeRecompilingPhase(Intent intent) { |
| 351 | // Indicate that the intent is entering the recompiling phase. |
| 352 | store.setState(intent, RECOMPILING); |
| 353 | |
| 354 | try { |
| 355 | // Compile the intent into installable derivatives. |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 356 | List<Intent> installable = compileIntent(intent); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 357 | |
| 358 | // If all went well, compare the existing list of installable |
| 359 | // intents with the newly compiled list. If they are the same, |
| 360 | // bail, out since the previous approach was determined not to |
| 361 | // be viable. |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 362 | List<Intent> originalInstallable = store.getInstallableIntents(intent.id()); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 363 | |
| 364 | if (Objects.equals(originalInstallable, installable)) { |
| 365 | eventDispatcher.post(store.setState(intent, FAILED)); |
| 366 | } else { |
| 367 | // Otherwise, re-associate the newly compiled installable intents |
| 368 | // with the top-level intent and kick off installing phase. |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 369 | store.setInstallableIntents(intent.id(), installable); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 370 | executeInstallingPhase(intent); |
| 371 | } |
| 372 | } catch (Exception e) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 373 | log.warn("Unable to recompile intent {} due to:", intent.id(), e); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 374 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 375 | // If compilation failed, mark the intent as failed. |
| 376 | eventDispatcher.post(store.setState(intent, FAILED)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Uninstalls the specified intent by uninstalling all of its associated |
| 382 | * installable derivatives. |
| 383 | * |
| 384 | * @param intent intent to be installed |
| 385 | */ |
| 386 | private void executeWithdrawingPhase(Intent intent) { |
| 387 | // Indicate that the intent is being withdrawn. |
| 388 | store.setState(intent, WITHDRAWING); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 389 | uninstallIntent(intent, WITHDRAWN); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 390 | |
| 391 | // If all went well, disassociate the top-level intent with its |
| 392 | // installable derivatives and mark it as withdrawn. |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 393 | // FIXME need to clean up |
| 394 | //store.removeInstalledIntents(intent.id()); |
| 395 | // FIXME |
| 396 | //eventDispatcher.post(store.setState(intent, WITHDRAWN)); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 400 | * Uninstalls all installable intents associated with the given intent. |
| 401 | * |
| 402 | * @param intent intent to be uninstalled |
| 403 | */ |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 404 | private void uninstallIntent(Intent intent, IntentState nextState) { |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 405 | List<FlowRuleBatchOperation> uninstallWork = Lists.newArrayList(); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 406 | try { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 407 | List<Intent> installables = store.getInstallableIntents(intent.id()); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 408 | if (installables != null) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 409 | for (Intent installable : installables) { |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 410 | trackerService.removeTrackedResources(intent.id(), |
| 411 | installable.resources()); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 412 | List<FlowRuleBatchOperation> batches = getInstaller(installable).uninstall(installable); |
| 413 | uninstallWork.addAll(batches); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 416 | monitorExecutor.execute(new IntentInstallMonitor(intent, uninstallWork, nextState)); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 417 | } catch (IntentException e) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 418 | log.warn("Unable to uninstall intent {} due to:", intent.id(), e); |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 423 | * Registers an intent compiler of the specified intent if an intent compiler |
| 424 | * for the intent is not registered. This method traverses the class hierarchy of |
| 425 | * the intent. Once an intent compiler for a parent type is found, this method |
| 426 | * registers the found intent compiler. |
| 427 | * |
| 428 | * @param intent intent |
| 429 | */ |
| 430 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 431 | if (!compilers.containsKey(intent.getClass())) { |
| 432 | Class<?> cls = intent.getClass(); |
| 433 | while (cls != Object.class) { |
| 434 | // As long as we're within the Intent class descendants |
| 435 | if (Intent.class.isAssignableFrom(cls)) { |
| 436 | IntentCompiler<?> compiler = compilers.get(cls); |
| 437 | if (compiler != null) { |
| 438 | compilers.put(intent.getClass(), compiler); |
| 439 | return; |
| 440 | } |
| 441 | } |
| 442 | cls = cls.getSuperclass(); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Registers an intent installer of the specified intent if an intent installer |
| 449 | * for the intent is not registered. This method traverses the class hierarchy of |
| 450 | * the intent. Once an intent installer for a parent type is found, this method |
| 451 | * registers the found intent installer. |
| 452 | * |
| 453 | * @param intent intent |
| 454 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 455 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 456 | if (!installers.containsKey(intent.getClass())) { |
| 457 | Class<?> cls = intent.getClass(); |
| 458 | while (cls != Object.class) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 459 | // As long as we're within the Intent class descendants |
| 460 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 461 | IntentInstaller<?> installer = installers.get(cls); |
| 462 | if (installer != null) { |
| 463 | installers.put(intent.getClass(), installer); |
| 464 | return; |
| 465 | } |
| 466 | } |
| 467 | cls = cls.getSuperclass(); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 472 | // Store delegate to re-post events emitted from the store. |
| 473 | private class InternalStoreDelegate implements IntentStoreDelegate { |
| 474 | @Override |
| 475 | public void notify(IntentEvent event) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 476 | eventDispatcher.post(event); |
| 477 | if (event.type() == IntentEvent.Type.SUBMITTED) { |
| 478 | executor.execute(new IntentTask(COMPILING, event.subject())); |
| 479 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 483 | // Topology change delegate |
| 484 | private class InternalTopoChangeDelegate implements TopologyChangeDelegate { |
| 485 | @Override |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 486 | public void triggerCompile(Iterable<IntentId> intentIds, |
| 487 | boolean compileAllFailed) { |
| 488 | // Attempt recompilation of the specified intents first. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 489 | for (IntentId intentId : intentIds) { |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 490 | Intent intent = getIntent(intentId); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 491 | uninstallIntent(intent, RECOMPILING); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 492 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 493 | //FIXME |
| 494 | //executeRecompilingPhase(intent); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (compileAllFailed) { |
| 498 | // If required, compile all currently failed intents. |
| 499 | for (Intent intent : getIntents()) { |
| 500 | if (getIntentState(intent.id()) == FAILED) { |
| 501 | executeCompilingPhase(intent); |
| 502 | } |
| 503 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 506 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 507 | |
| 508 | // Auxiliary runnable to perform asynchronous tasks. |
| 509 | private class IntentTask implements Runnable { |
| 510 | private final IntentState state; |
| 511 | private final Intent intent; |
| 512 | |
| 513 | public IntentTask(IntentState state, Intent intent) { |
| 514 | this.state = state; |
| 515 | this.intent = intent; |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void run() { |
| 520 | if (state == COMPILING) { |
| 521 | executeCompilingPhase(intent); |
| 522 | } else if (state == RECOMPILING) { |
| 523 | executeRecompilingPhase(intent); |
| 524 | } else if (state == WITHDRAWING) { |
| 525 | executeWithdrawingPhase(intent); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 530 | private class IntentInstallMonitor implements Runnable { |
| 531 | |
| 532 | private final Intent intent; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 533 | private final List<FlowRuleBatchOperation> work; |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 534 | private final List<Future<CompletedBatchOperation>> futures; |
| 535 | private final IntentState nextState; |
| 536 | |
| 537 | public IntentInstallMonitor(Intent intent, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 538 | List<FlowRuleBatchOperation> work, |
| 539 | IntentState nextState) { |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 540 | this.intent = intent; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 541 | this.work = work; |
| 542 | // TODO how many Futures can be outstanding? one? |
| 543 | this.futures = Lists.newLinkedList(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 544 | this.nextState = nextState; |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 545 | |
| 546 | // TODO need to kick off the first batch sometime, why not now? |
| 547 | futures.add(applyNextBatch()); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 550 | /** |
| 551 | * Update the intent store with the next status for this intent. |
| 552 | */ |
| 553 | private void updateIntent() { |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 554 | if (nextState == RECOMPILING) { |
| 555 | executor.execute(new IntentTask(nextState, intent)); |
| 556 | } else if (nextState == INSTALLED || nextState == WITHDRAWN) { |
| 557 | eventDispatcher.post(store.setState(intent, nextState)); |
| 558 | } else { |
| 559 | log.warn("Invalid next intent state {} for intent {}", nextState, intent); |
| 560 | } |
| 561 | } |
| 562 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 563 | /** |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 564 | * Applies the next batch. |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 565 | */ |
| 566 | private Future<CompletedBatchOperation> applyNextBatch() { |
| 567 | if (work.isEmpty()) { |
| 568 | return null; |
| 569 | } |
| 570 | FlowRuleBatchOperation batch = work.remove(0); |
| 571 | return flowRuleService.applyBatch(batch); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Iterate through the pending futures, and remove them when they have completed. |
| 576 | */ |
| 577 | private void processFutures() { |
| 578 | List<Future<CompletedBatchOperation>> newFutures = Lists.newArrayList(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 579 | for (Iterator<Future<CompletedBatchOperation>> i = futures.iterator(); i.hasNext();) { |
| 580 | Future<CompletedBatchOperation> future = i.next(); |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 581 | try { |
| 582 | // TODO: we may want to get the future here and go back to the future. |
alshabib | c7e1cb6 | 2014-10-08 20:20:03 -0700 | [diff] [blame] | 583 | CompletedBatchOperation completed = future.get(100, TimeUnit.NANOSECONDS); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 584 | if (completed.isSuccess()) { |
| 585 | Future<CompletedBatchOperation> newFuture = applyNextBatch(); |
| 586 | if (newFuture != null) { |
| 587 | // we'll add this later so that we don't get a ConcurrentModException |
| 588 | newFutures.add(newFuture); |
| 589 | } |
| 590 | } else { |
| 591 | // TODO check if future succeeded and if not report fail items |
| 592 | log.warn("Failed items: {}", completed.failedItems()); |
| 593 | // TODO revert.... |
| 594 | //uninstallIntent(intent, RECOMPILING); |
| 595 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 596 | i.remove(); |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 597 | } catch (TimeoutException | InterruptedException | ExecutionException te) { |
| 598 | log.debug("Intallations of intent {} is still pending", intent); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 601 | futures.addAll(newFutures); |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void run() { |
| 606 | processFutures(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 607 | if (futures.isEmpty()) { |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 608 | // woohoo! we are done! |
| 609 | updateIntent(); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 610 | } else { |
| 611 | // resubmit ourselves if we are not done yet |
| 612 | monitorExecutor.submit(this); |
| 613 | } |
| 614 | } |
| 615 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 616 | } |