blob: c60a58ca6692756b0adacf87662dfe67c0f1cd9f [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;
Laszlo Papp84adcac2018-03-13 12:11:51 +000044import org.onosproject.net.driver.Driver;
jiangruia6b46092015-11-11 16:28:51 +080045import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080046import org.onosproject.net.group.DefaultGroup;
47import org.onosproject.net.group.Group;
helenyrwu89470f12016-08-12 13:18:10 -070048import org.onosproject.net.group.GroupBucket;
sangho5afd02a2015-02-03 20:07:35 -080049import org.onosproject.net.group.GroupBuckets;
50import org.onosproject.net.group.GroupDescription;
51import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080052import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080053import org.onosproject.net.group.GroupOperations;
54import org.onosproject.net.group.GroupProvider;
55import org.onosproject.net.group.GroupProviderRegistry;
56import org.onosproject.net.group.GroupProviderService;
helenyrwu89470f12016-08-12 13:18:10 -070057import org.onosproject.net.group.GroupService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070058import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080059import org.onosproject.net.provider.AbstractProvider;
60import org.onosproject.net.provider.ProviderId;
61import org.onosproject.openflow.controller.Dpid;
62import org.onosproject.openflow.controller.OpenFlowController;
63import org.onosproject.openflow.controller.OpenFlowEventListener;
64import org.onosproject.openflow.controller.OpenFlowSwitch;
65import org.onosproject.openflow.controller.OpenFlowSwitchListener;
66import org.onosproject.openflow.controller.RoleState;
dvaddireb57fdb02017-06-20 00:14:08 +053067import org.osgi.service.component.ComponentContext;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070068import org.projectfloodlight.openflow.protocol.OFBucketCounter;
Maksym Polovyi3585a7a2018-02-15 20:21:16 +020069import org.projectfloodlight.openflow.protocol.OFCapabilities;
sangho5afd02a2015-02-03 20:07:35 -080070import org.projectfloodlight.openflow.protocol.OFErrorMsg;
71import org.projectfloodlight.openflow.protocol.OFErrorType;
72import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
73import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
74import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080075import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080076import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
77import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
78import org.projectfloodlight.openflow.protocol.OFGroupType;
79import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070080import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080081import org.projectfloodlight.openflow.protocol.OFPortStatus;
82import org.projectfloodlight.openflow.protocol.OFStatsReply;
83import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080084import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080085import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080086import org.slf4j.Logger;
87
jiangruia6b46092015-11-11 16:28:51 +080088import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080089
90/**
91 * Provider which uses an OpenFlow controller to handle Group.
92 */
93@Component(immediate = true)
94public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
95
96 private final Logger log = getLogger(getClass());
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected OpenFlowController controller;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected GroupProviderRegistry providerRegistry;
103
jiangruia6b46092015-11-11 16:28:51 +0800104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected DriverService driverService;
106
helenyrwu89470f12016-08-12 13:18:10 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected DeviceService deviceService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected GroupService groupService;
112
dvaddireb57fdb02017-06-20 00:14:08 +0530113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected ComponentConfigService cfgService;
115
sangho5afd02a2015-02-03 20:07:35 -0800116 private GroupProviderService providerService;
117
dvaddireb57fdb02017-06-20 00:14:08 +0530118 private static final int DEFAULT_POLL_INTERVAL = 10;
119 private static final String COMPONENT = "org.onosproject.provider.of.group.impl.OpenFlowGroupProvider";
120 private static final String GROUP_POLL_INTERVAL_CONST = "groupPollInterval";
121
122 @Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL,
123 label = "Frequency (in seconds) for polling group statistics")
124 private int groupPollInterval = DEFAULT_POLL_INTERVAL;
sangho5afd02a2015-02-03 20:07:35 -0800125
126 private final InternalGroupProvider listener = new InternalGroupProvider();
127
sanghoa09f2742015-02-06 14:49:47 -0800128 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800129 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800130 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800131 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800132 Maps.newConcurrentMap();
133
sanghoa09f2742015-02-06 14:49:47 -0800134 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800135 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800136
sangho5afd02a2015-02-03 20:07:35 -0800137 /**
138 * Creates a OpenFlow group provider.
139 */
140 public OpenFlowGroupProvider() {
141 super(new ProviderId("of", "org.onosproject.provider.group"));
142 }
143
144 @Activate
dvaddireb57fdb02017-06-20 00:14:08 +0530145 public void activate(ComponentContext context) {
146 cfgService.registerProperties(getClass());
sangho5afd02a2015-02-03 20:07:35 -0800147 providerService = providerRegistry.register(this);
148 controller.addListener(listener);
149 controller.addEventListener(listener);
150
dvaddireb57fdb02017-06-20 00:14:08 +0530151 modified(context);
152
sangho5afd02a2015-02-03 20:07:35 -0800153 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800154 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530155 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
sangho01a883c2015-02-23 11:01:50 -0800156 gsc.start();
157 collectors.put(new Dpid(sw.getId()), gsc);
158 }
sangho5afd02a2015-02-03 20:07:35 -0800159 }
160
161 log.info("Started");
162 }
163
164 @Deactivate
165 public void deactivate() {
dvaddireb57fdb02017-06-20 00:14:08 +0530166 cfgService.unregisterProperties(getClass(), false);
sangho5afd02a2015-02-03 20:07:35 -0800167 providerRegistry.unregister(this);
168 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800169 collectors.values().forEach(GroupStatsCollector::stop);
170 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800171 log.info("Stopped");
172 }
173
dvaddireb57fdb02017-06-20 00:14:08 +0530174 @Modified
175 public void modified(ComponentContext context) {
176 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
177 Integer newGroupPollInterval = getIntegerProperty(properties, GROUP_POLL_INTERVAL_CONST);
178 if (newGroupPollInterval != null && newGroupPollInterval > 0
179 && newGroupPollInterval != groupPollInterval) {
180 groupPollInterval = newGroupPollInterval;
181 modifyPollInterval();
182 } else if (newGroupPollInterval != null && newGroupPollInterval <= 0) {
183 log.warn("groupPollInterval must be greater than 0");
184 //If the new value <= 0 reset property with old value.
185 cfgService.setProperty(COMPONENT, GROUP_POLL_INTERVAL_CONST, Integer.toString(groupPollInterval));
186 }
187 }
188
189 private void modifyPollInterval() {
190 collectors.values().forEach(gsc -> gsc.adjustRate(groupPollInterval));
191
192 }
sangho5afd02a2015-02-03 20:07:35 -0800193 @Override
194 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800195 final Dpid dpid = Dpid.dpid(deviceId.uri());
196 OpenFlowSwitch sw = controller.getSwitch(dpid);
197 for (GroupOperation groupOperation: groupOps.operations()) {
198 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800199 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800200 return;
201 }
sanghoa09f2742015-02-06 14:49:47 -0800202 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800203 GroupModBuilder builder = null;
204 if (driverService == null) {
205 builder = GroupModBuilder.builder(groupOperation.buckets(),
206 groupOperation.groupId(),
207 groupOperation.groupType(),
208 sw.factory(),
209 Optional.of(groupModXid));
210 } else {
211 builder = GroupModBuilder.builder(groupOperation.buckets(),
212 groupOperation.groupId(),
213 groupOperation.groupType(),
214 sw.factory(),
215 Optional.of(groupModXid),
216 Optional.of(driverService));
217 }
sangho5afd02a2015-02-03 20:07:35 -0800218 OFGroupMod groupMod = null;
219 switch (groupOperation.opType()) {
220 case ADD:
221 groupMod = builder.buildGroupAdd();
222 break;
223 case MODIFY:
224 groupMod = builder.buildGroupMod();
225 break;
226 case DELETE:
227 groupMod = builder.buildGroupDel();
228 break;
229 default:
230 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530231 return;
sangho5afd02a2015-02-03 20:07:35 -0800232 }
233 sw.sendMsg(groupMod);
Yi Tsengfa394de2017-02-01 11:26:40 -0800234 GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber());
sangho22a805d2015-02-13 09:45:43 -0800235 pendingGroupOperations.put(groudId, groupOperation);
236 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800237 }
238 }
239
240 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
241 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200242 OpenFlowSwitch sw = controller.getSwitch(dpid);
243 boolean containsGroupStats = false;
244 if (sw != null && sw.features() != null) {
245 containsGroupStats = sw.features()
246 .getCapabilities()
247 .contains(OFCapabilities.GROUP_STATS);
248 }
sangho5afd02a2015-02-03 20:07:35 -0800249
250 OFGroupStatsReply groupStatsReply = null;
251 OFGroupDescStatsReply groupDescStatsReply = null;
252
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200253 if (containsGroupStats) {
254 synchronized (groupStats) {
255 if (statsReply.getStatsType() == OFStatsType.GROUP) {
256 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
257 if (reply != null) {
258 groupStatsReply = (OFGroupStatsReply) statsReply;
259 groupDescStatsReply = (OFGroupDescStatsReply) reply;
260 groupStats.remove(statsReply.getXid() + 1);
261 } else {
262 groupStats.put(statsReply.getXid(), statsReply);
263 }
264 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
265 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
266 if (reply != null) {
267 groupStatsReply = (OFGroupStatsReply) reply;
268 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
269 groupStats.remove(statsReply.getXid() - 1);
270 } else {
271 groupStats.put(statsReply.getXid(), statsReply);
272 }
sanghoa09f2742015-02-06 14:49:47 -0800273 }
sangho5afd02a2015-02-03 20:07:35 -0800274 }
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200275 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
276 // We are only requesting group desc stats; see GroupStatsCollector.java:sendGroupStatisticRequests()
277 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
sangho5afd02a2015-02-03 20:07:35 -0800278 }
279
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200280 if (providerService != null && groupDescStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800281 Collection<Group> groups = buildGroupMetrics(deviceId,
282 groupStatsReply, groupDescStatsReply);
283 providerService.pushGroupMetrics(deviceId, groups);
284 for (Group group: groups) {
285 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800286 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800287 }
288 }
289 }
290
291 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
292 OFGroupStatsReply groupStatsReply,
293 OFGroupDescStatsReply groupDescStatsReply) {
294
295 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800296 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700297
sangho5afd02a2015-02-03 20:07:35 -0800298 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
299 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800300 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800301 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800302 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
303 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800304 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
305 groups.put(id, group);
306 }
307
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200308 if (groupStatsReply == null) {
309 return groups.values();
310 }
311
sangho5afd02a2015-02-03 20:07:35 -0800312 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
313 int groupId = entry.getGroup().getGroupNumber();
314 DefaultGroup group = (DefaultGroup) groups.get(groupId);
315 if (group != null) {
316 group.setBytes(entry.getByteCount().getValue());
317 group.setLife(entry.getDurationSec());
318 group.setPackets(entry.getPacketCount().getValue());
319 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700320 int bucketIndex = 0;
321 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
322 ((StoredGroupBucketEntry) group.buckets().buckets()
323 .get(bucketIndex))
324 .setPackets(bucketStats
325 .getPacketCount().getValue());
326 ((StoredGroupBucketEntry) group.buckets().buckets()
327 .get(bucketIndex))
328 .setBytes(entry.getBucketStats()
329 .get(bucketIndex)
330 .getByteCount().getValue());
331 bucketIndex++;
332 }
sangho5afd02a2015-02-03 20:07:35 -0800333 }
334 }
335
336 return groups.values();
337 }
338
339 private GroupDescription.Type getGroupType(OFGroupType type) {
340 switch (type) {
341 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700342 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800343 case INDIRECT:
344 return GroupDescription.Type.INDIRECT;
345 case SELECT:
346 return GroupDescription.Type.SELECT;
347 case FF:
348 return GroupDescription.Type.FAILOVER;
349 default:
350 log.error("Unsupported OF group type : {}", type);
351 break;
352 }
353 return null;
354 }
355
sanghoa09f2742015-02-06 14:49:47 -0800356 /**
357 * Returns a transaction ID for entire group operations and increases
358 * the counter by the number given.
359 *
360 * @param increase the amount to increase the counter by
361 * @return a transaction ID
362 */
363 public static long getXidAndAdd(int increase) {
364 return XID_COUNTER.getAndAdd(increase);
365 }
366
sangho01a883c2015-02-23 11:01:50 -0800367 private boolean isGroupSupported(OpenFlowSwitch sw) {
368 if (sw.factory().getVersion() == OFVersion.OF_10 ||
369 sw.factory().getVersion() == OFVersion.OF_11 ||
Laszlo Papp84adcac2018-03-13 12:11:51 +0000370 sw.factory().getVersion() == OFVersion.OF_12 ||
371 !isGroupCapable(sw)) {
sangho01a883c2015-02-23 11:01:50 -0800372 return false;
373 }
374
375 return true;
376 }
377
Laszlo Papp84adcac2018-03-13 12:11:51 +0000378 /**
379 * Determine whether the given switch is group-capable.
380 *
381 * @param sw switch
382 * @return the boolean value of groupCapable property, or true if it is not configured.
383 */
384 private boolean isGroupCapable(OpenFlowSwitch sw) {
385 Driver driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid())));
386 if (driver == null) {
387 return true;
388 }
389 String isGroupCapable = driver.getProperty(GROUP_CAPABLE);
390 return isGroupCapable == null || Boolean.parseBoolean(isGroupCapable);
391 }
392
sangho5afd02a2015-02-03 20:07:35 -0800393 private class InternalGroupProvider
394 implements OpenFlowSwitchListener, OpenFlowEventListener {
395
396 @Override
397 public void handleMessage(Dpid dpid, OFMessage msg) {
398 switch (msg.getType()) {
399 case STATS_REPLY:
400 pushGroupMetrics(dpid, (OFStatsReply) msg);
401 break;
402 case ERROR:
403 OFErrorMsg errorMsg = (OFErrorMsg) msg;
404 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800405 GroupId pendingGroupId = null;
406 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800407 if (entry.getValue() == errorMsg.getXid()) {
408 pendingGroupId = entry.getKey();
409 break;
410 }
411 }
sangho22a805d2015-02-13 09:45:43 -0800412 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800413 log.warn("Error for unknown group operation: {}",
414 errorMsg.getXid());
415 } else {
416 GroupOperation operation =
417 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800418 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800419 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800420 OFGroupModFailedCode code =
421 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
422 GroupMsgErrorCode failureCode =
423 GroupMsgErrorCode.values()[(code.ordinal())];
424 GroupOperation failedOperation = GroupOperation
425 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800426 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800427 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800428 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800429 pendingGroupOperations.remove(pendingGroupId);
430 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800431 } else {
432 log.error("Cannot find pending group operation with group ID: {}",
433 pendingGroupId);
434 }
sangho5afd02a2015-02-03 20:07:35 -0800435 }
436 break;
437 }
438 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800439 break;
sangho5afd02a2015-02-03 20:07:35 -0800440 }
441 }
442
443 @Override
444 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800445 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500446 if (sw == null) {
447 return;
448 }
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000449 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530450 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700451 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900452 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800453 }
Lei Xudee1aff2015-10-16 00:45:10 -0500454
455 //figure out race condition
456 if (controller.getSwitch(dpid) == null) {
457 switchRemoved(dpid);
458 }
sangho5afd02a2015-02-03 20:07:35 -0800459 }
460
461 @Override
462 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700463 stopCollectorIfNeeded(collectors.remove(dpid));
464 }
465
466 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800467 if (collector != null) {
468 collector.stop();
469 }
470 }
471
472 @Override
473 public void switchChanged(Dpid dpid) {
474 }
475
476 @Override
477 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700478 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800479 }
480
481 @Override
482 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
483 }
484 }
485
helenyrwu89470f12016-08-12 13:18:10 -0700486 /**
487 * Builds a list of failover Groups whose primary live bucket failed over
488 * (i.e. bucket in use has changed).
489 *
490 * @param dpid DPID of switch whose port's status changed
491 * @param status new status of port
492 * @return list of groups whose primary live bucket failed over
493 */
494 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
495 List<Group> groupList = new ArrayList<>();
496 OFPortDesc desc = status.getDesc();
497 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
498 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
499 if (desc.isEnabled()) {
500 return groupList;
501 }
502 Iterator<Group> iterator = groupService.getGroups(id).iterator();
503 while (iterator.hasNext()) {
504 Group group = iterator.next();
505 if (group.type() == GroupDescription.Type.FAILOVER &&
506 checkFailoverGroup(group, id, portNumber)) {
507 groupList.add(group);
508 }
509 }
510 return groupList;
511 }
512
513 /**
514 * Checks whether the first live port in the failover group's bucket
515 * has failed over.
516 *
517 * @param group failover group to be checked for failover
518 * @param id device ID of switch whose port's status changed
519 * @param portNumber port number of port that was disabled
520 * @return whether the failover group experienced failover
521 */
522 private boolean checkFailoverGroup(Group group, DeviceId id,
523 PortNumber portNumber) {
524 boolean portReached = false;
525 boolean portEnabled = false;
526 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
527 GroupBucket bucket;
528 while (bIterator.hasNext() && !portReached) {
529 bucket = bIterator.next();
530 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
531 portEnabled = true;
532 }
533 if (bucket.watchPort().equals(portNumber)) {
534 portReached = true;
535 }
536 }
537 return portReached && !portEnabled;
538 }
539
sangho5afd02a2015-02-03 20:07:35 -0800540}