blob: 038a0d97d08871c8c4ff55dbbccddd8c61e86aa9 [file] [log] [blame]
alshabib57044ba2014-09-16 15:58:01 -07001package org.onlab.onos.net.trivial.flow.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07005import java.util.ArrayList;
6import java.util.List;
7
alshabib57044ba2014-09-16 15:58:01 -07008import org.apache.felix.scr.annotations.Activate;
9import org.apache.felix.scr.annotations.Component;
10import org.apache.felix.scr.annotations.Deactivate;
11import org.apache.felix.scr.annotations.Reference;
12import org.apache.felix.scr.annotations.ReferenceCardinality;
13import org.apache.felix.scr.annotations.Service;
14import org.onlab.onos.event.AbstractListenerRegistry;
15import org.onlab.onos.event.EventDeliveryService;
16import org.onlab.onos.net.Device;
17import org.onlab.onos.net.DeviceId;
18import org.onlab.onos.net.device.DeviceService;
19import org.onlab.onos.net.flow.FlowEntry;
20import org.onlab.onos.net.flow.FlowRule;
21import org.onlab.onos.net.flow.FlowRuleEvent;
22import org.onlab.onos.net.flow.FlowRuleListener;
23import org.onlab.onos.net.flow.FlowRuleProvider;
24import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
25import org.onlab.onos.net.flow.FlowRuleProviderService;
26import org.onlab.onos.net.flow.FlowRuleService;
27import org.onlab.onos.net.provider.AbstractProviderRegistry;
28import org.onlab.onos.net.provider.AbstractProviderService;
29import org.slf4j.Logger;
30
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070031import static com.google.common.base.Preconditions.checkNotNull;
32
alshabib57044ba2014-09-16 15:58:01 -070033@Component(immediate = true)
34@Service
35public class SimpleFlowRuleManager
36extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
37implements FlowRuleService, FlowRuleProviderRegistry {
38
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070039 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070040 private final Logger log = getLogger(getClass());
41
42 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
43 listenerRegistry = new AbstractListenerRegistry<>();
44
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045 private final SimpleFlowRuleStore store = new SimpleFlowRuleStore();
46
alshabib57044ba2014-09-16 15:58:01 -070047 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070048 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070049
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070051 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070052
53 @Activate
54 public void activate() {
55 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deactivate() {
61 eventDispatcher.removeSink(FlowRuleEvent.class);
62 log.info("Stopped");
63 }
64
65 @Override
66 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070067 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070068 }
69
70 @Override
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070071 public List<FlowEntry> applyFlowRules(FlowRule... flowRules) {
72 List<FlowEntry> entries = new ArrayList<FlowEntry>();
73
alshabib57044ba2014-09-16 15:58:01 -070074 for (int i = 0; i < flowRules.length; i++) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070075 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070076 final Device device = deviceService.getDevice(f.deviceId());
77 final FlowRuleProvider frp = getProvider(device.providerId());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070078 entries.add(store.storeFlowRule(f));
alshabib57044ba2014-09-16 15:58:01 -070079 frp.applyFlowRule(f);
80 }
81
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070082 return entries;
alshabib57044ba2014-09-16 15:58:01 -070083 }
84
85 @Override
86 public void removeFlowRules(FlowRule... flowRules) {
87 for (int i = 0; i < flowRules.length; i++) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070088 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070089 final Device device = deviceService.getDevice(f.deviceId());
90 final FlowRuleProvider frp = getProvider(device.providerId());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070091 store.removeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070092 frp.removeFlowRule(f);
93 }
94
95 }
96
97 @Override
98 public void addListener(FlowRuleListener listener) {
99 listenerRegistry.addListener(listener);
100 }
101
102 @Override
103 public void removeListener(FlowRuleListener listener) {
104 listenerRegistry.removeListener(listener);
105 }
106
107 @Override
108 protected FlowRuleProviderService createProviderService(
109 FlowRuleProvider provider) {
110 return new InternalFlowRuleProviderService(provider);
111 }
112
113 private class InternalFlowRuleProviderService
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700114 extends AbstractProviderService<FlowRuleProvider>
115 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700116
117 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
118 super(provider);
119 }
120
121 @Override
122 public void flowRemoved(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700123 checkNotNull(flowRule, FLOW_RULE_NULL);
124 checkValidity();
125 FlowRuleEvent event = store.removeFlowRule(flowRule);
alshabib57044ba2014-09-16 15:58:01 -0700126
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700127 if (event != null) {
128 log.debug("Flow {} removed", flowRule);
129 post(event);
130 }
alshabib57044ba2014-09-16 15:58:01 -0700131 }
132
133 @Override
134 public void flowMissing(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700135 checkNotNull(flowRule, FLOW_RULE_NULL);
136 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700137 // TODO Auto-generated method stub
138
139 }
140
141 @Override
142 public void flowAdded(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700143 checkNotNull(flowRule, FLOW_RULE_NULL);
144 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700145
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700146 FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule);
147 if (event == null) {
148 log.debug("Flow {} updated", flowRule);
149 } else {
150 log.debug("Flow {} added", flowRule);
151 post(event);
152 }
alshabib57044ba2014-09-16 15:58:01 -0700153 }
154
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700155 // Posts the specified event to the local event dispatcher.
156 private void post(FlowRuleEvent event) {
157 if (event != null) {
158 eventDispatcher.post(event);
159 }
160 }
alshabib57044ba2014-09-16 15:58:01 -0700161 }
162
163}