blob: 50e68ce67bb8f80312fbc23a32500fba8e9cc42f [file] [log] [blame]
Sho SHIMIZU913969b2014-08-18 15:04:21 -07001package net.onrc.onos.api.flowmanager;
2
3import com.google.common.collect.ImmutableList;
4import net.onrc.onos.core.flowmanager.FlowBatchHandleImpl;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -07005import net.onrc.onos.core.util.IdGenerator;
Sho SHIMIZU913969b2014-08-18 15:04:21 -07006
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.List;
12import java.util.concurrent.ExecutorService;
13import java.util.concurrent.Executors;
14
15/**
16 * Fake implementation of {@link FlowManagerService} for testing.
17 */
18public class FakeFlowManagerService implements FlowManagerService {
19 private final List<FlowManagerListener> listeners = new ArrayList<>();
20 private final ExecutorService executor = Executors.newSingleThreadExecutor();
21
22 private final FlowId target;
23 private final List<FlowState> transition;
24 private final boolean returnNull;
25
26 public FakeFlowManagerService(FlowId target, boolean returnNull, FlowState... transition) {
27 this.target = target;
28 this.transition = ImmutableList.copyOf(transition);
29 this.returnNull = returnNull;
30 }
31
32 public FlowBatchHandle addFlow(Flow flow) {
33 return processFlow();
34 }
35
36 @Override
37 public FlowBatchHandle removeFlow(FlowId id) {
38 return processFlow();
39 }
40
41 private FlowBatchHandle processFlow() {
42 if (returnNull) {
43 return null;
44 }
45
46 executor.execute(new Runnable() {
47 @Override
48 public void run() {
49 changeStates();
50 }
51 });
52
53 // This is a test-only workaround. Passing null to the constructor is harmful,
54 // but we could not create a FlowOperationMap instance due to visibility of
55 // the constructor.
56 // TODO: consider correct visibility of the constructor and package structure
57 return new FlowBatchHandleImpl(null, new FlowBatchId(1));
58 }
59
60 private void changeStates() {
61 for (int i = 0; i < transition.size(); i++) {
62 FlowStateChange change;
63 if (i == 0) {
64 change = new FlowStateChange(target,
65 transition.get(i), null);
66 } else {
67 change = new FlowStateChange(target,
68 transition.get(i), transition.get(i - 1));
69 }
70 HashSet<FlowStateChange> changes = new HashSet<>(Arrays.asList(change));
71 invokeListeners(new FlowStatesChangedEvent(System.currentTimeMillis(), changes));
72 }
73 }
74
75 @Override
76 public Flow getFlow(FlowId id) {
77 throw new UnsupportedOperationException();
78 }
79
80 @Override
81 public Collection<Flow> getFlows() {
82 throw new UnsupportedOperationException();
83 }
84
85 @Override
86 public FlowBatchHandle executeBatch(FlowBatchOperation ops) {
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070091 public IdGenerator<FlowId> getFlowIdGenerator() {
Sho SHIMIZU913969b2014-08-18 15:04:21 -070092 throw new UnsupportedOperationException();
93 }
94
95 @Override
96 public void setConflictDetectionPolicy(ConflictDetectionPolicy policy) {
97 throw new UnsupportedOperationException();
98 }
99
100 @Override
101 public ConflictDetectionPolicy getConflictDetectionPolicy() {
102 throw new UnsupportedOperationException();
103 }
104
105 @Override
106 public void addListener(FlowManagerListener listener) {
107 listeners.add(listener);
108 }
109
110 @Override
111 public void removeListener(FlowManagerListener listener) {
112 listeners.remove(listener);
113 }
114
115 private void invokeListeners(FlowStatesChangedEvent event) {
116 for (FlowManagerListener listener: listeners) {
117 listener.flowStatesChanged(event);
118 }
119 }
120}