Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.trivial.impl; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 18 | import com.google.common.base.Function; |
| 19 | import com.google.common.collect.FluentIterable; |
| 20 | import com.google.common.util.concurrent.Futures; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.DeviceId; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 28 | import org.onlab.onos.net.flow.FlowEntry; |
| 29 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.FlowId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.FlowRule; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 33 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 34 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
| 35 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 36 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.FlowRuleEvent; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.StoredFlowEntry; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 43 | import org.onlab.util.NewConcurrentHashMap; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 46 | import java.util.Arrays; |
| 47 | import java.util.Collections; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 48 | import java.util.List; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 49 | import java.util.concurrent.ConcurrentHashMap; |
| 50 | import java.util.concurrent.ConcurrentMap; |
| 51 | import java.util.concurrent.CopyOnWriteArrayList; |
| 52 | import java.util.concurrent.Future; |
| 53 | |
| 54 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 55 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 56 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 57 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 60 | */ |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 61 | @Component(immediate = true) |
| 62 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 63 | public class SimpleFlowRuleStore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 64 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 65 | implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 66 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 69 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 70 | // inner Map is Device flow table |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 71 | // inner Map value (FlowId synonym list) must be synchronized before modifying |
| 72 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>> |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 73 | flowEntries = new ConcurrentHashMap<>(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 74 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 75 | @Activate |
| 76 | public void activate() { |
| 77 | log.info("Started"); |
| 78 | } |
| 79 | |
| 80 | @Deactivate |
| 81 | public void deactivate() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 82 | flowEntries.clear(); |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 86 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 87 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 88 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 89 | int sum = 0; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 90 | for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) { |
| 91 | for (List<StoredFlowEntry> fes : ft.values()) { |
| 92 | sum += fes.size(); |
| 93 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 94 | } |
| 95 | return sum; |
| 96 | } |
| 97 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 98 | private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() { |
| 99 | return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded(); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 100 | } |
| 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 HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 108 | private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 109 | return createIfAbsentUnchecked(flowEntries, |
| 110 | deviceId, lazyEmptyFlowTable()); |
| 111 | } |
| 112 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 113 | 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; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 138 | public FlowEntry getFlowEntry(FlowRule rule) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 139 | return getFlowEntryInternal(rule.deviceId(), rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 144 | // flatten and make iterator unmodifiable |
| 145 | return FluentIterable.from(getFlowTable(deviceId).values()) |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 146 | .transformAndConcat( |
| 147 | new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 148 | |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public Iterable<? extends FlowEntry> apply( |
| 151 | List<StoredFlowEntry> input) { |
| 152 | return Collections.unmodifiableList(input); |
| 153 | } |
| 154 | }); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 158 | public void storeFlowRule(FlowRule rule) { |
| 159 | storeFlowRuleInternal(rule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 162 | private void storeFlowRuleInternal(FlowRule rule) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 163 | StoredFlowEntry f = new DefaultFlowEntry(rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 164 | final DeviceId did = f.deviceId(); |
| 165 | final FlowId fid = f.id(); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 166 | 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 Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 171 | return; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | // new flow rule added |
| 175 | existing.add(f); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 176 | notifyDelegate(FlowRuleBatchEvent.requested( |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 177 | new FlowRuleBatchRequest(1, /* FIXME generate something */ |
| 178 | Arrays.<FlowEntry>asList(f), |
| 179 | Collections.<FlowEntry>emptyList()))); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 184 | public void deleteFlowRule(FlowRule rule) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 185 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 186 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 187 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 188 | synchronized (entries) { |
| 189 | for (StoredFlowEntry entry : entries) { |
| 190 | if (entry.equals(rule)) { |
| 191 | synchronized (entry) { |
| 192 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 193 | // TODO: Should we notify only if it's "remote" event? |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 194 | notifyDelegate(FlowRuleBatchEvent.requested( |
| 195 | new FlowRuleBatchRequest(1, /* FIXME generate something */ |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 196 | Collections.<FlowEntry>emptyList(), |
| 197 | Arrays.<FlowEntry>asList(entry)))); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 201 | } |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 202 | |
| 203 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 204 | //log.warn("Cannot find rule {}", rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 207 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 208 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 209 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 210 | 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 HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 225 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 226 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 227 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 228 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 229 | // should not reach here |
| 230 | // storeFlowRule was expected to be called |
| 231 | log.error("FlowRule was not found in store {} to update", rule); |
| 232 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 233 | //flowEntries.put(did, rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 234 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 235 | } |
| 236 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 237 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 238 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 239 | // This is where one could mark a rule as removed and still keep it in the store. |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 240 | final DeviceId did = rule.deviceId(); |
| 241 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 242 | List<StoredFlowEntry> entries = getFlowEntries(did, rule.id()); |
| 243 | synchronized (entries) { |
| 244 | if (entries.remove(rule)) { |
| 245 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 246 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 247 | } |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 248 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 249 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 250 | |
| 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 Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 270 | } |