blob: a43c29505188c7d17e22c131e53e05dfcd8f0d6e [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;
87import static org.slf4j.LoggerFactory.getLogger;
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070097 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangho5afd02a2015-02-03 20:07:35 -080098 protected OpenFlowController controller;
99
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangho5afd02a2015-02-03 20:07:35 -0800101 protected GroupProviderRegistry providerRegistry;
102
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruia6b46092015-11-11 16:28:51 +0800104 protected DriverService driverService;
105
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY)
helenyrwu89470f12016-08-12 13:18:10 -0700107 protected DeviceService deviceService;
108
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY)
helenyrwu89470f12016-08-12 13:18:10 -0700110 protected GroupService groupService;
111
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY)
dvaddireb57fdb02017-06-20 00:14:08 +0530113 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
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700121 //@Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL,
122 // label = "Frequency (in seconds) for polling group statistics")
dvaddireb57fdb02017-06-20 00:14:08 +0530123 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 ||
Laszlo Papp84adcac2018-03-13 12:11:51 +0000369 sw.factory().getVersion() == OFVersion.OF_12 ||
370 !isGroupCapable(sw)) {
sangho01a883c2015-02-23 11:01:50 -0800371 return false;
372 }
373
374 return true;
375 }
376
Laszlo Papp84adcac2018-03-13 12:11:51 +0000377 /**
378 * Determine whether the given switch is group-capable.
379 *
380 * @param sw switch
381 * @return the boolean value of groupCapable property, or true if it is not configured.
382 */
383 private boolean isGroupCapable(OpenFlowSwitch sw) {
Yuta HIGUCHI9721f852018-04-03 14:27:06 -0700384 Driver driver;
385 try {
386 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid())));
387 } catch (ItemNotFoundException e) {
388 driver = driverService.getDriver(sw.manufacturerDescription(),
389 sw.hardwareDescription(),
390 sw.softwareDescription());
391 }
Laszlo Papp84adcac2018-03-13 12:11:51 +0000392 if (driver == null) {
393 return true;
394 }
Devin Limf853a592018-04-06 18:21:11 -0700395 String isGroupCapable = driver.getProperty(GROUP_CAPABLE);
396 return isGroupCapable == null || Boolean.parseBoolean(isGroupCapable);
Laszlo Papp84adcac2018-03-13 12:11:51 +0000397 }
398
sangho5afd02a2015-02-03 20:07:35 -0800399 private class InternalGroupProvider
400 implements OpenFlowSwitchListener, OpenFlowEventListener {
401
402 @Override
403 public void handleMessage(Dpid dpid, OFMessage msg) {
404 switch (msg.getType()) {
405 case STATS_REPLY:
406 pushGroupMetrics(dpid, (OFStatsReply) msg);
407 break;
408 case ERROR:
409 OFErrorMsg errorMsg = (OFErrorMsg) msg;
410 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800411 GroupId pendingGroupId = null;
412 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800413 if (entry.getValue() == errorMsg.getXid()) {
414 pendingGroupId = entry.getKey();
415 break;
416 }
417 }
sangho22a805d2015-02-13 09:45:43 -0800418 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800419 log.warn("Error for unknown group operation: {}",
420 errorMsg.getXid());
421 } else {
422 GroupOperation operation =
423 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800424 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800425 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800426 OFGroupModFailedCode code =
427 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
428 GroupMsgErrorCode failureCode =
429 GroupMsgErrorCode.values()[(code.ordinal())];
430 GroupOperation failedOperation = GroupOperation
431 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800432 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800433 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800434 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800435 pendingGroupOperations.remove(pendingGroupId);
436 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800437 } else {
438 log.error("Cannot find pending group operation with group ID: {}",
439 pendingGroupId);
440 }
sangho5afd02a2015-02-03 20:07:35 -0800441 }
442 break;
443 }
444 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800445 break;
sangho5afd02a2015-02-03 20:07:35 -0800446 }
447 }
448
449 @Override
450 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800451 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500452 if (sw == null) {
453 return;
454 }
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000455 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530456 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700457 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900458 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800459 }
Lei Xudee1aff2015-10-16 00:45:10 -0500460
461 //figure out race condition
462 if (controller.getSwitch(dpid) == null) {
463 switchRemoved(dpid);
464 }
sangho5afd02a2015-02-03 20:07:35 -0800465 }
466
467 @Override
468 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700469 stopCollectorIfNeeded(collectors.remove(dpid));
470 }
471
472 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800473 if (collector != null) {
474 collector.stop();
475 }
476 }
477
478 @Override
479 public void switchChanged(Dpid dpid) {
480 }
481
482 @Override
483 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700484 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800485 }
486
487 @Override
488 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
489 }
490 }
491
helenyrwu89470f12016-08-12 13:18:10 -0700492 /**
493 * Builds a list of failover Groups whose primary live bucket failed over
494 * (i.e. bucket in use has changed).
495 *
496 * @param dpid DPID of switch whose port's status changed
497 * @param status new status of port
498 * @return list of groups whose primary live bucket failed over
499 */
500 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
501 List<Group> groupList = new ArrayList<>();
502 OFPortDesc desc = status.getDesc();
503 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
504 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
505 if (desc.isEnabled()) {
506 return groupList;
507 }
508 Iterator<Group> iterator = groupService.getGroups(id).iterator();
509 while (iterator.hasNext()) {
510 Group group = iterator.next();
511 if (group.type() == GroupDescription.Type.FAILOVER &&
512 checkFailoverGroup(group, id, portNumber)) {
513 groupList.add(group);
514 }
515 }
516 return groupList;
517 }
518
519 /**
520 * Checks whether the first live port in the failover group's bucket
521 * has failed over.
522 *
523 * @param group failover group to be checked for failover
524 * @param id device ID of switch whose port's status changed
525 * @param portNumber port number of port that was disabled
526 * @return whether the failover group experienced failover
527 */
528 private boolean checkFailoverGroup(Group group, DeviceId id,
529 PortNumber portNumber) {
530 boolean portReached = false;
531 boolean portEnabled = false;
532 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
533 GroupBucket bucket;
534 while (bIterator.hasNext() && !portReached) {
535 bucket = bIterator.next();
536 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
537 portEnabled = true;
538 }
539 if (bucket.watchPort().equals(portNumber)) {
540 portReached = true;
541 }
542 }
543 return portReached && !portEnabled;
544 }
545
sangho5afd02a2015-02-03 20:07:35 -0800546}