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