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