Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 18 | import java.util.Collections; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 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; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 30 | import java.util.concurrent.atomic.AtomicReference; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 33 | import com.google.common.collect.Streams; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Activate; |
| 35 | import org.apache.felix.scr.annotations.Component; |
| 36 | import org.apache.felix.scr.annotations.Deactivate; |
| 37 | import org.apache.felix.scr.annotations.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.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; |
| 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; |
Yuta HIGUCHI | 10e91fb | 2017-06-15 13:30:50 -0700 | [diff] [blame] | 93 | import com.google.common.util.concurrent.Futures; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 94 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 95 | import static com.google.common.base.Strings.isNullOrEmpty; |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 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.FlowStoreMessageSubjects.APPLY_BATCH_FLOWS; |
| 101 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.FLOW_TABLE_BACKUP; |
| 102 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES; |
| 103 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.GET_FLOW_ENTRY; |
| 104 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.REMOTE_APPLY_COMPLETED; |
| 105 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.REMOVE_FLOW_ENTRY; |
| 106 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Manages inventory of flow rules using a distributed state management protocol. |
| 110 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 111 | @Component(immediate = true) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 112 | @Service |
Madan Jampani | 37d04c6 | 2016-04-25 15:53:55 -0700 | [diff] [blame] | 113 | public class DistributedFlowRuleStore |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 120 | private static final int DEFAULT_MAX_BACKUP_COUNT = 2; |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 121 | private static final boolean DEFAULT_PERSISTENCE_ENABLED = false; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 122 | private static final int DEFAULT_BACKUP_PERIOD_MILLIS = 2000; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 123 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 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; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 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; |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 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; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 137 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 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 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 166 | protected PersistenceService persistenceService; |
| 167 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 168 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
| 169 | private ExecutorService messageHandlingExecutor; |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 170 | private ExecutorService eventHandler; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 171 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 172 | private ScheduledFuture<?> backupTask; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 173 | private final ScheduledExecutorService backupSenderExecutor = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 174 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/flow", "backup-sender", 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 | 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 | |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 183 | protected final Serializer serializer = Serializer.using(KryoNamespaces.API); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 184 | |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 185 | protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder() |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 186 | .register(KryoNamespaces.API) |
| 187 | .register(MastershipBasedTimestamp.class); |
| 188 | |
| 189 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 201 | eventHandler = Executors.newSingleThreadExecutor( |
| 202 | groupedThreads("onos/flow", "event-handler", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 203 | messageHandlingExecutor = Executors.newFixedThreadPool( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 204 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 205 | |
| 206 | registerMessageHandlers(messageHandlingExecutor); |
| 207 | |
sivachidambaram subramanian | 0b33c08 | 2017-04-06 13:26:44 +0530 | [diff] [blame] | 208 | replicaInfoManager.addListener(flowTable); |
| 209 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 210 | flowTable::backup, |
| 211 | 0, |
| 212 | backupPeriod, |
| 213 | TimeUnit.MILLISECONDS); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 214 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 215 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
| 216 | .withName("onos-flow-table-stats") |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 217 | .withSerializer(serializerBuilder) |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 218 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 219 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 220 | .withTombstonesDisabled() |
| 221 | .build(); |
| 222 | deviceTableStats.addListener(tableStatsListener); |
| 223 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 224 | logConfig("Started"); |
| 225 | } |
| 226 | |
| 227 | @Deactivate |
| 228 | public void deactivate(ComponentContext context) { |
sivachidambaram subramanian | 0b33c08 | 2017-04-06 13:26:44 +0530 | [diff] [blame] | 229 | replicaInfoManager.removeListener(flowTable); |
| 230 | backupTask.cancel(true); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 231 | configService.unregisterProperties(getClass(), false); |
| 232 | unregisterMessageHandlers(); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 233 | deviceTableStats.removeListener(tableStatsListener); |
| 234 | deviceTableStats.destroy(); |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 235 | eventHandler.shutdownNow(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 245 | logConfig("Default config"); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | Dictionary properties = context.getProperties(); |
| 250 | int newPoolSize; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 251 | int newBackupPeriod; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 252 | int newBackupCount; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 253 | try { |
| 254 | String s = get(properties, "msgHandlerPoolSize"); |
| 255 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 256 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 257 | s = get(properties, "backupPeriod"); |
| 258 | newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim()); |
| 259 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 260 | s = get(properties, "backupCount"); |
| 261 | newBackupCount = isNullOrEmpty(s) ? backupCount : Integer.parseInt(s.trim()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 262 | } catch (NumberFormatException | ClassCastException e) { |
| 263 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 264 | newBackupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 265 | newBackupCount = DEFAULT_MAX_BACKUP_COUNT; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 268 | boolean restartBackupTask = false; |
sivachidambaram subramanian | 0b33c08 | 2017-04-06 13:26:44 +0530 | [diff] [blame] | 269 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 270 | if (newBackupPeriod != backupPeriod) { |
| 271 | backupPeriod = newBackupPeriod; |
sivachidambaram subramanian | 0b33c08 | 2017-04-06 13:26:44 +0530 | [diff] [blame] | 272 | restartBackupTask = true; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 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); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 284 | } |
| 285 | if (newPoolSize != msgHandlerPoolSize) { |
| 286 | msgHandlerPoolSize = newPoolSize; |
| 287 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 288 | messageHandlingExecutor = Executors.newFixedThreadPool( |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 289 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 290 | |
| 291 | // replace previously registered handlers. |
| 292 | registerMessageHandlers(messageHandlingExecutor); |
| 293 | oldMsgHandler.shutdown(); |
| 294 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 295 | if (backupCount != newBackupCount) { |
| 296 | backupCount = newBackupCount; |
| 297 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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( |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 305 | REMOTE_APPLY_COMPLETED, serializer::decode, this::notifyDelegate, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 306 | clusterCommunicator.addSubscriber( |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 307 | GET_FLOW_ENTRY, serializer::decode, flowTable::getFlowEntry, serializer::encode, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 308 | clusterCommunicator.addSubscriber( |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 309 | GET_DEVICE_FLOW_ENTRIES, serializer::decode, flowTable::getFlowEntries, serializer::encode, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 310 | clusterCommunicator.addSubscriber( |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 311 | REMOVE_FLOW_ENTRY, serializer::decode, this::removeFlowRuleInternal, serializer::encode, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 312 | clusterCommunicator.addSubscriber( |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 313 | FLOW_TABLE_BACKUP, serializer::decode, flowTable::onBackupReceipt, serializer::encode, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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) { |
sivachidambaram subramanian | 0b33c08 | 2017-04-06 13:26:44 +0530 | [diff] [blame] | 326 | log.info("{} with msgHandlerPoolSize = {}; backupPeriod = {}, backupCount = {}", |
| 327 | prefix, msgHandlerPoolSize, backupPeriod, backupCount); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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() { |
Yuta HIGUCHI | 10e91fb | 2017-06-15 13:30:50 -0700 | [diff] [blame] | 335 | return Streams.stream(deviceService.getDevices()).parallel() |
| 336 | .mapToInt(device -> Iterables.size(getFlowEntries(device.id()))) |
| 337 | .sum(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 342 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 343 | |
| 344 | if (master == null) { |
Thomas Vachuska | 88fd690 | 2015-08-04 10:08:34 -0700 | [diff] [blame] | 345 | log.debug("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 346 | return null; |
| 347 | } |
| 348 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 349 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 358 | serializer::encode, |
| 359 | serializer::decode, |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 360 | master), |
| 361 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 362 | TimeUnit.MILLISECONDS, |
| 363 | null); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 368 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 369 | |
| 370 | if (master == null) { |
Thomas Vachuska | 88fd690 | 2015-08-04 10:08:34 -0700 | [diff] [blame] | 371 | log.debug("Failed to getFlowEntries: No master for {}", deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 372 | return Collections.emptyList(); |
| 373 | } |
| 374 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 375 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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 | FlowStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES, |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 384 | serializer::encode, |
| 385 | serializer::decode, |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 395 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 409 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 410 | |
| 411 | if (master == null) { |
| 412 | log.warn("No master for {} : flows will be marked for removal", 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 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 422 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 423 | storeBatchInternal(operation); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 428 | master, deviceId); |
| 429 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 430 | clusterCommunicator.unicast(operation, |
| 431 | APPLY_BATCH_FLOWS, |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 432 | serializer::encode, |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 433 | master) |
| 434 | .whenComplete((result, error) -> { |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 435 | if (error != null) { |
Madan Jampani | 15f1bc4 | 2015-05-28 10:51:52 -0700 | [diff] [blame] | 436 | log.warn("Failed to storeBatch: {} to {}", operation, master, error); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 437 | |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 438 | Set<FlowRule> allFailures = operation.getOperations() |
| 439 | .stream() |
| 440 | .map(op -> op.target()) |
| 441 | .collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 442 | |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 443 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 444 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 445 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 446 | } |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 447 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 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: |
| 473 | entry = new DefaultFlowEntry(op.target()); |
| 474 | // always add requested FlowRule |
| 475 | // Note: 2 equal FlowEntry may have different treatment |
| 476 | flowTable.remove(entry.deviceId(), entry); |
| 477 | flowTable.add(entry); |
| 478 | |
| 479 | return op; |
| 480 | case REMOVE: |
| 481 | entry = flowTable.getFlowEntry(op.target()); |
| 482 | if (entry != null) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 483 | //FIXME modification of "stored" flow entry outside of flow table |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 484 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 485 | log.debug("Setting state of rule to pending remove: {}", entry); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 486 | return op; |
| 487 | } |
| 488 | break; |
| 489 | case MODIFY: |
| 490 | //TODO: figure this out at some point |
| 491 | break; |
| 492 | default: |
| 493 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 494 | } |
| 495 | return null; |
| 496 | } |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 497 | ).filter(Objects::nonNull).collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public void deleteFlowRule(FlowRule rule) { |
| 502 | storeBatch( |
| 503 | new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 504 | Collections.singletonList( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 505 | new FlowRuleBatchEntry( |
| 506 | FlowRuleOperation.REMOVE, |
| 507 | rule)), rule.deviceId(), idGenerator.getNewId())); |
| 508 | } |
| 509 | |
| 510 | @Override |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 511 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
| 512 | if (mastershipService.isLocalMaster(rule.deviceId())) { |
| 513 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 514 | if (stored != null && |
| 515 | stored.state() != FlowEntryState.PENDING_ADD) { |
| 516 | stored.setState(FlowEntryState.PENDING_ADD); |
| 517 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 518 | } |
| 519 | } |
| 520 | return null; |
| 521 | } |
| 522 | |
| 523 | @Override |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 524 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 525 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 526 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 527 | return addOrUpdateFlowRuleInternal(rule); |
| 528 | } |
| 529 | |
| 530 | log.warn("Tried to update FlowRule {} state," |
| 531 | + " while the Node was not the master.", rule); |
| 532 | return null; |
| 533 | } |
| 534 | |
| 535 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
| 536 | // check if this new rule is an update to an existing entry |
| 537 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 538 | if (stored != null) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 539 | //FIXME modification of "stored" flow entry outside of flow table |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 540 | stored.setBytes(rule.bytes()); |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 541 | stored.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 542 | stored.setLiveType(rule.liveType()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 543 | stored.setPackets(rule.packets()); |
Jonathan Hart | 89e981f | 2016-01-04 13:59:55 -0800 | [diff] [blame] | 544 | stored.setLastSeen(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 545 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 546 | stored.setState(FlowEntryState.ADDED); |
| 547 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 548 | } |
| 549 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 550 | } |
| 551 | |
| 552 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 553 | // TODO: also update backup if the behavior is correct. |
| 554 | flowTable.add(rule); |
| 555 | return null; |
| 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 560 | final DeviceId deviceId = rule.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 561 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 562 | |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 563 | if (Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 564 | // bypass and handle it locally |
| 565 | return removeFlowRuleInternal(rule); |
| 566 | } |
| 567 | |
| 568 | if (master == null) { |
| 569 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
| 570 | // TODO: revisit if this should be null (="no-op") or Exception |
| 571 | return null; |
| 572 | } |
| 573 | |
| 574 | log.trace("Forwarding removeFlowRule to {}, which is the master for device {}", |
| 575 | master, deviceId); |
| 576 | |
Madan Jampani | 222229e | 2016-07-14 10:43:25 -0700 | [diff] [blame] | 577 | return Futures.getUnchecked(clusterCommunicator.sendAndReceive( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 578 | rule, |
| 579 | REMOVE_FLOW_ENTRY, |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 580 | serializer::encode, |
| 581 | serializer::decode, |
Madan Jampani | 222229e | 2016-07-14 10:43:25 -0700 | [diff] [blame] | 582 | master)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
| 586 | final DeviceId deviceId = rule.deviceId(); |
| 587 | // This is where one could mark a rule as removed and still keep it in the store. |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 588 | final FlowEntry removed = flowTable.remove(deviceId, rule); |
| 589 | // rule may be partial rule that is missing treatment, we should use rule from store instead |
| 590 | return removed != null ? new FlowRuleEvent(RULE_REMOVED, removed) : null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 594 | public void purgeFlowRule(DeviceId deviceId) { |
| 595 | flowTable.purgeFlowRule(deviceId); |
| 596 | } |
| 597 | |
| 598 | @Override |
Victor Silva | 139bca4 | 2016-08-18 11:46:35 -0300 | [diff] [blame] | 599 | public void purgeFlowRules() { |
| 600 | flowTable.purgeFlowRules(); |
| 601 | } |
| 602 | |
| 603 | @Override |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 604 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 605 | //FIXME: need a per device pending response |
| 606 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 607 | if (nodeId == null) { |
| 608 | notifyDelegate(event); |
| 609 | } else { |
| 610 | // TODO check unicast return value |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 611 | clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, serializer::encode, nodeId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 612 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 617 | |
| 618 | @Override |
| 619 | public void handle(final ClusterMessage message) { |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 620 | FlowRuleBatchOperation operation = serializer.decode(message.payload()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 621 | log.debug("received batch request {}", operation); |
| 622 | |
| 623 | final DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 624 | NodeId master = mastershipService.getMasterFor(deviceId); |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 625 | if (!Objects.equals(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 626 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 627 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 628 | failures.add(op.target()); |
| 629 | } |
| 630 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
| 631 | // This node is no longer the master, respond as all failed. |
| 632 | // TODO: we might want to wrap response in envelope |
| 633 | // to distinguish sw programming failure and hand over |
| 634 | // it make sense in the latter case to retry immediately. |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 635 | message.respond(serializer.encode(allFailed)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
| 639 | pendingResponses.put(operation.id(), message.sender()); |
| 640 | storeBatchInternal(operation); |
| 641 | } |
| 642 | } |
| 643 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 644 | private class BackupOperation { |
| 645 | private final NodeId nodeId; |
| 646 | private final DeviceId deviceId; |
| 647 | |
| 648 | public BackupOperation(NodeId nodeId, DeviceId deviceId) { |
| 649 | this.nodeId = nodeId; |
| 650 | this.deviceId = deviceId; |
| 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public int hashCode() { |
| 655 | return Objects.hash(nodeId, deviceId); |
| 656 | } |
| 657 | |
| 658 | @Override |
| 659 | public boolean equals(Object other) { |
| 660 | if (other != null && other instanceof BackupOperation) { |
| 661 | BackupOperation that = (BackupOperation) other; |
| 662 | return this.nodeId.equals(that.nodeId) && |
| 663 | this.deviceId.equals(that.deviceId); |
| 664 | } else { |
| 665 | return false; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 670 | private class InternalFlowTable implements ReplicaInfoEventListener { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 671 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 672 | //TODO replace the Map<V,V> with ExtendedSet |
| 673 | private final Map<DeviceId, Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> |
Madan Jampani | 5c3766c | 2015-06-02 15:54:41 -0700 | [diff] [blame] | 674 | flowEntries = Maps.newConcurrentMap(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 675 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 676 | private final Map<BackupOperation, Long> lastBackupTimes = Maps.newConcurrentMap(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 677 | private final Map<DeviceId, Long> lastUpdateTimes = Maps.newConcurrentMap(); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 678 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 679 | @Override |
| 680 | public void event(ReplicaInfoEvent event) { |
Madan Jampani | 71c32ca | 2016-06-22 08:23:18 -0700 | [diff] [blame] | 681 | eventHandler.execute(() -> handleEvent(event)); |
| 682 | } |
| 683 | |
| 684 | private void handleEvent(ReplicaInfoEvent event) { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 685 | DeviceId deviceId = event.subject(); |
sivachidambaram subramanian | 0b33c08 | 2017-04-06 13:26:44 +0530 | [diff] [blame] | 686 | if (!mastershipService.isLocalMaster(deviceId)) { |
Madan Jampani | a98bf93 | 2015-06-02 12:01:36 -0700 | [diff] [blame] | 687 | return; |
| 688 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 689 | if (event.type() == MASTER_CHANGED) { |
| 690 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 691 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 692 | backupSenderExecutor.schedule(this::backup, 0, TimeUnit.SECONDS); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 695 | private void sendBackups(NodeId nodeId, Set<DeviceId> deviceIds) { |
| 696 | // split up the devices into smaller batches and send them separately. |
| 697 | Iterables.partition(deviceIds, FLOW_TABLE_BACKUP_BATCH_SIZE) |
| 698 | .forEach(ids -> backupFlowEntries(nodeId, Sets.newHashSet(ids))); |
| 699 | } |
| 700 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 701 | private void backupFlowEntries(NodeId nodeId, Set<DeviceId> deviceIds) { |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 702 | if (deviceIds.isEmpty()) { |
| 703 | return; |
| 704 | } |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 705 | log.debug("Sending flowEntries for devices {} to {} for backup.", deviceIds, nodeId); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 706 | Map<DeviceId, Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> |
| 707 | deviceFlowEntries = Maps.newConcurrentMap(); |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 708 | deviceIds.forEach(id -> deviceFlowEntries.put(id, getFlowTableCopy(id))); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 709 | clusterCommunicator.<Map<DeviceId, |
| 710 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>>, |
| 711 | Set<DeviceId>> |
| 712 | sendAndReceive(deviceFlowEntries, |
| 713 | FLOW_TABLE_BACKUP, |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 714 | serializer::encode, |
| 715 | serializer::decode, |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 716 | nodeId) |
| 717 | .whenComplete((backedupDevices, error) -> { |
| 718 | Set<DeviceId> devicesNotBackedup = error != null ? |
| 719 | deviceFlowEntries.keySet() : |
| 720 | Sets.difference(deviceFlowEntries.keySet(), backedupDevices); |
| 721 | if (devicesNotBackedup.size() > 0) { |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 722 | log.warn("Failed to backup devices: {}. Reason: {}, Node: {}", |
| 723 | devicesNotBackedup, error != null ? error.getMessage() : "none", |
| 724 | nodeId); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 725 | } |
| 726 | if (backedupDevices != null) { |
| 727 | backedupDevices.forEach(id -> { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 728 | lastBackupTimes.put(new BackupOperation(nodeId, id), System.currentTimeMillis()); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 729 | }); |
| 730 | } |
| 731 | }); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 734 | /** |
| 735 | * Returns the flow table for specified device. |
| 736 | * |
| 737 | * @param deviceId identifier of the device |
| 738 | * @return Map representing Flow Table of given device. |
| 739 | */ |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 740 | private Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 741 | if (persistenceEnabled) { |
| 742 | return flowEntries.computeIfAbsent(deviceId, id -> persistenceService |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 743 | .<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>persistentMapBuilder() |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 744 | .withName("FlowTable:" + deviceId.toString()) |
| 745 | .withSerializer(new Serializer() { |
| 746 | @Override |
| 747 | public <T> byte[] encode(T object) { |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 748 | return serializer.encode(object); |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public <T> T decode(byte[] bytes) { |
Madan Jampani | 884d443 | 2016-08-23 10:46:55 -0700 | [diff] [blame] | 753 | return serializer.decode(bytes); |
Aaron Kruglikov | a62fdbb | 2015-10-27 16:32:06 -0700 | [diff] [blame] | 754 | } |
| 755 | }) |
| 756 | .build()); |
| 757 | } else { |
| 758 | return flowEntries.computeIfAbsent(deviceId, id -> Maps.newConcurrentMap()); |
| 759 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Ray Milkey | 2b6ff42 | 2016-08-26 13:03:15 -0700 | [diff] [blame] | 762 | private Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> getFlowTableCopy(DeviceId deviceId) { |
| 763 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> copy = Maps.newHashMap(); |
| 764 | if (persistenceEnabled) { |
| 765 | return flowEntries.computeIfAbsent(deviceId, id -> persistenceService |
| 766 | .<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>persistentMapBuilder() |
| 767 | .withName("FlowTable:" + deviceId.toString()) |
| 768 | .withSerializer(new Serializer() { |
| 769 | @Override |
| 770 | public <T> byte[] encode(T object) { |
| 771 | return serializer.encode(object); |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public <T> T decode(byte[] bytes) { |
| 776 | return serializer.decode(bytes); |
| 777 | } |
| 778 | }) |
| 779 | .build()); |
| 780 | } else { |
| 781 | flowEntries.computeIfAbsent(deviceId, id -> Maps.newConcurrentMap()).forEach((k, v) -> { |
| 782 | copy.put(k, Maps.newHashMap(v)); |
| 783 | }); |
| 784 | return copy; |
| 785 | } |
| 786 | } |
| 787 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 788 | private Map<StoredFlowEntry, StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) { |
| 789 | return getFlowTable(deviceId).computeIfAbsent(flowId, id -> Maps.newConcurrentMap()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | private StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 793 | return getFlowEntriesInternal(rule.deviceId(), rule.id()).get(rule); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 797 | return getFlowTable(deviceId).values().stream() |
| 798 | .flatMap(m -> m.values().stream()) |
| 799 | .collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
| 803 | return getFlowEntryInternal(rule); |
| 804 | } |
| 805 | |
| 806 | public Set<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 807 | return getFlowEntriesInternal(deviceId); |
| 808 | } |
| 809 | |
| 810 | public void add(FlowEntry rule) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 811 | getFlowEntriesInternal(rule.deviceId(), rule.id()) |
| 812 | .compute((StoredFlowEntry) rule, (k, stored) -> { |
| 813 | //TODO compare stored and rule timestamps |
| 814 | //TODO the key is not updated |
| 815 | return (StoredFlowEntry) rule; |
| 816 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 817 | lastUpdateTimes.put(rule.deviceId(), System.currentTimeMillis()); |
| 818 | } |
| 819 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 820 | public FlowEntry remove(DeviceId deviceId, FlowEntry rule) { |
| 821 | final AtomicReference<FlowEntry> removedRule = new AtomicReference<>(); |
| 822 | getFlowEntriesInternal(rule.deviceId(), rule.id()) |
| 823 | .computeIfPresent((StoredFlowEntry) rule, (k, stored) -> { |
| 824 | if (rule instanceof DefaultFlowEntry) { |
| 825 | DefaultFlowEntry toRemove = (DefaultFlowEntry) rule; |
| 826 | if (stored instanceof DefaultFlowEntry) { |
| 827 | DefaultFlowEntry storedEntry = (DefaultFlowEntry) stored; |
| 828 | if (toRemove.created() < storedEntry.created()) { |
| 829 | log.debug("Trying to remove more recent flow entry {} (stored: {})", |
| 830 | toRemove, stored); |
| 831 | // the key is not updated, removedRule remains null |
| 832 | return stored; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | removedRule.set(stored); |
| 837 | return null; |
| 838 | }); |
| 839 | |
| 840 | if (removedRule.get() != null) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 841 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 842 | return removedRule.get(); |
| 843 | } else { |
| 844 | return null; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 848 | public void purgeFlowRule(DeviceId deviceId) { |
| 849 | flowEntries.remove(deviceId); |
| 850 | } |
| 851 | |
Victor Silva | 139bca4 | 2016-08-18 11:46:35 -0300 | [diff] [blame] | 852 | public void purgeFlowRules() { |
| 853 | flowEntries.clear(); |
| 854 | } |
| 855 | |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 856 | private List<NodeId> getBackupNodes(DeviceId deviceId) { |
| 857 | // The returned backup node list is in the order of preference i.e. next likely master first. |
| 858 | List<NodeId> allPossibleBackupNodes = replicaInfoManager.getReplicaInfoFor(deviceId).backups(); |
| 859 | return ImmutableList.copyOf(allPossibleBackupNodes) |
| 860 | .subList(0, Math.min(allPossibleBackupNodes.size(), backupCount)); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | private void backup() { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 864 | try { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 865 | // compute a mapping from node to the set of devices whose flow entries it should backup |
| 866 | Map<NodeId, Set<DeviceId>> devicesToBackupByNode = Maps.newHashMap(); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 867 | flowEntries.keySet().forEach(deviceId -> { |
Madan Jampani | c6d69f7 | 2016-07-15 15:47:12 -0700 | [diff] [blame] | 868 | List<NodeId> backupNodes = getBackupNodes(deviceId); |
| 869 | backupNodes.forEach(backupNode -> { |
| 870 | if (lastBackupTimes.getOrDefault(new BackupOperation(backupNode, deviceId), 0L) |
| 871 | < lastUpdateTimes.getOrDefault(deviceId, 0L)) { |
| 872 | devicesToBackupByNode.computeIfAbsent(backupNode, |
| 873 | nodeId -> Sets.newHashSet()).add(deviceId); |
| 874 | } |
| 875 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 876 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 877 | // send the device flow entries to their respective backup nodes |
Madan Jampani | adea890 | 2015-06-04 17:39:45 -0700 | [diff] [blame] | 878 | devicesToBackupByNode.forEach(this::sendBackups); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 879 | } catch (Exception e) { |
| 880 | log.error("Backup failed.", e); |
| 881 | } |
| 882 | } |
| 883 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 884 | private Set<DeviceId> onBackupReceipt(Map<DeviceId, |
| 885 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>>> flowTables) { |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 886 | log.debug("Received flowEntries for {} to backup", flowTables.keySet()); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 887 | Set<DeviceId> backedupDevices = Sets.newHashSet(); |
| 888 | try { |
Madan Jampani | a98bf93 | 2015-06-02 12:01:36 -0700 | [diff] [blame] | 889 | flowTables.forEach((deviceId, deviceFlowTable) -> { |
| 890 | // Only process those devices are that not managed by the local node. |
Sho SHIMIZU | 828bc16 | 2016-01-13 23:10:43 -0800 | [diff] [blame] | 891 | if (!Objects.equals(local, mastershipService.getMasterFor(deviceId))) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 892 | Map<FlowId, Map<StoredFlowEntry, StoredFlowEntry>> backupFlowTable = |
| 893 | getFlowTable(deviceId); |
Madan Jampani | a98bf93 | 2015-06-02 12:01:36 -0700 | [diff] [blame] | 894 | backupFlowTable.clear(); |
| 895 | backupFlowTable.putAll(deviceFlowTable); |
| 896 | backedupDevices.add(deviceId); |
| 897 | } |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 898 | }); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 899 | } catch (Exception e) { |
| 900 | log.warn("Failure processing backup request", e); |
| 901 | } |
| 902 | return backedupDevices; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 903 | } |
| 904 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 905 | |
| 906 | @Override |
| 907 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, |
| 908 | List<TableStatisticsEntry> tableStats) { |
| 909 | deviceTableStats.put(deviceId, tableStats); |
| 910 | return null; |
| 911 | } |
| 912 | |
| 913 | @Override |
| 914 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
| 915 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 916 | |
| 917 | if (master == null) { |
| 918 | log.debug("Failed to getTableStats: No master for {}", deviceId); |
| 919 | return Collections.emptyList(); |
| 920 | } |
| 921 | |
| 922 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 923 | if (tableStats == null) { |
| 924 | return Collections.emptyList(); |
| 925 | } |
| 926 | return ImmutableList.copyOf(tableStats); |
| 927 | } |
| 928 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 929 | @Override |
| 930 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 931 | return Streams.stream(getTableStatistics(deviceId)) |
| 932 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 933 | .sum(); |
| 934 | } |
| 935 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 936 | private class InternalTableStatsListener |
| 937 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
| 938 | @Override |
| 939 | public void event(EventuallyConsistentMapEvent<DeviceId, |
| 940 | List<TableStatisticsEntry>> event) { |
| 941 | //TODO: Generate an event to listeners (do we need?) |
| 942 | } |
| 943 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 944 | } |