Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collections; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
| 27 | |
| 28 | /** |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 29 | * Fake implementation of the intent service to assist in developing tests of |
| 30 | * the interface contract. |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 31 | */ |
| 32 | public class FakeIntentManager implements TestableIntentService { |
| 33 | |
| 34 | private final Map<IntentId, Intent> intents = new HashMap<>(); |
| 35 | private final Map<IntentId, IntentState> intentStates = new HashMap<>(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 36 | private final Map<IntentId, List<Intent>> installables = new HashMap<>(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 37 | private final Set<IntentListener> listeners = new HashSet<>(); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 38 | |
| 39 | private final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = new HashMap<>(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 40 | private final Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> installers |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 41 | = new HashMap<>(); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 42 | |
| 43 | private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 44 | private final List<IntentException> exceptions = new ArrayList<>(); |
| 45 | |
| 46 | @Override |
| 47 | public List<IntentException> getExceptions() { |
| 48 | return exceptions; |
| 49 | } |
| 50 | |
| 51 | // Provides an out-of-thread simulation of intent submit life-cycle |
| 52 | private void executeSubmit(final Intent intent) { |
| 53 | registerSubclassCompilerIfNeeded(intent); |
| 54 | executor.execute(new Runnable() { |
| 55 | @Override |
| 56 | public void run() { |
| 57 | try { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 58 | executeCompilingPhase(intent); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 59 | } catch (IntentException e) { |
| 60 | exceptions.add(e); |
| 61 | } |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | // Provides an out-of-thread simulation of intent withdraw life-cycle |
| 67 | private void executeWithdraw(final Intent intent) { |
| 68 | executor.execute(new Runnable() { |
| 69 | @Override |
| 70 | public void run() { |
| 71 | try { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 72 | List<Intent> installable = getInstallable(intent.id()); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 73 | executeWithdrawingPhase(intent, installable); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 74 | } catch (IntentException e) { |
| 75 | exceptions.add(e); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 83 | @SuppressWarnings("unchecked") |
| 84 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 85 | if (compiler == null) { |
| 86 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 87 | } |
| 88 | return compiler; |
| 89 | } |
| 90 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 91 | private <T extends Intent> IntentInstaller<T> getInstaller(T intent) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 92 | @SuppressWarnings("unchecked") |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 93 | IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent |
| 94 | .getClass()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 95 | if (installer == null) { |
| 96 | throw new IntentException("no installer for class " + intent.getClass()); |
| 97 | } |
| 98 | return installer; |
| 99 | } |
| 100 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 101 | private <T extends Intent> void executeCompilingPhase(T intent) { |
| 102 | setState(intent, IntentState.COMPILING); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 103 | try { |
| 104 | // For the fake, we compile using a single level pass |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 105 | List<Intent> installable = new ArrayList<>(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 106 | for (Intent compiled : getCompiler(intent).compile(intent, null, null)) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 107 | installable.add((Intent) compiled); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 108 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 109 | executeInstallingPhase(intent, installable); |
| 110 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 111 | } catch (IntentException e) { |
| 112 | setState(intent, IntentState.FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 113 | dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 117 | private void executeInstallingPhase(Intent intent, |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 118 | List<Intent> installable) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 119 | setState(intent, IntentState.INSTALLING); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 120 | try { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 121 | for (Intent ii : installable) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 122 | registerSubclassInstallerIfNeeded(ii); |
| 123 | getInstaller(ii).install(ii); |
| 124 | } |
| 125 | setState(intent, IntentState.INSTALLED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 126 | putInstallable(intent.id(), installable); |
| 127 | dispatch(new IntentEvent(IntentEvent.Type.INSTALLED, intent)); |
| 128 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 129 | } catch (IntentException e) { |
| 130 | setState(intent, IntentState.FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 131 | dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 135 | private void executeWithdrawingPhase(Intent intent, |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 136 | List<Intent> installable) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 137 | setState(intent, IntentState.WITHDRAWING); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 138 | try { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 139 | for (Intent ii : installable) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 140 | getInstaller(ii).uninstall(ii); |
| 141 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 142 | removeInstallable(intent.id()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 143 | setState(intent, IntentState.WITHDRAWN); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 144 | dispatch(new IntentEvent(IntentEvent.Type.WITHDRAWN, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 145 | } catch (IntentException e) { |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 146 | // FIXME: Rework this to always go from WITHDRAWING to WITHDRAWN! |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 147 | setState(intent, IntentState.FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 148 | dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 152 | // Sets the internal state for the given intent and dispatches an event |
| 153 | private void setState(Intent intent, IntentState state) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 154 | intentStates.put(intent.id(), state); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 157 | private void putInstallable(IntentId id, List<Intent> installable) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 158 | installables.put(id, installable); |
| 159 | } |
| 160 | |
| 161 | private void removeInstallable(IntentId id) { |
| 162 | installables.remove(id); |
| 163 | } |
| 164 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 165 | private List<Intent> getInstallable(IntentId id) { |
| 166 | List<Intent> installable = installables.get(id); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 167 | if (installable != null) { |
| 168 | return installable; |
| 169 | } else { |
| 170 | return Collections.emptyList(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void submit(Intent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 176 | intents.put(intent.id(), intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 177 | setState(intent, IntentState.INSTALL_REQ); |
| 178 | dispatch(new IntentEvent(IntentEvent.Type.INSTALL_REQ, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 179 | executeSubmit(intent); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void withdraw(Intent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 184 | intents.remove(intent.id()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 185 | executeWithdraw(intent); |
| 186 | } |
| 187 | |
| 188 | @Override |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 189 | public void replace(IntentId oldIntentId, Intent newIntent) { |
| 190 | // TODO: implement later |
| 191 | } |
| 192 | |
| 193 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 194 | public void execute(IntentOperations operations) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 195 | // TODO: implement later |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Set<Intent> getIntents() { |
| 200 | return Collections.unmodifiableSet(new HashSet<>(intents.values())); |
| 201 | } |
| 202 | |
| 203 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 204 | public long getIntentCount() { |
| 205 | return intents.size(); |
| 206 | } |
| 207 | |
| 208 | @Override |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 209 | public Intent getIntent(IntentId id) { |
| 210 | return intents.get(id); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public IntentState getIntentState(IntentId id) { |
| 215 | return intentStates.get(id); |
| 216 | } |
| 217 | |
| 218 | @Override |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 219 | public List<Intent> getInstallableIntents(IntentId intentId) { |
| 220 | return installables.get(intentId); |
| 221 | } |
| 222 | |
| 223 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 224 | public void addListener(IntentListener listener) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 225 | listeners.add(listener); |
| 226 | } |
| 227 | |
| 228 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 229 | public void removeListener(IntentListener listener) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 230 | listeners.remove(listener); |
| 231 | } |
| 232 | |
| 233 | private void dispatch(IntentEvent event) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 234 | for (IntentListener listener : listeners) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 235 | listener.event(event); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 240 | public <T extends Intent> void registerCompiler(Class<T> cls, |
| 241 | IntentCompiler<T> compiler) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 242 | compilers.put(cls, compiler); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 247 | compilers.remove(cls); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 252 | return Collections.unmodifiableMap(compilers); |
| 253 | } |
| 254 | |
| 255 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 256 | public <T extends Intent> void registerInstaller(Class<T> cls, |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 257 | IntentInstaller<T> installer) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 258 | installers.put(cls, installer); |
| 259 | } |
| 260 | |
| 261 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 262 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 263 | installers.remove(cls); |
| 264 | } |
| 265 | |
| 266 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 267 | public Map<Class<? extends Intent>, |
| 268 | IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 269 | return Collections.unmodifiableMap(installers); |
| 270 | } |
| 271 | |
| 272 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 273 | if (!compilers.containsKey(intent.getClass())) { |
| 274 | Class<?> cls = intent.getClass(); |
| 275 | while (cls != Object.class) { |
| 276 | // As long as we're within the Intent class descendants |
| 277 | if (Intent.class.isAssignableFrom(cls)) { |
| 278 | IntentCompiler<?> compiler = compilers.get(cls); |
| 279 | if (compiler != null) { |
| 280 | compilers.put(intent.getClass(), compiler); |
| 281 | return; |
| 282 | } |
| 283 | } |
| 284 | cls = cls.getSuperclass(); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 289 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 290 | if (!installers.containsKey(intent.getClass())) { |
| 291 | Class<?> cls = intent.getClass(); |
| 292 | while (cls != Object.class) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 293 | // As long as we're within the Intent class |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 294 | // descendants |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 295 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 296 | IntentInstaller<?> installer = installers.get(cls); |
| 297 | if (installer != null) { |
| 298 | installers.put(intent.getClass(), installer); |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | cls = cls.getSuperclass(); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | } |