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