blob: 808106a6551347e5de51c46c351864faa9077eeb [file] [log] [blame]
Toshio Koided6dbb952014-08-22 14:29:04 -07001package net.onrc.onos.core.flowmanager;
2
3import java.util.EventListener;
4import java.util.concurrent.CopyOnWriteArraySet;
5
6import net.onrc.onos.api.flowmanager.Flow;
7import net.onrc.onos.api.flowmanager.FlowBatchHandle;
8import net.onrc.onos.api.flowmanager.FlowBatchId;
9import net.onrc.onos.api.flowmanager.FlowBatchOperation;
10import net.onrc.onos.api.flowmanager.FlowBatchState;
11import net.onrc.onos.api.flowmanager.FlowBatchStateChangedEvent;
12import net.onrc.onos.api.flowmanager.FlowId;
13import net.onrc.onos.api.flowmanager.FlowManagerListener;
14import net.onrc.onos.api.flowmanager.FlowState;
15import net.onrc.onos.api.flowmanager.FlowStateChange;
16import net.onrc.onos.api.flowmanager.FlowStatesChangedEvent;
17import net.onrc.onos.core.matchaction.MatchActionService;
18
19import com.google.common.collect.Sets;
20
21/**
22 * Manages flow manager related events.
23 */
24public class FlowEventDispatcher implements
25 EventListener, FlowMapEventListener, FlowBatchMapEventListener {
26 private final FlowMap flowMap;
27 private final FlowBatchMap flowBatchMap;
28 private final MatchActionService maService;
29 private CopyOnWriteArraySet<FlowManagerListener> listeners;
30
31 /**
32 * Creates an instance using {@link FlowMap}, {@link FlowBatchMap} and
33 * {@link MatchActionService}.
34 *
35 * @param flowMap the {@link FlowMap} object
36 * @param flowBatchMap the {@link FlowBatchMap} object
37 * @param maService the {@link MatchActionService} object
38 */
39 FlowEventDispatcher(FlowMap flowMap, FlowBatchMap flowBatchMap,
40 MatchActionService maService) {
41 this.flowMap = flowMap;
42 this.flowBatchMap = flowBatchMap;
43 this.maService = maService;
44 this.listeners = new CopyOnWriteArraySet<>();
45 }
46
47 /**
48 * Adds the event listener.
49 *
50 * @param listener the listener to be added
51 */
52 void addListener(FlowManagerListener listener) {
53 listeners.add(listener);
54 }
55
56 /**
57 * Removes the event listener.
58 *
59 * @param listener the listener to be removed
60 */
61 void removeListener(FlowManagerListener listener) {
62 listeners.remove(listener);
63 }
64
65 /**
66 * Starts listening flow manager related events.
67 */
68 void start() {
69 maService.addEventListener(this);
70 flowMap.addListener(this);
71 flowBatchMap.addListener(this);
72 }
73
74 /**
75 * Stops listening flow manager related events.
76 */
77 void stop() {
78 maService.removeEventListener(this);
79 flowMap.removeListener(this);
80 flowBatchMap.removeListener(this);
81 }
82
83 @Override
84 public void flowAdded(FlowId id, Flow flow) {
85 // do nothing. (This event is not used for now)
86 }
87
88 @Override
89 public void flowRemoved(FlowId id) {
90 // do nothing. (This event is not used for now)
91 }
92
93 @Override
94 public void flowStateChanged(FlowId id, FlowState oldState, FlowState currentState) {
95 FlowStateChange stateChange = new FlowStateChange(id, currentState, oldState);
96 long time = System.currentTimeMillis();
97 FlowStatesChangedEvent event =
98 new FlowStatesChangedEvent(time, Sets.newHashSet(stateChange));
99 for (FlowManagerListener e : listeners) {
100 e.flowStatesChanged(event);
101 }
102 }
103
104 @Override
105 public void flowBatchOperationAdded(FlowBatchId id, FlowBatchOperation flowOp) {
106 // do nothing. (This event is not used for now)
107 }
108
109 @Override
110 public void flowBatchOperationRemoved(FlowBatchId id) {
111 // do nothing. (This event is not used for now)
112 }
113
114 @Override
115 public void flowBatchOperationStateChanged(FlowBatchId id, FlowBatchState oldState,
116 FlowBatchState currentState) {
117 FlowBatchHandle handle = new FlowBatchHandleImpl(flowBatchMap, id);
118 long time = System.currentTimeMillis();
119 FlowBatchStateChangedEvent event = new FlowBatchStateChangedEvent(time, handle,
120 currentState, currentState);
121
122 for (FlowManagerListener e : listeners) {
123 e.flowBatchStateChanged(event);
124 }
125 }
126}