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