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; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 21 | import com.google.common.collect.FluentIterable; |
| 22 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 23 | import com.google.common.util.concurrent.SettableFuture; |
| 24 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Service; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.DeviceId; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 32 | import org.onlab.onos.net.flow.FlowEntry; |
| 33 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.FlowId; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.FlowRule; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 37 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 38 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
| 39 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 40 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.FlowRuleEvent; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.flow.StoredFlowEntry; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 46 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 47 | import org.onlab.util.NewConcurrentHashMap; |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 49 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 50 | import java.util.ArrayList; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 51 | import java.util.Collections; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 52 | import java.util.List; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 53 | import java.util.concurrent.ConcurrentHashMap; |
| 54 | import java.util.concurrent.ConcurrentMap; |
| 55 | import java.util.concurrent.CopyOnWriteArrayList; |
| 56 | import java.util.concurrent.Future; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 57 | import java.util.concurrent.TimeUnit; |
| 58 | import java.util.concurrent.atomic.AtomicInteger; |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 59 | |
| 60 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 61 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 63 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Manages inventory of flow rules using trivial in-memory implementation. |
| 66 | */ |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 67 | @Component(immediate = true) |
| 68 | @Service |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 69 | public class SimpleFlowRuleStore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 70 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 71 | implements FlowRuleStore { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 72 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 73 | private final Logger log = getLogger(getClass()); |
| 74 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 75 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 76 | // inner Map is Device flow table |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 77 | // inner Map value (FlowId synonym list) must be synchronized before modifying |
| 78 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>> |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 79 | flowEntries = new ConcurrentHashMap<>(); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 80 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 81 | private final AtomicInteger localBatchIdGen = new AtomicInteger(); |
| 82 | |
| 83 | // TODO: make this configurable |
| 84 | private int pendingFutureTimeoutMinutes = 5; |
| 85 | |
| 86 | private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures = |
| 87 | CacheBuilder.newBuilder() |
| 88 | .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES) |
| 89 | // TODO Explicitly fail the future if expired? |
| 90 | //.removalListener(listener) |
| 91 | .build(); |
| 92 | |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 93 | @Activate |
| 94 | public void activate() { |
| 95 | log.info("Started"); |
| 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 100 | flowEntries.clear(); |
tom | 85c612b | 2014-09-19 19:25:47 -0700 | [diff] [blame] | 101 | log.info("Stopped"); |
| 102 | } |
| 103 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 104 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 105 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 106 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 107 | int sum = 0; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 108 | for (ConcurrentMap<FlowId, List<StoredFlowEntry>> ft : flowEntries.values()) { |
| 109 | for (List<StoredFlowEntry> fes : ft.values()) { |
| 110 | sum += fes.size(); |
| 111 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 112 | } |
| 113 | return sum; |
| 114 | } |
| 115 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 116 | private static NewConcurrentHashMap<FlowId, List<StoredFlowEntry>> lazyEmptyFlowTable() { |
| 117 | return NewConcurrentHashMap.<FlowId, List<StoredFlowEntry>>ifNeeded(); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the flow table for specified device. |
| 122 | * |
| 123 | * @param deviceId identifier of the device |
| 124 | * @return Map representing Flow Table of given device. |
| 125 | */ |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 126 | private ConcurrentMap<FlowId, List<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 127 | return createIfAbsentUnchecked(flowEntries, |
| 128 | deviceId, lazyEmptyFlowTable()); |
| 129 | } |
| 130 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 131 | private List<StoredFlowEntry> getFlowEntries(DeviceId deviceId, FlowId flowId) { |
| 132 | final ConcurrentMap<FlowId, List<StoredFlowEntry>> flowTable = getFlowTable(deviceId); |
| 133 | List<StoredFlowEntry> r = flowTable.get(flowId); |
| 134 | if (r == null) { |
| 135 | final List<StoredFlowEntry> concurrentlyAdded; |
| 136 | r = new CopyOnWriteArrayList<>(); |
| 137 | concurrentlyAdded = flowTable.putIfAbsent(flowId, r); |
| 138 | if (concurrentlyAdded != null) { |
| 139 | return concurrentlyAdded; |
| 140 | } |
| 141 | } |
| 142 | return r; |
| 143 | } |
| 144 | |
| 145 | private FlowEntry getFlowEntryInternal(DeviceId deviceId, FlowRule rule) { |
| 146 | List<StoredFlowEntry> fes = getFlowEntries(deviceId, rule.id()); |
| 147 | for (StoredFlowEntry fe : fes) { |
| 148 | if (fe.equals(rule)) { |
| 149 | return fe; |
| 150 | } |
| 151 | } |
| 152 | return null; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 156 | public FlowEntry getFlowEntry(FlowRule rule) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 157 | return getFlowEntryInternal(rule.deviceId(), rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 162 | // flatten and make iterator unmodifiable |
| 163 | return FluentIterable.from(getFlowTable(deviceId).values()) |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 164 | .transformAndConcat( |
| 165 | new Function<List<StoredFlowEntry>, Iterable<? extends FlowEntry>>() { |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 166 | |
Thomas Vachuska | 8ac922d | 2014-10-23 16:17:03 -0700 | [diff] [blame] | 167 | @Override |
| 168 | public Iterable<? extends FlowEntry> apply( |
| 169 | List<StoredFlowEntry> input) { |
| 170 | return Collections.unmodifiableList(input); |
| 171 | } |
| 172 | }); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 176 | public void storeFlowRule(FlowRule rule) { |
| 177 | storeFlowRuleInternal(rule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 180 | private void storeFlowRuleInternal(FlowRule rule) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 181 | StoredFlowEntry f = new DefaultFlowEntry(rule); |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 182 | final DeviceId did = f.deviceId(); |
| 183 | final FlowId fid = f.id(); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 184 | List<StoredFlowEntry> existing = getFlowEntries(did, fid); |
| 185 | synchronized (existing) { |
| 186 | for (StoredFlowEntry fe : existing) { |
| 187 | if (fe.equals(rule)) { |
| 188 | // was already there? ignore |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 189 | return; |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | // new flow rule added |
| 193 | existing.add(f); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 198 | public void deleteFlowRule(FlowRule rule) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 199 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 200 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 201 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 202 | synchronized (entries) { |
| 203 | for (StoredFlowEntry entry : entries) { |
| 204 | if (entry.equals(rule)) { |
| 205 | synchronized (entry) { |
| 206 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 210 | } |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 211 | |
| 212 | |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 213 | //log.warn("Cannot find rule {}", rule); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 214 | } |
| 215 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 216 | @Override |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 217 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 218 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | 94ffdd4 | 2014-10-20 16:36:52 -0700 | [diff] [blame] | 219 | List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id()); |
| 220 | synchronized (entries) { |
| 221 | for (StoredFlowEntry stored : entries) { |
| 222 | if (stored.equals(rule)) { |
| 223 | synchronized (stored) { |
| 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 |
| 261 | public Future<CompletedBatchOperation> storeBatch( |
| 262 | FlowRuleBatchOperation batchOperation) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 263 | List<FlowRule> toAdd = new ArrayList<>(); |
| 264 | List<FlowRule> toRemove = new ArrayList<>(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 265 | for (FlowRuleBatchEntry entry : batchOperation.getOperations()) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 266 | final FlowRule flowRule = entry.getTarget(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 267 | if (entry.getOperator().equals(FlowRuleOperation.ADD)) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 268 | if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) { |
| 269 | storeFlowRule(flowRule); |
| 270 | toAdd.add(flowRule); |
| 271 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 272 | } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 273 | if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) { |
| 274 | deleteFlowRule(flowRule); |
| 275 | toRemove.add(flowRule); |
| 276 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 277 | } else { |
| 278 | throw new UnsupportedOperationException("Unsupported operation type"); |
| 279 | } |
| 280 | } |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 281 | |
| 282 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
| 283 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowRule>emptySet())); |
| 284 | } |
| 285 | |
| 286 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 287 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 288 | |
| 289 | pendingFutures.put(batchId, r); |
| 290 | notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove))); |
| 291 | |
| 292 | return r; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame^] | 297 | final Integer batchId = event.subject().batchId(); |
| 298 | SettableFuture<CompletedBatchOperation> future |
| 299 | = pendingFutures.getIfPresent(batchId); |
| 300 | if (future != null) { |
| 301 | future.set(event.result()); |
| 302 | pendingFutures.invalidate(batchId); |
| 303 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 304 | notifyDelegate(event); |
| 305 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 306 | } |