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