blob: d18a72f919a0e03f9541d9cd2e4755d5e11a03ab [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;
13import java.util.concurrent.Future;
14import 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;
72import com.google.common.collect.Lists;
73import com.google.common.collect.Maps;
74import com.google.common.collect.Multimap;
alshabibeec3a062014-09-17 18:01:26 -070075
alshabib1cc04f72014-09-16 16:09:58 -070076/**
77 * Provider which uses an OpenFlow controller to detect network
78 * end-station hosts.
79 */
80@Component(immediate = true)
81public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
82
alshabib193525b2014-10-08 18:58:03 -070083 enum BatchState { STARTED, FINISHED, CANCELLED };
84
alshabib1cc04f72014-09-16 16:09:58 -070085 private final Logger log = getLogger(getClass());
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected FlowRuleProviderRegistry providerRegistry;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected OpenFlowController controller;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected TopologyService topologyService;
95
96 private FlowRuleProviderService providerService;
97
alshabibeec3a062014-09-17 18:01:26 -070098 private final InternalFlowProvider listener = new InternalFlowProvider();
99
alshabib902d41b2014-10-07 16:52:05 -0700100 private final Map<Long, InstallationFuture> pendingFutures =
101 new ConcurrentHashMap<Long, InstallationFuture>();
102
alshabib193525b2014-10-08 18:58:03 -0700103 private final Map<Long, InstallationFuture> pendingFMs =
104 new ConcurrentHashMap<Long, InstallationFuture>();
105
alshabib1cc04f72014-09-16 16:09:58 -0700106 /**
107 * Creates an OpenFlow host provider.
108 */
109 public OpenFlowRuleProvider() {
tom7e02cda2014-09-18 12:05:46 -0700110 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700111 }
112
113 @Activate
114 public void activate() {
115 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700116 controller.addListener(listener);
117 controller.addEventListener(listener);
alshabib1cc04f72014-09-16 16:09:58 -0700118 log.info("Started");
119 }
120
121 @Deactivate
122 public void deactivate() {
123 providerRegistry.unregister(this);
124 providerService = null;
125
126 log.info("Stopped");
127 }
128 @Override
129 public void applyFlowRule(FlowRule... flowRules) {
alshabib35edb1a2014-09-16 17:44:44 -0700130 for (int i = 0; i < flowRules.length; i++) {
131 applyRule(flowRules[i]);
132 }
alshabib1cc04f72014-09-16 16:09:58 -0700133 }
134
alshabib35edb1a2014-09-16 17:44:44 -0700135 private void applyRule(FlowRule flowRule) {
136 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib902d41b2014-10-07 16:52:05 -0700137 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowAdd());
alshabib35edb1a2014-09-16 17:44:44 -0700138 }
139
alshabib35edb1a2014-09-16 17:44:44 -0700140
alshabib35edb1a2014-09-16 17:44:44 -0700141
alshabib1cc04f72014-09-16 16:09:58 -0700142 @Override
143 public void removeFlowRule(FlowRule... flowRules) {
alshabib219ebaa2014-09-22 15:41:24 -0700144 for (int i = 0; i < flowRules.length; i++) {
145 removeRule(flowRules[i]);
146 }
alshabib1cc04f72014-09-16 16:09:58 -0700147
148 }
149
alshabib219ebaa2014-09-22 15:41:24 -0700150 private void removeRule(FlowRule flowRule) {
151 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
152 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowDel());
153 }
154
alshabiba68eb962014-09-24 20:34:13 -0700155 @Override
156 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
157 // TODO: optimize using the ApplicationId
158 removeFlowRule(flowRules);
159 }
160
alshabib193525b2014-10-08 18:58:03 -0700161 @Override
162 public Future<CompletedBatchOperation> executeBatch(BatchOperation<FlowRuleBatchEntry> batch) {
Jonathan Hart11096402014-10-20 17:31:49 -0700163 final Set<Dpid> sws =
164 Collections.newSetFromMap(new ConcurrentHashMap<Dpid, Boolean>());
alshabib193525b2014-10-08 18:58:03 -0700165 final Map<Long, FlowRuleBatchEntry> fmXids = new HashMap<Long, FlowRuleBatchEntry>();
166 OFFlowMod mod = null;
167 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
168 FlowRule flowRule = fbe.getTarget();
169 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib7911a052014-10-16 17:49:37 -0700170 if (sw == null) {
alshabib3effd042014-10-17 12:00:31 -0700171 /*
172 * if a switch we are supposed to install to is gone then
173 * cancel (ie. rollback) the work that has been done so far
174 * and return the associated future.
175 */
176 InstallationFuture failed = new InstallationFuture(sws, fmXids);
177 failed.cancel(true);
178 return failed;
alshabib7911a052014-10-16 17:49:37 -0700179 }
alshabib193525b2014-10-08 18:58:03 -0700180 sws.add(new Dpid(sw.getId()));
181 FlowModBuilder builder = new FlowModBuilder(flowRule, sw.factory());
182 switch (fbe.getOperator()) {
183 case ADD:
184 mod = builder.buildFlowAdd();
185 break;
186 case REMOVE:
187 mod = builder.buildFlowDel();
188 break;
189 case MODIFY:
190 mod = builder.buildFlowMod();
191 break;
192 default:
193 log.error("Unsupported batch operation {}", fbe.getOperator());
194 }
195 if (mod != null) {
196 sw.sendMsg(mod);
197 fmXids.put(mod.getXid(), fbe);
198 } else {
199 log.error("Conversion of flowrule {} failed.", flowRule);
200 }
alshabib219ebaa2014-09-22 15:41:24 -0700201
alshabib193525b2014-10-08 18:58:03 -0700202 }
203 InstallationFuture installation = new InstallationFuture(sws, fmXids);
204 for (Long xid : fmXids.keySet()) {
205 pendingFMs.put(xid, installation);
206 }
207 pendingFutures.put(U32.f(batch.hashCode()), installation);
alshabibdfc7afb2014-10-21 20:13:27 -0700208 installation.verify(U32.f(batch.hashCode()));
alshabib193525b2014-10-08 18:58:03 -0700209 return installation;
210 }
211
212
alshabib8f1cf4a2014-09-17 14:44:48 -0700213 private class InternalFlowProvider
214 implements OpenFlowSwitchListener, OpenFlowEventListener {
215
alshabibeec3a062014-09-17 18:01:26 -0700216 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
alshabib1c319ff2014-10-04 20:29:09 -0700217 private final Multimap<DeviceId, FlowEntry> completeEntries =
alshabib54ce5892014-09-23 17:50:51 -0700218 ArrayListMultimap.create();
alshabib8f1cf4a2014-09-17 14:44:48 -0700219
220 @Override
221 public void switchAdded(Dpid dpid) {
alshabibba5ac482014-10-02 17:15:20 -0700222 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
alshabibeec3a062014-09-17 18:01:26 -0700223 fsc.start();
224 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700225 }
226
227 @Override
228 public void switchRemoved(Dpid dpid) {
alshabibdfc7afb2014-10-21 20:13:27 -0700229 FlowStatsCollector collector = collectors.remove(dpid);
230 if (collector != null) {
231 collector.stop();
232 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700233 }
234
235 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700236 public void switchChanged(Dpid dpid) {
237 }
238
239 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700240 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 }
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700320
alshabib8f1cf4a2014-09-17 14:44:48 -0700321 }
alshabib1cc04f72014-09-16 16:09:58 -0700322
alshabib193525b2014-10-08 18:58:03 -0700323 private class InstallationFuture implements Future<CompletedBatchOperation> {
alshabib902d41b2014-10-07 16:52:05 -0700324
325 private final Set<Dpid> sws;
326 private final AtomicBoolean ok = new AtomicBoolean(true);
alshabib193525b2014-10-08 18:58:03 -0700327 private final Map<Long, FlowRuleBatchEntry> fms;
328
alshabib902d41b2014-10-07 16:52:05 -0700329 private final List<FlowEntry> offendingFlowMods = Lists.newLinkedList();
330
331 private final CountDownLatch countDownLatch;
alshabibdfc7afb2014-10-21 20:13:27 -0700332 private Long pendingXid;
alshabib193525b2014-10-08 18:58:03 -0700333 private BatchState state;
alshabib902d41b2014-10-07 16:52:05 -0700334
alshabib193525b2014-10-08 18:58:03 -0700335 public InstallationFuture(Set<Dpid> sws, Map<Long, FlowRuleBatchEntry> fmXids) {
336 this.state = BatchState.STARTED;
alshabib902d41b2014-10-07 16:52:05 -0700337 this.sws = sws;
alshabib193525b2014-10-08 18:58:03 -0700338 this.fms = fmXids;
alshabib902d41b2014-10-07 16:52:05 -0700339 countDownLatch = new CountDownLatch(sws.size());
340 }
341
342 public void fail(OFErrorMsg msg, Dpid dpid) {
343 ok.set(false);
alshabib7911a052014-10-16 17:49:37 -0700344 removeRequirement(dpid);
alshabib193525b2014-10-08 18:58:03 -0700345 FlowEntry fe = null;
346 FlowRuleBatchEntry fbe = fms.get(msg.getXid());
347 FlowRule offending = fbe.getTarget();
alshabib902d41b2014-10-07 16:52:05 -0700348 //TODO handle specific error msgs
alshabib902d41b2014-10-07 16:52:05 -0700349 switch (msg.getErrType()) {
350 case BAD_ACTION:
alshabib193525b2014-10-08 18:58:03 -0700351 OFBadActionErrorMsg bad = (OFBadActionErrorMsg) msg;
352 fe = new DefaultFlowEntry(offending, bad.getErrType().ordinal(),
353 bad.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700354 break;
355 case BAD_INSTRUCTION:
alshabib193525b2014-10-08 18:58:03 -0700356 OFBadInstructionErrorMsg badins = (OFBadInstructionErrorMsg) msg;
357 fe = new DefaultFlowEntry(offending, badins.getErrType().ordinal(),
358 badins.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700359 break;
360 case BAD_MATCH:
alshabib193525b2014-10-08 18:58:03 -0700361 OFBadMatchErrorMsg badMatch = (OFBadMatchErrorMsg) msg;
362 fe = new DefaultFlowEntry(offending, badMatch.getErrType().ordinal(),
363 badMatch.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700364 break;
365 case BAD_REQUEST:
alshabib193525b2014-10-08 18:58:03 -0700366 OFBadRequestErrorMsg badReq = (OFBadRequestErrorMsg) msg;
367 fe = new DefaultFlowEntry(offending, badReq.getErrType().ordinal(),
368 badReq.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700369 break;
370 case FLOW_MOD_FAILED:
alshabib193525b2014-10-08 18:58:03 -0700371 OFFlowModFailedErrorMsg fmFail = (OFFlowModFailedErrorMsg) msg;
372 fe = new DefaultFlowEntry(offending, fmFail.getErrType().ordinal(),
373 fmFail.getCode().ordinal());
alshabib902d41b2014-10-07 16:52:05 -0700374 break;
alshabib193525b2014-10-08 18:58:03 -0700375 case EXPERIMENTER:
alshabib902d41b2014-10-07 16:52:05 -0700376 case GROUP_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700377 case HELLO_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700378 case METER_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700379 case PORT_MOD_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700380 case QUEUE_OP_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700381 case ROLE_REQUEST_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700382 case SWITCH_CONFIG_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700383 case TABLE_FEATURES_FAILED:
alshabib902d41b2014-10-07 16:52:05 -0700384 case TABLE_MOD_FAILED:
alshabib193525b2014-10-08 18:58:03 -0700385 fe = new DefaultFlowEntry(offending, msg.getErrType().ordinal(), 0);
alshabib902d41b2014-10-07 16:52:05 -0700386 break;
387 default:
alshabib193525b2014-10-08 18:58:03 -0700388 log.error("Unknown error type {}", msg.getErrType());
alshabib902d41b2014-10-07 16:52:05 -0700389
390 }
alshabib193525b2014-10-08 18:58:03 -0700391 offendingFlowMods.add(fe);
alshabib902d41b2014-10-07 16:52:05 -0700392
393 }
394
alshabib193525b2014-10-08 18:58:03 -0700395
alshabib902d41b2014-10-07 16:52:05 -0700396 public void satisfyRequirement(Dpid dpid) {
alshabib3effd042014-10-17 12:00:31 -0700397 log.debug("Satisfaction from switch {}", dpid);
alshabib7911a052014-10-16 17:49:37 -0700398 removeRequirement(dpid);
alshabib902d41b2014-10-07 16:52:05 -0700399 }
400
alshabib193525b2014-10-08 18:58:03 -0700401
alshabibdfc7afb2014-10-21 20:13:27 -0700402 public void verify(Long id) {
alshabib193525b2014-10-08 18:58:03 -0700403 pendingXid = id;
alshabib902d41b2014-10-07 16:52:05 -0700404 for (Dpid dpid : sws) {
405 OpenFlowSwitch sw = controller.getSwitch(dpid);
406 OFBarrierRequest.Builder builder = sw.factory()
407 .buildBarrierRequest()
408 .setXid(id);
409 sw.sendMsg(builder.build());
410 }
alshabib902d41b2014-10-07 16:52:05 -0700411 }
412
413 @Override
414 public boolean cancel(boolean mayInterruptIfRunning) {
alshabib7911a052014-10-16 17:49:37 -0700415 ok.set(false);
alshabib193525b2014-10-08 18:58:03 -0700416 this.state = BatchState.CANCELLED;
417 cleanUp();
418 for (FlowRuleBatchEntry fbe : fms.values()) {
419 if (fbe.getOperator() == FlowRuleOperation.ADD ||
420 fbe.getOperator() == FlowRuleOperation.MODIFY) {
421 removeFlowRule(fbe.getTarget());
422 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
423 applyRule(fbe.getTarget());
424 }
425
426 }
427 return isCancelled();
alshabib902d41b2014-10-07 16:52:05 -0700428 }
429
430 @Override
431 public boolean isCancelled() {
alshabib193525b2014-10-08 18:58:03 -0700432 return this.state == BatchState.CANCELLED;
alshabib902d41b2014-10-07 16:52:05 -0700433 }
434
435 @Override
436 public boolean isDone() {
alshabib193525b2014-10-08 18:58:03 -0700437 return this.state == BatchState.FINISHED;
alshabib902d41b2014-10-07 16:52:05 -0700438 }
439
440 @Override
alshabib193525b2014-10-08 18:58:03 -0700441 public CompletedBatchOperation get() throws InterruptedException, ExecutionException {
alshabib902d41b2014-10-07 16:52:05 -0700442 countDownLatch.await();
alshabib193525b2014-10-08 18:58:03 -0700443 this.state = BatchState.FINISHED;
444 return new CompletedBatchOperation(ok.get(), offendingFlowMods);
alshabib902d41b2014-10-07 16:52:05 -0700445 }
446
447 @Override
alshabib193525b2014-10-08 18:58:03 -0700448 public CompletedBatchOperation get(long timeout, TimeUnit unit)
alshabib902d41b2014-10-07 16:52:05 -0700449 throws InterruptedException, ExecutionException,
450 TimeoutException {
alshabib26834582014-10-08 20:15:46 -0700451 if (countDownLatch.await(timeout, unit)) {
452 this.state = BatchState.FINISHED;
453 return new CompletedBatchOperation(ok.get(), offendingFlowMods);
454 }
455 throw new TimeoutException();
alshabib193525b2014-10-08 18:58:03 -0700456 }
457
458 private void cleanUp() {
alshabib7911a052014-10-16 17:49:37 -0700459 if (isDone() || isCancelled()) {
alshabibdfc7afb2014-10-21 20:13:27 -0700460 if (pendingXid != null) {
461 pendingFutures.remove(pendingXid);
462 }
alshabib193525b2014-10-08 18:58:03 -0700463 for (Long xid : fms.keySet()) {
464 pendingFMs.remove(xid);
465 }
466 }
alshabib902d41b2014-10-07 16:52:05 -0700467 }
468
alshabib7911a052014-10-16 17:49:37 -0700469 private void removeRequirement(Dpid dpid) {
470 countDownLatch.countDown();
471 sws.remove(dpid);
472 cleanUp();
473 }
474
alshabib902d41b2014-10-07 16:52:05 -0700475 }
alshabiba68eb962014-09-24 20:34:13 -0700476
alshabib1cc04f72014-09-16 16:09:58 -0700477}