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 | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 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 | |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 34 | private final Map<Key, Intent> intents = new HashMap<>(); |
| 35 | private final Map<Key, IntentState> intentStates = new HashMap<>(); |
| 36 | private final Map<Key, 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 { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 72 | List<Intent> installable = getInstallable(intent.key()); |
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); |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 126 | putInstallable(intent.key(), installable); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 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 | } |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 142 | removeInstallable(intent.key()); |
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) { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 154 | intentStates.put(intent.key(), state); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 157 | private void putInstallable(Key key, List<Intent> installable) { |
| 158 | installables.put(key, installable); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 161 | private void removeInstallable(Key key) { |
| 162 | installables.remove(key); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 165 | private List<Intent> getInstallable(Key key) { |
| 166 | List<Intent> installable = installables.get(key); |
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) { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 176 | intents.put(intent.key(), 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) { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 184 | intents.remove(intent.key()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 185 | executeWithdraw(intent); |
| 186 | } |
| 187 | |
| 188 | @Override |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 189 | public Set<Intent> getIntents() { |
| 190 | return Collections.unmodifiableSet(new HashSet<>(intents.values())); |
| 191 | } |
| 192 | |
| 193 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 194 | public long getIntentCount() { |
| 195 | return intents.size(); |
| 196 | } |
| 197 | |
| 198 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 199 | public Intent getIntent(Key intentKey) { |
| 200 | return intents.get(intentKey); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 204 | public IntentState getIntentState(Key intentKey) { |
| 205 | return intentStates.get(intentKey); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 209 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 210 | return installables.get(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 214 | public void addListener(IntentListener listener) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 215 | listeners.add(listener); |
| 216 | } |
| 217 | |
| 218 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 219 | public void removeListener(IntentListener listener) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 220 | listeners.remove(listener); |
| 221 | } |
| 222 | |
| 223 | private void dispatch(IntentEvent event) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 224 | for (IntentListener listener : listeners) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 225 | listener.event(event); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 230 | public <T extends Intent> void registerCompiler(Class<T> cls, |
| 231 | IntentCompiler<T> compiler) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 232 | compilers.put(cls, compiler); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 237 | compilers.remove(cls); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 242 | return Collections.unmodifiableMap(compilers); |
| 243 | } |
| 244 | |
| 245 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 246 | public <T extends Intent> void registerInstaller(Class<T> cls, |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 247 | IntentInstaller<T> installer) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 248 | installers.put(cls, installer); |
| 249 | } |
| 250 | |
| 251 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 252 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 253 | installers.remove(cls); |
| 254 | } |
| 255 | |
| 256 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 257 | public Map<Class<? extends Intent>, |
| 258 | IntentInstaller<? extends Intent>> getInstallers() { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 259 | return Collections.unmodifiableMap(installers); |
| 260 | } |
| 261 | |
| 262 | private void registerSubclassCompilerIfNeeded(Intent intent) { |
| 263 | if (!compilers.containsKey(intent.getClass())) { |
| 264 | Class<?> cls = intent.getClass(); |
| 265 | while (cls != Object.class) { |
| 266 | // As long as we're within the Intent class descendants |
| 267 | if (Intent.class.isAssignableFrom(cls)) { |
| 268 | IntentCompiler<?> compiler = compilers.get(cls); |
| 269 | if (compiler != null) { |
| 270 | compilers.put(intent.getClass(), compiler); |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | cls = cls.getSuperclass(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 279 | private void registerSubclassInstallerIfNeeded(Intent intent) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 280 | if (!installers.containsKey(intent.getClass())) { |
| 281 | Class<?> cls = intent.getClass(); |
| 282 | while (cls != Object.class) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 283 | // As long as we're within the Intent class |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 284 | // descendants |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 285 | if (Intent.class.isAssignableFrom(cls)) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 286 | IntentInstaller<?> installer = installers.get(cls); |
| 287 | if (installer != null) { |
| 288 | installers.put(intent.getClass(), installer); |
| 289 | return; |
| 290 | } |
| 291 | } |
| 292 | cls = cls.getSuperclass(); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | } |