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