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