blob: 385e300d61fb1a6068db514069ef3c5ef50fa42f [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
alshabiba7f7ca82014-09-22 11:41:23 -070043extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
44implements 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>
alshabiba7f7ca82014-09-22 11:41:23 -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
alshabib1c319ff2014-10-04 20:29:09 -070078 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070079 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070080 }
81
82 @Override
alshabib219ebaa2014-09-22 15:41:24 -070083 public void applyFlowRules(FlowRule... flowRules) {
alshabib57044ba2014-09-16 15:58:01 -070084 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -070085 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070086 final Device device = deviceService.getDevice(f.deviceId());
87 final FlowRuleProvider frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -070088 store.storeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070089 frp.applyFlowRule(f);
90 }
alshabib57044ba2014-09-16 15:58:01 -070091 }
92
93 @Override
94 public void removeFlowRules(FlowRule... flowRules) {
alshabibbb8b1282014-09-22 17:00:18 -070095 FlowRule f;
alshabiba68eb962014-09-24 20:34:13 -070096 FlowRuleProvider frp;
97 Device device;
alshabib57044ba2014-09-16 15:58:01 -070098 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -070099 f = flowRules[i];
100 device = deviceService.getDevice(f.deviceId());
101 frp = getProvider(device.providerId());
alshabib219ebaa2014-09-22 15:41:24 -0700102 store.deleteFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -0700103 frp.removeFlowRule(f);
104 }
alshabiba68eb962014-09-24 20:34:13 -0700105 }
alshabib57044ba2014-09-16 15:58:01 -0700106
alshabiba68eb962014-09-24 20:34:13 -0700107 @Override
108 public void removeFlowRulesById(ApplicationId id) {
109 Iterable<FlowRule> rules = getFlowRulesById(id);
110 FlowRuleProvider frp;
111 Device device;
alshabibbb42cad2014-09-25 11:43:05 -0700112
alshabiba68eb962014-09-24 20:34:13 -0700113 for (FlowRule f : rules) {
114 store.deleteFlowRule(f);
115 device = deviceService.getDevice(f.deviceId());
116 frp = getProvider(device.providerId());
117 frp.removeRulesById(id, f);
118 }
119 }
120
121 @Override
122 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
alshabib1c319ff2014-10-04 20:29:09 -0700123 return store.getFlowRulesByAppId(id);
alshabib57044ba2014-09-16 15:58:01 -0700124 }
125
126 @Override
127 public void addListener(FlowRuleListener listener) {
128 listenerRegistry.addListener(listener);
129 }
130
131 @Override
132 public void removeListener(FlowRuleListener listener) {
133 listenerRegistry.removeListener(listener);
134 }
135
136 @Override
137 protected FlowRuleProviderService createProviderService(
138 FlowRuleProvider provider) {
139 return new InternalFlowRuleProviderService(provider);
140 }
141
142 private class InternalFlowRuleProviderService
alshabiba7f7ca82014-09-22 11:41:23 -0700143 extends AbstractProviderService<FlowRuleProvider>
144 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700145
146 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
147 super(provider);
148 }
149
150 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700151 public void flowRemoved(FlowEntry flowEntry) {
152 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700153 checkValidity();
alshabib1c319ff2014-10-04 20:29:09 -0700154 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700155 if (stored == null) {
alshabib1c319ff2014-10-04 20:29:09 -0700156 log.info("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700157 return;
158 }
alshabib1c319ff2014-10-04 20:29:09 -0700159 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700160 FlowRuleProvider frp = getProvider(device.providerId());
161 FlowRuleEvent event = null;
162 switch (stored.state()) {
163 case ADDED:
164 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700165 frp.applyFlowRule(stored);
alshabiba68eb962014-09-24 20:34:13 -0700166 break;
167 case PENDING_REMOVE:
168 case REMOVED:
alshabib1c319ff2014-10-04 20:29:09 -0700169 event = store.removeFlowRule(stored);
alshabiba68eb962014-09-24 20:34:13 -0700170 break;
171 default:
172 break;
alshabib57044ba2014-09-16 15:58:01 -0700173
alshabiba68eb962014-09-24 20:34:13 -0700174 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700175 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700176 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700177 post(event);
178 }
alshabib57044ba2014-09-16 15:58:01 -0700179 }
180
alshabibba5ac482014-10-02 17:15:20 -0700181
alshabib1c319ff2014-10-04 20:29:09 -0700182 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700183 checkNotNull(flowRule, FLOW_RULE_NULL);
184 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700185 Device device = deviceService.getDevice(flowRule.deviceId());
186 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700187 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700188 switch (flowRule.state()) {
189 case PENDING_REMOVE:
190 case REMOVED:
alshabibbb42cad2014-09-25 11:43:05 -0700191 event = store.removeFlowRule(flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700192 frp.removeFlowRule(flowRule);
193 break;
194 case ADDED:
195 case PENDING_ADD:
196 frp.applyFlowRule(flowRule);
197 break;
198 default:
199 log.debug("Flow {} has not been installed.", flowRule);
200 }
201
alshabibbb42cad2014-09-25 11:43:05 -0700202 if (event != null) {
203 log.debug("Flow {} removed", flowRule);
204 post(event);
205 }
alshabib57044ba2014-09-16 15:58:01 -0700206
207 }
208
alshabibba5ac482014-10-02 17:15:20 -0700209
210 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700211 checkNotNull(flowRule, FLOW_RULE_NULL);
212 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700213 removeFlowRules(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700214 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700215 }
216
alshabibba5ac482014-10-02 17:15:20 -0700217
alshabib1c319ff2014-10-04 20:29:09 -0700218 private void flowAdded(FlowEntry flowEntry) {
219 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700220 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700221
alshabib1c319ff2014-10-04 20:29:09 -0700222 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700223
alshabib1c319ff2014-10-04 20:29:09 -0700224 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700225 if (event == null) {
226 log.debug("No flow store event generated.");
227 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700228 log.debug("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700229 post(event);
230 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700231 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700232 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700233 }
alshabib219ebaa2014-09-22 15:41:24 -0700234
alshabib57044ba2014-09-16 15:58:01 -0700235 }
236
alshabib1c319ff2014-10-04 20:29:09 -0700237 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
238 if (storedRule == null) {
239 return false;
240 }
alshabib85c41972014-10-03 13:48:39 -0700241 long timeout = storedRule.timeout() * 1000;
242 Long currentTime = System.currentTimeMillis();
243 if (storedRule.packets() != swRule.packets()) {
alshabib1c319ff2014-10-04 20:29:09 -0700244 storedRule.setLastSeen();
alshabib85c41972014-10-03 13:48:39 -0700245 return true;
246 }
alshabib85c41972014-10-03 13:48:39 -0700247
alshabib1c319ff2014-10-04 20:29:09 -0700248 if ((currentTime - storedRule.lastSeen()) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700249 return true;
250 }
251 return false;
alshabibba5ac482014-10-02 17:15:20 -0700252 }
253
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700254 // Posts the specified event to the local event dispatcher.
255 private void post(FlowRuleEvent event) {
256 if (event != null) {
257 eventDispatcher.post(event);
258 }
259 }
alshabib5c370ff2014-09-18 10:12:14 -0700260
261 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700262 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
263 List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabibbb8b1282014-09-22 17:00:18 -0700264
alshabib1c319ff2014-10-04 20:29:09 -0700265 Iterator<FlowEntry> switchRulesIterator = flowEntries.iterator();
alshabiba7f7ca82014-09-22 11:41:23 -0700266
267 while (switchRulesIterator.hasNext()) {
alshabib1c319ff2014-10-04 20:29:09 -0700268 FlowEntry rule = switchRulesIterator.next();
alshabiba7f7ca82014-09-22 11:41:23 -0700269 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700270 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700271 flowAdded(rule);
272 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700273 // the device has a rule the store does not have
274 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700275 }
276 }
alshabib1c319ff2014-10-04 20:29:09 -0700277 for (FlowEntry rule : storedRules) {
alshabiba7f7ca82014-09-22 11:41:23 -0700278 // there are rules in the store that aren't on the switch
279 flowMissing(rule);
alshabib54ce5892014-09-23 17:50:51 -0700280
alshabiba7f7ca82014-09-22 11:41:23 -0700281 }
alshabib5c370ff2014-09-18 10:12:14 -0700282 }
alshabib57044ba2014-09-16 15:58:01 -0700283 }
284
tomc78acee2014-09-24 15:16:55 -0700285 // Store delegate to re-post events emitted from the store.
286 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
287 @Override
288 public void notify(FlowRuleEvent event) {
289 eventDispatcher.post(event);
290 }
291 }
alshabib57044ba2014-09-16 15:58:01 -0700292}