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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.collect.Sets; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.core.IdGenerator; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.event.AbstractListenerRegistry; |
| 34 | import org.onosproject.event.EventDeliveryService; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.device.DeviceService; |
| 38 | import org.onosproject.net.flow.CompletedBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowEntry; |
| 40 | import org.onosproject.net.flow.FlowRule; |
| 41 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 43 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 44 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 45 | import org.onosproject.net.flow.FlowRuleEvent; |
| 46 | import org.onosproject.net.flow.FlowRuleListener; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.FlowRuleOperation; |
| 48 | import org.onosproject.net.flow.FlowRuleOperations; |
| 49 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRuleProvider; |
| 51 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 52 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 53 | import org.onosproject.net.flow.FlowRuleService; |
| 54 | import org.onosproject.net.flow.FlowRuleStore; |
| 55 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 56 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 57 | import org.onosproject.net.provider.AbstractProviderService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 60 | import java.util.Collections; |
| 61 | import java.util.List; |
| 62 | import java.util.Map; |
| 63 | import java.util.Set; |
| 64 | import java.util.concurrent.ConcurrentHashMap; |
| 65 | import java.util.concurrent.ExecutorService; |
| 66 | import java.util.concurrent.Executors; |
| 67 | import java.util.concurrent.atomic.AtomicBoolean; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 68 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 69 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 70 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 71 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 72 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Provides implementation of the flow NB & SB APIs. |
| 75 | */ |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 76 | @Component(immediate = true, enabled = true) |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 77 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 78 | public class FlowRuleManager |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 79 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 80 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 81 | |
Sho SHIMIZU | 183b12fd | 2015-01-20 14:56:39 -0800 | [diff] [blame] | 82 | enum BatchState { STARTED, FINISHED, CANCELLED } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 83 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 84 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 85 | private final Logger log = getLogger(getClass()); |
| 86 | |
| 87 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 88 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 89 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 90 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 91 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 92 | protected ExecutorService deviceInstallers = |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 93 | Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d")); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 94 | |
| 95 | protected ExecutorService operationsService = |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 96 | Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d")); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 97 | |
| 98 | private IdGenerator idGenerator; |
| 99 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 100 | private Map<Long, FlowOperationsProcessor> pendingFlowOperations |
| 101 | = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 102 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 105 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 107 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 110 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 111 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected CoreService coreService; |
| 114 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 115 | @Activate |
| 116 | public void activate() { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 117 | |
| 118 | idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC); |
| 119 | |
| 120 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 121 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 122 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 123 | log.info("Started"); |
| 124 | } |
| 125 | |
| 126 | @Deactivate |
| 127 | public void deactivate() { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 128 | deviceInstallers.shutdownNow(); |
| 129 | operationsService.shutdownNow(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 130 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 131 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 132 | log.info("Stopped"); |
| 133 | } |
| 134 | |
| 135 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 136 | public int getFlowRuleCount() { |
| 137 | return store.getFlowRuleCount(); |
| 138 | } |
| 139 | |
| 140 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 141 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 142 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 146 | public void applyFlowRules(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 147 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 148 | for (int i = 0; i < flowRules.length; i++) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 149 | builder.add(flowRules[i]); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 150 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 151 | apply(builder.build()); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void removeFlowRules(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 156 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 157 | for (int i = 0; i < flowRules.length; i++) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 158 | builder.remove(flowRules[i]); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 159 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 160 | apply(builder.build()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 161 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 162 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public void removeFlowRulesById(ApplicationId id) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 165 | removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 170 | Set<FlowRule> flowEntries = Sets.newHashSet(); |
| 171 | for (Device d : deviceService.getDevices()) { |
| 172 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 173 | if (flowEntry.appId() == id.id()) { |
| 174 | flowEntries.add(flowEntry); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return flowEntries; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 182 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
| 183 | Set<FlowRule> matches = Sets.newHashSet(); |
| 184 | long toLookUp = ((long) appId.id() << 16) | groupId; |
| 185 | for (Device d : deviceService.getDevices()) { |
| 186 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 187 | if ((flowEntry.id().value() >>> 32) == toLookUp) { |
| 188 | matches.add(flowEntry); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | return matches; |
| 193 | } |
| 194 | |
| 195 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 196 | public void apply(FlowRuleOperations ops) { |
| 197 | operationsService.submit(new FlowOperationsProcessor(ops)); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 201 | public void addListener(FlowRuleListener listener) { |
| 202 | listenerRegistry.addListener(listener); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void removeListener(FlowRuleListener listener) { |
| 207 | listenerRegistry.removeListener(listener); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | protected FlowRuleProviderService createProviderService( |
| 212 | FlowRuleProvider provider) { |
| 213 | return new InternalFlowRuleProviderService(provider); |
| 214 | } |
| 215 | |
| 216 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 217 | extends AbstractProviderService<FlowRuleProvider> |
| 218 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 219 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 220 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 221 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 222 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 223 | super(provider); |
| 224 | } |
| 225 | |
| 226 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 227 | public void flowRemoved(FlowEntry flowEntry) { |
| 228 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 229 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 230 | lastSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 231 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 232 | if (stored == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 233 | log.debug("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 236 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 237 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 238 | FlowRuleEvent event = null; |
| 239 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 240 | case ADDED: |
| 241 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 242 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 243 | break; |
| 244 | case PENDING_REMOVE: |
| 245 | case REMOVED: |
| 246 | event = store.removeFlowRule(stored); |
| 247 | break; |
| 248 | default: |
| 249 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 250 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 251 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 252 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 253 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 254 | post(event); |
| 255 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 258 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 259 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 260 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 261 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 262 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 263 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 264 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 265 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 266 | case PENDING_REMOVE: |
| 267 | case REMOVED: |
| 268 | event = store.removeFlowRule(flowRule); |
| 269 | frp.removeFlowRule(flowRule); |
| 270 | break; |
| 271 | case ADDED: |
| 272 | case PENDING_ADD: |
| 273 | frp.applyFlowRule(flowRule); |
| 274 | break; |
| 275 | default: |
| 276 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 277 | } |
| 278 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 279 | if (event != null) { |
| 280 | log.debug("Flow {} removed", flowRule); |
| 281 | post(event); |
| 282 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 283 | |
| 284 | } |
| 285 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 286 | |
| 287 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 288 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 289 | checkValidity(); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 290 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 291 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 292 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 293 | } |
| 294 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 295 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 296 | private void flowAdded(FlowEntry flowEntry) { |
| 297 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 298 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 299 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 300 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 301 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 302 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 303 | if (event == null) { |
| 304 | log.debug("No flow store event generated."); |
| 305 | } else { |
Jonathan Hart | 58682dd | 2014-11-24 20:11:16 -0800 | [diff] [blame] | 306 | log.trace("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 307 | post(event); |
| 308 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 309 | } else { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 310 | log.debug("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 311 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 312 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 313 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 314 | } |
| 315 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 316 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 317 | if (storedRule == null) { |
| 318 | return false; |
| 319 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 320 | if (storedRule.isPermanent()) { |
| 321 | return true; |
| 322 | } |
| 323 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 324 | final long timeout = storedRule.timeout() * 1000; |
| 325 | final long currentTime = System.currentTimeMillis(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 326 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 327 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 328 | return true; |
| 329 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 330 | if (!lastSeen.containsKey(storedRule)) { |
| 331 | // checking for the first time |
| 332 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 333 | // Use following if lastSeen attr. was removed. |
| 334 | //lastSeen.put(storedRule, currentTime); |
| 335 | } |
| 336 | Long last = lastSeen.get(storedRule); |
| 337 | if (last == null) { |
| 338 | // concurrently removed? let the liveness check fail |
| 339 | return false; |
| 340 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 341 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 342 | if ((currentTime - last) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 348 | // Posts the specified event to the local event dispatcher. |
| 349 | private void post(FlowRuleEvent event) { |
| 350 | if (event != null) { |
| 351 | eventDispatcher.post(event); |
| 352 | } |
| 353 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 354 | |
| 355 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 356 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 357 | Set<FlowEntry> storedRules = Sets.newHashSet(store.getFlowEntries(deviceId)); |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame^] | 358 | for (FlowEntry rule : flowEntries) { |
| 359 | try { |
| 360 | if (storedRules.remove(rule)) { |
| 361 | // we both have the rule, let's update some info then. |
| 362 | flowAdded(rule); |
| 363 | } else { |
| 364 | // the device has a rule the store does not have |
| 365 | extraneousFlow(rule); |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 366 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame^] | 367 | } catch (Throwable e) { |
| 368 | log.debug("Can't process added or extra rule {}", e.getMessage()); |
| 369 | continue; |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 370 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame^] | 371 | } |
| 372 | for (FlowEntry rule : storedRules) { |
| 373 | try { |
| 374 | // there are rules in the store that aren't on the switch |
| 375 | flowMissing(rule); |
| 376 | } catch (Throwable e) { |
| 377 | log.debug("Can't add missing flow rule {}", e.getMessage()); |
| 378 | continue; |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 379 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame^] | 380 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 381 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 382 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 383 | |
| 384 | @Override |
| 385 | public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) { |
| 386 | store.batchOperationComplete(FlowRuleBatchEvent.completed( |
| 387 | new FlowRuleBatchRequest(batchId, Collections.emptySet()), |
| 388 | operation |
| 389 | )); |
| 390 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 391 | } |
| 392 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 393 | // Store delegate to re-post events emitted from the store. |
| 394 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 395 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 396 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 397 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 398 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 399 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 400 | public void notify(FlowRuleBatchEvent event) { |
| 401 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 402 | switch (event.type()) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 403 | case BATCH_OPERATION_REQUESTED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 404 | // Request has been forwarded to MASTER Node, and was |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 405 | request.ops().stream().forEach( |
| 406 | op -> { |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 407 | switch (op.operator()) { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 408 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 409 | case ADD: |
| 410 | eventDispatcher.post( |
| 411 | new FlowRuleEvent( |
| 412 | FlowRuleEvent.Type.RULE_ADD_REQUESTED, |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 413 | op.target())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 414 | break; |
| 415 | case REMOVE: |
| 416 | eventDispatcher.post( |
| 417 | new FlowRuleEvent( |
| 418 | FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 419 | op.target())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 420 | break; |
| 421 | case MODIFY: |
| 422 | //TODO: do something here when the time comes. |
| 423 | break; |
| 424 | default: |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 425 | log.warn("Unknown flow operation operator: {}", op.operator()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | ); |
| 429 | |
| 430 | DeviceId deviceId = event.deviceId(); |
| 431 | |
| 432 | FlowRuleBatchOperation batchOperation = |
| 433 | request.asBatchOperation(deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 434 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 435 | FlowRuleProvider flowRuleProvider = |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 436 | getProvider(deviceId); |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 437 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 438 | flowRuleProvider.executeBatch(batchOperation); |
| 439 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 440 | break; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 441 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 442 | case BATCH_OPERATION_COMPLETED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 443 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 444 | FlowOperationsProcessor fops = pendingFlowOperations.remove( |
| 445 | event.subject().batchId()); |
| 446 | if (event.result().isSuccess()) { |
| 447 | if (fops != null) { |
| 448 | fops.satisfy(event.deviceId()); |
| 449 | } |
| 450 | } else { |
| 451 | fops.fail(event.deviceId(), event.result().failedItems()); |
| 452 | } |
| 453 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 454 | break; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 455 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 456 | default: |
| 457 | break; |
| 458 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 461 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 462 | private class FlowOperationsProcessor implements Runnable { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 463 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 464 | private final List<Set<FlowRuleOperation>> stages; |
| 465 | private final FlowRuleOperationsContext context; |
| 466 | private final FlowRuleOperations fops; |
| 467 | private final AtomicBoolean hasFailed = new AtomicBoolean(false); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 468 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 469 | private Set<DeviceId> pendingDevices; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 470 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 471 | public FlowOperationsProcessor(FlowRuleOperations ops) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 472 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 473 | this.stages = Lists.newArrayList(ops.stages()); |
| 474 | this.context = ops.callback(); |
| 475 | this.fops = ops; |
| 476 | pendingDevices = Sets.newConcurrentHashSet(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 477 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 478 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 482 | public void run() { |
| 483 | if (stages.size() > 0) { |
| 484 | process(stages.remove(0)); |
| 485 | } else if (!hasFailed.get() && context != null) { |
| 486 | context.onSuccess(fops); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 490 | private void process(Set<FlowRuleOperation> ops) { |
| 491 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = |
| 492 | ArrayListMultimap.create(); |
| 493 | |
| 494 | FlowRuleBatchEntry fbe; |
| 495 | for (FlowRuleOperation flowRuleOperation : ops) { |
| 496 | switch (flowRuleOperation.type()) { |
| 497 | // FIXME: Brian needs imagination when creating class names. |
| 498 | case ADD: |
| 499 | fbe = new FlowRuleBatchEntry( |
| 500 | FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule()); |
| 501 | break; |
| 502 | case MODIFY: |
| 503 | fbe = new FlowRuleBatchEntry( |
| 504 | FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule()); |
| 505 | break; |
| 506 | case REMOVE: |
| 507 | fbe = new FlowRuleBatchEntry( |
| 508 | FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule()); |
| 509 | break; |
| 510 | default: |
| 511 | throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type()); |
| 512 | } |
| 513 | pendingDevices.add(flowRuleOperation.rule().deviceId()); |
| 514 | perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
| 519 | Long id = idGenerator.getNewId(); |
| 520 | final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId), |
| 521 | deviceId, id); |
| 522 | pendingFlowOperations.put(id, this); |
| 523 | deviceInstallers.submit(new Runnable() { |
| 524 | @Override |
| 525 | public void run() { |
| 526 | store.storeBatch(b); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 527 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 528 | }); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 532 | public void satisfy(DeviceId devId) { |
| 533 | pendingDevices.remove(devId); |
| 534 | if (pendingDevices.isEmpty()) { |
| 535 | operationsService.submit(this); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 536 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 537 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 538 | |
| 539 | |
| 540 | |
| 541 | public void fail(DeviceId devId, Set<? extends FlowRule> failures) { |
| 542 | hasFailed.set(true); |
| 543 | pendingDevices.remove(devId); |
| 544 | if (pendingDevices.isEmpty()) { |
| 545 | operationsService.submit(this); |
| 546 | } |
| 547 | |
| 548 | if (context != null) { |
| 549 | final FlowRuleOperations.Builder failedOpsBuilder = |
| 550 | FlowRuleOperations.builder(); |
| 551 | failures.stream().forEach(failedOpsBuilder::add); |
| 552 | |
| 553 | context.onError(failedOpsBuilder.build()); |
| 554 | } |
| 555 | } |
| 556 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 557 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 558 | } |