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