blob: 33ba95b697554f2e121899de2a6c071e06da2e1c [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;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -07006import java.util.Collections;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07007import java.util.HashSet;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -07008import java.util.List;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -07009import java.util.Set;
10import java.util.concurrent.ConcurrentHashMap;
11import java.util.concurrent.ConcurrentMap;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070012import java.util.concurrent.CopyOnWriteArrayList;
alshabiba68eb962014-09-24 20:34:13 -070013
tom85c612b2014-09-19 19:25:47 -070014import org.apache.felix.scr.annotations.Activate;
15import org.apache.felix.scr.annotations.Component;
16import org.apache.felix.scr.annotations.Deactivate;
17import org.apache.felix.scr.annotations.Service;
alshabiba68eb962014-09-24 20:34:13 -070018import org.onlab.onos.ApplicationId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070019import org.onlab.onos.net.DeviceId;
alshabib1c319ff2014-10-04 20:29:09 -070020import org.onlab.onos.net.flow.DefaultFlowEntry;
21import org.onlab.onos.net.flow.FlowEntry;
22import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070023import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070024import org.onlab.onos.net.flow.FlowRule;
25import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070026import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070027import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070028import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070029import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070030import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070031import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070032import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070033
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070034import com.google.common.base.Function;
35import com.google.common.collect.FluentIterable;
36
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037/**
38 * Manages inventory of flow rules using trivial in-memory implementation.
39 */
tom85c612b2014-09-19 19:25:47 -070040@Component(immediate = true)
41@Service
tomc78acee2014-09-24 15:16:55 -070042public class SimpleFlowRuleStore
43 extends AbstractStore<FlowRuleEvent, FlowRuleStoreDelegate>
44 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045
tom85c612b2014-09-19 19:25:47 -070046 private final Logger log = getLogger(getClass());
47
alshabiba68eb962014-09-24 20:34:13 -070048
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070049 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070050 // inner Map value (FlowId synonym list) must be synchronized before modifying
51 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070052 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070053
tom85c612b2014-09-19 19:25:47 -070054 @Activate
55 public void activate() {
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070061 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -070062 log.info("Stopped");
63 }
64
alshabiba68eb962014-09-24 20:34:13 -070065
tombe988312014-09-19 18:38:47 -070066 @Override
tom9b4030d2014-10-06 10:39:03 -070067 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070068 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070069 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
70 for (List<StoredFlowEntry> fes : ft.values()) {
71 sum += fes.size();
72 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070073 }
74 return sum;
75 }
76
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070077 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
78 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070079 }
80
81 /**
82 * Returns the flow table for specified device.
83 *
84 * @param deviceId identifier of the device
85 * @return Map representing Flow Table of given device.
86 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070087 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070088 return createIfAbsentUnchecked(flowEntries,
89 deviceId, lazyEmptyFlowTable());
90 }
91
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070092 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
93 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
94 List<StoredFlowEntry> r = flowTable.get(flowId);
95 if (r == null) {
96 final List<StoredFlowEntry> concurrentlyAdded;
97 r = new CopyOnWriteArrayList<>();
98 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
99 if (concurrentlyAdded != null) {
100 return concurrentlyAdded;
101 }
102 }
103 return r;
104 }
105
106 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
107 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
108 for (StoredFlowEntry fe : fes) {
109 if (fe.equals(rule)) {
110 return fe;
111 }
112 }
113 return null;
tom9b4030d2014-10-06 10:39:03 -0700114 }
115
116 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700117 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700118 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700119 }
120
121 @Override
122 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700123 // flatten and make iterator unmodifiable
124 return FluentIterable.from(getFlowTable(deviceId).values())
125 .transformAndConcat(
126 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
127
128 @Override
129 public Iterable<? extends FlowEntry> apply(
130 List<StoredFlowEntry> input) {
131 return Collections.unmodifiableList(input);
132 }
133 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700134 }
135
136 @Override
137 public Iterable<FlowRule> getFlowRulesByAppId(ApplicationId appId) {
138
139 Set<FlowRule> rules = new HashSet<>();
140 for (DeviceId did : flowEntries.keySet()) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700141 for (FlowEntry fe : getFlowEntries(did)) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700142 if (fe.appId() == appId.id()) {
143 rules.add(fe);
144 }
alshabiba68eb962014-09-24 20:34:13 -0700145 }
146 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700147 return rules;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700148 }
149
tombe988312014-09-19 18:38:47 -0700150 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700151 public void storeFlowRule(FlowRule rule) {
152 final boolean added = storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700153 }
154
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700155 private boolean storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700156 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700157 final DeviceId did = f.deviceId();
158 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700159 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
160 synchronized (existing) {
161 for (StoredFlowEntry fe : existing) {
162 if (fe.equals(rule)) {
163 // was already there? ignore
164 return false;
165 }
166 }
167 // new flow rule added
168 existing.add(f);
169 // TODO: notify through delegate about remote event?
170 return true;
alshabiba68eb962014-09-24 20:34:13 -0700171 }
172 }
173
174 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700175 public void deleteFlowRule(FlowRule rule) {
176
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700177 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
178 synchronized (entries) {
179 for (StoredFlowEntry entry : entries) {
180 if (entry.equals(rule)) {
181 synchronized (entry) {
182 entry.setState(FlowEntryState.PENDING_REMOVE);
183 return;
184 }
185 }
186 }
alshabib219ebaa2014-09-22 15:41:24 -0700187 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700188 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700189 }
190
tombe988312014-09-19 18:38:47 -0700191 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700192 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700193 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700194 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
195 synchronized (entries) {
196 for (StoredFlowEntry stored : entries) {
197 if (stored.equals(rule)) {
198 synchronized (stored) {
199 stored.setBytes(rule.bytes());
200 stored.setLife(rule.life());
201 stored.setPackets(rule.packets());
202 if (stored.state() == FlowEntryState.PENDING_ADD) {
203 stored.setState(FlowEntryState.ADDED);
204 // TODO: Do we need to change `rule` state?
205 return new FlowRuleEvent(Type.RULE_ADDED, rule);
206 }
207 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
208 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700209 }
alshabibba5ac482014-10-02 17:15:20 -0700210 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700211 }
alshabib219ebaa2014-09-22 15:41:24 -0700212
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700213 // should not reach here
214 // storeFlowRule was expected to be called
215 log.error("FlowRule was not found in store {} to update", rule);
216
alshabib8ca53902014-10-07 13:11:17 -0700217 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700218 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700219 }
220
tombe988312014-09-19 18:38:47 -0700221 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700222 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700223 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700224 final DeviceId did = rule.deviceId();
225
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700226 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
227 synchronized (entries) {
228 if (entries.remove(rule)) {
229 return new FlowRuleEvent(RULE_REMOVED, rule);
230 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700231 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700232 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700233 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700234}