blob: a31e986ca8ca84129951b4b90918cc44ccc2ef93 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorf3d06162014-10-02 15:54:12 -070017
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
Ray Milkey4f5d93e2015-02-10 17:04:10 -080034 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'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<>();
Brian O'Connorf3d06162014-10-02 15:54:12 -070040
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);
52 executor.execute(new Runnable() {
53 @Override
54 public void run() {
55 try {
tom85258ee2014-10-07 00:10:02 -070056 executeCompilingPhase(intent);
Brian O'Connorf3d06162014-10-02 15:54:12 -070057 } catch (IntentException e) {
58 exceptions.add(e);
59 }
60 }
61 });
62 }
63
64 // Provides an out-of-thread simulation of intent withdraw life-cycle
65 private void executeWithdraw(final Intent intent) {
66 executor.execute(new Runnable() {
67 @Override
68 public void run() {
69 try {
Ray Milkey4f5d93e2015-02-10 17:04:10 -080070 List<Intent> installable = getInstallable(intent.key());
tom85258ee2014-10-07 00:10:02 -070071 executeWithdrawingPhase(intent, installable);
Brian O'Connorf3d06162014-10-02 15:54:12 -070072 } catch (IntentException e) {
73 exceptions.add(e);
74 }
75
76 }
77 });
78 }
79
80 private <T extends Intent> IntentCompiler<T> getCompiler(T intent) {
81 @SuppressWarnings("unchecked")
82 IntentCompiler<T> compiler = (IntentCompiler<T>) compilers.get(intent.getClass());
83 if (compiler == null) {
84 throw new IntentException("no compiler for class " + intent.getClass());
85 }
86 return compiler;
87 }
88
tom85258ee2014-10-07 00:10:02 -070089 private <T extends Intent> void executeCompilingPhase(T intent) {
90 setState(intent, IntentState.COMPILING);
Brian O'Connorf3d06162014-10-02 15:54:12 -070091 try {
92 // For the fake, we compile using a single level pass
Thomas Vachuskac96058a2014-10-20 23:00:16 -070093 List<Intent> installable = new ArrayList<>();
Brian O'Connorfa81eae2014-10-30 13:20:05 -070094 for (Intent compiled : getCompiler(intent).compile(intent, null, null)) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070095 installable.add((Intent) compiled);
Brian O'Connorf3d06162014-10-02 15:54:12 -070096 }
tom85258ee2014-10-07 00:10:02 -070097 executeInstallingPhase(intent, installable);
98
Brian O'Connorf3d06162014-10-02 15:54:12 -070099 } catch (IntentException e) {
100 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700101 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700102 }
103 }
104
tom85258ee2014-10-07 00:10:02 -0700105 private void executeInstallingPhase(Intent intent,
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700106 List<Intent> installable) {
tom85258ee2014-10-07 00:10:02 -0700107 setState(intent, IntentState.INSTALLING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700108 try {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700109 setState(intent, IntentState.INSTALLED);
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800110 putInstallable(intent.key(), installable);
tom85258ee2014-10-07 00:10:02 -0700111 dispatch(new IntentEvent(IntentEvent.Type.INSTALLED, intent));
112
Brian O'Connorf3d06162014-10-02 15:54:12 -0700113 } catch (IntentException e) {
114 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700115 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700116 }
117 }
118
tom85258ee2014-10-07 00:10:02 -0700119 private void executeWithdrawingPhase(Intent intent,
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700120 List<Intent> installable) {
tom85258ee2014-10-07 00:10:02 -0700121 setState(intent, IntentState.WITHDRAWING);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700122 try {
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800123 removeInstallable(intent.key());
Brian O'Connorf3d06162014-10-02 15:54:12 -0700124 setState(intent, IntentState.WITHDRAWN);
tom85258ee2014-10-07 00:10:02 -0700125 dispatch(new IntentEvent(IntentEvent.Type.WITHDRAWN, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700126 } catch (IntentException e) {
tom53945d52014-10-07 11:01:36 -0700127 // FIXME: Rework this to always go from WITHDRAWING to WITHDRAWN!
Brian O'Connorf3d06162014-10-02 15:54:12 -0700128 setState(intent, IntentState.FAILED);
tom85258ee2014-10-07 00:10:02 -0700129 dispatch(new IntentEvent(IntentEvent.Type.FAILED, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700130 }
131 }
132
Brian O'Connorf3d06162014-10-02 15:54:12 -0700133 // Sets the internal state for the given intent and dispatches an event
134 private void setState(Intent intent, IntentState state) {
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800135 intentStates.put(intent.key(), state);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700136 }
137
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800138 private void putInstallable(Key key, List<Intent> installable) {
139 installables.put(key, installable);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700140 }
141
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800142 private void removeInstallable(Key key) {
143 installables.remove(key);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700144 }
145
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800146 private List<Intent> getInstallable(Key key) {
147 List<Intent> installable = installables.get(key);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700148 if (installable != null) {
149 return installable;
150 } else {
151 return Collections.emptyList();
152 }
153 }
154
155 @Override
156 public void submit(Intent intent) {
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800157 intents.put(intent.key(), intent);
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800158 setState(intent, IntentState.INSTALL_REQ);
159 dispatch(new IntentEvent(IntentEvent.Type.INSTALL_REQ, intent));
Brian O'Connorf3d06162014-10-02 15:54:12 -0700160 executeSubmit(intent);
161 }
162
163 @Override
164 public void withdraw(Intent intent) {
Ray Milkey4f5d93e2015-02-10 17:04:10 -0800165 intents.remove(intent.key());
Brian O'Connorf3d06162014-10-02 15:54:12 -0700166 executeWithdraw(intent);
167 }
168
169 @Override
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700170 public void purge(Intent intent) {
171 IntentState currentState = intentStates.get(intent.key());
172 if (currentState == IntentState.WITHDRAWN
173 || currentState == IntentState.FAILED) {
174 intents.remove(intent.key());
175 installables.remove(intent.key());
176 intentStates.remove(intent.key());
177 }
178 }
179
180 @Override
Brian O'Connorf3d06162014-10-02 15:54:12 -0700181 public Set<Intent> getIntents() {
182 return Collections.unmodifiableSet(new HashSet<>(intents.values()));
183 }
184
185 @Override
Thomas Vachuskac46af202015-06-03 16:43:27 -0700186 public Iterable<IntentData> getIntentData() {
187 throw new UnsupportedOperationException();
188 }
189
190 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700191 public long getIntentCount() {
192 return intents.size();
193 }
194
195 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800196 public Intent getIntent(Key intentKey) {
197 return intents.get(intentKey);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700198 }
199
200 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800201 public IntentState getIntentState(Key intentKey) {
202 return intentStates.get(intentKey);
Brian O'Connorf3d06162014-10-02 15:54:12 -0700203 }
204
205 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800206 public List<Intent> getInstallableIntents(Key intentKey) {
207 return installables.get(intentKey);
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700208 }
209
210 @Override
Jonathan Hart13d3dd92015-02-25 16:09:42 -0800211 public boolean isLocal(Key intentKey) {
212 return true;
213 }
214
215 @Override
216 public Iterable<Intent> getPending() {
217 return Collections.emptyList();
218 }
219
220 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700221 public void addListener(IntentListener listener) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700222 listeners.add(listener);
223 }
224
225 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700226 public void removeListener(IntentListener listener) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700227 listeners.remove(listener);
228 }
229
230 private void dispatch(IntentEvent event) {
Brian O'Connor66630c82014-10-02 21:08:19 -0700231 for (IntentListener listener : listeners) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700232 listener.event(event);
233 }
234 }
235
236 @Override
Brian O'Connor66630c82014-10-02 21:08:19 -0700237 public <T extends Intent> void registerCompiler(Class<T> cls,
238 IntentCompiler<T> compiler) {
Brian O'Connorf3d06162014-10-02 15:54:12 -0700239 compilers.put(cls, compiler);
240 }
241
242 @Override
243 public <T extends Intent> void unregisterCompiler(Class<T> cls) {
244 compilers.remove(cls);
245 }
246
247 @Override
248 public Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> getCompilers() {
249 return Collections.unmodifiableMap(compilers);
250 }
251
Brian O'Connorf3d06162014-10-02 15:54:12 -0700252 private void registerSubclassCompilerIfNeeded(Intent intent) {
253 if (!compilers.containsKey(intent.getClass())) {
254 Class<?> cls = intent.getClass();
255 while (cls != Object.class) {
256 // As long as we're within the Intent class descendants
257 if (Intent.class.isAssignableFrom(cls)) {
258 IntentCompiler<?> compiler = compilers.get(cls);
259 if (compiler != null) {
260 compilers.put(intent.getClass(), compiler);
261 return;
262 }
263 }
264 cls = cls.getSuperclass();
265 }
266 }
267 }
Brian O'Connorf3d06162014-10-02 15:54:12 -0700268}