blob: df1bb6a59ab141ca9a878a41bd4ba39f93520b02 [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;
Maksym Polovyi3585a7a2018-02-15 20:21:16 +020068import 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));
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200241 OpenFlowSwitch sw = controller.getSwitch(dpid);
242 boolean containsGroupStats = false;
243 if (sw != null && sw.features() != null) {
244 containsGroupStats = sw.features()
245 .getCapabilities()
246 .contains(OFCapabilities.GROUP_STATS);
247 }
sangho5afd02a2015-02-03 20:07:35 -0800248
249 OFGroupStatsReply groupStatsReply = null;
250 OFGroupDescStatsReply groupDescStatsReply = null;
251
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200252 if (containsGroupStats) {
253 synchronized (groupStats) {
254 if (statsReply.getStatsType() == OFStatsType.GROUP) {
255 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
256 if (reply != null) {
257 groupStatsReply = (OFGroupStatsReply) statsReply;
258 groupDescStatsReply = (OFGroupDescStatsReply) reply;
259 groupStats.remove(statsReply.getXid() + 1);
260 } else {
261 groupStats.put(statsReply.getXid(), statsReply);
262 }
263 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
264 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
265 if (reply != null) {
266 groupStatsReply = (OFGroupStatsReply) reply;
267 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
268 groupStats.remove(statsReply.getXid() - 1);
269 } else {
270 groupStats.put(statsReply.getXid(), statsReply);
271 }
sanghoa09f2742015-02-06 14:49:47 -0800272 }
sangho5afd02a2015-02-03 20:07:35 -0800273 }
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200274 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
275 // We are only requesting group desc stats; see GroupStatsCollector.java:sendGroupStatisticRequests()
276 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
sangho5afd02a2015-02-03 20:07:35 -0800277 }
278
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200279 if (providerService != null && groupDescStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800280 Collection<Group> groups = buildGroupMetrics(deviceId,
281 groupStatsReply, groupDescStatsReply);
282 providerService.pushGroupMetrics(deviceId, groups);
283 for (Group group: groups) {
284 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800285 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800286 }
287 }
288 }
289
290 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
291 OFGroupStatsReply groupStatsReply,
292 OFGroupDescStatsReply groupDescStatsReply) {
293
294 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800295 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700296
sangho5afd02a2015-02-03 20:07:35 -0800297 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
298 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800299 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800300 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800301 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
302 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800303 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
304 groups.put(id, group);
305 }
306
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200307 if (groupStatsReply == null) {
308 return groups.values();
309 }
310
sangho5afd02a2015-02-03 20:07:35 -0800311 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
312 int groupId = entry.getGroup().getGroupNumber();
313 DefaultGroup group = (DefaultGroup) groups.get(groupId);
314 if (group != null) {
315 group.setBytes(entry.getByteCount().getValue());
316 group.setLife(entry.getDurationSec());
317 group.setPackets(entry.getPacketCount().getValue());
318 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700319 int bucketIndex = 0;
320 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
321 ((StoredGroupBucketEntry) group.buckets().buckets()
322 .get(bucketIndex))
323 .setPackets(bucketStats
324 .getPacketCount().getValue());
325 ((StoredGroupBucketEntry) group.buckets().buckets()
326 .get(bucketIndex))
327 .setBytes(entry.getBucketStats()
328 .get(bucketIndex)
329 .getByteCount().getValue());
330 bucketIndex++;
331 }
sangho5afd02a2015-02-03 20:07:35 -0800332 }
333 }
334
335 return groups.values();
336 }
337
338 private GroupDescription.Type getGroupType(OFGroupType type) {
339 switch (type) {
340 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700341 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800342 case INDIRECT:
343 return GroupDescription.Type.INDIRECT;
344 case SELECT:
345 return GroupDescription.Type.SELECT;
346 case FF:
347 return GroupDescription.Type.FAILOVER;
348 default:
349 log.error("Unsupported OF group type : {}", type);
350 break;
351 }
352 return null;
353 }
354
sanghoa09f2742015-02-06 14:49:47 -0800355 /**
356 * Returns a transaction ID for entire group operations and increases
357 * the counter by the number given.
358 *
359 * @param increase the amount to increase the counter by
360 * @return a transaction ID
361 */
362 public static long getXidAndAdd(int increase) {
363 return XID_COUNTER.getAndAdd(increase);
364 }
365
sangho01a883c2015-02-23 11:01:50 -0800366 private boolean isGroupSupported(OpenFlowSwitch sw) {
367 if (sw.factory().getVersion() == OFVersion.OF_10 ||
368 sw.factory().getVersion() == OFVersion.OF_11 ||
369 sw.factory().getVersion() == OFVersion.OF_12) {
370 return false;
371 }
372
373 return true;
374 }
375
sangho5afd02a2015-02-03 20:07:35 -0800376 private class InternalGroupProvider
377 implements OpenFlowSwitchListener, OpenFlowEventListener {
378
379 @Override
380 public void handleMessage(Dpid dpid, OFMessage msg) {
381 switch (msg.getType()) {
382 case STATS_REPLY:
383 pushGroupMetrics(dpid, (OFStatsReply) msg);
384 break;
385 case ERROR:
386 OFErrorMsg errorMsg = (OFErrorMsg) msg;
387 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800388 GroupId pendingGroupId = null;
389 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800390 if (entry.getValue() == errorMsg.getXid()) {
391 pendingGroupId = entry.getKey();
392 break;
393 }
394 }
sangho22a805d2015-02-13 09:45:43 -0800395 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800396 log.warn("Error for unknown group operation: {}",
397 errorMsg.getXid());
398 } else {
399 GroupOperation operation =
400 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800401 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800402 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800403 OFGroupModFailedCode code =
404 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
405 GroupMsgErrorCode failureCode =
406 GroupMsgErrorCode.values()[(code.ordinal())];
407 GroupOperation failedOperation = GroupOperation
408 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800409 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800410 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800411 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800412 pendingGroupOperations.remove(pendingGroupId);
413 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800414 } else {
415 log.error("Cannot find pending group operation with group ID: {}",
416 pendingGroupId);
417 }
sangho5afd02a2015-02-03 20:07:35 -0800418 }
419 break;
420 }
421 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800422 break;
sangho5afd02a2015-02-03 20:07:35 -0800423 }
424 }
425
426 @Override
427 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800428 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500429 if (sw == null) {
430 return;
431 }
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000432 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530433 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700434 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900435 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800436 }
Lei Xudee1aff2015-10-16 00:45:10 -0500437
438 //figure out race condition
439 if (controller.getSwitch(dpid) == null) {
440 switchRemoved(dpid);
441 }
sangho5afd02a2015-02-03 20:07:35 -0800442 }
443
444 @Override
445 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700446 stopCollectorIfNeeded(collectors.remove(dpid));
447 }
448
449 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800450 if (collector != null) {
451 collector.stop();
452 }
453 }
454
455 @Override
456 public void switchChanged(Dpid dpid) {
457 }
458
459 @Override
460 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700461 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800462 }
463
464 @Override
465 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
466 }
467 }
468
helenyrwu89470f12016-08-12 13:18:10 -0700469 /**
470 * Builds a list of failover Groups whose primary live bucket failed over
471 * (i.e. bucket in use has changed).
472 *
473 * @param dpid DPID of switch whose port's status changed
474 * @param status new status of port
475 * @return list of groups whose primary live bucket failed over
476 */
477 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
478 List<Group> groupList = new ArrayList<>();
479 OFPortDesc desc = status.getDesc();
480 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
481 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
482 if (desc.isEnabled()) {
483 return groupList;
484 }
485 Iterator<Group> iterator = groupService.getGroups(id).iterator();
486 while (iterator.hasNext()) {
487 Group group = iterator.next();
488 if (group.type() == GroupDescription.Type.FAILOVER &&
489 checkFailoverGroup(group, id, portNumber)) {
490 groupList.add(group);
491 }
492 }
493 return groupList;
494 }
495
496 /**
497 * Checks whether the first live port in the failover group's bucket
498 * has failed over.
499 *
500 * @param group failover group to be checked for failover
501 * @param id device ID of switch whose port's status changed
502 * @param portNumber port number of port that was disabled
503 * @return whether the failover group experienced failover
504 */
505 private boolean checkFailoverGroup(Group group, DeviceId id,
506 PortNumber portNumber) {
507 boolean portReached = false;
508 boolean portEnabled = false;
509 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
510 GroupBucket bucket;
511 while (bIterator.hasNext() && !portReached) {
512 bucket = bIterator.next();
513 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
514 portEnabled = true;
515 }
516 if (bucket.watchPort().equals(portNumber)) {
517 portReached = true;
518 }
519 }
520 return portReached && !portEnabled;
521 }
522
sangho5afd02a2015-02-03 20:07:35 -0800523}