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