Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 16 | package org.onosproject.store.flow.impl; |
| 17 | |
Jordan Halterman | 01f3bdd | 2019-04-17 11:05:16 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.Dictionary; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ExecutionException; |
| 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.ScheduledExecutorService; |
| 30 | import java.util.concurrent.TimeUnit; |
| 31 | import java.util.concurrent.TimeoutException; |
| 32 | import java.util.function.Function; |
| 33 | import java.util.stream.Collectors; |
| 34 | import java.util.stream.StreamSupport; |
| 35 | |
Jordan Halterman | 8f90d6d | 2018-06-12 11:23:33 -0700 | [diff] [blame] | 36 | import com.google.common.collect.ImmutableList; |
| 37 | import com.google.common.collect.Maps; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 38 | import com.google.common.collect.Streams; |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 39 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 40 | import org.apache.commons.lang3.tuple.Pair; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 41 | import org.onlab.util.KryoNamespace; |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 42 | import org.onlab.util.OrderedExecutor; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 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; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 49 | import org.onosproject.event.AbstractListenerManager; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 50 | import org.onosproject.mastership.MastershipService; |
| 51 | import org.onosproject.net.DeviceId; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceService; |
| 55 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 56 | import org.onosproject.net.flow.DefaultFlowEntry; |
pierventre | 019b0729 | 2021-05-21 12:39:09 +0200 | [diff] [blame^] | 57 | import org.onosproject.net.flow.DefaultFlowRule; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flow.FlowEntry; |
| 59 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 60 | import org.onosproject.net.flow.FlowRule; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flow.FlowRuleEvent; |
| 62 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 63 | import org.onosproject.net.flow.FlowRuleService; |
| 64 | import org.onosproject.net.flow.FlowRuleStore; |
| 65 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 66 | import org.onosproject.net.flow.StoredFlowEntry; |
Jordan Halterman | 01f3bdd | 2019-04-17 11:05:16 -0700 | [diff] [blame] | 67 | import org.onosproject.net.flow.FlowRuleStoreException; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 68 | import org.onosproject.net.flow.TableStatisticsEntry; |
Jordan Halterman | 8f90d6d | 2018-06-12 11:23:33 -0700 | [diff] [blame] | 69 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 70 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation; |
| 71 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 72 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 73 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 74 | import org.onosproject.persistence.PersistenceService; |
| 75 | import org.onosproject.store.AbstractStore; |
| 76 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 77 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 78 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 79 | import org.onosproject.store.flow.ReplicaInfo; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 80 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 81 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 82 | import org.onosproject.store.flow.ReplicaInfoService; |
| 83 | import org.onosproject.store.impl.MastershipBasedTimestamp; |
| 84 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 85 | import org.onosproject.store.service.AsyncConsistentMap; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 86 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 87 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 88 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 89 | import org.onosproject.store.service.MapEvent; |
| 90 | import org.onosproject.store.service.MapEventListener; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 91 | import org.onosproject.store.service.Serializer; |
| 92 | import org.onosproject.store.service.StorageService; |
| 93 | import org.onosproject.store.service.WallClockTimestamp; |
| 94 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | import org.osgi.service.component.annotations.Activate; |
| 96 | import org.osgi.service.component.annotations.Component; |
| 97 | import org.osgi.service.component.annotations.Deactivate; |
| 98 | import org.osgi.service.component.annotations.Modified; |
| 99 | import org.osgi.service.component.annotations.Reference; |
| 100 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 101 | import org.slf4j.Logger; |
| 102 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 103 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 104 | import static java.lang.Math.max; |
| 105 | import static java.lang.Math.min; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 106 | import static org.onlab.util.Tools.get; |
| 107 | import static org.onlab.util.Tools.groupedThreads; |
| 108 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 109 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.APPLY_BATCH_FLOWS; |
| 110 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.FLOW_TABLE_BACKUP; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 111 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.GET_DEVICE_FLOW_COUNT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 112 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY; |
| 113 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.REMOTE_APPLY_COMPLETED; |
| 114 | import static org.onosproject.store.flow.impl.ECFlowRuleStoreMessageSubjects.REMOVE_FLOW_ENTRY; |
| 115 | import static org.slf4j.LoggerFactory.getLogger; |
| 116 | |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 117 | import static org.onosproject.store.OsgiPropertyConstants.*; |
| 118 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Manages inventory of flow rules using a distributed state management protocol. |
| 121 | */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 122 | @Component( |
| 123 | immediate = true, |
| 124 | service = FlowRuleStore.class, |
| 125 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 126 | MESSAGE_HANDLER_THREAD_POOL_SIZE + ":Integer=" + MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT, |
| 127 | BACKUP_PERIOD_MILLIS + ":Integer=" + BACKUP_PERIOD_MILLIS_DEFAULT, |
| 128 | ANTI_ENTROPY_PERIOD_MILLIS + ":Integer=" + ANTI_ENTROPY_PERIOD_MILLIS_DEFAULT, |
| 129 | EC_FLOW_RULE_STORE_PERSISTENCE_ENABLED + ":Boolean=" + EC_FLOW_RULE_STORE_PERSISTENCE_ENABLED_DEFAULT, |
| 130 | MAX_BACKUP_COUNT + ":Integer=" + MAX_BACKUP_COUNT_DEFAULT |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 131 | } |
| 132 | ) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 133 | public class ECFlowRuleStore |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 134 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 135 | implements FlowRuleStore { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 136 | |
| 137 | private final Logger log = getLogger(getClass()); |
| 138 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 139 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Jordan Halterman | 4330078 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 140 | private static final int GET_FLOW_ENTRIES_TIMEOUT = 30; //seconds |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 141 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 142 | /** Number of threads in the message handler pool. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 143 | private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 144 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 145 | /** Delay in ms between successive backup runs. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 146 | private int backupPeriod = BACKUP_PERIOD_MILLIS_DEFAULT; |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 147 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 148 | /** Delay in ms between anti-entropy runs. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 149 | private int antiEntropyPeriod = ANTI_ENTROPY_PERIOD_MILLIS_DEFAULT; |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 150 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 151 | /** Indicates whether or not changes in the flow table should be persisted to disk. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 152 | private boolean persistenceEnabled = EC_FLOW_RULE_STORE_PERSISTENCE_ENABLED_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 153 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 154 | /** Max number of backup copies for each device. */ |
Andrea Campanella | 5daa7c46 | 2020-03-13 12:04:23 +0100 | [diff] [blame] | 155 | protected static volatile int backupCount = MAX_BACKUP_COUNT_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 156 | |
| 157 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 158 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 160 | protected ReplicaInfoService replicaInfoManager; |
| 161 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 163 | protected ClusterCommunicationService clusterCommunicator; |
| 164 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 166 | protected ClusterService clusterService; |
| 167 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 168 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 169 | protected DeviceService deviceService; |
| 170 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 171 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 172 | protected CoreService coreService; |
| 173 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 174 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 175 | protected ComponentConfigService configService; |
| 176 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 177 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 178 | protected MastershipService mastershipService; |
| 179 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 181 | protected PersistenceService persistenceService; |
| 182 | |
| 183 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
| 184 | private ExecutorService messageHandlingExecutor; |
| 185 | private ExecutorService eventHandler; |
| 186 | |
Ray Milkey | d1092d6 | 2019-04-02 09:12:00 -0700 | [diff] [blame] | 187 | private ScheduledExecutorService backupScheduler; |
| 188 | private ExecutorService backupExecutor; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 189 | |
| 190 | private EventuallyConsistentMap<DeviceId, List<TableStatisticsEntry>> deviceTableStats; |
| 191 | private final EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> tableStatsListener = |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 192 | new InternalTableStatsListener(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 193 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 194 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 195 | protected StorageService storageService; |
| 196 | |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 197 | protected final Serializer serializer = Serializer.using(KryoNamespace.newBuilder() |
| 198 | .register(KryoNamespaces.API) |
Jordan Halterman | a765d22 | 2018-06-01 00:40:56 -0700 | [diff] [blame] | 199 | .register(BucketId.class) |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 200 | .register(FlowBucket.class) |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 201 | .register(ImmutablePair.class) |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 202 | .build()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 203 | |
| 204 | protected final KryoNamespace.Builder serializerBuilder = KryoNamespace.newBuilder() |
Jordan Halterman | a765d22 | 2018-06-01 00:40:56 -0700 | [diff] [blame] | 205 | .register(KryoNamespaces.API) |
| 206 | .register(BucketId.class) |
| 207 | .register(MastershipBasedTimestamp.class); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 208 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 209 | protected AsyncConsistentMap<DeviceId, Long> mastershipTermLifecycles; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 210 | |
| 211 | private IdGenerator idGenerator; |
| 212 | private NodeId local; |
| 213 | |
| 214 | @Activate |
| 215 | public void activate(ComponentContext context) { |
| 216 | configService.registerProperties(getClass()); |
| 217 | |
Ray Milkey | d1092d6 | 2019-04-02 09:12:00 -0700 | [diff] [blame] | 218 | backupScheduler = Executors.newSingleThreadScheduledExecutor( |
| 219 | groupedThreads("onos/flow", "backup-scheduler", log)); |
| 220 | backupExecutor = Executors.newFixedThreadPool( |
| 221 | max(min(Runtime.getRuntime().availableProcessors() * 2, 16), 4), |
| 222 | groupedThreads("onos/flow", "backup-%d", log)); |
| 223 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 224 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 225 | |
| 226 | local = clusterService.getLocalNode().id(); |
| 227 | |
| 228 | eventHandler = Executors.newSingleThreadExecutor( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 229 | groupedThreads("onos/flow", "event-handler", log)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 230 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 231 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 232 | |
| 233 | registerMessageHandlers(messageHandlingExecutor); |
| 234 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 235 | mastershipTermLifecycles = storageService.<DeviceId, Long>consistentMapBuilder() |
| 236 | .withName("onos-flow-store-terms") |
| 237 | .withSerializer(serializer) |
| 238 | .buildAsyncMap(); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 239 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 240 | deviceTableStats = storageService.<DeviceId, List<TableStatisticsEntry>>eventuallyConsistentMapBuilder() |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 241 | .withName("onos-flow-table-stats") |
| 242 | .withSerializer(serializerBuilder) |
| 243 | .withAntiEntropyPeriod(5, TimeUnit.SECONDS) |
| 244 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 245 | .withTombstonesDisabled() |
| 246 | .build(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 247 | deviceTableStats.addListener(tableStatsListener); |
| 248 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 249 | deviceService.addListener(flowTable); |
| 250 | deviceService.getDevices().forEach(device -> flowTable.addDevice(device.id())); |
| 251 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 252 | logConfig("Started"); |
| 253 | } |
| 254 | |
| 255 | @Deactivate |
| 256 | public void deactivate(ComponentContext context) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 257 | configService.unregisterProperties(getClass(), false); |
| 258 | unregisterMessageHandlers(); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 259 | deviceService.removeListener(flowTable); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 260 | deviceTableStats.removeListener(tableStatsListener); |
| 261 | deviceTableStats.destroy(); |
| 262 | eventHandler.shutdownNow(); |
| 263 | messageHandlingExecutor.shutdownNow(); |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 264 | backupScheduler.shutdownNow(); |
| 265 | backupExecutor.shutdownNow(); |
Ray Milkey | d1092d6 | 2019-04-02 09:12:00 -0700 | [diff] [blame] | 266 | backupScheduler = null; |
| 267 | backupExecutor = null; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 268 | log.info("Stopped"); |
| 269 | } |
| 270 | |
| 271 | @SuppressWarnings("rawtypes") |
| 272 | @Modified |
| 273 | public void modified(ComponentContext context) { |
| 274 | if (context == null) { |
| 275 | logConfig("Default config"); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | Dictionary properties = context.getProperties(); |
| 280 | int newPoolSize; |
| 281 | int newBackupPeriod; |
| 282 | int newBackupCount; |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 283 | int newAntiEntropyPeriod; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 284 | try { |
| 285 | String s = get(properties, "msgHandlerPoolSize"); |
| 286 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 287 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 288 | s = get(properties, BACKUP_PERIOD_MILLIS); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 289 | newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim()); |
| 290 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 291 | s = get(properties, MAX_BACKUP_COUNT); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 292 | newBackupCount = isNullOrEmpty(s) ? backupCount : Integer.parseInt(s.trim()); |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 293 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 294 | s = get(properties, ANTI_ENTROPY_PERIOD_MILLIS); |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 295 | newAntiEntropyPeriod = isNullOrEmpty(s) ? antiEntropyPeriod : Integer.parseInt(s.trim()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 296 | } catch (NumberFormatException | ClassCastException e) { |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 297 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE_DEFAULT; |
| 298 | newBackupPeriod = BACKUP_PERIOD_MILLIS_DEFAULT; |
| 299 | newBackupCount = MAX_BACKUP_COUNT_DEFAULT; |
| 300 | newAntiEntropyPeriod = ANTI_ENTROPY_PERIOD_MILLIS_DEFAULT; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 303 | if (newBackupPeriod != backupPeriod) { |
| 304 | backupPeriod = newBackupPeriod; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 305 | flowTable.setBackupPeriod(newBackupPeriod); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 306 | } |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 307 | |
| 308 | if (newAntiEntropyPeriod != antiEntropyPeriod) { |
| 309 | antiEntropyPeriod = newAntiEntropyPeriod; |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 310 | flowTable.setAntiEntropyPeriod(newAntiEntropyPeriod); |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 313 | if (newPoolSize != msgHandlerPoolSize) { |
| 314 | msgHandlerPoolSize = newPoolSize; |
| 315 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 316 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 317 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers", log)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 318 | |
| 319 | // replace previously registered handlers. |
| 320 | registerMessageHandlers(messageHandlingExecutor); |
| 321 | oldMsgHandler.shutdown(); |
| 322 | } |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 323 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 324 | if (backupCount != newBackupCount) { |
| 325 | backupCount = newBackupCount; |
| 326 | } |
| 327 | logConfig("Reconfigured"); |
| 328 | } |
| 329 | |
| 330 | private void registerMessageHandlers(ExecutorService executor) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 331 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(), executor); |
| 332 | clusterCommunicator.<FlowRuleBatchEvent>addSubscriber( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 333 | REMOTE_APPLY_COMPLETED, serializer::decode, this::notifyDelegate, executor); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 334 | clusterCommunicator.addSubscriber( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 335 | GET_FLOW_ENTRY, serializer::decode, flowTable::getFlowEntry, serializer::encode, executor); |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 336 | clusterCommunicator.<Pair<DeviceId, FlowEntryState>, Integer>addSubscriber( |
| 337 | GET_DEVICE_FLOW_COUNT, |
| 338 | serializer::decode, |
| 339 | p -> flowTable.getFlowRuleCount(p.getLeft(), p.getRight()), |
| 340 | serializer::encode, executor); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 341 | clusterCommunicator.addSubscriber( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 342 | REMOVE_FLOW_ENTRY, serializer::decode, this::removeFlowRuleInternal, serializer::encode, executor); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | private void unregisterMessageHandlers() { |
| 346 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 347 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_COUNT); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 348 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 349 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
| 350 | clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED); |
| 351 | clusterCommunicator.removeSubscriber(FLOW_TABLE_BACKUP); |
| 352 | } |
| 353 | |
| 354 | private void logConfig(String prefix) { |
| 355 | log.info("{} with msgHandlerPoolSize = {}; backupPeriod = {}, backupCount = {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 356 | prefix, msgHandlerPoolSize, backupPeriod, backupCount); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 359 | @Override |
| 360 | public int getFlowRuleCount() { |
| 361 | return Streams.stream(deviceService.getDevices()).parallel() |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 362 | .mapToInt(device -> getFlowRuleCount(device.id())) |
| 363 | .sum(); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public int getFlowRuleCount(DeviceId deviceId) { |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 368 | return getFlowRuleCount(deviceId, null); |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public int getFlowRuleCount(DeviceId deviceId, FlowEntryState state) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 373 | NodeId master = mastershipService.getMasterFor(deviceId); |
Andrea Campanella | 5daa7c46 | 2020-03-13 12:04:23 +0100 | [diff] [blame] | 374 | if (master == null && deviceService.isAvailable(deviceId)) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 375 | log.debug("Failed to getFlowRuleCount: No master for {}", deviceId); |
| 376 | return 0; |
| 377 | } |
| 378 | |
Andrea Campanella | 5daa7c46 | 2020-03-13 12:04:23 +0100 | [diff] [blame] | 379 | if (Objects.equals(local, master) || master == null) { |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 380 | return flowTable.getFlowRuleCount(deviceId, state); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | log.trace("Forwarding getFlowRuleCount to master {} for device {}", master, deviceId); |
| 384 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 385 | Pair.of(deviceId, state), |
| 386 | GET_DEVICE_FLOW_COUNT, |
| 387 | serializer::encode, |
| 388 | serializer::decode, |
| 389 | master), |
| 390 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 391 | TimeUnit.MILLISECONDS, |
| 392 | 0); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public FlowEntry getFlowEntry(FlowRule rule) { |
| 397 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 398 | |
Andrea Campanella | 5daa7c46 | 2020-03-13 12:04:23 +0100 | [diff] [blame] | 399 | if (master == null && deviceService.isAvailable(rule.deviceId())) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 400 | log.debug("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
| 401 | return null; |
| 402 | } |
| 403 | |
Andrea Campanella | 5daa7c46 | 2020-03-13 12:04:23 +0100 | [diff] [blame] | 404 | if (Objects.equals(local, master) || master == null) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 405 | return flowTable.getFlowEntry(rule); |
| 406 | } |
| 407 | |
| 408 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 409 | master, rule.deviceId()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 410 | |
| 411 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(rule, |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 412 | ECFlowRuleStoreMessageSubjects.GET_FLOW_ENTRY, |
| 413 | serializer::encode, |
| 414 | serializer::decode, |
| 415 | master), |
| 416 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 417 | TimeUnit.MILLISECONDS, |
| 418 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Jordan Halterman | 01f3bdd | 2019-04-17 11:05:16 -0700 | [diff] [blame] | 423 | return flowTable.getFlowEntries(deviceId); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public void storeFlowRule(FlowRule rule) { |
| 428 | storeBatch(new FlowRuleBatchOperation( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 429 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
| 430 | rule.deviceId(), idGenerator.getNewId())); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 435 | if (operation.getOperations().isEmpty()) { |
| 436 | notifyDelegate(FlowRuleBatchEvent.completed( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 437 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 438 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | DeviceId deviceId = operation.deviceId(); |
| 443 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 444 | |
| 445 | if (master == null) { |
| 446 | log.warn("No master for {} ", deviceId); |
| 447 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 448 | Set<FlowRule> allFailures = operation.getOperations() |
| 449 | .stream() |
| 450 | .map(op -> op.target()) |
| 451 | .collect(Collectors.toSet()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 452 | notifyDelegate(FlowRuleBatchEvent.completed( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 453 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 454 | new CompletedBatchOperation(false, allFailures, deviceId))); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
| 458 | if (Objects.equals(local, master)) { |
| 459 | storeBatchInternal(operation); |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 464 | master, deviceId); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 465 | |
| 466 | clusterCommunicator.unicast(operation, |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 467 | APPLY_BATCH_FLOWS, |
| 468 | serializer::encode, |
| 469 | master) |
| 470 | .whenComplete((result, error) -> { |
| 471 | if (error != null) { |
| 472 | log.warn("Failed to storeBatch: {} to {}", operation, master, error); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 473 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 474 | Set<FlowRule> allFailures = operation.getOperations() |
| 475 | .stream() |
| 476 | .map(op -> op.target()) |
| 477 | .collect(Collectors.toSet()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 478 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 479 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 480 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 481 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 482 | } |
| 483 | }); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
| 487 | |
| 488 | final DeviceId did = operation.deviceId(); |
| 489 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
| 490 | Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation); |
| 491 | if (currentOps.isEmpty()) { |
| 492 | batchOperationComplete(FlowRuleBatchEvent.completed( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 493 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 494 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | notifyDelegate(FlowRuleBatchEvent.requested(new |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 499 | FlowRuleBatchRequest(operation.id(), |
| 500 | currentOps), operation.deviceId())); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 504 | return operation.getOperations().stream().map( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 505 | op -> { |
| 506 | StoredFlowEntry entry; |
| 507 | switch (op.operator()) { |
| 508 | case ADD: |
| 509 | entry = new DefaultFlowEntry(op.target()); |
Jordan Halterman | c88c265 | 2019-01-14 16:23:31 -0800 | [diff] [blame] | 510 | log.debug("Adding flow rule: {}", entry); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 511 | flowTable.add(entry); |
| 512 | return op; |
| 513 | case MODIFY: |
| 514 | entry = new DefaultFlowEntry(op.target()); |
Jordan Halterman | c88c265 | 2019-01-14 16:23:31 -0800 | [diff] [blame] | 515 | log.debug("Updating flow rule: {}", entry); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 516 | flowTable.update(entry); |
| 517 | return op; |
| 518 | case REMOVE: |
| 519 | return flowTable.update(op.target(), stored -> { |
| 520 | stored.setState(FlowEntryState.PENDING_REMOVE); |
| 521 | log.debug("Setting state of rule to pending remove: {}", stored); |
pierventre | 019b0729 | 2021-05-21 12:39:09 +0200 | [diff] [blame^] | 522 | // Using the stored value instead of op to allow the removal |
| 523 | // of flows that do not specify actions or have empty treatment |
| 524 | return new FlowRuleBatchEntry(op.operator(), new DefaultFlowRule(stored)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 525 | }); |
| 526 | default: |
| 527 | log.warn("Unknown flow operation operator: {}", op.operator()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 528 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 529 | return null; |
| 530 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 531 | ).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void deleteFlowRule(FlowRule rule) { |
| 536 | storeBatch( |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 537 | new FlowRuleBatchOperation( |
| 538 | Collections.singletonList( |
| 539 | new FlowRuleBatchEntry( |
| 540 | FlowRuleOperation.REMOVE, |
| 541 | rule)), rule.deviceId(), idGenerator.getNewId())); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
| 546 | if (mastershipService.isLocalMaster(rule.deviceId())) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 547 | return flowTable.update(rule, stored -> { |
| 548 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 549 | stored.setState(FlowEntryState.PENDING_ADD); |
| 550 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 551 | } |
| 552 | return null; |
| 553 | }); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 554 | } |
| 555 | return null; |
| 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 560 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 561 | if (Objects.equals(local, master)) { |
| 562 | return addOrUpdateFlowRuleInternal(rule); |
| 563 | } |
| 564 | |
| 565 | log.warn("Tried to update FlowRule {} state," |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 566 | + " while the Node was not the master.", rule); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 567 | return null; |
| 568 | } |
| 569 | |
| 570 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 571 | FlowRuleEvent event = flowTable.update(rule, stored -> { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 572 | stored.setBytes(rule.bytes()); |
| 573 | stored.setLife(rule.life(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); |
| 574 | stored.setLiveType(rule.liveType()); |
| 575 | stored.setPackets(rule.packets()); |
| 576 | stored.setLastSeen(); |
| 577 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 578 | stored.setState(FlowEntryState.ADDED); |
| 579 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 580 | } |
| 581 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 582 | }); |
| 583 | if (event != null) { |
| 584 | return event; |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 588 | // TODO: also update backup if the behavior is correct. |
| 589 | flowTable.add(rule); |
| 590 | return null; |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 595 | final DeviceId deviceId = rule.deviceId(); |
| 596 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 597 | |
| 598 | if (Objects.equals(local, master)) { |
| 599 | // bypass and handle it locally |
| 600 | return removeFlowRuleInternal(rule); |
| 601 | } |
| 602 | |
| 603 | if (master == null) { |
| 604 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
| 605 | // TODO: revisit if this should be null (="no-op") or Exception |
| 606 | return null; |
| 607 | } |
| 608 | |
| 609 | log.trace("Forwarding removeFlowRule to {}, which is the master for device {}", |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 610 | master, deviceId); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 611 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 612 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive( |
| 613 | rule, |
| 614 | REMOVE_FLOW_ENTRY, |
| 615 | serializer::encode, |
| 616 | serializer::decode, |
| 617 | master), |
| 618 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 619 | TimeUnit.MILLISECONDS, |
| 620 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 624 | // This is where one could mark a rule as removed and still keep it in the store. |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 625 | final FlowEntry removed = flowTable.remove(rule); |
Jordan Halterman | c88c265 | 2019-01-14 16:23:31 -0800 | [diff] [blame] | 626 | log.debug("Removed flow rule: {}", removed); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 627 | // rule may be partial rule that is missing treatment, we should use rule from store instead |
| 628 | return removed != null ? new FlowRuleEvent(RULE_REMOVED, removed) : null; |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public void purgeFlowRule(DeviceId deviceId) { |
| 633 | flowTable.purgeFlowRule(deviceId); |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public void purgeFlowRules() { |
| 638 | flowTable.purgeFlowRules(); |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 643 | //FIXME: need a per device pending response |
| 644 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 645 | if (nodeId == null) { |
| 646 | notifyDelegate(event); |
| 647 | } else { |
| 648 | // TODO check unicast return value |
| 649 | clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, serializer::encode, nodeId); |
| 650 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 655 | |
| 656 | @Override |
| 657 | public void handle(final ClusterMessage message) { |
| 658 | FlowRuleBatchOperation operation = serializer.decode(message.payload()); |
| 659 | log.debug("received batch request {}", operation); |
| 660 | |
| 661 | final DeviceId deviceId = operation.deviceId(); |
| 662 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 663 | if (!Objects.equals(local, master)) { |
| 664 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 665 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 666 | failures.add(op.target()); |
| 667 | } |
| 668 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
| 669 | // This node is no longer the master, respond as all failed. |
| 670 | // TODO: we might want to wrap response in envelope |
| 671 | // to distinguish sw programming failure and hand over |
| 672 | // it make sense in the latter case to retry immediately. |
| 673 | message.respond(serializer.encode(allFailed)); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | pendingResponses.put(operation.id(), message.sender()); |
| 678 | storeBatchInternal(operation); |
| 679 | } |
| 680 | } |
| 681 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 682 | private class InternalFlowTable implements DeviceListener { |
| 683 | private final Map<DeviceId, DeviceFlowTable> flowTables = Maps.newConcurrentMap(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 684 | |
| 685 | @Override |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 686 | public void event(DeviceEvent event) { |
| 687 | if (event.type() == DeviceEvent.Type.DEVICE_ADDED) { |
| 688 | addDevice(event.subject().id()); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 692 | /** |
| 693 | * Adds the given device to the flow table. |
| 694 | * |
| 695 | * @param deviceId the device to add to the table |
| 696 | */ |
| 697 | public void addDevice(DeviceId deviceId) { |
| 698 | flowTables.computeIfAbsent(deviceId, id -> new DeviceFlowTable( |
| 699 | id, |
| 700 | clusterService, |
| 701 | clusterCommunicator, |
| 702 | new InternalLifecycleManager(id), |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 703 | deviceService, |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 704 | backupScheduler, |
| 705 | new OrderedExecutor(backupExecutor), |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 706 | backupPeriod, |
| 707 | antiEntropyPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 710 | /** |
| 711 | * Sets the flow table backup period. |
| 712 | * |
| 713 | * @param backupPeriod the flow table backup period |
| 714 | */ |
| 715 | void setBackupPeriod(int backupPeriod) { |
| 716 | flowTables.values().forEach(flowTable -> flowTable.setBackupPeriod(backupPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 719 | /** |
| 720 | * Sets the flow table anti-entropy period. |
| 721 | * |
| 722 | * @param antiEntropyPeriod the flow table anti-entropy period |
| 723 | */ |
| 724 | void setAntiEntropyPeriod(int antiEntropyPeriod) { |
| 725 | flowTables.values().forEach(flowTable -> flowTable.setAntiEntropyPeriod(antiEntropyPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Returns the flow table for a specific device. |
| 730 | * |
| 731 | * @param deviceId the device identifier |
| 732 | * @return the flow table for the given device |
| 733 | */ |
| 734 | private DeviceFlowTable getFlowTable(DeviceId deviceId) { |
| 735 | DeviceFlowTable flowTable = flowTables.get(deviceId); |
| 736 | return flowTable != null ? flowTable : flowTables.computeIfAbsent(deviceId, id -> new DeviceFlowTable( |
| 737 | deviceId, |
| 738 | clusterService, |
| 739 | clusterCommunicator, |
| 740 | new InternalLifecycleManager(deviceId), |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 741 | deviceService, |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 742 | backupScheduler, |
| 743 | new OrderedExecutor(backupExecutor), |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 744 | backupPeriod, |
| 745 | antiEntropyPeriod)); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 748 | /** |
| 749 | * Returns the flow rule count for the given device. |
| 750 | * |
| 751 | * @param deviceId the device for which to return the flow rule count |
| 752 | * @return the flow rule count for the given device |
| 753 | */ |
| 754 | public int getFlowRuleCount(DeviceId deviceId) { |
| 755 | return getFlowTable(deviceId).count(); |
| 756 | } |
| 757 | |
| 758 | /** |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 759 | * Returns the count of flow rules in the given state for the given device. |
| 760 | * |
| 761 | * @param deviceId the device for which to return the flow rule count |
| 762 | * @return the flow rule count for the given device |
| 763 | */ |
| 764 | public int getFlowRuleCount(DeviceId deviceId, FlowEntryState state) { |
| 765 | if (state == null) { |
| 766 | return getFlowRuleCount(deviceId); |
| 767 | } |
Jordan Halterman | 01f3bdd | 2019-04-17 11:05:16 -0700 | [diff] [blame] | 768 | return (int) StreamSupport.stream(getFlowEntries(deviceId).spliterator(), false) |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 769 | .filter(rule -> rule.state() == state) |
| 770 | .count(); |
| 771 | } |
| 772 | |
| 773 | /** |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 774 | * Returns the flow entry for the given rule. |
| 775 | * |
| 776 | * @param rule the rule for which to return the flow entry |
| 777 | * @return the flow entry for the given rule |
| 778 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 779 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 780 | return getFlowTable(rule.deviceId()).getFlowEntry(rule); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 783 | /** |
| 784 | * Returns the set of flow entries for the given device. |
| 785 | * |
| 786 | * @param deviceId the device for which to lookup flow entries |
| 787 | * @return the set of flow entries for the given device |
| 788 | */ |
Jordan Halterman | 01f3bdd | 2019-04-17 11:05:16 -0700 | [diff] [blame] | 789 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 790 | try { |
| 791 | return getFlowTable(deviceId).getFlowEntries() |
Jordan Halterman | 4330078 | 2019-05-21 11:27:50 -0700 | [diff] [blame] | 792 | .get(GET_FLOW_ENTRIES_TIMEOUT, TimeUnit.SECONDS); |
Jordan Halterman | 01f3bdd | 2019-04-17 11:05:16 -0700 | [diff] [blame] | 793 | } catch (ExecutionException e) { |
| 794 | throw new FlowRuleStoreException(e.getCause()); |
| 795 | } catch (TimeoutException e) { |
| 796 | throw new FlowRuleStoreException.Timeout(); |
| 797 | } catch (InterruptedException e) { |
| 798 | throw new FlowRuleStoreException.Interrupted(); |
| 799 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 802 | /** |
| 803 | * Adds the given flow rule. |
| 804 | * |
| 805 | * @param rule the rule to add |
| 806 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 807 | public void add(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 808 | Tools.futureGetOrElse( |
| 809 | getFlowTable(rule.deviceId()).add(rule), |
| 810 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 811 | TimeUnit.MILLISECONDS, |
| 812 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 815 | /** |
| 816 | * Updates the given flow rule. |
| 817 | * |
| 818 | * @param rule the rule to update |
| 819 | */ |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 820 | public void update(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 821 | Tools.futureGetOrElse( |
| 822 | getFlowTable(rule.deviceId()).update(rule), |
| 823 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 824 | TimeUnit.MILLISECONDS, |
| 825 | null); |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 828 | /** |
| 829 | * Applies the given update function to the rule. |
| 830 | * |
| 831 | * @param function the update function to apply |
| 832 | * @return a future to be completed with the update event or {@code null} if the rule was not updated |
| 833 | */ |
| 834 | public <T> T update(FlowRule rule, Function<StoredFlowEntry, T> function) { |
| 835 | return Tools.futureGetOrElse( |
| 836 | getFlowTable(rule.deviceId()).update(rule, function), |
| 837 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 838 | TimeUnit.MILLISECONDS, |
| 839 | null); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Removes the given flow rule. |
| 844 | * |
| 845 | * @param rule the rule to remove |
| 846 | */ |
Jordan Halterman | 2edfeef | 2018-01-16 14:59:49 -0800 | [diff] [blame] | 847 | public FlowEntry remove(FlowEntry rule) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 848 | return Tools.futureGetOrElse( |
| 849 | getFlowTable(rule.deviceId()).remove(rule), |
| 850 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 851 | TimeUnit.MILLISECONDS, |
| 852 | null); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 855 | /** |
| 856 | * Purges flow rules for the given device. |
| 857 | * |
| 858 | * @param deviceId the device for which to purge flow rules |
| 859 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 860 | public void purgeFlowRule(DeviceId deviceId) { |
Jordan Halterman | da3b9f0 | 2018-10-05 13:28:51 -0700 | [diff] [blame] | 861 | // If the device is still present in the store, purge the underlying DeviceFlowTable. |
| 862 | // Otherwise, remove the DeviceFlowTable and unregister message handlers. |
| 863 | if (deviceService.getDevice(deviceId) != null) { |
| 864 | DeviceFlowTable flowTable = flowTables.get(deviceId); |
| 865 | if (flowTable != null) { |
| 866 | flowTable.purge(); |
| 867 | } |
| 868 | } else { |
| 869 | DeviceFlowTable flowTable = flowTables.remove(deviceId); |
| 870 | if (flowTable != null) { |
| 871 | flowTable.close(); |
| 872 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 873 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 876 | /** |
| 877 | * Purges all flow rules from the table. |
| 878 | */ |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 879 | public void purgeFlowRules() { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 880 | Iterator<DeviceFlowTable> iterator = flowTables.values().iterator(); |
| 881 | while (iterator.hasNext()) { |
| 882 | iterator.next().close(); |
| 883 | iterator.remove(); |
Jordan Halterman | 8f90d6d | 2018-06-12 11:23:33 -0700 | [diff] [blame] | 884 | } |
Jordan Halterman | b2f5795 | 2018-03-21 12:52:37 -0700 | [diff] [blame] | 885 | } |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | @Override |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 889 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, List<TableStatisticsEntry> tableStats) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 890 | deviceTableStats.put(deviceId, tableStats); |
| 891 | return null; |
| 892 | } |
| 893 | |
| 894 | @Override |
| 895 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
| 896 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 897 | |
Andrea Campanella | 5daa7c46 | 2020-03-13 12:04:23 +0100 | [diff] [blame] | 898 | if (master == null && deviceService.isAvailable(deviceId)) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 899 | log.debug("Failed to getTableStats: No master for {}", deviceId); |
| 900 | return Collections.emptyList(); |
| 901 | } |
| 902 | |
| 903 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 904 | if (tableStats == null) { |
| 905 | return Collections.emptyList(); |
| 906 | } |
| 907 | return ImmutableList.copyOf(tableStats); |
| 908 | } |
| 909 | |
| 910 | @Override |
| 911 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 912 | return Streams.stream(getTableStatistics(deviceId)) |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 913 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 914 | .sum(); |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | private class InternalTableStatsListener |
| 918 | implements EventuallyConsistentMapListener<DeviceId, List<TableStatisticsEntry>> { |
| 919 | @Override |
| 920 | public void event(EventuallyConsistentMapEvent<DeviceId, |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 921 | List<TableStatisticsEntry>> event) { |
Jon Hall | fa13229 | 2017-10-24 11:11:24 -0700 | [diff] [blame] | 922 | //TODO: Generate an event to listeners (do we need?) |
| 923 | } |
| 924 | } |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 925 | |
| 926 | /** |
| 927 | * Device lifecycle manager implementation. |
| 928 | */ |
| 929 | private final class InternalLifecycleManager |
| 930 | extends AbstractListenerManager<LifecycleEvent, LifecycleEventListener> |
| 931 | implements LifecycleManager, ReplicaInfoEventListener, MapEventListener<DeviceId, Long> { |
| 932 | |
| 933 | private final DeviceId deviceId; |
| 934 | |
| 935 | private volatile DeviceReplicaInfo replicaInfo; |
| 936 | |
| 937 | InternalLifecycleManager(DeviceId deviceId) { |
| 938 | this.deviceId = deviceId; |
| 939 | replicaInfoManager.addListener(this); |
| 940 | mastershipTermLifecycles.addListener(this); |
| 941 | replicaInfo = toDeviceReplicaInfo(replicaInfoManager.getReplicaInfoFor(deviceId)); |
| 942 | } |
| 943 | |
| 944 | @Override |
| 945 | public DeviceReplicaInfo getReplicaInfo() { |
| 946 | return replicaInfo; |
| 947 | } |
| 948 | |
| 949 | @Override |
| 950 | public void activate(long term) { |
| 951 | final ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 952 | if (replicaInfo != null && replicaInfo.term() == term) { |
| 953 | mastershipTermLifecycles.put(deviceId, term); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public void event(ReplicaInfoEvent event) { |
Jordan Halterman | e4bf856 | 2018-06-22 16:58:08 -0700 | [diff] [blame] | 959 | if (event.subject().equals(deviceId)) { |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 960 | onReplicaInfoChange(event.replicaInfo()); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | @Override |
| 965 | public void event(MapEvent<DeviceId, Long> event) { |
| 966 | if (event.key().equals(deviceId) && event.newValue() != null) { |
| 967 | onActivate(event.newValue().value()); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Handles a term activation event. |
| 973 | * |
| 974 | * @param term the term that was activated |
| 975 | */ |
| 976 | private void onActivate(long term) { |
| 977 | final ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 978 | if (replicaInfo != null && replicaInfo.term() == term) { |
| 979 | NodeId master = replicaInfo.master().orElse(null); |
| 980 | List<NodeId> backups = replicaInfo.backups() |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 981 | .subList(0, min(replicaInfo.backups().size(), backupCount)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 982 | listenerRegistry.process(new LifecycleEvent( |
| 983 | LifecycleEvent.Type.TERM_ACTIVE, |
| 984 | new DeviceReplicaInfo(term, master, backups))); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * Handles a replica info change event. |
| 990 | * |
| 991 | * @param replicaInfo the updated replica info |
| 992 | */ |
| 993 | private synchronized void onReplicaInfoChange(ReplicaInfo replicaInfo) { |
| 994 | DeviceReplicaInfo oldReplicaInfo = this.replicaInfo; |
| 995 | this.replicaInfo = toDeviceReplicaInfo(replicaInfo); |
| 996 | if (oldReplicaInfo == null || oldReplicaInfo.term() < replicaInfo.term()) { |
| 997 | if (oldReplicaInfo != null) { |
| 998 | listenerRegistry.process(new LifecycleEvent(LifecycleEvent.Type.TERM_END, oldReplicaInfo)); |
| 999 | } |
| 1000 | listenerRegistry.process(new LifecycleEvent(LifecycleEvent.Type.TERM_START, this.replicaInfo)); |
Jordan Halterman | e4bf856 | 2018-06-22 16:58:08 -0700 | [diff] [blame] | 1001 | } else if (oldReplicaInfo.term() == replicaInfo.term()) { |
| 1002 | listenerRegistry.process(new LifecycleEvent(LifecycleEvent.Type.TERM_UPDATE, this.replicaInfo)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Converts the given replica info into a {@link DeviceReplicaInfo} instance. |
| 1008 | * |
| 1009 | * @param replicaInfo the replica info to convert |
| 1010 | * @return the converted replica info |
| 1011 | */ |
| 1012 | private DeviceReplicaInfo toDeviceReplicaInfo(ReplicaInfo replicaInfo) { |
| 1013 | NodeId master = replicaInfo.master().orElse(null); |
| 1014 | List<NodeId> backups = replicaInfo.backups() |
Jordan Halterman | aeda275 | 2019-02-22 12:31:25 -0800 | [diff] [blame] | 1015 | .subList(0, min(replicaInfo.backups().size(), backupCount)); |
Jordan Halterman | 281dbf3 | 2018-06-15 17:46:28 -0700 | [diff] [blame] | 1016 | return new DeviceReplicaInfo(replicaInfo.term(), master, backups); |
| 1017 | } |
| 1018 | |
| 1019 | @Override |
| 1020 | public void close() { |
| 1021 | replicaInfoManager.removeListener(this); |
| 1022 | mastershipTermLifecycles.removeListener(this); |
| 1023 | } |
| 1024 | } |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 1025 | |
| 1026 | private static class CountMessage { |
| 1027 | private final DeviceId deviceId; |
| 1028 | private final FlowEntryState state; |
| 1029 | |
| 1030 | CountMessage(DeviceId deviceId, FlowEntryState state) { |
| 1031 | this.deviceId = deviceId; |
| 1032 | this.state = state; |
| 1033 | } |
| 1034 | } |
Jordan Halterman | 5259b33 | 2018-06-12 15:34:19 -0700 | [diff] [blame] | 1035 | } |