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; |
| 14 | import java.util.concurrent.ExecutorService; |
| 15 | import java.util.concurrent.Executors; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 16 | import java.util.concurrent.Future; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 17 | import java.util.concurrent.TimeUnit; |
| 18 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 19 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 20 | import java.util.List; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onlab.onos.ApplicationId; |
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 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 162 | log.info("Started"); |
| 163 | } |
| 164 | |
| 165 | @Deactivate |
| 166 | public void deactivate() { |
| 167 | log.info("Stopped"); |
| 168 | } |
| 169 | |
| 170 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 171 | // TODO: This is not a efficient operation on a distributed sharded |
| 172 | // flow store. We need to revisit the need for this operation or at least |
| 173 | // make it device specific. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 174 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 175 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 176 | // implementing in-efficient operation for debugging purpose. |
| 177 | int sum = 0; |
| 178 | for (Device device : deviceService.getDevices()) { |
| 179 | final DeviceId did = device.id(); |
| 180 | sum += Iterables.size(getFlowEntries(did)); |
| 181 | } |
| 182 | return sum; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 186 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 187 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
| 188 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 189 | return getFlowEntryInternal(rule); |
| 190 | } |
| 191 | |
| 192 | log.info("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 193 | replicaInfo.master().orNull(), rule.deviceId()); |
| 194 | |
| 195 | ClusterMessage message = new ClusterMessage( |
| 196 | clusterService.getLocalNode().id(), |
| 197 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 198 | SERIALIZER.encode(rule)); |
| 199 | |
| 200 | try { |
| 201 | ClusterMessageResponse response = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 202 | return SERIALIZER.decode(response.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 203 | } catch (IOException | TimeoutException e) { |
| 204 | // FIXME: throw a FlowStoreException |
| 205 | throw new RuntimeException(e); |
| 206 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private synchronized StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 210 | for (StoredFlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 211 | if (f.equals(rule)) { |
| 212 | return f; |
| 213 | } |
| 214 | } |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 219 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 220 | Collection<? extends FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 221 | if (rules == null) { |
| 222 | return Collections.emptyList(); |
| 223 | } |
| 224 | return ImmutableSet.copyOf(rules); |
| 225 | } |
| 226 | |
| 227 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 228 | public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) { |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 229 | Collection<FlowRule> rules = flowEntriesById.get(appId.id()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 230 | if (rules == null) { |
| 231 | return Collections.emptyList(); |
| 232 | } |
| 233 | return ImmutableSet.copyOf(rules); |
| 234 | } |
| 235 | |
| 236 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 237 | public void storeFlowRule(FlowRule rule) { |
| 238 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)))); |
| 239 | } |
| 240 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 241 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 242 | public Future<CompletedBatchOperation> storeBatch(FlowRuleBatchOperation operation) { |
| 243 | if (operation.getOperations().isEmpty()) { |
| 244 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 245 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 246 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 247 | DeviceId deviceId = operation.getOperations().get(0).getTarget().deviceId(); |
| 248 | |
| 249 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 250 | |
| 251 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 252 | return storeBatchInternal(operation); |
| 253 | } |
| 254 | |
| 255 | log.info("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 256 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 257 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 258 | ClusterMessage message = new ClusterMessage( |
| 259 | clusterService.getLocalNode().id(), |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 260 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 261 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 262 | |
| 263 | try { |
| 264 | ClusterMessageResponse response = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 265 | response.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 266 | } catch (IOException | TimeoutException e) { |
| 267 | // FIXME: throw a FlowStoreException |
| 268 | throw new RuntimeException(e); |
| 269 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 270 | |
| 271 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 274 | private ListenableFuture<CompletedBatchOperation> storeBatchInternal(FlowRuleBatchOperation operation) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 275 | List<FlowEntry> toRemove = new ArrayList<>(); |
| 276 | List<FlowEntry> toAdd = new ArrayList<>(); |
| 277 | // TODO: backup changes to hazelcast map |
| 278 | for (FlowRuleBatchEntry batchEntry : operation.getOperations()) { |
| 279 | FlowRule flowRule = batchEntry.getTarget(); |
| 280 | FlowRuleOperation op = batchEntry.getOperator(); |
| 281 | if (op.equals(FlowRuleOperation.REMOVE)) { |
| 282 | StoredFlowEntry entry = getFlowEntryInternal(flowRule); |
| 283 | if (entry != null) { |
| 284 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 285 | toRemove.add(entry); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 286 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 287 | } else if (op.equals(FlowRuleOperation.ADD)) { |
| 288 | StoredFlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 289 | DeviceId deviceId = flowRule.deviceId(); |
| 290 | if (!flowEntries.containsEntry(deviceId, flowEntry)) { |
| 291 | flowEntries.put(deviceId, flowEntry); |
| 292 | flowEntriesById.put(flowRule.appId(), flowEntry); |
| 293 | toAdd.add(flowEntry); |
| 294 | } |
| 295 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 296 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 297 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
| 298 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
| 299 | } |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 300 | |
| 301 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 302 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 303 | |
| 304 | pendingFutures.put(batchId, r); |
| 305 | notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove))); |
| 306 | return r; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 310 | public void deleteFlowRule(FlowRule rule) { |
| 311 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)))); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 315 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 316 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 317 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 318 | return addOrUpdateFlowRuleInternal(rule); |
| 319 | } |
| 320 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 321 | log.error("Tried to update FlowRule {} state," |
| 322 | + " while the Node was not the master.", rule); |
| 323 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | private synchronized FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 327 | DeviceId did = rule.deviceId(); |
| 328 | |
| 329 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 330 | StoredFlowEntry stored = getFlowEntryInternal(rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 331 | if (stored != null) { |
| 332 | stored.setBytes(rule.bytes()); |
| 333 | stored.setLife(rule.life()); |
| 334 | stored.setPackets(rule.packets()); |
| 335 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 336 | stored.setState(FlowEntryState.ADDED); |
| 337 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 338 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 339 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 340 | } |
| 341 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 342 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 343 | flowEntries.put(did, new DefaultFlowEntry(rule)); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 344 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 345 | |
| 346 | // TODO: also update backup. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 350 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 351 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 352 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 353 | // bypass and handle it locally |
| 354 | return removeFlowRuleInternal(rule); |
| 355 | } |
| 356 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 357 | log.error("Tried to remove FlowRule {}," |
| 358 | + " while the Node was not the master.", rule); |
| 359 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | private synchronized FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 363 | // 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] | 364 | if (flowEntries.remove(rule.deviceId(), rule)) { |
| 365 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 366 | } else { |
| 367 | return null; |
| 368 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 369 | // TODO: also update backup. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 370 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 371 | |
| 372 | @Override |
| 373 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame^] | 374 | SettableFuture<CompletedBatchOperation> future |
| 375 | = pendingFutures.get(event.subject().batchId()); |
| 376 | if (future != null) { |
| 377 | future.set(event.result()); |
| 378 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 379 | notifyDelegate(event); |
| 380 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 381 | } |