blob: 7074a5c7479ac2f29a9642c034a779734de1f1ba [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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'Connorf3d06162014-10-02 15:54:12 -070016package org.onlab.onos.net.intent;
17
18import java.util.ArrayList;
19import java.util.Collections;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.ExecutorService;
26import java.util.concurrent.Executors;
27
28/**
Brian O'Connor66630c82014-10-02 21:08:19 -070029 * Fake implementation of the intent service to assist in developing tests of
30 * the interface contract.
Brian O'Connorf3d06162014-10-02 15:54:12 -070031 */
32public class FakeIntentManager implements TestableIntentService {
33
34 private final Map<IntentId, Intent> intents = new HashMap<>();
35 private final Map<IntentId, IntentState> intentStates = new HashMap<>();
Thomas Vachuskac96058a2014-10-20 23:00:16 -070036 private final Map<IntentId, List<Intent>> installables = new HashMap<>();
Brian O'Connor66630c82014-10-02 21:08:19 -070037 private final Set<IntentListener> listeners = new HashSet<>();
Brian O'Connorf3d06162014-10-02 15:54:12 -070038
39 private final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = new HashMap<>();
Thomas Vachuskac96058a2014-10-20 23:00:16 -070040 private final Map<Class<? extends Intent>, IntentInstaller<? extends Intent>> installers
Brian O'Connor66630c82014-10-02 21:08:19 -070041 = new HashMap<>();
Brian O'Connorf3d06162014-10-02 15:54:12 -070042
43 private final ExecutorService executor = Executors.newSingleThreadExecutor();
44 private final List<IntentException> exceptions = new ArrayList<>();
45
46 @Override
47 public List<IntentException> getExceptions() {
48 return exceptions;
49 }
50
51 // Provides an out-of-thread simulation of intent submit life-cycle
52 private void executeSubmit(final Intent intent) {
53 registerSubclassCompilerIfNeeded(intent);
54 executor.execute(new Runnable() {
55 @Override
56 public void run() {
57 try {
tom85258ee2014-10-07 00:10:02 -070058 executeCompilingPhase(intent);
Brian O'Connorf3d06162014-10-02 15:54:12 -070059 } catch (IntentException e) {
60 exceptions.add(e);
61 }
62 }
63 });
64 }
65
66 // Provides an out-of-thread simulation of intent withdraw life-cycle
67 private void executeWithdraw(final Intent intent) {
68 executor.execute(new Runnable() {
69 @Override
70 public void run() {
71 try {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070072 List<Intent> installable = getInstallable(intent.id());
tom85258ee2014-10-07 00:10:02 -070073 executeWithdrawingPhase(intent, installable);
Brian O'Connorf3d06162014-10-02 15:54:12 -070074 } catch (IntentException e) {
75 exceptions.add(e);
76 }
77
78 }
79 });
80 }
81
82 private <T extends Intent> IntentCompiler<T> getCompiler(T intent) {
83 @SuppressWarnings("unchecked")
84 IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass());
85 if (compiler == null) {
86 throw new IntentException("no compiler for class " + intent.getClass());
87 }
88 return compiler;
89 }
90
Thomas Vachuskac96058a2014-10-20 23:00:16 -070091 private <T extends Intent> IntentInstaller<T> getInstaller(T intent) {
Brian O'Connorf3d06162014-10-02 15:54:12 -070092 @SuppressWarnings("unchecked")
Brian O'Connor66630c82014-10-02 21:08:19 -070093 IntentInstaller<T> installer = (IntentInstaller<T>) installers.get(intent
94 .getClass());
Brian O'Connorf3d06162014-10-02 15:54:12 -070095 if (installer == null) {
96 throw new IntentException("no installer for class " + intent.getClass());
97 }
98 return installer;
99 }
100
tom85258ee2014-10-07 00:10:02 -0700101 private <T extends Intent> void executeCompilingPhase(T intent) {
102 setState(intent, IntentState.COMPILING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700103 try {
104 // For the fake, we compile using a single level pass
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700105 List<Intent> installable = new ArrayList<>();
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700106 for (Intent compiled : getCompiler(intent).compile(intent, null, null)) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700107 installable.add((Intent) compiled);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700108 }
tom85258ee2014-10-07 00:10:02 -0700109 executeInstallingPhase(intent, installable);
110
Brian O'Connorf3d06162014-10-02 15:54:12 -0700111 } catch (IntentException e) {
112 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700113 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700114 }
115 }
116
tom85258ee2014-10-07 00:10:02 -0700117 private void executeInstallingPhase(Intent intent,
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700118 List<Intent> installable) {
tom85258ee2014-10-07 00:10:02 -0700119 setState(intent, IntentState.INSTALLING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700120 try {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700121 for (Intent ii : installable) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700122 registerSubclassInstallerIfNeeded(ii);
123 getInstaller(ii).install(ii);
124 }
125 setState(intent, IntentState.INSTALLED);
tom85258ee2014-10-07 00:10:02 -0700126 putInstallable(intent.id(), installable);
127 dispatch(new IntentEvent(IntentEvent.Type.INSTALLED, intent));
128
Brian O'Connorf3d06162014-10-02 15:54:12 -0700129 } catch (IntentException e) {
130 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700131 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700132 }
133 }
134
tom85258ee2014-10-07 00:10:02 -0700135 private void executeWithdrawingPhase(Intent intent,
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700136 List<Intent> installable) {
tom85258ee2014-10-07 00:10:02 -0700137 setState(intent, IntentState.WITHDRAWING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700138 try {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700139 for (Intent ii : installable) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700140 getInstaller(ii).uninstall(ii);
141 }
tom85258ee2014-10-07 00:10:02 -0700142 removeInstallable(intent.id());
Brian O'Connorf3d06162014-10-02 15:54:12 -0700143 setState(intent, IntentState.WITHDRAWN);
tom85258ee2014-10-07 00:10:02 -0700144 dispatch(new IntentEvent(IntentEvent.Type.WITHDRAWN, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700145 } catch (IntentException e) {
tom53945d52014-10-07 11:01:36 -0700146 // FIXME: Rework this to always go from WITHDRAWING to WITHDRAWN!
Brian O'Connorf3d06162014-10-02 15:54:12 -0700147 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700148 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700149 }
150 }
151
Brian O'Connorf3d06162014-10-02 15:54:12 -0700152 // Sets the internal state for the given intent and dispatches an event
153 private void setState(Intent intent, IntentState state) {
tom85258ee2014-10-07 00:10:02 -0700154 intentStates.put(intent.id(), state);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700155 }
156
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700157 private void putInstallable(IntentId id, List<Intent> installable) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700158 installables.put(id, installable);
159 }
160
161 private void removeInstallable(IntentId id) {
162 installables.remove(id);
163 }
164
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700165 private List<Intent> getInstallable(IntentId id) {
166 List<Intent> installable = installables.get(id);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700167 if (installable != null) {
168 return installable;
169 } else {
170 return Collections.emptyList();
171 }
172 }
173
174 @Override
175 public void submit(Intent intent) {
tom85258ee2014-10-07 00:10:02 -0700176 intents.put(intent.id(), intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800177 setState(intent, IntentState.INSTALL_REQ);
178 dispatch(new IntentEvent(IntentEvent.Type.INSTALL_REQ, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700179 executeSubmit(intent);
180 }
181
182 @Override
183 public void withdraw(Intent intent) {
tom85258ee2014-10-07 00:10:02 -0700184 intents.remove(intent.id());
Brian O'Connorf3d06162014-10-02 15:54:12 -0700185 executeWithdraw(intent);
186 }
187
188 @Override
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700189 public void replace(IntentId oldIntentId, Intent newIntent) {
190 // TODO: implement later
191 }
192
193 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700194 public void execute(IntentOperations operations) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700195 // TODO: implement later
196 }
197
198 @Override
199 public Set<Intent> getIntents() {
200 return Collections.unmodifiableSet(new HashSet<>(intents.values()));
201 }
202
203 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700204 public long getIntentCount() {
205 return intents.size();
206 }
207
208 @Override
Brian O'Connorf3d06162014-10-02 15:54:12 -0700209 public Intent getIntent(IntentId id) {
210 return intents.get(id);
211 }
212
213 @Override
214 public IntentState getIntentState(IntentId id) {
215 return intentStates.get(id);
216 }
217
218 @Override
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700219 public List<Intent> getInstallableIntents(IntentId intentId) {
220 return installables.get(intentId);
221 }
222
223 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700224 public void addListener(IntentListener listener) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700225 listeners.add(listener);
226 }
227
228 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700229 public void removeListener(IntentListener listener) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700230 listeners.remove(listener);
231 }
232
233 private void dispatch(IntentEvent event) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700234 for (IntentListener listener : listeners) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700235 listener.event(event);
236 }
237 }
238
239 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700240 public <T extends Intent> void registerCompiler(Class<T> cls,
241 IntentCompiler<T> compiler) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700242 compilers.put(cls, compiler);
243 }
244
245 @Override
246 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
247 compilers.remove(cls);
248 }
249
250 @Override
251 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
252 return Collections.unmodifiableMap(compilers);
253 }
254
255 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700256 public <T extends Intent> void registerInstaller(Class<T> cls,
Brian O'Connor66630c82014-10-02 21:08:19 -0700257 IntentInstaller<T> installer) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700258 installers.put(cls, installer);
259 }
260
261 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700262 public <T extends Intent> void unregisterInstaller(Class<T> cls) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700263 installers.remove(cls);
264 }
265
266 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700267 public Map<Class<? extends Intent>,
268 IntentInstaller<? extends Intent>> getInstallers() {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700269 return Collections.unmodifiableMap(installers);
270 }
271
272 private void registerSubclassCompilerIfNeeded(Intent intent) {
273 if (!compilers.containsKey(intent.getClass())) {
274 Class<?> cls = intent.getClass();
275 while (cls != Object.class) {
276 // As long as we're within the Intent class descendants
277 if (Intent.class.isAssignableFrom(cls)) {
278 IntentCompiler<?> compiler = compilers.get(cls);
279 if (compiler != null) {
280 compilers.put(intent.getClass(), compiler);
281 return;
282 }
283 }
284 cls = cls.getSuperclass();
285 }
286 }
287 }
288
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700289 private void registerSubclassInstallerIfNeeded(Intent intent) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700290 if (!installers.containsKey(intent.getClass())) {
291 Class<?> cls = intent.getClass();
292 while (cls != Object.class) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700293 // As long as we're within the Intent class
Brian O'Connor66630c82014-10-02 21:08:19 -0700294 // descendants
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700295 if (Intent.class.isAssignableFrom(cls)) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700296 IntentInstaller<?> installer = installers.get(cls);
297 if (installer != null) {
298 installers.put(intent.getClass(), installer);
299 return;
300 }
301 }
302 cls = cls.getSuperclass();
303 }
304 }
305 }
306
307}