blob: b3481c1a744eb457d04414482394dba4e53e700e [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
tome4729872014-09-23 00:37:37 -070036/**
37 * Provides implementation of the flow NB & SB APIs.
38 */
alshabib57044ba2014-09-16 15:58:01 -070039@Component(immediate = true)
40@Service
tom202175a2014-09-19 19:00:11 -070041public class FlowRuleManager
alshabiba7f7ca82014-09-22 11:41:23 -070042extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
43implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070044
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070046 private final Logger log = getLogger(getClass());
47
48 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
alshabiba7f7ca82014-09-22 11:41:23 -070049 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -070050
tombe988312014-09-19 18:38:47 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070053
alshabib57044ba2014-09-16 15:58:01 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070055 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070056
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070058 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070059
60 @Activate
61 public void activate() {
62 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
63 log.info("Started");
64 }
65
66 @Deactivate
67 public void deactivate() {
68 eventDispatcher.removeSink(FlowRuleEvent.class);
69 log.info("Stopped");
70 }
71
72 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070073 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070074 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070075 }
76
77 @Override
alshabib219ebaa2014-09-22 15:41:24 -070078 public void applyFlowRules(FlowRule... flowRules) {
alshabib57044ba2014-09-16 15:58:01 -070079 for (int i = 0; i < flowRules.length; i++) {
alshabiba7f7ca82014-09-22 11:41:23 -070080 FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_ADD);
alshabib57044ba2014-09-16 15:58:01 -070081 final Device device = deviceService.getDevice(f.deviceId());
82 final FlowRuleProvider frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -070083 store.storeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070084 frp.applyFlowRule(f);
85 }
alshabib57044ba2014-09-16 15:58:01 -070086 }
87
88 @Override
89 public void removeFlowRules(FlowRule... flowRules) {
alshabibbb8b1282014-09-22 17:00:18 -070090 FlowRule f;
alshabib57044ba2014-09-16 15:58:01 -070091 for (int i = 0; i < flowRules.length; i++) {
alshabibbb8b1282014-09-22 17:00:18 -070092 f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_REMOVE);
alshabib57044ba2014-09-16 15:58:01 -070093 final Device device = deviceService.getDevice(f.deviceId());
94 final FlowRuleProvider frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -070095 store.deleteFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070096 frp.removeFlowRule(f);
97 }
98
99 }
100
101 @Override
102 public void addListener(FlowRuleListener listener) {
103 listenerRegistry.addListener(listener);
104 }
105
106 @Override
107 public void removeListener(FlowRuleListener listener) {
108 listenerRegistry.removeListener(listener);
109 }
110
111 @Override
112 protected FlowRuleProviderService createProviderService(
113 FlowRuleProvider provider) {
114 return new InternalFlowRuleProviderService(provider);
115 }
116
117 private class InternalFlowRuleProviderService
alshabiba7f7ca82014-09-22 11:41:23 -0700118 extends AbstractProviderService<FlowRuleProvider>
119 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700120
121 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
122 super(provider);
123 }
124
125 @Override
126 public void flowRemoved(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700127 checkNotNull(flowRule, FLOW_RULE_NULL);
128 checkValidity();
129 FlowRuleEvent event = store.removeFlowRule(flowRule);
alshabib57044ba2014-09-16 15:58:01 -0700130
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700131 if (event != null) {
132 log.debug("Flow {} removed", flowRule);
133 post(event);
134 }
alshabib57044ba2014-09-16 15:58:01 -0700135 }
136
137 @Override
138 public void flowMissing(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700139 checkNotNull(flowRule, FLOW_RULE_NULL);
140 checkValidity();
alshabib54ce5892014-09-23 17:50:51 -0700141 log.debug("Flow {} has not been installed.", flowRule);
alshabib57044ba2014-09-16 15:58:01 -0700142
143 }
144
145 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700146 public void extraneousFlow(FlowRule flowRule) {
147 checkNotNull(flowRule, FLOW_RULE_NULL);
148 checkValidity();
alshabib54ce5892014-09-23 17:50:51 -0700149 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700150 }
151
152 @Override
alshabib57044ba2014-09-16 15:58:01 -0700153 public void flowAdded(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700154 checkNotNull(flowRule, FLOW_RULE_NULL);
155 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700156
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700157 FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule);
158 if (event == null) {
alshabib219ebaa2014-09-22 15:41:24 -0700159 log.debug("No flow store event generated.");
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700160 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700161 log.debug("Flow {} {}", flowRule, event.type());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700162 post(event);
163 }
alshabib219ebaa2014-09-22 15:41:24 -0700164
alshabib57044ba2014-09-16 15:58:01 -0700165 }
166
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700167 // Posts the specified event to the local event dispatcher.
168 private void post(FlowRuleEvent event) {
169 if (event != null) {
170 eventDispatcher.post(event);
171 }
172 }
alshabib5c370ff2014-09-18 10:12:14 -0700173
174 @Override
alshabiba7f7ca82014-09-22 11:41:23 -0700175 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) {
176 List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabibbb8b1282014-09-22 17:00:18 -0700177
178 Iterator<FlowRule> switchRulesIterator = flowEntries.iterator();
alshabiba7f7ca82014-09-22 11:41:23 -0700179
180 while (switchRulesIterator.hasNext()) {
181 FlowRule rule = switchRulesIterator.next();
182 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700183 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700184 flowAdded(rule);
185 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700186 // the device has a rule the store does not have
187 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700188 }
189 }
190 for (FlowRule rule : storedRules) {
alshabib54ce5892014-09-23 17:50:51 -0700191
alshabiba7f7ca82014-09-22 11:41:23 -0700192 // there are rules in the store that aren't on the switch
193 flowMissing(rule);
alshabib54ce5892014-09-23 17:50:51 -0700194
alshabiba7f7ca82014-09-22 11:41:23 -0700195 }
alshabib5c370ff2014-09-18 10:12:14 -0700196 }
alshabib57044ba2014-09-16 15:58:01 -0700197 }
198
199}