blob: a74e2c7f302838a0e21a64acfc5f7476b9f48d67 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import com.google.common.collect.Maps;
Yuta HIGUCHI9721f852018-04-03 14:27:06 -070020import org.onlab.util.ItemNotFoundException;
dvaddireb57fdb02017-06-20 00:14:08 +053021import org.onosproject.cfg.ComponentConfigService;
sangho5afd02a2015-02-03 20:07:35 -080022import org.onosproject.core.GroupId;
23import org.onosproject.net.DeviceId;
helenyrwu89470f12016-08-12 13:18:10 -070024import org.onosproject.net.PortNumber;
25import org.onosproject.net.device.DeviceService;
Laszlo Papp84adcac2018-03-13 12:11:51 +000026import org.onosproject.net.driver.Driver;
jiangruia6b46092015-11-11 16:28:51 +080027import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080028import org.onosproject.net.group.DefaultGroup;
29import org.onosproject.net.group.Group;
helenyrwu89470f12016-08-12 13:18:10 -070030import org.onosproject.net.group.GroupBucket;
sangho5afd02a2015-02-03 20:07:35 -080031import org.onosproject.net.group.GroupBuckets;
32import org.onosproject.net.group.GroupDescription;
33import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080034import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080035import org.onosproject.net.group.GroupOperations;
36import org.onosproject.net.group.GroupProvider;
37import org.onosproject.net.group.GroupProviderRegistry;
38import org.onosproject.net.group.GroupProviderService;
helenyrwu89470f12016-08-12 13:18:10 -070039import org.onosproject.net.group.GroupService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070040import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080041import org.onosproject.net.provider.AbstractProvider;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.openflow.controller.Dpid;
44import org.onosproject.openflow.controller.OpenFlowController;
45import org.onosproject.openflow.controller.OpenFlowEventListener;
46import org.onosproject.openflow.controller.OpenFlowSwitch;
47import org.onosproject.openflow.controller.OpenFlowSwitchListener;
48import org.onosproject.openflow.controller.RoleState;
dvaddireb57fdb02017-06-20 00:14:08 +053049import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050import org.osgi.service.component.annotations.Activate;
51import org.osgi.service.component.annotations.Component;
52import org.osgi.service.component.annotations.Deactivate;
53import org.osgi.service.component.annotations.Modified;
54import org.osgi.service.component.annotations.Reference;
55import org.osgi.service.component.annotations.ReferenceCardinality;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070056import org.projectfloodlight.openflow.protocol.OFBucketCounter;
Maksym Polovyi3585a7a2018-02-15 20:21:16 +020057import org.projectfloodlight.openflow.protocol.OFCapabilities;
sangho5afd02a2015-02-03 20:07:35 -080058import org.projectfloodlight.openflow.protocol.OFErrorMsg;
59import org.projectfloodlight.openflow.protocol.OFErrorType;
60import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
61import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
62import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080063import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080064import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
65import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
66import org.projectfloodlight.openflow.protocol.OFGroupType;
67import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070068import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080069import org.projectfloodlight.openflow.protocol.OFPortStatus;
70import org.projectfloodlight.openflow.protocol.OFStatsReply;
71import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080072import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080073import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080074import org.slf4j.Logger;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076import java.util.ArrayList;
77import java.util.Collection;
78import java.util.Dictionary;
79import java.util.Iterator;
80import java.util.List;
81import java.util.Map;
82import java.util.Optional;
83import java.util.Properties;
84import java.util.concurrent.atomic.AtomicLong;
85
86import static org.onlab.util.Tools.getIntegerProperty;
Thomas Vachuska4167c3f2018-10-16 07:16:31 -070087import static org.onosproject.provider.of.group.impl.OsgiPropertyConstants.POLL_FREQUENCY;
88import static org.onosproject.provider.of.group.impl.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089import static org.slf4j.LoggerFactory.getLogger;
sangho5afd02a2015-02-03 20:07:35 -080090
91/**
92 * Provider which uses an OpenFlow controller to handle Group.
93 */
Thomas Vachuska4167c3f2018-10-16 07:16:31 -070094@Component(immediate = true,
95 property = {
96 POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT,
97 })
sangho5afd02a2015-02-03 20:07:35 -080098public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
99
100 private final Logger log = getLogger(getClass());
101
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangho5afd02a2015-02-03 20:07:35 -0800103 protected OpenFlowController controller;
104
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangho5afd02a2015-02-03 20:07:35 -0800106 protected GroupProviderRegistry providerRegistry;
107
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruia6b46092015-11-11 16:28:51 +0800109 protected DriverService driverService;
110
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY)
helenyrwu89470f12016-08-12 13:18:10 -0700112 protected DeviceService deviceService;
113
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY)
helenyrwu89470f12016-08-12 13:18:10 -0700115 protected GroupService groupService;
116
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY)
dvaddireb57fdb02017-06-20 00:14:08 +0530118 protected ComponentConfigService cfgService;
119
sangho5afd02a2015-02-03 20:07:35 -0800120 private GroupProviderService providerService;
121
dvaddireb57fdb02017-06-20 00:14:08 +0530122 private static final String COMPONENT = "org.onosproject.provider.of.group.impl.OpenFlowGroupProvider";
dvaddireb57fdb02017-06-20 00:14:08 +0530123
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700124 //@Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL,
125 // label = "Frequency (in seconds) for polling group statistics")
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700126 private int groupPollInterval = POLL_FREQUENCY_DEFAULT;
sangho5afd02a2015-02-03 20:07:35 -0800127
128 private final InternalGroupProvider listener = new InternalGroupProvider();
129
sanghoa09f2742015-02-06 14:49:47 -0800130 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800131 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800132 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800133 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800134 Maps.newConcurrentMap();
135
sanghoa09f2742015-02-06 14:49:47 -0800136 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800137 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800138
sangho5afd02a2015-02-03 20:07:35 -0800139 /**
140 * Creates a OpenFlow group provider.
141 */
142 public OpenFlowGroupProvider() {
143 super(new ProviderId("of", "org.onosproject.provider.group"));
144 }
145
146 @Activate
dvaddireb57fdb02017-06-20 00:14:08 +0530147 public void activate(ComponentContext context) {
148 cfgService.registerProperties(getClass());
sangho5afd02a2015-02-03 20:07:35 -0800149 providerService = providerRegistry.register(this);
150 controller.addListener(listener);
151 controller.addEventListener(listener);
152
dvaddireb57fdb02017-06-20 00:14:08 +0530153 modified(context);
154
sangho5afd02a2015-02-03 20:07:35 -0800155 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800156 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530157 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
sangho01a883c2015-02-23 11:01:50 -0800158 gsc.start();
159 collectors.put(new Dpid(sw.getId()), gsc);
160 }
sangho5afd02a2015-02-03 20:07:35 -0800161 }
162
163 log.info("Started");
164 }
165
166 @Deactivate
167 public void deactivate() {
dvaddireb57fdb02017-06-20 00:14:08 +0530168 cfgService.unregisterProperties(getClass(), false);
sangho5afd02a2015-02-03 20:07:35 -0800169 providerRegistry.unregister(this);
170 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800171 collectors.values().forEach(GroupStatsCollector::stop);
172 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800173 log.info("Stopped");
174 }
175
dvaddireb57fdb02017-06-20 00:14:08 +0530176 @Modified
177 public void modified(ComponentContext context) {
178 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700179 Integer newGroupPollInterval = getIntegerProperty(properties, POLL_FREQUENCY);
dvaddireb57fdb02017-06-20 00:14:08 +0530180 if (newGroupPollInterval != null && newGroupPollInterval > 0
181 && newGroupPollInterval != groupPollInterval) {
182 groupPollInterval = newGroupPollInterval;
183 modifyPollInterval();
184 } else if (newGroupPollInterval != null && newGroupPollInterval <= 0) {
185 log.warn("groupPollInterval must be greater than 0");
186 //If the new value <= 0 reset property with old value.
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700187 cfgService.setProperty(COMPONENT, POLL_FREQUENCY, Integer.toString(groupPollInterval));
dvaddireb57fdb02017-06-20 00:14:08 +0530188 }
189 }
190
191 private void modifyPollInterval() {
192 collectors.values().forEach(gsc -> gsc.adjustRate(groupPollInterval));
193
194 }
sangho5afd02a2015-02-03 20:07:35 -0800195 @Override
196 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800197 final Dpid dpid = Dpid.dpid(deviceId.uri());
198 OpenFlowSwitch sw = controller.getSwitch(dpid);
199 for (GroupOperation groupOperation: groupOps.operations()) {
200 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800201 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800202 return;
203 }
sanghoa09f2742015-02-06 14:49:47 -0800204 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800205 GroupModBuilder builder = null;
206 if (driverService == null) {
207 builder = GroupModBuilder.builder(groupOperation.buckets(),
208 groupOperation.groupId(),
209 groupOperation.groupType(),
210 sw.factory(),
211 Optional.of(groupModXid));
212 } else {
213 builder = GroupModBuilder.builder(groupOperation.buckets(),
214 groupOperation.groupId(),
215 groupOperation.groupType(),
216 sw.factory(),
217 Optional.of(groupModXid),
218 Optional.of(driverService));
219 }
sangho5afd02a2015-02-03 20:07:35 -0800220 OFGroupMod groupMod = null;
221 switch (groupOperation.opType()) {
222 case ADD:
223 groupMod = builder.buildGroupAdd();
224 break;
225 case MODIFY:
226 groupMod = builder.buildGroupMod();
227 break;
228 case DELETE:
229 groupMod = builder.buildGroupDel();
230 break;
231 default:
232 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530233 return;
sangho5afd02a2015-02-03 20:07:35 -0800234 }
235 sw.sendMsg(groupMod);
Yi Tsengfa394de2017-02-01 11:26:40 -0800236 GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber());
sangho22a805d2015-02-13 09:45:43 -0800237 pendingGroupOperations.put(groudId, groupOperation);
238 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800239 }
240 }
241
242 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
243 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200244 OpenFlowSwitch sw = controller.getSwitch(dpid);
245 boolean containsGroupStats = false;
246 if (sw != null && sw.features() != null) {
247 containsGroupStats = sw.features()
248 .getCapabilities()
249 .contains(OFCapabilities.GROUP_STATS);
250 }
sangho5afd02a2015-02-03 20:07:35 -0800251
252 OFGroupStatsReply groupStatsReply = null;
253 OFGroupDescStatsReply groupDescStatsReply = null;
254
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200255 if (containsGroupStats) {
256 synchronized (groupStats) {
257 if (statsReply.getStatsType() == OFStatsType.GROUP) {
258 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
259 if (reply != null) {
260 groupStatsReply = (OFGroupStatsReply) statsReply;
261 groupDescStatsReply = (OFGroupDescStatsReply) reply;
262 groupStats.remove(statsReply.getXid() + 1);
263 } else {
264 groupStats.put(statsReply.getXid(), statsReply);
265 }
266 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
267 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
268 if (reply != null) {
269 groupStatsReply = (OFGroupStatsReply) reply;
270 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
271 groupStats.remove(statsReply.getXid() - 1);
272 } else {
273 groupStats.put(statsReply.getXid(), statsReply);
274 }
sanghoa09f2742015-02-06 14:49:47 -0800275 }
sangho5afd02a2015-02-03 20:07:35 -0800276 }
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200277 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
278 // We are only requesting group desc stats; see GroupStatsCollector.java:sendGroupStatisticRequests()
279 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
sangho5afd02a2015-02-03 20:07:35 -0800280 }
281
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200282 if (providerService != null && groupDescStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800283 Collection<Group> groups = buildGroupMetrics(deviceId,
284 groupStatsReply, groupDescStatsReply);
285 providerService.pushGroupMetrics(deviceId, groups);
286 for (Group group: groups) {
287 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800288 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800289 }
290 }
291 }
292
293 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
294 OFGroupStatsReply groupStatsReply,
295 OFGroupDescStatsReply groupDescStatsReply) {
296
297 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800298 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700299
sangho5afd02a2015-02-03 20:07:35 -0800300 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
301 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800302 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800303 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800304 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
305 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800306 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
307 groups.put(id, group);
308 }
309
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200310 if (groupStatsReply == null) {
311 return groups.values();
312 }
313
sangho5afd02a2015-02-03 20:07:35 -0800314 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
315 int groupId = entry.getGroup().getGroupNumber();
316 DefaultGroup group = (DefaultGroup) groups.get(groupId);
317 if (group != null) {
318 group.setBytes(entry.getByteCount().getValue());
319 group.setLife(entry.getDurationSec());
320 group.setPackets(entry.getPacketCount().getValue());
321 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700322 int bucketIndex = 0;
323 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
324 ((StoredGroupBucketEntry) group.buckets().buckets()
325 .get(bucketIndex))
326 .setPackets(bucketStats
327 .getPacketCount().getValue());
328 ((StoredGroupBucketEntry) group.buckets().buckets()
329 .get(bucketIndex))
330 .setBytes(entry.getBucketStats()
331 .get(bucketIndex)
332 .getByteCount().getValue());
333 bucketIndex++;
334 }
sangho5afd02a2015-02-03 20:07:35 -0800335 }
336 }
337
338 return groups.values();
339 }
340
341 private GroupDescription.Type getGroupType(OFGroupType type) {
342 switch (type) {
343 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700344 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800345 case INDIRECT:
346 return GroupDescription.Type.INDIRECT;
347 case SELECT:
348 return GroupDescription.Type.SELECT;
349 case FF:
350 return GroupDescription.Type.FAILOVER;
351 default:
352 log.error("Unsupported OF group type : {}", type);
353 break;
354 }
355 return null;
356 }
357
sanghoa09f2742015-02-06 14:49:47 -0800358 /**
359 * Returns a transaction ID for entire group operations and increases
360 * the counter by the number given.
361 *
362 * @param increase the amount to increase the counter by
363 * @return a transaction ID
364 */
365 public static long getXidAndAdd(int increase) {
366 return XID_COUNTER.getAndAdd(increase);
367 }
368
sangho01a883c2015-02-23 11:01:50 -0800369 private boolean isGroupSupported(OpenFlowSwitch sw) {
370 if (sw.factory().getVersion() == OFVersion.OF_10 ||
371 sw.factory().getVersion() == OFVersion.OF_11 ||
Laszlo Papp84adcac2018-03-13 12:11:51 +0000372 sw.factory().getVersion() == OFVersion.OF_12 ||
373 !isGroupCapable(sw)) {
sangho01a883c2015-02-23 11:01:50 -0800374 return false;
375 }
376
377 return true;
378 }
379
Laszlo Papp84adcac2018-03-13 12:11:51 +0000380 /**
381 * Determine whether the given switch is group-capable.
382 *
383 * @param sw switch
384 * @return the boolean value of groupCapable property, or true if it is not configured.
385 */
386 private boolean isGroupCapable(OpenFlowSwitch sw) {
Yuta HIGUCHI9721f852018-04-03 14:27:06 -0700387 Driver driver;
388 try {
389 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid())));
390 } catch (ItemNotFoundException e) {
391 driver = driverService.getDriver(sw.manufacturerDescription(),
392 sw.hardwareDescription(),
393 sw.softwareDescription());
394 }
Laszlo Papp84adcac2018-03-13 12:11:51 +0000395 if (driver == null) {
396 return true;
397 }
Devin Limf853a592018-04-06 18:21:11 -0700398 String isGroupCapable = driver.getProperty(GROUP_CAPABLE);
399 return isGroupCapable == null || Boolean.parseBoolean(isGroupCapable);
Laszlo Papp84adcac2018-03-13 12:11:51 +0000400 }
401
sangho5afd02a2015-02-03 20:07:35 -0800402 private class InternalGroupProvider
403 implements OpenFlowSwitchListener, OpenFlowEventListener {
404
405 @Override
406 public void handleMessage(Dpid dpid, OFMessage msg) {
407 switch (msg.getType()) {
408 case STATS_REPLY:
409 pushGroupMetrics(dpid, (OFStatsReply) msg);
410 break;
411 case ERROR:
412 OFErrorMsg errorMsg = (OFErrorMsg) msg;
413 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800414 GroupId pendingGroupId = null;
415 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800416 if (entry.getValue() == errorMsg.getXid()) {
417 pendingGroupId = entry.getKey();
418 break;
419 }
420 }
sangho22a805d2015-02-13 09:45:43 -0800421 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800422 log.warn("Error for unknown group operation: {}",
423 errorMsg.getXid());
424 } else {
425 GroupOperation operation =
426 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800427 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800428 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800429 OFGroupModFailedCode code =
430 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
431 GroupMsgErrorCode failureCode =
432 GroupMsgErrorCode.values()[(code.ordinal())];
433 GroupOperation failedOperation = GroupOperation
434 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800435 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800436 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800437 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800438 pendingGroupOperations.remove(pendingGroupId);
439 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800440 } else {
441 log.error("Cannot find pending group operation with group ID: {}",
442 pendingGroupId);
443 }
sangho5afd02a2015-02-03 20:07:35 -0800444 }
445 break;
446 }
447 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800448 break;
sangho5afd02a2015-02-03 20:07:35 -0800449 }
450 }
451
452 @Override
453 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800454 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500455 if (sw == null) {
456 return;
457 }
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000458 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530459 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700460 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900461 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800462 }
Lei Xudee1aff2015-10-16 00:45:10 -0500463
464 //figure out race condition
465 if (controller.getSwitch(dpid) == null) {
466 switchRemoved(dpid);
467 }
sangho5afd02a2015-02-03 20:07:35 -0800468 }
469
470 @Override
471 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700472 stopCollectorIfNeeded(collectors.remove(dpid));
473 }
474
475 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800476 if (collector != null) {
477 collector.stop();
478 }
479 }
480
481 @Override
482 public void switchChanged(Dpid dpid) {
483 }
484
485 @Override
486 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700487 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800488 }
489
490 @Override
491 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
492 }
493 }
494
helenyrwu89470f12016-08-12 13:18:10 -0700495 /**
496 * Builds a list of failover Groups whose primary live bucket failed over
497 * (i.e. bucket in use has changed).
498 *
499 * @param dpid DPID of switch whose port's status changed
500 * @param status new status of port
501 * @return list of groups whose primary live bucket failed over
502 */
503 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
504 List<Group> groupList = new ArrayList<>();
505 OFPortDesc desc = status.getDesc();
506 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
507 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
508 if (desc.isEnabled()) {
509 return groupList;
510 }
511 Iterator<Group> iterator = groupService.getGroups(id).iterator();
512 while (iterator.hasNext()) {
513 Group group = iterator.next();
514 if (group.type() == GroupDescription.Type.FAILOVER &&
515 checkFailoverGroup(group, id, portNumber)) {
516 groupList.add(group);
517 }
518 }
519 return groupList;
520 }
521
522 /**
523 * Checks whether the first live port in the failover group's bucket
524 * has failed over.
525 *
526 * @param group failover group to be checked for failover
527 * @param id device ID of switch whose port's status changed
528 * @param portNumber port number of port that was disabled
529 * @return whether the failover group experienced failover
530 */
531 private boolean checkFailoverGroup(Group group, DeviceId id,
532 PortNumber portNumber) {
533 boolean portReached = false;
534 boolean portEnabled = false;
535 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
536 GroupBucket bucket;
537 while (bIterator.hasNext() && !portReached) {
538 bucket = bIterator.next();
539 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
540 portEnabled = true;
541 }
542 if (bucket.watchPort().equals(portNumber)) {
543 portReached = true;
544 }
545 }
546 return portReached && !portEnabled;
547 }
548
sangho5afd02a2015-02-03 20:07:35 -0800549}