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