Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 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 | |
| 18 | import java.util.Collections; |
| 19 | import java.util.Dictionary; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
| 27 | import java.util.concurrent.ScheduledExecutorService; |
| 28 | import java.util.concurrent.ScheduledFuture; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | import java.util.concurrent.atomic.AtomicReference; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
| 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; |
| 37 | import org.apache.felix.scr.annotations.Modified; |
| 38 | import org.apache.felix.scr.annotations.Property; |
| 39 | import org.apache.felix.scr.annotations.Reference; |
| 40 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 41 | import org.apache.felix.scr.annotations.Service; |
| 42 | import org.onlab.util.KryoNamespace; |
| 43 | import org.onlab.util.Tools; |
| 44 | import org.onosproject.cfg.ComponentConfigService; |
| 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.oldbatch.FlowRuleBatchEntry; |
| 59 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation; |
| 60 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 61 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 62 | import org.onosproject.net.flow.oldbatch.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; |
| 70 | import org.onosproject.persistence.PersistenceService; |
| 71 | import org.onosproject.store.AbstractStore; |
| 72 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 73 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 74 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 75 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 76 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 77 | import org.onosproject.store.flow.ReplicaInfoService; |
| 78 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
| 79 | import org.onosproject.store.serializers.KryoNamespaces; |
| 80 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 81 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 82 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 83 | import org.onosproject.store.service.Serializer; |
| 84 | import org.onosproject.store.service.StorageService; |
| 85 | import org.onosproject.store.service.WallClockTimestamp; |
| 86 | import org.osgi.service.component.ComponentContext; |
| 87 | import org.slf4j.Logger; |
| 88 | |
| 89 | import com.google.common.collect.ImmutableList; |
| 90 | import com.google.common.collect.Iterables; |
| 91 | import com.google.common.collect.Maps; |
| 92 | import com.google.common.collect.Sets; |
| 93 | import com.google.common.util.concurrent.Futures; |
| 94 | |
| 95 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 96 | import static org.onlab.util.Tools.get; |
| 97 | import static org.onlab.util.Tools.groupedThreads; |
| 98 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 99 | import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED; |
| 100 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.APPLY_BATCH_FLOWS; |
| 101 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.FLOW_TABLE_BACKUP; |
| 102 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES; |
| 103 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY; |
| 104 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.REMOTE_APPLY_COMPLETED; |
| 105 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.REMOVE_FLOW_ENTRY; |
| 106 | import static org.slf4j.LoggerFactory.getLogger; |
| 107 | |
| 108 | /** |
| 109 | * Manages inventory of flow rules using a distributed state management protocol. |
| 110 | */ |
Thomas Vachuska | 71026b2 | 2018-01-05 16:01:44 -0800 | [diff] [blame] | 111 | @Component(immediate = true) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 112 | @Service |
| 113 | public class ECFlowRuleStore |
| 114 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 115 | implements FlowRuleStore { |
| 116 | |
| 117 | private final Logger log = getLogger(getClass()); |
| 118 | |
| 119 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; |
| 120 | private static final int DEFAULT_MAX_BACKUP_COUNT = 2; |
| 121 | private static final boolean DEFAULT_PERSISTENCE_ENABLED = false; |
| 122 | private static final int DEFAULT_BACKUP_PERIOD_MILLIS = 2000; |
| 123 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
| 124 | // number of devices whose flow entries will be backed up in one communication round |
| 125 | private static final int FLOW_TABLE_BACKUP_BATCH_SIZE = 1; |
| 126 | |
| 127 | @Property(name = "msgHandlerPoolSize", intValue = MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 128 | label = "Number of threads in the message handler pool") |
| 129 | private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 130 | |
| 131 | @Property(name = "backupPeriod", intValue = DEFAULT_BACKUP_PERIOD_MILLIS, |
| 132 | label = "Delay in ms between successive backup runs") |
| 133 | private int backupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
| 134 | @Property(name = "persistenceEnabled", boolValue = false, |
| 135 | label = "Indicates whether or not changes in the flow table should be persisted to disk.") |
| 136 | private boolean persistenceEnabled = DEFAULT_PERSISTENCE_ENABLED; |
| 137 | |
| 138 | @Property(name = "backupCount", intValue = DEFAULT_MAX_BACKUP_COUNT, |
| 139 | label = "Max number of backup copies for each device") |
| 140 | private volatile int backupCount = DEFAULT_MAX_BACKUP_COUNT; |
| 141 | |
| 142 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 143 | |
| 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 145 | protected ReplicaInfoService replicaInfoManager; |
| 146 | |
| 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 148 | protected ClusterCommunicationService clusterCommunicator; |
| 149 | |
| 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 151 | protected ClusterService clusterService; |
| 152 | |
| 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 154 | protected DeviceService deviceService; |
| 155 | |
| 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 157 | protected CoreService coreService; |
| 158 | |
| 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 160 | protected ComponentConfigService configService; |
| 161 | |
| 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 163 | protected MastershipService mastershipService; |
| 164 | |
| 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 166 | protected PersistenceService persistenceService; |
| 167 | |
| 168 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
| 169 | private ExecutorService messageHandlingExecutor; |
| 170 | private ExecutorService eventHandler; |
| 171 | |
| 172 | private ScheduledFuture<?> backupTask; |
| 173 | private final ScheduledExecutorService backupSenderExecutor = |
| 174 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/flow", "backup-sender", log)); |
| 175 | |
| 176 | private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats; |
| 177 | private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener = |
| 178 | new InternalTableStatsListener(); |
| 179 | |
| 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 181 | protected StorageService storageService; |
| 182 | |
| 183 | protected final Serializer serializer = Serializer.using(KryoNamespaces.API); |
| 184 | |
| 185 | protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder() |
| 186 | .register(KryoNamespaces.API) |
| 187 | .register(MastershipBasedTimestamp.class); |
| 188 | |
| 189 | |
| 190 | private IdGenerator idGenerator; |
| 191 | private NodeId local; |
| 192 | |
| 193 | @Activate |
| 194 | public void activate(ComponentContext context) { |
| 195 | configService.registerProperties(getClass()); |
| 196 | |
| 197 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 198 | |
| 199 | local = clusterService.getLocalNode().id(); |
| 200 | |
| 201 | eventHandler = Executors.newSingleThreadExecutor( |
| 202 | groupedThreads("onos/flow", "event-handler", log)); |
| 203 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 204 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
| 205 | |
| 206 | registerMessageHandlers(messageHandlingExecutor); |
| 207 | |
| 208 | replicaInfoManager.addListener(flowTable); |
| 209 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 210 | flowTable::backup, |
| 211 | 0, |
| 212 | backupPeriod, |
| 213 | TimeUnit.MILLISECONDS); |
| 214 | |
| 215 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
| 216 | .withName("onos-flow-table-stats") |
| 217 | .withSerializer(serializerBuilder) |
| 218 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 219 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 220 | .withTombstonesDisabled() |
| 221 | .build(); |
| 222 | deviceTableStats.addListener(tableStatsListener); |
| 223 | |
| 224 | logConfig("Started"); |
| 225 | } |
| 226 | |
| 227 | @Deactivate |
| 228 | public void deactivate(ComponentContext context) { |
| 229 | replicaInfoManager.removeListener(flowTable); |
| 230 | backupTask.cancel(true); |
| 231 | configService.unregisterProperties(getClass(), false); |
| 232 | unregisterMessageHandlers(); |
| 233 | deviceTableStats.removeListener(tableStatsListener); |
| 234 | deviceTableStats.destroy(); |
| 235 | eventHandler.shutdownNow(); |
| 236 | messageHandlingExecutor.shutdownNow(); |
| 237 | backupSenderExecutor.shutdownNow(); |
| 238 | log.info("Stopped"); |
| 239 | } |
| 240 | |
| 241 | @SuppressWarnings("rawtypes") |
| 242 | @Modified |
| 243 | public void modified(ComponentContext context) { |
| 244 | if (context == null) { |
| 245 | logConfig("Default config"); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | Dictionary properties = context.getProperties(); |
| 250 | int newPoolSize; |
| 251 | int newBackupPeriod; |
| 252 | int newBackupCount; |
| 253 | try { |
| 254 | String s = get(properties, "msgHandlerPoolSize"); |
| 255 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 256 | |
| 257 | s = get(properties, "backupPeriod"); |
| 258 | newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim()); |
| 259 | |
| 260 | s = get(properties, "backupCount"); |
| 261 | newBackupCount = isNullOrEmpty(s) ? backupCount : Integer.parseInt(s.trim()); |
| 262 | } catch (NumberFormatException | ClassCastException e) { |
| 263 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 264 | newBackupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
| 265 | newBackupCount = DEFAULT_MAX_BACKUP_COUNT; |
| 266 | } |
| 267 | |
| 268 | boolean restartBackupTask = false; |
| 269 | |
| 270 | if (newBackupPeriod != backupPeriod) { |
| 271 | backupPeriod = newBackupPeriod; |
| 272 | restartBackupTask = true; |
| 273 | } |
| 274 | if (restartBackupTask) { |
| 275 | if (backupTask != null) { |
| 276 | // cancel previously running task |
| 277 | backupTask.cancel(false); |
| 278 | } |
| 279 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 280 | flowTable::backup, |
| 281 | 0, |
| 282 | backupPeriod, |
| 283 | TimeUnit.MILLISECONDS); |
| 284 | } |
| 285 | if (newPoolSize != msgHandlerPoolSize) { |
| 286 | msgHandlerPoolSize = newPoolSize; |
| 287 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 288 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 289 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
| 290 | |
| 291 | // replace previously registered handlers. |
| 292 | registerMessageHandlers(messageHandlingExecutor); |
| 293 | oldMsgHandler.shutdown(); |
| 294 | } |
| 295 | if (backupCount != newBackupCount) { |
| 296 | backupCount = newBackupCount; |
| 297 | } |
| 298 | logConfig("Reconfigured"); |
| 299 | } |
| 300 | |
| 301 | private void registerMessageHandlers(ExecutorService executor) { |
| 302 | |
| 303 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(), executor); |
| 304 | clusterCommunicator.<FlowRuleBatchEvent>addSubscriber( |
| 305 | REMOTE_APPLY_COMPLETED, serializer::decode, this::notifyDelegate, executor); |
| 306 | clusterCommunicator.addSubscriber( |
| 307 | GET_FLOW_ENTRY, serializer::decode, flowTable::getFlowEntry, serializer::encode, executor); |
| 308 | clusterCommunicator.addSubscriber( |
| 309 | GET_DEVICE_FLOW_ENTRIES, serializer::decode, flowTable::getFlowEntries, serializer::encode, executor); |
| 310 | clusterCommunicator.addSubscriber( |
| 311 | REMOVE_FLOW_ENTRY, serializer::decode, this::removeFlowRuleInternal, serializer::encode, executor); |
| 312 | clusterCommunicator.addSubscriber( |
| 313 | FLOW_TABLE_BACKUP, serializer::decode, flowTable::onBackupReceipt, serializer::encode, executor); |
| 314 | } |
| 315 | |
| 316 | private void unregisterMessageHandlers() { |
| 317 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
| 318 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_ENTRIES); |
| 319 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 320 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
| 321 | clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED); |
| 322 | clusterCommunicator.removeSubscriber(FLOW_TABLE_BACKUP); |
| 323 | } |
| 324 | |
| 325 | private void logConfig(String prefix) { |
| 326 | log.info("{} with msgHandlerPoolSize = {}; backupPeriod = {}, backupCount = {}", |
| 327 | prefix, msgHandlerPoolSize, backupPeriod, backupCount); |
| 328 | } |
| 329 | |
| 330 | // This is not a efficient operation on a distributed sharded |
| 331 | // flow store. We need to revisit the need for this operation or at least |
| 332 | // make it device specific. |
| 333 | @Override |
| 334 | public int getFlowRuleCount() { |
| 335 | return Streams.stream(deviceService.getDevices()).parallel() |
| 336 | .mapToInt(device -> Iterables.size(getFlowEntries(device.id()))) |
| 337 | .sum(); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public FlowEntry getFlowEntry(FlowRule rule) { |
| 342 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 343 | |
| 344 | if (master == null) { |
| 345 | log.debug("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
| 346 | return null; |
| 347 | } |
| 348 | |
| 349 | if (Objects.equals(local, master)) { |
| 350 | return flowTable.getFlowEntry(rule); |
| 351 | } |
| 352 | |
| 353 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 354 | master, rule.deviceId()); |
| 355 | |
| 356 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(rule, |
| 357 | ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY, |
| 358 | serializer::encode, |
| 359 | serializer::decode, |
| 360 | master), |
| 361 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 362 | TimeUnit.MILLISECONDS, |
| 363 | null); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 368 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 369 | |
| 370 | if (master == null) { |
| 371 | log.debug("Failed to getFlowEntries: No master for {}", deviceId); |
| 372 | return Collections.emptyList(); |
| 373 | } |
| 374 | |
| 375 | if (Objects.equals(local, master)) { |
| 376 | return flowTable.getFlowEntries(deviceId); |
| 377 | } |
| 378 | |
| 379 | log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
| 380 | master, deviceId); |
| 381 | |
| 382 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(deviceId, |
| 383 | ECFlowRuleStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES, |
| 384 | serializer::encode, |
| 385 | serializer::decode, |
| 386 | master), |
| 387 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 388 | TimeUnit.MILLISECONDS, |
| 389 | Collections.emptyList()); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void storeFlowRule(FlowRule rule) { |
| 394 | storeBatch(new FlowRuleBatchOperation( |
| 395 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
| 396 | rule.deviceId(), idGenerator.getNewId())); |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 401 | if (operation.getOperations().isEmpty()) { |
| 402 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 403 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 404 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | DeviceId deviceId = operation.deviceId(); |
| 409 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 410 | |
| 411 | if (master == null) { |
| 412 | log.warn("No master for {} ", deviceId); |
| 413 | |
| 414 | updateStoreInternal(operation); |
| 415 | |
| 416 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 417 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 418 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | if (Objects.equals(local, master)) { |
| 423 | storeBatchInternal(operation); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 428 | master, deviceId); |
| 429 | |
| 430 | clusterCommunicator.unicast(operation, |
| 431 | APPLY_BATCH_FLOWS, |
| 432 | serializer::encode, |
| 433 | master) |
| 434 | .whenComplete((result, error) -> { |
| 435 | if (error != null) { |
| 436 | log.warn("Failed to storeBatch: {} to {}", operation, master, error); |
| 437 | |
| 438 | Set<FlowRule> allFailures = operation.getOperations() |
| 439 | .stream() |
| 440 | .map(op -> op.target()) |
| 441 | .collect(Collectors.toSet()); |
| 442 | |
| 443 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 444 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 445 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 446 | } |
| 447 | }); |
| 448 | } |
| 449 | |
| 450 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
| 451 | |
| 452 | final DeviceId did = operation.deviceId(); |
| 453 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
| 454 | Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation); |
| 455 | if (currentOps.isEmpty()) { |
| 456 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 457 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 458 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | notifyDelegate(FlowRuleBatchEvent.requested(new |
| 463 | FlowRuleBatchRequest(operation.id(), |
| 464 | currentOps), operation.deviceId())); |
| 465 | } |
| 466 | |
| 467 | private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 468 | return operation.getOperations().stream().map( |
| 469 | op -> { |
| 470 | StoredFlowEntry entry; |
| 471 | switch (op.operator()) { |
| 472 | case ADD: |
Thomas Vachuska | 914b0b1 | 2018-01-09 11:54:52 -0800 | [diff] [blame] | 473 | case MODIFY: |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 474 | entry = new DefaultFlowEntry(op.target()); |
| 475 | // always add requested FlowRule |
| 476 | // Note: 2 equal FlowEntry may have different treatment |
| 477 | flowTable.remove(entry.deviceId(), entry); |
| 478 | flowTable.add(entry); |
| 479 | |
| 480 | return op; |
| 481 | case REMOVE: |
| 482 | entry = flowTable.getFlowEntry(op.target()); |
| 483 | if (entry != null) { |
| 484 | //FIXME modification of "stored" flow entry outside of flow table |
| 485 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 486 | log.debug("Setting state of rule to pending remove: {}", entry); |
| 487 | return op; |
| 488 | } |
| 489 | break; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 490 | default: |
| 491 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 492 | } |
| 493 | return null; |
| 494 | } |
| 495 | ).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void deleteFlowRule(FlowRule rule) { |
| 500 | storeBatch( |
| 501 | new FlowRuleBatchOperation( |
| 502 | Collections.singletonList( |
| 503 | new FlowRuleBatchEntry( |
| 504 | FlowRuleOperation.REMOVE, |
| 505 | rule)), rule.deviceId(), idGenerator.getNewId())); |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
| 510 | if (mastershipService.isLocalMaster(rule.deviceId())) { |
| 511 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 512 | if (stored != null && |
| 513 | stored.state() != FlowEntryState.PENDING_ADD) { |
| 514 | stored.setState(FlowEntryState.PENDING_ADD); |
| 515 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 516 | } |
| 517 | } |
| 518 | return null; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 523 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 524 | if (Objects.equals(local, master)) { |
| 525 | return addOrUpdateFlowRuleInternal(rule); |
| 526 | } |
| 527 | |
| 528 | log.warn("Tried to update FlowRule {} state," |
| 529 | + " while the Node was not the master.", rule); |
| 530 | return null; |
| 531 | } |
| 532 | |
| 533 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
| 534 | // check if this new rule is an update to an existing entry |
| 535 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 536 | if (stored != null) { |
| 537 | //FIXME modification of "stored" flow entry outside of flow table |
| 538 | stored.setBytes(rule.bytes()); |
| 539 | stored.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
| 540 | stored.setLiveType(rule.liveType()); |
| 541 | stored.setPackets(rule.packets()); |
| 542 | stored.setLastSeen(); |
| 543 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 544 | stored.setState(FlowEntryState.ADDED); |
| 545 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 546 | } |
| 547 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 548 | } |
| 549 | |
| 550 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 551 | // TODO: also update backup if the behavior is correct. |
| 552 | flowTable.add(rule); |
| 553 | return null; |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 558 | final DeviceId deviceId = rule.deviceId(); |
| 559 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 560 | |
| 561 | if (Objects.equals(local, master)) { |
| 562 | // bypass and handle it locally |
| 563 | return removeFlowRuleInternal(rule); |
| 564 | } |
| 565 | |
| 566 | if (master == null) { |
| 567 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
| 568 | // TODO: revisit if this should be null (="no-op") or Exception |
| 569 | return null; |
| 570 | } |
| 571 | |
| 572 | log.trace("Forwarding removeFlowRule to {}, which is the master for device {}", |
| 573 | master, deviceId); |
| 574 | |
| 575 | return Futures.getUnchecked(clusterCommunicator.sendAndReceive( |
| 576 | rule, |
| 577 | REMOVE_FLOW_ENTRY, |
| 578 | serializer::encode, |
| 579 | serializer::decode, |
| 580 | master)); |
| 581 | } |
| 582 | |
| 583 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
| 584 | final DeviceId deviceId = rule.deviceId(); |
| 585 | // This is where one could mark a rule as removed and still keep it in the store. |
| 586 | final FlowEntry removed = flowTable.remove(deviceId, rule); |
| 587 | // rule may be partial rule that is missing treatment, we should use rule from store instead |
| 588 | return removed != null ? new FlowRuleEvent(RULE_REMOVED, removed) : null; |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public void purgeFlowRule(DeviceId deviceId) { |
| 593 | flowTable.purgeFlowRule(deviceId); |
| 594 | } |
| 595 | |
| 596 | @Override |
| 597 | public void purgeFlowRules() { |
| 598 | flowTable.purgeFlowRules(); |
| 599 | } |
| 600 | |
| 601 | @Override |
| 602 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 603 | //FIXME: need a per device pending response |
| 604 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 605 | if (nodeId == null) { |
| 606 | notifyDelegate(event); |
| 607 | } else { |
| 608 | // TODO check unicast return value |
| 609 | clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, serializer::encode, nodeId); |
| 610 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 615 | |
| 616 | @Override |
| 617 | public void handle(final ClusterMessage message) { |
| 618 | FlowRuleBatchOperation operation = serializer.decode(message.payload()); |
| 619 | log.debug("received batch request {}", operation); |
| 620 | |
| 621 | final DeviceId deviceId = operation.deviceId(); |
| 622 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 623 | if (!Objects.equals(local, master)) { |
| 624 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 625 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 626 | failures.add(op.target()); |
| 627 | } |
| 628 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
| 629 | // This node is no longer the master, respond as all failed. |
| 630 | // TODO: we might want to wrap response in envelope |
| 631 | // to distinguish sw programming failure and hand over |
| 632 | // it make sense in the latter case to retry immediately. |
| 633 | message.respond(serializer.encode(allFailed)); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | pendingResponses.put(operation.id(), message.sender()); |
| 638 | storeBatchInternal(operation); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | private class BackupOperation { |
| 643 | private final NodeId nodeId; |
| 644 | private final DeviceId deviceId; |
| 645 | |
| 646 | public BackupOperation(NodeId nodeId, DeviceId deviceId) { |
| 647 | this.nodeId = nodeId; |
| 648 | this.deviceId = deviceId; |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public int hashCode() { |
| 653 | return Objects.hash(nodeId, deviceId); |
| 654 | } |
| 655 | |
| 656 | @Override |
| 657 | public boolean equals(Object other) { |
| 658 | if (other != null && other instanceof BackupOperation) { |
| 659 | BackupOperation that = (BackupOperation) other; |
| 660 | return this.nodeId.equals(that.nodeId) && |
| 661 | this.deviceId.equals(that.deviceId); |
| 662 | } else { |
| 663 | return false; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | private class InternalFlowTable implements ReplicaInfoEventListener { |
| 669 | |
| 670 | //TODO replace the Map<V,V> with ExtendedSet |
| 671 | private final Map<DeviceId, Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> |
| 672 | flowEntries = Maps.newConcurrentMap(); |
| 673 | |
| 674 | private final Map<BackupOperation, Long> lastBackupTimes = Maps.newConcurrentMap(); |
| 675 | private final Map<DeviceId, Long> lastUpdateTimes = Maps.newConcurrentMap(); |
| 676 | |
| 677 | @Override |
| 678 | public void event(ReplicaInfoEvent event) { |
| 679 | eventHandler.execute(() -> handleEvent(event)); |
| 680 | } |
| 681 | |
| 682 | private void handleEvent(ReplicaInfoEvent event) { |
| 683 | DeviceId deviceId = event.subject(); |
| 684 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 685 | return; |
| 686 | } |
| 687 | if (event.type() == MASTER_CHANGED) { |
| 688 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
| 689 | } |
| 690 | backupSenderExecutor.schedule(this::backup, 0, TimeUnit.SECONDS); |
| 691 | } |
| 692 | |
| 693 | private void sendBackups(NodeId nodeId, Set<DeviceId> deviceIds) { |
| 694 | // split up the devices into smaller batches and send them separately. |
| 695 | Iterables.partition(deviceIds, FLOW_TABLE_BACKUP_BATCH_SIZE) |
| 696 | .forEach(ids -> backupFlowEntries(nodeId, Sets.newHashSet(ids))); |
| 697 | } |
| 698 | |
| 699 | private void backupFlowEntries(NodeId nodeId, Set<DeviceId> deviceIds) { |
| 700 | if (deviceIds.isEmpty()) { |
| 701 | return; |
| 702 | } |
| 703 | log.debug("Sending flowEntries for devices {} to {} for backup.", deviceIds, nodeId); |
| 704 | Map<DeviceId, Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> |
| 705 | deviceFlowEntries = Maps.newConcurrentMap(); |
| 706 | deviceIds.forEach(id -> deviceFlowEntries.put(id, getFlowTableCopy(id))); |
| 707 | clusterCommunicator.<Map<DeviceId, |
| 708 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>>, |
| 709 | Set<DeviceId>> |
| 710 | sendAndReceive(deviceFlowEntries, |
| 711 | FLOW_TABLE_BACKUP, |
| 712 | serializer::encode, |
| 713 | serializer::decode, |
| 714 | nodeId) |
| 715 | .whenComplete((backedupDevices, error) -> { |
| 716 | Set<DeviceId> devicesNotBackedup = error != null ? |
| 717 | deviceFlowEntries.keySet() : |
| 718 | Sets.difference(deviceFlowEntries.keySet(), backedupDevices); |
| 719 | if (devicesNotBackedup.size() > 0) { |
| 720 | log.warn("Failed to backup devices: {}. Reason: {}, Node: {}", |
| 721 | devicesNotBackedup, error != null ? error.getMessage() : "none", |
| 722 | nodeId); |
| 723 | } |
| 724 | if (backedupDevices != null) { |
| 725 | backedupDevices.forEach(id -> { |
| 726 | lastBackupTimes.put(new BackupOperation(nodeId, id), System.currentTimeMillis()); |
| 727 | }); |
| 728 | } |
| 729 | }); |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Returns the flow table for specified device. |
| 734 | * |
| 735 | * @param deviceId identifier of the device |
| 736 | * @return Map representing Flow Table of given device. |
| 737 | */ |
| 738 | private Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
| 739 | if (persistenceEnabled) { |
| 740 | return flowEntries.computeIfAbsent(deviceId, id -> persistenceService |
| 741 | .<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>persistentMapBuilder() |
| 742 | .withName("FlowTable:" + deviceId.toString()) |
| 743 | .withSerializer(new Serializer() { |
| 744 | @Override |
| 745 | public <T> byte[] encode(T object) { |
| 746 | return serializer.encode(object); |
| 747 | } |
| 748 | |
| 749 | @Override |
| 750 | public <T> T decode(byte[] bytes) { |
| 751 | return serializer.decode(bytes); |
| 752 | } |
| 753 | |
| 754 | @Override |
| 755 | public <T> T copy(T object) { |
| 756 | return serializer.copy(object); |
| 757 | } |
| 758 | }) |
| 759 | .build()); |
| 760 | } else { |
| 761 | return flowEntries.computeIfAbsent(deviceId, id -> Maps.newConcurrentMap()); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | private Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> getFlowTableCopy(DeviceId deviceId) { |
| 766 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> copy = Maps.newHashMap(); |
| 767 | if (persistenceEnabled) { |
| 768 | return flowEntries.computeIfAbsent(deviceId, id -> persistenceService |
| 769 | .<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>persistentMapBuilder() |
| 770 | .withName("FlowTable:" + deviceId.toString()) |
| 771 | .withSerializer(new Serializer() { |
| 772 | @Override |
| 773 | public <T> byte[] encode(T object) { |
| 774 | return serializer.encode(object); |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public <T> T decode(byte[] bytes) { |
| 779 | return serializer.decode(bytes); |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public <T> T copy(T object) { |
| 784 | return serializer.copy(object); |
| 785 | } |
| 786 | }) |
| 787 | .build()); |
| 788 | } else { |
| 789 | flowEntries.computeIfAbsent(deviceId, id -> Maps.newConcurrentMap()).forEach((k, v) -> { |
| 790 | copy.put(k, Maps.newHashMap(v)); |
| 791 | }); |
| 792 | return copy; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | private Map<StoredFlowEntry, StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) { |
| 797 | return getFlowTable(deviceId).computeIfAbsent(flowId, id -> Maps.newConcurrentMap()); |
| 798 | } |
| 799 | |
| 800 | private StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 801 | return getFlowEntriesInternal(rule.deviceId(), rule.id()).get(rule); |
| 802 | } |
| 803 | |
| 804 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
| 805 | return getFlowTable(deviceId).values().stream() |
| 806 | .flatMap(m -> m.values().stream()) |
| 807 | .collect(Collectors.toSet()); |
| 808 | } |
| 809 | |
| 810 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
| 811 | return getFlowEntryInternal(rule); |
| 812 | } |
| 813 | |
| 814 | public Set<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 815 | return getFlowEntriesInternal(deviceId); |
| 816 | } |
| 817 | |
| 818 | public void add(FlowEntry rule) { |
| 819 | getFlowEntriesInternal(rule.deviceId(), rule.id()) |
| 820 | .compute((StoredFlowEntry) rule, (k, stored) -> { |
| 821 | //TODO compare stored and rule timestamps |
| 822 | //TODO the key is not updated |
| 823 | return (StoredFlowEntry) rule; |
| 824 | }); |
| 825 | lastUpdateTimes.put(rule.deviceId(), System.currentTimeMillis()); |
| 826 | } |
| 827 | |
| 828 | public FlowEntry remove(DeviceId deviceId, FlowEntry rule) { |
| 829 | final AtomicReference<FlowEntry> removedRule = new AtomicReference<>(); |
| 830 | getFlowEntriesInternal(rule.deviceId(), rule.id()) |
| 831 | .computeIfPresent((StoredFlowEntry) rule, (k, stored) -> { |
| 832 | if (rule instanceof DefaultFlowEntry) { |
| 833 | DefaultFlowEntry toRemove = (DefaultFlowEntry) rule; |
| 834 | if (stored instanceof DefaultFlowEntry) { |
| 835 | DefaultFlowEntry storedEntry = (DefaultFlowEntry) stored; |
| 836 | if (toRemove.created() < storedEntry.created()) { |
| 837 | log.debug("Trying to remove more recent flow entry {} (stored: {})", |
| 838 | toRemove, stored); |
| 839 | // the key is not updated, removedRule remains null |
| 840 | return stored; |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | removedRule.set(stored); |
| 845 | return null; |
| 846 | }); |
| 847 | |
| 848 | if (removedRule.get() != null) { |
| 849 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
| 850 | return removedRule.get(); |
| 851 | } else { |
| 852 | return null; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | public void purgeFlowRule(DeviceId deviceId) { |
| 857 | flowEntries.remove(deviceId); |
| 858 | } |
| 859 | |
| 860 | public void purgeFlowRules() { |
| 861 | flowEntries.clear(); |
| 862 | } |
| 863 | |
| 864 | private List<NodeId> getBackupNodes(DeviceId deviceId) { |
| 865 | // The returned backup node list is in the order of preference i.e. next likely master first. |
| 866 | List<NodeId> allPossibleBackupNodes = replicaInfoManager.getReplicaInfoFor(deviceId).backups(); |
| 867 | return ImmutableList.copyOf(allPossibleBackupNodes) |
| 868 | .subList(0, Math.min(allPossibleBackupNodes.size(), backupCount)); |
| 869 | } |
| 870 | |
| 871 | private void backup() { |
| 872 | try { |
| 873 | // compute a mapping from node to the set of devices whose flow entries it should backup |
| 874 | Map<NodeId, Set<DeviceId>> devicesToBackupByNode = Maps.newHashMap(); |
| 875 | flowEntries.keySet().forEach(deviceId -> { |
| 876 | List<NodeId> backupNodes = getBackupNodes(deviceId); |
| 877 | backupNodes.forEach(backupNode -> { |
| 878 | if (lastBackupTimes.getOrDefault(new BackupOperation(backupNode, deviceId), 0L) |
| 879 | < lastUpdateTimes.getOrDefault(deviceId, 0L)) { |
| 880 | devicesToBackupByNode.computeIfAbsent(backupNode, |
| 881 | nodeId -> Sets.newHashSet()).add(deviceId); |
| 882 | } |
| 883 | }); |
| 884 | }); |
| 885 | // send the device flow entries to their respective backup nodes |
| 886 | devicesToBackupByNode.forEach(this::sendBackups); |
| 887 | } catch (Exception e) { |
| 888 | log.error("Backup failed.", e); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | private Set<DeviceId> onBackupReceipt(Map<DeviceId, |
| 893 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> flowTables) { |
| 894 | log.debug("Received flowEntries for {} to backup", flowTables.keySet()); |
| 895 | Set<DeviceId> backedupDevices = Sets.newHashSet(); |
| 896 | try { |
| 897 | flowTables.forEach((deviceId, deviceFlowTable) -> { |
| 898 | // Only process those devices are that not managed by the local node. |
| 899 | if (!Objects.equals(local, mastershipService.getMasterFor(deviceId))) { |
| 900 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> backupFlowTable = |
| 901 | getFlowTable(deviceId); |
| 902 | backupFlowTable.clear(); |
| 903 | backupFlowTable.putAll(deviceFlowTable); |
| 904 | backedupDevices.add(deviceId); |
| 905 | } |
| 906 | }); |
| 907 | } catch (Exception e) { |
| 908 | log.warn("Failure processing backup request", e); |
| 909 | } |
| 910 | return backedupDevices; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, |
| 916 | List<TableStatisticsEntry> tableStats) { |
| 917 | deviceTableStats.put(deviceId, tableStats); |
| 918 | return null; |
| 919 | } |
| 920 | |
| 921 | @Override |
| 922 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
| 923 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 924 | |
| 925 | if (master == null) { |
| 926 | log.debug("Failed to getTableStats: No master for {}", deviceId); |
| 927 | return Collections.emptyList(); |
| 928 | } |
| 929 | |
| 930 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 931 | if (tableStats == null) { |
| 932 | return Collections.emptyList(); |
| 933 | } |
| 934 | return ImmutableList.copyOf(tableStats); |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 939 | return Streams.stream(getTableStatistics(deviceId)) |
| 940 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 941 | .sum(); |
| 942 | } |
| 943 | |
| 944 | private class InternalTableStatsListener |
| 945 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
| 946 | @Override |
| 947 | public void event(EventuallyConsistentMapEvent<DeviceId, |
| 948 | List<TableStatisticsEntry>> event) { |
| 949 | //TODO: Generate an event to listeners (do we need?) |
| 950 | } |
| 951 | } |
| 952 | } |