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 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 6 | import java.util.List; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 7 | import java.util.Map; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 8 | import java.util.concurrent.CancellationException; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 9 | import java.util.concurrent.ExecutionException; |
| 10 | import java.util.concurrent.Future; |
| 11 | import java.util.concurrent.TimeUnit; |
| 12 | import java.util.concurrent.TimeoutException; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 13 | import java.util.concurrent.atomic.AtomicReference; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 14 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 15 | import org.apache.felix.scr.annotations.Activate; |
| 16 | import org.apache.felix.scr.annotations.Component; |
| 17 | import org.apache.felix.scr.annotations.Deactivate; |
| 18 | import org.apache.felix.scr.annotations.Reference; |
| 19 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 20 | import org.apache.felix.scr.annotations.Service; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 21 | import org.onlab.onos.ApplicationId; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 22 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 23 | import org.onlab.onos.event.EventDeliveryService; |
| 24 | import org.onlab.onos.net.Device; |
| 25 | import org.onlab.onos.net.DeviceId; |
| 26 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 35 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 36 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 37 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 38 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 39 | import org.onlab.onos.net.flow.FlowRuleService; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 43 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 44 | import org.slf4j.Logger; |
| 45 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 46 | import com.google.common.collect.ArrayListMultimap; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 47 | import com.google.common.collect.Lists; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 48 | import com.google.common.collect.Maps; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 49 | import com.google.common.collect.Multimap; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 50 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Provides implementation of the flow NB & SB APIs. |
| 53 | */ |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 54 | @Component(immediate = true) |
| 55 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 56 | public class FlowRuleManager |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 57 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 58 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 59 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 60 | enum BatchState { STARTED, FINISHED, CANCELLED }; |
| 61 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 62 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 66 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 67 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 68 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 69 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 72 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 74 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 77 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 78 | |
| 79 | @Activate |
| 80 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 81 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 82 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 88 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 89 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 94 | public int getFlowRuleCount() { |
| 95 | return store.getFlowRuleCount(); |
| 96 | } |
| 97 | |
| 98 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 99 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 100 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 104 | public void applyFlowRules(FlowRule... flowRules) { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 105 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 106 | FlowRule f = flowRules[i]; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 107 | boolean local = store.storeFlowRule(f); |
| 108 | if (local) { |
| 109 | // TODO: aggregate all local rules and push down once? |
| 110 | applyFlowRulesToProviders(f); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void applyFlowRulesToProviders(FlowRule... flowRules) { |
| 116 | DeviceId did = null; |
| 117 | FlowRuleProvider frp = null; |
| 118 | for (FlowRule f : flowRules) { |
| 119 | if (!f.deviceId().equals(did)) { |
| 120 | did = f.deviceId(); |
| 121 | final Device device = deviceService.getDevice(did); |
| 122 | frp = getProvider(device.providerId()); |
| 123 | } |
| 124 | if (frp != null) { |
| 125 | frp.applyFlowRule(f); |
| 126 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 127 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void removeFlowRules(FlowRule... flowRules) { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 132 | FlowRule f; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 133 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 134 | f = flowRules[i]; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 135 | boolean local = store.deleteFlowRule(f); |
| 136 | if (local) { |
| 137 | // TODO: aggregate all local rules and push down once? |
| 138 | removeFlowRulesFromProviders(f); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private void removeFlowRulesFromProviders(FlowRule... flowRules) { |
| 144 | DeviceId did = null; |
| 145 | FlowRuleProvider frp = null; |
| 146 | for (FlowRule f : flowRules) { |
| 147 | if (!f.deviceId().equals(did)) { |
| 148 | did = f.deviceId(); |
| 149 | final Device device = deviceService.getDevice(did); |
tom | 7951b23 | 2014-10-06 13:35:30 -0700 | [diff] [blame] | 150 | frp = getProvider(device.providerId()); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 151 | } |
| 152 | if (frp != null) { |
tom | 7951b23 | 2014-10-06 13:35:30 -0700 | [diff] [blame] | 153 | frp.removeFlowRule(f); |
| 154 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 155 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 156 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 157 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public void removeFlowRulesById(ApplicationId id) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 160 | Iterable<FlowRule> rules = getFlowRulesById(id); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 161 | FlowRuleProvider frp; |
| 162 | Device device; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 163 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 164 | for (FlowRule f : rules) { |
| 165 | store.deleteFlowRule(f); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 166 | // FIXME: only accept request and push to provider on internal event |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 167 | device = deviceService.getDevice(f.deviceId()); |
| 168 | frp = getProvider(device.providerId()); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 169 | // FIXME: flows removed from store and flows removed from might diverge |
| 170 | // get rid of #removeRulesById? |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 171 | frp.removeRulesById(id, f); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 177 | return store.getFlowRulesByAppId(id); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 181 | public Future<CompletedBatchOperation> applyBatch( |
| 182 | FlowRuleBatchOperation batch) { |
| 183 | Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches = |
| 184 | ArrayListMultimap.create(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 185 | List<Future<CompletedBatchOperation>> futures = Lists.newArrayList(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 186 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 187 | final FlowRule f = fbe.getTarget(); |
| 188 | final Device device = deviceService.getDevice(f.deviceId()); |
| 189 | final FlowRuleProvider frp = getProvider(device.providerId()); |
| 190 | batches.put(frp, fbe); |
| 191 | switch (fbe.getOperator()) { |
| 192 | case ADD: |
| 193 | store.storeFlowRule(f); |
| 194 | break; |
| 195 | case REMOVE: |
| 196 | store.deleteFlowRule(f); |
| 197 | break; |
| 198 | case MODIFY: |
| 199 | default: |
| 200 | log.error("Batch operation type {} unsupported.", fbe.getOperator()); |
| 201 | } |
| 202 | } |
| 203 | for (FlowRuleProvider provider : batches.keySet()) { |
| 204 | FlowRuleBatchOperation b = |
| 205 | new FlowRuleBatchOperation(batches.get(provider)); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 206 | Future<CompletedBatchOperation> future = provider.executeBatch(b); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 207 | futures.add(future); |
| 208 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 209 | return new FlowRuleBatchFuture(futures, batches); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 213 | public void addListener(FlowRuleListener listener) { |
| 214 | listenerRegistry.addListener(listener); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void removeListener(FlowRuleListener listener) { |
| 219 | listenerRegistry.removeListener(listener); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | protected FlowRuleProviderService createProviderService( |
| 224 | FlowRuleProvider provider) { |
| 225 | return new InternalFlowRuleProviderService(provider); |
| 226 | } |
| 227 | |
| 228 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 229 | extends AbstractProviderService<FlowRuleProvider> |
| 230 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 231 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 232 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 233 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 234 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 235 | super(provider); |
| 236 | } |
| 237 | |
| 238 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 239 | public void flowRemoved(FlowEntry flowEntry) { |
| 240 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 241 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 242 | lastSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 243 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 244 | if (stored == null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 245 | log.info("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 248 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 249 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 250 | FlowRuleEvent event = null; |
| 251 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 252 | case ADDED: |
| 253 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 254 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 255 | break; |
| 256 | case PENDING_REMOVE: |
| 257 | case REMOVED: |
| 258 | event = store.removeFlowRule(stored); |
| 259 | break; |
| 260 | default: |
| 261 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 262 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 263 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 264 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 265 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 266 | post(event); |
| 267 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 268 | } |
| 269 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 270 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 271 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 272 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 273 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 274 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 275 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 276 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 277 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 278 | case PENDING_REMOVE: |
| 279 | case REMOVED: |
| 280 | event = store.removeFlowRule(flowRule); |
| 281 | frp.removeFlowRule(flowRule); |
| 282 | break; |
| 283 | case ADDED: |
| 284 | case PENDING_ADD: |
| 285 | frp.applyFlowRule(flowRule); |
| 286 | break; |
| 287 | default: |
| 288 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 289 | } |
| 290 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 291 | if (event != null) { |
| 292 | log.debug("Flow {} removed", flowRule); |
| 293 | post(event); |
| 294 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 295 | |
| 296 | } |
| 297 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 298 | |
| 299 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 300 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 301 | checkValidity(); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame^] | 302 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 303 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 304 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 305 | } |
| 306 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 307 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 308 | private void flowAdded(FlowEntry flowEntry) { |
| 309 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 310 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 311 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 312 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 313 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 314 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 315 | if (event == null) { |
| 316 | log.debug("No flow store event generated."); |
| 317 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 318 | log.debug("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 319 | post(event); |
| 320 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 321 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 322 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 323 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 324 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 325 | } |
| 326 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 327 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 328 | if (storedRule == null) { |
| 329 | return false; |
| 330 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 331 | if (storedRule.isPermanent()) { |
| 332 | return true; |
| 333 | } |
| 334 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 335 | final long timeout = storedRule.timeout() * 1000; |
| 336 | final long currentTime = System.currentTimeMillis(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 337 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 338 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 339 | return true; |
| 340 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 341 | if (!lastSeen.containsKey(storedRule)) { |
| 342 | // checking for the first time |
| 343 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 344 | // Use following if lastSeen attr. was removed. |
| 345 | //lastSeen.put(storedRule, currentTime); |
| 346 | } |
| 347 | Long last = lastSeen.get(storedRule); |
| 348 | if (last == null) { |
| 349 | // concurrently removed? let the liveness check fail |
| 350 | return false; |
| 351 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 352 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 353 | if ((currentTime - last) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 359 | // Posts the specified event to the local event dispatcher. |
| 360 | private void post(FlowRuleEvent event) { |
| 361 | if (event != null) { |
| 362 | eventDispatcher.post(event); |
| 363 | } |
| 364 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 365 | |
| 366 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 367 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 368 | List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 369 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 370 | for (FlowEntry rule : flowEntries) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 371 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 372 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 373 | flowAdded(rule); |
| 374 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 375 | // the device has a rule the store does not have |
| 376 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 379 | for (FlowEntry rule : storedRules) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 380 | // there are rules in the store that aren't on the switch |
| 381 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 382 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 383 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 384 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 385 | } |
| 386 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 387 | // Store delegate to re-post events emitted from the store. |
| 388 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
| 389 | @Override |
| 390 | public void notify(FlowRuleEvent event) { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 391 | switch (event.type()) { |
| 392 | case RULE_ADD_REQUESTED: |
| 393 | applyFlowRulesToProviders(event.subject()); |
| 394 | break; |
| 395 | case RULE_REMOVE_REQUESTED: |
| 396 | removeFlowRulesFromProviders(event.subject()); |
| 397 | break; |
| 398 | |
| 399 | case RULE_ADDED: |
| 400 | case RULE_REMOVED: |
| 401 | case RULE_UPDATED: |
| 402 | // only dispatch events related to switch |
| 403 | eventDispatcher.post(event); |
| 404 | break; |
| 405 | default: |
| 406 | break; |
| 407 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 410 | |
| 411 | private class FlowRuleBatchFuture |
| 412 | implements Future<CompletedBatchOperation> { |
| 413 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 414 | private final List<Future<CompletedBatchOperation>> futures; |
| 415 | private final Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches; |
| 416 | private final AtomicReference<BatchState> state; |
| 417 | private CompletedBatchOperation overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 418 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 419 | |
| 420 | |
| 421 | public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures, |
| 422 | Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 423 | this.futures = futures; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 424 | this.batches = batches; |
| 425 | state = new AtomicReference<FlowRuleManager.BatchState>(); |
| 426 | state.set(BatchState.STARTED); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public boolean cancel(boolean mayInterruptIfRunning) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 431 | if (state.get() == BatchState.FINISHED) { |
| 432 | return false; |
| 433 | } |
| 434 | if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) { |
| 435 | return false; |
| 436 | } |
| 437 | cleanUpBatch(); |
| 438 | for (Future<CompletedBatchOperation> f : futures) { |
| 439 | f.cancel(mayInterruptIfRunning); |
| 440 | } |
| 441 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 446 | return state.get() == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public boolean isDone() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 451 | return state.get() == BatchState.FINISHED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 452 | } |
| 453 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 454 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 455 | @Override |
| 456 | public CompletedBatchOperation get() throws InterruptedException, |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 457 | ExecutionException { |
| 458 | |
| 459 | if (isDone()) { |
| 460 | return overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 461 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 462 | |
| 463 | boolean success = true; |
| 464 | List<FlowEntry> failed = Lists.newLinkedList(); |
| 465 | CompletedBatchOperation completed; |
| 466 | for (Future<CompletedBatchOperation> future : futures) { |
| 467 | completed = future.get(); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 468 | success = validateBatchOperation(failed, completed); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return finalizeBatchOperation(success, failed); |
| 472 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 477 | throws InterruptedException, ExecutionException, |
| 478 | TimeoutException { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 479 | |
| 480 | if (isDone()) { |
| 481 | return overall; |
| 482 | } |
| 483 | boolean success = true; |
| 484 | List<FlowEntry> failed = Lists.newLinkedList(); |
| 485 | CompletedBatchOperation completed; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 486 | long start = System.nanoTime(); |
| 487 | long end = start + unit.toNanos(timeout); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 488 | |
| 489 | for (Future<CompletedBatchOperation> future : futures) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 490 | long now = System.nanoTime(); |
| 491 | long thisTimeout = end - now; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 492 | completed = future.get(thisTimeout, TimeUnit.NANOSECONDS); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 493 | success = validateBatchOperation(failed, completed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 494 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 495 | return finalizeBatchOperation(success, failed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 496 | } |
| 497 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 498 | private boolean validateBatchOperation(List<FlowEntry> failed, |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 499 | CompletedBatchOperation completed) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 500 | |
| 501 | if (isCancelled()) { |
| 502 | throw new CancellationException(); |
| 503 | } |
| 504 | if (!completed.isSuccess()) { |
| 505 | failed.addAll(completed.failedItems()); |
| 506 | cleanUpBatch(); |
| 507 | cancelAllSubBatches(); |
| 508 | return false; |
| 509 | } |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | private void cancelAllSubBatches() { |
| 514 | for (Future<CompletedBatchOperation> f : futures) { |
| 515 | f.cancel(true); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | private CompletedBatchOperation finalizeBatchOperation(boolean success, |
| 520 | List<FlowEntry> failed) { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 521 | synchronized (this) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 522 | if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) { |
| 523 | if (state.get() == BatchState.FINISHED) { |
| 524 | return overall; |
| 525 | } |
| 526 | throw new CancellationException(); |
| 527 | } |
| 528 | overall = new CompletedBatchOperation(success, failed); |
| 529 | return overall; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | private void cleanUpBatch() { |
| 534 | for (FlowRuleBatchEntry fbe : batches.values()) { |
| 535 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
| 536 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
| 537 | store.deleteFlowRule(fbe.getTarget()); |
| 538 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 539 | store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 540 | store.storeFlowRule(fbe.getTarget()); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 548 | |
| 549 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 550 | } |