blob: 1cee01ad294ec5fad9c7c0f566a518d772265fcf [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
alshabiba7f7ca82014-09-22 11:41:23 -07006import java.util.Iterator;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07007import 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;
alshabiba7f7ca82014-09-22 11:41:23 -070020import org.onlab.onos.net.flow.DefaultFlowRule;
alshabib57044ba2014-09-16 15:58:01 -070021import org.onlab.onos.net.flow.FlowRule;
alshabiba7f7ca82014-09-22 11:41:23 -070022import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
alshabib57044ba2014-09-16 15:58:01 -070023import org.onlab.onos.net.flow.FlowRuleEvent;
24import org.onlab.onos.net.flow.FlowRuleListener;
25import org.onlab.onos.net.flow.FlowRuleProvider;
26import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
27import org.onlab.onos.net.flow.FlowRuleProviderService;
28import org.onlab.onos.net.flow.FlowRuleService;
tombe988312014-09-19 18:38:47 -070029import org.onlab.onos.net.flow.FlowRuleStore;
alshabib57044ba2014-09-16 15:58:01 -070030import org.onlab.onos.net.provider.AbstractProviderRegistry;
31import org.onlab.onos.net.provider.AbstractProviderService;
32import org.slf4j.Logger;
33
alshabiba7f7ca82014-09-22 11:41:23 -070034import com.google.common.collect.Lists;
35
alshabib57044ba2014-09-16 15:58:01 -070036@Component(immediate = true)
37@Service
tom202175a2014-09-19 19:00:11 -070038public class FlowRuleManager
alshabiba7f7ca82014-09-22 11:41:23 -070039extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
40implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070041
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070042 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070043 private final Logger log = getLogger(getClass());
44
45 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
alshabiba7f7ca82014-09-22 11:41:23 -070046 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -070047
tombe988312014-09-19 18:38:47 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070050
alshabib57044ba2014-09-16 15:58:01 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070052 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070053
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070055 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070056
57 @Activate
58 public void activate() {
59 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
60 log.info("Started");
61 }
62
63 @Deactivate
64 public void deactivate() {
65 eventDispatcher.removeSink(FlowRuleEvent.class);
66 log.info("Stopped");
67 }
68
69 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070070 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070071 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070072 }
73
74 @Override
alshabib219ebaa2014-09-22 15:41:24 -070075 public void applyFlowRules(FlowRule... flowRules) {
alshabib57044ba2014-09-16 15:58:01 -070076 for (int i = 0; i < flowRules.length; i++) {
alshabiba7f7ca82014-09-22 11:41:23 -070077 FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_ADD);
alshabib57044ba2014-09-16 15:58:01 -070078 final Device device = deviceService.getDevice(f.deviceId());
79 final FlowRuleProvider frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -070080 store.storeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070081 frp.applyFlowRule(f);
82 }
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++) {
alshabiba7f7ca82014-09-22 11:41:23 -070088 FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_REMOVE);
alshabib57044ba2014-09-16 15:58:01 -070089 final Device device = deviceService.getDevice(f.deviceId());
90 final FlowRuleProvider frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -070091 store.deleteFlowRule(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
alshabiba7f7ca82014-09-22 11:41:23 -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();
alshabib219ebaa2014-09-22 15:41:24 -0700137 log.info("Flow {} has not been installed.");
alshabib57044ba2014-09-16 15:58:01 -0700138
139 }
140
141 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700142 public void extraneousFlow(FlowRule flowRule) {
143 checkNotNull(flowRule, FLOW_RULE_NULL);
144 checkValidity();
145 log.info("Flow {} is on switch but not in store.");
146 }
147
148 @Override
alshabib57044ba2014-09-16 15:58:01 -0700149 public void flowAdded(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700150 checkNotNull(flowRule, FLOW_RULE_NULL);
151 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700152
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700153 FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule);
154 if (event == null) {
alshabib219ebaa2014-09-22 15:41:24 -0700155 log.debug("No flow store event generated.");
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700156 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700157 log.debug("Flow {} {}", flowRule, event.type());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700158 post(event);
159 }
alshabib219ebaa2014-09-22 15:41:24 -0700160
alshabib57044ba2014-09-16 15:58:01 -0700161 }
162
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700163 // Posts the specified event to the local event dispatcher.
164 private void post(FlowRuleEvent event) {
165 if (event != null) {
166 eventDispatcher.post(event);
167 }
168 }
alshabib5c370ff2014-09-18 10:12:14 -0700169
170 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700171 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) {
172 List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabib219ebaa2014-09-22 15:41:24 -0700173 //List<FlowRule> switchRules = Lists.newLinkedList(flowEntries);
174 Iterator<FlowRule> switchRulesIterator = flowEntries.iterator(); //switchRules.iterator();
alshabiba7f7ca82014-09-22 11:41:23 -0700175
176 while (switchRulesIterator.hasNext()) {
177 FlowRule rule = switchRulesIterator.next();
178 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700179 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700180 flowAdded(rule);
181 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700182 // the device has a rule the store does not have
183 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700184 }
185 }
186 for (FlowRule rule : storedRules) {
187 // there are rules in the store that aren't on the switch
188 flowMissing(rule);
189 }
alshabib5c370ff2014-09-18 10:12:14 -0700190 }
alshabib57044ba2014-09-16 15:58:01 -0700191 }
192
193}