blob: 8d925bd917d4f615898f6ea46652a3ab7264da65 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -07002
Thomas Vachuska8ac922d2014-10-23 16:17:03 -07003import com.google.common.base.Function;
4import com.google.common.collect.FluentIterable;
5import com.google.common.util.concurrent.Futures;
tom85c612b2014-09-19 19:25:47 -07006import org.apache.felix.scr.annotations.Activate;
7import org.apache.felix.scr.annotations.Component;
8import org.apache.felix.scr.annotations.Deactivate;
9import org.apache.felix.scr.annotations.Service;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070010import org.onlab.onos.net.DeviceId;
Madan Jampani117aaae2014-10-23 10:04:05 -070011import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070012import org.onlab.onos.net.flow.DefaultFlowEntry;
13import org.onlab.onos.net.flow.FlowEntry;
14import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070015import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070016import org.onlab.onos.net.flow.FlowRule;
Madan Jampani117aaae2014-10-23 10:04:05 -070017import org.onlab.onos.net.flow.FlowRuleBatchEntry;
18import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
19import org.onlab.onos.net.flow.FlowRuleBatchEvent;
20import org.onlab.onos.net.flow.FlowRuleBatchOperation;
21import org.onlab.onos.net.flow.FlowRuleBatchRequest;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070022import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070023import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070024import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070025import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070026import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070027import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070028import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070029import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070030
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070031import java.util.Arrays;
32import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070033import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070034import java.util.concurrent.ConcurrentHashMap;
35import java.util.concurrent.ConcurrentMap;
36import java.util.concurrent.CopyOnWriteArrayList;
37import java.util.concurrent.Future;
38
39import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
40import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
41import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070042
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070043/**
44 * Manages inventory of flow rules using trivial in-memory implementation.
45 */
tom85c612b2014-09-19 19:25:47 -070046@Component(immediate = true)
47@Service
tomc78acee2014-09-24 15:16:55 -070048public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070049 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070050 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070051
tom85c612b2014-09-19 19:25:47 -070052 private final Logger log = getLogger(getClass());
53
alshabiba68eb962014-09-24 20:34:13 -070054
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070055 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070056 // inner Map value (FlowId synonym list) must be synchronized before modifying
57 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070058 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070059
tom85c612b2014-09-19 19:25:47 -070060 @Activate
61 public void activate() {
62 log.info("Started");
63 }
64
65 @Deactivate
66 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070067 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -070068 log.info("Stopped");
69 }
70
alshabiba68eb962014-09-24 20:34:13 -070071
tombe988312014-09-19 18:38:47 -070072 @Override
tom9b4030d2014-10-06 10:39:03 -070073 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070074 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070075 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
76 for (List<StoredFlowEntry> fes : ft.values()) {
77 sum += fes.size();
78 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070079 }
80 return sum;
81 }
82
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070083 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
84 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070085 }
86
87 /**
88 * Returns the flow table for specified device.
89 *
90 * @param deviceId identifier of the device
91 * @return Map representing Flow Table of given device.
92 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070093 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070094 return createIfAbsentUnchecked(flowEntries,
95 deviceId, lazyEmptyFlowTable());
96 }
97
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070098 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
99 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
100 List<StoredFlowEntry> r = flowTable.get(flowId);
101 if (r == null) {
102 final List<StoredFlowEntry> concurrentlyAdded;
103 r = new CopyOnWriteArrayList<>();
104 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
105 if (concurrentlyAdded != null) {
106 return concurrentlyAdded;
107 }
108 }
109 return r;
110 }
111
112 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
113 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
114 for (StoredFlowEntry fe : fes) {
115 if (fe.equals(rule)) {
116 return fe;
117 }
118 }
119 return null;
tom9b4030d2014-10-06 10:39:03 -0700120 }
121
122 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700123 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700124 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700125 }
126
127 @Override
128 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700129 // flatten and make iterator unmodifiable
130 return FluentIterable.from(getFlowTable(deviceId).values())
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700131 .transformAndConcat(
132 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700133
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700134 @Override
135 public Iterable<? extends FlowEntry> apply(
136 List<StoredFlowEntry> input) {
137 return Collections.unmodifiableList(input);
138 }
139 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700140 }
141
142 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700143 public void storeFlowRule(FlowRule rule) {
144 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700145 }
146
Madan Jampani117aaae2014-10-23 10:04:05 -0700147 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700148 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700149 final DeviceId did = f.deviceId();
150 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700151 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
152 synchronized (existing) {
153 for (StoredFlowEntry fe : existing) {
154 if (fe.equals(rule)) {
155 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700156 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700157 }
158 }
159 // new flow rule added
160 existing.add(f);
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700161 notifyDelegate(FlowRuleBatchEvent.requested(
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700162 new FlowRuleBatchRequest(1, /* FIXME generate something */
163 Arrays.<FlowEntry>asList(f),
164 Collections.<FlowEntry>emptyList())));
alshabiba68eb962014-09-24 20:34:13 -0700165 }
166 }
167
168 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700169 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700170
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700171 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700172
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700173 synchronized (entries) {
174 for (StoredFlowEntry entry : entries) {
175 if (entry.equals(rule)) {
176 synchronized (entry) {
177 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700178 // TODO: Should we notify only if it's "remote" event?
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700179 notifyDelegate(FlowRuleBatchEvent.requested(
180 new FlowRuleBatchRequest(1, /* FIXME generate something */
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700181 Collections.<FlowEntry>emptyList(),
182 Arrays.<FlowEntry>asList(entry))));
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700183 }
184 }
185 }
alshabib219ebaa2014-09-22 15:41:24 -0700186 }
Madan Jampani31961c12014-10-23 12:06:58 -0700187
188
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700189 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700190 }
191
tombe988312014-09-19 18:38:47 -0700192 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700193 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700194 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700195 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
196 synchronized (entries) {
197 for (StoredFlowEntry stored : entries) {
198 if (stored.equals(rule)) {
199 synchronized (stored) {
200 stored.setBytes(rule.bytes());
201 stored.setLife(rule.life());
202 stored.setPackets(rule.packets());
203 if (stored.state() == FlowEntryState.PENDING_ADD) {
204 stored.setState(FlowEntryState.ADDED);
205 // TODO: Do we need to change `rule` state?
206 return new FlowRuleEvent(Type.RULE_ADDED, rule);
207 }
208 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
209 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700210 }
alshabibba5ac482014-10-02 17:15:20 -0700211 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700212 }
alshabib219ebaa2014-09-22 15:41:24 -0700213
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700214 // should not reach here
215 // storeFlowRule was expected to be called
216 log.error("FlowRule was not found in store {} to update", rule);
217
alshabib8ca53902014-10-07 13:11:17 -0700218 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700219 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700220 }
221
tombe988312014-09-19 18:38:47 -0700222 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700223 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700224 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700225 final DeviceId did = rule.deviceId();
226
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700227 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
228 synchronized (entries) {
229 if (entries.remove(rule)) {
230 return new FlowRuleEvent(RULE_REMOVED, rule);
231 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700232 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700233 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700234 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700235
236 @Override
237 public Future<CompletedBatchOperation> storeBatch(
238 FlowRuleBatchOperation batchOperation) {
239 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
240 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
241 storeFlowRule(entry.getTarget());
242 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
243 deleteFlowRule(entry.getTarget());
244 } else {
245 throw new UnsupportedOperationException("Unsupported operation type");
246 }
247 }
248 return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet()));
249 }
250
251 @Override
252 public void batchOperationComplete(FlowRuleBatchEvent event) {
253 notifyDelegate(event);
254 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700255}