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