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