blob: 214bde3b9f48a7935a5b8c70a46578af812a0aa5 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabib1cc04f72014-09-16 16:09:58 -070016package org.onlab.onos.provider.of.flow.impl;
17
Thomas Vachuska9b2da212014-11-10 19:30:25 -080018import com.google.common.collect.ArrayListMultimap;
19import com.google.common.collect.Maps;
20import com.google.common.collect.Multimap;
21import com.google.common.collect.Sets;
22import com.google.common.util.concurrent.ExecutionList;
alshabib1cc04f72014-09-16 16:09:58 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070028import org.onlab.onos.core.ApplicationId;
alshabiba7f7ca82014-09-22 11:41:23 -070029import org.onlab.onos.net.DeviceId;
Jonathan Hart86e59352014-10-22 10:42:16 -070030import org.onlab.onos.net.flow.BatchOperation;
alshabib193525b2014-10-08 18:58:03 -070031import org.onlab.onos.net.flow.CompletedBatchOperation;
32import org.onlab.onos.net.flow.DefaultFlowEntry;
alshabib1c319ff2014-10-04 20:29:09 -070033import org.onlab.onos.net.flow.FlowEntry;
alshabib1cc04f72014-09-16 16:09:58 -070034import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070035import org.onlab.onos.net.flow.FlowRuleBatchEntry;
alshabib193525b2014-10-08 18:58:03 -070036import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
alshabib1cc04f72014-09-16 16:09:58 -070037import org.onlab.onos.net.flow.FlowRuleProvider;
38import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
39import org.onlab.onos.net.flow.FlowRuleProviderService;
40import org.onlab.onos.net.provider.AbstractProvider;
41import org.onlab.onos.net.provider.ProviderId;
42import org.onlab.onos.net.topology.TopologyService;
tom9c94c5b2014-09-17 13:14:42 -070043import org.onlab.onos.openflow.controller.Dpid;
44import org.onlab.onos.openflow.controller.OpenFlowController;
alshabibeec3a062014-09-17 18:01:26 -070045import org.onlab.onos.openflow.controller.OpenFlowEventListener;
tom9c94c5b2014-09-17 13:14:42 -070046import org.onlab.onos.openflow.controller.OpenFlowSwitch;
alshabibce4e5782014-09-17 14:56:42 -070047import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -070048import org.onlab.onos.openflow.controller.RoleState;
alshabib19fdc122014-10-03 11:38:19 -070049import org.projectfloodlight.openflow.protocol.OFActionType;
alshabib902d41b2014-10-07 16:52:05 -070050import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
51import org.projectfloodlight.openflow.protocol.OFErrorMsg;
alshabib193525b2014-10-08 18:58:03 -070052import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070053import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070054import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
55import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib19fdc122014-10-03 11:38:19 -070056import org.projectfloodlight.openflow.protocol.OFInstructionType;
alshabib8f1cf4a2014-09-17 14:44:48 -070057import org.projectfloodlight.openflow.protocol.OFMessage;
58import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070059import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib54ce5892014-09-23 17:50:51 -070060import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
alshabib5c370ff2014-09-18 10:12:14 -070061import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib19fdc122014-10-03 11:38:19 -070062import org.projectfloodlight.openflow.protocol.OFVersion;
63import org.projectfloodlight.openflow.protocol.action.OFAction;
64import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
alshabib193525b2014-10-08 18:58:03 -070065import org.projectfloodlight.openflow.protocol.errormsg.OFBadActionErrorMsg;
66import org.projectfloodlight.openflow.protocol.errormsg.OFBadInstructionErrorMsg;
67import org.projectfloodlight.openflow.protocol.errormsg.OFBadMatchErrorMsg;
68import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg;
69import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib19fdc122014-10-03 11:38:19 -070070import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
71import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
72import org.projectfloodlight.openflow.types.OFPort;
alshabib1cc04f72014-09-16 16:09:58 -070073import org.slf4j.Logger;
74
Thomas Vachuska9b2da212014-11-10 19:30:25 -080075import java.util.Collections;
76import java.util.HashMap;
77import java.util.List;
78import java.util.Map;
79import java.util.Set;
80import java.util.concurrent.ConcurrentHashMap;
81import java.util.concurrent.CountDownLatch;
82import java.util.concurrent.ExecutionException;
83import java.util.concurrent.Future;
84import java.util.concurrent.TimeUnit;
85import java.util.concurrent.TimeoutException;
86import java.util.concurrent.atomic.AtomicBoolean;
87import java.util.concurrent.atomic.AtomicLong;
88
89import static org.slf4j.LoggerFactory.getLogger;
alshabibeec3a062014-09-17 18:01:26 -070090
alshabib1cc04f72014-09-16 16:09:58 -070091/**
92 * Provider which uses an OpenFlow controller to detect network
93 * end-station hosts.
94 */
95@Component(immediate = true)
96public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
97
alshabib193525b2014-10-08 18:58:03 -070098 enum BatchState { STARTED, FINISHED, CANCELLED };
99
alshabib1cc04f72014-09-16 16:09:58 -0700100 private final Logger log = getLogger(getClass());
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected FlowRuleProviderRegistry providerRegistry;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected OpenFlowController controller;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected TopologyService topologyService;
110
111 private FlowRuleProviderService providerService;
112
alshabibeec3a062014-09-17 18:01:26 -0700113 private final InternalFlowProvider listener = new InternalFlowProvider();
114
Madan Jampani117aaae2014-10-23 10:04:05 -0700115 // FIXME: This should be an expiring map to ensure futures that don't have
116 // a future eventually get garbage collected.
alshabib902d41b2014-10-07 16:52:05 -0700117 private final Map<Long, InstallationFuture> pendingFutures =
118 new ConcurrentHashMap<Long, InstallationFuture>();
119
alshabib193525b2014-10-08 18:58:03 -0700120 private final Map<Long, InstallationFuture> pendingFMs =
121 new ConcurrentHashMap<Long, InstallationFuture>();
122
alshabib3d643ec2014-10-22 18:33:00 -0700123 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
124
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800125 private final AtomicLong xidCounter = new AtomicLong(0);
126
alshabib1cc04f72014-09-16 16:09:58 -0700127 /**
128 * Creates an OpenFlow host provider.
129 */
130 public OpenFlowRuleProvider() {
tom7e02cda2014-09-18 12:05:46 -0700131 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700132 }
133
134 @Activate
135 public void activate() {
136 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700137 controller.addListener(listener);
138 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700139
140 for (OpenFlowSwitch sw : controller.getSwitches()) {
141 FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL);
142 fsc.start();
143 collectors.put(new Dpid(sw.getId()), fsc);
144 }
145
146
alshabib1cc04f72014-09-16 16:09:58 -0700147 log.info("Started");
148 }
149
150 @Deactivate
151 public void deactivate() {
152 providerRegistry.unregister(this);
153 providerService = null;
154
155 log.info("Stopped");
156 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800157
alshabib1cc04f72014-09-16 16:09:58 -0700158 @Override
159 public void applyFlowRule(FlowRule... flowRules) {
alshabib35edb1a2014-09-16 17:44:44 -0700160 for (int i = 0; i < flowRules.length; i++) {
161 applyRule(flowRules[i]);
162 }
alshabib1cc04f72014-09-16 16:09:58 -0700163 }
164
alshabib35edb1a2014-09-16 17:44:44 -0700165 private void applyRule(FlowRule flowRule) {
166 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700167 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory()).buildFlowAdd());
alshabib35edb1a2014-09-16 17:44:44 -0700168 }
169
alshabib35edb1a2014-09-16 17:44:44 -0700170
alshabib1cc04f72014-09-16 16:09:58 -0700171 @Override
172 public void removeFlowRule(FlowRule... flowRules) {
alshabib219ebaa2014-09-22 15:41:24 -0700173 for (int i = 0; i < flowRules.length; i++) {
174 removeRule(flowRules[i]);
175 }
alshabib1cc04f72014-09-16 16:09:58 -0700176
177 }
178
alshabib219ebaa2014-09-22 15:41:24 -0700179 private void removeRule(FlowRule flowRule) {
180 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700181 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory()).buildFlowDel());
alshabib219ebaa2014-09-22 15:41:24 -0700182 }
183
alshabiba68eb962014-09-24 20:34:13 -0700184 @Override
185 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
186 // TODO: optimize using the ApplicationId
187 removeFlowRule(flowRules);
188 }
189
alshabib193525b2014-10-08 18:58:03 -0700190 @Override
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800191 public Future<CompletedBatchOperation> executeBatch(BatchOperation<FlowRuleBatchEntry> batch) {
Jonathan Hart11096402014-10-20 17:31:49 -0700192 final Set<Dpid> sws =
193 Collections.newSetFromMap(new ConcurrentHashMap<Dpid, Boolean>());
alshabib193525b2014-10-08 18:58:03 -0700194 final Map<Long, FlowRuleBatchEntry> fmXids = new HashMap<Long, FlowRuleBatchEntry>();
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800195 /*
196 * Use identity hash map for reference equality as we could have equal
197 * flow mods for different switches.
198 */
199 Map<OFFlowMod, OpenFlowSwitch> mods = Maps.newIdentityHashMap();
alshabib193525b2014-10-08 18:58:03 -0700200 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
201 FlowRule flowRule = fbe.getTarget();
202 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib7911a052014-10-16 17:49:37 -0700203 if (sw == null) {
alshabib3effd042014-10-17 12:00:31 -0700204 /*
205 * if a switch we are supposed to install to is gone then
206 * cancel (ie. rollback) the work that has been done so far
207 * and return the associated future.
208 */
209 InstallationFuture failed = new InstallationFuture(sws, fmXids);
210 failed.cancel(true);
211 return failed;
alshabib7911a052014-10-16 17:49:37 -0700212 }
alshabib193525b2014-10-08 18:58:03 -0700213 sws.add(new Dpid(sw.getId()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700214 FlowModBuilder builder = FlowModBuilder.builder(flowRule, sw.factory());
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800215 OFFlowMod mod = null;
alshabib193525b2014-10-08 18:58:03 -0700216 switch (fbe.getOperator()) {
217 case ADD:
218 mod = builder.buildFlowAdd();
219 break;
220 case REMOVE:
221 mod = builder.buildFlowDel();
222 break;
223 case MODIFY:
224 mod = builder.buildFlowMod();
225 break;
226 default:
227 log.error("Unsupported batch operation {}", fbe.getOperator());
228 }
229 if (mod != null) {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800230 mods.put(mod, sw);
231 fmXids.put(xidCounter.getAndIncrement(), fbe);
alshabib193525b2014-10-08 18:58:03 -0700232 } else {
233 log.error("Conversion of flowrule {} failed.", flowRule);
234 }
alshabib193525b2014-10-08 18:58:03 -0700235 }
236 InstallationFuture installation = new InstallationFuture(sws, fmXids);
237 for (Long xid : fmXids.keySet()) {
238 pendingFMs.put(xid, installation);
239 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800240 pendingFutures.put(installation.xid(), installation);
241 for (Map.Entry<OFFlowMod, OpenFlowSwitch> entry : mods.entrySet()) {
242 OpenFlowSwitch sw = entry.getValue();
243 OFFlowMod mod = entry.getKey();
244 sw.sendMsg(mod);
245 }
246 installation.verify();
alshabib193525b2014-10-08 18:58:03 -0700247 return installation;
248 }
249
250
alshabib8f1cf4a2014-09-17 14:44:48 -0700251 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800252 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700253
alshabib3d643ec2014-10-22 18:33:00 -0700254
alshabib1c319ff2014-10-04 20:29:09 -0700255 private final Multimap<DeviceId, FlowEntry> completeEntries =
alshabib54ce5892014-09-23 17:50:51 -0700256 ArrayListMultimap.create();
alshabib8f1cf4a2014-09-17 14:44:48 -0700257
258 @Override
259 public void switchAdded(Dpid dpid) {
alshabibba5ac482014-10-02 17:15:20 -0700260 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
alshabibeec3a062014-09-17 18:01:26 -0700261 fsc.start();
262 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700263 }
264
265 @Override
266 public void switchRemoved(Dpid dpid) {
alshabibdfc7afb2014-10-21 20:13:27 -0700267 FlowStatsCollector collector = collectors.remove(dpid);
268 if (collector != null) {
269 collector.stop();
270 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700271 }
272
273 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700274 public void switchChanged(Dpid dpid) {
275 }
276
277 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700278 public void portChanged(Dpid dpid, OFPortStatus status) {
279 //TODO: Decide whether to evict flows internal store.
280 }
281
282 @Override
283 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabib902d41b2014-10-07 16:52:05 -0700284 InstallationFuture future = null;
alshabib8f1cf4a2014-09-17 14:44:48 -0700285 switch (msg.getType()) {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800286 case FLOW_REMOVED:
287 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700288
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800289 FlowEntry fr = new FlowEntryBuilder(dpid, removed).build();
290 providerService.flowRemoved(fr);
291 break;
292 case STATS_REPLY:
293 pushFlowMetrics(dpid, (OFStatsReply) msg);
294 break;
295 case BARRIER_REPLY:
296 future = pendingFutures.get(msg.getXid());
297 if (future != null) {
298 future.satisfyRequirement(dpid);
299 }
300 break;
301 case ERROR:
302 future = pendingFMs.get(msg.getXid());
303 if (future != null) {
304 future.fail((OFErrorMsg) msg, dpid);
305 }
306 break;
307 default:
308 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700309 }
310
311 }
312
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700313 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700314 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800315 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700316 // Do nothing here for now.
317 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700318
alshabib54ce5892014-09-23 17:50:51 -0700319 private synchronized void pushFlowMetrics(Dpid dpid, OFStatsReply stats) {
alshabib5c370ff2014-09-18 10:12:14 -0700320 if (stats.getStatsType() != OFStatsType.FLOW) {
321 return;
322 }
alshabib54ce5892014-09-23 17:50:51 -0700323 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib5c370ff2014-09-18 10:12:14 -0700324 final OFFlowStatsReply replies = (OFFlowStatsReply) stats;
alshabib54ce5892014-09-23 17:50:51 -0700325 //final List<FlowRule> entries = Lists.newLinkedList();
326
alshabib5c370ff2014-09-18 10:12:14 -0700327 for (OFFlowStatsEntry reply : replies.getEntries()) {
alshabib19fdc122014-10-03 11:38:19 -0700328 if (!tableMissRule(dpid, reply)) {
alshabib1c319ff2014-10-04 20:29:09 -0700329 completeEntries.put(did, new FlowEntryBuilder(dpid, reply).build());
alshabib19fdc122014-10-03 11:38:19 -0700330 }
alshabib5c370ff2014-09-18 10:12:14 -0700331 }
alshabib54ce5892014-09-23 17:50:51 -0700332
333 if (!stats.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
334 log.debug("sending flowstats to core {}", completeEntries.get(did));
335 providerService.pushFlowMetrics(did, completeEntries.get(did));
336 completeEntries.removeAll(did);
337 }
alshabib5c370ff2014-09-18 10:12:14 -0700338 }
339
alshabib19fdc122014-10-03 11:38:19 -0700340 private boolean tableMissRule(Dpid dpid, OFFlowStatsEntry reply) {
alshabib19fdc122014-10-03 11:38:19 -0700341 if (reply.getVersion().equals(OFVersion.OF_10) ||
342 reply.getMatch().getMatchFields().iterator().hasNext()) {
343 return false;
344 }
345 for (OFInstruction ins : reply.getInstructions()) {
346 if (ins.getType() == OFInstructionType.APPLY_ACTIONS) {
347 OFInstructionApplyActions apply = (OFInstructionApplyActions) ins;
348 List<OFAction> acts = apply.getActions();
349 for (OFAction act : acts) {
350 if (act.getType() == OFActionType.OUTPUT) {
351 OFActionOutput out = (OFActionOutput) act;
352 if (out.getPort() == OFPort.CONTROLLER) {
353 return true;
354 }
355 }
356 }
357 }
358 }
359 return false;
360 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700361
alshabib8f1cf4a2014-09-17 14:44:48 -0700362 }
alshabib1cc04f72014-09-16 16:09:58 -0700363
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800364 private class InstallationFuture implements Future<CompletedBatchOperation> {
alshabib902d41b2014-10-07 16:52:05 -0700365
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800366 private final Long xid;
alshabib902d41b2014-10-07 16:52:05 -0700367 private final Set<Dpid> sws;
368 private final AtomicBoolean ok = new AtomicBoolean(true);
alshabib193525b2014-10-08 18:58:03 -0700369 private final Map<Long, FlowRuleBatchEntry> fms;
370
Madan Jampani117aaae2014-10-23 10:04:05 -0700371 private final Set<FlowEntry> offendingFlowMods = Sets.newHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700372
373 private final CountDownLatch countDownLatch;
alshabib193525b2014-10-08 18:58:03 -0700374 private BatchState state;
alshabib902d41b2014-10-07 16:52:05 -0700375
Madan Jampani117aaae2014-10-23 10:04:05 -0700376 private final ExecutionList executionList = new ExecutionList();
377
alshabib193525b2014-10-08 18:58:03 -0700378 public InstallationFuture(Set<Dpid> sws, Map<Long, FlowRuleBatchEntry> fmXids) {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800379 this.xid = xidCounter.getAndIncrement();
alshabib193525b2014-10-08 18:58:03 -0700380 this.state = BatchState.STARTED;
alshabib902d41b2014-10-07 16:52:05 -0700381 this.sws = sws;
alshabib193525b2014-10-08 18:58:03 -0700382 this.fms = fmXids;
alshabib902d41b2014-10-07 16:52:05 -0700383 countDownLatch = new CountDownLatch(sws.size());
384 }
385
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800386 public Long xid() {
387 return xid;
388 }
389
alshabib902d41b2014-10-07 16:52:05 -0700390 public void fail(OFErrorMsg msg, Dpid dpid) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700391
alshabib902d41b2014-10-07 16:52:05 -0700392 ok.set(false);
alshabib7911a052014-10-16 17:49:37 -0700393 removeRequirement(dpid);
alshabib193525b2014-10-08 18:58:03 -0700394 FlowEntry fe = null;
395 FlowRuleBatchEntry fbe = fms.get(msg.getXid());
396 FlowRule offending = fbe.getTarget();
alshabib902d41b2014-10-07 16:52:05 -0700397 //TODO handle specific error msgs
alshabib902d41b2014-10-07 16:52:05 -0700398 switch (msg.getErrType()) {
399 case BAD_ACTION:
alshabib193525b2014-10-08 18:58:03 -0700400 OFBadActionErrorMsg bad = (OFBadActionErrorMsg) msg;
401 fe = new DefaultFlowEntry(offending, bad.getErrType().ordinal(),
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800402 bad.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700403 break;
404 case BAD_INSTRUCTION:
alshabib193525b2014-10-08 18:58:03 -0700405 OFBadInstructionErrorMsg badins = (OFBadInstructionErrorMsg) msg;
406 fe = new DefaultFlowEntry(offending, badins.getErrType().ordinal(),
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800407 badins.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700408 break;
409 case BAD_MATCH:
alshabib193525b2014-10-08 18:58:03 -0700410 OFBadMatchErrorMsg badMatch = (OFBadMatchErrorMsg) msg;
411 fe = new DefaultFlowEntry(offending, badMatch.getErrType().ordinal(),
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800412 badMatch.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700413 break;
414 case BAD_REQUEST:
alshabib193525b2014-10-08 18:58:03 -0700415 OFBadRequestErrorMsg badReq = (OFBadRequestErrorMsg) msg;
416 fe = new DefaultFlowEntry(offending, badReq.getErrType().ordinal(),
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800417 badReq.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700418 break;
419 case FLOW_MOD_FAILED:
alshabib193525b2014-10-08 18:58:03 -0700420 OFFlowModFailedErrorMsg fmFail = (OFFlowModFailedErrorMsg) msg;
421 fe = new DefaultFlowEntry(offending, fmFail.getErrType().ordinal(),
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800422 fmFail.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700423 break;
alshabib193525b2014-10-08 18:58:03 -0700424 case EXPERIMENTER:
alshabib902d41b2014-10-07 16:52:05 -0700425 case GROUP_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700426 case HELLO_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700427 case METER_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700428 case PORT_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700429 case QUEUE_OP_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700430 case ROLE_REQUEST_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700431 case SWITCH_CONFIG_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700432 case TABLE_FEATURES_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700433 case TABLE_MOD_FAILED:
alshabib193525b2014-10-08 18:58:03 -0700434 fe = new DefaultFlowEntry(offending, msg.getErrType().ordinal(), 0);
alshabib902d41b2014-10-07 16:52:05 -0700435 break;
436 default:
alshabib193525b2014-10-08 18:58:03 -0700437 log.error("Unknown error type {}", msg.getErrType());
alshabib902d41b2014-10-07 16:52:05 -0700438
439 }
alshabib193525b2014-10-08 18:58:03 -0700440 offendingFlowMods.add(fe);
alshabib902d41b2014-10-07 16:52:05 -0700441
442 }
443
alshabib193525b2014-10-08 18:58:03 -0700444
alshabib902d41b2014-10-07 16:52:05 -0700445 public void satisfyRequirement(Dpid dpid) {
alshabib3effd042014-10-17 12:00:31 -0700446 log.debug("Satisfaction from switch {}", dpid);
alshabib7911a052014-10-16 17:49:37 -0700447 removeRequirement(dpid);
alshabib902d41b2014-10-07 16:52:05 -0700448 }
449
alshabib193525b2014-10-08 18:58:03 -0700450
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800451 public void verify() {
alshabib902d41b2014-10-07 16:52:05 -0700452 for (Dpid dpid : sws) {
453 OpenFlowSwitch sw = controller.getSwitch(dpid);
454 OFBarrierRequest.Builder builder = sw.factory()
455 .buildBarrierRequest()
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800456 .setXid(xid);
alshabib902d41b2014-10-07 16:52:05 -0700457 sw.sendMsg(builder.build());
458 }
alshabib902d41b2014-10-07 16:52:05 -0700459 }
460
461 @Override
462 public boolean cancel(boolean mayInterruptIfRunning) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700463 if (isDone()) {
464 return false;
465 }
alshabib7911a052014-10-16 17:49:37 -0700466 ok.set(false);
alshabib193525b2014-10-08 18:58:03 -0700467 this.state = BatchState.CANCELLED;
468 cleanUp();
469 for (FlowRuleBatchEntry fbe : fms.values()) {
470 if (fbe.getOperator() == FlowRuleOperation.ADD ||
471 fbe.getOperator() == FlowRuleOperation.MODIFY) {
472 removeFlowRule(fbe.getTarget());
473 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
474 applyRule(fbe.getTarget());
475 }
476
477 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700478 return true;
alshabib902d41b2014-10-07 16:52:05 -0700479 }
480
481 @Override
482 public boolean isCancelled() {
alshabib193525b2014-10-08 18:58:03 -0700483 return this.state == BatchState.CANCELLED;
alshabib902d41b2014-10-07 16:52:05 -0700484 }
485
486 @Override
487 public boolean isDone() {
Madan Jampani117aaae2014-10-23 10:04:05 -0700488 return this.state == BatchState.FINISHED || isCancelled();
alshabib902d41b2014-10-07 16:52:05 -0700489 }
490
491 @Override
alshabib193525b2014-10-08 18:58:03 -0700492 public CompletedBatchOperation get() throws InterruptedException, ExecutionException {
alshabib902d41b2014-10-07 16:52:05 -0700493 countDownLatch.await();
alshabib193525b2014-10-08 18:58:03 -0700494 this.state = BatchState.FINISHED;
Madan Jampani117aaae2014-10-23 10:04:05 -0700495 CompletedBatchOperation result = new CompletedBatchOperation(ok.get(), offendingFlowMods);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800496 //FIXME do cleanup here
Madan Jampani117aaae2014-10-23 10:04:05 -0700497 return result;
alshabib902d41b2014-10-07 16:52:05 -0700498 }
499
500 @Override
alshabib193525b2014-10-08 18:58:03 -0700501 public CompletedBatchOperation get(long timeout, TimeUnit unit)
alshabib902d41b2014-10-07 16:52:05 -0700502 throws InterruptedException, ExecutionException,
503 TimeoutException {
alshabib26834582014-10-08 20:15:46 -0700504 if (countDownLatch.await(timeout, unit)) {
505 this.state = BatchState.FINISHED;
Madan Jampani117aaae2014-10-23 10:04:05 -0700506 CompletedBatchOperation result = new CompletedBatchOperation(ok.get(), offendingFlowMods);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800507 // FIXME do cleanup here
Madan Jampani117aaae2014-10-23 10:04:05 -0700508 return result;
alshabib26834582014-10-08 20:15:46 -0700509 }
510 throw new TimeoutException();
alshabib193525b2014-10-08 18:58:03 -0700511 }
512
513 private void cleanUp() {
alshabib7911a052014-10-16 17:49:37 -0700514 if (isDone() || isCancelled()) {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800515 pendingFutures.remove(xid);
alshabib193525b2014-10-08 18:58:03 -0700516 for (Long xid : fms.keySet()) {
517 pendingFMs.remove(xid);
518 }
519 }
alshabib902d41b2014-10-07 16:52:05 -0700520 }
521
alshabib7911a052014-10-16 17:49:37 -0700522 private void removeRequirement(Dpid dpid) {
523 countDownLatch.countDown();
524 sws.remove(dpid);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800525 //FIXME don't do cleanup here
alshabib7911a052014-10-16 17:49:37 -0700526 cleanUp();
527 }
alshabib902d41b2014-10-07 16:52:05 -0700528 }
alshabiba68eb962014-09-24 20:34:13 -0700529
alshabib1cc04f72014-09-16 16:09:58 -0700530}