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