blob: 49b0c71e62561a0fe5ea379a81fd3a12c1cb466d [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;
19import com.google.common.collect.FluentIterable;
20import com.google.common.util.concurrent.Futures;
tom85c612b2014-09-19 19:25:47 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Service;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070025import org.onlab.onos.net.DeviceId;
Madan Jampani117aaae2014-10-23 10:04:05 -070026import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabib1c319ff2014-10-04 20:29:09 -070027import org.onlab.onos.net.flow.DefaultFlowEntry;
28import org.onlab.onos.net.flow.FlowEntry;
29import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070030import org.onlab.onos.net.flow.FlowId;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070031import org.onlab.onos.net.flow.FlowRule;
Madan Jampani117aaae2014-10-23 10:04:05 -070032import org.onlab.onos.net.flow.FlowRuleBatchEntry;
33import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
34import org.onlab.onos.net.flow.FlowRuleBatchEvent;
35import org.onlab.onos.net.flow.FlowRuleBatchOperation;
36import org.onlab.onos.net.flow.FlowRuleBatchRequest;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070037import org.onlab.onos.net.flow.FlowRuleEvent;
alshabib219ebaa2014-09-22 15:41:24 -070038import org.onlab.onos.net.flow.FlowRuleEvent.Type;
tombe988312014-09-19 18:38:47 -070039import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070040import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070041import org.onlab.onos.net.flow.StoredFlowEntry;
tomc78acee2014-09-24 15:16:55 -070042import org.onlab.onos.store.AbstractStore;
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070043import org.onlab.util.NewConcurrentHashMap;
tom85c612b2014-09-19 19:25:47 -070044import org.slf4j.Logger;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070045
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070046import java.util.Arrays;
47import java.util.Collections;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070048import java.util.List;
Thomas Vachuska8ac922d2014-10-23 16:17:03 -070049import java.util.concurrent.ConcurrentHashMap;
50import java.util.concurrent.ConcurrentMap;
51import java.util.concurrent.CopyOnWriteArrayList;
52import java.util.concurrent.Future;
53
54import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
55import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
56import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070057
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070058/**
59 * Manages inventory of flow rules using trivial in-memory implementation.
60 */
tom85c612b2014-09-19 19:25:47 -070061@Component(immediate = true)
62@Service
tomc78acee2014-09-24 15:16:55 -070063public class SimpleFlowRuleStore
Madan Jampani117aaae2014-10-23 10:04:05 -070064 extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate>
tomc78acee2014-09-24 15:16:55 -070065 implements FlowRuleStore {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070066
tom85c612b2014-09-19 19:25:47 -070067 private final Logger log = getLogger(getClass());
68
alshabiba68eb962014-09-24 20:34:13 -070069
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070070 // inner Map is Device flow table
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070071 // inner Map value (FlowId synonym list) must be synchronized before modifying
72 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070073 flowEntries = new ConcurrentHashMap<>();
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070074
tom85c612b2014-09-19 19:25:47 -070075 @Activate
76 public void activate() {
77 log.info("Started");
78 }
79
80 @Deactivate
81 public void deactivate() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070082 flowEntries.clear();
tom85c612b2014-09-19 19:25:47 -070083 log.info("Stopped");
84 }
85
alshabiba68eb962014-09-24 20:34:13 -070086
tombe988312014-09-19 18:38:47 -070087 @Override
tom9b4030d2014-10-06 10:39:03 -070088 public int getFlowRuleCount() {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070089 int sum = 0;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070090 for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) {
91 for (List<StoredFlowEntry> fes : ft.values()) {
92 sum += fes.size();
93 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -070094 }
95 return sum;
96 }
97
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -070098 private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() {
99 return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded();
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700100 }
101
102 /**
103 * Returns the flow table for specified device.
104 *
105 * @param deviceId identifier of the device
106 * @return Map representing Flow Table of given device.
107 */
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700108 private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700109 return createIfAbsentUnchecked(flowEntries,
110 deviceId, lazyEmptyFlowTable());
111 }
112
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700113 private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) {
114 final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId);
115 List<StoredFlowEntry> r = flowTable.get(flowId);
116 if (r == null) {
117 final List<StoredFlowEntry> concurrentlyAdded;
118 r = new CopyOnWriteArrayList<>();
119 concurrentlyAdded = flowTable.putIfAbsent(flowId, r);
120 if (concurrentlyAdded != null) {
121 return concurrentlyAdded;
122 }
123 }
124 return r;
125 }
126
127 private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) {
128 List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id());
129 for (StoredFlowEntry fe : fes) {
130 if (fe.equals(rule)) {
131 return fe;
132 }
133 }
134 return null;
tom9b4030d2014-10-06 10:39:03 -0700135 }
136
137 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700138 public FlowEntry getFlowEntry(FlowRule rule) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700139 return getFlowEntryInternal(rule.deviceId(), rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700140 }
141
142 @Override
143 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700144 // flatten and make iterator unmodifiable
145 return FluentIterable.from(getFlowTable(deviceId).values())
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700146 .transformAndConcat(
147 new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() {
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700148
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700149 @Override
150 public Iterable<? extends FlowEntry> apply(
151 List<StoredFlowEntry> input) {
152 return Collections.unmodifiableList(input);
153 }
154 });
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700155 }
156
157 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700158 public void storeFlowRule(FlowRule rule) {
159 storeFlowRuleInternal(rule);
alshabib219ebaa2014-09-22 15:41:24 -0700160 }
161
Madan Jampani117aaae2014-10-23 10:04:05 -0700162 private void storeFlowRuleInternal(FlowRule rule) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700163 StoredFlowEntry f = new DefaultFlowEntry(rule);
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700164 final DeviceId did = f.deviceId();
165 final FlowId fid = f.id();
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700166 List<StoredFlowEntry> existing = getFlowEntries(did, fid);
167 synchronized (existing) {
168 for (StoredFlowEntry fe : existing) {
169 if (fe.equals(rule)) {
170 // was already there? ignore
Madan Jampani117aaae2014-10-23 10:04:05 -0700171 return;
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700172 }
173 }
174 // new flow rule added
175 existing.add(f);
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700176 notifyDelegate(FlowRuleBatchEvent.requested(
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700177 new FlowRuleBatchRequest(1, /* FIXME generate something */
178 Arrays.<FlowEntry>asList(f),
179 Collections.<FlowEntry>emptyList())));
alshabiba68eb962014-09-24 20:34:13 -0700180 }
181 }
182
183 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700184 public void deleteFlowRule(FlowRule rule) {
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700185
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700186 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
alshabib2374fc92014-10-22 11:03:23 -0700187
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700188 synchronized (entries) {
189 for (StoredFlowEntry entry : entries) {
190 if (entry.equals(rule)) {
191 synchronized (entry) {
192 entry.setState(FlowEntryState.PENDING_REMOVE);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700193 // TODO: Should we notify only if it's "remote" event?
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700194 notifyDelegate(FlowRuleBatchEvent.requested(
195 new FlowRuleBatchRequest(1, /* FIXME generate something */
Thomas Vachuska8ac922d2014-10-23 16:17:03 -0700196 Collections.<FlowEntry>emptyList(),
197 Arrays.<FlowEntry>asList(entry))));
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700198 }
199 }
200 }
alshabib219ebaa2014-09-22 15:41:24 -0700201 }
Madan Jampani31961c12014-10-23 12:06:58 -0700202
203
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700204 //log.warn("Cannot find rule {}", rule);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700205 }
206
tombe988312014-09-19 18:38:47 -0700207 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700208 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700209 // check if this new rule is an update to an existing entry
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700210 List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
211 synchronized (entries) {
212 for (StoredFlowEntry stored : entries) {
213 if (stored.equals(rule)) {
214 synchronized (stored) {
215 stored.setBytes(rule.bytes());
216 stored.setLife(rule.life());
217 stored.setPackets(rule.packets());
218 if (stored.state() == FlowEntryState.PENDING_ADD) {
219 stored.setState(FlowEntryState.ADDED);
220 // TODO: Do we need to change `rule` state?
221 return new FlowRuleEvent(Type.RULE_ADDED, rule);
222 }
223 return new FlowRuleEvent(Type.RULE_UPDATED, rule);
224 }
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700225 }
alshabibba5ac482014-10-02 17:15:20 -0700226 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700227 }
alshabib219ebaa2014-09-22 15:41:24 -0700228
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700229 // should not reach here
230 // storeFlowRule was expected to be called
231 log.error("FlowRule was not found in store {} to update", rule);
232
alshabib8ca53902014-10-07 13:11:17 -0700233 //flowEntries.put(did, rule);
alshabibba5ac482014-10-02 17:15:20 -0700234 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700235 }
236
tombe988312014-09-19 18:38:47 -0700237 @Override
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700238 public FlowRuleEvent removeFlowRule(FlowEntry rule) {
alshabib1c319ff2014-10-04 20:29:09 -0700239 // This is where one could mark a rule as removed and still keep it in the store.
Yuta HIGUCHI605347c2014-10-17 21:05:23 -0700240 final DeviceId did = rule.deviceId();
241
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700242 List<StoredFlowEntry> entries = getFlowEntries(did, rule.id());
243 synchronized (entries) {
244 if (entries.remove(rule)) {
245 return new FlowRuleEvent(RULE_REMOVED, rule);
246 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700247 }
Yuta HIGUCHI94ffdd42014-10-20 16:36:52 -0700248 return null;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700249 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700250
251 @Override
252 public Future<CompletedBatchOperation> storeBatch(
253 FlowRuleBatchOperation batchOperation) {
254 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
255 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
256 storeFlowRule(entry.getTarget());
257 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
258 deleteFlowRule(entry.getTarget());
259 } else {
260 throw new UnsupportedOperationException("Unsupported operation type");
261 }
262 }
263 return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet()));
264 }
265
266 @Override
267 public void batchOperationComplete(FlowRuleBatchEvent event) {
268 notifyDelegate(event);
269 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700270}