blob: ebaf6af1be80ec995e827e08adf83ab4722651f2 [file] [log] [blame]
Sho SHIMIZU15ed4fd2014-08-05 14:40:42 -07001package net.onrc.onos.api.newintent;
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/**
14 * Fake implementation of the intent service to assist in developing tests
15 * of the interface contract.
16 */
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<>();
22 private final Set<IntentEventListener> listeners = new HashSet<>();
23
24 private final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = new HashMap<>();
25 private final Map<Class<? extends InstallableIntent>,
26 IntentInstaller<? extends InstallableIntent>> installers = new HashMap<>();
27
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 registerSubclassInstallerIfNeeded((InstallableIntent) intent);
40 executor.execute(new Runnable() {
41 @Override
42 public void run() {
43 try {
44 List<InstallableIntent> installable = compileIntent(intent);
45 installIntents(intent, installable);
46 } catch (IntentException e) {
47 exceptions.add(e);
48 }
49 }
50 });
51 }
52
53 // Provides an out-of-thread simulation of intent withdraw life-cycle
54 private void executeWithdraw(final Intent intent) {
55 executor.execute(new Runnable() {
56 @Override
57 public void run() {
58 try {
59 List<InstallableIntent> installable = getInstallable(intent.getId());
60 uninstallIntents(intent, installable);
61 } catch (IntentException e) {
62 exceptions.add(e);
63 }
64
65 }
66 });
67 }
68
69 private <T extends Intent> IntentCompiler<T> getCompiler(T intent) {
70 @SuppressWarnings("unchecked")
71 IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass());
72 if (compiler == null) {
73 throw new IntentException("no compiler for class " + intent.getClass());
74 }
75 return compiler;
76 }
77
78 private <T extends InstallableIntent> IntentInstaller<T> getInstaller(T intent) {
79 @SuppressWarnings("unchecked")
80 IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass());
81 if (installer == null) {
82 throw new IntentException("no installer for class " + intent.getClass());
83 }
84 return installer;
85 }
86
87 private <T extends Intent> List<InstallableIntent> compileIntent(T intent) {
88 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 }
94 setState(intent, IntentState.COMPILED);
95 return installable;
96 } catch (IntentException e) {
97 setState(intent, IntentState.FAILED);
98 throw e;
99 }
100 }
101
102 private void installIntents(Intent intent, List<InstallableIntent> installable) {
103 try {
104 for (InstallableIntent ii : installable) {
105 getInstaller(ii).install(ii);
106 }
107 setState(intent, IntentState.INSTALLED);
108 putInstallable(intent.getId(), installable);
109 } catch (IntentException e) {
110 setState(intent, IntentState.FAILED);
111 throw e;
112 }
113 }
114
115 private void uninstallIntents(Intent intent, List<InstallableIntent> installable) {
116 try {
117 for (InstallableIntent ii : installable) {
118 getInstaller(ii).remove(ii);
119 }
120 setState(intent, IntentState.WITHDRAWN);
121 removeInstallable(intent.getId());
122 } catch (IntentException e) {
123 setState(intent, IntentState.FAILED);
124 throw e;
125 }
126 }
127
128
129 // Sets the internal state for the given intent and dispatches an event
130 private void setState(Intent intent, IntentState state) {
131 IntentState previous = intentStates.get(intent.getId());
132 intentStates.put(intent.getId(), state);
133 dispatch(new IntentEvent(intent, state, previous, System.currentTimeMillis()));
134 }
135
136 private void putInstallable(IntentId id, List<InstallableIntent> installable) {
137 installables.put(id, installable);
138 }
139
140 private void removeInstallable(IntentId id) {
141 installables.remove(id);
142 }
143
144 private List<InstallableIntent> getInstallable(IntentId id) {
145 List<InstallableIntent> installable = installables.get(id);
146 if (installable != null) {
147 return installable;
148 } else {
149 return Collections.emptyList();
150 }
151 }
152
153 @Override
154 public void submit(Intent intent) {
155 intents.put(intent.getId(), intent);
156 setState(intent, IntentState.SUBMITTED);
157 executeSubmit(intent);
158 }
159
160 @Override
161 public void withdraw(Intent intent) {
162 intents.remove(intent.getId());
163 setState(intent, IntentState.WITHDRAWING);
164 executeWithdraw(intent);
165 }
166
167 @Override
168 public void execute(IntentOperations operations) {
169 // TODO: implement later
170 }
171
172 @Override
173 @SuppressWarnings("unchecked")
174 public Set<Intent> getIntents() {
175 return Collections.unmodifiableSet(new HashSet<>(intents.values()));
176 }
177
178 @Override
179 public Intent getIntent(IntentId id) {
180 return intents.get(id);
181 }
182
183 @Override
184 public IntentState getIntentState(IntentId id) {
185 return intentStates.get(id);
186 }
187
188 @Override
189 public void addListener(IntentEventListener listener) {
190 listeners.add(listener);
191 }
192
193 @Override
194 public void removeListener(IntentEventListener listener) {
195 listeners.remove(listener);
196 }
197
198 private void dispatch(IntentEvent event) {
199 for (IntentEventListener listener : listeners) {
200 listener.event(event);
201 }
202 }
203
204 @Override
205 public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) {
206 compilers.put(cls, compiler);
207 }
208
209 @Override
210 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
211 compilers.remove(cls);
212 }
213
214 @Override
215 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
216 return Collections.unmodifiableMap(compilers);
217 }
218
219 @Override
220 public <T extends InstallableIntent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) {
221 installers.put(cls, installer);
222 }
223
224 @Override
225 public <T extends InstallableIntent> void unregisterInstaller(Class<T> cls) {
226 installers.remove(cls);
227 }
228
229 @Override
230 public Map<Class<? extends InstallableIntent>,
231 IntentInstaller<? extends InstallableIntent>> getInstallers() {
232 return Collections.unmodifiableMap(installers);
233 }
234
235 @SuppressWarnings("unchecked")
236 private void registerSubclassCompilerIfNeeded(Intent intent) {
237 if (!compilers.containsKey(intent.getClass())) {
238 Class<?> cls = intent.getClass();
239 while (cls != Object.class) {
240 // As long as we're within the Intent class descendants
241 if (Intent.class.isAssignableFrom(cls)) {
242 IntentCompiler<?> compiler = compilers.get(cls);
243 if (compiler != null) {
244 compilers.put(intent.getClass(), compiler);
245 return;
246 }
247 }
248 cls = cls.getSuperclass();
249 }
250 }
251 }
252
253 @SuppressWarnings("unchecked")
254 private void registerSubclassInstallerIfNeeded(InstallableIntent intent) {
255 if (!installers.containsKey(intent.getClass())) {
256 Class<?> cls = intent.getClass();
257 while (cls != Object.class) {
258 // As long as we're within the InstallableIntent class descendants
259 if (InstallableIntent.class.isAssignableFrom(cls)) {
260 IntentInstaller<?> installer = installers.get(cls);
261 if (installer != null) {
262 installers.put(intent.getClass(), installer);
263 return;
264 }
265 }
266 cls = cls.getSuperclass();
267 }
268 }
269 }
270
271}