Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkState; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.Collections; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Optional; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.ConcurrentHashMap; |
| 28 | import java.util.concurrent.CountDownLatch; |
| 29 | import java.util.concurrent.ExecutionException; |
| 30 | import java.util.concurrent.Future; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | import java.util.concurrent.TimeoutException; |
| 33 | import java.util.concurrent.atomic.AtomicBoolean; |
| 34 | import java.util.concurrent.atomic.AtomicLong; |
| 35 | import java.util.stream.Collectors; |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 36 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 37 | import org.apache.felix.scr.annotations.Activate; |
| 38 | import org.apache.felix.scr.annotations.Component; |
| 39 | import org.apache.felix.scr.annotations.Deactivate; |
| 40 | import org.apache.felix.scr.annotations.Reference; |
| 41 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.core.ApplicationId; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.flow.BatchOperation; |
| 45 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 46 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 47 | import org.onosproject.net.flow.FlowEntry; |
| 48 | import org.onosproject.net.flow.FlowRule; |
| 49 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 50 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 51 | import org.onosproject.net.flow.FlowRuleProvider; |
| 52 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 53 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 54 | import org.onosproject.net.provider.AbstractProvider; |
| 55 | import org.onosproject.net.provider.ProviderId; |
| 56 | import org.onosproject.net.topology.TopologyService; |
| 57 | import org.onosproject.openflow.controller.Dpid; |
| 58 | import org.onosproject.openflow.controller.OpenFlowController; |
| 59 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 60 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 61 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 62 | import org.onosproject.openflow.controller.RoleState; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFActionType; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 65 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 69 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.protocol.OFInstructionType; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 72 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame^] | 74 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 75 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 76 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 77 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 78 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadActionErrorMsg; |
| 79 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadInstructionErrorMsg; |
| 80 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadMatchErrorMsg; |
| 81 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg; |
| 82 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 83 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 84 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions; |
| 85 | import org.projectfloodlight.openflow.types.OFPort; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 86 | import org.slf4j.Logger; |
| 87 | |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 88 | import com.google.common.base.MoreObjects; |
| 89 | import com.google.common.collect.ArrayListMultimap; |
| 90 | import com.google.common.collect.Maps; |
| 91 | import com.google.common.collect.Multimap; |
| 92 | import com.google.common.collect.Sets; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 93 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Provider which uses an OpenFlow controller to detect network |
| 96 | * end-station hosts. |
| 97 | */ |
| 98 | @Component(immediate = true) |
| 99 | public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider { |
| 100 | |
Sho SHIMIZU | fce9d4c | 2015-01-20 19:20:13 -0800 | [diff] [blame] | 101 | enum BatchState { STARTED, FINISHED, CANCELLED } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 102 | |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 103 | private static final int LOWEST_PRIORITY = 0; |
| 104 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 105 | private final Logger log = getLogger(getClass()); |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected FlowRuleProviderRegistry providerRegistry; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected OpenFlowController controller; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected TopologyService topologyService; |
| 115 | |
| 116 | private FlowRuleProviderService providerService; |
| 117 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 118 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 119 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 120 | // FIXME: This should be an expiring map to ensure futures that don't have |
| 121 | // a future eventually get garbage collected. |
Sho SHIMIZU | 500c317 | 2015-01-27 11:00:39 -0800 | [diff] [blame] | 122 | private final Map<Long, InstallationFuture> pendingFutures = new ConcurrentHashMap<>(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 123 | |
Sho SHIMIZU | 500c317 | 2015-01-27 11:00:39 -0800 | [diff] [blame] | 124 | private final Map<Long, InstallationFuture> pendingFMs = new ConcurrentHashMap<>(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 125 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 126 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
| 127 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 128 | private final AtomicLong xidCounter = new AtomicLong(1); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 129 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Creates an OpenFlow host provider. |
| 132 | */ |
| 133 | public OpenFlowRuleProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 134 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Activate |
| 138 | public void activate() { |
| 139 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 140 | controller.addListener(listener); |
| 141 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 142 | |
| 143 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 144 | FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL); |
| 145 | fsc.start(); |
| 146 | collectors.put(new Dpid(sw.getId()), fsc); |
| 147 | } |
| 148 | |
| 149 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 150 | log.info("Started"); |
| 151 | } |
| 152 | |
| 153 | @Deactivate |
| 154 | public void deactivate() { |
| 155 | providerRegistry.unregister(this); |
| 156 | providerService = null; |
| 157 | |
| 158 | log.info("Stopped"); |
| 159 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 160 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public void applyFlowRule(FlowRule... flowRules) { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 163 | for (int i = 0; i < flowRules.length; i++) { |
| 164 | applyRule(flowRules[i]); |
| 165 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 166 | } |
| 167 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 168 | private void applyRule(FlowRule flowRule) { |
| 169 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 170 | if (flowRule.type() == FlowRule.Type.DEFAULT) { |
| 171 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 172 | Optional.empty()).buildFlowAdd()); |
| 173 | } else { |
| 174 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 175 | Optional.empty()).buildFlowAdd(), getTableType(flowRule.type())); |
| 176 | } |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 177 | } |
| 178 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 179 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public void removeFlowRule(FlowRule... flowRules) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 182 | for (int i = 0; i < flowRules.length; i++) { |
| 183 | removeRule(flowRules[i]); |
| 184 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 185 | |
| 186 | } |
| 187 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 188 | private void removeRule(FlowRule flowRule) { |
| 189 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 190 | if (flowRule.type() == FlowRule.Type.DEFAULT) { |
| 191 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 192 | Optional.empty()).buildFlowDel()); |
| 193 | } else { |
| 194 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
| 195 | Optional.empty()).buildFlowDel(), getTableType(flowRule.type())); |
| 196 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 197 | } |
| 198 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 201 | // TODO: optimize using the ApplicationId |
| 202 | removeFlowRule(flowRules); |
| 203 | } |
| 204 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 205 | @Override |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 206 | public Future<CompletedBatchOperation> executeBatch(BatchOperation<FlowRuleBatchEntry> batch) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 207 | final Set<Dpid> sws = Sets.newConcurrentHashSet(); |
| 208 | final Map<Long, FlowRuleBatchEntry> fmXids = new HashMap<>(); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 209 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 210 | /* |
| 211 | * Use identity hash map for reference equality as we could have equal |
| 212 | * flow mods for different switches. |
| 213 | */ |
| 214 | Map<OFFlowMod, OpenFlowSwitch> mods = Maps.newIdentityHashMap(); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 215 | Map<OFFlowMod, OpenFlowSwitch.TableType> modTypes = Maps.newIdentityHashMap(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 216 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 217 | FlowRule flowRule = fbe.target(); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 218 | final Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 219 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 220 | if (sw == null) { |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 221 | /* |
| 222 | * if a switch we are supposed to install to is gone then |
| 223 | * cancel (ie. rollback) the work that has been done so far |
| 224 | * and return the associated future. |
| 225 | */ |
| 226 | InstallationFuture failed = new InstallationFuture(sws, fmXids); |
| 227 | failed.cancel(true); |
| 228 | return failed; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 229 | } |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 230 | sws.add(dpid); |
| 231 | final Long flowModXid = xidCounter.getAndIncrement(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 232 | FlowModBuilder builder = |
| 233 | FlowModBuilder.builder(flowRule, sw.factory(), |
| 234 | Optional.of(flowModXid)); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 235 | OFFlowMod mod = null; |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 236 | switch (fbe.operator()) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 237 | case ADD: |
| 238 | mod = builder.buildFlowAdd(); |
| 239 | break; |
| 240 | case REMOVE: |
| 241 | mod = builder.buildFlowDel(); |
| 242 | break; |
| 243 | case MODIFY: |
| 244 | mod = builder.buildFlowMod(); |
| 245 | break; |
| 246 | default: |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 247 | log.error("Unsupported batch operation {}", fbe.operator()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 248 | } |
| 249 | if (mod != null) { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 250 | mods.put(mod, sw); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 251 | modTypes.put(mod, getTableType(flowRule.type())); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 252 | fmXids.put(flowModXid, fbe); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 253 | } else { |
| 254 | log.error("Conversion of flowrule {} failed.", flowRule); |
| 255 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 256 | } |
| 257 | InstallationFuture installation = new InstallationFuture(sws, fmXids); |
| 258 | for (Long xid : fmXids.keySet()) { |
| 259 | pendingFMs.put(xid, installation); |
| 260 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 261 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 262 | pendingFutures.put(installation.xid(), installation); |
| 263 | for (Map.Entry<OFFlowMod, OpenFlowSwitch> entry : mods.entrySet()) { |
| 264 | OpenFlowSwitch sw = entry.getValue(); |
| 265 | OFFlowMod mod = entry.getKey(); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 266 | OpenFlowSwitch.TableType tableType = modTypes.get(mod); |
| 267 | if (tableType == OpenFlowSwitch.TableType.NONE) { |
| 268 | sw.sendMsg(mod); |
| 269 | } else { |
| 270 | sw.sendMsg(mod, tableType); |
| 271 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 272 | } |
| 273 | installation.verify(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 274 | return installation; |
| 275 | } |
| 276 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 277 | private OpenFlowSwitch.TableType getTableType(FlowRule.Type type) { |
| 278 | switch (type) { |
| 279 | case IP: |
| 280 | return OpenFlowSwitch.TableType.IP; |
| 281 | case MPLS: |
| 282 | return OpenFlowSwitch.TableType.MPLS; |
| 283 | case ACL: |
| 284 | return OpenFlowSwitch.TableType.ACL; |
| 285 | default: |
| 286 | return OpenFlowSwitch.TableType.NONE; |
| 287 | } |
| 288 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 289 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 290 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 291 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 292 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 293 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 294 | private final Multimap<DeviceId, FlowEntry> completeEntries = |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 295 | ArrayListMultimap.create(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 296 | |
| 297 | @Override |
| 298 | public void switchAdded(Dpid dpid) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 299 | FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 300 | fsc.start(); |
| 301 | collectors.put(dpid, fsc); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void switchRemoved(Dpid dpid) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 306 | FlowStatsCollector collector = collectors.remove(dpid); |
| 307 | if (collector != null) { |
| 308 | collector.stop(); |
| 309 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 313 | public void switchChanged(Dpid dpid) { |
| 314 | } |
| 315 | |
| 316 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 317 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 318 | //TODO: Decide whether to evict flows internal store. |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void handleMessage(Dpid dpid, OFMessage msg) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 323 | InstallationFuture future = null; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 324 | switch (msg.getType()) { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 325 | case FLOW_REMOVED: |
| 326 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 327 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 328 | FlowEntry fr = new FlowEntryBuilder(dpid, removed).build(); |
| 329 | providerService.flowRemoved(fr); |
| 330 | break; |
| 331 | case STATS_REPLY: |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame^] | 332 | if (((OFStatsReply) msg).getStatsType() != OFStatsType.FLOW) { |
| 333 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg); |
| 334 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 335 | break; |
| 336 | case BARRIER_REPLY: |
| 337 | future = pendingFutures.get(msg.getXid()); |
| 338 | if (future != null) { |
| 339 | future.satisfyRequirement(dpid); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 340 | } else { |
| 341 | log.warn("Received unknown Barrier Reply: {}", msg.getXid()); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 342 | } |
| 343 | break; |
| 344 | case ERROR: |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 345 | log.warn("received Error message {} from {}", msg, dpid); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 346 | future = pendingFMs.get(msg.getXid()); |
| 347 | if (future != null) { |
| 348 | future.fail((OFErrorMsg) msg, dpid); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 349 | } else { |
| 350 | log.warn("Received unknown Error Reply: {} {}", msg.getXid(), msg); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 351 | } |
| 352 | break; |
| 353 | default: |
| 354 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | } |
| 358 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 359 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 360 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 361 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 362 | // Do nothing here for now. |
| 363 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 364 | |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame^] | 365 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 366 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 367 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 368 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 369 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
| 370 | .filter(entry -> !tableMissRule(dpid, entry)) |
| 371 | .map(entry -> new FlowEntryBuilder(dpid, entry).build()) |
| 372 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 373 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 374 | providerService.pushFlowMetrics(did, flowEntries); |
| 375 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 376 | } |
| 377 | |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 378 | private boolean tableMissRule(Dpid dpid, OFFlowStatsEntry reply) { |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 379 | if (reply.getMatch().getMatchFields().iterator().hasNext()) { |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 380 | return false; |
| 381 | } |
Jonathan Hart | 2ffcd10 | 2015-01-16 16:47:50 -0800 | [diff] [blame] | 382 | if (reply.getVersion().equals(OFVersion.OF_10)) { |
| 383 | return reply.getPriority() == LOWEST_PRIORITY |
| 384 | && reply.getActions().isEmpty(); |
| 385 | } |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 386 | for (OFInstruction ins : reply.getInstructions()) { |
| 387 | if (ins.getType() == OFInstructionType.APPLY_ACTIONS) { |
| 388 | OFInstructionApplyActions apply = (OFInstructionApplyActions) ins; |
| 389 | List<OFAction> acts = apply.getActions(); |
| 390 | for (OFAction act : acts) { |
| 391 | if (act.getType() == OFActionType.OUTPUT) { |
| 392 | OFActionOutput out = (OFActionOutput) act; |
| 393 | if (out.getPort() == OFPort.CONTROLLER) { |
| 394 | return true; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | return false; |
| 401 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 402 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 403 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 404 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 405 | private class InstallationFuture implements Future<CompletedBatchOperation> { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 406 | |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 407 | // barrier xid |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 408 | private final Long xid; |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 409 | // waiting for barrier reply from... |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 410 | private final Set<Dpid> sws; |
| 411 | private final AtomicBoolean ok = new AtomicBoolean(true); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 412 | // FlowMod xid -> |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 413 | private final Map<Long, FlowRuleBatchEntry> fms; |
| 414 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 415 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 416 | private final Set<FlowEntry> offendingFlowMods = Sets.newHashSet(); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 417 | // Failed batch operation id |
Sho SHIMIZU | c0e8bfa | 2015-01-22 16:15:08 -0800 | [diff] [blame] | 418 | private Long failedId; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 419 | |
| 420 | private final CountDownLatch countDownLatch; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 421 | private BatchState state; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 422 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 423 | public InstallationFuture(Set<Dpid> sws, Map<Long, FlowRuleBatchEntry> fmXids) { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 424 | this.xid = xidCounter.getAndIncrement(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 425 | this.state = BatchState.STARTED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 426 | this.sws = sws; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 427 | this.fms = fmXids; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 428 | countDownLatch = new CountDownLatch(sws.size()); |
| 429 | } |
| 430 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 431 | public Long xid() { |
| 432 | return xid; |
| 433 | } |
| 434 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 435 | public void fail(OFErrorMsg msg, Dpid dpid) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 436 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 437 | ok.set(false); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 438 | FlowEntry fe = null; |
| 439 | FlowRuleBatchEntry fbe = fms.get(msg.getXid()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 440 | failedId = fbe.id(); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 441 | FlowRule offending = fbe.target(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 442 | //TODO handle specific error msgs |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 443 | switch (msg.getErrType()) { |
| 444 | case BAD_ACTION: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 445 | OFBadActionErrorMsg bad = (OFBadActionErrorMsg) msg; |
| 446 | fe = new DefaultFlowEntry(offending, bad.getErrType().ordinal(), |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 447 | bad.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 448 | break; |
| 449 | case BAD_INSTRUCTION: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 450 | OFBadInstructionErrorMsg badins = (OFBadInstructionErrorMsg) msg; |
| 451 | fe = new DefaultFlowEntry(offending, badins.getErrType().ordinal(), |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 452 | badins.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 453 | break; |
| 454 | case BAD_MATCH: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 455 | OFBadMatchErrorMsg badMatch = (OFBadMatchErrorMsg) msg; |
| 456 | fe = new DefaultFlowEntry(offending, badMatch.getErrType().ordinal(), |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 457 | badMatch.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 458 | break; |
| 459 | case BAD_REQUEST: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 460 | OFBadRequestErrorMsg badReq = (OFBadRequestErrorMsg) msg; |
| 461 | fe = new DefaultFlowEntry(offending, badReq.getErrType().ordinal(), |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 462 | badReq.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 463 | break; |
| 464 | case FLOW_MOD_FAILED: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 465 | OFFlowModFailedErrorMsg fmFail = (OFFlowModFailedErrorMsg) msg; |
| 466 | fe = new DefaultFlowEntry(offending, fmFail.getErrType().ordinal(), |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 467 | fmFail.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 468 | break; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 469 | case EXPERIMENTER: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 470 | case GROUP_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 471 | case HELLO_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 472 | case METER_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 473 | case PORT_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 474 | case QUEUE_OP_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 475 | case ROLE_REQUEST_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 476 | case SWITCH_CONFIG_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 477 | case TABLE_FEATURES_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 478 | case TABLE_MOD_FAILED: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 479 | fe = new DefaultFlowEntry(offending, msg.getErrType().ordinal(), 0); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 480 | break; |
| 481 | default: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 482 | log.error("Unknown error type {}", msg.getErrType()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 483 | |
| 484 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 485 | offendingFlowMods.add(fe); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 486 | |
Yuta HIGUCHI | 804a4bb | 2014-11-27 09:37:34 -0800 | [diff] [blame] | 487 | removeRequirement(dpid); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 488 | } |
| 489 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 490 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 491 | public void satisfyRequirement(Dpid dpid) { |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 492 | log.debug("Satisfaction from switch {}", dpid); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 493 | removeRequirement(dpid); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 494 | } |
| 495 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 496 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 497 | public void verify() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 498 | checkState(!sws.isEmpty()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 499 | for (Dpid dpid : sws) { |
| 500 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 501 | OFBarrierRequest.Builder builder = sw.factory() |
| 502 | .buildBarrierRequest() |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 503 | .setXid(xid); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 504 | sw.sendMsg(builder.build()); |
| 505 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public boolean cancel(boolean mayInterruptIfRunning) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 510 | if (isDone()) { |
| 511 | return false; |
| 512 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 513 | ok.set(false); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 514 | this.state = BatchState.CANCELLED; |
| 515 | cleanUp(); |
| 516 | for (FlowRuleBatchEntry fbe : fms.values()) { |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 517 | if (fbe.operator() == FlowRuleOperation.ADD || |
| 518 | fbe.operator() == FlowRuleOperation.MODIFY) { |
| 519 | removeFlowRule(fbe.target()); |
| 520 | } else if (fbe.operator() == FlowRuleOperation.REMOVE) { |
| 521 | applyRule(fbe.target()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 525 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 530 | return this.state == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | @Override |
| 534 | public boolean isDone() { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 535 | return this.state == BatchState.FINISHED || isCancelled(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 539 | public CompletedBatchOperation get() throws InterruptedException, ExecutionException { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 540 | countDownLatch.await(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 541 | this.state = BatchState.FINISHED; |
Sho SHIMIZU | c0e8bfa | 2015-01-22 16:15:08 -0800 | [diff] [blame] | 542 | Set<Long> failedIds = (failedId != null) ? Sets.newHashSet(failedId) : Collections.emptySet(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 543 | CompletedBatchOperation result = |
| 544 | new CompletedBatchOperation(ok.get(), offendingFlowMods, failedIds); |
| 545 | //FIXME do cleanup here (moved by BOC) |
| 546 | cleanUp(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 547 | return result; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 551 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 552 | throws InterruptedException, ExecutionException, |
| 553 | TimeoutException { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 554 | if (countDownLatch.await(timeout, unit)) { |
| 555 | this.state = BatchState.FINISHED; |
Sho SHIMIZU | c0e8bfa | 2015-01-22 16:15:08 -0800 | [diff] [blame] | 556 | Set<Long> failedIds = (failedId != null) ? Sets.newHashSet(failedId) : Collections.emptySet(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 557 | CompletedBatchOperation result = |
| 558 | new CompletedBatchOperation(ok.get(), offendingFlowMods, failedIds); |
| 559 | // FIXME do cleanup here (moved by BOC) |
| 560 | cleanUp(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 561 | return result; |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 562 | } |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 563 | throw new TimeoutException(this.toString()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | private void cleanUp() { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 567 | if (isDone() || isCancelled()) { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 568 | pendingFutures.remove(xid); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 569 | for (Long xid : fms.keySet()) { |
| 570 | pendingFMs.remove(xid); |
| 571 | } |
| 572 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 573 | } |
| 574 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 575 | private void removeRequirement(Dpid dpid) { |
| 576 | countDownLatch.countDown(); |
| 577 | sws.remove(dpid); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 578 | //FIXME don't do cleanup here (moved by BOC) |
| 579 | //cleanUp(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 580 | } |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 581 | |
| 582 | @Override |
| 583 | public String toString() { |
| 584 | return MoreObjects.toStringHelper(getClass()) |
| 585 | .add("xid", xid) |
| 586 | .add("pending devices", sws) |
| 587 | .add("devices in batch", |
| 588 | fms.values().stream() |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 589 | .map((fbe) -> fbe.target().deviceId()) |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 590 | .distinct().collect(Collectors.toList())) |
| 591 | .add("failedId", failedId) |
| 592 | .add("latchCount", countDownLatch.getCount()) |
| 593 | .add("state", state) |
| 594 | .add("no error?", ok.get()) |
| 595 | .toString(); |
| 596 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 597 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 598 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 599 | } |