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