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