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