Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 20 | import com.google.common.cache.RemovalListener; |
| 21 | import com.google.common.cache.RemovalNotification; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 22 | import com.google.common.collect.FluentIterable; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 25 | import com.google.common.collect.Streams; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 26 | import com.google.common.util.concurrent.SettableFuture; |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 27 | import org.onlab.util.Tools; |
Daniele Moro | 641b4f1 | 2021-07-15 17:02:59 +0200 | [diff] [blame^] | 28 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 31 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 32 | import org.onosproject.net.flow.FlowEntry; |
| 33 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 34 | import org.onosproject.net.flow.FlowId; |
| 35 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.FlowRuleEvent; |
| 37 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 38 | import org.onosproject.net.flow.FlowRuleStore; |
| 39 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 40 | import org.onosproject.net.flow.StoredFlowEntry; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 43 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry.FlowRuleOperation; |
| 44 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 45 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 46 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.store.AbstractStore; |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 48 | import org.osgi.service.component.ComponentContext; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 50 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 51 | import java.util.ArrayList; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 53 | import java.util.Dictionary; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 54 | import java.util.List; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 55 | import java.util.concurrent.ConcurrentHashMap; |
| 56 | import java.util.concurrent.ConcurrentMap; |
| 57 | import java.util.concurrent.CopyOnWriteArrayList; |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 58 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 59 | import java.util.concurrent.TimeUnit; |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 60 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 61 | import java.util.concurrent.atomic.AtomicInteger; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 62 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 64 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 65 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 68 | */ |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 69 | public class SimpleFlowRuleStore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 70 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 71 | implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 72 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 73 | private final Logger log = getLogger(getClass()); |
| 74 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 75 | // inner Map is Device flow table |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 76 | // inner Map value (FlowId synonym list) must be synchronized before modifying |
| 77 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>> |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 78 | flowEntries = new ConcurrentHashMap<>(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 79 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 80 | private final ConcurrentMap<DeviceId, List<TableStatisticsEntry>> |
| 81 | deviceTableStats = new ConcurrentHashMap<>(); |
| 82 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 83 | private final AtomicInteger localBatchIdGen = new AtomicInteger(); |
| 84 | |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 85 | private static final int DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES = 5; |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 86 | private int pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 87 | |
| 88 | private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures = |
| 89 | CacheBuilder.newBuilder() |
| 90 | .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES) |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 91 | .removalListener(new TimeoutFuture()) |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 92 | .build(); |
| 93 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 94 | public void activate() { |
| 95 | log.info("Started"); |
| 96 | } |
| 97 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 98 | public void deactivate() { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 99 | deviceTableStats.clear(); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 100 | flowEntries.clear(); |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 101 | log.info("Stopped"); |
| 102 | } |
| 103 | |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 104 | public void modified(ComponentContext context) { |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 105 | readComponentConfiguration(context); |
| 106 | |
| 107 | // Reset Cache and copy all. |
| 108 | Cache<Integer, SettableFuture<CompletedBatchOperation>> prevFutures = pendingFutures; |
| 109 | pendingFutures = CacheBuilder.newBuilder() |
| 110 | .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES) |
| 111 | .removalListener(new TimeoutFuture()) |
| 112 | .build(); |
| 113 | |
| 114 | pendingFutures.putAll(prevFutures.asMap()); |
| 115 | } |
| 116 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 117 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 118 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 119 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 120 | int sum = 0; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 121 | for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) { |
| 122 | for (List<StoredFlowEntry> fes : ft.values()) { |
| 123 | sum += fes.size(); |
| 124 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 125 | } |
| 126 | return sum; |
| 127 | } |
| 128 | |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 129 | /** |
| 130 | * Extracts properties from the component configuration context. |
| 131 | * |
| 132 | * @param context the component context |
| 133 | */ |
| 134 | private void readComponentConfiguration(ComponentContext context) { |
| 135 | Dictionary<?, ?> properties = context.getProperties(); |
| 136 | |
| 137 | Integer newPendingFutureTimeoutMinutes = |
| 138 | Tools.getIntegerProperty(properties, "pendingFutureTimeoutMinutes"); |
| 139 | if (newPendingFutureTimeoutMinutes == null) { |
| 140 | pendingFutureTimeoutMinutes = DEFAULT_PENDING_FUTURE_TIMEOUT_MINUTES; |
| 141 | log.info("Pending future timeout is not configured, " + |
| 142 | "using current value of {}", pendingFutureTimeoutMinutes); |
| 143 | } else { |
| 144 | pendingFutureTimeoutMinutes = newPendingFutureTimeoutMinutes; |
| 145 | log.info("Configured. Pending future timeout is configured to {}", |
| 146 | pendingFutureTimeoutMinutes); |
| 147 | } |
| 148 | } |
| 149 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Returns the flow table for specified device. |
| 152 | * |
| 153 | * @param deviceId identifier of the device |
| 154 | * @return Map representing Flow Table of given device. |
| 155 | */ |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 156 | private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 157 | return flowEntries.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 160 | private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) { |
| 161 | final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId); |
| 162 | List<StoredFlowEntry> r = flowTable.get(flowId); |
| 163 | if (r == null) { |
| 164 | final List<StoredFlowEntry> concurrentlyAdded; |
| 165 | r = new CopyOnWriteArrayList<>(); |
| 166 | concurrentlyAdded = flowTable.putIfAbsent(flowId, r); |
| 167 | if (concurrentlyAdded != null) { |
| 168 | return concurrentlyAdded; |
| 169 | } |
| 170 | } |
| 171 | return r; |
| 172 | } |
| 173 | |
| 174 | private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) { |
| 175 | List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id()); |
| 176 | for (StoredFlowEntry fe : fes) { |
| 177 | if (fe.equals(rule)) { |
| 178 | return fe; |
| 179 | } |
| 180 | } |
| 181 | return null; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 185 | public FlowEntry getFlowEntry(FlowRule rule) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 186 | return getFlowEntryInternal(rule.deviceId(), rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 191 | // flatten and make iterator unmodifiable |
| 192 | return FluentIterable.from(getFlowTable(deviceId).values()) |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 193 | .transformAndConcat(Collections::unmodifiableList); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 197 | public void storeFlowRule(FlowRule rule) { |
| 198 | storeFlowRuleInternal(rule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 201 | private void storeFlowRuleInternal(FlowRule rule) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 202 | StoredFlowEntry f = new DefaultFlowEntry(rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 203 | final DeviceId did = f.deviceId(); |
| 204 | final FlowId fid = f.id(); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 205 | List<StoredFlowEntry> existing = getFlowEntries(did, fid); |
| 206 | synchronized (existing) { |
| 207 | for (StoredFlowEntry fe : existing) { |
| 208 | if (fe.equals(rule)) { |
| 209 | // was already there? ignore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 210 | return; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | // new flow rule added |
| 214 | existing.add(f); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 219 | public void deleteFlowRule(FlowRule rule) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 220 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 221 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 222 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 223 | synchronized (entries) { |
| 224 | for (StoredFlowEntry entry : entries) { |
| 225 | if (entry.equals(rule)) { |
| 226 | synchronized (entry) { |
| 227 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 231 | } |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 232 | |
| 233 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 234 | //log.warn("Cannot find rule {}", rule); |
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 addOrUpdateFlowRule(FlowEntry rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 239 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 240 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
| 241 | synchronized (entries) { |
| 242 | for (StoredFlowEntry stored : entries) { |
| 243 | if (stored.equals(rule)) { |
| 244 | synchronized (stored) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 245 | //FIXME modification of "stored" flow entry outside of flow table |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 246 | stored.setBytes(rule.bytes()); |
| 247 | stored.setLife(rule.life()); |
| 248 | stored.setPackets(rule.packets()); |
| 249 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 250 | stored.setState(FlowEntryState.ADDED); |
| 251 | // TODO: Do we need to change `rule` state? |
| 252 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 253 | } |
| 254 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 255 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 256 | } |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 257 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 258 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 259 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 260 | // should not reach here |
| 261 | // storeFlowRule was expected to be called |
| 262 | log.error("FlowRule was not found in store {} to update", rule); |
| 263 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 264 | //flowEntries.put(did, rule); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 265 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 266 | } |
| 267 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 268 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 269 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 270 | // 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] | 271 | final DeviceId did = rule.deviceId(); |
| 272 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 273 | List<StoredFlowEntry> entries = getFlowEntries(did, rule.id()); |
| 274 | synchronized (entries) { |
| 275 | if (entries.remove(rule)) { |
| 276 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 277 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 278 | } |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 279 | return null; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 280 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 281 | |
| 282 | @Override |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 283 | public FlowRuleEvent pendingFlowRule(FlowEntry rule) { |
| 284 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
| 285 | synchronized (entries) { |
| 286 | for (StoredFlowEntry entry : entries) { |
| 287 | if (entry.equals(rule) && |
| 288 | entry.state() != FlowEntryState.PENDING_ADD) { |
| 289 | synchronized (entry) { |
| 290 | entry.setState(FlowEntryState.PENDING_ADD); |
| 291 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | return null; |
| 297 | } |
| 298 | |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 299 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 300 | public void purgeFlowRule(DeviceId deviceId) { |
| 301 | flowEntries.remove(deviceId); |
| 302 | } |
| 303 | |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 304 | @Override |
Daniele Moro | 641b4f1 | 2021-07-15 17:02:59 +0200 | [diff] [blame^] | 305 | public boolean purgeFlowRules(DeviceId deviceId, ApplicationId appId) { |
| 306 | flowEntries.get(deviceId).values() |
| 307 | .removeIf(storedFlowEntries -> { |
| 308 | storedFlowEntries.removeIf(storedFlowEntry -> storedFlowEntry.appId() == appId.id()); |
| 309 | return storedFlowEntries.isEmpty(); |
| 310 | }); |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | @Override |
Victor Silva | 139bca4 | 2016-08-18 11:46:35 -0300 | [diff] [blame] | 315 | public void purgeFlowRules() { |
| 316 | flowEntries.clear(); |
| 317 | } |
| 318 | |
| 319 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 320 | public void storeBatch( |
| 321 | FlowRuleBatchOperation operation) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 322 | List<FlowRuleBatchEntry> toAdd = new ArrayList<>(); |
| 323 | List<FlowRuleBatchEntry> toRemove = new ArrayList<>(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 324 | |
| 325 | for (FlowRuleBatchEntry entry : operation.getOperations()) { |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 326 | final FlowRule flowRule = entry.target(); |
| 327 | if (entry.operator().equals(FlowRuleOperation.ADD)) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 328 | if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) { |
| 329 | storeFlowRule(flowRule); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 330 | toAdd.add(entry); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 331 | } |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 332 | } else if (entry.operator().equals(FlowRuleOperation.REMOVE)) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 333 | if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) { |
| 334 | deleteFlowRule(flowRule); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 335 | toRemove.add(entry); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 336 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 337 | } else { |
| 338 | throw new UnsupportedOperationException("Unsupported operation type"); |
| 339 | } |
| 340 | } |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 341 | |
| 342 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 343 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 344 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 345 | new CompletedBatchOperation(true, Collections.emptySet(), |
| 346 | operation.deviceId()))); |
| 347 | return; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 351 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 352 | |
| 353 | pendingFutures.put(batchId, r); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 354 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 355 | toAdd.addAll(toRemove); |
| 356 | notifyDelegate(FlowRuleBatchEvent.requested( |
| 357 | new FlowRuleBatchRequest(batchId, Sets.newHashSet(toAdd)), operation.deviceId())); |
| 358 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 363 | final Long batchId = event.subject().batchId(); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 364 | SettableFuture<CompletedBatchOperation> future |
| 365 | = pendingFutures.getIfPresent(batchId); |
| 366 | if (future != null) { |
| 367 | future.set(event.result()); |
| 368 | pendingFutures.invalidate(batchId); |
| 369 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 370 | notifyDelegate(event); |
| 371 | } |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 372 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 373 | @Override |
| 374 | public FlowRuleEvent updateTableStatistics(DeviceId deviceId, |
| 375 | List<TableStatisticsEntry> tableStats) { |
| 376 | deviceTableStats.put(deviceId, tableStats); |
| 377 | return null; |
| 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId) { |
| 382 | List<TableStatisticsEntry> tableStats = deviceTableStats.get(deviceId); |
| 383 | if (tableStats == null) { |
| 384 | return Collections.emptyList(); |
| 385 | } |
| 386 | return ImmutableList.copyOf(tableStats); |
| 387 | } |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 388 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 389 | @Override |
| 390 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
| 391 | return Streams.stream(getTableStatistics(deviceId)) |
| 392 | .mapToLong(TableStatisticsEntry::activeFlowEntries) |
| 393 | .sum(); |
| 394 | } |
| 395 | |
sangyun-han | c180686 | 2016-04-05 11:42:03 +0900 | [diff] [blame] | 396 | private static final class TimeoutFuture |
| 397 | implements RemovalListener<Integer, SettableFuture<CompletedBatchOperation>> { |
| 398 | @Override |
| 399 | public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) { |
| 400 | // wrapping in ExecutionException to support Future.get |
| 401 | if (notification.wasEvicted()) { |
| 402 | notification.getValue() |
| 403 | .setException(new ExecutionException("Timed out", |
| 404 | new TimeoutException())); |
| 405 | } |
| 406 | } |
| 407 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 408 | } |