blob: 27a86a32676e19f1a95564b810d580e6e458b9b2 [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.flow.impl;
alshabib57044ba2014-09-16 15:58:01 -07002
alshabibbb42cad2014-09-25 11:43:05 -07003import static com.google.common.base.Preconditions.checkNotNull;
4import static org.slf4j.LoggerFactory.getLogger;
5
6import java.util.Iterator;
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;
alshabiba68eb962014-09-24 20:34:13 -070015import org.onlab.onos.ApplicationId;
alshabib57044ba2014-09-16 15:58:01 -070016import org.onlab.onos.event.AbstractListenerRegistry;
17import org.onlab.onos.event.EventDeliveryService;
18import org.onlab.onos.net.Device;
19import org.onlab.onos.net.DeviceId;
20import org.onlab.onos.net.device.DeviceService;
alshabib1c319ff2014-10-04 20:29:09 -070021import org.onlab.onos.net.flow.FlowEntry;
alshabib57044ba2014-09-16 15:58:01 -070022import org.onlab.onos.net.flow.FlowRule;
23import 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;
tomc78acee2014-09-24 15:16:55 -070030import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
alshabib57044ba2014-09-16 15:58:01 -070031import org.onlab.onos.net.provider.AbstractProviderRegistry;
32import org.onlab.onos.net.provider.AbstractProviderService;
33import org.slf4j.Logger;
34
alshabibbb42cad2014-09-25 11:43:05 -070035import com.google.common.collect.Lists;
alshabiba7f7ca82014-09-22 11:41:23 -070036
tome4729872014-09-23 00:37:37 -070037/**
38 * Provides implementation of the flow NB & SB APIs.
39 */
alshabib57044ba2014-09-16 15:58:01 -070040@Component(immediate = true)
41@Service
tom202175a2014-09-19 19:00:11 -070042public class FlowRuleManager
tom9b4030d2014-10-06 10:39:03 -070043 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
44 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070045
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070046 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070047 private final Logger log = getLogger(getClass());
48
49 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
tom9b4030d2014-10-06 10:39:03 -070050 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -070051
alshabibbb42cad2014-09-25 11:43:05 -070052 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -070053
tombe988312014-09-19 18:38:47 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070056
alshabib57044ba2014-09-16 15:58:01 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070058 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070059
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070061 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070062
63 @Activate
64 public void activate() {
tomc78acee2014-09-24 15:16:55 -070065 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -070066 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
67 log.info("Started");
68 }
69
70 @Deactivate
71 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070072 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -070073 eventDispatcher.removeSink(FlowRuleEvent.class);
74 log.info("Stopped");
75 }
76
77 @Override
tom9b4030d2014-10-06 10:39:03 -070078 public int getFlowRuleCount() {
79 return store.getFlowRuleCount();
80 }
81
82 @Override
alshabib1c319ff2014-10-04 20:29:09 -070083 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070084 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070085 }
86
87 @Override
alshabib219ebaa2014-09-22 15:41:24 -070088 public void applyFlowRules(FlowRule... flowRules) {
alshabib57044ba2014-09-16 15:58:01 -070089 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -070090 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070091 final Device device = deviceService.getDevice(f.deviceId());
92 final FlowRuleProvider frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -070093 store.storeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070094 frp.applyFlowRule(f);
95 }
alshabib57044ba2014-09-16 15:58:01 -070096 }
97
98 @Override
99 public void removeFlowRules(FlowRule... flowRules) {
alshabibbb8b1282014-09-22 17:00:18 -0700100 FlowRule f;
alshabiba68eb962014-09-24 20:34:13 -0700101 FlowRuleProvider frp;
102 Device device;
alshabib57044ba2014-09-16 15:58:01 -0700103 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -0700104 f = flowRules[i];
105 device = deviceService.getDevice(f.deviceId());
106 frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -0700107 store.deleteFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -0700108 frp.removeFlowRule(f);
109 }
alshabiba68eb962014-09-24 20:34:13 -0700110 }
alshabib57044ba2014-09-16 15:58:01 -0700111
alshabiba68eb962014-09-24 20:34:13 -0700112 @Override
113 public void removeFlowRulesById(ApplicationId id) {
tom9b4030d2014-10-06 10:39:03 -0700114 Iterable<FlowRule> rules = getFlowRulesById(id);
alshabiba68eb962014-09-24 20:34:13 -0700115 FlowRuleProvider frp;
116 Device device;
alshabibbb42cad2014-09-25 11:43:05 -0700117
alshabiba68eb962014-09-24 20:34:13 -0700118 for (FlowRule f : rules) {
119 store.deleteFlowRule(f);
120 device = deviceService.getDevice(f.deviceId());
121 frp = getProvider(device.providerId());
122 frp.removeRulesById(id, f);
123 }
124 }
125
126 @Override
127 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
alshabib1c319ff2014-10-04 20:29:09 -0700128 return store.getFlowRulesByAppId(id);
alshabib57044ba2014-09-16 15:58:01 -0700129 }
130
131 @Override
132 public void addListener(FlowRuleListener listener) {
133 listenerRegistry.addListener(listener);
134 }
135
136 @Override
137 public void removeListener(FlowRuleListener listener) {
138 listenerRegistry.removeListener(listener);
139 }
140
141 @Override
142 protected FlowRuleProviderService createProviderService(
143 FlowRuleProvider provider) {
144 return new InternalFlowRuleProviderService(provider);
145 }
146
147 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700148 extends AbstractProviderService<FlowRuleProvider>
149 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700150
151 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
152 super(provider);
153 }
154
155 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700156 public void flowRemoved(FlowEntry flowEntry) {
157 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700158 checkValidity();
alshabib1c319ff2014-10-04 20:29:09 -0700159 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700160 if (stored == null) {
alshabib1c319ff2014-10-04 20:29:09 -0700161 log.info("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700162 return;
163 }
alshabib1c319ff2014-10-04 20:29:09 -0700164 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700165 FlowRuleProvider frp = getProvider(device.providerId());
166 FlowRuleEvent event = null;
167 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700168 case ADDED:
169 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700170 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700171 break;
172 case PENDING_REMOVE:
173 case REMOVED:
174 event = store.removeFlowRule(stored);
175 break;
176 default:
177 break;
alshabib57044ba2014-09-16 15:58:01 -0700178
alshabiba68eb962014-09-24 20:34:13 -0700179 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700180 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700181 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700182 post(event);
183 }
alshabib57044ba2014-09-16 15:58:01 -0700184 }
185
alshabibba5ac482014-10-02 17:15:20 -0700186
alshabib1c319ff2014-10-04 20:29:09 -0700187 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700188 checkNotNull(flowRule, FLOW_RULE_NULL);
189 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700190 Device device = deviceService.getDevice(flowRule.deviceId());
191 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700192 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700193 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700194 case PENDING_REMOVE:
195 case REMOVED:
196 event = store.removeFlowRule(flowRule);
197 frp.removeFlowRule(flowRule);
198 break;
199 case ADDED:
200 case PENDING_ADD:
201 frp.applyFlowRule(flowRule);
202 break;
203 default:
204 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700205 }
206
alshabibbb42cad2014-09-25 11:43:05 -0700207 if (event != null) {
208 log.debug("Flow {} removed", flowRule);
209 post(event);
210 }
alshabib57044ba2014-09-16 15:58:01 -0700211
212 }
213
alshabibba5ac482014-10-02 17:15:20 -0700214
215 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700216 checkNotNull(flowRule, FLOW_RULE_NULL);
217 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700218 removeFlowRules(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700219 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700220 }
221
alshabibba5ac482014-10-02 17:15:20 -0700222
alshabib1c319ff2014-10-04 20:29:09 -0700223 private void flowAdded(FlowEntry flowEntry) {
224 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700225 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700226
alshabib1c319ff2014-10-04 20:29:09 -0700227 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700228
alshabib1c319ff2014-10-04 20:29:09 -0700229 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700230 if (event == null) {
231 log.debug("No flow store event generated.");
232 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700233 log.debug("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700234 post(event);
235 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700236 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700237 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700238 }
alshabib219ebaa2014-09-22 15:41:24 -0700239
alshabib57044ba2014-09-16 15:58:01 -0700240 }
241
alshabib1c319ff2014-10-04 20:29:09 -0700242 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
243 if (storedRule == null) {
244 return false;
245 }
alshabib85c41972014-10-03 13:48:39 -0700246 long timeout = storedRule.timeout() * 1000;
247 Long currentTime = System.currentTimeMillis();
248 if (storedRule.packets() != swRule.packets()) {
alshabib1c319ff2014-10-04 20:29:09 -0700249 storedRule.setLastSeen();
alshabib85c41972014-10-03 13:48:39 -0700250 return true;
251 }
alshabib85c41972014-10-03 13:48:39 -0700252
alshabib1c319ff2014-10-04 20:29:09 -0700253 if ((currentTime - storedRule.lastSeen()) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700254 return true;
255 }
256 return false;
alshabibba5ac482014-10-02 17:15:20 -0700257 }
258
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700259 // Posts the specified event to the local event dispatcher.
260 private void post(FlowRuleEvent event) {
261 if (event != null) {
262 eventDispatcher.post(event);
263 }
264 }
alshabib5c370ff2014-09-18 10:12:14 -0700265
266 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700267 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
268 List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabibbb8b1282014-09-22 17:00:18 -0700269
alshabib1c319ff2014-10-04 20:29:09 -0700270 Iterator<FlowEntry> switchRulesIterator = flowEntries.iterator();
alshabiba7f7ca82014-09-22 11:41:23 -0700271
272 while (switchRulesIterator.hasNext()) {
alshabib1c319ff2014-10-04 20:29:09 -0700273 FlowEntry rule = switchRulesIterator.next();
alshabiba7f7ca82014-09-22 11:41:23 -0700274 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700275 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700276 flowAdded(rule);
277 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700278 // the device has a rule the store does not have
279 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700280 }
281 }
alshabib1c319ff2014-10-04 20:29:09 -0700282 for (FlowEntry rule : storedRules) {
alshabiba7f7ca82014-09-22 11:41:23 -0700283 // there are rules in the store that aren't on the switch
284 flowMissing(rule);
alshabib54ce5892014-09-23 17:50:51 -0700285
alshabiba7f7ca82014-09-22 11:41:23 -0700286 }
alshabib5c370ff2014-09-18 10:12:14 -0700287 }
alshabib57044ba2014-09-16 15:58:01 -0700288 }
289
tomc78acee2014-09-24 15:16:55 -0700290 // Store delegate to re-post events emitted from the store.
291 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
292 @Override
293 public void notify(FlowRuleEvent event) {
294 eventDispatcher.post(event);
295 }
296 }
alshabib57044ba2014-09-16 15:58:01 -0700297}