blob: b7d26fb05c752452ca60737d0901934612b47294 [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;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070021import com.google.common.collect.FluentIterable;
22import com.google.common.util.concurrent.Futures;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080023import com.google.common.util.concurrent.SettableFuture;
24
tom85c612b2014-09-19 19:25:47 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Service;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070029import org.onlab.onos.net.DeviceId;
Madan Jampani117aaae2014-10-23 10:04:05 -070030import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070031import org.onlab.onos.net.flow.DefaultFlowEntry;
32import org.onlab.onos.net.flow.FlowEntry;
33import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070034import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070035import org.onlab.onos.net.flow.FlowRule;
Madan Jampani117aaae2014-10-23 10:04:05 -070036import org.onlab.onos.net.flow.FlowRuleBatchEntry;
37import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
38import org.onlab.onos.net.flow.FlowRuleBatchEvent;
39import org.onlab.onos.net.flow.FlowRuleBatchOperation;
40import org.onlab.onos.net.flow.FlowRuleBatchRequest;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070041import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070042import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070043import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070044import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070045import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070046import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070047import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070048import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070049
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080050import java.util.ArrayList;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070051import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070052import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070053import java.util.concurrent.ConcurrentHashMap;
54import java.util.concurrent.ConcurrentMap;
55import java.util.concurrent.CopyOnWriteArrayList;
56import java.util.concurrent.Future;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080057import java.util.concurrent.TimeUnit;
58import java.util.concurrent.atomic.AtomicInteger;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070059
60import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
61import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
62import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070063
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070064/**
65 * Manages inventory of flow rules using trivial in-memory implementation.
66 */
tom85c612b2014-09-19 19:25:47 -070067@Component(immediate = true)
68@Service
tomc78acee2014-09-24 15:16:55 -070069public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070070 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070071 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070072
tom85c612b2014-09-19 19:25:47 -070073 private final Logger log = getLogger(getClass());
74
alshabiba68eb962014-09-24 20:34:13 -070075
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070076 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070077 // inner Map value (FlowId synonym list) must be synchronized before modifying
78 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070079 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070080
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -080081 private final AtomicInteger localBatchIdGen = new AtomicInteger();
82
83 // TODO: make this configurable
84 private int pendingFutureTimeoutMinutes = 5;
85
86 private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
87 CacheBuilder.newBuilder()
88 .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
89 // TODO Explicitly fail the future if expired?
90 //.removalListener(listener)
91 .build();
92
tom85c612b2014-09-19 19:25:47 -070093 @Activate
94 public void activate() {
95 log.info("Started");
96 }
97
98 @Deactivate
99 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700100 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -0700101 log.info("Stopped");
102 }
103
alshabiba68eb962014-09-24 20:34:13 -0700104
tombe988312014-09-19 18:38:47 -0700105 @Override
tom9b4030d2014-10-06 10:39:03 -0700106 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700107 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700108 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
109 for (List<StoredFlowEntry> fes : ft.values()) {
110 sum += fes.size();
111 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700112 }
113 return sum;
114 }
115
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700116 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
117 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700118 }
119
120 /**
121 * Returns the flow table for specified device.
122 *
123 * @param deviceId identifier of the device
124 * @return Map representing Flow Table of given device.
125 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700126 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700127 return createIfAbsentUnchecked(flowEntries,
128 deviceId, lazyEmptyFlowTable());
129 }
130
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700131 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
132 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
133 List<StoredFlowEntry> r = flowTable.get(flowId);
134 if (r == null) {
135 final List<StoredFlowEntry> concurrentlyAdded;
136 r = new CopyOnWriteArrayList<>();
137 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
138 if (concurrentlyAdded != null) {
139 return concurrentlyAdded;
140 }
141 }
142 return r;
143 }
144
145 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
146 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
147 for (StoredFlowEntry fe : fes) {
148 if (fe.equals(rule)) {
149 return fe;
150 }
151 }
152 return null;
tom9b4030d2014-10-06 10:39:03 -0700153 }
154
155 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700156 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700157 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700158 }
159
160 @Override
161 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700162 // flatten and make iterator unmodifiable
163 return FluentIterable.from(getFlowTable(deviceId).values())
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700164 .transformAndConcat(
165 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700166
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700167 @Override
168 public Iterable<? extends FlowEntry> apply(
169 List<StoredFlowEntry> input) {
170 return Collections.unmodifiableList(input);
171 }
172 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700173 }
174
175 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700176 public void storeFlowRule(FlowRule rule) {
177 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700178 }
179
Madan Jampani117aaae2014-10-23 10:04:05 -0700180 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700181 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700182 final DeviceId did = f.deviceId();
183 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700184 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
185 synchronized (existing) {
186 for (StoredFlowEntry fe : existing) {
187 if (fe.equals(rule)) {
188 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700189 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700190 }
191 }
192 // new flow rule added
193 existing.add(f);
alshabiba68eb962014-09-24 20:34:13 -0700194 }
195 }
196
197 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700198 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700199
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700200 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700201
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700202 synchronized (entries) {
203 for (StoredFlowEntry entry : entries) {
204 if (entry.equals(rule)) {
205 synchronized (entry) {
206 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700207 }
208 }
209 }
alshabib219ebaa2014-09-22 15:41:24 -0700210 }
Madan Jampani31961c12014-10-23 12:06:58 -0700211
212
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700213 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700214 }
215
tombe988312014-09-19 18:38:47 -0700216 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700217 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700218 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700219 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
220 synchronized (entries) {
221 for (StoredFlowEntry stored : entries) {
222 if (stored.equals(rule)) {
223 synchronized (stored) {
224 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
261 public Future<CompletedBatchOperation> storeBatch(
262 FlowRuleBatchOperation batchOperation) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800263 List<FlowRule> toAdd = new ArrayList<>();
264 List<FlowRule> toRemove = new ArrayList<>();
Madan Jampani117aaae2014-10-23 10:04:05 -0700265 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800266 final FlowRule flowRule = entry.getTarget();
Madan Jampani117aaae2014-10-23 10:04:05 -0700267 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800268 if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
269 storeFlowRule(flowRule);
270 toAdd.add(flowRule);
271 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700272 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800273 if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
274 deleteFlowRule(flowRule);
275 toRemove.add(flowRule);
276 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700277 } else {
278 throw new UnsupportedOperationException("Unsupported operation type");
279 }
280 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800281
282 if (toAdd.isEmpty() && toRemove.isEmpty()) {
283 return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowRule>emptySet()));
284 }
285
286 SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
287 final int batchId = localBatchIdGen.incrementAndGet();
288
289 pendingFutures.put(batchId, r);
290 notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove)));
291
292 return r;
Madan Jampani117aaae2014-10-23 10:04:05 -0700293 }
294
295 @Override
296 public void batchOperationComplete(FlowRuleBatchEvent event) {
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800297 final Integer batchId = event.subject().batchId();
298 SettableFuture<CompletedBatchOperation> future
299 = pendingFutures.getIfPresent(batchId);
300 if (future != null) {
301 future.set(event.result());
302 pendingFutures.invalidate(batchId);
303 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700304 notifyDelegate(event);
305 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700306}