blob: 3d10d3d52eb347596dcb203257d37d2d67b7286f [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07002
alshabib219ebaa2014-09-22 15:41:24 -07003import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
4import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07005import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Madan Jampani117aaae2014-10-23 10:04:05 -07006
7import java.util.Arrays;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -07008import java.util.Collections;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07009import java.util.HashSet;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070010import java.util.List;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070011import java.util.Set;
12import java.util.concurrent.ConcurrentHashMap;
13import java.util.concurrent.ConcurrentMap;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070014import java.util.concurrent.CopyOnWriteArrayList;
Madan Jampani117aaae2014-10-23 10:04:05 -070015import java.util.concurrent.Future;
alshabiba68eb962014-09-24 20:34:13 -070016
tom85c612b2014-09-19 19:25:47 -070017import org.apache.felix.scr.annotations.Activate;
18import org.apache.felix.scr.annotations.Component;
19import org.apache.felix.scr.annotations.Deactivate;
20import org.apache.felix.scr.annotations.Service;
alshabiba68eb962014-09-24 20:34:13 -070021import org.onlab.onos.ApplicationId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070022import org.onlab.onos.net.DeviceId;
Madan Jampani117aaae2014-10-23 10:04:05 -070023import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070024import org.onlab.onos.net.flow.DefaultFlowEntry;
25import org.onlab.onos.net.flow.FlowEntry;
26import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070027import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070028import org.onlab.onos.net.flow.FlowRule;
Madan Jampani117aaae2014-10-23 10:04:05 -070029import org.onlab.onos.net.flow.FlowRuleBatchEntry;
30import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
31import org.onlab.onos.net.flow.FlowRuleBatchEvent;
32import org.onlab.onos.net.flow.FlowRuleBatchOperation;
33import org.onlab.onos.net.flow.FlowRuleBatchRequest;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070034import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070035import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070036import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070037import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070038import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070039import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070040import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070041import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070042
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070043import com.google.common.base.Function;
44import com.google.common.collect.FluentIterable;
Madan Jampani117aaae2014-10-23 10:04:05 -070045import com.google.common.util.concurrent.Futures;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070046
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070047/**
48 * Manages inventory of flow rules using trivial in-memory implementation.
49 */
tom85c612b2014-09-19 19:25:47 -070050@Component(immediate = true)
51@Service
tomc78acee2014-09-24 15:16:55 -070052public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070053 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070054 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070055
tom85c612b2014-09-19 19:25:47 -070056 private final Logger log = getLogger(getClass());
57
alshabiba68eb962014-09-24 20:34:13 -070058
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070059 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070060 // inner Map value (FlowId synonym list) must be synchronized before modifying
61 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070062 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070063
tom85c612b2014-09-19 19:25:47 -070064 @Activate
65 public void activate() {
66 log.info("Started");
67 }
68
69 @Deactivate
70 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070071 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -070072 log.info("Stopped");
73 }
74
alshabiba68eb962014-09-24 20:34:13 -070075
tombe988312014-09-19 18:38:47 -070076 @Override
tom9b4030d2014-10-06 10:39:03 -070077 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070078 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070079 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
80 for (List<StoredFlowEntry> fes : ft.values()) {
81 sum += fes.size();
82 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070083 }
84 return sum;
85 }
86
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070087 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
88 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070089 }
90
91 /**
92 * Returns the flow table for specified device.
93 *
94 * @param deviceId identifier of the device
95 * @return Map representing Flow Table of given device.
96 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070097 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070098 return createIfAbsentUnchecked(flowEntries,
99 deviceId, lazyEmptyFlowTable());
100 }
101
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700102 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
103 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
104 List<StoredFlowEntry> r = flowTable.get(flowId);
105 if (r == null) {
106 final List<StoredFlowEntry> concurrentlyAdded;
107 r = new CopyOnWriteArrayList<>();
108 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
109 if (concurrentlyAdded != null) {
110 return concurrentlyAdded;
111 }
112 }
113 return r;
114 }
115
116 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
117 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
118 for (StoredFlowEntry fe : fes) {
119 if (fe.equals(rule)) {
120 return fe;
121 }
122 }
123 return null;
tom9b4030d2014-10-06 10:39:03 -0700124 }
125
126 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700127 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700128 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700129 }
130
131 @Override
132 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700133 // flatten and make iterator unmodifiable
134 return FluentIterable.from(getFlowTable(deviceId).values())
135 .transformAndConcat(
136 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
137
138 @Override
139 public Iterable<? extends FlowEntry> apply(
140 List<StoredFlowEntry> input) {
141 return Collections.unmodifiableList(input);
142 }
143 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700144 }
145
146 @Override
147 public Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
148
149 Set<FlowRule> rules = new HashSet<>();
150 for (DeviceId did : flowEntries.keySet()) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700151 for (FlowEntry fe : getFlowEntries(did)) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700152 if (fe.appId() == appId.id()) {
153 rules.add(fe);
154 }
alshabiba68eb962014-09-24 20:34:13 -0700155 }
156 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700157 return rules;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700158 }
159
tombe988312014-09-19 18:38:47 -0700160 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700161 public void storeFlowRule(FlowRule rule) {
162 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700163 }
164
Madan Jampani117aaae2014-10-23 10:04:05 -0700165 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700166 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700167 final DeviceId did = f.deviceId();
168 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700169 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
170 synchronized (existing) {
171 for (StoredFlowEntry fe : existing) {
172 if (fe.equals(rule)) {
173 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700174 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700175 }
176 }
177 // new flow rule added
178 existing.add(f);
Madan Jampani117aaae2014-10-23 10:04:05 -0700179 notifyDelegate(FlowRuleBatchEvent.create(
180 new FlowRuleBatchRequest(
181 Arrays.<FlowEntry>asList(f),
182 Collections.<FlowEntry>emptyList())));
alshabiba68eb962014-09-24 20:34:13 -0700183 }
184 }
185
186 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700187 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700188
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700189 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
190 synchronized (entries) {
191 for (StoredFlowEntry entry : entries) {
192 if (entry.equals(rule)) {
193 synchronized (entry) {
194 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700195 // TODO: Should we notify only if it's "remote" event?
196 //notifyDelegate(new FlowRuleEvent(Type.RULE_REMOVE_REQUESTED, rule));
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700197 }
198 }
199 }
alshabib219ebaa2014-09-22 15:41:24 -0700200 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700201 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700202 }
203
tombe988312014-09-19 18:38:47 -0700204 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700205 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700206 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700207 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
208 synchronized (entries) {
209 for (StoredFlowEntry stored : entries) {
210 if (stored.equals(rule)) {
211 synchronized (stored) {
212 stored.setBytes(rule.bytes());
213 stored.setLife(rule.life());
214 stored.setPackets(rule.packets());
215 if (stored.state() == FlowEntryState.PENDING_ADD) {
216 stored.setState(FlowEntryState.ADDED);
217 // TODO: Do we need to change `rule` state?
218 return new FlowRuleEvent(Type.RULE_ADDED, rule);
219 }
220 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
221 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700222 }
alshabibba5ac482014-10-02 17:15:20 -0700223 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700224 }
alshabib219ebaa2014-09-22 15:41:24 -0700225
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700226 // should not reach here
227 // storeFlowRule was expected to be called
228 log.error("FlowRule was not found in store {} to update", rule);
229
alshabib8ca53902014-10-07 13:11:17 -0700230 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700231 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700232 }
233
tombe988312014-09-19 18:38:47 -0700234 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700235 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700236 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700237 final DeviceId did = rule.deviceId();
238
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700239 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
240 synchronized (entries) {
241 if (entries.remove(rule)) {
242 return new FlowRuleEvent(RULE_REMOVED, rule);
243 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700244 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700245 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700246 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700247
248 @Override
249 public Future<CompletedBatchOperation> storeBatch(
250 FlowRuleBatchOperation batchOperation) {
251 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
252 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
253 storeFlowRule(entry.getTarget());
254 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
255 deleteFlowRule(entry.getTarget());
256 } else {
257 throw new UnsupportedOperationException("Unsupported operation type");
258 }
259 }
260 return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet()));
261 }
262
263 @Override
264 public void batchOperationComplete(FlowRuleBatchEvent event) {
265 notifyDelegate(event);
266 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700267}