alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.flow.impl; |
| 2 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 5 | import static org.onlab.onos.store.flow.impl.FlowStoreMessageSubjects.*; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 6 | import static org.onlab.util.Tools.namedThreads; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 7 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 8 | import java.io.IOException; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 9 | import java.util.ArrayList; |
| 10 | import java.util.Arrays; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 11 | import java.util.Collection; |
| 12 | import java.util.Collections; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 13 | import java.util.Map; |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 14 | import java.util.Set; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 15 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 16 | import java.util.concurrent.ExecutorService; |
| 17 | import java.util.concurrent.Executors; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 18 | import java.util.concurrent.Future; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 19 | import java.util.concurrent.TimeUnit; |
| 20 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 21 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 22 | import java.util.List; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 30 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.Device; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.DeviceId; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.device.DeviceService; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 36 | import org.onlab.onos.net.flow.FlowEntry; |
| 37 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.FlowRule; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 40 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
| 41 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 42 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.flow.FlowRuleEvent; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
| 46 | import org.onlab.onos.net.flow.FlowRuleStore; |
| 47 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.flow.StoredFlowEntry; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 49 | import org.onlab.onos.store.AbstractStore; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 50 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 51 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 52 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 53 | import org.onlab.onos.store.flow.ReplicaInfo; |
Yuta HIGUCHI | fe280eb | 2014-10-17 12:10:43 -0700 | [diff] [blame] | 54 | import org.onlab.onos.store.flow.ReplicaInfoService; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 55 | import org.onlab.onos.store.serializers.DistributedStoreSerializers; |
| 56 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 57 | import org.onlab.util.KryoNamespace; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 60 | import com.google.common.base.Function; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 61 | import com.google.common.collect.ArrayListMultimap; |
| 62 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 63 | import com.google.common.collect.Iterables; |
| 64 | import com.google.common.collect.Maps; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 65 | import com.google.common.collect.Multimap; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 66 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 67 | import com.google.common.util.concurrent.ListenableFuture; |
| 68 | import com.google.common.util.concurrent.SettableFuture; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 71 | * Manages inventory of flow rules using a distributed state management protocol. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 72 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 73 | @Component(immediate = true) |
| 74 | @Service |
| 75 | public class DistributedFlowRuleStore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 76 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 77 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 78 | |
| 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
| 81 | // store entries as a pile of rules, no info about device tables |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 82 | private final Multimap<DeviceId, StoredFlowEntry> flowEntries = |
| 83 | ArrayListMultimap.<DeviceId, StoredFlowEntry>create(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 84 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 85 | private final Multimap<Short, FlowRule> flowEntriesById = |
| 86 | ArrayListMultimap.<Short, FlowRule>create(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 87 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 89 | protected ReplicaInfoService replicaInfoManager; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 92 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 95 | protected ClusterService clusterService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected DeviceService deviceService; |
| 99 | |
| 100 | private final AtomicInteger localBatchIdGen = new AtomicInteger(); |
| 101 | |
| 102 | |
| 103 | // FIXME switch to expiraing map/Cache? |
| 104 | private Map<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures = Maps.newConcurrentMap(); |
| 105 | |
| 106 | private final ExecutorService futureListeners = |
| 107 | Executors.newCachedThreadPool(namedThreads("flowstore-peer-responders")); |
| 108 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 109 | |
| 110 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 111 | @Override |
| 112 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 113 | serializerPool = KryoNamespace.newBuilder() |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 114 | .register(DistributedStoreSerializers.COMMON) |
| 115 | .build() |
| 116 | .populate(1); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | // TODO: make this configurable |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 121 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 122 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 123 | @Activate |
| 124 | public void activate() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 125 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new ClusterMessageHandler() { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 126 | |
| 127 | @Override |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 128 | public void handle(final ClusterMessage message) { |
| 129 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
| 130 | log.info("received batch request {}", operation); |
| 131 | final ListenableFuture<CompletedBatchOperation> f = storeBatchInternal(operation); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 132 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 133 | f.addListener(new Runnable() { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public void run() { |
| 137 | CompletedBatchOperation result = Futures.getUnchecked(f); |
| 138 | try { |
| 139 | message.respond(SERIALIZER.encode(result)); |
| 140 | } catch (IOException e) { |
| 141 | log.error("Failed to respond back", e); |
| 142 | } |
| 143 | } |
| 144 | }, futureListeners); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 145 | } |
| 146 | }); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 147 | |
| 148 | clusterCommunicator.addSubscriber(GET_FLOW_ENTRY, new ClusterMessageHandler() { |
| 149 | |
| 150 | @Override |
| 151 | public void handle(ClusterMessage message) { |
| 152 | FlowRule rule = SERIALIZER.decode(message.payload()); |
| 153 | log.info("received get flow entry request for {}", rule); |
| 154 | FlowEntry flowEntry = getFlowEntryInternal(rule); |
| 155 | try { |
| 156 | message.respond(SERIALIZER.encode(flowEntry)); |
| 157 | } catch (IOException e) { |
| 158 | log.error("Failed to respond back", e); |
| 159 | } |
| 160 | } |
| 161 | }); |
| 162 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 163 | clusterCommunicator.addSubscriber(GET_DEVICE_FLOW_ENTRIES, new ClusterMessageHandler() { |
| 164 | |
| 165 | @Override |
| 166 | public void handle(ClusterMessage message) { |
| 167 | DeviceId deviceId = SERIALIZER.decode(message.payload()); |
| 168 | log.info("Received get flow entries request for {} from {}", deviceId, message.sender()); |
| 169 | Set<FlowEntry> flowEntries = getFlowEntriesInternal(deviceId); |
| 170 | try { |
| 171 | message.respond(SERIALIZER.encode(flowEntries)); |
| 172 | } catch (IOException e) { |
| 173 | log.error("Failed to respond to peer's getFlowEntries request", e); |
| 174 | } |
| 175 | } |
| 176 | }); |
| 177 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 178 | log.info("Started"); |
| 179 | } |
| 180 | |
| 181 | @Deactivate |
| 182 | public void deactivate() { |
| 183 | log.info("Stopped"); |
| 184 | } |
| 185 | |
| 186 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 187 | // TODO: This is not a efficient operation on a distributed sharded |
| 188 | // flow store. We need to revisit the need for this operation or at least |
| 189 | // make it device specific. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 190 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 191 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 192 | // implementing in-efficient operation for debugging purpose. |
| 193 | int sum = 0; |
| 194 | for (Device device : deviceService.getDevices()) { |
| 195 | final DeviceId did = device.id(); |
| 196 | sum += Iterables.size(getFlowEntries(did)); |
| 197 | } |
| 198 | return sum; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 202 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 203 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
| 204 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 205 | return getFlowEntryInternal(rule); |
| 206 | } |
| 207 | |
| 208 | log.info("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 209 | replicaInfo.master().orNull(), rule.deviceId()); |
| 210 | |
| 211 | ClusterMessage message = new ClusterMessage( |
| 212 | clusterService.getLocalNode().id(), |
| 213 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 214 | SERIALIZER.encode(rule)); |
| 215 | |
| 216 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 217 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 218 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 219 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 220 | // FIXME: throw a FlowStoreException |
| 221 | throw new RuntimeException(e); |
| 222 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | private synchronized StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 226 | for (StoredFlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 227 | if (f.equals(rule)) { |
| 228 | return f; |
| 229 | } |
| 230 | } |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 235 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 236 | |
| 237 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 238 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 239 | return getFlowEntriesInternal(deviceId); |
| 240 | } |
| 241 | |
| 242 | log.info("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
| 243 | replicaInfo.master().orNull(), deviceId); |
| 244 | |
| 245 | ClusterMessage message = new ClusterMessage( |
| 246 | clusterService.getLocalNode().id(), |
| 247 | GET_DEVICE_FLOW_ENTRIES, |
| 248 | SERIALIZER.encode(deviceId)); |
| 249 | |
| 250 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 251 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 252 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 253 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 254 | // FIXME: throw a FlowStoreException |
| 255 | throw new RuntimeException(e); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 260 | Collection<? extends FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 261 | if (rules == null) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 262 | return Collections.emptySet(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 263 | } |
| 264 | return ImmutableSet.copyOf(rules); |
| 265 | } |
| 266 | |
| 267 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 268 | public void storeFlowRule(FlowRule rule) { |
| 269 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)))); |
| 270 | } |
| 271 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 272 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 273 | public Future<CompletedBatchOperation> storeBatch(FlowRuleBatchOperation operation) { |
| 274 | if (operation.getOperations().isEmpty()) { |
| 275 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 276 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 277 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 278 | DeviceId deviceId = operation.getOperations().get(0).getTarget().deviceId(); |
| 279 | |
| 280 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 281 | |
| 282 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 283 | return storeBatchInternal(operation); |
| 284 | } |
| 285 | |
| 286 | log.info("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 287 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 288 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 289 | ClusterMessage message = new ClusterMessage( |
| 290 | clusterService.getLocalNode().id(), |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 291 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 292 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 293 | |
| 294 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 295 | ListenableFuture<byte[]> responseFuture = |
| 296 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 297 | return Futures.transform(responseFuture, new Function<byte[], CompletedBatchOperation>() { |
| 298 | @Override |
| 299 | public CompletedBatchOperation apply(byte[] input) { |
| 300 | return SERIALIZER.decode(input); |
| 301 | } |
| 302 | }); |
| 303 | } catch (IOException e) { |
| 304 | return Futures.immediateFailedFuture(e); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 308 | private ListenableFuture<CompletedBatchOperation> storeBatchInternal(FlowRuleBatchOperation operation) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 309 | List<FlowEntry> toRemove = new ArrayList<>(); |
| 310 | List<FlowEntry> toAdd = new ArrayList<>(); |
| 311 | // TODO: backup changes to hazelcast map |
| 312 | for (FlowRuleBatchEntry batchEntry : operation.getOperations()) { |
| 313 | FlowRule flowRule = batchEntry.getTarget(); |
| 314 | FlowRuleOperation op = batchEntry.getOperator(); |
| 315 | if (op.equals(FlowRuleOperation.REMOVE)) { |
| 316 | StoredFlowEntry entry = getFlowEntryInternal(flowRule); |
| 317 | if (entry != null) { |
| 318 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 319 | toRemove.add(entry); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 320 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 321 | } else if (op.equals(FlowRuleOperation.ADD)) { |
| 322 | StoredFlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 323 | DeviceId deviceId = flowRule.deviceId(); |
| 324 | if (!flowEntries.containsEntry(deviceId, flowEntry)) { |
| 325 | flowEntries.put(deviceId, flowEntry); |
| 326 | flowEntriesById.put(flowRule.appId(), flowEntry); |
| 327 | toAdd.add(flowEntry); |
| 328 | } |
| 329 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 330 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 331 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
| 332 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
| 333 | } |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 334 | |
| 335 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 336 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 337 | |
| 338 | pendingFutures.put(batchId, r); |
| 339 | notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove))); |
| 340 | return r; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 344 | public void deleteFlowRule(FlowRule rule) { |
| 345 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)))); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 349 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 350 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 351 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 352 | return addOrUpdateFlowRuleInternal(rule); |
| 353 | } |
| 354 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 355 | log.error("Tried to update FlowRule {} state," |
| 356 | + " while the Node was not the master.", rule); |
| 357 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | private synchronized FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 361 | DeviceId did = rule.deviceId(); |
| 362 | |
| 363 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 364 | StoredFlowEntry stored = getFlowEntryInternal(rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 365 | if (stored != null) { |
| 366 | stored.setBytes(rule.bytes()); |
| 367 | stored.setLife(rule.life()); |
| 368 | stored.setPackets(rule.packets()); |
| 369 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 370 | stored.setState(FlowEntryState.ADDED); |
| 371 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 372 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 373 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 374 | } |
| 375 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 376 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 377 | flowEntries.put(did, new DefaultFlowEntry(rule)); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 378 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 379 | |
| 380 | // TODO: also update backup. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 384 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 385 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 386 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 387 | // bypass and handle it locally |
| 388 | return removeFlowRuleInternal(rule); |
| 389 | } |
| 390 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 391 | log.error("Tried to remove FlowRule {}," |
| 392 | + " while the Node was not the master.", rule); |
| 393 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private synchronized FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 397 | // This is where one could mark a rule as removed and still keep it in the store. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 398 | if (flowEntries.remove(rule.deviceId(), rule)) { |
| 399 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 400 | } else { |
| 401 | return null; |
| 402 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 403 | // TODO: also update backup. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 404 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 405 | |
| 406 | @Override |
| 407 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 408 | SettableFuture<CompletedBatchOperation> future |
| 409 | = pendingFutures.get(event.subject().batchId()); |
| 410 | if (future != null) { |
| 411 | future.set(event.result()); |
| 412 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 413 | notifyDelegate(event); |
| 414 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 415 | } |