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