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