blob: 08c87e16dba68700badcd679a9bd7f24e1833a6b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
jcc3d4e14a2015-04-21 11:32:05 +08002 * 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
Thomas Vachuska75aaa672015-04-29 12:24:43 -070018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
22import com.google.common.collect.Maps;
23import com.google.common.collect.Sets;
alshabib1cc04f72014-09-16 16:09:58 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070027import org.apache.felix.scr.annotations.Modified;
28import org.apache.felix.scr.annotations.Property;
alshabib1cc04f72014-09-16 16:09:58 -070029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070031import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.core.ApplicationId;
33import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.CompletedBatchOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080038import org.onosproject.net.flow.FlowRuleBatchOperation;
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -070039import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.FlowRuleProvider;
41import org.onosproject.net.flow.FlowRuleProviderRegistry;
42import org.onosproject.net.flow.FlowRuleProviderService;
43import org.onosproject.net.provider.AbstractProvider;
44import org.onosproject.net.provider.ProviderId;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070045import org.onosproject.net.statistic.DefaultLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.openflow.controller.Dpid;
47import org.onosproject.openflow.controller.OpenFlowController;
48import org.onosproject.openflow.controller.OpenFlowEventListener;
49import org.onosproject.openflow.controller.OpenFlowSwitch;
50import org.onosproject.openflow.controller.OpenFlowSwitchListener;
51import org.onosproject.openflow.controller.RoleState;
jcc3d4e14a2015-04-21 11:32:05 +080052import org.onosproject.openflow.controller.ThirdPartyMessage;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070053import org.osgi.service.component.ComponentContext;
alshabib902d41b2014-10-07 16:52:05 -070054import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
55import org.projectfloodlight.openflow.protocol.OFErrorMsg;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080056import org.projectfloodlight.openflow.protocol.OFErrorType;
alshabib193525b2014-10-08 18:58:03 -070057import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070058import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070059import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib8f1cf4a2014-09-17 14:44:48 -070060import org.projectfloodlight.openflow.protocol.OFMessage;
61import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070062import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080063import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib193525b2014-10-08 18:58:03 -070064import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib1cc04f72014-09-16 16:09:58 -070065import org.slf4j.Logger;
66
Thomas Vachuska75aaa672015-04-29 12:24:43 -070067import java.util.Collections;
68import java.util.Dictionary;
69import java.util.List;
70import java.util.Map;
71import java.util.Optional;
72import java.util.Set;
73import java.util.Timer;
74import java.util.concurrent.TimeUnit;
75import java.util.stream.Collectors;
76
77import static com.google.common.base.Strings.isNullOrEmpty;
78import static org.onlab.util.Tools.get;
79import static org.slf4j.LoggerFactory.getLogger;
alshabibeec3a062014-09-17 18:01:26 -070080
alshabib1cc04f72014-09-16 16:09:58 -070081/**
jcc3d4e14a2015-04-21 11:32:05 +080082 * Provider which uses an OpenFlow controller to detect network end-station
83 * hosts.
alshabib1cc04f72014-09-16 16:09:58 -070084 */
85@Component(immediate = true)
jcc3d4e14a2015-04-21 11:32:05 +080086public class OpenFlowRuleProvider extends AbstractProvider
87 implements FlowRuleProvider {
alshabib1cc04f72014-09-16 16:09:58 -070088
89 private final Logger log = getLogger(getClass());
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected FlowRuleProviderRegistry providerRegistry;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected OpenFlowController controller;
96
Thomas Vachuska75aaa672015-04-29 12:24:43 -070097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected ComponentConfigService cfgService;
99
100 private static final int DEFAULT_POLL_FREQUENCY = 10;
101 @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
102 label = "Frequency (in seconds) for polling flow statistics")
103 private int flowPollFrequency = DEFAULT_POLL_FREQUENCY;
104
alshabib1cc04f72014-09-16 16:09:58 -0700105 private FlowRuleProviderService providerService;
106
alshabibeec3a062014-09-17 18:01:26 -0700107 private final InternalFlowProvider listener = new InternalFlowProvider();
108
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800109 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -0700110
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700111 private final Timer timer = new Timer("onos-openflow-collector");
alshabib3d643ec2014-10-22 18:33:00 -0700112 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
113
alshabib1cc04f72014-09-16 16:09:58 -0700114 /**
115 * Creates an OpenFlow host provider.
116 */
117 public OpenFlowRuleProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800118 super(new ProviderId("of", "org.onosproject.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700119 }
120
121 @Activate
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700122 public void activate(ComponentContext context) {
123 cfgService.registerProperties(getClass());
alshabib1cc04f72014-09-16 16:09:58 -0700124 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700125 controller.addListener(listener);
126 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700127
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700128 pendingBatches = createBatchCache();
129 createCollectors();
alshabib3d643ec2014-10-22 18:33:00 -0700130
alshabib1cc04f72014-09-16 16:09:58 -0700131 log.info("Started");
132 }
133
134 @Deactivate
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700135 public void deactivate(ComponentContext context) {
136 cfgService.unregisterProperties(getClass(), false);
137 stopCollectors();
alshabib1cc04f72014-09-16 16:09:58 -0700138 providerRegistry.unregister(this);
139 providerService = null;
140
141 log.info("Stopped");
142 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800143
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700144 @Modified
145 public void modified(ComponentContext context) {
146 Dictionary<?, ?> properties = context.getProperties();
147 int newFlowPollFrequency;
148 try {
149 String s = get(properties, "flowPollFrequency");
150 newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim());
151
152 } catch (NumberFormatException | ClassCastException e) {
153 newFlowPollFrequency = flowPollFrequency;
154 }
155
156 if (newFlowPollFrequency != flowPollFrequency) {
157 flowPollFrequency = newFlowPollFrequency;
158 adjustRate();
159 }
160
161 log.info("Settings: flowPollFrequency={}", flowPollFrequency);
162 }
163
164 private Cache<Long, InternalCacheEntry> createBatchCache() {
165 return CacheBuilder.newBuilder()
166 .expireAfterWrite(10, TimeUnit.SECONDS)
167 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
168 if (notification.getCause() == RemovalCause.EXPIRED) {
169 providerService.batchOperationCompleted(notification.getKey(),
170 notification.getValue().failedCompletion());
171 }
172 }).build();
173 }
174
175 private void createCollectors() {
176 controller.getSwitches().forEach(this::createCollector);
177 }
178
179 private void createCollector(OpenFlowSwitch sw) {
180 FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
181 fsc.start();
182 collectors.put(new Dpid(sw.getId()), fsc);
183 }
184
185 private void stopCollectors() {
186 collectors.values().forEach(FlowStatsCollector::stop);
187 collectors.clear();
188 }
189
190 private void adjustRate() {
191 DefaultLoad.setPollInterval(flowPollFrequency);
192 collectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
193 }
194
alshabib1cc04f72014-09-16 16:09:58 -0700195 @Override
196 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800197 for (FlowRule flowRule : flowRules) {
198 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700199 }
alshabib1cc04f72014-09-16 16:09:58 -0700200 }
201
alshabib35edb1a2014-09-16 17:44:44 -0700202 private void applyRule(FlowRule flowRule) {
jcc3d4e14a2015-04-21 11:32:05 +0800203 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId()
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700204 .uri()));
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700205 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
206 if (hasPayload(flowRuleExtPayLoad)) {
207 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800208 sw.sendMsg(msg);
209 return;
210 }
alshabibbdcbb102015-04-22 14:16:38 -0700211 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
jcc3d4e14a2015-04-21 11:32:05 +0800212 Optional.empty()).buildFlowAdd());
alshabib35edb1a2014-09-16 17:44:44 -0700213 }
214
alshabib1cc04f72014-09-16 16:09:58 -0700215 @Override
216 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800217 for (FlowRule flowRule : flowRules) {
218 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700219 }
alshabib1cc04f72014-09-16 16:09:58 -0700220 }
221
alshabib219ebaa2014-09-22 15:41:24 -0700222 private void removeRule(FlowRule flowRule) {
jcc3d4e14a2015-04-21 11:32:05 +0800223 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId()
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700224 .uri()));
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700225 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
226 if (hasPayload(flowRuleExtPayLoad)) {
227 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800228 sw.sendMsg(msg);
229 return;
230 }
alshabibbdcbb102015-04-22 14:16:38 -0700231 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
jcc3d4e14a2015-04-21 11:32:05 +0800232 Optional.empty()).buildFlowDel());
alshabib219ebaa2014-09-22 15:41:24 -0700233 }
234
alshabiba68eb962014-09-24 20:34:13 -0700235 @Override
236 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
237 // TODO: optimize using the ApplicationId
238 removeFlowRule(flowRules);
239 }
240
alshabib193525b2014-10-08 18:58:03 -0700241 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800242 public void executeBatch(FlowRuleBatchOperation batch) {
243
244 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
245
jcc3d4e14a2015-04-21 11:32:05 +0800246 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(batch.deviceId()
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700247 .uri()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800248 OFFlowMod mod;
alshabib193525b2014-10-08 18:58:03 -0700249 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
jcc3d4e14a2015-04-21 11:32:05 +0800250 // flow is the third party privacy flow
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700251
252 FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
253 if (hasPayload(flowRuleExtPayLoad)) {
254 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800255 sw.sendMsg(msg);
256 continue;
257 }
258 FlowModBuilder builder = FlowModBuilder.builder(fbe.target(), sw
259 .factory(), Optional.of(batch.id()));
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800260 switch (fbe.operator()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700261 case ADD:
262 mod = builder.buildFlowAdd();
263 break;
264 case REMOVE:
265 mod = builder.buildFlowDel();
266 break;
267 case MODIFY:
268 mod = builder.buildFlowMod();
269 break;
270 default:
271 log.error("Unsupported batch operation {}; skipping flowmod {}",
272 fbe.operator(), fbe);
273 continue;
jcc3d4e14a2015-04-21 11:32:05 +0800274 }
Saurav Das3ea46622015-04-22 14:01:34 -0700275 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700276 }
jcc3d4e14a2015-04-21 11:32:05 +0800277 OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest()
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800278 .setXid(batch.id());
279 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700280 }
281
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700282 private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
283 return flowRuleExtPayLoad != null &&
284 flowRuleExtPayLoad.payLoad() != null &&
285 flowRuleExtPayLoad.payLoad().length > 0;
286 }
287
alshabib8f1cf4a2014-09-17 14:44:48 -0700288 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800289 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700290
alshabib8f1cf4a2014-09-17 14:44:48 -0700291 @Override
292 public void switchAdded(Dpid dpid) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700293 createCollector(controller.getSwitch(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700294 }
295
296 @Override
297 public void switchRemoved(Dpid dpid) {
alshabibdfc7afb2014-10-21 20:13:27 -0700298 FlowStatsCollector collector = collectors.remove(dpid);
299 if (collector != null) {
300 collector.stop();
301 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700302 }
303
304 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700305 public void switchChanged(Dpid dpid) {
306 }
307
308 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700309 public void portChanged(Dpid dpid, OFPortStatus status) {
jcc3d4e14a2015-04-21 11:32:05 +0800310 // TODO: Decide whether to evict flows internal store.
alshabib8f1cf4a2014-09-17 14:44:48 -0700311 }
312
313 @Override
314 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabibda1644e2015-03-13 14:01:35 -0700315 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib8f1cf4a2014-09-17 14:44:48 -0700316 switch (msg.getType()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700317 case FLOW_REMOVED:
318 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700319
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700320 FlowEntry fr = new FlowEntryBuilder(dpid, removed).build();
321 providerService.flowRemoved(fr);
322 break;
323 case STATS_REPLY:
324 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
325 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
sangho89bf6fb2015-02-09 09:33:13 -0800326 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700327 break;
328 case BARRIER_REPLY:
329 try {
330 InternalCacheEntry entry = pendingBatches.getIfPresent(msg
331 .getXid());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800332 if (entry != null) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700333 providerService
334 .batchOperationCompleted(msg.getXid(),
335 entry.completed());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800336 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700337 log.warn("Received unknown Barrier Reply: {}",
338 msg.getXid());
339 }
340 } finally {
341 pendingBatches.invalidate(msg.getXid());
342 }
343 break;
344 case ERROR:
345 log.warn("received Error message {} from {}", msg, dpid);
346
347 OFErrorMsg error = (OFErrorMsg) msg;
348 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
349 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
350 if (fmFailed.getData().getParsedMessage().isPresent()) {
351 OFMessage m = fmFailed.getData().getParsedMessage()
352 .get();
353 OFFlowMod fm = (OFFlowMod) m;
354 InternalCacheEntry entry = pendingBatches
355 .getIfPresent(msg.getXid());
356 if (entry != null) {
357 entry.appendFailure(new FlowEntryBuilder(dpid, fm)
358 .build());
359 } else {
360 log.error("No matching batch for this error: {}",
361 error);
362 }
363 } else {
364 // FIXME: Potentially add flowtracking to avoid this
365 // message.
366 log.error("Flow installation failed but switch didn't"
367 + " tell us which one.");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800368 }
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800369 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700370 log.warn("Received error {}", error);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800371 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800372
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700373 default:
374 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700375 }
376
377 }
378
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700379 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700380 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800381 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700382 // Do nothing here for now.
383 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700384
sangho89bf6fb2015-02-09 09:33:13 -0800385 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800386
alshabib54ce5892014-09-23 17:50:51 -0700387 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
Saurav Dasfa2fa932015-03-03 11:29:48 -0800388 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib54ce5892014-09-23 17:50:51 -0700389
alshabib64def642014-12-02 23:27:37 -0800390 List<FlowEntry> flowEntries = replies.getEntries().stream()
alshabibbdcbb102015-04-22 14:16:38 -0700391 .map(entry -> new FlowEntryBuilder(dpid, entry).build())
alshabib64def642014-12-02 23:27:37 -0800392 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700393
alshabib64def642014-12-02 23:27:37 -0800394 providerService.pushFlowMetrics(did, flowEntries);
alshabib5c370ff2014-09-18 10:12:14 -0700395 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700396 }
alshabib1cc04f72014-09-16 16:09:58 -0700397
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800398 /**
jcc3d4e14a2015-04-21 11:32:05 +0800399 * The internal cache entry holding the original request as well as
400 * accumulating the any failures along the way.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700401 * <p/>
jcc3d4e14a2015-04-21 11:32:05 +0800402 * If this entry is evicted from the cache then the entire operation is
403 * considered failed. Otherwise, only the failures reported by the device
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800404 * will be propagated up.
405 */
406 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700407
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800408 private final FlowRuleBatchOperation operation;
409 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700410
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800411 public InternalCacheEntry(FlowRuleBatchOperation operation) {
412 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700413 }
414
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800415 /**
416 * Appends a failed rule to the set of failed items.
jcc3d4e14a2015-04-21 11:32:05 +0800417 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800418 * @param rule the failed rule
419 */
420 public void appendFailure(FlowRule rule) {
421 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800422 }
423
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800424 /**
425 * Fails the entire batch and returns the failed operation.
jcc3d4e14a2015-04-21 11:32:05 +0800426 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800427 * @return the failed operation
428 */
429 public CompletedBatchOperation failedCompletion() {
430 Set<FlowRule> fails = operation.getOperations().stream()
431 .map(op -> op.target()).collect(Collectors.toSet());
jcc3d4e14a2015-04-21 11:32:05 +0800432 return new CompletedBatchOperation(false,
433 Collections
434 .unmodifiableSet(fails),
435 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700436 }
437
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800438 /**
439 * Returns the completed operation and whether the batch suceeded.
jcc3d4e14a2015-04-21 11:32:05 +0800440 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800441 * @return the completed operation
442 */
443 public CompletedBatchOperation completed() {
jcc3d4e14a2015-04-21 11:32:05 +0800444 return new CompletedBatchOperation(
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700445 failures.isEmpty(),
446 Collections
447 .unmodifiableSet(failures),
448 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700449 }
alshabib902d41b2014-10-07 16:52:05 -0700450 }
alshabiba68eb962014-09-24 20:34:13 -0700451
alshabib1cc04f72014-09-16 16:09:58 -0700452}