blob: 274c84f8ef190ac180a49df2790ac77a127d452a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070017
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080020import com.google.common.cache.RemovalListener;
21import com.google.common.cache.RemovalNotification;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070022import com.google.common.collect.FluentIterable;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070023import com.google.common.collect.ImmutableList;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080024import com.google.common.collect.Sets;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080025import com.google.common.util.concurrent.SettableFuture;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070026
tom85c612b2014-09-19 19:25:47 -070027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Service;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080031import org.onlab.util.NewConcurrentHashMap;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.DeviceId;
33import org.onosproject.net.flow.CompletedBatchOperation;
34import org.onosproject.net.flow.DefaultFlowEntry;
35import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowEntry.FlowEntryState;
37import org.onosproject.net.flow.FlowId;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.FlowRuleBatchEntry;
40import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
41import org.onosproject.net.flow.FlowRuleBatchEvent;
42import org.onosproject.net.flow.FlowRuleBatchOperation;
43import org.onosproject.net.flow.FlowRuleBatchRequest;
44import org.onosproject.net.flow.FlowRuleEvent;
45import org.onosproject.net.flow.FlowRuleEvent.Type;
46import org.onosproject.net.flow.FlowRuleStore;
47import org.onosproject.net.flow.FlowRuleStoreDelegate;
48import org.onosproject.net.flow.StoredFlowEntry;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070049import org.onosproject.net.flow.TableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.store.AbstractStore;
tom85c612b2014-09-19 19:25:47 -070051import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070052
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080053import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070054import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070055import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070056import java.util.concurrent.ConcurrentHashMap;
57import java.util.concurrent.ConcurrentMap;
58import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080059import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080060import java.util.concurrent.TimeUnit;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080061import java.util.concurrent.TimeoutException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080062import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070063
64import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070066import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070067
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070068/**
69 * Manages inventory of flow rules using trivial in-memory implementation.
70 */
tom85c612b2014-09-19 19:25:47 -070071@Component(immediate = true)
72@Service
tomc78acee2014-09-24 15:16:55 -070073public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070074 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070075 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070076
tom85c612b2014-09-19 19:25:47 -070077 private final Logger log = getLogger(getClass());
78
alshabiba68eb962014-09-24 20:34:13 -070079
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070080 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070081 // inner Map value (FlowId synonym list) must be synchronized before modifying
82 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070083 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070084
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070085 private final ConcurrentMap<DeviceId, List<TableStatisticsEntry>>
86 deviceTableStats = new ConcurrentHashMap<>();
87
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080088 private final AtomicInteger localBatchIdGen = new AtomicInteger();
89
90 // TODO: make this configurable
91 private int pendingFutureTimeoutMinutes = 5;
92
93 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
94 CacheBuilder.newBuilder()
95 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080096 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080097 .build();
98
tom85c612b2014-09-19 19:25:47 -070099 @Activate
100 public void activate() {
101 log.info("Started");
102 }
103
104 @Deactivate
105 public void deactivate() {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700106 deviceTableStats.clear();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700107 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700108 log.info("Stopped");
109 }
110
alshabiba68eb962014-09-24 20:34:13 -0700111
tombe988312014-09-19 18:38:47 -0700112 @Override
tom9b4030d2014-10-06 10:39:03 -0700113 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700114 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700115 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
116 for (List<StoredFlowEntry> fes : ft.values()) {
117 sum += fes.size();
118 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700119 }
120 return sum;
121 }
122
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700123 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
124 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700125 }
126
127 /**
128 * Returns the flow table for specified device.
129 *
130 * @param deviceId identifier of the device
131 * @return Map representing Flow Table of given device.
132 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700133 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700134 return createIfAbsentUnchecked(flowEntries,
135 deviceId, lazyEmptyFlowTable());
136 }
137
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700138 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
139 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
140 List<StoredFlowEntry> r = flowTable.get(flowId);
141 if (r == null) {
142 final List<StoredFlowEntry> concurrentlyAdded;
143 r = new CopyOnWriteArrayList<>();
144 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
145 if (concurrentlyAdded != null) {
146 return concurrentlyAdded;
147 }
148 }
149 return r;
150 }
151
152 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
153 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
154 for (StoredFlowEntry fe : fes) {
155 if (fe.equals(rule)) {
156 return fe;
157 }
158 }
159 return null;
tom9b4030d2014-10-06 10:39:03 -0700160 }
161
162 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700163 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700164 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700165 }
166
167 @Override
168 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700169 // flatten and make iterator unmodifiable
170 return FluentIterable.from(getFlowTable(deviceId).values())
Sho SHIMIZU74626412015-09-11 11:46:27 -0700171 .transformAndConcat(Collections::unmodifiableList);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700172 }
173
174 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700175 public void storeFlowRule(FlowRule rule) {
176 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700177 }
178
Madan Jampani117aaae2014-10-23 10:04:05 -0700179 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700180 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700181 final DeviceId did = f.deviceId();
182 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700183 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
184 synchronized (existing) {
185 for (StoredFlowEntry fe : existing) {
186 if (fe.equals(rule)) {
187 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700188 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700189 }
190 }
191 // new flow rule added
192 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700193 }
194 }
195
196 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700197 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700198
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700199 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700200
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700201 synchronized (entries) {
202 for (StoredFlowEntry entry : entries) {
203 if (entry.equals(rule)) {
204 synchronized (entry) {
205 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700206 }
207 }
208 }
alshabib219ebaa2014-09-22 15:41:24 -0700209 }
Madan Jampani31961c12014-10-23 12:06:58 -0700210
211
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700212 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700213 }
214
tombe988312014-09-19 18:38:47 -0700215 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700216 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700217 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700218 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
219 synchronized (entries) {
220 for (StoredFlowEntry stored : entries) {
221 if (stored.equals(rule)) {
222 synchronized (stored) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800223 //FIXME modification of "stored" flow entry outside of flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700224 stored.setBytes(rule.bytes());
225 stored.setLife(rule.life());
226 stored.setPackets(rule.packets());
227 if (stored.state() == FlowEntryState.PENDING_ADD) {
228 stored.setState(FlowEntryState.ADDED);
229 // TODO: Do we need to change `rule` state?
230 return new FlowRuleEvent(Type.RULE_ADDED, rule);
231 }
232 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
233 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700234 }
alshabibba5ac482014-10-02 17:15:20 -0700235 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700236 }
alshabib219ebaa2014-09-22 15:41:24 -0700237
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700238 // should not reach here
239 // storeFlowRule was expected to be called
240 log.error("FlowRule was not found in store {} to update", rule);
241
alshabib8ca53902014-10-07 13:11:17 -0700242 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700243 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700244 }
245
tombe988312014-09-19 18:38:47 -0700246 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700247 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700248 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700249 final DeviceId did = rule.deviceId();
250
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700251 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
252 synchronized (entries) {
253 if (entries.remove(rule)) {
254 return new FlowRuleEvent(RULE_REMOVED, rule);
255 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700256 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700257 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700258 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700259
260 @Override
Charles Chan93fa7272016-01-26 22:27:02 -0800261 public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
262 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
263 synchronized (entries) {
264 for (StoredFlowEntry entry : entries) {
265 if (entry.equals(rule) &&
266 entry.state() != FlowEntryState.PENDING_ADD) {
267 synchronized (entry) {
268 entry.setState(FlowEntryState.PENDING_ADD);
269 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
270 }
271 }
272 }
273 }
274 return null;
275 }
276
Charles Chan0c7c43b2016-01-14 17:39:20 -0800277 public void purgeFlowRule(DeviceId deviceId) {
278 flowEntries.remove(deviceId);
279 }
280
Charles Chan93fa7272016-01-26 22:27:02 -0800281 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800282 public void storeBatch(
283 FlowRuleBatchOperation operation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800284 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
285 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800286
287 for (FlowRuleBatchEntry entry : operation.getOperations()) {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800288 final FlowRule flowRule = entry.target();
289 if (entry.operator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800290 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
291 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800292 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800293 }
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800294 } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800295 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
296 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800297 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800298 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700299 } else {
300 throw new UnsupportedOperationException("Unsupported operation type");
301 }
302 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800303
304 if (toAdd.isEmpty() && toRemove.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800305 notifyDelegate(FlowRuleBatchEvent.completed(
306 new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
307 new CompletedBatchOperation(true, Collections.emptySet(),
308 operation.deviceId())));
309 return;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800310 }
311
312 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
313 final int batchId = localBatchIdGen.incrementAndGet();
314
315 pendingFutures.put(batchId, r);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800316
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800317 toAdd.addAll(toRemove);
318 notifyDelegate(FlowRuleBatchEvent.requested(
319 new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId()));
320
Madan Jampani117aaae2014-10-23 10:04:05 -0700321 }
322
323 @Override
324 public void batchOperationComplete(FlowRuleBatchEvent event) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800325 final Long batchId = event.subject().batchId();
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800326 SettableFuture<CompletedBatchOperation> future
327 = pendingFutures.getIfPresent(batchId);
328 if (future != null) {
329 future.set(event.result());
330 pendingFutures.invalidate(batchId);
331 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700332 notifyDelegate(event);
333 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800334
335 private static final class TimeoutFuture
336 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
337 @Override
338 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
339 // wrapping in ExecutionException to support Future.get
Yuta HIGUCHIc611d922015-02-10 16:07:38 -0800340 if (notification.wasEvicted()) {
341 notification.getValue()
342 .setException(new ExecutionException("Timed out",
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800343 new TimeoutException()));
Yuta HIGUCHIc611d922015-02-10 16:07:38 -0800344 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800345 }
346 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700347
348 @Override
349 public FlowRuleEvent updateTableStatistics(DeviceId deviceId,
350 List<TableStatisticsEntry> tableStats) {
351 deviceTableStats.put(deviceId, tableStats);
352 return null;
353 }
354
355 @Override
356 public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) {
357 List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId);
358 if (tableStats == null) {
359 return Collections.emptyList();
360 }
361 return ImmutableList.copyOf(tableStats);
362 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700363}