blob: 82ea4e2063c1d0e0c0c0766dcf5c44dbb118cc00 [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;
alshabiba68eb962014-09-24 20:34:13 -070010import org.onlab.onos.ApplicationId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070011import org.onlab.onos.net.DeviceId;
Madan Jampani117aaae2014-10-23 10:04:05 -070012import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070013import org.onlab.onos.net.flow.DefaultFlowEntry;
14import org.onlab.onos.net.flow.FlowEntry;
15import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070016import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070017import org.onlab.onos.net.flow.FlowRule;
Madan Jampani117aaae2014-10-23 10:04:05 -070018import org.onlab.onos.net.flow.FlowRuleBatchEntry;
19import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
20import org.onlab.onos.net.flow.FlowRuleBatchEvent;
21import org.onlab.onos.net.flow.FlowRuleBatchOperation;
22import org.onlab.onos.net.flow.FlowRuleBatchRequest;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070023import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070024import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070025import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070026import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070027import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070028import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070029import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070030import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070031
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070032import java.util.Arrays;
33import java.util.Collections;
34import java.util.HashSet;
35import java.util.List;
36import java.util.Set;
37import java.util.concurrent.ConcurrentHashMap;
38import java.util.concurrent.ConcurrentMap;
39import java.util.concurrent.CopyOnWriteArrayList;
40import java.util.concurrent.Future;
41
42import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
43import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
44import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070045
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070046/**
47 * Manages inventory of flow rules using trivial in-memory implementation.
48 */
tom85c612b2014-09-19 19:25:47 -070049@Component(immediate = true)
50@Service
tomc78acee2014-09-24 15:16:55 -070051public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070052 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070053 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070054
tom85c612b2014-09-19 19:25:47 -070055 private final Logger log = getLogger(getClass());
56
alshabiba68eb962014-09-24 20:34:13 -070057
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070058 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070059 // inner Map value (FlowId synonym list) must be synchronized before modifying
60 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070061 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070062
tom85c612b2014-09-19 19:25:47 -070063 @Activate
64 public void activate() {
65 log.info("Started");
66 }
67
68 @Deactivate
69 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070070 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -070071 log.info("Stopped");
72 }
73
alshabiba68eb962014-09-24 20:34:13 -070074
tombe988312014-09-19 18:38:47 -070075 @Override
tom9b4030d2014-10-06 10:39:03 -070076 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070077 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070078 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
79 for (List<StoredFlowEntry> fes : ft.values()) {
80 sum += fes.size();
81 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070082 }
83 return sum;
84 }
85
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070086 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
87 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070088 }
89
90 /**
91 * Returns the flow table for specified device.
92 *
93 * @param deviceId identifier of the device
94 * @return Map representing Flow Table of given device.
95 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070096 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070097 return createIfAbsentUnchecked(flowEntries,
98 deviceId, lazyEmptyFlowTable());
99 }
100
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700101 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
102 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
103 List<StoredFlowEntry> r = flowTable.get(flowId);
104 if (r == null) {
105 final List<StoredFlowEntry> concurrentlyAdded;
106 r = new CopyOnWriteArrayList<>();
107 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
108 if (concurrentlyAdded != null) {
109 return concurrentlyAdded;
110 }
111 }
112 return r;
113 }
114
115 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
116 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
117 for (StoredFlowEntry fe : fes) {
118 if (fe.equals(rule)) {
119 return fe;
120 }
121 }
122 return null;
tom9b4030d2014-10-06 10:39:03 -0700123 }
124
125 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700126 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700127 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700128 }
129
130 @Override
131 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700132 // flatten and make iterator unmodifiable
133 return FluentIterable.from(getFlowTable(deviceId).values())
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700134 .transformAndConcat(
135 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700136
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700137 @Override
138 public Iterable<? extends FlowEntry> apply(
139 List<StoredFlowEntry> input) {
140 return Collections.unmodifiableList(input);
141 }
142 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700143 }
144
145 @Override
146 public Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
147
148 Set<FlowRule> rules = new HashSet<>();
149 for (DeviceId did : flowEntries.keySet()) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700150 for (FlowEntry fe : getFlowEntries(did)) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700151 if (fe.appId() == appId.id()) {
152 rules.add(fe);
153 }
alshabiba68eb962014-09-24 20:34:13 -0700154 }
155 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700156 return rules;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700157 }
158
tombe988312014-09-19 18:38:47 -0700159 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700160 public void storeFlowRule(FlowRule rule) {
161 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700162 }
163
Madan Jampani117aaae2014-10-23 10:04:05 -0700164 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700165 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700166 final DeviceId did = f.deviceId();
167 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700168 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
169 synchronized (existing) {
170 for (StoredFlowEntry fe : existing) {
171 if (fe.equals(rule)) {
172 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700173 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700174 }
175 }
176 // new flow rule added
177 existing.add(f);
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700178 notifyDelegate(FlowRuleBatchEvent.requested(
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700179 new FlowRuleBatchRequest(1, /* FIXME generate something */
180 Arrays.<FlowEntry>asList(f),
181 Collections.<FlowEntry>emptyList())));
alshabiba68eb962014-09-24 20:34:13 -0700182 }
183 }
184
185 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700186 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700187
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700188 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700189
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700190 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?
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700196 notifyDelegate(FlowRuleBatchEvent.requested(
197 new FlowRuleBatchRequest(1, /* FIXME generate something */
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700198 Collections.<FlowEntry>emptyList(),
199 Arrays.<FlowEntry>asList(entry))));
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700200 }
201 }
202 }
alshabib219ebaa2014-09-22 15:41:24 -0700203 }
Madan Jampani31961c12014-10-23 12:06:58 -0700204
205
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700206 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700207 }
208
tombe988312014-09-19 18:38:47 -0700209 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700210 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700211 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700212 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
213 synchronized (entries) {
214 for (StoredFlowEntry stored : entries) {
215 if (stored.equals(rule)) {
216 synchronized (stored) {
217 stored.setBytes(rule.bytes());
218 stored.setLife(rule.life());
219 stored.setPackets(rule.packets());
220 if (stored.state() == FlowEntryState.PENDING_ADD) {
221 stored.setState(FlowEntryState.ADDED);
222 // TODO: Do we need to change `rule` state?
223 return new FlowRuleEvent(Type.RULE_ADDED, rule);
224 }
225 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
226 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700227 }
alshabibba5ac482014-10-02 17:15:20 -0700228 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700229 }
alshabib219ebaa2014-09-22 15:41:24 -0700230
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700231 // should not reach here
232 // storeFlowRule was expected to be called
233 log.error("FlowRule was not found in store {} to update", rule);
234
alshabib8ca53902014-10-07 13:11:17 -0700235 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700236 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700237 }
238
tombe988312014-09-19 18:38:47 -0700239 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700240 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700241 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700242 final DeviceId did = rule.deviceId();
243
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700244 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
245 synchronized (entries) {
246 if (entries.remove(rule)) {
247 return new FlowRuleEvent(RULE_REMOVED, rule);
248 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700249 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700250 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700251 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700252
253 @Override
254 public Future<CompletedBatchOperation> storeBatch(
255 FlowRuleBatchOperation batchOperation) {
256 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
257 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
258 storeFlowRule(entry.getTarget());
259 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
260 deleteFlowRule(entry.getTarget());
261 } else {
262 throw new UnsupportedOperationException("Unsupported operation type");
263 }
264 }
265 return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet()));
266 }
267
268 @Override
269 public void batchOperationComplete(FlowRuleBatchEvent event) {
270 notifyDelegate(event);
271 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700272}