blob: 58c5a9cb8dbd82591db99105e14fc374b46e53cb [file] [log] [blame]
Brian O'Connorf3d06162014-10-02 15:54:12 -07001package org.onlab.onos.net.intent;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Map;
9import java.util.Set;
10import java.util.concurrent.ExecutorService;
11import java.util.concurrent.Executors;
12
13/**
Brian O'Connor66630c82014-10-02 21:08:19 -070014 * Fake implementation of the intent service to assist in developing tests of
15 * the interface contract.
Brian O'Connorf3d06162014-10-02 15:54:12 -070016 */
17public 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'Connor66630c82014-10-02 21:08:19 -070022 private final Set<IntentListener> listeners = new HashSet<>();
Brian O'Connorf3d06162014-10-02 15:54:12 -070023
24 private final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = new HashMap<>();
Brian O'Connor66630c82014-10-02 21:08:19 -070025 private final Map<Class<? extends InstallableIntent>, IntentInstaller<? extends InstallableIntent>> installers
26 = new HashMap<>();
Brian O'Connorf3d06162014-10-02 15:54:12 -070027
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 {
tom85258ee2014-10-07 00:10:02 -070043 executeCompilingPhase(intent);
Brian O'Connorf3d06162014-10-02 15:54:12 -070044 } 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 {
tom85258ee2014-10-07 00:10:02 -070057 List<InstallableIntent> installable = getInstallable(intent.id());
58 executeWithdrawingPhase(intent, installable);
Brian O'Connorf3d06162014-10-02 15:54:12 -070059 } 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'Connor66630c82014-10-02 21:08:19 -070078 IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent
79 .getClass());
Brian O'Connorf3d06162014-10-02 15:54:12 -070080 if (installer == null) {
81 throw new IntentException("no installer for class " + intent.getClass());
82 }
83 return installer;
84 }
85
tom85258ee2014-10-07 00:10:02 -070086 private <T extends Intent> void executeCompilingPhase(T intent) {
87 setState(intent, IntentState.COMPILING);
Brian O'Connorf3d06162014-10-02 15:54:12 -070088 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 }
tom85258ee2014-10-07 00:10:02 -070094 executeInstallingPhase(intent, installable);
95
Brian O'Connorf3d06162014-10-02 15:54:12 -070096 } catch (IntentException e) {
97 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -070098 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -070099 }
100 }
101
tom85258ee2014-10-07 00:10:02 -0700102 private void executeInstallingPhase(Intent intent,
103 List<InstallableIntent> installable) {
104 setState(intent, IntentState.INSTALLING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700105 try {
106 for (InstallableIntent ii : installable) {
107 registerSubclassInstallerIfNeeded(ii);
108 getInstaller(ii).install(ii);
109 }
110 setState(intent, IntentState.INSTALLED);
tom85258ee2014-10-07 00:10:02 -0700111 putInstallable(intent.id(), installable);
112 dispatch(new IntentEvent(IntentEvent.Type.INSTALLED, intent));
113
Brian O'Connorf3d06162014-10-02 15:54:12 -0700114 } catch (IntentException e) {
115 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700116 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700117 }
118 }
119
tom85258ee2014-10-07 00:10:02 -0700120 private void executeWithdrawingPhase(Intent intent,
121 List<InstallableIntent> installable) {
122 setState(intent, IntentState.WITHDRAWING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700123 try {
124 for (InstallableIntent ii : installable) {
125 getInstaller(ii).uninstall(ii);
126 }
tom85258ee2014-10-07 00:10:02 -0700127 removeInstallable(intent.id());
Brian O'Connorf3d06162014-10-02 15:54:12 -0700128 setState(intent, IntentState.WITHDRAWN);
tom85258ee2014-10-07 00:10:02 -0700129 dispatch(new IntentEvent(IntentEvent.Type.WITHDRAWN, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700130 } catch (IntentException e) {
tom53945d52014-10-07 11:01:36 -0700131 // FIXME: Rework this to always go from WITHDRAWING to WITHDRAWN!
Brian O'Connorf3d06162014-10-02 15:54:12 -0700132 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700133 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700134 }
135 }
136
Brian O'Connorf3d06162014-10-02 15:54:12 -0700137 // Sets the internal state for the given intent and dispatches an event
138 private void setState(Intent intent, IntentState state) {
tom85258ee2014-10-07 00:10:02 -0700139 intentStates.put(intent.id(), state);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700140 }
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) {
tom85258ee2014-10-07 00:10:02 -0700161 intents.put(intent.id(), intent);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700162 setState(intent, IntentState.SUBMITTED);
tom85258ee2014-10-07 00:10:02 -0700163 dispatch(new IntentEvent(IntentEvent.Type.SUBMITTED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700164 executeSubmit(intent);
165 }
166
167 @Override
168 public void withdraw(Intent intent) {
tom85258ee2014-10-07 00:10:02 -0700169 intents.remove(intent.id());
Brian O'Connorf3d06162014-10-02 15:54:12 -0700170 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'Connor66630c82014-10-02 21:08:19 -0700184 public long getIntentCount() {
185 return intents.size();
186 }
187
188 @Override
Brian O'Connorf3d06162014-10-02 15:54:12 -0700189 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'Connor66630c82014-10-02 21:08:19 -0700199 public void addListener(IntentListener listener) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700200 listeners.add(listener);
201 }
202
203 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700204 public void removeListener(IntentListener listener) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700205 listeners.remove(listener);
206 }
207
208 private void dispatch(IntentEvent event) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700209 for (IntentListener listener : listeners) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700210 listener.event(event);
211 }
212 }
213
214 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700215 public <T extends Intent> void registerCompiler(Class<T> cls,
216 IntentCompiler<T> compiler) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700217 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'Connor66630c82014-10-02 21:08:19 -0700231 public <T extends InstallableIntent> void registerInstaller(Class<T> cls,
232 IntentInstaller<T> installer) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700233 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'Connor66630c82014-10-02 21:08:19 -0700243 IntentInstaller<? extends InstallableIntent>> getInstallers() {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700244 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'Connor66630c82014-10-02 21:08:19 -0700268 // As long as we're within the InstallableIntent class
269 // descendants
Brian O'Connorf3d06162014-10-02 15:54:12 -0700270 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}