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