blob: 1c4b20ef5c6e627361230a999d22471c9e50fb15 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
alshabib1cc04f72014-09-16 16:09:58 -070017
Jonathan Hart2ffcd102015-01-16 16:47:50 -080018
Brian O'Connor72cb19a2015-01-16 16:14:41 -080019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.cache.RemovalCause;
22import com.google.common.cache.RemovalNotification;
23import com.google.common.collect.Maps;
24import com.google.common.collect.Sets;
alshabib1cc04f72014-09-16 16:09:58 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.CompletedBatchOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.FlowEntry;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080036import org.onosproject.net.flow.FlowRuleBatchOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.flow.FlowRuleProvider;
38import org.onosproject.net.flow.FlowRuleProviderRegistry;
39import org.onosproject.net.flow.FlowRuleProviderService;
40import org.onosproject.net.provider.AbstractProvider;
41import org.onosproject.net.provider.ProviderId;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.openflow.controller.Dpid;
43import org.onosproject.openflow.controller.OpenFlowController;
44import org.onosproject.openflow.controller.OpenFlowEventListener;
45import org.onosproject.openflow.controller.OpenFlowSwitch;
46import org.onosproject.openflow.controller.OpenFlowSwitchListener;
47import org.onosproject.openflow.controller.RoleState;
alshabib902d41b2014-10-07 16:52:05 -070048import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
49import org.projectfloodlight.openflow.protocol.OFErrorMsg;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080050import org.projectfloodlight.openflow.protocol.OFErrorType;
alshabib193525b2014-10-08 18:58:03 -070051import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070052import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070053import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib8f1cf4a2014-09-17 14:44:48 -070054import org.projectfloodlight.openflow.protocol.OFMessage;
55import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070056import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080057import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib193525b2014-10-08 18:58:03 -070058import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib1cc04f72014-09-16 16:09:58 -070059import org.slf4j.Logger;
60
Brian O'Connor72cb19a2015-01-16 16:14:41 -080061import java.util.Collections;
62import java.util.List;
63import java.util.Map;
64import java.util.Optional;
65import java.util.Set;
66import java.util.concurrent.TimeUnit;
67import java.util.stream.Collectors;
68
69import static org.slf4j.LoggerFactory.getLogger;
70
alshabibeec3a062014-09-17 18:01:26 -070071
alshabib1cc04f72014-09-16 16:09:58 -070072/**
73 * Provider which uses an OpenFlow controller to detect network
74 * end-station hosts.
75 */
76@Component(immediate = true)
77public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
78
79 private final Logger log = getLogger(getClass());
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected FlowRuleProviderRegistry providerRegistry;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected OpenFlowController controller;
86
alshabib1cc04f72014-09-16 16:09:58 -070087
88 private FlowRuleProviderService providerService;
89
alshabibeec3a062014-09-17 18:01:26 -070090 private final InternalFlowProvider listener = new InternalFlowProvider();
91
Brian O'Connor72cb19a2015-01-16 16:14:41 -080092 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -070093
alshabib3d643ec2014-10-22 18:33:00 -070094 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
95
Thomas Vachuska9b2da212014-11-10 19:30:25 -080096
alshabib1cc04f72014-09-16 16:09:58 -070097 /**
98 * Creates an OpenFlow host provider.
99 */
100 public OpenFlowRuleProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800101 super(new ProviderId("of", "org.onosproject.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700102 }
103
104 @Activate
105 public void activate() {
106 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700107 controller.addListener(listener);
108 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700109
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110 pendingBatches = CacheBuilder.newBuilder()
111 .expireAfterWrite(10, TimeUnit.SECONDS)
112 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
113 if (notification.getCause() == RemovalCause.EXPIRED) {
114 providerService.batchOperationCompleted(notification.getKey(),
115 notification.getValue().failedCompletion());
116 }
117 }).build();
118
119
alshabib3d643ec2014-10-22 18:33:00 -0700120 for (OpenFlowSwitch sw : controller.getSwitches()) {
121 FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL);
122 fsc.start();
123 collectors.put(new Dpid(sw.getId()), fsc);
124 }
125
126
alshabib1cc04f72014-09-16 16:09:58 -0700127 log.info("Started");
128 }
129
130 @Deactivate
131 public void deactivate() {
132 providerRegistry.unregister(this);
133 providerService = null;
134
135 log.info("Stopped");
136 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800137
alshabib1cc04f72014-09-16 16:09:58 -0700138 @Override
139 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140 for (FlowRule flowRule : flowRules) {
141 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700142 }
alshabib1cc04f72014-09-16 16:09:58 -0700143 }
144
alshabib35edb1a2014-09-16 17:44:44 -0700145 private void applyRule(FlowRule flowRule) {
146 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
sangho87af8112015-01-29 12:53:08 -0800147 if (flowRule.type() == FlowRule.Type.DEFAULT) {
148 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
149 Optional.empty()).buildFlowAdd());
150 } else {
alshabib9af70072015-02-09 14:34:16 -0800151 OpenFlowSwitch.TableType type = getTableType(flowRule.type());
Saurav Dasfa2fa932015-03-03 11:29:48 -0800152 sw.transformAndSendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
alshabib9af70072015-02-09 14:34:16 -0800153 Optional.empty()).buildFlowAdd(),
154 type);
sangho87af8112015-01-29 12:53:08 -0800155 }
alshabib35edb1a2014-09-16 17:44:44 -0700156 }
157
alshabib35edb1a2014-09-16 17:44:44 -0700158
alshabib1cc04f72014-09-16 16:09:58 -0700159 @Override
160 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800161 for (FlowRule flowRule : flowRules) {
162 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700163 }
alshabib1cc04f72014-09-16 16:09:58 -0700164
165 }
166
alshabib219ebaa2014-09-22 15:41:24 -0700167 private void removeRule(FlowRule flowRule) {
168 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
sangho87af8112015-01-29 12:53:08 -0800169 if (flowRule.type() == FlowRule.Type.DEFAULT) {
170 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
171 Optional.empty()).buildFlowDel());
172 } else {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800173 sw.transformAndSendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
sangho87af8112015-01-29 12:53:08 -0800174 Optional.empty()).buildFlowDel(), getTableType(flowRule.type()));
175 }
alshabib219ebaa2014-09-22 15:41:24 -0700176 }
177
alshabiba68eb962014-09-24 20:34:13 -0700178 @Override
179 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
180 // TODO: optimize using the ApplicationId
181 removeFlowRule(flowRules);
182 }
183
alshabib193525b2014-10-08 18:58:03 -0700184 @Override
sangho87af8112015-01-29 12:53:08 -0800185
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800186 public void executeBatch(FlowRuleBatchOperation batch) {
187
188 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
189
190
191 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(batch.deviceId().uri()));
192 OFFlowMod mod;
193
alshabib193525b2014-10-08 18:58:03 -0700194 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800195
Brian O'Connor427a1762014-11-19 18:40:32 -0800196 FlowModBuilder builder =
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800197 FlowModBuilder.builder(fbe.target(), sw.factory(),
198 Optional.of(batch.id()));
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800199 switch (fbe.operator()) {
alshabib193525b2014-10-08 18:58:03 -0700200 case ADD:
201 mod = builder.buildFlowAdd();
202 break;
203 case REMOVE:
204 mod = builder.buildFlowDel();
205 break;
206 case MODIFY:
207 mod = builder.buildFlowMod();
208 break;
209 default:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800210 log.error("Unsupported batch operation {}; skipping flowmod {}",
211 fbe.operator(), fbe);
212 continue;
213 }
sangho22a805d2015-02-13 09:45:43 -0800214 if (fbe.target().type() == FlowRule.Type.DEFAULT) {
215 sw.sendMsg(mod);
216 } else {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800217 sw.transformAndSendMsg(mod, getTableType(fbe.target().type()));
sangho22a805d2015-02-13 09:45:43 -0800218 }
alshabib193525b2014-10-08 18:58:03 -0700219 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800220 OFBarrierRequest.Builder builder = sw.factory()
221 .buildBarrierRequest()
222 .setXid(batch.id());
223 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700224 }
225
sangho87af8112015-01-29 12:53:08 -0800226 private OpenFlowSwitch.TableType getTableType(FlowRule.Type type) {
227 switch (type) {
alshabib9af70072015-02-09 14:34:16 -0800228
229 case DEFAULT:
230 return OpenFlowSwitch.TableType.NONE;
sangho87af8112015-01-29 12:53:08 -0800231 case IP:
232 return OpenFlowSwitch.TableType.IP;
233 case MPLS:
234 return OpenFlowSwitch.TableType.MPLS;
235 case ACL:
236 return OpenFlowSwitch.TableType.ACL;
alshabib9af70072015-02-09 14:34:16 -0800237 case VLAN_MPLS:
238 return OpenFlowSwitch.TableType.VLAN_MPLS;
239 case VLAN:
240 return OpenFlowSwitch.TableType.VLAN;
241 case ETHER:
242 return OpenFlowSwitch.TableType.ETHER;
243 case COS:
244 return OpenFlowSwitch.TableType.COS;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800245 case FIRST:
246 return OpenFlowSwitch.TableType.FIRST;
sangho87af8112015-01-29 12:53:08 -0800247 default:
248 return OpenFlowSwitch.TableType.NONE;
249 }
250 }
alshabib193525b2014-10-08 18:58:03 -0700251
Saurav Dasfa2fa932015-03-03 11:29:48 -0800252 private FlowRule.Type getType(OpenFlowSwitch.TableType tableType) {
253 switch (tableType) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800254
Saurav Dasfa2fa932015-03-03 11:29:48 -0800255 case NONE:
256 return FlowRule.Type.DEFAULT;
257 case IP:
258 return FlowRule.Type.IP;
259 case MPLS:
260 return FlowRule.Type.MPLS;
261 case ACL:
262 return FlowRule.Type.ACL;
263 case VLAN_MPLS:
264 return FlowRule.Type.VLAN_MPLS;
265 case VLAN:
266 return FlowRule.Type.VLAN;
267 case ETHER:
268 return FlowRule.Type.ETHER;
269 case COS:
270 return FlowRule.Type.COS;
271 case FIRST:
272 return FlowRule.Type.FIRST;
273 default:
274 return FlowRule.Type.DEFAULT;
275 }
276 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800277
278
alshabib8f1cf4a2014-09-17 14:44:48 -0700279 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800280 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700281
alshabib8f1cf4a2014-09-17 14:44:48 -0700282 @Override
283 public void switchAdded(Dpid dpid) {
alshabibba5ac482014-10-02 17:15:20 -0700284 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
alshabibeec3a062014-09-17 18:01:26 -0700285 fsc.start();
286 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700287 }
288
289 @Override
290 public void switchRemoved(Dpid dpid) {
alshabibdfc7afb2014-10-21 20:13:27 -0700291 FlowStatsCollector collector = collectors.remove(dpid);
292 if (collector != null) {
293 collector.stop();
294 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700295 }
296
297 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700298 public void switchChanged(Dpid dpid) {
299 }
300
301 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700302 public void portChanged(Dpid dpid, OFPortStatus status) {
303 //TODO: Decide whether to evict flows internal store.
304 }
305
306 @Override
307 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabibda1644e2015-03-13 14:01:35 -0700308 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib8f1cf4a2014-09-17 14:44:48 -0700309 switch (msg.getType()) {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800310 case FLOW_REMOVED:
311 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700312
alshabibda1644e2015-03-13 14:01:35 -0700313 FlowEntry fr = new FlowEntryBuilder(dpid, removed,
314 getType(sw.getTableType(removed.getTableId()))).build();
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800315 providerService.flowRemoved(fr);
316 break;
317 case STATS_REPLY:
sangho20f91162015-02-12 11:24:23 -0800318 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
sangho89bf6fb2015-02-09 09:33:13 -0800319 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
320 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800321 break;
322 case BARRIER_REPLY:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800323 try {
324 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
325 if (entry != null) {
326 providerService.batchOperationCompleted(msg.getXid(), entry.completed());
327 } else {
328 log.warn("Received unknown Barrier Reply: {}", msg.getXid());
329 }
330 } finally {
331 pendingBatches.invalidate(msg.getXid());
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800332 }
333 break;
334 case ERROR:
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800335 log.warn("received Error message {} from {}", msg, dpid);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800336
337 OFErrorMsg error = (OFErrorMsg) msg;
338 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
339 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
340 if (fmFailed.getData().getParsedMessage().isPresent()) {
341 OFMessage m = fmFailed.getData().getParsedMessage().get();
342 OFFlowMod fm = (OFFlowMod) m;
343 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
344 if (entry != null) {
alshabibda1644e2015-03-13 14:01:35 -0700345 entry.appendFailure(new FlowEntryBuilder(dpid, fm,
346 getType(sw.getTableType(fm.getTableId())))
347 .build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800348 } else {
349 log.error("No matching batch for this error: {}", error);
350 }
351 } else {
352 //FIXME: Potentially add flowtracking to avoid this message.
353 log.error("Flow installation failed but switch didn't" +
354 " tell us which one.");
355 }
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800356 } else {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800357 log.warn("Received error {}", error);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800358 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800359
360
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800361 default:
362 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700363 }
364
365 }
366
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700367 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700368 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800369 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700370 // Do nothing here for now.
371 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700372
sangho89bf6fb2015-02-09 09:33:13 -0800373 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800374
alshabib54ce5892014-09-23 17:50:51 -0700375 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
Saurav Dasfa2fa932015-03-03 11:29:48 -0800376 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib54ce5892014-09-23 17:50:51 -0700377
alshabib64def642014-12-02 23:27:37 -0800378 List<FlowEntry> flowEntries = replies.getEntries().stream()
Saurav Dasfa2fa932015-03-03 11:29:48 -0800379 .map(entry -> new FlowEntryBuilder(dpid, entry,
380 getType(sw.getTableType(entry.getTableId())))
381 .build())
alshabib64def642014-12-02 23:27:37 -0800382 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700383
alshabib64def642014-12-02 23:27:37 -0800384 providerService.pushFlowMetrics(did, flowEntries);
385
alshabib5c370ff2014-09-18 10:12:14 -0700386 }
387
alshabib8f1cf4a2014-09-17 14:44:48 -0700388 }
alshabib1cc04f72014-09-16 16:09:58 -0700389
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800390 /**
391 * The internal cache entry holding the original request as well
392 * as accumulating the any failures along the way.
393 *
394 * If this entry is evicted from the cache then the entire operation
395 * is considered failed. Otherwise, only the failures reported by the device
396 * will be propagated up.
397 */
398 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700399
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800400 private final FlowRuleBatchOperation operation;
401 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700402
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800403 public InternalCacheEntry(FlowRuleBatchOperation operation) {
404 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700405 }
406
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800407 /**
408 * Appends a failed rule to the set of failed items.
409 * @param rule the failed rule
410 */
411 public void appendFailure(FlowRule rule) {
412 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800413 }
414
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800415 /**
416 * Fails the entire batch and returns the failed operation.
417 * @return the failed operation
418 */
419 public CompletedBatchOperation failedCompletion() {
420 Set<FlowRule> fails = operation.getOperations().stream()
421 .map(op -> op.target()).collect(Collectors.toSet());
422 return new CompletedBatchOperation(false, Collections.unmodifiableSet(fails), operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700423 }
424
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800425 /**
426 * Returns the completed operation and whether the batch suceeded.
427 * @return the completed operation
428 */
429 public CompletedBatchOperation completed() {
430 return new CompletedBatchOperation(failures.isEmpty(),
431 Collections.unmodifiableSet(failures), operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700432 }
433
alshabib902d41b2014-10-07 16:52:05 -0700434 }
alshabiba68eb962014-09-24 20:34:13 -0700435
alshabib1cc04f72014-09-16 16:09:58 -0700436}