blob: df46ec50f9ec172f2d560a9cb039a8efe86edc77 [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/**
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 executor.execute(new Runnable() {
40 @Override
41 public void run() {
42 try {
43 List<InstallableIntent> installable = compileIntent(intent);
44 installIntents(intent, installable);
45 } catch (IntentException e) {
46 exceptions.add(e);
47 }
48 }
49 });
50 }
51
52 // Provides an out-of-thread simulation of intent withdraw life-cycle
53 private void executeWithdraw(final Intent intent) {
54 executor.execute(new Runnable() {
55 @Override
56 public void run() {
57 try {
58 List<InstallableIntent> installable = getInstallable(intent.getId());
59 uninstallIntents(intent, installable);
60 } catch (IntentException e) {
61 exceptions.add(e);
62 }
63
64 }
65 });
66 }
67
68 private <T extends Intent> IntentCompiler<T> getCompiler(T intent) {
69 @SuppressWarnings("unchecked")
70 IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass());
71 if (compiler == null) {
72 throw new IntentException("no compiler for class " + intent.getClass());
73 }
74 return compiler;
75 }
76
77 private <T extends InstallableIntent> IntentInstaller<T> getInstaller(T intent) {
78 @SuppressWarnings("unchecked")
79 IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent.getClass());
80 if (installer == null) {
81 throw new IntentException("no installer for class " + intent.getClass());
82 }
83 return installer;
84 }
85
86 private <T extends Intent> List<InstallableIntent> compileIntent(T intent) {
87 try {
88 // For the fake, we compile using a single level pass
89 List<InstallableIntent> installable = new ArrayList<>();
90 for (Intent compiled : getCompiler(intent).compile(intent)) {
91 installable.add((InstallableIntent) compiled);
92 }
93 setState(intent, IntentState.COMPILED);
94 return installable;
95 } catch (IntentException e) {
96 setState(intent, IntentState.FAILED);
97 throw e;
98 }
99 }
100
101 private void installIntents(Intent intent, List<InstallableIntent> installable) {
102 try {
103 for (InstallableIntent ii : installable) {
104 registerSubclassInstallerIfNeeded(ii);
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).uninstall(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 public Set<Intent> getIntents() {
174 return Collections.unmodifiableSet(new HashSet<>(intents.values()));
175 }
176
177 @Override
178 public Intent getIntent(IntentId id) {
179 return intents.get(id);
180 }
181
182 @Override
183 public IntentState getIntentState(IntentId id) {
184 return intentStates.get(id);
185 }
186
187 @Override
188 public void addListener(IntentEventListener listener) {
189 listeners.add(listener);
190 }
191
192 @Override
193 public void removeListener(IntentEventListener listener) {
194 listeners.remove(listener);
195 }
196
197 private void dispatch(IntentEvent event) {
198 for (IntentEventListener listener : listeners) {
199 listener.event(event);
200 }
201 }
202
203 @Override
204 public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) {
205 compilers.put(cls, compiler);
206 }
207
208 @Override
209 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
210 compilers.remove(cls);
211 }
212
213 @Override
214 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
215 return Collections.unmodifiableMap(compilers);
216 }
217
218 @Override
219 public <T extends InstallableIntent> void registerInstaller(Class<T> cls, IntentInstaller<T> installer) {
220 installers.put(cls, installer);
221 }
222
223 @Override
224 public <T extends InstallableIntent> void unregisterInstaller(Class<T> cls) {
225 installers.remove(cls);
226 }
227
228 @Override
229 public Map<Class<? extends InstallableIntent>,
230 IntentInstaller<? extends InstallableIntent>> getInstallers() {
231 return Collections.unmodifiableMap(installers);
232 }
233
234 private void registerSubclassCompilerIfNeeded(Intent intent) {
235 if (!compilers.containsKey(intent.getClass())) {
236 Class<?> cls = intent.getClass();
237 while (cls != Object.class) {
238 // As long as we're within the Intent class descendants
239 if (Intent.class.isAssignableFrom(cls)) {
240 IntentCompiler<?> compiler = compilers.get(cls);
241 if (compiler != null) {
242 compilers.put(intent.getClass(), compiler);
243 return;
244 }
245 }
246 cls = cls.getSuperclass();
247 }
248 }
249 }
250
251 private void registerSubclassInstallerIfNeeded(InstallableIntent intent) {
252 if (!installers.containsKey(intent.getClass())) {
253 Class<?> cls = intent.getClass();
254 while (cls != Object.class) {
255 // As long as we're within the InstallableIntent class descendants
256 if (InstallableIntent.class.isAssignableFrom(cls)) {
257 IntentInstaller<?> installer = installers.get(cls);
258 if (installer != null) {
259 installers.put(intent.getClass(), installer);
260 return;
261 }
262 }
263 cls = cls.getSuperclass();
264 }
265 }
266 }
267
268}