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