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