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