sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.provider.of.group.impl; |
| 18 | |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 22 | import java.util.Collection; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 23 | import java.util.Iterator; |
| 24 | import java.util.List; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 25 | import java.util.Map; |
| 26 | import java.util.Optional; |
| 27 | import java.util.concurrent.atomic.AtomicLong; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 28 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.onosproject.core.DefaultGroupId; |
| 35 | import org.onosproject.core.GroupId; |
| 36 | import org.onosproject.net.DeviceId; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.device.DeviceService; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 39 | import org.onosproject.net.driver.DriverService; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 40 | import org.onosproject.net.group.DefaultGroup; |
| 41 | import org.onosproject.net.group.Group; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 42 | import org.onosproject.net.group.GroupBucket; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 43 | import org.onosproject.net.group.GroupBuckets; |
| 44 | import org.onosproject.net.group.GroupDescription; |
| 45 | import org.onosproject.net.group.GroupOperation; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 46 | import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 47 | import org.onosproject.net.group.GroupOperations; |
| 48 | import org.onosproject.net.group.GroupProvider; |
| 49 | import org.onosproject.net.group.GroupProviderRegistry; |
| 50 | import org.onosproject.net.group.GroupProviderService; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 51 | import org.onosproject.net.group.GroupService; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 52 | import org.onosproject.net.group.StoredGroupBucketEntry; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 53 | import org.onosproject.net.provider.AbstractProvider; |
| 54 | import org.onosproject.net.provider.ProviderId; |
| 55 | import org.onosproject.openflow.controller.Dpid; |
| 56 | import org.onosproject.openflow.controller.OpenFlowController; |
| 57 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 58 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 59 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 60 | import org.onosproject.openflow.controller.RoleState; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFBucketCounter; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 63 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
| 64 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 65 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 66 | import org.projectfloodlight.openflow.protocol.OFGroupMod; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 69 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
| 70 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 71 | import org.projectfloodlight.openflow.protocol.OFMessage; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 72 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 74 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 75 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 78 | import org.slf4j.Logger; |
| 79 | |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 80 | import com.google.common.collect.Maps; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Provider which uses an OpenFlow controller to handle Group. |
| 84 | */ |
| 85 | @Component(immediate = true) |
| 86 | public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider { |
| 87 | |
| 88 | private final Logger log = getLogger(getClass()); |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected OpenFlowController controller; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected GroupProviderRegistry providerRegistry; |
| 95 | |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected DriverService driverService; |
| 98 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected DeviceService deviceService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected GroupService groupService; |
| 104 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 105 | private GroupProviderService providerService; |
| 106 | |
| 107 | static final int POLL_INTERVAL = 10; |
| 108 | |
| 109 | private final InternalGroupProvider listener = new InternalGroupProvider(); |
| 110 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 111 | private static final AtomicLong XID_COUNTER = new AtomicLong(1); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 112 | private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap(); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 113 | private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap(); |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 114 | private final Map<GroupId, GroupOperation> pendingGroupOperations = |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 115 | Maps.newConcurrentMap(); |
| 116 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 117 | /* Map<Group ID, Transaction ID> */ |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 118 | private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap(); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 119 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Creates a OpenFlow group provider. |
| 122 | */ |
| 123 | public OpenFlowGroupProvider() { |
| 124 | super(new ProviderId("of", "org.onosproject.provider.group")); |
| 125 | } |
| 126 | |
| 127 | @Activate |
| 128 | public void activate() { |
| 129 | providerService = providerRegistry.register(this); |
| 130 | controller.addListener(listener); |
| 131 | controller.addEventListener(listener); |
| 132 | |
| 133 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 134 | if (isGroupSupported(sw)) { |
| 135 | GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL); |
| 136 | gsc.start(); |
| 137 | collectors.put(new Dpid(sw.getId()), gsc); |
| 138 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | log.info("Started"); |
| 142 | } |
| 143 | |
| 144 | @Deactivate |
| 145 | public void deactivate() { |
| 146 | providerRegistry.unregister(this); |
| 147 | providerService = null; |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 148 | collectors.values().forEach(GroupStatsCollector::stop); |
| 149 | collectors.clear(); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 150 | log.info("Stopped"); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) { |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 155 | final Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 156 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 157 | for (GroupOperation groupOperation: groupOps.operations()) { |
| 158 | if (sw == null) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 159 | log.error("SW {} is not found", dpid); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 160 | return; |
| 161 | } |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 162 | final Long groupModXid = XID_COUNTER.getAndIncrement(); |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 163 | GroupModBuilder builder = null; |
| 164 | if (driverService == null) { |
| 165 | builder = GroupModBuilder.builder(groupOperation.buckets(), |
| 166 | groupOperation.groupId(), |
| 167 | groupOperation.groupType(), |
| 168 | sw.factory(), |
| 169 | Optional.of(groupModXid)); |
| 170 | } else { |
| 171 | builder = GroupModBuilder.builder(groupOperation.buckets(), |
| 172 | groupOperation.groupId(), |
| 173 | groupOperation.groupType(), |
| 174 | sw.factory(), |
| 175 | Optional.of(groupModXid), |
| 176 | Optional.of(driverService)); |
| 177 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 178 | OFGroupMod groupMod = null; |
| 179 | switch (groupOperation.opType()) { |
| 180 | case ADD: |
| 181 | groupMod = builder.buildGroupAdd(); |
| 182 | break; |
| 183 | case MODIFY: |
| 184 | groupMod = builder.buildGroupMod(); |
| 185 | break; |
| 186 | case DELETE: |
| 187 | groupMod = builder.buildGroupDel(); |
| 188 | break; |
| 189 | default: |
| 190 | log.error("Unsupported Group operation"); |
Satish K | e6d91c5 | 2015-11-21 19:04:20 +0530 | [diff] [blame] | 191 | return; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 192 | } |
| 193 | sw.sendMsg(groupMod); |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 194 | GroupId groudId = new DefaultGroupId(groupMod.getGroup().getGroupNumber()); |
| 195 | pendingGroupOperations.put(groudId, groupOperation); |
| 196 | pendingXidMaps.put(groudId, groupModXid); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) { |
| 201 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
| 202 | |
| 203 | OFGroupStatsReply groupStatsReply = null; |
| 204 | OFGroupDescStatsReply groupDescStatsReply = null; |
| 205 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 206 | synchronized (groupStats) { |
| 207 | if (statsReply.getStatsType() == OFStatsType.GROUP) { |
| 208 | OFStatsReply reply = groupStats.get(statsReply.getXid() + 1); |
| 209 | if (reply != null) { |
| 210 | groupStatsReply = (OFGroupStatsReply) statsReply; |
| 211 | groupDescStatsReply = (OFGroupDescStatsReply) reply; |
| 212 | groupStats.remove(statsReply.getXid() + 1); |
| 213 | } else { |
| 214 | groupStats.put(statsReply.getXid(), statsReply); |
| 215 | } |
| 216 | } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) { |
| 217 | OFStatsReply reply = groupStats.get(statsReply.getXid() - 1); |
| 218 | if (reply != null) { |
| 219 | groupStatsReply = (OFGroupStatsReply) reply; |
| 220 | groupDescStatsReply = (OFGroupDescStatsReply) statsReply; |
| 221 | groupStats.remove(statsReply.getXid() - 1); |
| 222 | } else { |
| 223 | groupStats.put(statsReply.getXid(), statsReply); |
| 224 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 228 | if (providerService != null && groupStatsReply != null) { |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 229 | Collection<Group> groups = buildGroupMetrics(deviceId, |
| 230 | groupStatsReply, groupDescStatsReply); |
| 231 | providerService.pushGroupMetrics(deviceId, groups); |
| 232 | for (Group group: groups) { |
| 233 | pendingGroupOperations.remove(group.id()); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 234 | pendingXidMaps.remove(group.id()); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private Collection<Group> buildGroupMetrics(DeviceId deviceId, |
| 240 | OFGroupStatsReply groupStatsReply, |
| 241 | OFGroupDescStatsReply groupDescStatsReply) { |
| 242 | |
| 243 | Map<Integer, Group> groups = Maps.newHashMap(); |
Hyunsun Moon | a834c18 | 2015-12-16 03:01:56 -0800 | [diff] [blame] | 244 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 245 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 246 | for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) { |
| 247 | int id = entry.getGroup().getGroupNumber(); |
| 248 | GroupId groupId = new DefaultGroupId(id); |
| 249 | GroupDescription.Type type = getGroupType(entry.getGroupType()); |
Hyunsun Moon | a834c18 | 2015-12-16 03:01:56 -0800 | [diff] [blame] | 250 | GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(), |
| 251 | entry.getGroupType(), driverService).build(); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 252 | DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets); |
| 253 | groups.put(id, group); |
| 254 | } |
| 255 | |
| 256 | for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) { |
| 257 | int groupId = entry.getGroup().getGroupNumber(); |
| 258 | DefaultGroup group = (DefaultGroup) groups.get(groupId); |
| 259 | if (group != null) { |
| 260 | group.setBytes(entry.getByteCount().getValue()); |
| 261 | group.setLife(entry.getDurationSec()); |
| 262 | group.setPackets(entry.getPacketCount().getValue()); |
| 263 | group.setReferenceCount(entry.getRefCount()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 264 | int bucketIndex = 0; |
| 265 | for (OFBucketCounter bucketStats:entry.getBucketStats()) { |
| 266 | ((StoredGroupBucketEntry) group.buckets().buckets() |
| 267 | .get(bucketIndex)) |
| 268 | .setPackets(bucketStats |
| 269 | .getPacketCount().getValue()); |
| 270 | ((StoredGroupBucketEntry) group.buckets().buckets() |
| 271 | .get(bucketIndex)) |
| 272 | .setBytes(entry.getBucketStats() |
| 273 | .get(bucketIndex) |
| 274 | .getByteCount().getValue()); |
| 275 | bucketIndex++; |
| 276 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | return groups.values(); |
| 281 | } |
| 282 | |
| 283 | private GroupDescription.Type getGroupType(OFGroupType type) { |
| 284 | switch (type) { |
| 285 | case ALL: |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 286 | return GroupDescription.Type.ALL; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 287 | case INDIRECT: |
| 288 | return GroupDescription.Type.INDIRECT; |
| 289 | case SELECT: |
| 290 | return GroupDescription.Type.SELECT; |
| 291 | case FF: |
| 292 | return GroupDescription.Type.FAILOVER; |
| 293 | default: |
| 294 | log.error("Unsupported OF group type : {}", type); |
| 295 | break; |
| 296 | } |
| 297 | return null; |
| 298 | } |
| 299 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 300 | /** |
| 301 | * Returns a transaction ID for entire group operations and increases |
| 302 | * the counter by the number given. |
| 303 | * |
| 304 | * @param increase the amount to increase the counter by |
| 305 | * @return a transaction ID |
| 306 | */ |
| 307 | public static long getXidAndAdd(int increase) { |
| 308 | return XID_COUNTER.getAndAdd(increase); |
| 309 | } |
| 310 | |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 311 | private boolean isGroupSupported(OpenFlowSwitch sw) { |
| 312 | if (sw.factory().getVersion() == OFVersion.OF_10 || |
| 313 | sw.factory().getVersion() == OFVersion.OF_11 || |
| 314 | sw.factory().getVersion() == OFVersion.OF_12) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 321 | private class InternalGroupProvider |
| 322 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 323 | |
| 324 | @Override |
| 325 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 326 | switch (msg.getType()) { |
| 327 | case STATS_REPLY: |
| 328 | pushGroupMetrics(dpid, (OFStatsReply) msg); |
| 329 | break; |
| 330 | case ERROR: |
| 331 | OFErrorMsg errorMsg = (OFErrorMsg) msg; |
| 332 | if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) { |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 333 | GroupId pendingGroupId = null; |
| 334 | for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 335 | if (entry.getValue() == errorMsg.getXid()) { |
| 336 | pendingGroupId = entry.getKey(); |
| 337 | break; |
| 338 | } |
| 339 | } |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 340 | if (pendingGroupId == null) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 341 | log.warn("Error for unknown group operation: {}", |
| 342 | errorMsg.getXid()); |
| 343 | } else { |
| 344 | GroupOperation operation = |
| 345 | pendingGroupOperations.get(pendingGroupId); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 346 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 347 | if (operation != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 348 | OFGroupModFailedCode code = |
| 349 | ((OFGroupModFailedErrorMsg) errorMsg).getCode(); |
| 350 | GroupMsgErrorCode failureCode = |
| 351 | GroupMsgErrorCode.values()[(code.ordinal())]; |
| 352 | GroupOperation failedOperation = GroupOperation |
| 353 | .createFailedGroupOperation(operation, failureCode); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 354 | log.warn("Received a group mod error {}", msg); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 355 | providerService.groupOperationFailed(deviceId, |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 356 | failedOperation); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 357 | pendingGroupOperations.remove(pendingGroupId); |
| 358 | pendingXidMaps.remove(pendingGroupId); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 359 | } else { |
| 360 | log.error("Cannot find pending group operation with group ID: {}", |
| 361 | pendingGroupId); |
| 362 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 363 | } |
| 364 | break; |
| 365 | } |
| 366 | default: |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 367 | break; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void switchAdded(Dpid dpid) { |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 373 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 374 | if (sw == null) { |
| 375 | return; |
| 376 | } |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 377 | if (isGroupSupported(sw)) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 378 | GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL); |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 379 | gsc.start(); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 380 | stopCollectorIfNeeded(collectors.put(dpid, gsc)); |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 381 | } |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 382 | |
| 383 | //figure out race condition |
| 384 | if (controller.getSwitch(dpid) == null) { |
| 385 | switchRemoved(dpid); |
| 386 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void switchRemoved(Dpid dpid) { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 391 | stopCollectorIfNeeded(collectors.remove(dpid)); |
| 392 | } |
| 393 | |
| 394 | private void stopCollectorIfNeeded(GroupStatsCollector collector) { |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 395 | if (collector != null) { |
| 396 | collector.stop(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public void switchChanged(Dpid dpid) { |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void portChanged(Dpid dpid, OFPortStatus status) { |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 406 | providerService.notifyOfFailovers(checkFailoverGroups(dpid, status)); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 411 | } |
| 412 | } |
| 413 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 414 | /** |
| 415 | * Builds a list of failover Groups whose primary live bucket failed over |
| 416 | * (i.e. bucket in use has changed). |
| 417 | * |
| 418 | * @param dpid DPID of switch whose port's status changed |
| 419 | * @param status new status of port |
| 420 | * @return list of groups whose primary live bucket failed over |
| 421 | */ |
| 422 | private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) { |
| 423 | List<Group> groupList = new ArrayList<>(); |
| 424 | OFPortDesc desc = status.getDesc(); |
| 425 | PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber()); |
| 426 | DeviceId id = DeviceId.deviceId(Dpid.uri(dpid)); |
| 427 | if (desc.isEnabled()) { |
| 428 | return groupList; |
| 429 | } |
| 430 | Iterator<Group> iterator = groupService.getGroups(id).iterator(); |
| 431 | while (iterator.hasNext()) { |
| 432 | Group group = iterator.next(); |
| 433 | if (group.type() == GroupDescription.Type.FAILOVER && |
| 434 | checkFailoverGroup(group, id, portNumber)) { |
| 435 | groupList.add(group); |
| 436 | } |
| 437 | } |
| 438 | return groupList; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Checks whether the first live port in the failover group's bucket |
| 443 | * has failed over. |
| 444 | * |
| 445 | * @param group failover group to be checked for failover |
| 446 | * @param id device ID of switch whose port's status changed |
| 447 | * @param portNumber port number of port that was disabled |
| 448 | * @return whether the failover group experienced failover |
| 449 | */ |
| 450 | private boolean checkFailoverGroup(Group group, DeviceId id, |
| 451 | PortNumber portNumber) { |
| 452 | boolean portReached = false; |
| 453 | boolean portEnabled = false; |
| 454 | Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator(); |
| 455 | GroupBucket bucket; |
| 456 | while (bIterator.hasNext() && !portReached) { |
| 457 | bucket = bIterator.next(); |
| 458 | if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) { |
| 459 | portEnabled = true; |
| 460 | } |
| 461 | if (bucket.watchPort().equals(portNumber)) { |
| 462 | portReached = true; |
| 463 | } |
| 464 | } |
| 465 | return portReached && !portEnabled; |
| 466 | } |
| 467 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 468 | } |