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