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