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 | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 302 | removeFlowRules(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 303 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 304 | } |
| 305 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 306 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 307 | private void flowAdded(FlowEntry flowEntry) { |
| 308 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 309 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 310 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 311 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 312 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 313 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 314 | if (event == null) { |
| 315 | log.debug("No flow store event generated."); |
| 316 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 317 | log.debug("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 318 | post(event); |
| 319 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 320 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 321 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 322 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 323 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 324 | } |
| 325 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 326 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 327 | if (storedRule == null) { |
| 328 | return false; |
| 329 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 330 | final long timeout = storedRule.timeout() * 1000; |
| 331 | final long currentTime = System.currentTimeMillis(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 332 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 333 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 334 | return true; |
| 335 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 336 | if (!lastSeen.containsKey(storedRule)) { |
| 337 | // checking for the first time |
| 338 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 339 | // Use following if lastSeen attr. was removed. |
| 340 | //lastSeen.put(storedRule, currentTime); |
| 341 | } |
| 342 | Long last = lastSeen.get(storedRule); |
| 343 | if (last == null) { |
| 344 | // concurrently removed? let the liveness check fail |
| 345 | return false; |
| 346 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 347 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 348 | if ((currentTime - last) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 354 | // Posts the specified event to the local event dispatcher. |
| 355 | private void post(FlowRuleEvent event) { |
| 356 | if (event != null) { |
| 357 | eventDispatcher.post(event); |
| 358 | } |
| 359 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 360 | |
| 361 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 362 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 363 | List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 364 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 365 | for (FlowEntry rule : flowEntries) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 366 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 367 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 368 | flowAdded(rule); |
| 369 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 370 | // the device has a rule the store does not have |
| 371 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 374 | for (FlowEntry rule : storedRules) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 375 | // there are rules in the store that aren't on the switch |
| 376 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 377 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 378 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 379 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 380 | } |
| 381 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 382 | // Store delegate to re-post events emitted from the store. |
| 383 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
| 384 | @Override |
| 385 | public void notify(FlowRuleEvent event) { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame^] | 386 | switch (event.type()) { |
| 387 | case RULE_ADD_REQUESTED: |
| 388 | applyFlowRulesToProviders(event.subject()); |
| 389 | break; |
| 390 | case RULE_REMOVE_REQUESTED: |
| 391 | removeFlowRulesFromProviders(event.subject()); |
| 392 | break; |
| 393 | |
| 394 | case RULE_ADDED: |
| 395 | case RULE_REMOVED: |
| 396 | case RULE_UPDATED: |
| 397 | // only dispatch events related to switch |
| 398 | eventDispatcher.post(event); |
| 399 | break; |
| 400 | default: |
| 401 | break; |
| 402 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 405 | |
| 406 | private class FlowRuleBatchFuture |
| 407 | implements Future<CompletedBatchOperation> { |
| 408 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 409 | private final List<Future<CompletedBatchOperation>> futures; |
| 410 | private final Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches; |
| 411 | private final AtomicReference<BatchState> state; |
| 412 | private CompletedBatchOperation overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 413 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 414 | |
| 415 | |
| 416 | public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures, |
| 417 | Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 418 | this.futures = futures; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 419 | this.batches = batches; |
| 420 | state = new AtomicReference<FlowRuleManager.BatchState>(); |
| 421 | state.set(BatchState.STARTED); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public boolean cancel(boolean mayInterruptIfRunning) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 426 | if (state.get() == BatchState.FINISHED) { |
| 427 | return false; |
| 428 | } |
| 429 | if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) { |
| 430 | return false; |
| 431 | } |
| 432 | cleanUpBatch(); |
| 433 | for (Future<CompletedBatchOperation> f : futures) { |
| 434 | f.cancel(mayInterruptIfRunning); |
| 435 | } |
| 436 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 441 | return state.get() == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public boolean isDone() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 446 | return state.get() == BatchState.FINISHED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 447 | } |
| 448 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 449 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 450 | @Override |
| 451 | public CompletedBatchOperation get() throws InterruptedException, |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 452 | ExecutionException { |
| 453 | |
| 454 | if (isDone()) { |
| 455 | return overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 456 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 457 | |
| 458 | boolean success = true; |
| 459 | List<FlowEntry> failed = Lists.newLinkedList(); |
| 460 | CompletedBatchOperation completed; |
| 461 | for (Future<CompletedBatchOperation> future : futures) { |
| 462 | completed = future.get(); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 463 | success = validateBatchOperation(failed, completed); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return finalizeBatchOperation(success, failed); |
| 467 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 472 | throws InterruptedException, ExecutionException, |
| 473 | TimeoutException { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 474 | |
| 475 | if (isDone()) { |
| 476 | return overall; |
| 477 | } |
| 478 | boolean success = true; |
| 479 | List<FlowEntry> failed = Lists.newLinkedList(); |
| 480 | CompletedBatchOperation completed; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 481 | long start = System.nanoTime(); |
| 482 | long end = start + unit.toNanos(timeout); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 483 | |
| 484 | for (Future<CompletedBatchOperation> future : futures) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 485 | long now = System.nanoTime(); |
| 486 | long thisTimeout = end - now; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 487 | completed = future.get(thisTimeout, TimeUnit.NANOSECONDS); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 488 | success = validateBatchOperation(failed, completed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 489 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 490 | return finalizeBatchOperation(success, failed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 491 | } |
| 492 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 493 | private boolean validateBatchOperation(List<FlowEntry> failed, |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 494 | CompletedBatchOperation completed) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 495 | |
| 496 | if (isCancelled()) { |
| 497 | throw new CancellationException(); |
| 498 | } |
| 499 | if (!completed.isSuccess()) { |
| 500 | failed.addAll(completed.failedItems()); |
| 501 | cleanUpBatch(); |
| 502 | cancelAllSubBatches(); |
| 503 | return false; |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | private void cancelAllSubBatches() { |
| 509 | for (Future<CompletedBatchOperation> f : futures) { |
| 510 | f.cancel(true); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private CompletedBatchOperation finalizeBatchOperation(boolean success, |
| 515 | List<FlowEntry> failed) { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 516 | synchronized (this) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 517 | if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) { |
| 518 | if (state.get() == BatchState.FINISHED) { |
| 519 | return overall; |
| 520 | } |
| 521 | throw new CancellationException(); |
| 522 | } |
| 523 | overall = new CompletedBatchOperation(success, failed); |
| 524 | return overall; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | private void cleanUpBatch() { |
| 529 | for (FlowRuleBatchEntry fbe : batches.values()) { |
| 530 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
| 531 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
| 532 | store.deleteFlowRule(fbe.getTarget()); |
| 533 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 534 | store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 535 | store.storeFlowRule(fbe.getTarget()); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 543 | |
| 544 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 545 | } |