blob: 4cd31d5cdf03e7b156ca3acfd59813f16eb3267a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
tomea961ff2014-10-01 12:45:15 -070016package org.onlab.onos.store.trivial.impl;
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;
24import com.google.common.util.concurrent.Futures;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080025import com.google.common.util.concurrent.SettableFuture;
26
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;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070031import org.onlab.onos.net.DeviceId;
Madan Jampani117aaae2014-10-23 10:04:05 -070032import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070033import org.onlab.onos.net.flow.DefaultFlowEntry;
34import org.onlab.onos.net.flow.FlowEntry;
35import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070036import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037import org.onlab.onos.net.flow.FlowRule;
Madan Jampani117aaae2014-10-23 10:04:05 -070038import org.onlab.onos.net.flow.FlowRuleBatchEntry;
39import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
40import org.onlab.onos.net.flow.FlowRuleBatchEvent;
41import org.onlab.onos.net.flow.FlowRuleBatchOperation;
42import org.onlab.onos.net.flow.FlowRuleBatchRequest;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070043import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070044import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070045import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070046import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070047import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070048import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070049import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070050import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070051
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080052import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070053import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070054import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070055import java.util.concurrent.ConcurrentHashMap;
56import java.util.concurrent.ConcurrentMap;
57import java.util.concurrent.CopyOnWriteArrayList;
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080058import java.util.concurrent.ExecutionException;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070059import java.util.concurrent.Future;
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;
65import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
66import 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
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080085 private final AtomicInteger localBatchIdGen = new AtomicInteger();
86
87 // TODO: make this configurable
88 private int pendingFutureTimeoutMinutes = 5;
89
90 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
91 CacheBuilder.newBuilder()
92 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -080093 .removalListener(new TimeoutFuture())
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080094 .build();
95
tom85c612b2014-09-19 19:25:47 -070096 @Activate
97 public void activate() {
98 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700103 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700104 log.info("Stopped");
105 }
106
alshabiba68eb962014-09-24 20:34:13 -0700107
tombe988312014-09-19 18:38:47 -0700108 @Override
tom9b4030d2014-10-06 10:39:03 -0700109 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700110 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700111 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
112 for (List<StoredFlowEntry> fes : ft.values()) {
113 sum += fes.size();
114 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700115 }
116 return sum;
117 }
118
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700119 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
120 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700121 }
122
123 /**
124 * Returns the flow table for specified device.
125 *
126 * @param deviceId identifier of the device
127 * @return Map representing Flow Table of given device.
128 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700129 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700130 return createIfAbsentUnchecked(flowEntries,
131 deviceId, lazyEmptyFlowTable());
132 }
133
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700134 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
135 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
136 List<StoredFlowEntry> r = flowTable.get(flowId);
137 if (r == null) {
138 final List<StoredFlowEntry> concurrentlyAdded;
139 r = new CopyOnWriteArrayList<>();
140 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
141 if (concurrentlyAdded != null) {
142 return concurrentlyAdded;
143 }
144 }
145 return r;
146 }
147
148 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
149 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
150 for (StoredFlowEntry fe : fes) {
151 if (fe.equals(rule)) {
152 return fe;
153 }
154 }
155 return null;
tom9b4030d2014-10-06 10:39:03 -0700156 }
157
158 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700159 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700160 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700161 }
162
163 @Override
164 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700165 // flatten and make iterator unmodifiable
166 return FluentIterable.from(getFlowTable(deviceId).values())
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700167 .transformAndConcat(
168 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700169
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700170 @Override
171 public Iterable<? extends FlowEntry> apply(
172 List<StoredFlowEntry> input) {
173 return Collections.unmodifiableList(input);
174 }
175 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700176 }
177
178 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700179 public void storeFlowRule(FlowRule rule) {
180 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700181 }
182
Madan Jampani117aaae2014-10-23 10:04:05 -0700183 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700184 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700185 final DeviceId did = f.deviceId();
186 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700187 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
188 synchronized (existing) {
189 for (StoredFlowEntry fe : existing) {
190 if (fe.equals(rule)) {
191 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700192 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700193 }
194 }
195 // new flow rule added
196 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700197 }
198 }
199
200 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700201 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700202
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700203 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700204
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700205 synchronized (entries) {
206 for (StoredFlowEntry entry : entries) {
207 if (entry.equals(rule)) {
208 synchronized (entry) {
209 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700210 }
211 }
212 }
alshabib219ebaa2014-09-22 15:41:24 -0700213 }
Madan Jampani31961c12014-10-23 12:06:58 -0700214
215
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700216 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700217 }
218
tombe988312014-09-19 18:38:47 -0700219 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700220 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700221 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700222 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
223 synchronized (entries) {
224 for (StoredFlowEntry stored : entries) {
225 if (stored.equals(rule)) {
226 synchronized (stored) {
227 stored.setBytes(rule.bytes());
228 stored.setLife(rule.life());
229 stored.setPackets(rule.packets());
230 if (stored.state() == FlowEntryState.PENDING_ADD) {
231 stored.setState(FlowEntryState.ADDED);
232 // TODO: Do we need to change `rule` state?
233 return new FlowRuleEvent(Type.RULE_ADDED, rule);
234 }
235 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
236 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700237 }
alshabibba5ac482014-10-02 17:15:20 -0700238 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700239 }
alshabib219ebaa2014-09-22 15:41:24 -0700240
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700241 // should not reach here
242 // storeFlowRule was expected to be called
243 log.error("FlowRule was not found in store {} to update", rule);
244
alshabib8ca53902014-10-07 13:11:17 -0700245 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700246 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700247 }
248
tombe988312014-09-19 18:38:47 -0700249 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700250 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700251 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700252 final DeviceId did = rule.deviceId();
253
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700254 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
255 synchronized (entries) {
256 if (entries.remove(rule)) {
257 return new FlowRuleEvent(RULE_REMOVED, rule);
258 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700259 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700260 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700261 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700262
263 @Override
264 public Future<CompletedBatchOperation> storeBatch(
265 FlowRuleBatchOperation batchOperation) {
Brian O'Connor427a1762014-11-19 18:40:32 -0800266 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
267 List<FlowRuleBatchEntry> toRemove = new ArrayList<>();
Madan Jampani117aaae2014-10-23 10:04:05 -0700268 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800269 final FlowRule flowRule = entry.getTarget();
Madan Jampani117aaae2014-10-23 10:04:05 -0700270 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800271 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
272 storeFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800273 toAdd.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800274 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700275 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800276 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
277 deleteFlowRule(flowRule);
Brian O'Connor427a1762014-11-19 18:40:32 -0800278 toRemove.add(entry);
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800279 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700280 } else {
281 throw new UnsupportedOperationException("Unsupported operation type");
282 }
283 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800284
285 if (toAdd.isEmpty() && toRemove.isEmpty()) {
286 return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowRule>emptySet()));
287 }
288
289 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
290 final int batchId = localBatchIdGen.incrementAndGet();
291
292 pendingFutures.put(batchId, r);
293 notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove)));
294
295 return r;
Madan Jampani117aaae2014-10-23 10:04:05 -0700296 }
297
298 @Override
299 public void batchOperationComplete(FlowRuleBatchEvent event) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800300 final Integer batchId = event.subject().batchId();
301 SettableFuture<CompletedBatchOperation> future
302 = pendingFutures.getIfPresent(batchId);
303 if (future != null) {
304 future.set(event.result());
305 pendingFutures.invalidate(batchId);
306 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700307 notifyDelegate(event);
308 }
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800309
310 private static final class TimeoutFuture
311 implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> {
312 @Override
313 public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
314 // wrapping in ExecutionException to support Future.get
315 notification.getValue()
316 .setException(new ExecutionException("Timed out",
317 new TimeoutException()));
318 }
319 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700320}