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