Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | |
| 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<>(); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 40 | |
| 41 | private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 42 | private final List<IntentException> exceptions = new ArrayList<>(); |
| 43 | |
| 44 | @Override |
| 45 | public List<IntentException> getExceptions() { |
| 46 | return exceptions; |
| 47 | } |
| 48 | |
| 49 | // Provides an out-of-thread simulation of intent submit life-cycle |
| 50 | private void executeSubmit(final Intent intent) { |
| 51 | registerSubclassCompilerIfNeeded(intent); |
| 52 | executor.execute(new Runnable() { |
| 53 | @Override |
| 54 | public void run() { |
| 55 | try { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 56 | executeCompilingPhase(intent); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 57 | } catch (IntentException e) { |
| 58 | exceptions.add(e); |
| 59 | } |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | // Provides an out-of-thread simulation of intent withdraw life-cycle |
| 65 | private void executeWithdraw(final Intent intent) { |
| 66 | executor.execute(new Runnable() { |
| 67 | @Override |
| 68 | public void run() { |
| 69 | try { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 70 | List<Intent> installable = getInstallable(intent.key()); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 71 | executeWithdrawingPhase(intent, installable); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 72 | } catch (IntentException e) { |
| 73 | exceptions.add(e); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | private <T extends Intent> IntentCompiler<T> getCompiler(T intent) { |
| 81 | @SuppressWarnings("unchecked") |
| 82 | IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass()); |
| 83 | if (compiler == null) { |
| 84 | throw new IntentException("no compiler for class " + intent.getClass()); |
| 85 | } |
| 86 | return compiler; |
| 87 | } |
| 88 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 89 | private <T extends Intent> void executeCompilingPhase(T intent) { |
| 90 | setState(intent, IntentState.COMPILING); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 91 | try { |
| 92 | // For the fake, we compile using a single level pass |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 93 | List<Intent> installable = new ArrayList<>(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 94 | for (Intent compiled : getCompiler(intent).compile(intent, null, null)) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 95 | installable.add((Intent) compiled); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 96 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 97 | executeInstallingPhase(intent, installable); |
| 98 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 99 | } catch (IntentException e) { |
| 100 | setState(intent, IntentState.FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 101 | dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 105 | private void executeInstallingPhase(Intent intent, |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 106 | List<Intent> installable) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 107 | setState(intent, IntentState.INSTALLING); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 108 | try { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 109 | setState(intent, IntentState.INSTALLED); |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 110 | putInstallable(intent.key(), installable); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 111 | dispatch(new IntentEvent(IntentEvent.Type.INSTALLED, intent)); |
| 112 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 113 | } catch (IntentException e) { |
| 114 | setState(intent, IntentState.FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 115 | dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 119 | private void executeWithdrawingPhase(Intent intent, |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 120 | List<Intent> installable) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 121 | setState(intent, IntentState.WITHDRAWING); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 122 | try { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 123 | removeInstallable(intent.key()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 124 | setState(intent, IntentState.WITHDRAWN); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 125 | dispatch(new IntentEvent(IntentEvent.Type.WITHDRAWN, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 126 | } catch (IntentException e) { |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 127 | // FIXME: Rework this to always go from WITHDRAWING to WITHDRAWN! |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 128 | setState(intent, IntentState.FAILED); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 129 | dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 133 | // Sets the internal state for the given intent and dispatches an event |
| 134 | private void setState(Intent intent, IntentState state) { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 135 | intentStates.put(intent.key(), state); |
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 putInstallable(Key key, List<Intent> installable) { |
| 139 | installables.put(key, installable); |
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 void removeInstallable(Key key) { |
| 143 | installables.remove(key); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 146 | private List<Intent> getInstallable(Key key) { |
| 147 | List<Intent> installable = installables.get(key); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 148 | if (installable != null) { |
| 149 | return installable; |
| 150 | } else { |
| 151 | return Collections.emptyList(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void submit(Intent intent) { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 157 | intents.put(intent.key(), intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 158 | setState(intent, IntentState.INSTALL_REQ); |
| 159 | dispatch(new IntentEvent(IntentEvent.Type.INSTALL_REQ, intent)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 160 | executeSubmit(intent); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void withdraw(Intent intent) { |
Ray Milkey | 4f5d93e | 2015-02-10 17:04:10 -0800 | [diff] [blame] | 165 | intents.remove(intent.key()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 166 | executeWithdraw(intent); |
| 167 | } |
| 168 | |
| 169 | @Override |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 170 | public void purge(Intent intent) { |
| 171 | IntentState currentState = intentStates.get(intent.key()); |
| 172 | if (currentState == IntentState.WITHDRAWN |
| 173 | || currentState == IntentState.FAILED) { |
| 174 | intents.remove(intent.key()); |
| 175 | installables.remove(intent.key()); |
| 176 | intentStates.remove(intent.key()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | @Override |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 181 | public Set<Intent> getIntents() { |
| 182 | return Collections.unmodifiableSet(new HashSet<>(intents.values())); |
| 183 | } |
| 184 | |
| 185 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 186 | public long getIntentCount() { |
| 187 | return intents.size(); |
| 188 | } |
| 189 | |
| 190 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 191 | public Intent getIntent(Key intentKey) { |
| 192 | return intents.get(intentKey); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 196 | public IntentState getIntentState(Key intentKey) { |
| 197 | return intentStates.get(intentKey); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 201 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 202 | return installables.get(intentKey); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
Jonathan Hart | 13d3dd9 | 2015-02-25 16:09:42 -0800 | [diff] [blame] | 206 | public boolean isLocal(Key intentKey) { |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Iterable<Intent> getPending() { |
| 212 | return Collections.emptyList(); |
| 213 | } |
| 214 | |
| 215 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 216 | public void addListener(IntentListener listener) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 217 | listeners.add(listener); |
| 218 | } |
| 219 | |
| 220 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 221 | public void removeListener(IntentListener listener) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 222 | listeners.remove(listener); |
| 223 | } |
| 224 | |
| 225 | private void dispatch(IntentEvent event) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 226 | for (IntentListener listener : listeners) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 227 | listener.event(event); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 232 | public <T extends Intent> void registerCompiler(Class<T> cls, |
| 233 | IntentCompiler<T> compiler) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 234 | compilers.put(cls, compiler); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public <T extends Intent> void unregisterCompiler(Class<T> cls) { |
| 239 | compilers.remove(cls); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() { |
| 244 | return Collections.unmodifiableMap(compilers); |
| 245 | } |
| 246 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 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 | } |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 263 | } |