blob: 4a1cd3b4df973a787b0727ca7d70df6226fdfb3b [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.flow.impl;
alshabib57044ba2014-09-16 15:58:01 -07002
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;
alshabib57044ba2014-09-16 15:58:01 -070020import 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;
tombe988312014-09-19 18:38:47 -070027import org.onlab.onos.net.flow.FlowRuleStore;
alshabib57044ba2014-09-16 15:58:01 -070028import 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
tombe988312014-09-19 18:38:47 -070035 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
36 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070037
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>
tombe988312014-09-19 18:38:47 -070042 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -070043
tombe988312014-09-19 18:38:47 -070044 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070046
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
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070066 public Iterable<FlowRule> 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 Koshibed4e53e12014-09-18 14:24:55 -070071 public List<FlowRule> applyFlowRules(FlowRule... flowRules) {
72 List<FlowRule> entries = new ArrayList<FlowRule>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070073
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
tombe988312014-09-19 18:38:47 -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 }
alshabib5c370ff2014-09-18 10:12:14 -0700161
162 @Override
alshabib97044902014-09-18 14:52:16 -0700163 public void pushFlowMetrics(Iterable<FlowRule> flowEntries) {
alshabib5c370ff2014-09-18 10:12:14 -0700164 // TODO Auto-generated method stub
165
166 }
alshabib57044ba2014-09-16 15:58:01 -0700167 }
168
169}