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