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