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