tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 2 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
| 6 | import java.util.Iterator; |
| 7 | import java.util.List; |
| 8 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 14 | import org.apache.felix.scr.annotations.Service; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 15 | import org.onlab.onos.ApplicationId; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 16 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 17 | import org.onlab.onos.event.EventDeliveryService; |
| 18 | import org.onlab.onos.net.Device; |
| 19 | import org.onlab.onos.net.DeviceId; |
| 20 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.flow.FlowRule; |
| 22 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 23 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 24 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 25 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 26 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 27 | import org.onlab.onos.net.flow.FlowRuleService; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 31 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 32 | import org.slf4j.Logger; |
| 33 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 34 | import com.google.common.collect.Lists; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 35 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Provides implementation of the flow NB & SB APIs. |
| 38 | */ |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 39 | @Component(immediate = true) |
| 40 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 41 | public class FlowRuleManager |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 42 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 43 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 44 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 45 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 49 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 50 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 51 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 52 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 55 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 57 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 60 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 61 | |
| 62 | @Activate |
| 63 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 64 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 65 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 71 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 72 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 73 | log.info("Stopped"); |
| 74 | } |
| 75 | |
| 76 | @Override |
Ayaka Koshibe | d4e53e1 | 2014-09-18 14:24:55 -0700 | [diff] [blame] | 77 | public Iterable<FlowRule> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 78 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 82 | public void applyFlowRules(FlowRule... flowRules) { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 83 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 84 | FlowRule f = flowRules[i]; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 85 | final Device device = deviceService.getDevice(f.deviceId()); |
| 86 | final FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 87 | store.storeFlowRule(f); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 88 | frp.applyFlowRule(f); |
| 89 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void removeFlowRules(FlowRule... flowRules) { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 94 | FlowRule f; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 95 | FlowRuleProvider frp; |
| 96 | Device device; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 97 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 98 | f = flowRules[i]; |
| 99 | device = deviceService.getDevice(f.deviceId()); |
| 100 | frp = getProvider(device.providerId()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 101 | store.deleteFlowRule(f); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 102 | frp.removeFlowRule(f); |
| 103 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 104 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 105 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public void removeFlowRulesById(ApplicationId id) { |
| 108 | Iterable<FlowRule> rules = getFlowRulesById(id); |
| 109 | FlowRuleProvider frp; |
| 110 | Device device; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 111 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 112 | for (FlowRule f : rules) { |
| 113 | store.deleteFlowRule(f); |
| 114 | device = deviceService.getDevice(f.deviceId()); |
| 115 | frp = getProvider(device.providerId()); |
| 116 | frp.removeRulesById(id, f); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
| 122 | return store.getFlowEntriesByAppId(id); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void addListener(FlowRuleListener listener) { |
| 127 | listenerRegistry.addListener(listener); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void removeListener(FlowRuleListener listener) { |
| 132 | listenerRegistry.removeListener(listener); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected FlowRuleProviderService createProviderService( |
| 137 | FlowRuleProvider provider) { |
| 138 | return new InternalFlowRuleProviderService(provider); |
| 139 | } |
| 140 | |
| 141 | private class InternalFlowRuleProviderService |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 142 | extends AbstractProviderService<FlowRuleProvider> |
| 143 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 144 | |
| 145 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 146 | super(provider); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void flowRemoved(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 151 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 152 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 153 | FlowRule stored = store.getFlowRule(flowRule); |
| 154 | if (stored == null) { |
| 155 | log.debug("Rule already evicted from store: {}", flowRule); |
| 156 | return; |
| 157 | } |
| 158 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 159 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 160 | FlowRuleEvent event = null; |
| 161 | switch (stored.state()) { |
| 162 | case ADDED: |
| 163 | case PENDING_ADD: |
| 164 | frp.applyFlowRule(flowRule); |
| 165 | break; |
| 166 | case PENDING_REMOVE: |
| 167 | case REMOVED: |
| 168 | event = store.removeFlowRule(flowRule); |
| 169 | break; |
| 170 | default: |
| 171 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 172 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 173 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 174 | if (event != null) { |
| 175 | log.debug("Flow {} removed", flowRule); |
| 176 | post(event); |
| 177 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void flowMissing(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 182 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 183 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 184 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 185 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 186 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 187 | switch (flowRule.state()) { |
| 188 | case PENDING_REMOVE: |
| 189 | case REMOVED: |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 190 | event = store.removeFlowRule(flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 191 | frp.removeFlowRule(flowRule); |
| 192 | break; |
| 193 | case ADDED: |
| 194 | case PENDING_ADD: |
| 195 | frp.applyFlowRule(flowRule); |
| 196 | break; |
| 197 | default: |
| 198 | log.debug("Flow {} has not been installed.", flowRule); |
| 199 | } |
| 200 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 201 | if (event != null) { |
| 202 | log.debug("Flow {} removed", flowRule); |
| 203 | post(event); |
| 204 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 205 | |
| 206 | } |
| 207 | |
| 208 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 209 | public void extraneousFlow(FlowRule flowRule) { |
| 210 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 211 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 212 | removeFlowRules(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 213 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 217 | public void flowAdded(FlowRule flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 218 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 219 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 220 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 221 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowRule); |
| 222 | if (event == null) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 223 | log.debug("No flow store event generated."); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 224 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 225 | log.debug("Flow {} {}", flowRule, event.type()); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 226 | post(event); |
| 227 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 228 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 231 | // Posts the specified event to the local event dispatcher. |
| 232 | private void post(FlowRuleEvent event) { |
| 233 | if (event != null) { |
| 234 | eventDispatcher.post(event); |
| 235 | } |
| 236 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 237 | |
| 238 | @Override |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 239 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) { |
| 240 | List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 241 | |
| 242 | Iterator<FlowRule> switchRulesIterator = flowEntries.iterator(); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 243 | |
| 244 | while (switchRulesIterator.hasNext()) { |
| 245 | FlowRule rule = switchRulesIterator.next(); |
| 246 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 247 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 248 | flowAdded(rule); |
| 249 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 250 | // the device has a rule the store does not have |
| 251 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | for (FlowRule rule : storedRules) { |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame^] | 255 | log.info("missing rule is {}", rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 256 | // there are rules in the store that aren't on the switch |
| 257 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 258 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 259 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 260 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 261 | } |
| 262 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 263 | // Store delegate to re-post events emitted from the store. |
| 264 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
| 265 | @Override |
| 266 | public void notify(FlowRuleEvent event) { |
| 267 | eventDispatcher.post(event); |
| 268 | } |
| 269 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 270 | } |