blob: b536fdac4991c57f1604b14398a525be6c764219 [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho5afd02a2015-02-03 20:07:35 -08003 *
4 * 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
7 *
8 * 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.
15 */
16
17package org.onosproject.provider.of.group.impl;
18
dvaddireb57fdb02017-06-20 00:14:08 +053019import static org.onlab.util.Tools.getIntegerProperty;
jiangruia6b46092015-11-11 16:28:51 +080020import static org.slf4j.LoggerFactory.getLogger;
21
helenyrwu89470f12016-08-12 13:18:10 -070022import java.util.ArrayList;
jiangruia6b46092015-11-11 16:28:51 +080023import java.util.Collection;
dvaddireb57fdb02017-06-20 00:14:08 +053024import java.util.Dictionary;
helenyrwu89470f12016-08-12 13:18:10 -070025import java.util.Iterator;
26import java.util.List;
jiangruia6b46092015-11-11 16:28:51 +080027import java.util.Map;
28import java.util.Optional;
dvaddireb57fdb02017-06-20 00:14:08 +053029import java.util.Properties;
jiangruia6b46092015-11-11 16:28:51 +080030import java.util.concurrent.atomic.AtomicLong;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070031
sangho5afd02a2015-02-03 20:07:35 -080032import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Component;
34import org.apache.felix.scr.annotations.Deactivate;
dvaddireb57fdb02017-06-20 00:14:08 +053035import org.apache.felix.scr.annotations.Modified;
36import org.apache.felix.scr.annotations.Property;
sangho5afd02a2015-02-03 20:07:35 -080037import org.apache.felix.scr.annotations.Reference;
38import org.apache.felix.scr.annotations.ReferenceCardinality;
dvaddireb57fdb02017-06-20 00:14:08 +053039import org.onosproject.cfg.ComponentConfigService;
sangho5afd02a2015-02-03 20:07:35 -080040import org.onosproject.core.GroupId;
41import org.onosproject.net.DeviceId;
helenyrwu89470f12016-08-12 13:18:10 -070042import org.onosproject.net.PortNumber;
43import org.onosproject.net.device.DeviceService;
jiangruia6b46092015-11-11 16:28:51 +080044import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080045import org.onosproject.net.group.DefaultGroup;
46import org.onosproject.net.group.Group;
helenyrwu89470f12016-08-12 13:18:10 -070047import org.onosproject.net.group.GroupBucket;
sangho5afd02a2015-02-03 20:07:35 -080048import org.onosproject.net.group.GroupBuckets;
49import org.onosproject.net.group.GroupDescription;
50import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080051import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080052import org.onosproject.net.group.GroupOperations;
53import org.onosproject.net.group.GroupProvider;
54import org.onosproject.net.group.GroupProviderRegistry;
55import org.onosproject.net.group.GroupProviderService;
helenyrwu89470f12016-08-12 13:18:10 -070056import org.onosproject.net.group.GroupService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070057import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080058import org.onosproject.net.provider.AbstractProvider;
59import org.onosproject.net.provider.ProviderId;
60import org.onosproject.openflow.controller.Dpid;
61import org.onosproject.openflow.controller.OpenFlowController;
62import org.onosproject.openflow.controller.OpenFlowEventListener;
63import org.onosproject.openflow.controller.OpenFlowSwitch;
64import org.onosproject.openflow.controller.OpenFlowSwitchListener;
65import org.onosproject.openflow.controller.RoleState;
dvaddireb57fdb02017-06-20 00:14:08 +053066import org.osgi.service.component.ComponentContext;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070067import org.projectfloodlight.openflow.protocol.OFBucketCounter;
sangho5afd02a2015-02-03 20:07:35 -080068import org.projectfloodlight.openflow.protocol.OFErrorMsg;
69import org.projectfloodlight.openflow.protocol.OFErrorType;
70import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
71import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
72import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080073import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080074import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
75import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
76import org.projectfloodlight.openflow.protocol.OFGroupType;
77import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070078import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080079import org.projectfloodlight.openflow.protocol.OFPortStatus;
80import org.projectfloodlight.openflow.protocol.OFStatsReply;
81import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080082import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080083import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080084import org.slf4j.Logger;
85
jiangruia6b46092015-11-11 16:28:51 +080086import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080087
88/**
89 * Provider which uses an OpenFlow controller to handle Group.
90 */
91@Component(immediate = true)
92public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
93
94 private final Logger log = getLogger(getClass());
95
96 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected OpenFlowController controller;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected GroupProviderRegistry providerRegistry;
101
jiangruia6b46092015-11-11 16:28:51 +0800102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected DriverService driverService;
104
helenyrwu89470f12016-08-12 13:18:10 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected DeviceService deviceService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected GroupService groupService;
110
dvaddireb57fdb02017-06-20 00:14:08 +0530111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected ComponentConfigService cfgService;
113
sangho5afd02a2015-02-03 20:07:35 -0800114 private GroupProviderService providerService;
115
dvaddireb57fdb02017-06-20 00:14:08 +0530116 private static final int DEFAULT_POLL_INTERVAL = 10;
117 private static final String COMPONENT = "org.onosproject.provider.of.group.impl.OpenFlowGroupProvider";
118 private static final String GROUP_POLL_INTERVAL_CONST = "groupPollInterval";
119
120 @Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL,
121 label = "Frequency (in seconds) for polling group statistics")
122 private int groupPollInterval = DEFAULT_POLL_INTERVAL;
sangho5afd02a2015-02-03 20:07:35 -0800123
124 private final InternalGroupProvider listener = new InternalGroupProvider();
125
sanghoa09f2742015-02-06 14:49:47 -0800126 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800127 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800128 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800129 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800130 Maps.newConcurrentMap();
131
sanghoa09f2742015-02-06 14:49:47 -0800132 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800133 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800134
sangho5afd02a2015-02-03 20:07:35 -0800135 /**
136 * Creates a OpenFlow group provider.
137 */
138 public OpenFlowGroupProvider() {
139 super(new ProviderId("of", "org.onosproject.provider.group"));
140 }
141
142 @Activate
dvaddireb57fdb02017-06-20 00:14:08 +0530143 public void activate(ComponentContext context) {
144 cfgService.registerProperties(getClass());
sangho5afd02a2015-02-03 20:07:35 -0800145 providerService = providerRegistry.register(this);
146 controller.addListener(listener);
147 controller.addEventListener(listener);
148
dvaddireb57fdb02017-06-20 00:14:08 +0530149 modified(context);
150
sangho5afd02a2015-02-03 20:07:35 -0800151 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800152 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530153 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
sangho01a883c2015-02-23 11:01:50 -0800154 gsc.start();
155 collectors.put(new Dpid(sw.getId()), gsc);
156 }
sangho5afd02a2015-02-03 20:07:35 -0800157 }
158
159 log.info("Started");
160 }
161
162 @Deactivate
163 public void deactivate() {
dvaddireb57fdb02017-06-20 00:14:08 +0530164 cfgService.unregisterProperties(getClass(), false);
sangho5afd02a2015-02-03 20:07:35 -0800165 providerRegistry.unregister(this);
166 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800167 collectors.values().forEach(GroupStatsCollector::stop);
168 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800169 log.info("Stopped");
170 }
171
dvaddireb57fdb02017-06-20 00:14:08 +0530172 @Modified
173 public void modified(ComponentContext context) {
174 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
175 Integer newGroupPollInterval = getIntegerProperty(properties, GROUP_POLL_INTERVAL_CONST);
176 if (newGroupPollInterval != null && newGroupPollInterval > 0
177 && newGroupPollInterval != groupPollInterval) {
178 groupPollInterval = newGroupPollInterval;
179 modifyPollInterval();
180 } else if (newGroupPollInterval != null && newGroupPollInterval <= 0) {
181 log.warn("groupPollInterval must be greater than 0");
182 //If the new value <= 0 reset property with old value.
183 cfgService.setProperty(COMPONENT, GROUP_POLL_INTERVAL_CONST, Integer.toString(groupPollInterval));
184 }
185 }
186
187 private void modifyPollInterval() {
188 collectors.values().forEach(gsc -> gsc.adjustRate(groupPollInterval));
189
190 }
sangho5afd02a2015-02-03 20:07:35 -0800191 @Override
192 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800193 final Dpid dpid = Dpid.dpid(deviceId.uri());
194 OpenFlowSwitch sw = controller.getSwitch(dpid);
195 for (GroupOperation groupOperation: groupOps.operations()) {
196 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800197 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800198 return;
199 }
sanghoa09f2742015-02-06 14:49:47 -0800200 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800201 GroupModBuilder builder = null;
202 if (driverService == null) {
203 builder = GroupModBuilder.builder(groupOperation.buckets(),
204 groupOperation.groupId(),
205 groupOperation.groupType(),
206 sw.factory(),
207 Optional.of(groupModXid));
208 } else {
209 builder = GroupModBuilder.builder(groupOperation.buckets(),
210 groupOperation.groupId(),
211 groupOperation.groupType(),
212 sw.factory(),
213 Optional.of(groupModXid),
214 Optional.of(driverService));
215 }
sangho5afd02a2015-02-03 20:07:35 -0800216 OFGroupMod groupMod = null;
217 switch (groupOperation.opType()) {
218 case ADD:
219 groupMod = builder.buildGroupAdd();
220 break;
221 case MODIFY:
222 groupMod = builder.buildGroupMod();
223 break;
224 case DELETE:
225 groupMod = builder.buildGroupDel();
226 break;
227 default:
228 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530229 return;
sangho5afd02a2015-02-03 20:07:35 -0800230 }
231 sw.sendMsg(groupMod);
Yi Tsengfa394de2017-02-01 11:26:40 -0800232 GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber());
sangho22a805d2015-02-13 09:45:43 -0800233 pendingGroupOperations.put(groudId, groupOperation);
234 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800235 }
236 }
237
238 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
239 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
240
241 OFGroupStatsReply groupStatsReply = null;
242 OFGroupDescStatsReply groupDescStatsReply = null;
243
sanghoa09f2742015-02-06 14:49:47 -0800244 synchronized (groupStats) {
245 if (statsReply.getStatsType() == OFStatsType.GROUP) {
246 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
247 if (reply != null) {
248 groupStatsReply = (OFGroupStatsReply) statsReply;
249 groupDescStatsReply = (OFGroupDescStatsReply) reply;
250 groupStats.remove(statsReply.getXid() + 1);
251 } else {
252 groupStats.put(statsReply.getXid(), statsReply);
253 }
254 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
255 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
256 if (reply != null) {
257 groupStatsReply = (OFGroupStatsReply) reply;
258 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
259 groupStats.remove(statsReply.getXid() - 1);
260 } else {
261 groupStats.put(statsReply.getXid(), statsReply);
262 }
sangho5afd02a2015-02-03 20:07:35 -0800263 }
264 }
265
Ray Milkey0ae473d2016-04-04 10:56:47 -0700266 if (providerService != null && groupStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800267 Collection<Group> groups = buildGroupMetrics(deviceId,
268 groupStatsReply, groupDescStatsReply);
269 providerService.pushGroupMetrics(deviceId, groups);
270 for (Group group: groups) {
271 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800272 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800273 }
274 }
275 }
276
277 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
278 OFGroupStatsReply groupStatsReply,
279 OFGroupDescStatsReply groupDescStatsReply) {
280
281 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800282 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700283
sangho5afd02a2015-02-03 20:07:35 -0800284 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
285 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800286 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800287 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800288 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
289 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800290 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
291 groups.put(id, group);
292 }
293
294 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
295 int groupId = entry.getGroup().getGroupNumber();
296 DefaultGroup group = (DefaultGroup) groups.get(groupId);
297 if (group != null) {
298 group.setBytes(entry.getByteCount().getValue());
299 group.setLife(entry.getDurationSec());
300 group.setPackets(entry.getPacketCount().getValue());
301 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700302 int bucketIndex = 0;
303 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
304 ((StoredGroupBucketEntry) group.buckets().buckets()
305 .get(bucketIndex))
306 .setPackets(bucketStats
307 .getPacketCount().getValue());
308 ((StoredGroupBucketEntry) group.buckets().buckets()
309 .get(bucketIndex))
310 .setBytes(entry.getBucketStats()
311 .get(bucketIndex)
312 .getByteCount().getValue());
313 bucketIndex++;
314 }
sangho5afd02a2015-02-03 20:07:35 -0800315 }
316 }
317
318 return groups.values();
319 }
320
321 private GroupDescription.Type getGroupType(OFGroupType type) {
322 switch (type) {
323 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700324 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800325 case INDIRECT:
326 return GroupDescription.Type.INDIRECT;
327 case SELECT:
328 return GroupDescription.Type.SELECT;
329 case FF:
330 return GroupDescription.Type.FAILOVER;
331 default:
332 log.error("Unsupported OF group type : {}", type);
333 break;
334 }
335 return null;
336 }
337
sanghoa09f2742015-02-06 14:49:47 -0800338 /**
339 * Returns a transaction ID for entire group operations and increases
340 * the counter by the number given.
341 *
342 * @param increase the amount to increase the counter by
343 * @return a transaction ID
344 */
345 public static long getXidAndAdd(int increase) {
346 return XID_COUNTER.getAndAdd(increase);
347 }
348
sangho01a883c2015-02-23 11:01:50 -0800349 private boolean isGroupSupported(OpenFlowSwitch sw) {
350 if (sw.factory().getVersion() == OFVersion.OF_10 ||
351 sw.factory().getVersion() == OFVersion.OF_11 ||
352 sw.factory().getVersion() == OFVersion.OF_12) {
353 return false;
354 }
355
356 return true;
357 }
358
sangho5afd02a2015-02-03 20:07:35 -0800359 private class InternalGroupProvider
360 implements OpenFlowSwitchListener, OpenFlowEventListener {
361
362 @Override
363 public void handleMessage(Dpid dpid, OFMessage msg) {
364 switch (msg.getType()) {
365 case STATS_REPLY:
366 pushGroupMetrics(dpid, (OFStatsReply) msg);
367 break;
368 case ERROR:
369 OFErrorMsg errorMsg = (OFErrorMsg) msg;
370 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800371 GroupId pendingGroupId = null;
372 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800373 if (entry.getValue() == errorMsg.getXid()) {
374 pendingGroupId = entry.getKey();
375 break;
376 }
377 }
sangho22a805d2015-02-13 09:45:43 -0800378 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800379 log.warn("Error for unknown group operation: {}",
380 errorMsg.getXid());
381 } else {
382 GroupOperation operation =
383 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800384 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800385 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800386 OFGroupModFailedCode code =
387 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
388 GroupMsgErrorCode failureCode =
389 GroupMsgErrorCode.values()[(code.ordinal())];
390 GroupOperation failedOperation = GroupOperation
391 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800392 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800393 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800394 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800395 pendingGroupOperations.remove(pendingGroupId);
396 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800397 } else {
398 log.error("Cannot find pending group operation with group ID: {}",
399 pendingGroupId);
400 }
sangho5afd02a2015-02-03 20:07:35 -0800401 }
402 break;
403 }
404 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800405 break;
sangho5afd02a2015-02-03 20:07:35 -0800406 }
407 }
408
409 @Override
410 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800411 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500412 if (sw == null) {
413 return;
414 }
sangho01a883c2015-02-23 11:01:50 -0800415 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530416 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700417 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900418 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800419 }
Lei Xudee1aff2015-10-16 00:45:10 -0500420
421 //figure out race condition
422 if (controller.getSwitch(dpid) == null) {
423 switchRemoved(dpid);
424 }
sangho5afd02a2015-02-03 20:07:35 -0800425 }
426
427 @Override
428 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700429 stopCollectorIfNeeded(collectors.remove(dpid));
430 }
431
432 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800433 if (collector != null) {
434 collector.stop();
435 }
436 }
437
438 @Override
439 public void switchChanged(Dpid dpid) {
440 }
441
442 @Override
443 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700444 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800445 }
446
447 @Override
448 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
449 }
450 }
451
helenyrwu89470f12016-08-12 13:18:10 -0700452 /**
453 * Builds a list of failover Groups whose primary live bucket failed over
454 * (i.e. bucket in use has changed).
455 *
456 * @param dpid DPID of switch whose port's status changed
457 * @param status new status of port
458 * @return list of groups whose primary live bucket failed over
459 */
460 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
461 List<Group> groupList = new ArrayList<>();
462 OFPortDesc desc = status.getDesc();
463 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
464 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
465 if (desc.isEnabled()) {
466 return groupList;
467 }
468 Iterator<Group> iterator = groupService.getGroups(id).iterator();
469 while (iterator.hasNext()) {
470 Group group = iterator.next();
471 if (group.type() == GroupDescription.Type.FAILOVER &&
472 checkFailoverGroup(group, id, portNumber)) {
473 groupList.add(group);
474 }
475 }
476 return groupList;
477 }
478
479 /**
480 * Checks whether the first live port in the failover group's bucket
481 * has failed over.
482 *
483 * @param group failover group to be checked for failover
484 * @param id device ID of switch whose port's status changed
485 * @param portNumber port number of port that was disabled
486 * @return whether the failover group experienced failover
487 */
488 private boolean checkFailoverGroup(Group group, DeviceId id,
489 PortNumber portNumber) {
490 boolean portReached = false;
491 boolean portEnabled = false;
492 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
493 GroupBucket bucket;
494 while (bIterator.hasNext() && !portReached) {
495 bucket = bIterator.next();
496 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
497 portEnabled = true;
498 }
499 if (bucket.watchPort().equals(portNumber)) {
500 portReached = true;
501 }
502 }
503 return portReached && !portEnabled;
504 }
505
sangho5afd02a2015-02-03 20:07:35 -0800506}