Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.flow.impl; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 17 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 18 | import com.google.common.cache.CacheBuilder; |
| 19 | import com.google.common.cache.CacheLoader; |
| 20 | import com.google.common.cache.LoadingCache; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.Iterables; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 25 | import com.hazelcast.core.IMap; |
| 26 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Modified; |
| 31 | import org.apache.felix.scr.annotations.Property; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 35 | import org.onlab.util.BoundedThreadPool; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 36 | import org.onlab.util.KryoNamespace; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 37 | import org.onlab.util.NewConcurrentHashMap; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 38 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.cluster.ClusterService; |
| 40 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 41 | import org.onosproject.core.CoreService; |
| 42 | import org.onosproject.core.IdGenerator; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.device.DeviceService; |
| 46 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 47 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 48 | import org.onosproject.net.flow.FlowEntry; |
| 49 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 50 | import org.onosproject.net.flow.FlowId; |
| 51 | import org.onosproject.net.flow.FlowRule; |
| 52 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 55 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 56 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 57 | import org.onosproject.net.flow.FlowRuleEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 59 | import org.onosproject.net.flow.FlowRuleService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.FlowRuleStore; |
| 61 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 62 | import org.onosproject.net.flow.StoredFlowEntry; |
| 63 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 64 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 65 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 66 | import org.onosproject.store.flow.ReplicaInfo; |
| 67 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 68 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 69 | import org.onosproject.store.flow.ReplicaInfoService; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 70 | import org.onosproject.store.hz.AbstractHazelcastStore; |
| 71 | import org.onosproject.store.hz.SMap; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 72 | import org.onosproject.store.serializers.KryoSerializer; |
| 73 | import org.onosproject.store.serializers.StoreSerializer; |
| 74 | import org.onosproject.store.serializers.impl.DistributedStoreSerializers; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 75 | import org.osgi.service.component.ComponentContext; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 78 | import java.io.IOException; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 79 | import java.util.ArrayList; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 80 | import java.util.Arrays; |
| 81 | import java.util.Collections; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 82 | import java.util.Dictionary; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 83 | import java.util.HashSet; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 84 | import java.util.List; |
| 85 | import java.util.Map; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 86 | import java.util.Map.Entry; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 87 | import java.util.Set; |
| 88 | import java.util.concurrent.ConcurrentHashMap; |
| 89 | import java.util.concurrent.ConcurrentMap; |
| 90 | import java.util.concurrent.CopyOnWriteArraySet; |
| 91 | import java.util.concurrent.ExecutionException; |
| 92 | import java.util.concurrent.ExecutorService; |
| 93 | import java.util.concurrent.Executors; |
| 94 | import java.util.concurrent.Future; |
| 95 | import java.util.concurrent.TimeUnit; |
| 96 | import java.util.concurrent.TimeoutException; |
| 97 | import java.util.stream.Collectors; |
| 98 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 99 | import static com.google.common.base.Preconditions.checkNotNull; |
| 100 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 101 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 102 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 103 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 104 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 105 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.*; |
| 106 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 109 | * Manages inventory of flow rules using a distributed state management protocol. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 110 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 111 | @Component(immediate = true) |
| 112 | @Service |
| 113 | public class DistributedFlowRuleStore |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 114 | extends AbstractHazelcastStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 115 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 116 | |
| 117 | private final Logger log = getLogger(getClass()); |
| 118 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 119 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 120 | private static final boolean DEFAULT_BACKUP_ENABLED = true; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 121 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
| 122 | |
| 123 | @Property(name = "msgHandlerPoolSize", intValue = MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 124 | label = "Number of threads in the message handler pool") |
| 125 | private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 126 | |
| 127 | @Property(name = "backupEnabled", boolValue = DEFAULT_BACKUP_ENABLED, |
| 128 | label = "Indicates whether backups are enabled or not") |
| 129 | private boolean backupEnabled = DEFAULT_BACKUP_ENABLED; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 130 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 131 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 132 | |
| 133 | /*private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, Set<StoredFlowEntry>>> |
| 134 | flowEntries = new ConcurrentHashMap<>();*/ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 135 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 137 | protected ReplicaInfoService replicaInfoManager; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 138 | |
| 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 140 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 141 | |
| 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 143 | protected ClusterService clusterService; |
| 144 | |
| 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 146 | protected DeviceService deviceService; |
| 147 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 149 | protected CoreService coreService; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 150 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 152 | protected ComponentConfigService configService; |
| 153 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 154 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 155 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 156 | // Cache of SMaps used for backup data. each SMap contain device flow table |
| 157 | private LoadingCache<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> smaps; |
| 158 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 159 | private ExecutorService messageHandlingExecutor; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 160 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 161 | private final ExecutorService backupExecutors = |
| 162 | BoundedThreadPool.newSingleThreadExecutor(groupedThreads("onos/flow", "async-backups")); |
| 163 | //Executors.newSingleThreadExecutor(groupedThreads("onos/flow", "async-backups")); |
| 164 | |
| 165 | private boolean syncBackup = false; |
| 166 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 167 | protected static final StoreSerializer SERIALIZER = new KryoSerializer() { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 168 | @Override |
| 169 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 170 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 171 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 172 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
Yuta HIGUCHI | 2f15833 | 2014-11-25 13:32:06 -0800 | [diff] [blame] | 173 | .register(FlowRuleEvent.class) |
Jonathan Hart | 4fb5cde | 2014-12-22 12:09:07 -0800 | [diff] [blame] | 174 | .register(FlowRuleEvent.Type.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 175 | .build(); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 176 | } |
| 177 | }; |
| 178 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 179 | private ReplicaInfoEventListener replicaInfoEventListener; |
| 180 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 181 | private IdGenerator idGenerator; |
| 182 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 183 | @Activate |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 184 | public void activate(ComponentContext context) { |
| 185 | configService.registerProperties(getClass()); |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 186 | super.serializer = SERIALIZER; |
| 187 | super.theInstance = storeService.getHazelcastInstance(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 188 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 189 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 190 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 191 | // Cache to create SMap on demand |
| 192 | smaps = CacheBuilder.newBuilder() |
| 193 | .softValues() |
| 194 | .build(new SMapLoader()); |
| 195 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 196 | final NodeId local = clusterService.getLocalNode().id(); |
| 197 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 198 | messageHandlingExecutor = Executors.newFixedThreadPool( |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 199 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers")); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 200 | |
| 201 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(local), messageHandlingExecutor); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 202 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 203 | clusterCommunicator.addSubscriber(REMOTE_APPLY_COMPLETED, new ClusterMessageHandler() { |
| 204 | @Override |
| 205 | public void handle(ClusterMessage message) { |
| 206 | FlowRuleBatchEvent event = SERIALIZER.decode(message.payload()); |
| 207 | log.trace("received completed notification for {}", event); |
| 208 | notifyDelegate(event); |
| 209 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 210 | }, messageHandlingExecutor); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 211 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 212 | clusterCommunicator.addSubscriber(GET_FLOW_ENTRY, new ClusterMessageHandler() { |
| 213 | |
| 214 | @Override |
| 215 | public void handle(ClusterMessage message) { |
| 216 | FlowRule rule = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 217 | log.trace("received get flow entry request for {}", rule); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 218 | FlowEntry flowEntry = flowTable.getFlowEntry(rule); //getFlowEntryInternal(rule); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 219 | try { |
| 220 | message.respond(SERIALIZER.encode(flowEntry)); |
| 221 | } catch (IOException e) { |
| 222 | log.error("Failed to respond back", e); |
| 223 | } |
| 224 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 225 | }, messageHandlingExecutor); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 226 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 227 | clusterCommunicator.addSubscriber(GET_DEVICE_FLOW_ENTRIES, new ClusterMessageHandler() { |
| 228 | |
| 229 | @Override |
| 230 | public void handle(ClusterMessage message) { |
| 231 | DeviceId deviceId = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 232 | log.trace("Received get flow entries request for {} from {}", deviceId, message.sender()); |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 233 | Set<FlowEntry> flowEntries = flowTable.getFlowEntries(deviceId); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 234 | try { |
| 235 | message.respond(SERIALIZER.encode(flowEntries)); |
| 236 | } catch (IOException e) { |
| 237 | log.error("Failed to respond to peer's getFlowEntries request", e); |
| 238 | } |
| 239 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 240 | }, messageHandlingExecutor); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 241 | |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 242 | clusterCommunicator.addSubscriber(REMOVE_FLOW_ENTRY, new ClusterMessageHandler() { |
| 243 | |
| 244 | @Override |
| 245 | public void handle(ClusterMessage message) { |
| 246 | FlowEntry rule = SERIALIZER.decode(message.payload()); |
| 247 | log.trace("received get flow entry request for {}", rule); |
| 248 | FlowRuleEvent event = removeFlowRuleInternal(rule); |
| 249 | try { |
| 250 | message.respond(SERIALIZER.encode(event)); |
| 251 | } catch (IOException e) { |
| 252 | log.error("Failed to respond back", e); |
| 253 | } |
| 254 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 255 | }, messageHandlingExecutor); |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 256 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 257 | replicaInfoEventListener = new InternalReplicaInfoEventListener(); |
| 258 | |
| 259 | replicaInfoManager.addListener(replicaInfoEventListener); |
| 260 | |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 261 | logConfig("Started"); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Deactivate |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 265 | public void deactivate(ComponentContext context) { |
| 266 | configService.unregisterProperties(getClass(), false); |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 267 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
| 268 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_ENTRIES); |
| 269 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 270 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 271 | clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 272 | messageHandlingExecutor.shutdown(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 273 | replicaInfoManager.removeListener(replicaInfoEventListener); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 274 | log.info("Stopped"); |
| 275 | } |
| 276 | |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 277 | @Modified |
| 278 | public void modified(ComponentContext context) { |
| 279 | if (context == null) { |
| 280 | backupEnabled = DEFAULT_BACKUP_ENABLED; |
| 281 | logConfig("Default config"); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | Dictionary properties = context.getProperties(); |
| 286 | int newPoolSize; |
| 287 | boolean newBackupEnabled; |
| 288 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 289 | String s = get(properties, "msgHandlerPoolSize"); |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 290 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 291 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 292 | s = get(properties, "backupEnabled"); |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 293 | newBackupEnabled = isNullOrEmpty(s) ? backupEnabled : Boolean.parseBoolean(s.trim()); |
| 294 | |
| 295 | } catch (NumberFormatException | ClassCastException e) { |
| 296 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 297 | newBackupEnabled = DEFAULT_BACKUP_ENABLED; |
| 298 | } |
| 299 | |
| 300 | if (newPoolSize != msgHandlerPoolSize || newBackupEnabled != backupEnabled) { |
| 301 | msgHandlerPoolSize = newPoolSize; |
| 302 | backupEnabled = newBackupEnabled; |
Brian O'Connor | c4f351d | 2015-03-10 20:53:11 -0700 | [diff] [blame] | 303 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 304 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 305 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers")); |
| 306 | oldMsgHandler.shutdown(); |
| 307 | logConfig("Reconfigured"); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private void logConfig(String prefix) { |
| 312 | log.info("{} with msgHandlerPoolSize = {}; backupEnabled = {}", |
| 313 | prefix, msgHandlerPoolSize, backupEnabled); |
| 314 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 315 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 316 | |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 317 | // This is not a efficient operation on a distributed sharded |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 318 | // flow store. We need to revisit the need for this operation or at least |
| 319 | // make it device specific. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 320 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 321 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 322 | // implementing in-efficient operation for debugging purpose. |
| 323 | int sum = 0; |
| 324 | for (Device device : deviceService.getDevices()) { |
| 325 | final DeviceId did = device.id(); |
| 326 | sum += Iterables.size(getFlowEntries(did)); |
| 327 | } |
| 328 | return sum; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 332 | public FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 333 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 334 | |
| 335 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 336 | log.warn("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 337 | return null; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 340 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 341 | return flowTable.getFlowEntry(rule); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 344 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 345 | replicaInfo.master().orNull(), rule.deviceId()); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 346 | |
| 347 | ClusterMessage message = new ClusterMessage( |
| 348 | clusterService.getLocalNode().id(), |
| 349 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 350 | SERIALIZER.encode(rule)); |
| 351 | |
| 352 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 353 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 354 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 355 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 356 | log.warn("Unable to fetch flow store contents from {}", replicaInfo.master().get()); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 357 | } |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 358 | return null; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 361 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 362 | |
| 363 | @Override |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 364 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 365 | |
| 366 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 367 | |
| 368 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 369 | log.warn("Failed to getFlowEntries: No master for {}", deviceId); |
Yuta HIGUCHI | 2c1d847 | 2014-10-31 14:13:38 -0700 | [diff] [blame] | 370 | return Collections.emptyList(); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 373 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 374 | return flowTable.getFlowEntries(deviceId); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 377 | log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 378 | replicaInfo.master().orNull(), deviceId); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 379 | |
| 380 | ClusterMessage message = new ClusterMessage( |
| 381 | clusterService.getLocalNode().id(), |
| 382 | GET_DEVICE_FLOW_ENTRIES, |
| 383 | SERIALIZER.encode(deviceId)); |
| 384 | |
| 385 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 386 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 387 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 388 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 389 | log.warn("Unable to fetch flow store contents from {}", replicaInfo.master().get()); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 390 | } |
Yuta HIGUCHI | 24f79eb | 2014-12-12 15:46:43 -0800 | [diff] [blame] | 391 | return Collections.emptyList(); |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 394 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 395 | |
| 396 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 397 | public void storeFlowRule(FlowRule rule) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 398 | storeBatch(new FlowRuleBatchOperation( |
| 399 | Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
| 400 | rule.deviceId(), idGenerator.getNewId())); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 403 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 404 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 405 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 406 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 407 | if (operation.getOperations().isEmpty()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 408 | |
| 409 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 410 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 411 | new CompletedBatchOperation(true, Collections.emptySet(), |
| 412 | operation.deviceId()))); |
| 413 | return; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 414 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 415 | |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 416 | DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 417 | |
| 418 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 419 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 420 | if (!replicaInfo.master().isPresent()) { |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 421 | log.warn("No master for {} : flows will be marked for removal", deviceId); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 422 | |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 423 | updateStoreInternal(operation); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 424 | |
| 425 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 426 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 427 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 428 | return; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | final NodeId local = clusterService.getLocalNode().id(); |
| 432 | if (replicaInfo.master().get().equals(local)) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 433 | storeBatchInternal(operation); |
| 434 | return; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 437 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 438 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 439 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 440 | ClusterMessage message = new ClusterMessage( |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 441 | local, |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 442 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 443 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 444 | |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 445 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 446 | if (!clusterCommunicator.unicast(message, replicaInfo.master().get())) { |
| 447 | log.warn("Failed to storeBatch: {} to {}", message, replicaInfo.master()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 448 | |
| 449 | Set<FlowRule> allFailures = operation.getOperations().stream() |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 450 | .map(op -> op.target()) |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 451 | .collect(Collectors.toSet()); |
| 452 | |
| 453 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 454 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 455 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 456 | return; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 460 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | e9b2b00 | 2014-11-04 12:25:47 -0800 | [diff] [blame] | 461 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 462 | final DeviceId did = operation.deviceId(); |
| 463 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 464 | Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation); |
| 465 | if (currentOps.isEmpty()) { |
| 466 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 467 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 468 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
| 469 | return; |
| 470 | } |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 471 | updateBackup(did, currentOps); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 472 | |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 473 | notifyDelegate(FlowRuleBatchEvent.requested(new |
| 474 | FlowRuleBatchRequest(operation.id(), |
| 475 | currentOps), operation.deviceId())); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 476 | |
alshabib | 371abe8 | 2015-02-13 10:44:17 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 480 | return operation.getOperations().stream().map( |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 481 | op -> { |
| 482 | StoredFlowEntry entry; |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 483 | switch (op.operator()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 484 | case ADD: |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 485 | entry = new DefaultFlowEntry(op.target()); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 486 | // always add requested FlowRule |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 487 | // Note: 2 equal FlowEntry may have different treatment |
| 488 | flowTable.remove(entry.deviceId(), entry); |
| 489 | flowTable.add(entry); |
| 490 | |
| 491 | return op; |
| 492 | case REMOVE: |
| 493 | entry = flowTable.getFlowEntry(op.target()); |
| 494 | if (entry != null) { |
| 495 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 496 | return op; |
| 497 | } |
| 498 | break; |
| 499 | case MODIFY: |
| 500 | //TODO: figure this out at some point |
| 501 | break; |
| 502 | default: |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 503 | log.warn("Unknown flow operation operator: {}", op.operator()); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 504 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 505 | return null; |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 506 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 507 | ).filter(op -> op != null).collect(Collectors.toSet()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 510 | private void updateBackup(DeviceId deviceId, final Set<FlowRuleBatchEntry> entries) { |
| 511 | if (!backupEnabled) { |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | Future<?> backup = backupExecutors.submit(new UpdateBackup(deviceId, entries)); |
| 516 | |
| 517 | if (syncBackup) { |
| 518 | // wait for backup to complete |
| 519 | try { |
| 520 | backup.get(); |
| 521 | } catch (InterruptedException | ExecutionException e) { |
| 522 | log.error("Failed to create backups", e); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 527 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 528 | public void deleteFlowRule(FlowRule rule) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 529 | storeBatch( |
| 530 | new FlowRuleBatchOperation( |
| 531 | Arrays.asList( |
| 532 | new FlowRuleBatchEntry( |
| 533 | FlowRuleOperation.REMOVE, |
| 534 | rule)), rule.deviceId(), idGenerator.getNewId())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 538 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 539 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 540 | final NodeId localId = clusterService.getLocalNode().id(); |
| 541 | if (localId.equals(replicaInfo.master().orNull())) { |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 542 | return addOrUpdateFlowRuleInternal(rule); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 545 | log.warn("Tried to update FlowRule {} state," |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 546 | + " while the Node was not the master.", rule); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 547 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 550 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
| 551 | final DeviceId did = rule.deviceId(); |
| 552 | |
| 553 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 554 | // check if this new rule is an update to an existing entry |
| 555 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 556 | if (stored != null) { |
| 557 | stored.setBytes(rule.bytes()); |
| 558 | stored.setLife(rule.life()); |
| 559 | stored.setPackets(rule.packets()); |
| 560 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 561 | stored.setState(FlowEntryState.ADDED); |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 562 | FlowRuleBatchEntry entry = |
| 563 | new FlowRuleBatchEntry(FlowRuleOperation.ADD, stored); |
| 564 | updateBackup(did, Sets.newHashSet(entry)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 565 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 566 | } |
| 567 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 568 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 569 | |
| 570 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 571 | // TODO: also update backup if the behavior is correct. |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 572 | flowTable.add(rule); |
| 573 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 574 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 575 | return null; |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 576 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 580 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 581 | final DeviceId deviceId = rule.deviceId(); |
| 582 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 583 | |
| 584 | final NodeId localId = clusterService.getLocalNode().id(); |
| 585 | if (localId.equals(replicaInfo.master().orNull())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 586 | // bypass and handle it locally |
| 587 | return removeFlowRuleInternal(rule); |
| 588 | } |
| 589 | |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 590 | if (!replicaInfo.master().isPresent()) { |
Yuta HIGUCHI | 6b98ab6 | 2014-12-03 16:08:08 -0800 | [diff] [blame] | 591 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 592 | // TODO: revisit if this should be null (="no-op") or Exception |
| 593 | return null; |
| 594 | } |
| 595 | |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 596 | log.trace("Forwarding removeFlowRule to {}, which is the primary (master) for device {}", |
Yuta HIGUCHI | b6eb914 | 2014-11-26 16:18:13 -0800 | [diff] [blame] | 597 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 598 | |
| 599 | ClusterMessage message = new ClusterMessage( |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 600 | clusterService.getLocalNode().id(), |
| 601 | REMOVE_FLOW_ENTRY, |
| 602 | SERIALIZER.encode(rule)); |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 603 | |
| 604 | try { |
| 605 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 606 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 607 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 608 | // TODO: Retry against latest master or throw a FlowStoreException |
Yuta HIGUCHI | dfa45c1 | 2014-11-24 18:38:53 -0800 | [diff] [blame] | 609 | throw new RuntimeException(e); |
| 610 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 613 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 614 | final DeviceId deviceId = rule.deviceId(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 615 | // This is where one could mark a rule as removed and still keep it in the store. |
| 616 | final boolean removed = flowTable.remove(deviceId, rule); //flowEntries.remove(deviceId, rule); |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 617 | FlowRuleBatchEntry entry = |
| 618 | new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule); |
| 619 | updateBackup(deviceId, Sets.newHashSet(entry)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 620 | if (removed) { |
| 621 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 622 | } else { |
| 623 | return null; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 624 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 625 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 626 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 627 | |
| 628 | @Override |
| 629 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 630 | //FIXME: need a per device pending response |
| 631 | |
| 632 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 633 | if (nodeId == null) { |
| 634 | notifyDelegate(event); |
| 635 | } else { |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 636 | ClusterMessage message = new ClusterMessage( |
| 637 | clusterService.getLocalNode().id(), |
| 638 | REMOTE_APPLY_COMPLETED, |
| 639 | SERIALIZER.encode(event)); |
| 640 | // TODO check unicast return value |
| 641 | clusterCommunicator.unicast(message, nodeId); |
| 642 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 643 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 644 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 645 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 646 | private void loadFromBackup(final DeviceId did) { |
| 647 | if (!backupEnabled) { |
| 648 | return; |
| 649 | } |
| 650 | log.info("We are now the master for {}. Will load flow rules from backup", did); |
| 651 | try { |
| 652 | log.debug("Loading FlowRules for {} from backups", did); |
| 653 | SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(did); |
| 654 | for (Entry<FlowId, ImmutableList<StoredFlowEntry>> e |
| 655 | : backupFlowTable.entrySet()) { |
| 656 | |
| 657 | log.trace("loading {}", e.getValue()); |
| 658 | for (StoredFlowEntry entry : e.getValue()) { |
| 659 | flowTable.getFlowEntriesById(entry).remove(entry); |
| 660 | flowTable.getFlowEntriesById(entry).add(entry); |
| 661 | |
| 662 | |
| 663 | } |
| 664 | } |
| 665 | } catch (ExecutionException e) { |
| 666 | log.error("Failed to load backup flowtable for {}", did, e); |
| 667 | } |
| 668 | } |
| 669 | |
Yuta HIGUCHI | 885868f | 2014-11-13 19:12:07 -0800 | [diff] [blame] | 670 | private void removeFromPrimary(final DeviceId did) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 671 | flowTable.clearDevice(did); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 674 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 675 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 676 | private final NodeId local; |
| 677 | |
| 678 | private OnStoreBatch(NodeId local) { |
| 679 | this.local = local; |
| 680 | } |
| 681 | |
| 682 | @Override |
| 683 | public void handle(final ClusterMessage message) { |
| 684 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
Yuta HIGUCHI | faf9e1c | 2014-11-20 00:31:29 -0800 | [diff] [blame] | 685 | log.debug("received batch request {}", operation); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 686 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 687 | final DeviceId deviceId = operation.deviceId(); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 688 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 689 | if (!local.equals(replicaInfo.master().orNull())) { |
| 690 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 691 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 692 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 693 | failures.add(op.target()); |
| 694 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 695 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 696 | // This node is no longer the master, respond as all failed. |
| 697 | // TODO: we might want to wrap response in envelope |
| 698 | // to distinguish sw programming failure and hand over |
| 699 | // it make sense in the latter case to retry immediately. |
| 700 | try { |
| 701 | message.respond(SERIALIZER.encode(allFailed)); |
| 702 | } catch (IOException e) { |
| 703 | log.error("Failed to respond back", e); |
| 704 | } |
| 705 | return; |
| 706 | } |
| 707 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 708 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 709 | pendingResponses.put(operation.id(), message.sender()); |
| 710 | storeBatchInternal(operation); |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 711 | |
Yuta HIGUCHI | ea15015 | 2014-10-28 22:55:14 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 715 | private final class SMapLoader |
| 716 | extends CacheLoader<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> { |
| 717 | |
| 718 | @Override |
| 719 | public SMap<FlowId, ImmutableList<StoredFlowEntry>> load(DeviceId id) |
| 720 | throws Exception { |
| 721 | IMap<byte[], byte[]> map = theInstance.getMap("flowtable_" + id.toString()); |
| 722 | return new SMap<FlowId, ImmutableList<StoredFlowEntry>>(map, SERIALIZER); |
| 723 | } |
| 724 | } |
| 725 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 726 | private final class InternalReplicaInfoEventListener |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 727 | implements ReplicaInfoEventListener { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 728 | |
| 729 | @Override |
| 730 | public void event(ReplicaInfoEvent event) { |
| 731 | final NodeId local = clusterService.getLocalNode().id(); |
| 732 | final DeviceId did = event.subject(); |
| 733 | final ReplicaInfo rInfo = event.replicaInfo(); |
| 734 | |
| 735 | switch (event.type()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 736 | case MASTER_CHANGED: |
| 737 | if (local.equals(rInfo.master().orNull())) { |
| 738 | // This node is the new master, populate local structure |
| 739 | // from backup |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 740 | loadFromBackup(did); |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 741 | } |
| 742 | //else { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 743 | // This node is no longer the master holder, |
| 744 | // clean local structure |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 745 | //removeFromPrimary(did); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 746 | // TODO: probably should stop pending backup activities in |
| 747 | // executors to avoid overwriting with old value |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 748 | //} |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 749 | break; |
| 750 | default: |
| 751 | break; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 752 | |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 757 | // Task to update FlowEntries in backup HZ store |
| 758 | private final class UpdateBackup implements Runnable { |
| 759 | |
| 760 | private final DeviceId deviceId; |
| 761 | private final Set<FlowRuleBatchEntry> ops; |
| 762 | |
| 763 | |
| 764 | public UpdateBackup(DeviceId deviceId, |
| 765 | Set<FlowRuleBatchEntry> ops) { |
| 766 | this.deviceId = checkNotNull(deviceId); |
| 767 | this.ops = checkNotNull(ops); |
| 768 | |
| 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public void run() { |
| 773 | try { |
| 774 | log.trace("update backup {} {}", deviceId, ops |
| 775 | ); |
| 776 | final SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(deviceId); |
| 777 | |
| 778 | |
| 779 | ops.stream().forEach( |
| 780 | op -> { |
| 781 | final FlowRule entry = op.target(); |
| 782 | final FlowId id = entry.id(); |
| 783 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 784 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 785 | if (original != null) { |
| 786 | list.addAll(original); |
| 787 | } |
| 788 | list.remove(op.target()); |
| 789 | if (op.operator() == FlowRuleOperation.ADD) { |
| 790 | list.add((StoredFlowEntry) entry); |
| 791 | } |
| 792 | |
| 793 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 794 | boolean success; |
| 795 | if (original == null) { |
| 796 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 797 | } else { |
| 798 | success = backupFlowTable.replace(id, original, newValue); |
| 799 | } |
| 800 | if (!success) { |
| 801 | log.error("Updating backup failed."); |
| 802 | } |
| 803 | |
| 804 | } |
| 805 | ); |
| 806 | } catch (ExecutionException e) { |
| 807 | log.error("Failed to write to backups", e); |
| 808 | } |
| 809 | |
| 810 | } |
| 811 | } |
| 812 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 813 | private class InternalFlowTable { |
| 814 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 815 | /* |
| 816 | TODO: This needs to be cleaned up. Perhaps using the eventually consistent |
| 817 | map when it supports distributed to a sequence of instances. |
| 818 | */ |
| 819 | |
| 820 | |
| 821 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, Set<StoredFlowEntry>>> |
| 822 | flowEntries = new ConcurrentHashMap<>(); |
| 823 | |
| 824 | |
| 825 | private NewConcurrentHashMap<FlowId, Set<StoredFlowEntry>> lazyEmptyFlowTable() { |
| 826 | return NewConcurrentHashMap.<FlowId, Set<StoredFlowEntry>>ifNeeded(); |
| 827 | } |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 828 | |
| 829 | /** |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 830 | * Returns the flow table for specified device. |
| 831 | * |
| 832 | * @param deviceId identifier of the device |
| 833 | * @return Map representing Flow Table of given device. |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 834 | */ |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 835 | private ConcurrentMap<FlowId, Set<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
| 836 | return createIfAbsentUnchecked(flowEntries, |
| 837 | deviceId, lazyEmptyFlowTable()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | private Set<StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) { |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 841 | final ConcurrentMap<FlowId, Set<StoredFlowEntry>> flowTable = getFlowTable(deviceId); |
| 842 | Set<StoredFlowEntry> r = flowTable.get(flowId); |
| 843 | if (r == null) { |
| 844 | final Set<StoredFlowEntry> concurrentlyAdded; |
| 845 | r = new CopyOnWriteArraySet<>(); |
| 846 | concurrentlyAdded = flowTable.putIfAbsent(flowId, r); |
| 847 | if (concurrentlyAdded != null) { |
| 848 | return concurrentlyAdded; |
| 849 | } |
| 850 | } |
| 851 | return r; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | private StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 855 | for (StoredFlowEntry f : getFlowEntriesInternal(rule.deviceId(), rule.id())) { |
| 856 | if (f.equals(rule)) { |
| 857 | return f; |
| 858 | } |
| 859 | } |
| 860 | return null; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 863 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
| 864 | return getFlowTable(deviceId).values().stream() |
| 865 | .flatMap((list -> list.stream())).collect(Collectors.toSet()); |
| 866 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 869 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 870 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
| 871 | return getFlowEntryInternal(rule); |
| 872 | } |
| 873 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 874 | public Set<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 875 | return getFlowEntriesInternal(deviceId); |
| 876 | } |
| 877 | |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 878 | public Set<StoredFlowEntry> getFlowEntriesById(FlowEntry entry) { |
| 879 | return getFlowEntriesInternal(entry.deviceId(), entry.id()); |
| 880 | } |
| 881 | |
| 882 | public void add(FlowEntry rule) { |
| 883 | ((CopyOnWriteArraySet) |
| 884 | getFlowEntriesInternal(rule.deviceId(), rule.id())).add(rule); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | public boolean remove(DeviceId deviceId, FlowEntry rule) { |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 888 | return ((CopyOnWriteArraySet) |
| 889 | getFlowEntriesInternal(deviceId, rule.id())).remove(rule); |
| 890 | //return flowEntries.remove(deviceId, rule); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | public void clearDevice(DeviceId did) { |
| 894 | flowEntries.remove(did); |
| 895 | } |
| 896 | } |
Madan Jampani | b0a3dd6 | 2015-03-17 13:41:27 -0700 | [diff] [blame^] | 897 | |
| 898 | |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 899 | } |