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