blob: da8e6941b535924149d7c972d5dd0a92e004cc6c [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;
Yuta HIGUCHI9721f852018-04-03 14:27:06 -070039import org.onlab.util.ItemNotFoundException;
dvaddireb57fdb02017-06-20 00:14:08 +053040import org.onosproject.cfg.ComponentConfigService;
sangho5afd02a2015-02-03 20:07:35 -080041import org.onosproject.core.GroupId;
42import org.onosproject.net.DeviceId;
helenyrwu89470f12016-08-12 13:18:10 -070043import org.onosproject.net.PortNumber;
44import org.onosproject.net.device.DeviceService;
Laszlo Papp84adcac2018-03-13 12:11:51 +000045import org.onosproject.net.driver.Driver;
jiangruia6b46092015-11-11 16:28:51 +080046import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080047import org.onosproject.net.group.DefaultGroup;
48import org.onosproject.net.group.Group;
helenyrwu89470f12016-08-12 13:18:10 -070049import org.onosproject.net.group.GroupBucket;
sangho5afd02a2015-02-03 20:07:35 -080050import org.onosproject.net.group.GroupBuckets;
51import org.onosproject.net.group.GroupDescription;
52import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080053import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080054import org.onosproject.net.group.GroupOperations;
55import org.onosproject.net.group.GroupProvider;
56import org.onosproject.net.group.GroupProviderRegistry;
57import org.onosproject.net.group.GroupProviderService;
helenyrwu89470f12016-08-12 13:18:10 -070058import org.onosproject.net.group.GroupService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070059import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080060import org.onosproject.net.provider.AbstractProvider;
61import org.onosproject.net.provider.ProviderId;
62import org.onosproject.openflow.controller.Dpid;
63import org.onosproject.openflow.controller.OpenFlowController;
64import org.onosproject.openflow.controller.OpenFlowEventListener;
65import org.onosproject.openflow.controller.OpenFlowSwitch;
66import org.onosproject.openflow.controller.OpenFlowSwitchListener;
67import org.onosproject.openflow.controller.RoleState;
dvaddireb57fdb02017-06-20 00:14:08 +053068import org.osgi.service.component.ComponentContext;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070069import org.projectfloodlight.openflow.protocol.OFBucketCounter;
Maksym Polovyi3585a7a2018-02-15 20:21:16 +020070import org.projectfloodlight.openflow.protocol.OFCapabilities;
sangho5afd02a2015-02-03 20:07:35 -080071import org.projectfloodlight.openflow.protocol.OFErrorMsg;
72import org.projectfloodlight.openflow.protocol.OFErrorType;
73import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
74import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
75import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080076import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080077import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
78import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
79import org.projectfloodlight.openflow.protocol.OFGroupType;
80import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070081import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080082import org.projectfloodlight.openflow.protocol.OFPortStatus;
83import org.projectfloodlight.openflow.protocol.OFStatsReply;
84import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080085import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080086import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080087import org.slf4j.Logger;
88
jiangruia6b46092015-11-11 16:28:51 +080089import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080090
91/**
92 * Provider which uses an OpenFlow controller to handle Group.
93 */
94@Component(immediate = true)
95public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
96
97 private final Logger log = getLogger(getClass());
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected OpenFlowController controller;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected GroupProviderRegistry providerRegistry;
104
jiangruia6b46092015-11-11 16:28:51 +0800105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected DriverService driverService;
107
helenyrwu89470f12016-08-12 13:18:10 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected DeviceService deviceService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected GroupService groupService;
113
dvaddireb57fdb02017-06-20 00:14:08 +0530114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected ComponentConfigService cfgService;
116
sangho5afd02a2015-02-03 20:07:35 -0800117 private GroupProviderService providerService;
118
dvaddireb57fdb02017-06-20 00:14:08 +0530119 private static final int DEFAULT_POLL_INTERVAL = 10;
120 private static final String COMPONENT = "org.onosproject.provider.of.group.impl.OpenFlowGroupProvider";
121 private static final String GROUP_POLL_INTERVAL_CONST = "groupPollInterval";
122
123 @Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL,
124 label = "Frequency (in seconds) for polling group statistics")
125 private int groupPollInterval = DEFAULT_POLL_INTERVAL;
sangho5afd02a2015-02-03 20:07:35 -0800126
127 private final InternalGroupProvider listener = new InternalGroupProvider();
128
sanghoa09f2742015-02-06 14:49:47 -0800129 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800130 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800131 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800132 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800133 Maps.newConcurrentMap();
134
sanghoa09f2742015-02-06 14:49:47 -0800135 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800136 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800137
sangho5afd02a2015-02-03 20:07:35 -0800138 /**
139 * Creates a OpenFlow group provider.
140 */
141 public OpenFlowGroupProvider() {
142 super(new ProviderId("of", "org.onosproject.provider.group"));
143 }
144
145 @Activate
dvaddireb57fdb02017-06-20 00:14:08 +0530146 public void activate(ComponentContext context) {
147 cfgService.registerProperties(getClass());
sangho5afd02a2015-02-03 20:07:35 -0800148 providerService = providerRegistry.register(this);
149 controller.addListener(listener);
150 controller.addEventListener(listener);
151
dvaddireb57fdb02017-06-20 00:14:08 +0530152 modified(context);
153
sangho5afd02a2015-02-03 20:07:35 -0800154 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800155 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530156 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
sangho01a883c2015-02-23 11:01:50 -0800157 gsc.start();
158 collectors.put(new Dpid(sw.getId()), gsc);
159 }
sangho5afd02a2015-02-03 20:07:35 -0800160 }
161
162 log.info("Started");
163 }
164
165 @Deactivate
166 public void deactivate() {
dvaddireb57fdb02017-06-20 00:14:08 +0530167 cfgService.unregisterProperties(getClass(), false);
sangho5afd02a2015-02-03 20:07:35 -0800168 providerRegistry.unregister(this);
169 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800170 collectors.values().forEach(GroupStatsCollector::stop);
171 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800172 log.info("Stopped");
173 }
174
dvaddireb57fdb02017-06-20 00:14:08 +0530175 @Modified
176 public void modified(ComponentContext context) {
177 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
178 Integer newGroupPollInterval = getIntegerProperty(properties, GROUP_POLL_INTERVAL_CONST);
179 if (newGroupPollInterval != null && newGroupPollInterval > 0
180 && newGroupPollInterval != groupPollInterval) {
181 groupPollInterval = newGroupPollInterval;
182 modifyPollInterval();
183 } else if (newGroupPollInterval != null && newGroupPollInterval <= 0) {
184 log.warn("groupPollInterval must be greater than 0");
185 //If the new value <= 0 reset property with old value.
186 cfgService.setProperty(COMPONENT, GROUP_POLL_INTERVAL_CONST, Integer.toString(groupPollInterval));
187 }
188 }
189
190 private void modifyPollInterval() {
191 collectors.values().forEach(gsc -> gsc.adjustRate(groupPollInterval));
192
193 }
sangho5afd02a2015-02-03 20:07:35 -0800194 @Override
195 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800196 final Dpid dpid = Dpid.dpid(deviceId.uri());
197 OpenFlowSwitch sw = controller.getSwitch(dpid);
198 for (GroupOperation groupOperation: groupOps.operations()) {
199 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800200 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800201 return;
202 }
sanghoa09f2742015-02-06 14:49:47 -0800203 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800204 GroupModBuilder builder = null;
205 if (driverService == null) {
206 builder = GroupModBuilder.builder(groupOperation.buckets(),
207 groupOperation.groupId(),
208 groupOperation.groupType(),
209 sw.factory(),
210 Optional.of(groupModXid));
211 } else {
212 builder = GroupModBuilder.builder(groupOperation.buckets(),
213 groupOperation.groupId(),
214 groupOperation.groupType(),
215 sw.factory(),
216 Optional.of(groupModXid),
217 Optional.of(driverService));
218 }
sangho5afd02a2015-02-03 20:07:35 -0800219 OFGroupMod groupMod = null;
220 switch (groupOperation.opType()) {
221 case ADD:
222 groupMod = builder.buildGroupAdd();
223 break;
224 case MODIFY:
225 groupMod = builder.buildGroupMod();
226 break;
227 case DELETE:
228 groupMod = builder.buildGroupDel();
229 break;
230 default:
231 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530232 return;
sangho5afd02a2015-02-03 20:07:35 -0800233 }
234 sw.sendMsg(groupMod);
Yi Tsengfa394de2017-02-01 11:26:40 -0800235 GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber());
sangho22a805d2015-02-13 09:45:43 -0800236 pendingGroupOperations.put(groudId, groupOperation);
237 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800238 }
239 }
240
241 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
242 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200243 OpenFlowSwitch sw = controller.getSwitch(dpid);
244 boolean containsGroupStats = false;
245 if (sw != null && sw.features() != null) {
246 containsGroupStats = sw.features()
247 .getCapabilities()
248 .contains(OFCapabilities.GROUP_STATS);
249 }
sangho5afd02a2015-02-03 20:07:35 -0800250
251 OFGroupStatsReply groupStatsReply = null;
252 OFGroupDescStatsReply groupDescStatsReply = null;
253
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200254 if (containsGroupStats) {
255 synchronized (groupStats) {
256 if (statsReply.getStatsType() == OFStatsType.GROUP) {
257 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
258 if (reply != null) {
259 groupStatsReply = (OFGroupStatsReply) statsReply;
260 groupDescStatsReply = (OFGroupDescStatsReply) reply;
261 groupStats.remove(statsReply.getXid() + 1);
262 } else {
263 groupStats.put(statsReply.getXid(), statsReply);
264 }
265 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
266 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
267 if (reply != null) {
268 groupStatsReply = (OFGroupStatsReply) reply;
269 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
270 groupStats.remove(statsReply.getXid() - 1);
271 } else {
272 groupStats.put(statsReply.getXid(), statsReply);
273 }
sanghoa09f2742015-02-06 14:49:47 -0800274 }
sangho5afd02a2015-02-03 20:07:35 -0800275 }
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200276 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
277 // We are only requesting group desc stats; see GroupStatsCollector.java:sendGroupStatisticRequests()
278 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
sangho5afd02a2015-02-03 20:07:35 -0800279 }
280
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200281 if (providerService != null && groupDescStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800282 Collection<Group> groups = buildGroupMetrics(deviceId,
283 groupStatsReply, groupDescStatsReply);
284 providerService.pushGroupMetrics(deviceId, groups);
285 for (Group group: groups) {
286 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800287 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800288 }
289 }
290 }
291
292 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
293 OFGroupStatsReply groupStatsReply,
294 OFGroupDescStatsReply groupDescStatsReply) {
295
296 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800297 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700298
sangho5afd02a2015-02-03 20:07:35 -0800299 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
300 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800301 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800302 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800303 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
304 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800305 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
306 groups.put(id, group);
307 }
308
Maksym Polovyi3585a7a2018-02-15 20:21:16 +0200309 if (groupStatsReply == null) {
310 return groups.values();
311 }
312
sangho5afd02a2015-02-03 20:07:35 -0800313 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
314 int groupId = entry.getGroup().getGroupNumber();
315 DefaultGroup group = (DefaultGroup) groups.get(groupId);
316 if (group != null) {
317 group.setBytes(entry.getByteCount().getValue());
318 group.setLife(entry.getDurationSec());
319 group.setPackets(entry.getPacketCount().getValue());
320 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700321 int bucketIndex = 0;
322 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
323 ((StoredGroupBucketEntry) group.buckets().buckets()
324 .get(bucketIndex))
325 .setPackets(bucketStats
326 .getPacketCount().getValue());
327 ((StoredGroupBucketEntry) group.buckets().buckets()
328 .get(bucketIndex))
329 .setBytes(entry.getBucketStats()
330 .get(bucketIndex)
331 .getByteCount().getValue());
332 bucketIndex++;
333 }
sangho5afd02a2015-02-03 20:07:35 -0800334 }
335 }
336
337 return groups.values();
338 }
339
340 private GroupDescription.Type getGroupType(OFGroupType type) {
341 switch (type) {
342 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700343 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800344 case INDIRECT:
345 return GroupDescription.Type.INDIRECT;
346 case SELECT:
347 return GroupDescription.Type.SELECT;
348 case FF:
349 return GroupDescription.Type.FAILOVER;
350 default:
351 log.error("Unsupported OF group type : {}", type);
352 break;
353 }
354 return null;
355 }
356
sanghoa09f2742015-02-06 14:49:47 -0800357 /**
358 * Returns a transaction ID for entire group operations and increases
359 * the counter by the number given.
360 *
361 * @param increase the amount to increase the counter by
362 * @return a transaction ID
363 */
364 public static long getXidAndAdd(int increase) {
365 return XID_COUNTER.getAndAdd(increase);
366 }
367
sangho01a883c2015-02-23 11:01:50 -0800368 private boolean isGroupSupported(OpenFlowSwitch sw) {
369 if (sw.factory().getVersion() == OFVersion.OF_10 ||
370 sw.factory().getVersion() == OFVersion.OF_11 ||
Laszlo Papp84adcac2018-03-13 12:11:51 +0000371 sw.factory().getVersion() == OFVersion.OF_12 ||
372 !isGroupCapable(sw)) {
sangho01a883c2015-02-23 11:01:50 -0800373 return false;
374 }
375
376 return true;
377 }
378
Laszlo Papp84adcac2018-03-13 12:11:51 +0000379 /**
380 * Determine whether the given switch is group-capable.
381 *
382 * @param sw switch
383 * @return the boolean value of groupCapable property, or true if it is not configured.
384 */
385 private boolean isGroupCapable(OpenFlowSwitch sw) {
Yuta HIGUCHI9721f852018-04-03 14:27:06 -0700386 Driver driver;
387 try {
388 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid())));
389 } catch (ItemNotFoundException e) {
390 driver = driverService.getDriver(sw.manufacturerDescription(),
391 sw.hardwareDescription(),
392 sw.softwareDescription());
393 }
Laszlo Papp84adcac2018-03-13 12:11:51 +0000394 if (driver == null) {
395 return true;
396 }
Devin Limf853a592018-04-06 18:21:11 -0700397 String isGroupCapable = driver.getProperty(GROUP_CAPABLE);
398 return isGroupCapable == null || Boolean.parseBoolean(isGroupCapable);
Laszlo Papp84adcac2018-03-13 12:11:51 +0000399 }
400
sangho5afd02a2015-02-03 20:07:35 -0800401 private class InternalGroupProvider
402 implements OpenFlowSwitchListener, OpenFlowEventListener {
403
404 @Override
405 public void handleMessage(Dpid dpid, OFMessage msg) {
406 switch (msg.getType()) {
407 case STATS_REPLY:
408 pushGroupMetrics(dpid, (OFStatsReply) msg);
409 break;
410 case ERROR:
411 OFErrorMsg errorMsg = (OFErrorMsg) msg;
412 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800413 GroupId pendingGroupId = null;
414 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800415 if (entry.getValue() == errorMsg.getXid()) {
416 pendingGroupId = entry.getKey();
417 break;
418 }
419 }
sangho22a805d2015-02-13 09:45:43 -0800420 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800421 log.warn("Error for unknown group operation: {}",
422 errorMsg.getXid());
423 } else {
424 GroupOperation operation =
425 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800426 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800427 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800428 OFGroupModFailedCode code =
429 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
430 GroupMsgErrorCode failureCode =
431 GroupMsgErrorCode.values()[(code.ordinal())];
432 GroupOperation failedOperation = GroupOperation
433 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800434 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800435 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800436 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800437 pendingGroupOperations.remove(pendingGroupId);
438 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800439 } else {
440 log.error("Cannot find pending group operation with group ID: {}",
441 pendingGroupId);
442 }
sangho5afd02a2015-02-03 20:07:35 -0800443 }
444 break;
445 }
446 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800447 break;
sangho5afd02a2015-02-03 20:07:35 -0800448 }
449 }
450
451 @Override
452 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800453 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500454 if (sw == null) {
455 return;
456 }
Laszlo Pappcd91bfd2018-01-29 14:30:34 +0000457 if (isGroupSupported(sw)) {
dvaddireb57fdb02017-06-20 00:14:08 +0530458 GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700459 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900460 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800461 }
Lei Xudee1aff2015-10-16 00:45:10 -0500462
463 //figure out race condition
464 if (controller.getSwitch(dpid) == null) {
465 switchRemoved(dpid);
466 }
sangho5afd02a2015-02-03 20:07:35 -0800467 }
468
469 @Override
470 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700471 stopCollectorIfNeeded(collectors.remove(dpid));
472 }
473
474 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800475 if (collector != null) {
476 collector.stop();
477 }
478 }
479
480 @Override
481 public void switchChanged(Dpid dpid) {
482 }
483
484 @Override
485 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700486 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800487 }
488
489 @Override
490 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
491 }
492 }
493
helenyrwu89470f12016-08-12 13:18:10 -0700494 /**
495 * Builds a list of failover Groups whose primary live bucket failed over
496 * (i.e. bucket in use has changed).
497 *
498 * @param dpid DPID of switch whose port's status changed
499 * @param status new status of port
500 * @return list of groups whose primary live bucket failed over
501 */
502 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
503 List<Group> groupList = new ArrayList<>();
504 OFPortDesc desc = status.getDesc();
505 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
506 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
507 if (desc.isEnabled()) {
508 return groupList;
509 }
510 Iterator<Group> iterator = groupService.getGroups(id).iterator();
511 while (iterator.hasNext()) {
512 Group group = iterator.next();
513 if (group.type() == GroupDescription.Type.FAILOVER &&
514 checkFailoverGroup(group, id, portNumber)) {
515 groupList.add(group);
516 }
517 }
518 return groupList;
519 }
520
521 /**
522 * Checks whether the first live port in the failover group's bucket
523 * has failed over.
524 *
525 * @param group failover group to be checked for failover
526 * @param id device ID of switch whose port's status changed
527 * @param portNumber port number of port that was disabled
528 * @return whether the failover group experienced failover
529 */
530 private boolean checkFailoverGroup(Group group, DeviceId id,
531 PortNumber portNumber) {
532 boolean portReached = false;
533 boolean portEnabled = false;
534 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
535 GroupBucket bucket;
536 while (bIterator.hasNext() && !portReached) {
537 bucket = bIterator.next();
538 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
539 portEnabled = true;
540 }
541 if (bucket.watchPort().equals(portNumber)) {
542 portReached = true;
543 }
544 }
545 return portReached && !portEnabled;
546 }
547
sangho5afd02a2015-02-03 20:07:35 -0800548}