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