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