blob: 3b8f1d35ecf6e6af26ccb19dedca383495a79d29 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070018import com.google.common.base.Function;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080021import com.google.common.cache.RemovalListener;
22import com.google.common.cache.RemovalNotification;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070023import com.google.common.collect.FluentIterable;
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;
tom85c612b2014-09-19 19:25:47 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Service;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080030import org.onlab.util.NewConcurrentHashMap;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.DeviceId;
32import org.onosproject.net.flow.CompletedBatchOperation;
33import org.onosproject.net.flow.DefaultFlowEntry;
34import org.onosproject.net.flow.FlowEntry;
35import org.onosproject.net.flow.FlowEntry.FlowEntryState;
36import org.onosproject.net.flow.FlowId;
37import org.onosproject.net.flow.FlowRule;
38import org.onosproject.net.flow.FlowRuleBatchEntry;
39import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
40import org.onosproject.net.flow.FlowRuleBatchEvent;
41import org.onosproject.net.flow.FlowRuleBatchOperation;
42import org.onosproject.net.flow.FlowRuleBatchRequest;
43import org.onosproject.net.flow.FlowRuleEvent;
44import org.onosproject.net.flow.FlowRuleEvent.Type;
45import org.onosproject.net.flow.FlowRuleStore;
46import org.onosproject.net.flow.FlowRuleStoreDelegate;
47import org.onosproject.net.flow.StoredFlowEntry;
48import org.onosproject.store.AbstractStore;
tom85c612b2014-09-19 19:25:47 -070049import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070050
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080051import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070052import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070053import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070054import java.util.concurrent.ConcurrentHashMap;
55import java.util.concurrent.ConcurrentMap;
56import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080057import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080058import java.util.concurrent.TimeUnit;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080059import java.util.concurrent.TimeoutException;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080060import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070061
62import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070064import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070065
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070066/**
67 * Manages inventory of flow rules using trivial in-memory implementation.
68 */
tom85c612b2014-09-19 19:25:47 -070069@Component(immediate = true)
70@Service
tomc78acee2014-09-24 15:16:55 -070071public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070072 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070073 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070074
tom85c612b2014-09-19 19:25:47 -070075 private final Logger log = getLogger(getClass());
76
alshabiba68eb962014-09-24 20:34:13 -070077
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070078 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070079 // inner Map value (FlowId synonym list) must be synchronized before modifying
80 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070081 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070082
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080083 private final AtomicInteger localBatchIdGen = new AtomicInteger();
84
85 // TODO: make this configurable
86 private int pendingFutureTimeoutMinutes = 5;
87
88 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
89 CacheBuilder.newBuilder()
90 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080091 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080092 .build();
93
tom85c612b2014-09-19 19:25:47 -070094 @Activate
95 public void activate() {
96 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700101 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700102 log.info("Stopped");
103 }
104
alshabiba68eb962014-09-24 20:34:13 -0700105
tombe988312014-09-19 18:38:47 -0700106 @Override
tom9b4030d2014-10-06 10:39:03 -0700107 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700108 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700109 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
110 for (List<StoredFlowEntry> fes : ft.values()) {
111 sum += fes.size();
112 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700113 }
114 return sum;
115 }
116
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700117 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
118 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700119 }
120
121 /**
122 * Returns the flow table for specified device.
123 *
124 * @param deviceId identifier of the device
125 * @return Map representing Flow Table of given device.
126 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700127 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700128 return createIfAbsentUnchecked(flowEntries,
129 deviceId, lazyEmptyFlowTable());
130 }
131
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700132 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
133 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
134 List<StoredFlowEntry> r = flowTable.get(flowId);
135 if (r == null) {
136 final List<StoredFlowEntry> concurrentlyAdded;
137 r = new CopyOnWriteArrayList<>();
138 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
139 if (concurrentlyAdded != null) {
140 return concurrentlyAdded;
141 }
142 }
143 return r;
144 }
145
146 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
147 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
148 for (StoredFlowEntry fe : fes) {
149 if (fe.equals(rule)) {
150 return fe;
151 }
152 }
153 return null;
tom9b4030d2014-10-06 10:39:03 -0700154 }
155
156 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700157 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700158 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700159 }
160
161 @Override
162 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700163 // flatten and make iterator unmodifiable
164 return FluentIterable.from(getFlowTable(deviceId).values())
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700165 .transformAndConcat(
166 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700167
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700168 @Override
169 public Iterable<? extends FlowEntry> apply(
170 List<StoredFlowEntry> input) {
171 return Collections.unmodifiableList(input);
172 }
173 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700174 }
175
176 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700177 public void storeFlowRule(FlowRule rule) {
178 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700179 }
180
Madan Jampani117aaae2014-10-23 10:04:05 -0700181 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700182 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700183 final DeviceId did = f.deviceId();
184 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700185 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
186 synchronized (existing) {
187 for (StoredFlowEntry fe : existing) {
188 if (fe.equals(rule)) {
189 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700190 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700191 }
192 }
193 // new flow rule added
194 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700195 }
196 }
197
198 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700199 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700200
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700201 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700202
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700203 synchronized (entries) {
204 for (StoredFlowEntry entry : entries) {
205 if (entry.equals(rule)) {
206 synchronized (entry) {
207 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700208 }
209 }
210 }
alshabib219ebaa2014-09-22 15:41:24 -0700211 }
Madan Jampani31961c12014-10-23 12:06:58 -0700212
213
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700214 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700215 }
216
tombe988312014-09-19 18:38:47 -0700217 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700218 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700219 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700220 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
221 synchronized (entries) {
222 for (StoredFlowEntry stored : entries) {
223 if (stored.equals(rule)) {
224 synchronized (stored) {
225 stored.setBytes(rule.bytes());
226 stored.setLife(rule.life());
227 stored.setPackets(rule.packets());
228 if (stored.state() == FlowEntryState.PENDING_ADD) {
229 stored.setState(FlowEntryState.ADDED);
230 // TODO: Do we need to change `rule` state?
231 return new FlowRuleEvent(Type.RULE_ADDED, rule);
232 }
233 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
234 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700235 }
alshabibba5ac482014-10-02 17:15:20 -0700236 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700237 }
alshabib219ebaa2014-09-22 15:41:24 -0700238
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700239 // should not reach here
240 // storeFlowRule was expected to be called
241 log.error("FlowRule was not found in store {} to update", rule);
242
alshabib8ca53902014-10-07 13:11:17 -0700243 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700244 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700245 }
246
tombe988312014-09-19 18:38:47 -0700247 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700248 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700249 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700250 final DeviceId did = rule.deviceId();
251
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700252 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
253 synchronized (entries) {
254 if (entries.remove(rule)) {
255 return new FlowRuleEvent(RULE_REMOVED, rule);
256 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700257 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700258 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700259 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700260
261 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800262 public void storeBatch(
263 FlowRuleBatchOperation operation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800264 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
265 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800266
267 for (FlowRuleBatchEntry entry : operation.getOperations()) {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800268 final FlowRule flowRule = entry.target();
269 if (entry.operator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800270 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
271 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800272 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800273 }
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800274 } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800275 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
276 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800277 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800278 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700279 } else {
280 throw new UnsupportedOperationException("Unsupported operation type");
281 }
282 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800283
284 if (toAdd.isEmpty() && toRemove.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800285 notifyDelegate(FlowRuleBatchEvent.completed(
286 new FlowRuleBatchRequest(operation.id(), Collections.emptySet()),
287 new CompletedBatchOperation(true, Collections.emptySet(),
288 operation.deviceId())));
289 return;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800290 }
291
292 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
293 final int batchId = localBatchIdGen.incrementAndGet();
294
295 pendingFutures.put(batchId, r);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800296
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800297 toAdd.addAll(toRemove);
298 notifyDelegate(FlowRuleBatchEvent.requested(
299 new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId()));
300
Madan Jampani117aaae2014-10-23 10:04:05 -0700301 }
302
303 @Override
304 public void batchOperationComplete(FlowRuleBatchEvent event) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800305 final Long batchId = event.subject().batchId();
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800306 SettableFuture<CompletedBatchOperation> future
307 = pendingFutures.getIfPresent(batchId);
308 if (future != null) {
309 future.set(event.result());
310 pendingFutures.invalidate(batchId);
311 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700312 notifyDelegate(event);
313 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800314
315 private static final class TimeoutFuture
316 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
317 @Override
318 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
319 // wrapping in ExecutionException to support Future.get
Yuta HIGUCHIc611d922015-02-10 16:07:38 -0800320 if (notification.wasEvicted()) {
321 notification.getValue()
322 .setException(new ExecutionException("Timed out",
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800323 new TimeoutException()));
Yuta HIGUCHIc611d922015-02-10 16:07:38 -0800324 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800325 }
326 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700327}