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