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 | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 8 | import java.util.concurrent.ExecutionException; |
| 9 | import java.util.concurrent.Future; |
| 10 | import java.util.concurrent.TimeUnit; |
| 11 | import java.util.concurrent.TimeoutException; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 12 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 13 | import org.apache.felix.scr.annotations.Activate; |
| 14 | import org.apache.felix.scr.annotations.Component; |
| 15 | import org.apache.felix.scr.annotations.Deactivate; |
| 16 | import org.apache.felix.scr.annotations.Reference; |
| 17 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 18 | import org.apache.felix.scr.annotations.Service; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 19 | import org.onlab.onos.ApplicationId; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 20 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 21 | import org.onlab.onos.event.EventDeliveryService; |
| 22 | import org.onlab.onos.net.Device; |
| 23 | import org.onlab.onos.net.DeviceId; |
| 24 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 29 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 31 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 32 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 33 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 34 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 35 | import org.onlab.onos.net.flow.FlowRuleService; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 39 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 40 | import org.slf4j.Logger; |
| 41 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 42 | import com.google.common.collect.ArrayListMultimap; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 43 | import com.google.common.collect.Lists; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 44 | import com.google.common.collect.Multimap; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 45 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Provides implementation of the flow NB & SB APIs. |
| 48 | */ |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 49 | @Component(immediate = true) |
| 50 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 51 | public class FlowRuleManager |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 52 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 53 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 54 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 55 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 59 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 60 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 61 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 62 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 65 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 67 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 70 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 74 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 75 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 81 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 82 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 87 | public int getFlowRuleCount() { |
| 88 | return store.getFlowRuleCount(); |
| 89 | } |
| 90 | |
| 91 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 92 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 93 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 97 | public void applyFlowRules(FlowRule... flowRules) { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 98 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 99 | FlowRule f = flowRules[i]; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 100 | final Device device = deviceService.getDevice(f.deviceId()); |
| 101 | final FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 102 | store.storeFlowRule(f); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 103 | frp.applyFlowRule(f); |
| 104 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void removeFlowRules(FlowRule... flowRules) { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 109 | FlowRule f; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 110 | FlowRuleProvider frp; |
| 111 | Device device; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 112 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 113 | f = flowRules[i]; |
| 114 | device = deviceService.getDevice(f.deviceId()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 115 | store.deleteFlowRule(f); |
tom | 7951b23 | 2014-10-06 13:35:30 -0700 | [diff] [blame] | 116 | if (device != null) { |
| 117 | frp = getProvider(device.providerId()); |
| 118 | frp.removeFlowRule(f); |
| 119 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 120 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 121 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 122 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public void removeFlowRulesById(ApplicationId id) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 125 | Iterable<FlowRule> rules = getFlowRulesById(id); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 126 | FlowRuleProvider frp; |
| 127 | Device device; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 128 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 129 | for (FlowRule f : rules) { |
| 130 | store.deleteFlowRule(f); |
| 131 | device = deviceService.getDevice(f.deviceId()); |
| 132 | frp = getProvider(device.providerId()); |
| 133 | frp.removeRulesById(id, f); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 139 | return store.getFlowRulesByAppId(id); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 143 | public Future<CompletedBatchOperation> applyBatch( |
| 144 | FlowRuleBatchOperation batch) { |
| 145 | Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches = |
| 146 | ArrayListMultimap.create(); |
| 147 | List<Future<Void>> futures = Lists.newArrayList(); |
| 148 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 149 | final FlowRule f = fbe.getTarget(); |
| 150 | final Device device = deviceService.getDevice(f.deviceId()); |
| 151 | final FlowRuleProvider frp = getProvider(device.providerId()); |
| 152 | batches.put(frp, fbe); |
| 153 | switch (fbe.getOperator()) { |
| 154 | case ADD: |
| 155 | store.storeFlowRule(f); |
| 156 | break; |
| 157 | case REMOVE: |
| 158 | store.deleteFlowRule(f); |
| 159 | break; |
| 160 | case MODIFY: |
| 161 | default: |
| 162 | log.error("Batch operation type {} unsupported.", fbe.getOperator()); |
| 163 | } |
| 164 | } |
| 165 | for (FlowRuleProvider provider : batches.keySet()) { |
| 166 | FlowRuleBatchOperation b = |
| 167 | new FlowRuleBatchOperation(batches.get(provider)); |
| 168 | Future<Void> future = provider.executeBatch(b); |
| 169 | futures.add(future); |
| 170 | } |
| 171 | return new FlowRuleBatchFuture(futures); |
| 172 | } |
| 173 | |
| 174 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 175 | public void addListener(FlowRuleListener listener) { |
| 176 | listenerRegistry.addListener(listener); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void removeListener(FlowRuleListener listener) { |
| 181 | listenerRegistry.removeListener(listener); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | protected FlowRuleProviderService createProviderService( |
| 186 | FlowRuleProvider provider) { |
| 187 | return new InternalFlowRuleProviderService(provider); |
| 188 | } |
| 189 | |
| 190 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 191 | extends AbstractProviderService<FlowRuleProvider> |
| 192 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 193 | |
| 194 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 195 | super(provider); |
| 196 | } |
| 197 | |
| 198 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 199 | public void flowRemoved(FlowEntry flowEntry) { |
| 200 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 201 | checkValidity(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 202 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 203 | if (stored == null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 204 | log.info("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 207 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 208 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 209 | FlowRuleEvent event = null; |
| 210 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 211 | case ADDED: |
| 212 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 213 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 214 | break; |
| 215 | case PENDING_REMOVE: |
| 216 | case REMOVED: |
| 217 | event = store.removeFlowRule(stored); |
| 218 | break; |
| 219 | default: |
| 220 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 221 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 222 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 223 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 224 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 225 | post(event); |
| 226 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 227 | } |
| 228 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 229 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 230 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 231 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 232 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 233 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 234 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 235 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 236 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 237 | case PENDING_REMOVE: |
| 238 | case REMOVED: |
| 239 | event = store.removeFlowRule(flowRule); |
| 240 | frp.removeFlowRule(flowRule); |
| 241 | break; |
| 242 | case ADDED: |
| 243 | case PENDING_ADD: |
| 244 | frp.applyFlowRule(flowRule); |
| 245 | break; |
| 246 | default: |
| 247 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 248 | } |
| 249 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 250 | if (event != null) { |
| 251 | log.debug("Flow {} removed", flowRule); |
| 252 | post(event); |
| 253 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 254 | |
| 255 | } |
| 256 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 257 | |
| 258 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 259 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 260 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 261 | removeFlowRules(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 262 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 263 | } |
| 264 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 265 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 266 | private void flowAdded(FlowEntry flowEntry) { |
| 267 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 268 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 269 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 270 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 271 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 272 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 273 | if (event == null) { |
| 274 | log.debug("No flow store event generated."); |
| 275 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 276 | log.debug("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 277 | post(event); |
| 278 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 279 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 280 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 281 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 282 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 283 | } |
| 284 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 285 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 286 | if (storedRule == null) { |
| 287 | return false; |
| 288 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 289 | long timeout = storedRule.timeout() * 1000; |
| 290 | Long currentTime = System.currentTimeMillis(); |
| 291 | if (storedRule.packets() != swRule.packets()) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 292 | storedRule.setLastSeen(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 293 | return true; |
| 294 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 295 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 296 | if ((currentTime - storedRule.lastSeen()) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 302 | // Posts the specified event to the local event dispatcher. |
| 303 | private void post(FlowRuleEvent event) { |
| 304 | if (event != null) { |
| 305 | eventDispatcher.post(event); |
| 306 | } |
| 307 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 308 | |
| 309 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 310 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 311 | List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 312 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 313 | Iterator<FlowEntry> switchRulesIterator = flowEntries.iterator(); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 314 | |
| 315 | while (switchRulesIterator.hasNext()) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 316 | FlowEntry rule = switchRulesIterator.next(); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 317 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 318 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 319 | flowAdded(rule); |
| 320 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 321 | // the device has a rule the store does not have |
| 322 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 325 | for (FlowEntry rule : storedRules) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 326 | // there are rules in the store that aren't on the switch |
| 327 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 328 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 329 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 330 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 331 | } |
| 332 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 333 | // Store delegate to re-post events emitted from the store. |
| 334 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
| 335 | @Override |
| 336 | public void notify(FlowRuleEvent event) { |
| 337 | eventDispatcher.post(event); |
| 338 | } |
| 339 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 340 | |
| 341 | private class FlowRuleBatchFuture |
| 342 | implements Future<CompletedBatchOperation> { |
| 343 | |
| 344 | private final List<Future<Void>> futures; |
| 345 | |
| 346 | public FlowRuleBatchFuture(List<Future<Void>> futures) { |
| 347 | this.futures = futures; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 352 | // TODO Auto-generated method stub |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public boolean isCancelled() { |
| 358 | // TODO Auto-generated method stub |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public boolean isDone() { |
| 364 | boolean isDone = true; |
| 365 | for (Future<Void> future : futures) { |
| 366 | isDone &= future.isDone(); |
| 367 | } |
| 368 | return isDone; |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public CompletedBatchOperation get() throws InterruptedException, |
| 373 | ExecutionException { |
| 374 | // TODO Auto-generated method stub |
| 375 | for (Future<Void> future : futures) { |
| 376 | future.get(); |
| 377 | } |
| 378 | return new CompletedBatchOperation(); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 383 | throws InterruptedException, ExecutionException, |
| 384 | TimeoutException { |
| 385 | // TODO we should decrement the timeout |
| 386 | long start = System.nanoTime(); |
| 387 | long end = start + unit.toNanos(timeout); |
| 388 | for (Future<Void> future : futures) { |
| 389 | long now = System.nanoTime(); |
| 390 | long thisTimeout = end - now; |
| 391 | future.get(thisTimeout, TimeUnit.NANOSECONDS); |
| 392 | } |
| 393 | return new CompletedBatchOperation(); |
| 394 | } |
| 395 | |
| 396 | } |
| 397 | |
| 398 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 399 | } |