blob: 5062416d48c051576fbb5aab66a8fdd6394533be [file] [log] [blame]
alshabib339a3d92014-09-26 17:54:32 -07001package org.onlab.onos.store.flow.impl;
2
alshabib339a3d92014-09-26 17:54:32 -07003import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
4import static org.slf4j.LoggerFactory.getLogger;
5
Madan Jampani38b250d2014-10-17 11:02:38 -07006import java.io.IOException;
alshabib339a3d92014-09-26 17:54:32 -07007import java.util.Collection;
8import java.util.Collections;
Madan Jampani38b250d2014-10-17 11:02:38 -07009import java.util.concurrent.TimeUnit;
10import java.util.concurrent.TimeoutException;
alshabib339a3d92014-09-26 17:54:32 -070011
12import org.apache.felix.scr.annotations.Activate;
13import org.apache.felix.scr.annotations.Component;
14import org.apache.felix.scr.annotations.Deactivate;
Madan Jampani38b250d2014-10-17 11:02:38 -070015import org.apache.felix.scr.annotations.Reference;
16import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib339a3d92014-09-26 17:54:32 -070017import org.apache.felix.scr.annotations.Service;
18import org.onlab.onos.ApplicationId;
Madan Jampani38b250d2014-10-17 11:02:38 -070019import org.onlab.onos.cluster.ClusterService;
alshabib339a3d92014-09-26 17:54:32 -070020import org.onlab.onos.net.DeviceId;
alshabib1c319ff2014-10-04 20:29:09 -070021import org.onlab.onos.net.flow.DefaultFlowEntry;
22import org.onlab.onos.net.flow.FlowEntry;
23import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib339a3d92014-09-26 17:54:32 -070024import org.onlab.onos.net.flow.FlowRule;
alshabib339a3d92014-09-26 17:54:32 -070025import org.onlab.onos.net.flow.FlowRuleEvent;
26import org.onlab.onos.net.flow.FlowRuleEvent.Type;
27import org.onlab.onos.net.flow.FlowRuleStore;
28import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
29import org.onlab.onos.store.AbstractStore;
Madan Jampani38b250d2014-10-17 11:02:38 -070030import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
31import org.onlab.onos.store.cluster.messaging.ClusterMessage;
32import org.onlab.onos.store.cluster.messaging.ClusterMessageResponse;
33import org.onlab.onos.store.flow.ReplicaInfo;
34import org.onlab.onos.store.serializers.DistributedStoreSerializers;
35import org.onlab.onos.store.serializers.KryoSerializer;
36import org.onlab.util.KryoPool;
alshabib339a3d92014-09-26 17:54:32 -070037import org.slf4j.Logger;
38
39import com.google.common.collect.ArrayListMultimap;
40import com.google.common.collect.ImmutableSet;
41import com.google.common.collect.Multimap;
42
43/**
Madan Jampani38b250d2014-10-17 11:02:38 -070044 * Manages inventory of flow rules using a distributed state management protocol.
alshabib339a3d92014-09-26 17:54:32 -070045 */
alshabib339a3d92014-09-26 17:54:32 -070046@Component(immediate = true)
47@Service
48public class DistributedFlowRuleStore
alshabib1c319ff2014-10-04 20:29:09 -070049 extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
50 implements FlowRuleStore {
alshabib339a3d92014-09-26 17:54:32 -070051
52 private final Logger log = getLogger(getClass());
53
54 // store entries as a pile of rules, no info about device tables
alshabib1c319ff2014-10-04 20:29:09 -070055 private final Multimap<DeviceId, FlowEntry> flowEntries =
56 ArrayListMultimap.<DeviceId, FlowEntry>create();
alshabib339a3d92014-09-26 17:54:32 -070057
alshabib92c65ad2014-10-08 21:56:05 -070058 private final Multimap<Short, FlowRule> flowEntriesById =
59 ArrayListMultimap.<Short, FlowRule>create();
alshabib339a3d92014-09-26 17:54:32 -070060
Madan Jampani38b250d2014-10-17 11:02:38 -070061 @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
alshabib339a3d92014-09-26 17:54:32 -070083 @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
tom9b4030d2014-10-06 10:39:03 -070095 public int getFlowRuleCount() {
96 return flowEntries.size();
97 }
98
99 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700100 public synchronized FlowEntry getFlowEntry(FlowRule rule) {
101 for (FlowEntry f : flowEntries.get(rule.deviceId())) {
alshabib339a3d92014-09-26 17:54:32 -0700102 if (f.equals(rule)) {
103 return f;
104 }
105 }
106 return null;
107 }
108
109 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700110 public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
111 Collection<FlowEntry> rules = flowEntries.get(deviceId);
alshabib339a3d92014-09-26 17:54:32 -0700112 if (rules == null) {
113 return Collections.emptyList();
114 }
115 return ImmutableSet.copyOf(rules);
116 }
117
118 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700119 public synchronized Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
alshabib92c65ad2014-10-08 21:56:05 -0700120 Collection<FlowRule> rules = flowEntriesById.get(appId.id());
alshabib339a3d92014-09-26 17:54:32 -0700121 if (rules == null) {
122 return Collections.emptyList();
123 }
124 return ImmutableSet.copyOf(rules);
125 }
126
127 @Override
Madan Jampani38b250d2014-10-17 11:02:38 -0700128 public void storeFlowRule(FlowRule rule) {
129 ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId());
130 if (replicaInfo.master().get().equals(clusterService.getLocalNode())) {
131 storeFlowEntryInternal(rule);
132 return;
alshabib339a3d92014-09-26 17:54:32 -0700133 }
Madan Jampani38b250d2014-10-17 11:02:38 -0700134
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
149 public synchronized void storeFlowEntryInternal(FlowRule flowRule) {
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.
alshabib339a3d92014-09-26 17:54:32 -0700159 }
160
161 @Override
162 public synchronized void deleteFlowRule(FlowRule rule) {
Madan Jampani38b250d2014-10-17 11:02:38 -0700163 ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId());
164 if (replicaInfo.master().get().equals(clusterService.getLocalNode())) {
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
183 public synchronized void deleteFlowRuleInternal(FlowRule flowRule) {
184 FlowEntry entry = getFlowEntry(flowRule);
alshabib1c319ff2014-10-04 20:29:09 -0700185 if (entry == null) {
186 return;
alshabib339a3d92014-09-26 17:54:32 -0700187 }
alshabib1c319ff2014-10-04 20:29:09 -0700188 entry.setState(FlowEntryState.PENDING_REMOVE);
Madan Jampani38b250d2014-10-17 11:02:38 -0700189 // TODO: also update backup.
alshabib339a3d92014-09-26 17:54:32 -0700190 }
191
192 @Override
Madan Jampani38b250d2014-10-17 11:02:38 -0700193 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
194 ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId());
195 if (replicaInfo.master().get().equals(clusterService.getLocalNode())) {
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) {
alshabib339a3d92014-09-26 17:54:32 -0700214 DeviceId did = rule.deviceId();
215
216 // check if this new rule is an update to an existing entry
alshabib1c319ff2014-10-04 20:29:09 -0700217 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 }
alshabib339a3d92014-09-26 17:54:32 -0700226 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
227 }
228
229 flowEntries.put(did, rule);
alshabib1c319ff2014-10-04 20:29:09 -0700230 return null;
Madan Jampani38b250d2014-10-17 11:02:38 -0700231
232 // TODO: also update backup.
alshabib339a3d92014-09-26 17:54:32 -0700233 }
234
235 @Override
Madan Jampani38b250d2014-10-17 11:02:38 -0700236 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
237 ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId());
238 if (replicaInfo.master().get().equals(clusterService.getLocalNode())) {
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) {
alshabib1c319ff2014-10-04 20:29:09 -0700258 // This is where one could mark a rule as removed and still keep it in the store.
alshabib339a3d92014-09-26 17:54:32 -0700259 if (flowEntries.remove(rule.deviceId(), rule)) {
260 return new FlowRuleEvent(RULE_REMOVED, rule);
261 } else {
262 return null;
263 }
Madan Jampani38b250d2014-10-17 11:02:38 -0700264 // TODO: also update backup.
alshabib339a3d92014-09-26 17:54:32 -0700265 }
alshabib339a3d92014-09-26 17:54:32 -0700266}