blob: 0f55b41ce4be47ccd6a11f717e9b1134b6638ce1 [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;
Laszlo Pappc893b272018-01-03 17:57:43 +000068import org.projectfloodlight.openflow.protocol.OFCapabilities;
sangho5afd02a2015-02-03 20:07:35 -080069import org.projectfloodlight.openflow.protocol.OFErrorMsg;
70import org.projectfloodlight.openflow.protocol.OFErrorType;
71import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
72import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
73import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080074import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080075import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
76import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
77import org.projectfloodlight.openflow.protocol.OFGroupType;
78import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070079import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080080import org.projectfloodlight.openflow.protocol.OFPortStatus;
81import org.projectfloodlight.openflow.protocol.OFStatsReply;
82import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080083import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080084import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080085import org.slf4j.Logger;
86
jiangruia6b46092015-11-11 16:28:51 +080087import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080088
89/**
90 * Provider which uses an OpenFlow controller to handle Group.
91 */
92@Component(immediate = true)
93public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
94
95 private final Logger log = getLogger(getClass());
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected OpenFlowController controller;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected GroupProviderRegistry providerRegistry;
102
jiangruia6b46092015-11-11 16:28:51 +0800103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected DriverService driverService;
105
helenyrwu89470f12016-08-12 13:18:10 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected DeviceService deviceService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected GroupService groupService;
111
dvaddireb57fdb02017-06-20 00:14:08 +0530112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected ComponentConfigService cfgService;
114
sangho5afd02a2015-02-03 20:07:35 -0800115 private GroupProviderService providerService;
116
dvaddireb57fdb02017-06-20 00:14:08 +0530117 private static final int DEFAULT_POLL_INTERVAL = 10;
118 private static final String COMPONENT = "org.onosproject.provider.of.group.impl.OpenFlowGroupProvider";
119 private static final String GROUP_POLL_INTERVAL_CONST = "groupPollInterval";
120
121 @Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL,
122 label = "Frequency (in seconds) for polling group statistics")
123 private int groupPollInterval = DEFAULT_POLL_INTERVAL;
sangho5afd02a2015-02-03 20:07:35 -0800124
125 private final InternalGroupProvider listener = new InternalGroupProvider();
126
sanghoa09f2742015-02-06 14:49:47 -0800127 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800128 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800129 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800130 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800131 Maps.newConcurrentMap();
132
sanghoa09f2742015-02-06 14:49:47 -0800133 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800134 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800135
sangho5afd02a2015-02-03 20:07:35 -0800136 /**
137 * Creates a OpenFlow group provider.
138 */
139 public OpenFlowGroupProvider() {
140 super(new ProviderId("of", "org.onosproject.provider.group"));
141 }
142
143 @Activate
dvaddireb57fdb02017-06-20 00:14:08 +0530144 public void activate(ComponentContext context) {
145 cfgService.registerProperties(getClass());
sangho5afd02a2015-02-03 20:07:35 -0800146 providerService = providerRegistry.register(this);
147 controller.addListener(listener);
148 controller.addEventListener(listener);
149
dvaddireb57fdb02017-06-20 00:14:08 +0530150 modified(context);
151
sangho5afd02a2015-02-03 20:07:35 -0800152 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800153 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530154 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
sangho01a883c2015-02-23 11:01:50 -0800155 gsc.start();
156 collectors.put(new Dpid(sw.getId()), gsc);
157 }
sangho5afd02a2015-02-03 20:07:35 -0800158 }
159
160 log.info("Started");
161 }
162
163 @Deactivate
164 public void deactivate() {
dvaddireb57fdb02017-06-20 00:14:08 +0530165 cfgService.unregisterProperties(getClass(), false);
sangho5afd02a2015-02-03 20:07:35 -0800166 providerRegistry.unregister(this);
167 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800168 collectors.values().forEach(GroupStatsCollector::stop);
169 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800170 log.info("Stopped");
171 }
172
dvaddireb57fdb02017-06-20 00:14:08 +0530173 @Modified
174 public void modified(ComponentContext context) {
175 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
176 Integer newGroupPollInterval = getIntegerProperty(properties, GROUP_POLL_INTERVAL_CONST);
177 if (newGroupPollInterval != null && newGroupPollInterval > 0
178 && newGroupPollInterval != groupPollInterval) {
179 groupPollInterval = newGroupPollInterval;
180 modifyPollInterval();
181 } else if (newGroupPollInterval != null && newGroupPollInterval <= 0) {
182 log.warn("groupPollInterval must be greater than 0");
183 //If the new value <= 0 reset property with old value.
184 cfgService.setProperty(COMPONENT, GROUP_POLL_INTERVAL_CONST, Integer.toString(groupPollInterval));
185 }
186 }
187
188 private void modifyPollInterval() {
189 collectors.values().forEach(gsc -> gsc.adjustRate(groupPollInterval));
190
191 }
sangho5afd02a2015-02-03 20:07:35 -0800192 @Override
193 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800194 final Dpid dpid = Dpid.dpid(deviceId.uri());
195 OpenFlowSwitch sw = controller.getSwitch(dpid);
196 for (GroupOperation groupOperation: groupOps.operations()) {
197 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800198 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800199 return;
200 }
sanghoa09f2742015-02-06 14:49:47 -0800201 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800202 GroupModBuilder builder = null;
203 if (driverService == null) {
204 builder = GroupModBuilder.builder(groupOperation.buckets(),
205 groupOperation.groupId(),
206 groupOperation.groupType(),
207 sw.factory(),
208 Optional.of(groupModXid));
209 } else {
210 builder = GroupModBuilder.builder(groupOperation.buckets(),
211 groupOperation.groupId(),
212 groupOperation.groupType(),
213 sw.factory(),
214 Optional.of(groupModXid),
215 Optional.of(driverService));
216 }
sangho5afd02a2015-02-03 20:07:35 -0800217 OFGroupMod groupMod = null;
218 switch (groupOperation.opType()) {
219 case ADD:
220 groupMod = builder.buildGroupAdd();
221 break;
222 case MODIFY:
223 groupMod = builder.buildGroupMod();
224 break;
225 case DELETE:
226 groupMod = builder.buildGroupDel();
227 break;
228 default:
229 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530230 return;
sangho5afd02a2015-02-03 20:07:35 -0800231 }
232 sw.sendMsg(groupMod);
Yi Tsengfa394de2017-02-01 11:26:40 -0800233 GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber());
sangho22a805d2015-02-13 09:45:43 -0800234 pendingGroupOperations.put(groudId, groupOperation);
235 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800236 }
237 }
238
239 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
240 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
241
242 OFGroupStatsReply groupStatsReply = null;
243 OFGroupDescStatsReply groupDescStatsReply = null;
244
sanghoa09f2742015-02-06 14:49:47 -0800245 synchronized (groupStats) {
246 if (statsReply.getStatsType() == OFStatsType.GROUP) {
247 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
248 if (reply != null) {
249 groupStatsReply = (OFGroupStatsReply) statsReply;
250 groupDescStatsReply = (OFGroupDescStatsReply) reply;
251 groupStats.remove(statsReply.getXid() + 1);
252 } else {
253 groupStats.put(statsReply.getXid(), statsReply);
254 }
255 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
256 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
257 if (reply != null) {
258 groupStatsReply = (OFGroupStatsReply) reply;
259 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
260 groupStats.remove(statsReply.getXid() - 1);
261 } else {
262 groupStats.put(statsReply.getXid(), statsReply);
263 }
sangho5afd02a2015-02-03 20:07:35 -0800264 }
265 }
266
Ray Milkey0ae473d2016-04-04 10:56:47 -0700267 if (providerService != null && groupStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800268 Collection<Group> groups = buildGroupMetrics(deviceId,
269 groupStatsReply, groupDescStatsReply);
270 providerService.pushGroupMetrics(deviceId, groups);
271 for (Group group: groups) {
272 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800273 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800274 }
275 }
276 }
277
278 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
279 OFGroupStatsReply groupStatsReply,
280 OFGroupDescStatsReply groupDescStatsReply) {
281
282 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800283 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700284
sangho5afd02a2015-02-03 20:07:35 -0800285 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
286 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800287 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800288 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800289 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
290 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800291 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
292 groups.put(id, group);
293 }
294
295 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
296 int groupId = entry.getGroup().getGroupNumber();
297 DefaultGroup group = (DefaultGroup) groups.get(groupId);
298 if (group != null) {
299 group.setBytes(entry.getByteCount().getValue());
300 group.setLife(entry.getDurationSec());
301 group.setPackets(entry.getPacketCount().getValue());
302 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700303 int bucketIndex = 0;
304 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
305 ((StoredGroupBucketEntry) group.buckets().buckets()
306 .get(bucketIndex))
307 .setPackets(bucketStats
308 .getPacketCount().getValue());
309 ((StoredGroupBucketEntry) group.buckets().buckets()
310 .get(bucketIndex))
311 .setBytes(entry.getBucketStats()
312 .get(bucketIndex)
313 .getByteCount().getValue());
314 bucketIndex++;
315 }
sangho5afd02a2015-02-03 20:07:35 -0800316 }
317 }
318
319 return groups.values();
320 }
321
322 private GroupDescription.Type getGroupType(OFGroupType type) {
323 switch (type) {
324 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700325 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800326 case INDIRECT:
327 return GroupDescription.Type.INDIRECT;
328 case SELECT:
329 return GroupDescription.Type.SELECT;
330 case FF:
331 return GroupDescription.Type.FAILOVER;
332 default:
333 log.error("Unsupported OF group type : {}", type);
334 break;
335 }
336 return null;
337 }
338
sanghoa09f2742015-02-06 14:49:47 -0800339 /**
340 * Returns a transaction ID for entire group operations and increases
341 * the counter by the number given.
342 *
343 * @param increase the amount to increase the counter by
344 * @return a transaction ID
345 */
346 public static long getXidAndAdd(int increase) {
347 return XID_COUNTER.getAndAdd(increase);
348 }
349
sangho01a883c2015-02-23 11:01:50 -0800350 private boolean isGroupSupported(OpenFlowSwitch sw) {
351 if (sw.factory().getVersion() == OFVersion.OF_10 ||
352 sw.factory().getVersion() == OFVersion.OF_11 ||
353 sw.factory().getVersion() == OFVersion.OF_12) {
354 return false;
355 }
356
357 return true;
358 }
359
sangho5afd02a2015-02-03 20:07:35 -0800360 private class InternalGroupProvider
361 implements OpenFlowSwitchListener, OpenFlowEventListener {
362
363 @Override
364 public void handleMessage(Dpid dpid, OFMessage msg) {
365 switch (msg.getType()) {
366 case STATS_REPLY:
367 pushGroupMetrics(dpid, (OFStatsReply) msg);
368 break;
369 case ERROR:
370 OFErrorMsg errorMsg = (OFErrorMsg) msg;
371 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800372 GroupId pendingGroupId = null;
373 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800374 if (entry.getValue() == errorMsg.getXid()) {
375 pendingGroupId = entry.getKey();
376 break;
377 }
378 }
sangho22a805d2015-02-13 09:45:43 -0800379 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800380 log.warn("Error for unknown group operation: {}",
381 errorMsg.getXid());
382 } else {
383 GroupOperation operation =
384 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800385 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800386 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800387 OFGroupModFailedCode code =
388 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
389 GroupMsgErrorCode failureCode =
390 GroupMsgErrorCode.values()[(code.ordinal())];
391 GroupOperation failedOperation = GroupOperation
392 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800393 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800394 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800395 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800396 pendingGroupOperations.remove(pendingGroupId);
397 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800398 } else {
399 log.error("Cannot find pending group operation with group ID: {}",
400 pendingGroupId);
401 }
sangho5afd02a2015-02-03 20:07:35 -0800402 }
403 break;
404 }
405 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800406 break;
sangho5afd02a2015-02-03 20:07:35 -0800407 }
408 }
409
410 @Override
411 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800412 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500413 if (sw == null) {
414 return;
415 }
Laszlo Pappc893b272018-01-03 17:57:43 +0000416 if (isGroupSupported(sw) && sw.features().getCapabilities().contains(OFCapabilities.GROUP_STATS)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530417 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700418 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900419 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800420 }
Lei Xudee1aff2015-10-16 00:45:10 -0500421
422 //figure out race condition
423 if (controller.getSwitch(dpid) == null) {
424 switchRemoved(dpid);
425 }
sangho5afd02a2015-02-03 20:07:35 -0800426 }
427
428 @Override
429 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700430 stopCollectorIfNeeded(collectors.remove(dpid));
431 }
432
433 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800434 if (collector != null) {
435 collector.stop();
436 }
437 }
438
439 @Override
440 public void switchChanged(Dpid dpid) {
441 }
442
443 @Override
444 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700445 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800446 }
447
448 @Override
449 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
450 }
451 }
452
helenyrwu89470f12016-08-12 13:18:10 -0700453 /**
454 * Builds a list of failover Groups whose primary live bucket failed over
455 * (i.e. bucket in use has changed).
456 *
457 * @param dpid DPID of switch whose port's status changed
458 * @param status new status of port
459 * @return list of groups whose primary live bucket failed over
460 */
461 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
462 List<Group> groupList = new ArrayList<>();
463 OFPortDesc desc = status.getDesc();
464 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
465 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
466 if (desc.isEnabled()) {
467 return groupList;
468 }
469 Iterator<Group> iterator = groupService.getGroups(id).iterator();
470 while (iterator.hasNext()) {
471 Group group = iterator.next();
472 if (group.type() == GroupDescription.Type.FAILOVER &&
473 checkFailoverGroup(group, id, portNumber)) {
474 groupList.add(group);
475 }
476 }
477 return groupList;
478 }
479
480 /**
481 * Checks whether the first live port in the failover group's bucket
482 * has failed over.
483 *
484 * @param group failover group to be checked for failover
485 * @param id device ID of switch whose port's status changed
486 * @param portNumber port number of port that was disabled
487 * @return whether the failover group experienced failover
488 */
489 private boolean checkFailoverGroup(Group group, DeviceId id,
490 PortNumber portNumber) {
491 boolean portReached = false;
492 boolean portEnabled = false;
493 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
494 GroupBucket bucket;
495 while (bIterator.hasNext() && !portReached) {
496 bucket = bIterator.next();
497 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
498 portEnabled = true;
499 }
500 if (bucket.watchPort().equals(portNumber)) {
501 portReached = true;
502 }
503 }
504 return portReached && !portEnabled;
505 }
506
sangho5afd02a2015-02-03 20:07:35 -0800507}