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