blob: 30238c43cf045b94cb340b2e5866e576f608192d [file] [log] [blame]
alshabib57044ba2014-09-16 15:58:01 -07001package org.onlab.onos.net.trivial.flow.impl;
2
alshabib5c370ff2014-09-18 10:12:14 -07003import static com.google.common.base.Preconditions.checkNotNull;
alshabib57044ba2014-09-16 15:58:01 -07004import static org.slf4j.LoggerFactory.getLogger;
5
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07006import java.util.ArrayList;
7import java.util.List;
8
alshabib57044ba2014-09-16 15:58:01 -07009import org.apache.felix.scr.annotations.Activate;
10import org.apache.felix.scr.annotations.Component;
11import org.apache.felix.scr.annotations.Deactivate;
12import org.apache.felix.scr.annotations.Reference;
13import org.apache.felix.scr.annotations.ReferenceCardinality;
14import org.apache.felix.scr.annotations.Service;
15import org.onlab.onos.event.AbstractListenerRegistry;
16import org.onlab.onos.event.EventDeliveryService;
17import org.onlab.onos.net.Device;
18import org.onlab.onos.net.DeviceId;
19import org.onlab.onos.net.device.DeviceService;
20import org.onlab.onos.net.flow.FlowEntry;
21import org.onlab.onos.net.flow.FlowRule;
22import org.onlab.onos.net.flow.FlowRuleEvent;
23import org.onlab.onos.net.flow.FlowRuleListener;
24import org.onlab.onos.net.flow.FlowRuleProvider;
25import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
26import org.onlab.onos.net.flow.FlowRuleProviderService;
27import org.onlab.onos.net.flow.FlowRuleService;
28import org.onlab.onos.net.provider.AbstractProviderRegistry;
29import org.onlab.onos.net.provider.AbstractProviderService;
30import org.slf4j.Logger;
31
32@Component(immediate = true)
33@Service
34public class SimpleFlowRuleManager
35extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
36implements FlowRuleService, FlowRuleProviderRegistry {
37
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070038 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070039 private final Logger log = getLogger(getClass());
40
41 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
42 listenerRegistry = new AbstractListenerRegistry<>();
43
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070044 private final SimpleFlowRuleStore store = new SimpleFlowRuleStore();
45
alshabib57044ba2014-09-16 15:58:01 -070046 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 private EventDeliveryService eventDispatcher;
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 private DeviceService deviceService;
51
52 @Activate
53 public void activate() {
54 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
55 log.info("Started");
56 }
57
58 @Deactivate
59 public void deactivate() {
60 eventDispatcher.removeSink(FlowRuleEvent.class);
61 log.info("Stopped");
62 }
63
64 @Override
65 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070066 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070067 }
68
69 @Override
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070070 public List<FlowEntry> applyFlowRules(FlowRule... flowRules) {
71 List<FlowEntry> entries = new ArrayList<FlowEntry>();
72
alshabib57044ba2014-09-16 15:58:01 -070073 for (int i = 0; i < flowRules.length; i++) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070074 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070075 final Device device = deviceService.getDevice(f.deviceId());
76 final FlowRuleProvider frp = getProvider(device.providerId());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070077 entries.add(store.storeFlowRule(f));
alshabib57044ba2014-09-16 15:58:01 -070078 frp.applyFlowRule(f);
79 }
80
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070081 return entries;
alshabib57044ba2014-09-16 15:58:01 -070082 }
83
84 @Override
85 public void removeFlowRules(FlowRule... flowRules) {
86 for (int i = 0; i < flowRules.length; i++) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070087 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070088 final Device device = deviceService.getDevice(f.deviceId());
89 final FlowRuleProvider frp = getProvider(device.providerId());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070090 store.removeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070091 frp.removeFlowRule(f);
92 }
93
94 }
95
96 @Override
97 public void addListener(FlowRuleListener listener) {
98 listenerRegistry.addListener(listener);
99 }
100
101 @Override
102 public void removeListener(FlowRuleListener listener) {
103 listenerRegistry.removeListener(listener);
104 }
105
106 @Override
107 protected FlowRuleProviderService createProviderService(
108 FlowRuleProvider provider) {
109 return new InternalFlowRuleProviderService(provider);
110 }
111
112 private class InternalFlowRuleProviderService
alshabib5c370ff2014-09-18 10:12:14 -0700113 extends AbstractProviderService<FlowRuleProvider>
114 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700115
116 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
117 super(provider);
118 }
119
120 @Override
121 public void flowRemoved(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700122 checkNotNull(flowRule, FLOW_RULE_NULL);
123 checkValidity();
124 FlowRuleEvent event = store.removeFlowRule(flowRule);
alshabib57044ba2014-09-16 15:58:01 -0700125
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700126 if (event != null) {
127 log.debug("Flow {} removed", flowRule);
128 post(event);
129 }
alshabib57044ba2014-09-16 15:58:01 -0700130 }
131
132 @Override
133 public void flowMissing(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700134 checkNotNull(flowRule, FLOW_RULE_NULL);
135 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700136 // TODO Auto-generated method stub
137
138 }
139
140 @Override
141 public void flowAdded(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700142 checkNotNull(flowRule, FLOW_RULE_NULL);
143 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700144
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700145 FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule);
146 if (event == null) {
147 log.debug("Flow {} updated", flowRule);
148 } else {
149 log.debug("Flow {} added", flowRule);
150 post(event);
151 }
alshabib57044ba2014-09-16 15:58:01 -0700152 }
153
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700154 // Posts the specified event to the local event dispatcher.
155 private void post(FlowRuleEvent event) {
156 if (event != null) {
157 eventDispatcher.post(event);
158 }
159 }
alshabib5c370ff2014-09-18 10:12:14 -0700160
161 @Override
162 public void pushFlowMetrics(Iterable<FlowEntry> flowEntries) {
163 // TODO Auto-generated method stub
164
165 }
alshabib57044ba2014-09-16 15:58:01 -0700166 }
167
168}