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