Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.flow.impl; |
| 17 | |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | import java.util.Objects; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 22 | import java.util.Random; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 23 | import java.util.Set; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 24 | import java.util.concurrent.CompletableFuture; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 27 | import java.util.concurrent.ScheduledExecutorService; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 28 | import java.util.concurrent.TimeUnit; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 29 | import java.util.function.Supplier; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 31 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableList; |
| 33 | import com.google.common.collect.Iterables; |
| 34 | import com.google.common.collect.Maps; |
| 35 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 36 | import com.google.common.collect.Streams; |
| 37 | import org.apache.felix.scr.annotations.Activate; |
| 38 | import org.apache.felix.scr.annotations.Component; |
| 39 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 40 | import org.apache.felix.scr.annotations.Reference; |
| 41 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 42 | import org.apache.felix.scr.annotations.Service; |
| 43 | import org.onlab.util.KryoNamespace; |
| 44 | import org.onlab.util.Tools; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 45 | import org.onosproject.cluster.ClusterService; |
| 46 | import org.onosproject.cluster.NodeId; |
| 47 | import org.onosproject.core.CoreService; |
| 48 | import org.onosproject.core.IdGenerator; |
| 49 | import org.onosproject.mastership.MastershipService; |
| 50 | import org.onosproject.net.DeviceId; |
| 51 | import org.onosproject.net.device.DeviceService; |
| 52 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 53 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 54 | import org.onosproject.net.flow.FlowEntry; |
| 55 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 56 | import org.onosproject.net.flow.FlowId; |
| 57 | import org.onosproject.net.flow.FlowRule; |
| 58 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 59 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 60 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 61 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 62 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 63 | import org.onosproject.net.flow.FlowRuleEvent; |
| 64 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 65 | import org.onosproject.net.flow.FlowRuleService; |
| 66 | import org.onosproject.net.flow.FlowRuleStore; |
| 67 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 68 | import org.onosproject.net.flow.StoredFlowEntry; |
| 69 | import org.onosproject.net.flow.TableStatisticsEntry; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 70 | import org.onosproject.store.AbstractStore; |
| 71 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 72 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 73 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
| 74 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 75 | import org.onosproject.store.service.AsyncDocumentTree; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 76 | import org.onosproject.store.service.DocumentPath; |
| 77 | import org.onosproject.store.service.DocumentTree; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 78 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 79 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 80 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 81 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 82 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 83 | import org.onosproject.store.service.StorageException; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 84 | import org.onosproject.store.service.StorageService; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 85 | import org.onosproject.store.service.Versioned; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 86 | import org.onosproject.store.service.WallClockTimestamp; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 87 | import org.slf4j.Logger; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 88 | |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 89 | import static org.onlab.util.Tools.groupedThreads; |
| 90 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 91 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED; |
Yuta HIGUCHI | baaf8dc | 2017-06-21 17:44:09 -0700 | [diff] [blame] | 92 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Manages inventory of flow rules using a distributed state management protocol. |
| 96 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 97 | @Component(immediate = true) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 98 | @Service |
Madan Jampani | 37d04c6 | 2016-04-25 15:53:55 -0700 | [diff] [blame] | 99 | public class DistributedFlowRuleStore |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 100 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 101 | implements FlowRuleStore { |
| 102 | |
| 103 | private final Logger log = getLogger(getClass()); |
| 104 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 105 | // Constant exception used to indicate an atomic read-modify-write operation needs to be retried. |
| 106 | // We don't want to populate a stack trace every time an optimistic lock is retried. |
| 107 | private static final StorageException.ConcurrentModification RETRY; |
| 108 | |
| 109 | // Initialize retry exception with an empty stack trace. |
| 110 | static { |
| 111 | RETRY = new StorageException.ConcurrentModification(); |
| 112 | RETRY.setStackTrace(new StackTraceElement[0]); |
| 113 | } |
| 114 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 115 | private static final int SCHEDULED_THREAD_POOL_SIZE = 8; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 116 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 117 | private static final int MAX_RETRY_DELAY_MILLIS = 50; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 118 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 119 | private static final String FLOW_TABLE = "onos-flow-table"; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 120 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 121 | private static final MessageSubject APPLY_BATCH_FLOWS = new MessageSubject("onos-flow-apply"); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 122 | private static final MessageSubject COMPLETE_BATCH = new MessageSubject("onos-flow-batch-complete"); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected DeviceService deviceService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected CoreService coreService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 131 | protected MastershipService mastershipService; |
| 132 | |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 134 | protected ClusterCommunicationService clusterCommunicator; |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 135 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected ClusterService clusterService; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 138 | |
| 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 140 | protected StorageService storageService; |
| 141 | |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 142 | protected final Serializer serializer = Serializer.using(KryoNamespaces.API); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 143 | |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 144 | protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder() |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 145 | .register(KryoNamespaces.API) |
| 146 | .register(MastershipBasedTimestamp.class); |
| 147 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 148 | private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats; |
| 149 | private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener = |
| 150 | new InternalTableStatsListener(); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 151 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 152 | private Set<Long> pendingBatches = Sets.newConcurrentHashSet(); |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 153 | private ScheduledExecutorService scheduledExecutor; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 154 | private ExecutorService messageHandlingExecutor; |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 155 | private final Random random = new Random(); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 156 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 157 | private AsyncDocumentTree<Map<StoredFlowEntry, StoredFlowEntry>> asyncFlows; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 158 | private DocumentTree<Map<StoredFlowEntry, StoredFlowEntry>> flows; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 159 | private IdGenerator idGenerator; |
| 160 | private NodeId local; |
| 161 | |
| 162 | @Activate |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 163 | public void activate() { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 164 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 165 | |
| 166 | local = clusterService.getLocalNode().id(); |
| 167 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 168 | scheduledExecutor = Executors.newScheduledThreadPool( |
| 169 | SCHEDULED_THREAD_POOL_SIZE, |
| 170 | groupedThreads("onos/store/flow", "schedulers", log)); |
| 171 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 172 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 173 | MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 174 | groupedThreads("onos/store/flow", "message-handlers", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 175 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 176 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
| 177 | .withName("onos-flow-table-stats") |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 178 | .withSerializer(serializerBuilder) |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 179 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 180 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 181 | .withTombstonesDisabled() |
| 182 | .build(); |
| 183 | deviceTableStats.addListener(tableStatsListener); |
| 184 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 185 | asyncFlows = storageService.<Map<StoredFlowEntry, StoredFlowEntry>>documentTreeBuilder() |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 186 | .withName(FLOW_TABLE) |
| 187 | .withSerializer(serializer) |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 188 | .buildDocumentTree(); |
| 189 | flows = asyncFlows.asDocumentTree(); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 190 | |
| 191 | clusterCommunicator.addSubscriber( |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 192 | APPLY_BATCH_FLOWS, |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 193 | serializer::decode, |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 194 | this::applyBatchFlows, |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 195 | messageHandlingExecutor); |
| 196 | clusterCommunicator.addSubscriber( |
| 197 | COMPLETE_BATCH, |
| 198 | serializer::decode, |
| 199 | this::completeBatch, |
| 200 | messageHandlingExecutor); |
| 201 | |
| 202 | log.info("Started"); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Deactivate |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 206 | public void deactivate() { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 207 | deviceTableStats.removeListener(tableStatsListener); |
| 208 | deviceTableStats.destroy(); |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 209 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 210 | clusterCommunicator.removeSubscriber(COMPLETE_BATCH); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 211 | messageHandlingExecutor.shutdownNow(); |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 212 | scheduledExecutor.shutdownNow(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 213 | log.info("Stopped"); |
| 214 | } |
| 215 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Retries the given supplier until successful. |
| 218 | * <p> |
| 219 | * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In |
| 220 | * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes. |
| 221 | * |
| 222 | * @param supplier the supplier to retry |
| 223 | * @param <T> the return type |
| 224 | * @return the return value of the given supplier once it runs successfully |
| 225 | */ |
| 226 | private <T> T retryUntilSuccess(Supplier<T> supplier) { |
| 227 | return Tools.retryable( |
| 228 | supplier, |
| 229 | StorageException.ConcurrentModification.class, |
| 230 | Integer.MAX_VALUE, |
| 231 | MAX_RETRY_DELAY_MILLIS) |
| 232 | .get(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 235 | /** |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 236 | * Retries the given asynchronous supplier until successful. |
| 237 | * <p> |
| 238 | * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In |
| 239 | * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes. |
| 240 | * |
| 241 | * @param supplier the supplier to retry |
| 242 | * @param <T> the return type |
| 243 | * @return the return value of the given supplier once it runs successfully |
| 244 | */ |
| 245 | private <T> CompletableFuture<T> retryAsyncUntilSuccess(Supplier<CompletableFuture<T>> supplier) { |
| 246 | return retryAsyncUntilSuccess(supplier, new CompletableFuture<>()); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Retries the given asynchronous supplier until successful. |
| 251 | * <p> |
| 252 | * This method retries the given supplier until no {@code ConcurrentModification} exceptions are thrown. In |
| 253 | * between retries, it waits a semi-random interval to attempt to avoid transaction conflicts with other processes. |
| 254 | * |
| 255 | * @param supplier the supplier to retry |
| 256 | * @param future future to be completed once the operation has been successful |
| 257 | * @param <T> the return type |
| 258 | * @return the return value of the given supplier once it runs successfully |
| 259 | */ |
| 260 | private <T> CompletableFuture<T> retryAsyncUntilSuccess( |
| 261 | Supplier<CompletableFuture<T>> supplier, |
| 262 | CompletableFuture<T> future) { |
| 263 | supplier.get().whenComplete((result, error) -> { |
| 264 | if (error == null) { |
| 265 | future.complete(result); |
| 266 | } else { |
| 267 | Throwable cause = error.getCause() != null ? error.getCause() : error; |
| 268 | if (cause instanceof StorageException.ConcurrentModification) { |
| 269 | scheduledExecutor.schedule( |
| 270 | () -> retryAsyncUntilSuccess(supplier, future), |
| 271 | random.nextInt(50), |
| 272 | TimeUnit.MILLISECONDS); |
| 273 | } else { |
| 274 | future.completeExceptionally(error); |
| 275 | } |
| 276 | } |
| 277 | }); |
| 278 | return future; |
| 279 | } |
| 280 | |
| 281 | /** |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 282 | * Return method for {@link #retryUntilSuccess(Supplier)} callbacks to indicate that the callback needs to be |
| 283 | * retried after a randomized delay. |
| 284 | * |
| 285 | * @param <T> the return type |
| 286 | * @return nothing |
| 287 | * @throws StorageException.ConcurrentModification to force a retry of the callback |
| 288 | */ |
| 289 | private <T> T retry() { |
| 290 | throw RETRY; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 293 | /** |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 294 | * Handles a completed batch event received from the master node. |
| 295 | * <p> |
| 296 | * If this node is the source of the batch, notifies event listeners to complete the operations. |
| 297 | * |
| 298 | * @param event the event to handle |
| 299 | */ |
| 300 | private void completeBatch(FlowRuleBatchEvent event) { |
| 301 | if (pendingBatches.remove(event.subject().batchId())) { |
| 302 | notifyDelegate(event); |
| 303 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // This is not a efficient operation on a distributed sharded |
| 307 | // flow store. We need to revisit the need for this operation or at least |
| 308 | // make it device specific. |
| 309 | @Override |
| 310 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 10e91fb | 2017-06-15 13:30:50 -0700 | [diff] [blame] | 311 | return Streams.stream(deviceService.getDevices()).parallel() |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 312 | .mapToInt(device -> Iterables.size(getFlowEntries(device.id()))) |
| 313 | .sum(); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Returns the {@link DocumentPath} for the given {@link DeviceId}. |
| 318 | * |
| 319 | * @param deviceId the device identifier for which to return a path |
| 320 | * @return the path for the given device |
| 321 | */ |
| 322 | private DocumentPath getPathFor(DeviceId deviceId) { |
| 323 | return DocumentPath.from("root", deviceId.toString()); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Returns the {@link DocumentPath} for the given {@link DeviceId} and {@link FlowId}. |
| 328 | * |
| 329 | * @param deviceId the device identifier for which to return the path |
| 330 | * @param flowId the flow identifier for which to return the path |
| 331 | * @return the path for the given device/flow |
| 332 | */ |
| 333 | private DocumentPath getPathFor(DeviceId deviceId, FlowId flowId) { |
| 334 | return DocumentPath.from("root", deviceId.toString(), flowId.toString()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 338 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 339 | public FlowEntry getFlowEntry(FlowRule rule) { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 340 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 341 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> flowEntries = flows.get(path); |
| 342 | return flowEntries != null ? flowEntries.value().get(rule) : null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 347 | DocumentPath path = getPathFor(deviceId); |
| 348 | try { |
| 349 | return getFlowEntries(path); |
| 350 | } catch (NoSuchDocumentPathException e) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 351 | return Collections.emptyList(); |
| 352 | } |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 353 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 354 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 355 | @SuppressWarnings("unchecked") |
| 356 | private Iterable<FlowEntry> getFlowEntries(DocumentPath path) { |
| 357 | return flows.getChildren(path) |
| 358 | .values() |
| 359 | .stream() |
| 360 | .flatMap(v -> v.value().values().stream()) |
| 361 | .collect(Collectors.toList()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 365 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 366 | public void storeFlowRule(FlowRule rule) { |
| 367 | storeBatch(new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 368 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 369 | rule.deviceId(), idGenerator.getNewId())); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 374 | if (operation.getOperations().isEmpty()) { |
| 375 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 376 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 377 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 382 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 383 | |
| 384 | if (master == null) { |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 385 | log.warn("No master for {} ", deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 386 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 387 | updateStoreInternal(operation).whenComplete((result, error) -> { |
| 388 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 389 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 390 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 391 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 395 | pendingBatches.add(operation.id()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 396 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 397 | // If the local node is the master, apply the flows. Otherwise, send them to the master. |
| 398 | if (Objects.equals(local, master)) { |
| 399 | applyBatchFlows(operation); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 400 | } else { |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 401 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", master, deviceId); |
| 402 | clusterCommunicator.unicast( |
| 403 | operation, |
| 404 | APPLY_BATCH_FLOWS, |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 405 | serializer::encode, |
| 406 | master); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 407 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 410 | /** |
| 411 | * Asynchronously applies a batch of flows to the store. |
| 412 | * <p> |
| 413 | * This operation is performed on the master node to ensure that events occur <em>after</em> flows have been stored |
| 414 | * and are visible to the master node. If a non-master node stores flows and then triggers events on the master, |
| 415 | * the flows may not yet be visible to the master node due to the nature of sequentially consistent reads on the |
| 416 | * underlying {@code DocumentTree} primitive. |
| 417 | */ |
| 418 | private void applyBatchFlows(FlowRuleBatchOperation operation) { |
| 419 | updateStoreInternal(operation).whenComplete((operations, error) -> { |
| 420 | if (error == null) { |
| 421 | if (operations.isEmpty()) { |
| 422 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 423 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 424 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 425 | } else { |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 426 | notifyDelegate(FlowRuleBatchEvent.requested( |
| 427 | new FlowRuleBatchRequest(operation.id(), operations), |
| 428 | operation.deviceId())); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 429 | } |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 430 | } |
| 431 | }); |
| 432 | } |
| 433 | |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 434 | private CompletableFuture<Set<FlowRuleBatchEntry>> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 435 | return Tools.allOf(operation.getOperations().stream().map(op -> { |
| 436 | switch (op.operator()) { |
| 437 | case ADD: |
| 438 | return addBatchEntry(op).thenApply(succeeded -> succeeded ? op : null); |
| 439 | case REMOVE: |
| 440 | return removeBatchEntry(op).thenApply(succeeded -> succeeded ? op : null); |
| 441 | case MODIFY: |
| 442 | return CompletableFuture.<FlowRuleBatchEntry>completedFuture(null); |
| 443 | default: |
| 444 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 445 | return CompletableFuture.<FlowRuleBatchEntry>completedFuture(null); |
| 446 | } |
| 447 | }).collect(Collectors.toList())) |
| 448 | .thenApply(results -> results.stream() |
| 449 | .filter(Objects::nonNull) |
| 450 | .collect(Collectors.toSet())); |
| 451 | } |
| 452 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 453 | @SuppressWarnings("unchecked") |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 454 | private CompletableFuture<Boolean> addBatchEntry(FlowRuleBatchEntry batchEntry) { |
| 455 | StoredFlowEntry entry = new DefaultFlowEntry(batchEntry.target()); |
| 456 | DocumentPath path = getPathFor(entry.deviceId(), entry.id()); |
| 457 | return retryAsyncUntilSuccess(() -> { |
| 458 | CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 459 | asyncFlows.get(path).whenComplete((value, getError) -> { |
| 460 | if (getError == null) { |
| 461 | if (value != null) { |
| 462 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 463 | entries.put(entry, entry); |
| 464 | asyncFlows.replace(path, entries, value.version()).whenComplete((succeeded, replaceError) -> { |
| 465 | if (replaceError == null) { |
| 466 | if (succeeded) { |
| 467 | log.trace("Stored new flow rule: {}", entry); |
| 468 | future.complete(true); |
| 469 | } else { |
| 470 | log.trace("Failed to store new flow rule: {}", entry); |
| 471 | future.completeExceptionally(RETRY); |
| 472 | } |
| 473 | } else { |
| 474 | future.completeExceptionally(replaceError); |
| 475 | } |
| 476 | }); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 477 | } else { |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 478 | // If there are no entries stored for the device, initialize the device's flows. |
| 479 | Map<StoredFlowEntry, StoredFlowEntry> map = Maps.newHashMap(); |
| 480 | map.put(entry, entry); |
| 481 | asyncFlows.createRecursive(path, map).whenComplete((succeeded, createError) -> { |
| 482 | if (createError == null) { |
| 483 | if (succeeded) { |
| 484 | log.trace("Stored new flow rule: {}", entry); |
| 485 | future.complete(true); |
| 486 | } else { |
| 487 | log.trace("Failed to store new flow rule: {}", entry); |
| 488 | future.completeExceptionally(RETRY); |
| 489 | } |
| 490 | } else { |
| 491 | future.completeExceptionally(createError); |
| 492 | } |
| 493 | }); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 494 | } |
| 495 | } else { |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 496 | future.completeExceptionally(getError); |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 497 | } |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 498 | }); |
| 499 | return future; |
| 500 | }); |
| 501 | } |
| 502 | |
| 503 | @SuppressWarnings("unchecked") |
| 504 | private CompletableFuture<Boolean> removeBatchEntry(FlowRuleBatchEntry batchEntry) { |
| 505 | FlowRule rule = batchEntry.target(); |
| 506 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 507 | return retryAsyncUntilSuccess(() -> { |
| 508 | CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 509 | asyncFlows.get(path).whenComplete((value, getError) -> { |
| 510 | if (getError == null) { |
| 511 | if (value != null) { |
| 512 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 513 | StoredFlowEntry entry = entries.get(rule); |
| 514 | if (entry != null) { |
| 515 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 516 | asyncFlows.replace(path, entries, value.version()).whenComplete((succeeded, error) -> { |
| 517 | if (error == null) { |
| 518 | if (succeeded) { |
| 519 | log.trace("Updated flow rule state to PENDING_REMOVE: {}", entry); |
| 520 | future.complete(true); |
| 521 | } else { |
| 522 | log.trace("Failed to update flow rule state to PENDING_REMOVE: {}", entry); |
| 523 | future.completeExceptionally(RETRY); |
| 524 | } |
| 525 | } else { |
| 526 | future.completeExceptionally(error); |
| 527 | } |
| 528 | }); |
| 529 | } else { |
| 530 | future.complete(false); |
| 531 | } |
| 532 | } else { |
| 533 | future.complete(false); |
| 534 | } |
| 535 | } else { |
| 536 | future.completeExceptionally(getError); |
| 537 | } |
| 538 | }); |
| 539 | return future; |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 540 | }); |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 545 | if (pendingBatches.remove(event.subject().batchId())) { |
| 546 | notifyDelegate(event); |
| 547 | } else { |
| 548 | clusterCommunicator.broadcast(event, COMPLETE_BATCH, serializer::encode); |
| 549 | } |
| 550 | } |
| 551 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 552 | @Override |
| 553 | public void deleteFlowRule(FlowRule rule) { |
| 554 | storeBatch( |
| 555 | new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 556 | Collections.singletonList( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 557 | new FlowRuleBatchEntry( |
| 558 | FlowRuleOperation.REMOVE, |
| 559 | rule)), rule.deviceId(), idGenerator.getNewId())); |
| 560 | } |
| 561 | |
| 562 | @Override |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 563 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 564 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 565 | return retryUntilSuccess(() -> { |
| 566 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path); |
| 567 | if (value != null) { |
| 568 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 569 | StoredFlowEntry entry = entries.get(rule); |
| 570 | if (entry != null && entry.state() != FlowEntryState.PENDING_ADD) { |
| 571 | entry.setState(FlowEntryState.PENDING_ADD); |
| 572 | if (flows.replace(path, entries, value.version())) { |
| 573 | log.trace("Updated flow rule state to PENDING_ADD: {}", entry); |
| 574 | return new FlowRuleEvent(RULE_UPDATED, rule); |
| 575 | } else { |
| 576 | log.trace("Failed to update flow rule state to PENDING_ADD: {}", entry); |
| 577 | return retry(); |
| 578 | } |
| 579 | } else { |
| 580 | return null; |
| 581 | } |
| 582 | } else { |
| 583 | return null; |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 584 | } |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 585 | }); |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | @Override |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 589 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 590 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 591 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 592 | return retryUntilSuccess(() -> { |
| 593 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path); |
| 594 | if (value != null) { |
| 595 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 596 | StoredFlowEntry entry = entries.get(rule); |
| 597 | if (entry != null) { |
| 598 | FlowRuleEvent event; |
| 599 | String message; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 600 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 601 | entry.setBytes(rule.bytes()); |
| 602 | entry.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
| 603 | entry.setLiveType(rule.liveType()); |
| 604 | entry.setPackets(rule.packets()); |
| 605 | entry.setLastSeen(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 606 | |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 607 | // If the entry state is PENDING_ADD, set it to ADDED. Otherwise, just update the rule. |
| 608 | if (entry.state() == FlowEntryState.PENDING_ADD) { |
| 609 | entry.setState(FlowEntryState.ADDED); |
| 610 | event = new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 611 | message = "Updated flow rule state to ADDED: {}"; |
| 612 | } else { |
| 613 | event = new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 614 | message = "Updated flow rule: {}"; |
| 615 | } |
| 616 | |
| 617 | if (flows.replace(path, entries, value.version())) { |
| 618 | log.trace(message, entry); |
| 619 | return event; |
| 620 | } else { |
| 621 | log.trace("Failed to update flow rule: {}", entry); |
| 622 | return retry(); |
| 623 | } |
| 624 | } else { |
| 625 | // If the rule does not exist, return null. Inserting the rule risks race conditions |
| 626 | // that can result in removed rules being retained. |
| 627 | return null; |
| 628 | } |
| 629 | } else { |
Jordan Halterman | 9b3a7ce | 2017-08-25 15:12:54 -0700 | [diff] [blame] | 630 | return null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 631 | } |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 632 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | @Override |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 636 | @SuppressWarnings("unchecked") |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 637 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 638 | DocumentPath path = getPathFor(rule.deviceId(), rule.id()); |
| 639 | return retryUntilSuccess(() -> { |
| 640 | Versioned<Map<StoredFlowEntry, StoredFlowEntry>> value = flows.get(path); |
| 641 | if (value != null) { |
| 642 | Map<StoredFlowEntry, StoredFlowEntry> entries = Maps.newHashMap(value.value()); |
| 643 | StoredFlowEntry entry = entries.remove(rule); |
| 644 | if (entry != null) { |
| 645 | if (flows.replace(path, entries, value.version())) { |
| 646 | log.trace("Removed flow rule: {}", entry); |
| 647 | return new FlowRuleEvent(RULE_REMOVED, entry); |
| 648 | } else { |
| 649 | log.trace("Failed to remove flow rule: {}", entry); |
| 650 | return retry(); |
| 651 | } |
| 652 | } else { |
| 653 | return null; |
| 654 | } |
| 655 | } else { |
| 656 | return null; |
| 657 | } |
| 658 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 662 | public void purgeFlowRule(DeviceId deviceId) { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 663 | DocumentPath path = getPathFor(deviceId); |
| 664 | try { |
| 665 | for (String flowId : flows.getChildren(path).keySet()) { |
| 666 | flows.removeNode(DocumentPath.from("root", deviceId.toString(), flowId)); |
| 667 | } |
| 668 | } catch (NoSuchDocumentPathException e) { |
| 669 | // Do nothing |
| 670 | } |
| 671 | try { |
| 672 | flows.removeNode(path); |
| 673 | } catch (NoSuchDocumentPathException e) { |
| 674 | // Do nothing |
| 675 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Override |
Victor Silva | 139bca4 | 2016-08-18 11:46:35 -0300 | [diff] [blame] | 679 | public void purgeFlowRules() { |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 680 | try { |
| 681 | for (String deviceId : flows.getChildren(flows.root()).keySet()) { |
| 682 | purgeFlowRule(DeviceId.deviceId(deviceId)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 683 | } |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 684 | } catch (NoSuchDocumentPathException e) { |
| 685 | // Do nothing |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 688 | |
| 689 | @Override |
| 690 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 691 | List<TableStatisticsEntry> tableStats) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 692 | deviceTableStats.put(deviceId, tableStats); |
| 693 | return null; |
| 694 | } |
| 695 | |
| 696 | @Override |
| 697 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 698 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 699 | if (tableStats == null) { |
| 700 | return Collections.emptyList(); |
| 701 | } |
| 702 | return ImmutableList.copyOf(tableStats); |
| 703 | } |
| 704 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 705 | @Override |
| 706 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 707 | return Streams.stream(getTableStatistics(deviceId)) |
| 708 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 709 | .sum(); |
| 710 | } |
| 711 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 712 | private class InternalTableStatsListener |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 713 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 714 | @Override |
| 715 | public void event(EventuallyConsistentMapEvent<DeviceId, |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 716 | List<TableStatisticsEntry>> event) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 717 | //TODO: Generate an event to listeners (do we need?) |
| 718 | } |
| 719 | } |
Jordan Halterman | f755409 | 2017-07-30 15:05:51 -0700 | [diff] [blame] | 720 | } |