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