blob: b7d84f0571d5d8a8b09f9a36b563ae79fb9c0e2d [file] [log] [blame]
alshabib1cc04f72014-09-16 16:09:58 -07001package org.onlab.onos.provider.of.flow.impl;
2
Jonathan Hart11096402014-10-20 17:31:49 -07003import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9import java.util.Set;
10import java.util.concurrent.ConcurrentHashMap;
11import java.util.concurrent.CountDownLatch;
12import java.util.concurrent.ExecutionException;
Madan Jampani117aaae2014-10-23 10:04:05 -070013import java.util.concurrent.Executor;
Jonathan Hart11096402014-10-20 17:31:49 -070014import java.util.concurrent.TimeUnit;
15import java.util.concurrent.TimeoutException;
16import java.util.concurrent.atomic.AtomicBoolean;
17
alshabib1cc04f72014-09-16 16:09:58 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabiba68eb962014-09-24 20:34:13 -070023import org.onlab.onos.ApplicationId;
alshabiba7f7ca82014-09-22 11:41:23 -070024import org.onlab.onos.net.DeviceId;
alshabib193525b2014-10-08 18:58:03 -070025import org.onlab.onos.net.flow.CompletedBatchOperation;
26import org.onlab.onos.net.flow.DefaultFlowEntry;
alshabib1c319ff2014-10-04 20:29:09 -070027import org.onlab.onos.net.flow.FlowEntry;
alshabib1cc04f72014-09-16 16:09:58 -070028import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070029import org.onlab.onos.net.flow.FlowRuleBatchEntry;
alshabib193525b2014-10-08 18:58:03 -070030import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
alshabib1cc04f72014-09-16 16:09:58 -070031import org.onlab.onos.net.flow.FlowRuleProvider;
32import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
33import org.onlab.onos.net.flow.FlowRuleProviderService;
alshabib902d41b2014-10-07 16:52:05 -070034import org.onlab.onos.net.intent.BatchOperation;
alshabib1cc04f72014-09-16 16:09:58 -070035import org.onlab.onos.net.provider.AbstractProvider;
36import org.onlab.onos.net.provider.ProviderId;
37import org.onlab.onos.net.topology.TopologyService;
tom9c94c5b2014-09-17 13:14:42 -070038import org.onlab.onos.openflow.controller.Dpid;
39import org.onlab.onos.openflow.controller.OpenFlowController;
alshabibeec3a062014-09-17 18:01:26 -070040import org.onlab.onos.openflow.controller.OpenFlowEventListener;
tom9c94c5b2014-09-17 13:14:42 -070041import org.onlab.onos.openflow.controller.OpenFlowSwitch;
alshabibce4e5782014-09-17 14:56:42 -070042import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -070043import org.onlab.onos.openflow.controller.RoleState;
alshabib19fdc122014-10-03 11:38:19 -070044import org.projectfloodlight.openflow.protocol.OFActionType;
alshabib902d41b2014-10-07 16:52:05 -070045import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
46import org.projectfloodlight.openflow.protocol.OFErrorMsg;
alshabib193525b2014-10-08 18:58:03 -070047import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070048import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070049import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
50import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib19fdc122014-10-03 11:38:19 -070051import org.projectfloodlight.openflow.protocol.OFInstructionType;
alshabib8f1cf4a2014-09-17 14:44:48 -070052import org.projectfloodlight.openflow.protocol.OFMessage;
53import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070054import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib54ce5892014-09-23 17:50:51 -070055import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
alshabib5c370ff2014-09-18 10:12:14 -070056import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib19fdc122014-10-03 11:38:19 -070057import org.projectfloodlight.openflow.protocol.OFVersion;
58import org.projectfloodlight.openflow.protocol.action.OFAction;
59import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
alshabib193525b2014-10-08 18:58:03 -070060import org.projectfloodlight.openflow.protocol.errormsg.OFBadActionErrorMsg;
61import org.projectfloodlight.openflow.protocol.errormsg.OFBadInstructionErrorMsg;
62import org.projectfloodlight.openflow.protocol.errormsg.OFBadMatchErrorMsg;
63import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg;
64import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib19fdc122014-10-03 11:38:19 -070065import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
66import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
67import org.projectfloodlight.openflow.types.OFPort;
alshabib902d41b2014-10-07 16:52:05 -070068import org.projectfloodlight.openflow.types.U32;
alshabib1cc04f72014-09-16 16:09:58 -070069import org.slf4j.Logger;
70
Jonathan Hart11096402014-10-20 17:31:49 -070071import com.google.common.collect.ArrayListMultimap;
Jonathan Hart11096402014-10-20 17:31:49 -070072import com.google.common.collect.Maps;
73import com.google.common.collect.Multimap;
Madan Jampani117aaae2014-10-23 10:04:05 -070074import com.google.common.collect.Sets;
75import com.google.common.util.concurrent.ExecutionList;
76import com.google.common.util.concurrent.ListenableFuture;
alshabibeec3a062014-09-17 18:01:26 -070077
alshabib1cc04f72014-09-16 16:09:58 -070078/**
79 * Provider which uses an OpenFlow controller to detect network
80 * end-station hosts.
81 */
82@Component(immediate = true)
83public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
84
alshabib193525b2014-10-08 18:58:03 -070085 enum BatchState { STARTED, FINISHED, CANCELLED };
86
alshabib1cc04f72014-09-16 16:09:58 -070087 private final Logger log = getLogger(getClass());
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected FlowRuleProviderRegistry providerRegistry;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected OpenFlowController controller;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected TopologyService topologyService;
97
98 private FlowRuleProviderService providerService;
99
alshabibeec3a062014-09-17 18:01:26 -0700100 private final InternalFlowProvider listener = new InternalFlowProvider();
101
Madan Jampani117aaae2014-10-23 10:04:05 -0700102 // FIXME: This should be an expiring map to ensure futures that don't have
103 // a future eventually get garbage collected.
alshabib902d41b2014-10-07 16:52:05 -0700104 private final Map<Long, InstallationFuture> pendingFutures =
105 new ConcurrentHashMap<Long, InstallationFuture>();
106
alshabib193525b2014-10-08 18:58:03 -0700107 private final Map<Long, InstallationFuture> pendingFMs =
108 new ConcurrentHashMap<Long, InstallationFuture>();
109
alshabib1cc04f72014-09-16 16:09:58 -0700110 /**
111 * Creates an OpenFlow host provider.
112 */
113 public OpenFlowRuleProvider() {
tom7e02cda2014-09-18 12:05:46 -0700114 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700115 }
116
117 @Activate
118 public void activate() {
119 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700120 controller.addListener(listener);
121 controller.addEventListener(listener);
alshabib1cc04f72014-09-16 16:09:58 -0700122 log.info("Started");
123 }
124
125 @Deactivate
126 public void deactivate() {
127 providerRegistry.unregister(this);
128 providerService = null;
129
130 log.info("Stopped");
131 }
132 @Override
133 public void applyFlowRule(FlowRule... flowRules) {
alshabib35edb1a2014-09-16 17:44:44 -0700134 for (int i = 0; i < flowRules.length; i++) {
135 applyRule(flowRules[i]);
136 }
alshabib1cc04f72014-09-16 16:09:58 -0700137 }
138
alshabib35edb1a2014-09-16 17:44:44 -0700139 private void applyRule(FlowRule flowRule) {
140 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib902d41b2014-10-07 16:52:05 -0700141 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowAdd());
alshabib35edb1a2014-09-16 17:44:44 -0700142 }
143
alshabib35edb1a2014-09-16 17:44:44 -0700144
alshabib35edb1a2014-09-16 17:44:44 -0700145
alshabib1cc04f72014-09-16 16:09:58 -0700146 @Override
147 public void removeFlowRule(FlowRule... flowRules) {
alshabib219ebaa2014-09-22 15:41:24 -0700148 for (int i = 0; i < flowRules.length; i++) {
149 removeRule(flowRules[i]);
150 }
alshabib1cc04f72014-09-16 16:09:58 -0700151
152 }
153
alshabib219ebaa2014-09-22 15:41:24 -0700154 private void removeRule(FlowRule flowRule) {
155 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
156 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowDel());
157 }
158
alshabiba68eb962014-09-24 20:34:13 -0700159 @Override
160 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
161 // TODO: optimize using the ApplicationId
162 removeFlowRule(flowRules);
163 }
164
alshabib193525b2014-10-08 18:58:03 -0700165 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700166 public ListenableFuture<CompletedBatchOperation> executeBatch(BatchOperation<FlowRuleBatchEntry> batch) {
Jonathan Hart11096402014-10-20 17:31:49 -0700167 final Set<Dpid> sws =
168 Collections.newSetFromMap(new ConcurrentHashMap<Dpid, Boolean>());
alshabib193525b2014-10-08 18:58:03 -0700169 final Map<Long, FlowRuleBatchEntry> fmXids = new HashMap<Long, FlowRuleBatchEntry>();
170 OFFlowMod mod = null;
171 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
172 FlowRule flowRule = fbe.getTarget();
173 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib7911a052014-10-16 17:49:37 -0700174 if (sw == null) {
alshabib3effd042014-10-17 12:00:31 -0700175 /*
176 * if a switch we are supposed to install to is gone then
177 * cancel (ie. rollback) the work that has been done so far
178 * and return the associated future.
179 */
180 InstallationFuture failed = new InstallationFuture(sws, fmXids);
181 failed.cancel(true);
182 return failed;
alshabib7911a052014-10-16 17:49:37 -0700183 }
alshabib193525b2014-10-08 18:58:03 -0700184 sws.add(new Dpid(sw.getId()));
185 FlowModBuilder builder = new FlowModBuilder(flowRule, sw.factory());
186 switch (fbe.getOperator()) {
187 case ADD:
188 mod = builder.buildFlowAdd();
189 break;
190 case REMOVE:
191 mod = builder.buildFlowDel();
192 break;
193 case MODIFY:
194 mod = builder.buildFlowMod();
195 break;
196 default:
197 log.error("Unsupported batch operation {}", fbe.getOperator());
198 }
199 if (mod != null) {
200 sw.sendMsg(mod);
201 fmXids.put(mod.getXid(), fbe);
202 } else {
203 log.error("Conversion of flowrule {} failed.", flowRule);
204 }
alshabib219ebaa2014-09-22 15:41:24 -0700205
alshabib193525b2014-10-08 18:58:03 -0700206 }
207 InstallationFuture installation = new InstallationFuture(sws, fmXids);
208 for (Long xid : fmXids.keySet()) {
209 pendingFMs.put(xid, installation);
210 }
211 pendingFutures.put(U32.f(batch.hashCode()), installation);
alshabibdfc7afb2014-10-21 20:13:27 -0700212 installation.verify(U32.f(batch.hashCode()));
alshabib193525b2014-10-08 18:58:03 -0700213 return installation;
214 }
215
216
alshabib8f1cf4a2014-09-17 14:44:48 -0700217 private class InternalFlowProvider
218 implements OpenFlowSwitchListener, OpenFlowEventListener {
219
alshabibeec3a062014-09-17 18:01:26 -0700220 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
alshabib1c319ff2014-10-04 20:29:09 -0700221 private final Multimap<DeviceId, FlowEntry> completeEntries =
alshabib54ce5892014-09-23 17:50:51 -0700222 ArrayListMultimap.create();
alshabib8f1cf4a2014-09-17 14:44:48 -0700223
224 @Override
225 public void switchAdded(Dpid dpid) {
alshabibba5ac482014-10-02 17:15:20 -0700226 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
alshabibeec3a062014-09-17 18:01:26 -0700227 fsc.start();
228 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700229 }
230
231 @Override
232 public void switchRemoved(Dpid dpid) {
alshabibdfc7afb2014-10-21 20:13:27 -0700233 FlowStatsCollector collector = collectors.remove(dpid);
234 if (collector != null) {
235 collector.stop();
236 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700237 }
238
239 @Override
240 public void portChanged(Dpid dpid, OFPortStatus status) {
241 //TODO: Decide whether to evict flows internal store.
242 }
243
244 @Override
245 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabib902d41b2014-10-07 16:52:05 -0700246 InstallationFuture future = null;
alshabib8f1cf4a2014-09-17 14:44:48 -0700247 switch (msg.getType()) {
248 case FLOW_REMOVED:
249 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700250
alshabib1c319ff2014-10-04 20:29:09 -0700251 FlowEntry fr = new FlowEntryBuilder(dpid, removed).build();
alshabib8f1cf4a2014-09-17 14:44:48 -0700252 providerService.flowRemoved(fr);
253 break;
254 case STATS_REPLY:
alshabib97044902014-09-18 14:52:16 -0700255 pushFlowMetrics(dpid, (OFStatsReply) msg);
alshabibeec3a062014-09-17 18:01:26 -0700256 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700257 case BARRIER_REPLY:
alshabib902d41b2014-10-07 16:52:05 -0700258 future = pendingFutures.get(msg.getXid());
259 if (future != null) {
260 future.satisfyRequirement(dpid);
261 }
262 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700263 case ERROR:
alshabib193525b2014-10-08 18:58:03 -0700264 future = pendingFMs.get(msg.getXid());
alshabib902d41b2014-10-07 16:52:05 -0700265 if (future != null) {
266 future.fail((OFErrorMsg) msg, dpid);
267 }
268 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700269 default:
alshabib6eb438a2014-10-01 16:39:37 -0700270 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700271 }
272
273 }
274
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700275 @Override
alshabib193525b2014-10-08 18:58:03 -0700276 public void roleAssertFailed(Dpid dpid, RoleState role) {}
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700277
alshabib54ce5892014-09-23 17:50:51 -0700278 private synchronized void pushFlowMetrics(Dpid dpid, OFStatsReply stats) {
alshabib5c370ff2014-09-18 10:12:14 -0700279 if (stats.getStatsType() != OFStatsType.FLOW) {
280 return;
281 }
alshabib54ce5892014-09-23 17:50:51 -0700282 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib5c370ff2014-09-18 10:12:14 -0700283 final OFFlowStatsReply replies = (OFFlowStatsReply) stats;
alshabib54ce5892014-09-23 17:50:51 -0700284 //final List<FlowRule> entries = Lists.newLinkedList();
285
alshabib5c370ff2014-09-18 10:12:14 -0700286 for (OFFlowStatsEntry reply : replies.getEntries()) {
alshabib19fdc122014-10-03 11:38:19 -0700287 if (!tableMissRule(dpid, reply)) {
alshabib1c319ff2014-10-04 20:29:09 -0700288 completeEntries.put(did, new FlowEntryBuilder(dpid, reply).build());
alshabib19fdc122014-10-03 11:38:19 -0700289 }
alshabib5c370ff2014-09-18 10:12:14 -0700290 }
alshabib54ce5892014-09-23 17:50:51 -0700291
292 if (!stats.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
293 log.debug("sending flowstats to core {}", completeEntries.get(did));
294 providerService.pushFlowMetrics(did, completeEntries.get(did));
295 completeEntries.removeAll(did);
296 }
alshabib5c370ff2014-09-18 10:12:14 -0700297 }
298
alshabib19fdc122014-10-03 11:38:19 -0700299 private boolean tableMissRule(Dpid dpid, OFFlowStatsEntry reply) {
alshabib19fdc122014-10-03 11:38:19 -0700300 if (reply.getVersion().equals(OFVersion.OF_10) ||
301 reply.getMatch().getMatchFields().iterator().hasNext()) {
302 return false;
303 }
304 for (OFInstruction ins : reply.getInstructions()) {
305 if (ins.getType() == OFInstructionType.APPLY_ACTIONS) {
306 OFInstructionApplyActions apply = (OFInstructionApplyActions) ins;
307 List<OFAction> acts = apply.getActions();
308 for (OFAction act : acts) {
309 if (act.getType() == OFActionType.OUTPUT) {
310 OFActionOutput out = (OFActionOutput) act;
311 if (out.getPort() == OFPort.CONTROLLER) {
312 return true;
313 }
314 }
315 }
316 }
317 }
318 return false;
319 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700320 }
alshabib1cc04f72014-09-16 16:09:58 -0700321
Madan Jampani117aaae2014-10-23 10:04:05 -0700322 private class InstallationFuture implements ListenableFuture<CompletedBatchOperation> {
alshabib902d41b2014-10-07 16:52:05 -0700323
324 private final Set<Dpid> sws;
325 private final AtomicBoolean ok = new AtomicBoolean(true);
alshabib193525b2014-10-08 18:58:03 -0700326 private final Map<Long, FlowRuleBatchEntry> fms;
327
Madan Jampani117aaae2014-10-23 10:04:05 -0700328 private final Set<FlowEntry> offendingFlowMods = Sets.newHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700329
330 private final CountDownLatch countDownLatch;
alshabibdfc7afb2014-10-21 20:13:27 -0700331 private Long pendingXid;
alshabib193525b2014-10-08 18:58:03 -0700332 private BatchState state;
alshabib902d41b2014-10-07 16:52:05 -0700333
Madan Jampani117aaae2014-10-23 10:04:05 -0700334 private final ExecutionList executionList = new ExecutionList();
335
alshabib193525b2014-10-08 18:58:03 -0700336 public InstallationFuture(Set<Dpid> sws, Map<Long, FlowRuleBatchEntry> fmXids) {
337 this.state = BatchState.STARTED;
alshabib902d41b2014-10-07 16:52:05 -0700338 this.sws = sws;
alshabib193525b2014-10-08 18:58:03 -0700339 this.fms = fmXids;
alshabib902d41b2014-10-07 16:52:05 -0700340 countDownLatch = new CountDownLatch(sws.size());
341 }
342
343 public void fail(OFErrorMsg msg, Dpid dpid) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700344
alshabib902d41b2014-10-07 16:52:05 -0700345 ok.set(false);
alshabib7911a052014-10-16 17:49:37 -0700346 removeRequirement(dpid);
alshabib193525b2014-10-08 18:58:03 -0700347 FlowEntry fe = null;
348 FlowRuleBatchEntry fbe = fms.get(msg.getXid());
349 FlowRule offending = fbe.getTarget();
alshabib902d41b2014-10-07 16:52:05 -0700350 //TODO handle specific error msgs
alshabib902d41b2014-10-07 16:52:05 -0700351 switch (msg.getErrType()) {
352 case BAD_ACTION:
alshabib193525b2014-10-08 18:58:03 -0700353 OFBadActionErrorMsg bad = (OFBadActionErrorMsg) msg;
354 fe = new DefaultFlowEntry(offending, bad.getErrType().ordinal(),
355 bad.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700356 break;
357 case BAD_INSTRUCTION:
alshabib193525b2014-10-08 18:58:03 -0700358 OFBadInstructionErrorMsg badins = (OFBadInstructionErrorMsg) msg;
359 fe = new DefaultFlowEntry(offending, badins.getErrType().ordinal(),
360 badins.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700361 break;
362 case BAD_MATCH:
alshabib193525b2014-10-08 18:58:03 -0700363 OFBadMatchErrorMsg badMatch = (OFBadMatchErrorMsg) msg;
364 fe = new DefaultFlowEntry(offending, badMatch.getErrType().ordinal(),
365 badMatch.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700366 break;
367 case BAD_REQUEST:
alshabib193525b2014-10-08 18:58:03 -0700368 OFBadRequestErrorMsg badReq = (OFBadRequestErrorMsg) msg;
369 fe = new DefaultFlowEntry(offending, badReq.getErrType().ordinal(),
370 badReq.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700371 break;
372 case FLOW_MOD_FAILED:
alshabib193525b2014-10-08 18:58:03 -0700373 OFFlowModFailedErrorMsg fmFail = (OFFlowModFailedErrorMsg) msg;
374 fe = new DefaultFlowEntry(offending, fmFail.getErrType().ordinal(),
375 fmFail.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700376 break;
alshabib193525b2014-10-08 18:58:03 -0700377 case EXPERIMENTER:
alshabib902d41b2014-10-07 16:52:05 -0700378 case GROUP_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700379 case HELLO_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700380 case METER_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700381 case PORT_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700382 case QUEUE_OP_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700383 case ROLE_REQUEST_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700384 case SWITCH_CONFIG_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700385 case TABLE_FEATURES_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700386 case TABLE_MOD_FAILED:
alshabib193525b2014-10-08 18:58:03 -0700387 fe = new DefaultFlowEntry(offending, msg.getErrType().ordinal(), 0);
alshabib902d41b2014-10-07 16:52:05 -0700388 break;
389 default:
alshabib193525b2014-10-08 18:58:03 -0700390 log.error("Unknown error type {}", msg.getErrType());
alshabib902d41b2014-10-07 16:52:05 -0700391
392 }
alshabib193525b2014-10-08 18:58:03 -0700393 offendingFlowMods.add(fe);
alshabib902d41b2014-10-07 16:52:05 -0700394
395 }
396
alshabib193525b2014-10-08 18:58:03 -0700397
alshabib902d41b2014-10-07 16:52:05 -0700398 public void satisfyRequirement(Dpid dpid) {
alshabib3effd042014-10-17 12:00:31 -0700399 log.debug("Satisfaction from switch {}", dpid);
alshabib7911a052014-10-16 17:49:37 -0700400 removeRequirement(dpid);
alshabib902d41b2014-10-07 16:52:05 -0700401 }
402
alshabib193525b2014-10-08 18:58:03 -0700403
alshabibdfc7afb2014-10-21 20:13:27 -0700404 public void verify(Long id) {
alshabib193525b2014-10-08 18:58:03 -0700405 pendingXid = id;
alshabib902d41b2014-10-07 16:52:05 -0700406 for (Dpid dpid : sws) {
407 OpenFlowSwitch sw = controller.getSwitch(dpid);
408 OFBarrierRequest.Builder builder = sw.factory()
409 .buildBarrierRequest()
410 .setXid(id);
411 sw.sendMsg(builder.build());
412 }
alshabib902d41b2014-10-07 16:52:05 -0700413 }
414
415 @Override
416 public boolean cancel(boolean mayInterruptIfRunning) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700417 if (isDone()) {
418 return false;
419 }
alshabib7911a052014-10-16 17:49:37 -0700420 ok.set(false);
alshabib193525b2014-10-08 18:58:03 -0700421 this.state = BatchState.CANCELLED;
422 cleanUp();
423 for (FlowRuleBatchEntry fbe : fms.values()) {
424 if (fbe.getOperator() == FlowRuleOperation.ADD ||
425 fbe.getOperator() == FlowRuleOperation.MODIFY) {
426 removeFlowRule(fbe.getTarget());
427 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
428 applyRule(fbe.getTarget());
429 }
430
431 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700432 invokeCallbacks();
433 return true;
alshabib902d41b2014-10-07 16:52:05 -0700434 }
435
436 @Override
437 public boolean isCancelled() {
alshabib193525b2014-10-08 18:58:03 -0700438 return this.state == BatchState.CANCELLED;
alshabib902d41b2014-10-07 16:52:05 -0700439 }
440
441 @Override
442 public boolean isDone() {
Madan Jampani117aaae2014-10-23 10:04:05 -0700443 return this.state == BatchState.FINISHED || isCancelled();
alshabib902d41b2014-10-07 16:52:05 -0700444 }
445
446 @Override
alshabib193525b2014-10-08 18:58:03 -0700447 public CompletedBatchOperation get() throws InterruptedException, ExecutionException {
alshabib902d41b2014-10-07 16:52:05 -0700448 countDownLatch.await();
alshabib193525b2014-10-08 18:58:03 -0700449 this.state = BatchState.FINISHED;
Madan Jampani117aaae2014-10-23 10:04:05 -0700450 CompletedBatchOperation result = new CompletedBatchOperation(ok.get(), offendingFlowMods);
451 return result;
alshabib902d41b2014-10-07 16:52:05 -0700452 }
453
454 @Override
alshabib193525b2014-10-08 18:58:03 -0700455 public CompletedBatchOperation get(long timeout, TimeUnit unit)
alshabib902d41b2014-10-07 16:52:05 -0700456 throws InterruptedException, ExecutionException,
457 TimeoutException {
alshabib26834582014-10-08 20:15:46 -0700458 if (countDownLatch.await(timeout, unit)) {
459 this.state = BatchState.FINISHED;
Madan Jampani117aaae2014-10-23 10:04:05 -0700460 CompletedBatchOperation result = new CompletedBatchOperation(ok.get(), offendingFlowMods);
461 return result;
alshabib26834582014-10-08 20:15:46 -0700462 }
463 throw new TimeoutException();
alshabib193525b2014-10-08 18:58:03 -0700464 }
465
466 private void cleanUp() {
alshabib7911a052014-10-16 17:49:37 -0700467 if (isDone() || isCancelled()) {
alshabibdfc7afb2014-10-21 20:13:27 -0700468 if (pendingXid != null) {
469 pendingFutures.remove(pendingXid);
470 }
alshabib193525b2014-10-08 18:58:03 -0700471 for (Long xid : fms.keySet()) {
472 pendingFMs.remove(xid);
473 }
474 }
alshabib902d41b2014-10-07 16:52:05 -0700475 }
476
alshabib7911a052014-10-16 17:49:37 -0700477 private void removeRequirement(Dpid dpid) {
478 countDownLatch.countDown();
Madan Jampani117aaae2014-10-23 10:04:05 -0700479 if (countDownLatch.getCount() == 0) {
480 invokeCallbacks();
481 }
alshabib7911a052014-10-16 17:49:37 -0700482 sws.remove(dpid);
483 cleanUp();
484 }
485
Madan Jampani117aaae2014-10-23 10:04:05 -0700486 @Override
487 public void addListener(Runnable runnable, Executor executor) {
488 executionList.add(runnable, executor);
489 }
490
491 private void invokeCallbacks() {
492 executionList.execute();
493 }
alshabib902d41b2014-10-07 16:52:05 -0700494 }
alshabiba68eb962014-09-24 20:34:13 -0700495
alshabib1cc04f72014-09-16 16:09:58 -0700496}