ONOS-1048 - Define interfaces instead of using IntentManager
- Define IntentProcessor interface containing methods to process an intent - Pull out IntentCompiler related tasks to CompilerRegistry - Pull out IntentInstaller related tasks to InstallerRegistry - Create an IntentProcessor subclass as inner class in IntentManager Change-Id: Ia3e8d574a1053e7ddc9b961873ef758c9e0b1b26
Showing
11 changed files
with
524 additions
and
295 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2015 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 | + */ | ||
| 16 | +package org.onosproject.net.intent.impl; | ||
| 17 | + | ||
| 18 | +import com.google.common.collect.ImmutableList; | ||
| 19 | +import com.google.common.collect.ImmutableMap; | ||
| 20 | +import org.onosproject.net.intent.Intent; | ||
| 21 | +import org.onosproject.net.intent.IntentCompiler; | ||
| 22 | +import org.onosproject.net.intent.IntentException; | ||
| 23 | + | ||
| 24 | +import java.util.ArrayList; | ||
| 25 | +import java.util.List; | ||
| 26 | +import java.util.Map; | ||
| 27 | +import java.util.concurrent.ConcurrentHashMap; | ||
| 28 | +import java.util.concurrent.ConcurrentMap; | ||
| 29 | + | ||
| 30 | +// TODO: consider a better name | ||
| 31 | +class CompilerRegistry { | ||
| 32 | + | ||
| 33 | + private final ConcurrentMap<Class<? extends Intent>, | ||
| 34 | + IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * Registers the specified compiler for the given intent class. | ||
| 38 | + * | ||
| 39 | + * @param cls intent class | ||
| 40 | + * @param compiler intent compiler | ||
| 41 | + * @param <T> the type of intent | ||
| 42 | + */ | ||
| 43 | + public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { | ||
| 44 | + compilers.put(cls, compiler); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * Unregisters the compiler for the specified intent class. | ||
| 49 | + * | ||
| 50 | + * @param cls intent class | ||
| 51 | + * @param <T> the type of intent | ||
| 52 | + */ | ||
| 53 | + public <T extends Intent> void unregisterCompiler(Class<T> cls) { | ||
| 54 | + compilers.remove(cls); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * Returns immutable set of bindings of currently registered intent compilers. | ||
| 59 | + * | ||
| 60 | + * @return the set of compiler bindings | ||
| 61 | + */ | ||
| 62 | + public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { | ||
| 63 | + return ImmutableMap.copyOf(compilers); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * Compiles an intent recursively. | ||
| 68 | + * | ||
| 69 | + * @param intent intent | ||
| 70 | + * @param previousInstallables previous intent installables | ||
| 71 | + * @return result of compilation | ||
| 72 | + */ | ||
| 73 | + List<Intent> compile(Intent intent, List<Intent> previousInstallables) { | ||
| 74 | + if (intent.isInstallable()) { | ||
| 75 | + return ImmutableList.of(intent); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + registerSubclassCompilerIfNeeded(intent); | ||
| 79 | + // FIXME: get previous resources | ||
| 80 | + List<Intent> installable = new ArrayList<>(); | ||
| 81 | + for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { | ||
| 82 | + installable.addAll(compile(compiled, previousInstallables)); | ||
| 83 | + } | ||
| 84 | + return installable; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Returns the corresponding intent compiler to the specified intent. | ||
| 89 | + * | ||
| 90 | + * @param intent intent | ||
| 91 | + * @param <T> the type of intent | ||
| 92 | + * @return intent compiler corresponding to the specified intent | ||
| 93 | + */ | ||
| 94 | + private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { | ||
| 95 | + @SuppressWarnings("unchecked") | ||
| 96 | + IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); | ||
| 97 | + if (compiler == null) { | ||
| 98 | + throw new IntentException("no compiler for class " + intent.getClass()); | ||
| 99 | + } | ||
| 100 | + return compiler; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * Registers an intent compiler of the specified intent if an intent compiler | ||
| 105 | + * for the intent is not registered. This method traverses the class hierarchy of | ||
| 106 | + * the intent. Once an intent compiler for a parent type is found, this method | ||
| 107 | + * registers the found intent compiler. | ||
| 108 | + * | ||
| 109 | + * @param intent intent | ||
| 110 | + */ | ||
| 111 | + private void registerSubclassCompilerIfNeeded(Intent intent) { | ||
| 112 | + if (!compilers.containsKey(intent.getClass())) { | ||
| 113 | + Class<?> cls = intent.getClass(); | ||
| 114 | + while (cls != Object.class) { | ||
| 115 | + // As long as we're within the Intent class descendants | ||
| 116 | + if (Intent.class.isAssignableFrom(cls)) { | ||
| 117 | + IntentCompiler<?> compiler = compilers.get(cls); | ||
| 118 | + if (compiler != null) { | ||
| 119 | + compilers.put(intent.getClass(), compiler); | ||
| 120 | + return; | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + cls = cls.getSuperclass(); | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 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 | + */ | ||
| 16 | +package org.onosproject.net.intent.impl; | ||
| 17 | + | ||
| 18 | +import com.google.common.collect.ImmutableMap; | ||
| 19 | +import org.onosproject.net.flow.FlowRuleOperation; | ||
| 20 | +import org.onosproject.net.flow.FlowRuleOperations; | ||
| 21 | +import org.onosproject.net.flow.FlowRuleOperationsContext; | ||
| 22 | +import org.onosproject.net.intent.Intent; | ||
| 23 | +import org.onosproject.net.intent.IntentData; | ||
| 24 | +import org.onosproject.net.intent.IntentException; | ||
| 25 | +import org.onosproject.net.intent.IntentInstaller; | ||
| 26 | +import org.onosproject.net.intent.IntentStore; | ||
| 27 | +import org.slf4j.Logger; | ||
| 28 | +import org.slf4j.LoggerFactory; | ||
| 29 | + | ||
| 30 | +import java.util.ArrayList; | ||
| 31 | +import java.util.Collection; | ||
| 32 | +import java.util.Collections; | ||
| 33 | +import java.util.Iterator; | ||
| 34 | +import java.util.List; | ||
| 35 | +import java.util.Map; | ||
| 36 | +import java.util.concurrent.ConcurrentHashMap; | ||
| 37 | +import java.util.concurrent.ConcurrentMap; | ||
| 38 | + | ||
| 39 | +import static com.google.common.base.Preconditions.checkState; | ||
| 40 | +import static org.onlab.util.Tools.isNullOrEmpty; | ||
| 41 | +import static org.onosproject.net.intent.IntentState.FAILED; | ||
| 42 | +import static org.onosproject.net.intent.IntentState.INSTALLED; | ||
| 43 | +import static org.onosproject.net.intent.IntentState.WITHDRAWN; | ||
| 44 | + | ||
| 45 | +// TODO: consider a better name | ||
| 46 | +class InstallerRegistry { | ||
| 47 | + | ||
| 48 | + private static final Logger log = LoggerFactory.getLogger(InstallerRegistry.class); | ||
| 49 | + | ||
| 50 | + private final ConcurrentMap<Class<? extends Intent>, | ||
| 51 | + IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); | ||
| 52 | + /** | ||
| 53 | + * Registers the specified installer for the given installable intent class. | ||
| 54 | + * | ||
| 55 | + * @param cls installable intent class | ||
| 56 | + * @param installer intent installer | ||
| 57 | + * @param <T> the type of installable intent | ||
| 58 | + */ | ||
| 59 | + <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { | ||
| 60 | + installers.put(cls, installer); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * Unregisters the installer for the given installable intent class. | ||
| 65 | + * | ||
| 66 | + * @param cls installable intent class | ||
| 67 | + * @param <T> the type of installable intent | ||
| 68 | + */ | ||
| 69 | + <T extends Intent> void unregisterInstaller(Class<T> cls) { | ||
| 70 | + installers.remove(cls); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Returns immutable set of bindings of currently registered intent installers. | ||
| 75 | + * | ||
| 76 | + * @return the set of installer bindings | ||
| 77 | + */ | ||
| 78 | + Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { | ||
| 79 | + return ImmutableMap.copyOf(installers); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * Returns the corresponding intent installer to the specified installable intent. | ||
| 84 | + * | ||
| 85 | + * @param intent intent | ||
| 86 | + * @param <T> the type of installable intent | ||
| 87 | + * @return intent installer corresponding to the specified installable intent | ||
| 88 | + */ | ||
| 89 | + private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { | ||
| 90 | + @SuppressWarnings("unchecked") | ||
| 91 | + IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); | ||
| 92 | + if (installer == null) { | ||
| 93 | + throw new IntentException("no installer for class " + intent.getClass()); | ||
| 94 | + } | ||
| 95 | + return installer; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + /** | ||
| 99 | + * Registers an intent installer of the specified intent if an intent installer | ||
| 100 | + * for the intent is not registered. This method traverses the class hierarchy of | ||
| 101 | + * the intent. Once an intent installer for a parent type is found, this method | ||
| 102 | + * registers the found intent installer. | ||
| 103 | + * | ||
| 104 | + * @param intent intent | ||
| 105 | + */ | ||
| 106 | + private void registerSubclassInstallerIfNeeded(Intent intent) { | ||
| 107 | + if (!installers.containsKey(intent.getClass())) { | ||
| 108 | + Class<?> cls = intent.getClass(); | ||
| 109 | + while (cls != Object.class) { | ||
| 110 | + // As long as we're within the Intent class descendants | ||
| 111 | + if (Intent.class.isAssignableFrom(cls)) { | ||
| 112 | + IntentInstaller<?> installer = installers.get(cls); | ||
| 113 | + if (installer != null) { | ||
| 114 | + installers.put(intent.getClass(), installer); | ||
| 115 | + return; | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + cls = cls.getSuperclass(); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + /** | ||
| 124 | + * Generate a {@link FlowRuleOperations} instance from the specified intent data. | ||
| 125 | + * | ||
| 126 | + * @param current intent data stored in the store | ||
| 127 | + * @param pending intent data being processed | ||
| 128 | + * @param store intent store saving the intent state in this method | ||
| 129 | + * @param trackerService objective tracker that is used in this method | ||
| 130 | + * @return flow rule operations | ||
| 131 | + */ | ||
| 132 | + public FlowRuleOperations coordinate(IntentData current, IntentData pending, | ||
| 133 | + IntentStore store, ObjectiveTrackerService trackerService) { | ||
| 134 | + List<Intent> oldInstallables = (current != null) ? current.installables() : null; | ||
| 135 | + List<Intent> newInstallables = pending.installables(); | ||
| 136 | + | ||
| 137 | + checkState(isNullOrEmpty(oldInstallables) || | ||
| 138 | + oldInstallables.size() == newInstallables.size(), | ||
| 139 | + "Old and New Intent must have equivalent installable intents."); | ||
| 140 | + | ||
| 141 | + List<List<Collection<FlowRuleOperation>>> plans = new ArrayList<>(); | ||
| 142 | + for (int i = 0; i < newInstallables.size(); i++) { | ||
| 143 | + Intent newInstallable = newInstallables.get(i); | ||
| 144 | + registerSubclassInstallerIfNeeded(newInstallable); | ||
| 145 | + //TODO consider migrating installers to FlowRuleOperations | ||
| 146 | + /* FIXME | ||
| 147 | + - we need to do another pass on this method about that doesn't | ||
| 148 | + require the length of installables to be equal, and also doesn't | ||
| 149 | + depend on ordering | ||
| 150 | + - we should also reconsider when to start/stop tracking resources | ||
| 151 | + */ | ||
| 152 | + if (isNullOrEmpty(oldInstallables)) { | ||
| 153 | + plans.add(getInstaller(newInstallable).install(newInstallable)); | ||
| 154 | + } else { | ||
| 155 | + Intent oldInstallable = oldInstallables.get(i); | ||
| 156 | + checkState(oldInstallable.getClass().equals(newInstallable.getClass()), | ||
| 157 | + "Installable Intent type mismatch."); | ||
| 158 | + trackerService.removeTrackedResources(pending.key(), oldInstallable.resources()); | ||
| 159 | + plans.add(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); | ||
| 160 | + } | ||
| 161 | + trackerService.addTrackedResources(pending.key(), newInstallable.resources()); | ||
| 162 | +// } catch (IntentException e) { | ||
| 163 | +// log.warn("Unable to update intent {} due to:", oldIntent.id(), e); | ||
| 164 | +// //FIXME... we failed. need to uninstall (if same) or revert (if different) | ||
| 165 | +// trackerService.removeTrackedResources(newIntent.id(), newInstallable.resources()); | ||
| 166 | +// exception = e; | ||
| 167 | +// batches = uninstallIntent(oldIntent, oldInstallables); | ||
| 168 | +// } | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + return merge(plans).build(new FlowRuleOperationsContext() { // TODO move this out | ||
| 172 | + @Override | ||
| 173 | + public void onSuccess(FlowRuleOperations ops) { | ||
| 174 | + log.debug("Completed installing: {}", pending.key()); | ||
| 175 | + pending.setState(INSTALLED); | ||
| 176 | + store.write(pending); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + @Override | ||
| 180 | + public void onError(FlowRuleOperations ops) { | ||
| 181 | + log.warn("Failed installation: {} {} on {}", pending.key(), | ||
| 182 | + pending.intent(), ops); | ||
| 183 | + //TODO store.write(pending.setState(BROKEN)); | ||
| 184 | + pending.setState(FAILED); | ||
| 185 | + store.write(pending); | ||
| 186 | + } | ||
| 187 | + }); | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + /** | ||
| 191 | + * Generate a {@link FlowRuleOperations} instance from the specified intent data. | ||
| 192 | + * | ||
| 193 | + * @param current intent data stored in the store | ||
| 194 | + * @param pending intent date being processed | ||
| 195 | + * @param store intent store saving the intent state in this method | ||
| 196 | + * @param trackerService objective tracker that is used in this method | ||
| 197 | + * @return flow rule operations | ||
| 198 | + */ | ||
| 199 | + FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending, | ||
| 200 | + IntentStore store, ObjectiveTrackerService trackerService) { | ||
| 201 | + List<Intent> installables = current.installables(); | ||
| 202 | + List<List<Collection<FlowRuleOperation>>> plans = new ArrayList<>(); | ||
| 203 | + for (Intent installable : installables) { | ||
| 204 | + plans.add(getInstaller(installable).uninstall(installable)); | ||
| 205 | + trackerService.removeTrackedResources(pending.key(), installable.resources()); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + return merge(plans).build(new FlowRuleOperationsContext() { | ||
| 209 | + @Override | ||
| 210 | + public void onSuccess(FlowRuleOperations ops) { | ||
| 211 | + log.debug("Completed withdrawing: {}", pending.key()); | ||
| 212 | + pending.setState(WITHDRAWN); | ||
| 213 | + pending.setInstallables(Collections.emptyList()); | ||
| 214 | + store.write(pending); | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + @Override | ||
| 218 | + public void onError(FlowRuleOperations ops) { | ||
| 219 | + log.warn("Failed withdraw: {}", pending.key()); | ||
| 220 | + pending.setState(FAILED); | ||
| 221 | + store.write(pending); | ||
| 222 | + } | ||
| 223 | + }); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + | ||
| 227 | + // TODO needs tests... or maybe it's just perfect | ||
| 228 | + private FlowRuleOperations.Builder merge(List<List<Collection<FlowRuleOperation>>> plans) { | ||
| 229 | + FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); | ||
| 230 | + // Build a batch one stage at a time | ||
| 231 | + for (int stageNumber = 0;; stageNumber++) { | ||
| 232 | + // Get the sub-stage from each plan (List<Set<FlowRuleOperation>) | ||
| 233 | + for (Iterator<List<Collection<FlowRuleOperation>>> itr = plans.iterator(); itr.hasNext();) { | ||
| 234 | + List<Collection<FlowRuleOperation>> plan = itr.next(); | ||
| 235 | + if (plan.size() <= stageNumber) { | ||
| 236 | + // we have consumed all stages from this plan, so remove it | ||
| 237 | + itr.remove(); | ||
| 238 | + continue; | ||
| 239 | + } | ||
| 240 | + // write operations from this sub-stage into the builder | ||
| 241 | + Collection<FlowRuleOperation> stage = plan.get(stageNumber); | ||
| 242 | + for (FlowRuleOperation entry : stage) { | ||
| 243 | + builder.operation(entry); | ||
| 244 | + } | ||
| 245 | + } | ||
| 246 | + // we are done with the stage, start the next one... | ||
| 247 | + if (plans.isEmpty()) { | ||
| 248 | + break; // we don't need to start a new stage, we are done. | ||
| 249 | + } | ||
| 250 | + builder.newStage(); | ||
| 251 | + } | ||
| 252 | + return builder; | ||
| 253 | + } | ||
| 254 | +} |
| ... | @@ -16,7 +16,6 @@ | ... | @@ -16,7 +16,6 @@ |
| 16 | package org.onosproject.net.intent.impl; | 16 | package org.onosproject.net.intent.impl; |
| 17 | 17 | ||
| 18 | import com.google.common.collect.ImmutableList; | 18 | import com.google.common.collect.ImmutableList; |
| 19 | -import com.google.common.collect.ImmutableMap; | ||
| 20 | import org.apache.felix.scr.annotations.Activate; | 19 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; | 20 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; | 21 | import org.apache.felix.scr.annotations.Deactivate; |
| ... | @@ -27,16 +26,13 @@ import org.onosproject.core.CoreService; | ... | @@ -27,16 +26,13 @@ import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.IdGenerator; | 26 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.event.AbstractListenerRegistry; | 27 | import org.onosproject.event.AbstractListenerRegistry; |
| 29 | import org.onosproject.event.EventDeliveryService; | 28 | import org.onosproject.event.EventDeliveryService; |
| 30 | -import org.onosproject.net.flow.FlowRuleOperation; | ||
| 31 | import org.onosproject.net.flow.FlowRuleOperations; | 29 | import org.onosproject.net.flow.FlowRuleOperations; |
| 32 | -import org.onosproject.net.flow.FlowRuleOperationsContext; | ||
| 33 | import org.onosproject.net.flow.FlowRuleService; | 30 | import org.onosproject.net.flow.FlowRuleService; |
| 34 | import org.onosproject.net.intent.Intent; | 31 | import org.onosproject.net.intent.Intent; |
| 35 | import org.onosproject.net.intent.IntentBatchDelegate; | 32 | import org.onosproject.net.intent.IntentBatchDelegate; |
| 36 | import org.onosproject.net.intent.IntentCompiler; | 33 | import org.onosproject.net.intent.IntentCompiler; |
| 37 | import org.onosproject.net.intent.IntentData; | 34 | import org.onosproject.net.intent.IntentData; |
| 38 | import org.onosproject.net.intent.IntentEvent; | 35 | import org.onosproject.net.intent.IntentEvent; |
| 39 | -import org.onosproject.net.intent.IntentException; | ||
| 40 | import org.onosproject.net.intent.IntentExtensionService; | 36 | import org.onosproject.net.intent.IntentExtensionService; |
| 41 | import org.onosproject.net.intent.IntentInstaller; | 37 | import org.onosproject.net.intent.IntentInstaller; |
| 42 | import org.onosproject.net.intent.IntentListener; | 38 | import org.onosproject.net.intent.IntentListener; |
| ... | @@ -53,29 +49,26 @@ import org.onosproject.net.intent.impl.phase.WithdrawRequest; | ... | @@ -53,29 +49,26 @@ import org.onosproject.net.intent.impl.phase.WithdrawRequest; |
| 53 | import org.onosproject.net.intent.impl.phase.Withdrawn; | 49 | import org.onosproject.net.intent.impl.phase.Withdrawn; |
| 54 | import org.slf4j.Logger; | 50 | import org.slf4j.Logger; |
| 55 | 51 | ||
| 56 | -import java.util.ArrayList; | ||
| 57 | import java.util.Collection; | 52 | import java.util.Collection; |
| 58 | -import java.util.Collections; | ||
| 59 | import java.util.EnumSet; | 53 | import java.util.EnumSet; |
| 60 | -import java.util.Iterator; | ||
| 61 | import java.util.List; | 54 | import java.util.List; |
| 62 | import java.util.Map; | 55 | import java.util.Map; |
| 63 | import java.util.Optional; | 56 | import java.util.Optional; |
| 64 | import java.util.concurrent.Callable; | 57 | import java.util.concurrent.Callable; |
| 65 | -import java.util.concurrent.ConcurrentHashMap; | ||
| 66 | -import java.util.concurrent.ConcurrentMap; | ||
| 67 | import java.util.concurrent.ExecutionException; | 58 | import java.util.concurrent.ExecutionException; |
| 68 | import java.util.concurrent.ExecutorService; | 59 | import java.util.concurrent.ExecutorService; |
| 69 | import java.util.concurrent.Future; | 60 | import java.util.concurrent.Future; |
| 70 | import java.util.stream.Collectors; | 61 | import java.util.stream.Collectors; |
| 71 | 62 | ||
| 72 | import static com.google.common.base.Preconditions.checkNotNull; | 63 | import static com.google.common.base.Preconditions.checkNotNull; |
| 73 | -import static com.google.common.base.Preconditions.checkState; | ||
| 74 | import static java.util.concurrent.Executors.newFixedThreadPool; | 64 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 75 | import static java.util.concurrent.Executors.newSingleThreadExecutor; | 65 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 76 | import static org.onlab.util.Tools.groupedThreads; | 66 | import static org.onlab.util.Tools.groupedThreads; |
| 77 | import static org.onlab.util.Tools.isNullOrEmpty; | 67 | import static org.onlab.util.Tools.isNullOrEmpty; |
| 78 | -import static org.onosproject.net.intent.IntentState.*; | 68 | +import static org.onosproject.net.intent.IntentState.FAILED; |
| 69 | +import static org.onosproject.net.intent.IntentState.INSTALL_REQ; | ||
| 70 | +import static org.onosproject.net.intent.IntentState.WITHDRAWN; | ||
| 71 | +import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; | ||
| 79 | import static org.slf4j.LoggerFactory.getLogger; | 72 | import static org.slf4j.LoggerFactory.getLogger; |
| 80 | 73 | ||
| 81 | /** | 74 | /** |
| ... | @@ -95,12 +88,6 @@ public class IntentManager | ... | @@ -95,12 +88,6 @@ public class IntentManager |
| 95 | private static final EnumSet<IntentState> RECOMPILE | 88 | private static final EnumSet<IntentState> RECOMPILE |
| 96 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); | 89 | = EnumSet.of(INSTALL_REQ, FAILED, WITHDRAW_REQ); |
| 97 | 90 | ||
| 98 | - // Collections for compiler, installer, and listener are ONOS instance local | ||
| 99 | - private final ConcurrentMap<Class<? extends Intent>, | ||
| 100 | - IntentCompiler<? extends Intent>> compilers = new ConcurrentHashMap<>(); | ||
| 101 | - private final ConcurrentMap<Class<? extends Intent>, | ||
| 102 | - IntentInstaller<? extends Intent>> installers = new ConcurrentHashMap<>(); | ||
| 103 | - | ||
| 104 | private final AbstractListenerRegistry<IntentEvent, IntentListener> | 91 | private final AbstractListenerRegistry<IntentEvent, IntentListener> |
| 105 | listenerRegistry = new AbstractListenerRegistry<>(); | 92 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 106 | 93 | ||
| ... | @@ -124,6 +111,9 @@ public class IntentManager | ... | @@ -124,6 +111,9 @@ public class IntentManager |
| 124 | private ExecutorService batchExecutor; | 111 | private ExecutorService batchExecutor; |
| 125 | private ExecutorService workerExecutor; | 112 | private ExecutorService workerExecutor; |
| 126 | 113 | ||
| 114 | + private final CompilerRegistry compilerRegistry = new CompilerRegistry(); | ||
| 115 | + private final InstallerRegistry installerRegistry = new InstallerRegistry(); | ||
| 116 | + private final InternalIntentProcessor processor = new InternalIntentProcessor(); | ||
| 127 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); | 117 | private final IntentStoreDelegate delegate = new InternalStoreDelegate(); |
| 128 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); | 118 | private final TopologyChangeDelegate topoDelegate = new InternalTopoChangeDelegate(); |
| 129 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); | 119 | private final IntentBatchDelegate batchDelegate = new InternalBatchDelegate(); |
| ... | @@ -211,259 +201,32 @@ public class IntentManager | ... | @@ -211,259 +201,32 @@ public class IntentManager |
| 211 | 201 | ||
| 212 | @Override | 202 | @Override |
| 213 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { | 203 | public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) { |
| 214 | - compilers.put(cls, compiler); | 204 | + compilerRegistry.registerCompiler(cls, compiler); |
| 215 | } | 205 | } |
| 216 | 206 | ||
| 217 | @Override | 207 | @Override |
| 218 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { | 208 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 219 | - compilers.remove(cls); | 209 | + compilerRegistry.unregisterCompiler(cls); |
| 220 | } | 210 | } |
| 221 | 211 | ||
| 222 | @Override | 212 | @Override |
| 223 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { | 213 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 224 | - return ImmutableMap.copyOf(compilers); | 214 | + return compilerRegistry.getCompilers(); |
| 225 | } | 215 | } |
| 226 | 216 | ||
| 227 | @Override | 217 | @Override |
| 228 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { | 218 | public <T extends Intent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) { |
| 229 | - installers.put(cls, installer); | 219 | + installerRegistry.registerInstaller(cls, installer); |
| 230 | } | 220 | } |
| 231 | 221 | ||
| 232 | @Override | 222 | @Override |
| 233 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { | 223 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
| 234 | - installers.remove(cls); | 224 | + installerRegistry.unregisterInstaller(cls); |
| 235 | } | 225 | } |
| 236 | 226 | ||
| 237 | @Override | 227 | @Override |
| 238 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { | 228 | public Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> getInstallers() { |
| 239 | - return ImmutableMap.copyOf(installers); | 229 | + return installerRegistry.getInstallers(); |
| 240 | - } | ||
| 241 | - | ||
| 242 | - /** | ||
| 243 | - * Returns the corresponding intent compiler to the specified intent. | ||
| 244 | - * | ||
| 245 | - * @param intent intent | ||
| 246 | - * @param <T> the type of intent | ||
| 247 | - * @return intent compiler corresponding to the specified intent | ||
| 248 | - */ | ||
| 249 | - private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { | ||
| 250 | - @SuppressWarnings("unchecked") | ||
| 251 | - IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); | ||
| 252 | - if (compiler == null) { | ||
| 253 | - throw new IntentException("no compiler for class " + intent.getClass()); | ||
| 254 | - } | ||
| 255 | - return compiler; | ||
| 256 | - } | ||
| 257 | - | ||
| 258 | - /** | ||
| 259 | - * Returns the corresponding intent installer to the specified installable intent. | ||
| 260 | - * | ||
| 261 | - * @param intent intent | ||
| 262 | - * @param <T> the type of installable intent | ||
| 263 | - * @return intent installer corresponding to the specified installable intent | ||
| 264 | - */ | ||
| 265 | - private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { | ||
| 266 | - @SuppressWarnings("unchecked") | ||
| 267 | - IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass()); | ||
| 268 | - if (installer == null) { | ||
| 269 | - throw new IntentException("no installer for class " + intent.getClass()); | ||
| 270 | - } | ||
| 271 | - return installer; | ||
| 272 | - } | ||
| 273 | - | ||
| 274 | - /** | ||
| 275 | - * Compiles an intent recursively. | ||
| 276 | - * | ||
| 277 | - * @param intent intent | ||
| 278 | - * @param previousInstallables previous intent installables | ||
| 279 | - * @return result of compilation | ||
| 280 | - */ | ||
| 281 | - // TODO: make this non-public due to short term hack for ONOS-1051 | ||
| 282 | - public List<Intent> compileIntent(Intent intent, List<Intent> previousInstallables) { | ||
| 283 | - if (intent.isInstallable()) { | ||
| 284 | - return ImmutableList.of(intent); | ||
| 285 | - } | ||
| 286 | - | ||
| 287 | - registerSubclassCompilerIfNeeded(intent); | ||
| 288 | - // FIXME: get previous resources | ||
| 289 | - List<Intent> installable = new ArrayList<>(); | ||
| 290 | - for (Intent compiled : getCompiler(intent).compile(intent, previousInstallables, null)) { | ||
| 291 | - installable.addAll(compileIntent(compiled, previousInstallables)); | ||
| 292 | - } | ||
| 293 | - return installable; | ||
| 294 | - } | ||
| 295 | - | ||
| 296 | - //TODO javadoc | ||
| 297 | - //FIXME | ||
| 298 | - // TODO: make this non-public due to short term hack for ONOS-1051 | ||
| 299 | - public FlowRuleOperations coordinate(IntentData current, IntentData pending) { | ||
| 300 | - List<Intent> oldInstallables = (current != null) ? current.installables() : null; | ||
| 301 | - List<Intent> newInstallables = pending.installables(); | ||
| 302 | - | ||
| 303 | - checkState(isNullOrEmpty(oldInstallables) || | ||
| 304 | - oldInstallables.size() == newInstallables.size(), | ||
| 305 | - "Old and New Intent must have equivalent installable intents."); | ||
| 306 | - | ||
| 307 | - List<List<Collection<FlowRuleOperation>>> plans = new ArrayList<>(); | ||
| 308 | - for (int i = 0; i < newInstallables.size(); i++) { | ||
| 309 | - Intent newInstallable = newInstallables.get(i); | ||
| 310 | - registerSubclassInstallerIfNeeded(newInstallable); | ||
| 311 | - //TODO consider migrating installers to FlowRuleOperations | ||
| 312 | - /* FIXME | ||
| 313 | - - we need to do another pass on this method about that doesn't | ||
| 314 | - require the length of installables to be equal, and also doesn't | ||
| 315 | - depend on ordering | ||
| 316 | - - we should also reconsider when to start/stop tracking resources | ||
| 317 | - */ | ||
| 318 | - if (isNullOrEmpty(oldInstallables)) { | ||
| 319 | - plans.add(getInstaller(newInstallable).install(newInstallable)); | ||
| 320 | - } else { | ||
| 321 | - Intent oldInstallable = oldInstallables.get(i); | ||
| 322 | - checkState(oldInstallable.getClass().equals(newInstallable.getClass()), | ||
| 323 | - "Installable Intent type mismatch."); | ||
| 324 | - trackerService.removeTrackedResources(pending.key(), oldInstallable.resources()); | ||
| 325 | - plans.add(getInstaller(newInstallable).replace(oldInstallable, newInstallable)); | ||
| 326 | - } | ||
| 327 | - trackerService.addTrackedResources(pending.key(), newInstallable.resources()); | ||
| 328 | -// } catch (IntentException e) { | ||
| 329 | -// log.warn("Unable to update intent {} due to:", oldIntent.id(), e); | ||
| 330 | -// //FIXME... we failed. need to uninstall (if same) or revert (if different) | ||
| 331 | -// trackerService.removeTrackedResources(newIntent.id(), newInstallable.resources()); | ||
| 332 | -// exception = e; | ||
| 333 | -// batches = uninstallIntent(oldIntent, oldInstallables); | ||
| 334 | -// } | ||
| 335 | - } | ||
| 336 | - | ||
| 337 | - return merge(plans).build(new FlowRuleOperationsContext() { // TODO move this out | ||
| 338 | - @Override | ||
| 339 | - public void onSuccess(FlowRuleOperations ops) { | ||
| 340 | - log.debug("Completed installing: {}", pending.key()); | ||
| 341 | - pending.setState(INSTALLED); | ||
| 342 | - store.write(pending); | ||
| 343 | - } | ||
| 344 | - | ||
| 345 | - @Override | ||
| 346 | - public void onError(FlowRuleOperations ops) { | ||
| 347 | - log.warn("Failed installation: {} {} on {}", pending.key(), | ||
| 348 | - pending.intent(), ops); | ||
| 349 | - //TODO store.write(pending.setState(BROKEN)); | ||
| 350 | - pending.setState(FAILED); | ||
| 351 | - store.write(pending); | ||
| 352 | - } | ||
| 353 | - }); | ||
| 354 | - } | ||
| 355 | - | ||
| 356 | - /** | ||
| 357 | - * Generate a {@link FlowRuleOperations} instance from the specified intent data. | ||
| 358 | - * | ||
| 359 | - * @param current intent data stored in the store | ||
| 360 | - * @param pending intent data that is pending | ||
| 361 | - * @return flow rule operations | ||
| 362 | - */ | ||
| 363 | - // TODO: make this non-public due to short term hack for ONOS-1051 | ||
| 364 | - public FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending) { | ||
| 365 | - List<Intent> installables = current.installables(); | ||
| 366 | - List<List<Collection<FlowRuleOperation>>> plans = new ArrayList<>(); | ||
| 367 | - for (Intent installable : installables) { | ||
| 368 | - plans.add(getInstaller(installable).uninstall(installable)); | ||
| 369 | - trackerService.removeTrackedResources(pending.key(), installable.resources()); | ||
| 370 | - } | ||
| 371 | - | ||
| 372 | - return merge(plans).build(new FlowRuleOperationsContext() { | ||
| 373 | - @Override | ||
| 374 | - public void onSuccess(FlowRuleOperations ops) { | ||
| 375 | - log.debug("Completed withdrawing: {}", pending.key()); | ||
| 376 | - pending.setState(WITHDRAWN); | ||
| 377 | - pending.setInstallables(Collections.emptyList()); | ||
| 378 | - store.write(pending); | ||
| 379 | - } | ||
| 380 | - | ||
| 381 | - @Override | ||
| 382 | - public void onError(FlowRuleOperations ops) { | ||
| 383 | - log.warn("Failed withdraw: {}", pending.key()); | ||
| 384 | - pending.setState(FAILED); | ||
| 385 | - store.write(pending); | ||
| 386 | - } | ||
| 387 | - }); | ||
| 388 | - } | ||
| 389 | - | ||
| 390 | - | ||
| 391 | - // TODO needs tests... or maybe it's just perfect | ||
| 392 | - private FlowRuleOperations.Builder merge(List<List<Collection<FlowRuleOperation>>> plans) { | ||
| 393 | - FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); | ||
| 394 | - // Build a batch one stage at a time | ||
| 395 | - for (int stageNumber = 0;; stageNumber++) { | ||
| 396 | - // Get the sub-stage from each plan (List<Set<FlowRuleOperation>) | ||
| 397 | - for (Iterator<List<Collection<FlowRuleOperation>>> itr = plans.iterator(); itr.hasNext();) { | ||
| 398 | - List<Collection<FlowRuleOperation>> plan = itr.next(); | ||
| 399 | - if (plan.size() <= stageNumber) { | ||
| 400 | - // we have consumed all stages from this plan, so remove it | ||
| 401 | - itr.remove(); | ||
| 402 | - continue; | ||
| 403 | - } | ||
| 404 | - // write operations from this sub-stage into the builder | ||
| 405 | - Collection<FlowRuleOperation> stage = plan.get(stageNumber); | ||
| 406 | - for (FlowRuleOperation entry : stage) { | ||
| 407 | - builder.operation(entry); | ||
| 408 | - } | ||
| 409 | - } | ||
| 410 | - // we are done with the stage, start the next one... | ||
| 411 | - if (plans.isEmpty()) { | ||
| 412 | - break; // we don't need to start a new stage, we are done. | ||
| 413 | - } | ||
| 414 | - builder.newStage(); | ||
| 415 | - } | ||
| 416 | - return builder; | ||
| 417 | - } | ||
| 418 | - | ||
| 419 | - /** | ||
| 420 | - * Registers an intent compiler of the specified intent if an intent compiler | ||
| 421 | - * for the intent is not registered. This method traverses the class hierarchy of | ||
| 422 | - * the intent. Once an intent compiler for a parent type is found, this method | ||
| 423 | - * registers the found intent compiler. | ||
| 424 | - * | ||
| 425 | - * @param intent intent | ||
| 426 | - */ | ||
| 427 | - private void registerSubclassCompilerIfNeeded(Intent intent) { | ||
| 428 | - if (!compilers.containsKey(intent.getClass())) { | ||
| 429 | - Class<?> cls = intent.getClass(); | ||
| 430 | - while (cls != Object.class) { | ||
| 431 | - // As long as we're within the Intent class descendants | ||
| 432 | - if (Intent.class.isAssignableFrom(cls)) { | ||
| 433 | - IntentCompiler<?> compiler = compilers.get(cls); | ||
| 434 | - if (compiler != null) { | ||
| 435 | - compilers.put(intent.getClass(), compiler); | ||
| 436 | - return; | ||
| 437 | - } | ||
| 438 | - } | ||
| 439 | - cls = cls.getSuperclass(); | ||
| 440 | - } | ||
| 441 | - } | ||
| 442 | - } | ||
| 443 | - | ||
| 444 | - /** | ||
| 445 | - * Registers an intent installer of the specified intent if an intent installer | ||
| 446 | - * for the intent is not registered. This method traverses the class hierarchy of | ||
| 447 | - * the intent. Once an intent installer for a parent type is found, this method | ||
| 448 | - * registers the found intent installer. | ||
| 449 | - * | ||
| 450 | - * @param intent intent | ||
| 451 | - */ | ||
| 452 | - private void registerSubclassInstallerIfNeeded(Intent intent) { | ||
| 453 | - if (!installers.containsKey(intent.getClass())) { | ||
| 454 | - Class<?> cls = intent.getClass(); | ||
| 455 | - while (cls != Object.class) { | ||
| 456 | - // As long as we're within the Intent class descendants | ||
| 457 | - if (Intent.class.isAssignableFrom(cls)) { | ||
| 458 | - IntentInstaller<?> installer = installers.get(cls); | ||
| 459 | - if (installer != null) { | ||
| 460 | - installers.put(intent.getClass(), installer); | ||
| 461 | - return; | ||
| 462 | - } | ||
| 463 | - } | ||
| 464 | - cls = cls.getSuperclass(); | ||
| 465 | - } | ||
| 466 | - } | ||
| 467 | } | 230 | } |
| 468 | 231 | ||
| 469 | // Store delegate to re-post events emitted from the store. | 232 | // Store delegate to re-post events emitted from the store. |
| ... | @@ -525,12 +288,12 @@ public class IntentManager | ... | @@ -525,12 +288,12 @@ public class IntentManager |
| 525 | IntentData current = store.getIntentData(intentData.key()); | 288 | IntentData current = store.getIntentData(intentData.key()); |
| 526 | switch (intentData.state()) { | 289 | switch (intentData.state()) { |
| 527 | case INSTALL_REQ: | 290 | case INSTALL_REQ: |
| 528 | - return new InstallRequest(this, intentData, Optional.ofNullable(current)); | 291 | + return new InstallRequest(processor, intentData, Optional.ofNullable(current)); |
| 529 | case WITHDRAW_REQ: | 292 | case WITHDRAW_REQ: |
| 530 | if (current == null || isNullOrEmpty(current.installables())) { | 293 | if (current == null || isNullOrEmpty(current.installables())) { |
| 531 | return new Withdrawn(intentData, WITHDRAWN); | 294 | return new Withdrawn(intentData, WITHDRAWN); |
| 532 | } else { | 295 | } else { |
| 533 | - return new WithdrawRequest(this, intentData, current); | 296 | + return new WithdrawRequest(processor, intentData, current); |
| 534 | } | 297 | } |
| 535 | default: | 298 | default: |
| 536 | // illegal state | 299 | // illegal state |
| ... | @@ -648,4 +411,26 @@ public class IntentManager | ... | @@ -648,4 +411,26 @@ public class IntentManager |
| 648 | // TODO ensure that only one batch is in flight at a time | 411 | // TODO ensure that only one batch is in flight at a time |
| 649 | } | 412 | } |
| 650 | } | 413 | } |
| 414 | + | ||
| 415 | + private class InternalIntentProcessor implements IntentProcessor { | ||
| 416 | + @Override | ||
| 417 | + public List<Intent> compile(Intent intent, List<Intent> previousInstallables) { | ||
| 418 | + return compilerRegistry.compile(intent, previousInstallables); | ||
| 419 | + } | ||
| 420 | + | ||
| 421 | + @Override | ||
| 422 | + public FlowRuleOperations coordinate(IntentData current, IntentData pending) { | ||
| 423 | + return installerRegistry.coordinate(current, pending, store, trackerService); | ||
| 424 | + } | ||
| 425 | + | ||
| 426 | + @Override | ||
| 427 | + public FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending) { | ||
| 428 | + return installerRegistry.uninstallCoordinate(current, pending, store, trackerService); | ||
| 429 | + } | ||
| 430 | + | ||
| 431 | + @Override | ||
| 432 | + public void applyFlowRules(FlowRuleOperations flowRules) { | ||
| 433 | + flowRuleService.apply(flowRules); | ||
| 434 | + } | ||
| 435 | + } | ||
| 651 | } | 436 | } | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2015 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 | + */ | ||
| 16 | +package org.onosproject.net.intent.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.net.flow.FlowRuleOperations; | ||
| 19 | +import org.onosproject.net.intent.Intent; | ||
| 20 | +import org.onosproject.net.intent.IntentData; | ||
| 21 | + | ||
| 22 | +import java.util.List; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * A collection of methods to process an intent. | ||
| 26 | + * | ||
| 27 | + * This interface is public, but intended to be used only by IntentManager and | ||
| 28 | + * IntentProcessPhase subclasses stored under phase package. | ||
| 29 | + */ | ||
| 30 | +public interface IntentProcessor { | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * Compiles an intent recursively. | ||
| 34 | + * | ||
| 35 | + * @param intent intent | ||
| 36 | + * @param previousInstallables previous intent installables | ||
| 37 | + * @return result of compilation | ||
| 38 | + */ | ||
| 39 | + List<Intent> compile(Intent intent, List<Intent> previousInstallables); | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * Generate a {@link FlowRuleOperations} instance from the specified intent data. | ||
| 43 | + * | ||
| 44 | + * @param current intent data stored in the store | ||
| 45 | + * @param pending intent data being processed | ||
| 46 | + * @return flow rule operations | ||
| 47 | + */ | ||
| 48 | + FlowRuleOperations coordinate(IntentData current, IntentData pending); | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * Generate a {@link FlowRuleOperations} instance from the specified intent data. | ||
| 52 | + * | ||
| 53 | + * @param current intent data stored in the store | ||
| 54 | + * @param pending intent data being processed | ||
| 55 | + * @return flow rule operations | ||
| 56 | + */ | ||
| 57 | + FlowRuleOperations uninstallCoordinate(IntentData current, IntentData pending); | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Applies a batch operation of FlowRules. | ||
| 61 | + * | ||
| 62 | + * @param flowRules batch operation to apply | ||
| 63 | + */ | ||
| 64 | + // TODO: consider a better name | ||
| 65 | + // This methods gives strangeness a bit because | ||
| 66 | + // it doesn't receive/return intent related information | ||
| 67 | + void applyFlowRules(FlowRuleOperations flowRules); | ||
| 68 | +} |
| ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; | ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; |
| 18 | import org.onosproject.net.intent.Intent; | 18 | import org.onosproject.net.intent.Intent; |
| 19 | import org.onosproject.net.intent.IntentData; | 19 | import org.onosproject.net.intent.IntentData; |
| 20 | import org.onosproject.net.intent.IntentException; | 20 | import org.onosproject.net.intent.IntentException; |
| 21 | -import org.onosproject.net.intent.impl.IntentManager; | 21 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 22 | import org.slf4j.Logger; | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; | 23 | import org.slf4j.LoggerFactory; |
| 24 | 24 | ||
| ... | @@ -34,13 +34,12 @@ final class Compiling implements IntentProcessPhase { | ... | @@ -34,13 +34,12 @@ final class Compiling implements IntentProcessPhase { |
| 34 | 34 | ||
| 35 | private static final Logger log = LoggerFactory.getLogger(Compiling.class); | 35 | private static final Logger log = LoggerFactory.getLogger(Compiling.class); |
| 36 | 36 | ||
| 37 | - // TODO: define an interface and use it, instead of IntentManager | 37 | + private final IntentProcessor processor; |
| 38 | - private final IntentManager intentManager; | ||
| 39 | private final IntentData pending; | 38 | private final IntentData pending; |
| 40 | private final IntentData current; | 39 | private final IntentData current; |
| 41 | 40 | ||
| 42 | - Compiling(IntentManager intentManager, IntentData pending, IntentData current) { | 41 | + Compiling(IntentProcessor processor, IntentData pending, IntentData current) { |
| 43 | - this.intentManager = checkNotNull(intentManager); | 42 | + this.processor = checkNotNull(processor); |
| 44 | this.pending = checkNotNull(pending); | 43 | this.pending = checkNotNull(pending); |
| 45 | this.current = current; | 44 | this.current = current; |
| 46 | } | 45 | } |
| ... | @@ -49,11 +48,12 @@ final class Compiling implements IntentProcessPhase { | ... | @@ -49,11 +48,12 @@ final class Compiling implements IntentProcessPhase { |
| 49 | public Optional<IntentProcessPhase> execute() { | 48 | public Optional<IntentProcessPhase> execute() { |
| 50 | try { | 49 | try { |
| 51 | List<Intent> installables = (current != null) ? current.installables() : null; | 50 | List<Intent> installables = (current != null) ? current.installables() : null; |
| 52 | - pending.setInstallables(intentManager.compileIntent(pending.intent(), installables)); | 51 | + pending.setInstallables(processor.compile(pending.intent(), installables)); |
| 53 | - return Optional.of(new InstallCoordinating(intentManager, pending, current)); | 52 | + return Optional.of(new InstallCoordinating(processor, pending, current)); |
| 54 | } catch (IntentException e) { | 53 | } catch (IntentException e) { |
| 55 | log.debug("Unable to compile intent {} due to: {}", pending.intent(), e); | 54 | log.debug("Unable to compile intent {} due to: {}", pending.intent(), e); |
| 56 | return Optional.of(new CompilingFailed(pending)); | 55 | return Optional.of(new CompilingFailed(pending)); |
| 57 | } | 56 | } |
| 58 | } | 57 | } |
| 58 | + | ||
| 59 | } | 59 | } | ... | ... |
| ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; | ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; |
| 18 | import org.onosproject.net.flow.FlowRuleOperations; | 18 | import org.onosproject.net.flow.FlowRuleOperations; |
| 19 | import org.onosproject.net.intent.IntentData; | 19 | import org.onosproject.net.intent.IntentData; |
| 20 | import org.onosproject.net.intent.IntentException; | 20 | import org.onosproject.net.intent.IntentException; |
| 21 | -import org.onosproject.net.intent.impl.IntentManager; | 21 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 22 | import org.slf4j.Logger; | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; | 23 | import org.slf4j.LoggerFactory; |
| 24 | 24 | ||
| ... | @@ -34,13 +34,13 @@ final class InstallCoordinating implements IntentProcessPhase { | ... | @@ -34,13 +34,13 @@ final class InstallCoordinating implements IntentProcessPhase { |
| 34 | 34 | ||
| 35 | private static final Logger log = LoggerFactory.getLogger(InstallCoordinating.class); | 35 | private static final Logger log = LoggerFactory.getLogger(InstallCoordinating.class); |
| 36 | 36 | ||
| 37 | - private final IntentManager intentManager; | 37 | + private final IntentProcessor processor; |
| 38 | private final IntentData pending; | 38 | private final IntentData pending; |
| 39 | private final IntentData current; | 39 | private final IntentData current; |
| 40 | 40 | ||
| 41 | // TODO: define an interface and use it, instead of IntentManager | 41 | // TODO: define an interface and use it, instead of IntentManager |
| 42 | - InstallCoordinating(IntentManager intentManager, IntentData pending, IntentData current) { | 42 | + InstallCoordinating(IntentProcessor processor, IntentData pending, IntentData current) { |
| 43 | - this.intentManager = checkNotNull(intentManager); | 43 | + this.processor = checkNotNull(processor); |
| 44 | this.pending = checkNotNull(pending); | 44 | this.pending = checkNotNull(pending); |
| 45 | this.current = current; | 45 | this.current = current; |
| 46 | } | 46 | } |
| ... | @@ -50,8 +50,8 @@ final class InstallCoordinating implements IntentProcessPhase { | ... | @@ -50,8 +50,8 @@ final class InstallCoordinating implements IntentProcessPhase { |
| 50 | try { | 50 | try { |
| 51 | //FIXME we orphan flow rules that are currently on the data plane | 51 | //FIXME we orphan flow rules that are currently on the data plane |
| 52 | // ... should either reuse them or remove them | 52 | // ... should either reuse them or remove them |
| 53 | - FlowRuleOperations flowRules = intentManager.coordinate(current, pending); | 53 | + FlowRuleOperations flowRules = processor.coordinate(current, pending); |
| 54 | - return Optional.of(new Installing(intentManager, pending, flowRules)); | 54 | + return Optional.of(new Installing(processor, pending, flowRules)); |
| 55 | } catch (IntentException e) { | 55 | } catch (IntentException e) { |
| 56 | log.warn("Unable to generate a FlowRuleOperations from intent {} due to:", pending.intent().id(), e); | 56 | log.warn("Unable to generate a FlowRuleOperations from intent {} due to:", pending.intent().id(), e); |
| 57 | return Optional.of(new InstallingFailed(pending)); | 57 | return Optional.of(new InstallingFailed(pending)); | ... | ... |
| ... | @@ -16,7 +16,7 @@ | ... | @@ -16,7 +16,7 @@ |
| 16 | package org.onosproject.net.intent.impl.phase; | 16 | package org.onosproject.net.intent.impl.phase; |
| 17 | 17 | ||
| 18 | import org.onosproject.net.intent.IntentData; | 18 | import org.onosproject.net.intent.IntentData; |
| 19 | -import org.onosproject.net.intent.impl.IntentManager; | 19 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 20 | 20 | ||
| 21 | import java.util.Optional; | 21 | import java.util.Optional; |
| 22 | 22 | ||
| ... | @@ -27,13 +27,12 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -27,13 +27,12 @@ import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | */ | 27 | */ |
| 28 | public final class InstallRequest implements IntentProcessPhase { | 28 | public final class InstallRequest implements IntentProcessPhase { |
| 29 | 29 | ||
| 30 | - // TODO: define an interface and use it, instead of IntentManager | 30 | + private final IntentProcessor intentManager; |
| 31 | - private final IntentManager intentManager; | ||
| 32 | private final IntentData pending; | 31 | private final IntentData pending; |
| 33 | private final Optional<IntentData> current; | 32 | private final Optional<IntentData> current; |
| 34 | 33 | ||
| 35 | - public InstallRequest(IntentManager intentManager, IntentData intentData, Optional<IntentData> current) { | 34 | + public InstallRequest(IntentProcessor processor, IntentData intentData, Optional<IntentData> current) { |
| 36 | - this.intentManager = checkNotNull(intentManager); | 35 | + this.intentManager = checkNotNull(processor); |
| 37 | this.pending = checkNotNull(intentData); | 36 | this.pending = checkNotNull(intentData); |
| 38 | this.current = checkNotNull(current); | 37 | this.current = checkNotNull(current); |
| 39 | } | 38 | } | ... | ... |
| ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; | ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; |
| 18 | import org.onosproject.net.flow.FlowRuleOperations; | 18 | import org.onosproject.net.flow.FlowRuleOperations; |
| 19 | import org.onosproject.net.intent.IntentData; | 19 | import org.onosproject.net.intent.IntentData; |
| 20 | import org.onosproject.net.intent.IntentException; | 20 | import org.onosproject.net.intent.IntentException; |
| 21 | -import org.onosproject.net.intent.impl.IntentManager; | 21 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 22 | import org.slf4j.Logger; | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; | 23 | import org.slf4j.LoggerFactory; |
| 24 | 24 | ||
| ... | @@ -34,13 +34,12 @@ final class Installing implements IntentProcessPhase { | ... | @@ -34,13 +34,12 @@ final class Installing implements IntentProcessPhase { |
| 34 | 34 | ||
| 35 | private static final Logger log = LoggerFactory.getLogger(Installing.class); | 35 | private static final Logger log = LoggerFactory.getLogger(Installing.class); |
| 36 | 36 | ||
| 37 | - private final IntentManager intentManager; | 37 | + private final IntentProcessor processor; |
| 38 | private final IntentData pending; | 38 | private final IntentData pending; |
| 39 | private final FlowRuleOperations flowRules; | 39 | private final FlowRuleOperations flowRules; |
| 40 | 40 | ||
| 41 | - // TODO: define an interface and use it, instead of IntentManager | 41 | + Installing(IntentProcessor processor, IntentData pending, FlowRuleOperations flowRules) { |
| 42 | - Installing(IntentManager intentManager, IntentData pending, FlowRuleOperations flowRules) { | 42 | + this.processor = checkNotNull(processor); |
| 43 | - this.intentManager = checkNotNull(intentManager); | ||
| 44 | this.pending = checkNotNull(pending); | 43 | this.pending = checkNotNull(pending); |
| 45 | this.flowRules = flowRules; | 44 | this.flowRules = flowRules; |
| 46 | } | 45 | } |
| ... | @@ -48,7 +47,7 @@ final class Installing implements IntentProcessPhase { | ... | @@ -48,7 +47,7 @@ final class Installing implements IntentProcessPhase { |
| 48 | @Override | 47 | @Override |
| 49 | public Optional<IntentProcessPhase> execute() { | 48 | public Optional<IntentProcessPhase> execute() { |
| 50 | try { | 49 | try { |
| 51 | - intentManager.flowRuleService.apply(flowRules); // FIXME we need to provide a context | 50 | + processor.applyFlowRules(flowRules); |
| 52 | return Optional.of(new Installed(pending)); | 51 | return Optional.of(new Installed(pending)); |
| 53 | // What kinds of exceptions are thrown by FlowRuleService.apply()? | 52 | // What kinds of exceptions are thrown by FlowRuleService.apply()? |
| 54 | // Is IntentException a correct exception abstraction? | 53 | // Is IntentException a correct exception abstraction? | ... | ... |
| ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; | ... | @@ -18,7 +18,7 @@ package org.onosproject.net.intent.impl.phase; |
| 18 | import org.onosproject.net.flow.FlowRuleOperations; | 18 | import org.onosproject.net.flow.FlowRuleOperations; |
| 19 | import org.onosproject.net.intent.IntentData; | 19 | import org.onosproject.net.intent.IntentData; |
| 20 | import org.onosproject.net.intent.IntentException; | 20 | import org.onosproject.net.intent.IntentException; |
| 21 | -import org.onosproject.net.intent.impl.IntentManager; | 21 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 22 | import org.slf4j.Logger; | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; | 23 | import org.slf4j.LoggerFactory; |
| 24 | 24 | ||
| ... | @@ -34,13 +34,12 @@ final class WithdrawCoordinating implements IntentProcessPhase { | ... | @@ -34,13 +34,12 @@ final class WithdrawCoordinating implements IntentProcessPhase { |
| 34 | 34 | ||
| 35 | private static final Logger log = LoggerFactory.getLogger(WithdrawCoordinating.class); | 35 | private static final Logger log = LoggerFactory.getLogger(WithdrawCoordinating.class); |
| 36 | 36 | ||
| 37 | - // TODO: define an interface and use it, instead of IntentManager | 37 | + private final IntentProcessor processor; |
| 38 | - private final IntentManager intentManager; | ||
| 39 | private final IntentData pending; | 38 | private final IntentData pending; |
| 40 | private final IntentData current; | 39 | private final IntentData current; |
| 41 | 40 | ||
| 42 | - WithdrawCoordinating(IntentManager intentManager, IntentData pending, IntentData current) { | 41 | + WithdrawCoordinating(IntentProcessor processor, IntentData pending, IntentData current) { |
| 43 | - this.intentManager = checkNotNull(intentManager); | 42 | + this.processor = checkNotNull(processor); |
| 44 | this.pending = checkNotNull(pending); | 43 | this.pending = checkNotNull(pending); |
| 45 | this.current = checkNotNull(current); | 44 | this.current = checkNotNull(current); |
| 46 | } | 45 | } |
| ... | @@ -49,9 +48,9 @@ final class WithdrawCoordinating implements IntentProcessPhase { | ... | @@ -49,9 +48,9 @@ final class WithdrawCoordinating implements IntentProcessPhase { |
| 49 | public Optional<IntentProcessPhase> execute() { | 48 | public Optional<IntentProcessPhase> execute() { |
| 50 | try { | 49 | try { |
| 51 | // Note: current.installables() are not null or empty due to createIntentUpdate check | 50 | // Note: current.installables() are not null or empty due to createIntentUpdate check |
| 52 | - FlowRuleOperations flowRules = intentManager.uninstallCoordinate(current, pending); | 51 | + FlowRuleOperations flowRules = processor.uninstallCoordinate(current, pending); |
| 53 | pending.setInstallables(current.installables()); | 52 | pending.setInstallables(current.installables()); |
| 54 | - return Optional.of(new Withdrawing(intentManager, pending, flowRules)); | 53 | + return Optional.of(new Withdrawing(processor, pending, flowRules)); |
| 55 | } catch (IntentException e) { | 54 | } catch (IntentException e) { |
| 56 | log.warn("Unable to generate generate a FlowRuleOperations from intent {} due to:", pending.intent(), e); | 55 | log.warn("Unable to generate generate a FlowRuleOperations from intent {} due to:", pending.intent(), e); |
| 57 | return Optional.of(new WithdrawingFailed(pending)); | 56 | return Optional.of(new WithdrawingFailed(pending)); | ... | ... |
| ... | @@ -16,7 +16,7 @@ | ... | @@ -16,7 +16,7 @@ |
| 16 | package org.onosproject.net.intent.impl.phase; | 16 | package org.onosproject.net.intent.impl.phase; |
| 17 | 17 | ||
| 18 | import org.onosproject.net.intent.IntentData; | 18 | import org.onosproject.net.intent.IntentData; |
| 19 | -import org.onosproject.net.intent.impl.IntentManager; | 19 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 20 | 20 | ||
| 21 | import java.util.Optional; | 21 | import java.util.Optional; |
| 22 | 22 | ||
| ... | @@ -27,13 +27,12 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -27,13 +27,12 @@ import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | */ | 27 | */ |
| 28 | public final class WithdrawRequest implements IntentProcessPhase { | 28 | public final class WithdrawRequest implements IntentProcessPhase { |
| 29 | 29 | ||
| 30 | - // TODO: define an interface and use it, instead of IntentManager | 30 | + private final IntentProcessor processor; |
| 31 | - private final IntentManager intentManager; | ||
| 32 | private final IntentData pending; | 31 | private final IntentData pending; |
| 33 | private final IntentData current; | 32 | private final IntentData current; |
| 34 | 33 | ||
| 35 | - public WithdrawRequest(IntentManager intentManager, IntentData intentData, IntentData current) { | 34 | + public WithdrawRequest(IntentProcessor processor, IntentData intentData, IntentData current) { |
| 36 | - this.intentManager = checkNotNull(intentManager); | 35 | + this.processor = checkNotNull(processor); |
| 37 | this.pending = checkNotNull(intentData); | 36 | this.pending = checkNotNull(intentData); |
| 38 | this.current = checkNotNull(current); | 37 | this.current = checkNotNull(current); |
| 39 | } | 38 | } |
| ... | @@ -43,6 +42,6 @@ public final class WithdrawRequest implements IntentProcessPhase { | ... | @@ -43,6 +42,6 @@ public final class WithdrawRequest implements IntentProcessPhase { |
| 43 | //TODO perhaps we want to validate that the pending and current are the | 42 | //TODO perhaps we want to validate that the pending and current are the |
| 44 | // same version i.e. they are the same | 43 | // same version i.e. they are the same |
| 45 | // Note: this call is not just the symmetric version of submit | 44 | // Note: this call is not just the symmetric version of submit |
| 46 | - return Optional.of(new WithdrawCoordinating(intentManager, pending, current)); | 45 | + return Optional.of(new WithdrawCoordinating(processor, pending, current)); |
| 47 | } | 46 | } |
| 48 | } | 47 | } | ... | ... |
| ... | @@ -17,7 +17,7 @@ package org.onosproject.net.intent.impl.phase; | ... | @@ -17,7 +17,7 @@ package org.onosproject.net.intent.impl.phase; |
| 17 | 17 | ||
| 18 | import org.onosproject.net.flow.FlowRuleOperations; | 18 | import org.onosproject.net.flow.FlowRuleOperations; |
| 19 | import org.onosproject.net.intent.IntentData; | 19 | import org.onosproject.net.intent.IntentData; |
| 20 | -import org.onosproject.net.intent.impl.IntentManager; | 20 | +import org.onosproject.net.intent.impl.IntentProcessor; |
| 21 | 21 | ||
| 22 | import java.util.Optional; | 22 | import java.util.Optional; |
| 23 | 23 | ||
| ... | @@ -29,20 +29,19 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -29,20 +29,19 @@ import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | */ | 29 | */ |
| 30 | class Withdrawing implements IntentProcessPhase { | 30 | class Withdrawing implements IntentProcessPhase { |
| 31 | 31 | ||
| 32 | - // TODO: define an interface and use it, instead of IntentManager | 32 | + private final IntentProcessor processor; |
| 33 | - private final IntentManager intentManager; | ||
| 34 | private final IntentData pending; | 33 | private final IntentData pending; |
| 35 | private final FlowRuleOperations flowRules; | 34 | private final FlowRuleOperations flowRules; |
| 36 | 35 | ||
| 37 | - Withdrawing(IntentManager intentManager, IntentData pending, FlowRuleOperations flowRules) { | 36 | + Withdrawing(IntentProcessor processor, IntentData pending, FlowRuleOperations flowRules) { |
| 38 | - this.intentManager = checkNotNull(intentManager); | 37 | + this.processor = checkNotNull(processor); |
| 39 | this.pending = checkNotNull(pending); | 38 | this.pending = checkNotNull(pending); |
| 40 | this.flowRules = checkNotNull(flowRules); | 39 | this.flowRules = checkNotNull(flowRules); |
| 41 | } | 40 | } |
| 42 | 41 | ||
| 43 | @Override | 42 | @Override |
| 44 | public Optional<IntentProcessPhase> execute() { | 43 | public Optional<IntentProcessPhase> execute() { |
| 45 | - intentManager.flowRuleService.apply(flowRules); | 44 | + processor.applyFlowRules(flowRules); |
| 46 | return Optional.of(new Withdrawn(pending)); | 45 | return Optional.of(new Withdrawn(pending)); |
| 47 | } | 46 | } |
| 48 | } | 47 | } | ... | ... |
-
Please register or login to post a comment