Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame^] | 16 | package org.onosproject.segmentrouting.grouphandler; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Arrays; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Iterator; |
| 24 | import java.util.List; |
| 25 | |
sangho | 32a5932 | 2015-02-17 12:07:41 -0800 | [diff] [blame] | 26 | import org.onlab.packet.MplsLabel; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.GroupId; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame^] | 29 | import org.onosproject.segmentrouting.grouphandler.GroupBucketIdentifier.BucketOutputType; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.group.DefaultGroupBucket; |
| 35 | import org.onosproject.net.group.DefaultGroupDescription; |
| 36 | import org.onosproject.net.group.GroupBucket; |
| 37 | import org.onosproject.net.group.GroupBuckets; |
| 38 | import org.onosproject.net.group.GroupDescription; |
| 39 | import org.onosproject.net.group.GroupEvent; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 40 | import org.onosproject.net.group.GroupService; |
| 41 | import org.onosproject.net.link.LinkService; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | /** |
| 45 | * A module to create group chains based on the specified device |
| 46 | * ports and label stack to be applied on each port. |
| 47 | */ |
| 48 | public class PolicyGroupHandler extends DefaultGroupHandler { |
| 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 51 | private HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier> dependentGroups = |
| 52 | new HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 55 | * Policy group handler constructor. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 56 | * |
| 57 | * @param deviceId device identifier |
| 58 | * @param appId application identifier |
| 59 | * @param config interface to retrieve the device properties |
| 60 | * @param linkService link service object |
| 61 | * @param groupService group service object |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 62 | */ |
| 63 | public PolicyGroupHandler(DeviceId deviceId, |
| 64 | ApplicationId appId, |
| 65 | DeviceProperties config, |
| 66 | LinkService linkService, |
| 67 | GroupService groupService) { |
| 68 | super(deviceId, appId, config, linkService, groupService); |
| 69 | } |
| 70 | |
| 71 | public PolicyGroupIdentifier createPolicyGroupChain(String id, |
| 72 | List<PolicyGroupParams> params) { |
| 73 | List<GroupBucketIdentifier> bucketIds = new ArrayList<GroupBucketIdentifier>(); |
| 74 | for (PolicyGroupParams param: params) { |
| 75 | List<PortNumber> ports = param.getPorts(); |
| 76 | if (ports == null) { |
| 77 | log.warn("createPolicyGroupChain in sw {} with wrong " |
| 78 | + "input parameters", deviceId); |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | int labelStackSize = (param.getLabelStack() != null) ? |
| 83 | param.getLabelStack().size() : 0; |
| 84 | |
| 85 | if (labelStackSize > 1) { |
| 86 | for (PortNumber sp : ports) { |
| 87 | PolicyGroupIdentifier previousGroupkey = null; |
| 88 | DeviceId neighbor = portDeviceMap.get(sp); |
| 89 | for (int idx = 0; idx < param.getLabelStack().size(); idx++) { |
| 90 | int label = param.getLabelStack().get(idx).intValue(); |
| 91 | if (idx == (labelStackSize - 1)) { |
| 92 | // Innermost Group |
| 93 | GroupBucketIdentifier bucketId = |
| 94 | new GroupBucketIdentifier(label, |
| 95 | previousGroupkey); |
| 96 | bucketIds.add(bucketId); |
| 97 | } else if (idx == 0) { |
| 98 | // Outermost Group |
| 99 | List<GroupBucket> outBuckets = new ArrayList<GroupBucket>(); |
| 100 | GroupBucketIdentifier bucketId = |
| 101 | new GroupBucketIdentifier(label, sp); |
| 102 | PolicyGroupIdentifier key = new |
| 103 | PolicyGroupIdentifier(id, |
| 104 | Arrays.asList(param), |
| 105 | Arrays.asList(bucketId)); |
| 106 | TrafficTreatment.Builder tBuilder = |
| 107 | DefaultTrafficTreatment.builder(); |
| 108 | tBuilder.setOutput(sp) |
| 109 | .setEthDst(deviceConfig. |
| 110 | getDeviceMac(neighbor)) |
| 111 | .setEthSrc(nodeMacAddr) |
| 112 | .pushMpls() |
sangho | 32a5932 | 2015-02-17 12:07:41 -0800 | [diff] [blame] | 113 | .setMpls(MplsLabel.mplsLabel(label)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 114 | outBuckets.add(DefaultGroupBucket. |
| 115 | createSelectGroupBucket(tBuilder.build())); |
| 116 | GroupDescription desc = new |
| 117 | DefaultGroupDescription(deviceId, |
| 118 | GroupDescription.Type.INDIRECT, |
| 119 | new GroupBuckets(outBuckets)); |
| 120 | //TODO: BoS |
| 121 | previousGroupkey = key; |
| 122 | groupService.addGroup(desc); |
| 123 | } else { |
| 124 | // Intermediate Groups |
| 125 | GroupBucketIdentifier bucketId = |
| 126 | new GroupBucketIdentifier(label, |
| 127 | previousGroupkey); |
| 128 | PolicyGroupIdentifier key = new |
| 129 | PolicyGroupIdentifier(id, |
| 130 | Arrays.asList(param), |
| 131 | Arrays.asList(bucketId)); |
| 132 | // Add to group dependency list |
| 133 | dependentGroups.put(previousGroupkey, key); |
| 134 | previousGroupkey = key; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } else { |
| 139 | int label = -1; |
| 140 | if (labelStackSize == 1) { |
| 141 | label = param.getLabelStack().get(0).intValue(); |
| 142 | } |
| 143 | for (PortNumber sp : ports) { |
| 144 | GroupBucketIdentifier bucketId = |
| 145 | new GroupBucketIdentifier(label, sp); |
| 146 | bucketIds.add(bucketId); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | PolicyGroupIdentifier innermostGroupkey = null; |
| 151 | if (!bucketIds.isEmpty()) { |
| 152 | innermostGroupkey = new |
| 153 | PolicyGroupIdentifier(id, |
| 154 | params, |
| 155 | bucketIds); |
| 156 | // Add to group dependency list |
| 157 | boolean fullyResolved = true; |
| 158 | for (GroupBucketIdentifier bucketId:bucketIds) { |
| 159 | if (bucketId.type() == BucketOutputType.GROUP) { |
| 160 | dependentGroups.put(bucketId.outGroup(), |
| 161 | innermostGroupkey); |
| 162 | fullyResolved = false; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (fullyResolved) { |
| 167 | List<GroupBucket> outBuckets = new ArrayList<GroupBucket>(); |
| 168 | for (GroupBucketIdentifier bucketId:bucketIds) { |
| 169 | DeviceId neighbor = portDeviceMap. |
| 170 | get(bucketId.outPort()); |
| 171 | TrafficTreatment.Builder tBuilder = |
| 172 | DefaultTrafficTreatment.builder(); |
| 173 | tBuilder.setOutput(bucketId.outPort()) |
| 174 | .setEthDst(deviceConfig. |
| 175 | getDeviceMac(neighbor)) |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 176 | .setEthSrc(nodeMacAddr); |
| 177 | if (bucketId.label() != NeighborSet.NO_EDGE_LABEL) { |
| 178 | tBuilder.pushMpls() |
sangho | 32a5932 | 2015-02-17 12:07:41 -0800 | [diff] [blame] | 179 | .setMpls(MplsLabel.mplsLabel(bucketId.label())); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 180 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 181 | //TODO: BoS |
| 182 | outBuckets.add(DefaultGroupBucket. |
| 183 | createSelectGroupBucket(tBuilder.build())); |
| 184 | } |
| 185 | GroupDescription desc = new |
| 186 | DefaultGroupDescription(deviceId, |
| 187 | GroupDescription.Type.SELECT, |
| 188 | new GroupBuckets(outBuckets)); |
| 189 | groupService.addGroup(desc); |
| 190 | } |
| 191 | } |
| 192 | return innermostGroupkey; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | protected void handleGroupEvent(GroupEvent event) { |
| 197 | if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 198 | if (dependentGroups.get(event.subject().appCookie()) != null) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 199 | PolicyGroupIdentifier dependentGroupKey = dependentGroups.get(event.subject().appCookie()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 200 | dependentGroups.remove(event.subject().appCookie()); |
| 201 | boolean fullyResolved = true; |
| 202 | for (GroupBucketIdentifier bucketId: |
| 203 | dependentGroupKey.bucketIds()) { |
| 204 | if (bucketId.type() != BucketOutputType.GROUP) { |
| 205 | continue; |
| 206 | } |
| 207 | if (dependentGroups.containsKey(bucketId.outGroup())) { |
| 208 | fullyResolved = false; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (fullyResolved) { |
| 214 | List<GroupBucket> outBuckets = new ArrayList<GroupBucket>(); |
| 215 | for (GroupBucketIdentifier bucketId: |
| 216 | dependentGroupKey.bucketIds()) { |
| 217 | TrafficTreatment.Builder tBuilder = |
| 218 | DefaultTrafficTreatment.builder(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 219 | if (bucketId.label() != NeighborSet.NO_EDGE_LABEL) { |
| 220 | tBuilder.pushMpls() |
| 221 | .setMpls(MplsLabel. |
| 222 | mplsLabel(bucketId.label())); |
| 223 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 224 | //TODO: BoS |
| 225 | if (bucketId.type() == BucketOutputType.PORT) { |
| 226 | DeviceId neighbor = portDeviceMap. |
| 227 | get(bucketId.outPort()); |
| 228 | tBuilder.setOutput(bucketId.outPort()) |
| 229 | .setEthDst(deviceConfig. |
| 230 | getDeviceMac(neighbor)) |
| 231 | .setEthSrc(nodeMacAddr); |
| 232 | } else { |
| 233 | if (groupService. |
| 234 | getGroup(deviceId, |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 235 | getGroupKey(bucketId. |
| 236 | outGroup())) == null) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 237 | throw new IllegalStateException(); |
| 238 | } |
| 239 | GroupId indirectGroupId = groupService. |
| 240 | getGroup(deviceId, |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 241 | getGroupKey(bucketId. |
| 242 | outGroup())).id(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 243 | tBuilder.group(indirectGroupId); |
| 244 | } |
| 245 | outBuckets.add(DefaultGroupBucket. |
| 246 | createSelectGroupBucket(tBuilder.build())); |
| 247 | } |
| 248 | GroupDescription desc = new |
| 249 | DefaultGroupDescription(deviceId, |
| 250 | GroupDescription.Type.SELECT, |
| 251 | new GroupBuckets(outBuckets)); |
| 252 | groupService.addGroup(desc); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 258 | public PolicyGroupIdentifier generatePolicyGroupKey(String id, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 259 | List<PolicyGroupParams> params) { |
| 260 | List<GroupBucketIdentifier> bucketIds = new ArrayList<GroupBucketIdentifier>(); |
| 261 | for (PolicyGroupParams param: params) { |
| 262 | List<PortNumber> ports = param.getPorts(); |
| 263 | if (ports == null) { |
| 264 | log.warn("generateGroupKey in sw {} with wrong " |
| 265 | + "input parameters", deviceId); |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | int labelStackSize = (param.getLabelStack() != null) |
| 270 | ? param.getLabelStack().size() : 0; |
| 271 | |
| 272 | if (labelStackSize > 1) { |
| 273 | for (PortNumber sp : ports) { |
| 274 | PolicyGroupIdentifier previousGroupkey = null; |
| 275 | for (int idx = 0; idx < param.getLabelStack().size(); idx++) { |
| 276 | int label = param.getLabelStack().get(idx).intValue(); |
| 277 | if (idx == (labelStackSize - 1)) { |
| 278 | // Innermost Group |
| 279 | GroupBucketIdentifier bucketId = |
| 280 | new GroupBucketIdentifier(label, |
| 281 | previousGroupkey); |
| 282 | bucketIds.add(bucketId); |
| 283 | } else if (idx == 0) { |
| 284 | // Outermost Group |
| 285 | GroupBucketIdentifier bucketId = |
| 286 | new GroupBucketIdentifier(label, sp); |
| 287 | PolicyGroupIdentifier key = new |
| 288 | PolicyGroupIdentifier(id, |
| 289 | Arrays.asList(param), |
| 290 | Arrays.asList(bucketId)); |
| 291 | previousGroupkey = key; |
| 292 | } else { |
| 293 | // Intermediate Groups |
| 294 | GroupBucketIdentifier bucketId = |
| 295 | new GroupBucketIdentifier(label, |
| 296 | previousGroupkey); |
| 297 | PolicyGroupIdentifier key = new |
| 298 | PolicyGroupIdentifier(id, |
| 299 | Arrays.asList(param), |
| 300 | Arrays.asList(bucketId)); |
| 301 | previousGroupkey = key; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } else { |
| 306 | int label = -1; |
| 307 | if (labelStackSize == 1) { |
| 308 | label = param.getLabelStack().get(0).intValue(); |
| 309 | } |
| 310 | for (PortNumber sp : ports) { |
| 311 | GroupBucketIdentifier bucketId = |
| 312 | new GroupBucketIdentifier(label, sp); |
| 313 | bucketIds.add(bucketId); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | PolicyGroupIdentifier innermostGroupkey = null; |
| 318 | if (!bucketIds.isEmpty()) { |
| 319 | innermostGroupkey = new |
| 320 | PolicyGroupIdentifier(id, |
| 321 | params, |
| 322 | bucketIds); |
| 323 | } |
| 324 | return innermostGroupkey; |
| 325 | } |
| 326 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 327 | public void removeGroupChain(PolicyGroupIdentifier key) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 328 | if (!(key instanceof PolicyGroupIdentifier)) { |
| 329 | throw new IllegalArgumentException(); |
| 330 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 331 | List<PolicyGroupIdentifier> groupsToBeDeleted = |
| 332 | new ArrayList<PolicyGroupIdentifier>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 333 | groupsToBeDeleted.add(key); |
| 334 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 335 | Iterator<PolicyGroupIdentifier> it = |
| 336 | groupsToBeDeleted.iterator(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 337 | |
| 338 | while (it.hasNext()) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 339 | PolicyGroupIdentifier innerMostGroupKey = it.next(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 340 | for (GroupBucketIdentifier bucketId: |
| 341 | innerMostGroupKey.bucketIds()) { |
| 342 | if (bucketId.type() != BucketOutputType.GROUP) { |
| 343 | groupsToBeDeleted.add(bucketId.outGroup()); |
| 344 | } |
| 345 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 346 | groupService.removeGroup(deviceId, |
| 347 | getGroupKey(innerMostGroupKey), |
| 348 | appId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 349 | it.remove(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | } |