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