blob: 7003271cb54948ff31150de0872cc7879532336e [file] [log] [blame]
alshabib57044ba2014-09-16 15:58:01 -07001package org.onlab.onos.net.trivial.flow.impl;
2
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;
27import org.onlab.onos.net.provider.AbstractProviderRegistry;
28import org.onlab.onos.net.provider.AbstractProviderService;
29import org.slf4j.Logger;
30
31@Component(immediate = true)
32@Service
33public class SimpleFlowRuleManager
34extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
35implements FlowRuleService, FlowRuleProviderRegistry {
36
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070038 private final Logger log = getLogger(getClass());
39
40 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
41 listenerRegistry = new AbstractListenerRegistry<>();
42
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070043 private final SimpleFlowRuleStore store = new SimpleFlowRuleStore();
44
alshabib57044ba2014-09-16 15:58:01 -070045 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070046 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070047
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070049 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070050
51 @Activate
52 public void activate() {
53 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
54 log.info("Started");
55 }
56
57 @Deactivate
58 public void deactivate() {
59 eventDispatcher.removeSink(FlowRuleEvent.class);
60 log.info("Stopped");
61 }
62
63 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070064 public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070065 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -070066 }
67
68 @Override
Ayaka Koshibed4e53e12014-09-18 14:24:55 -070069 public List<FlowRule> applyFlowRules(FlowRule... flowRules) {
70 List<FlowRule> entries = new ArrayList<FlowRule>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070071
alshabib57044ba2014-09-16 15:58:01 -070072 for (int i = 0; i < flowRules.length; i++) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070073 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070074 final Device device = deviceService.getDevice(f.deviceId());
75 final FlowRuleProvider frp = getProvider(device.providerId());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070076 entries.add(store.storeFlowRule(f));
alshabib57044ba2014-09-16 15:58:01 -070077 frp.applyFlowRule(f);
78 }
79
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070080 return entries;
alshabib57044ba2014-09-16 15:58:01 -070081 }
82
83 @Override
84 public void removeFlowRules(FlowRule... flowRules) {
85 for (int i = 0; i < flowRules.length; i++) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070086 FlowRule f = flowRules[i];
alshabib57044ba2014-09-16 15:58:01 -070087 final Device device = deviceService.getDevice(f.deviceId());
88 final FlowRuleProvider frp = getProvider(device.providerId());
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070089 store.removeFlowRule(f);
alshabib57044ba2014-09-16 15:58:01 -070090 frp.removeFlowRule(f);
91 }
92
93 }
94
95 @Override
96 public void addListener(FlowRuleListener listener) {
97 listenerRegistry.addListener(listener);
98 }
99
100 @Override
101 public void removeListener(FlowRuleListener listener) {
102 listenerRegistry.removeListener(listener);
103 }
104
105 @Override
106 protected FlowRuleProviderService createProviderService(
107 FlowRuleProvider provider) {
108 return new InternalFlowRuleProviderService(provider);
109 }
110
111 private class InternalFlowRuleProviderService
alshabib5c370ff2014-09-18 10:12:14 -0700112 extends AbstractProviderService<FlowRuleProvider>
113 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700114
115 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
116 super(provider);
117 }
118
119 @Override
120 public void flowRemoved(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700121 checkNotNull(flowRule, FLOW_RULE_NULL);
122 checkValidity();
123 FlowRuleEvent event = store.removeFlowRule(flowRule);
alshabib57044ba2014-09-16 15:58:01 -0700124
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700125 if (event != null) {
126 log.debug("Flow {} removed", flowRule);
127 post(event);
128 }
alshabib57044ba2014-09-16 15:58:01 -0700129 }
130
131 @Override
132 public void flowMissing(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700133 checkNotNull(flowRule, FLOW_RULE_NULL);
134 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700135 // TODO Auto-generated method stub
136
137 }
138
139 @Override
140 public void flowAdded(FlowRule flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700141 checkNotNull(flowRule, FLOW_RULE_NULL);
142 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700143
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700144 FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule);
145 if (event == null) {
146 log.debug("Flow {} updated", flowRule);
147 } else {
148 log.debug("Flow {} added", flowRule);
149 post(event);
150 }
alshabib57044ba2014-09-16 15:58:01 -0700151 }
152
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700153 // Posts the specified event to the local event dispatcher.
154 private void post(FlowRuleEvent event) {
155 if (event != null) {
156 eventDispatcher.post(event);
157 }
158 }
alshabib5c370ff2014-09-18 10:12:14 -0700159
160 @Override
alshabib97044902014-09-18 14:52:16 -0700161 public void pushFlowMetrics(Iterable<FlowRule> flowEntries) {
alshabib5c370ff2014-09-18 10:12:14 -0700162 // TODO Auto-generated method stub
163
164 }
alshabib57044ba2014-09-16 15:58:01 -0700165 }
166
167}