blob: a2a1b0936d7460c08f55994d57fe818b9769d809 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
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 Vavilapalli4db76e32015-04-07 15:12:32 -070016package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080017
Sho SHIMIZU24deb902015-05-11 18:40:48 -070018import static com.google.common.base.Preconditions.checkArgument;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080019import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070022import java.util.Collections;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080023import java.util.HashMap;
24import java.util.Iterator;
25import java.util.List;
26
sangho32a59322015-02-17 12:07:41 -080027import org.onlab.packet.MplsLabel;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080028import org.onosproject.core.ApplicationId;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070029import org.onosproject.segmentrouting.grouphandler.GroupBucketIdentifier.BucketOutputType;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030import org.onosproject.net.DeviceId;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070034import org.onosproject.net.flowobjective.FlowObjectiveService;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080035import org.onosproject.net.group.GroupBucket;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080036import org.onosproject.net.link.LinkService;
37import 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 */
43public class PolicyGroupHandler extends DefaultGroupHandler {
44
45 private final Logger log = getLogger(getClass());
Sho SHIMIZU8b4b9f02015-05-11 18:37:27 -070046 private HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier> dependentGroups = new HashMap<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080047
48 /**
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070049 * Policy group handler constructor.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080050 *
51 * @param deviceId device identifier
52 * @param appId application identifier
53 * @param config interface to retrieve the device properties
54 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070055 * @param flowObjService flow objective service object
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080056 */
57 public PolicyGroupHandler(DeviceId deviceId,
58 ApplicationId appId,
59 DeviceProperties config,
60 LinkService linkService,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070061 FlowObjectiveService flowObjService) {
62 super(deviceId, appId, config, linkService, flowObjService);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080063 }
64
65 public PolicyGroupIdentifier createPolicyGroupChain(String id,
66 List<PolicyGroupParams> params) {
Sho SHIMIZU8b4b9f02015-05-11 18:37:27 -070067 List<GroupBucketIdentifier> bucketIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080068 for (PolicyGroupParams param: params) {
69 List<PortNumber> ports = param.getPorts();
70 if (ports == null) {
71 log.warn("createPolicyGroupChain in sw {} with wrong "
72 + "input parameters", deviceId);
73 return null;
74 }
75
76 int labelStackSize = (param.getLabelStack() != null) ?
77 param.getLabelStack().size() : 0;
78
79 if (labelStackSize > 1) {
80 for (PortNumber sp : ports) {
81 PolicyGroupIdentifier previousGroupkey = null;
82 DeviceId neighbor = portDeviceMap.get(sp);
83 for (int idx = 0; idx < param.getLabelStack().size(); idx++) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -070084 int label = param.getLabelStack().get(idx);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080085 if (idx == (labelStackSize - 1)) {
86 // Innermost Group
87 GroupBucketIdentifier bucketId =
88 new GroupBucketIdentifier(label,
89 previousGroupkey);
90 bucketIds.add(bucketId);
91 } else if (idx == 0) {
92 // Outermost Group
Sho SHIMIZU8b4b9f02015-05-11 18:37:27 -070093 List<GroupBucket> outBuckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080094 GroupBucketIdentifier bucketId =
95 new GroupBucketIdentifier(label, sp);
96 PolicyGroupIdentifier key = new
97 PolicyGroupIdentifier(id,
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070098 Collections.singletonList(param),
99 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800100 TrafficTreatment.Builder tBuilder =
101 DefaultTrafficTreatment.builder();
102 tBuilder.setOutput(sp)
103 .setEthDst(deviceConfig.
104 getDeviceMac(neighbor))
105 .setEthSrc(nodeMacAddr)
106 .pushMpls()
sangho32a59322015-02-17 12:07:41 -0800107 .setMpls(MplsLabel.mplsLabel(label));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700108 /*outBuckets.add(DefaultGroupBucket.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800109 createSelectGroupBucket(tBuilder.build()));
110 GroupDescription desc = new
111 DefaultGroupDescription(deviceId,
112 GroupDescription.Type.INDIRECT,
113 new GroupBuckets(outBuckets));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700114 //TODO: BoS*/
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800115 previousGroupkey = key;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700116 //groupService.addGroup(desc);
117 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800118 } else {
119 // Intermediate Groups
120 GroupBucketIdentifier bucketId =
121 new GroupBucketIdentifier(label,
122 previousGroupkey);
123 PolicyGroupIdentifier key = new
124 PolicyGroupIdentifier(id,
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700125 Collections.singletonList(param),
126 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800127 // Add to group dependency list
128 dependentGroups.put(previousGroupkey, key);
129 previousGroupkey = key;
130 }
131 }
132 }
133 } else {
134 int label = -1;
135 if (labelStackSize == 1) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -0700136 label = param.getLabelStack().get(0);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800137 }
138 for (PortNumber sp : ports) {
139 GroupBucketIdentifier bucketId =
140 new GroupBucketIdentifier(label, sp);
141 bucketIds.add(bucketId);
142 }
143 }
144 }
145 PolicyGroupIdentifier innermostGroupkey = null;
146 if (!bucketIds.isEmpty()) {
147 innermostGroupkey = new
148 PolicyGroupIdentifier(id,
149 params,
150 bucketIds);
151 // Add to group dependency list
152 boolean fullyResolved = true;
153 for (GroupBucketIdentifier bucketId:bucketIds) {
154 if (bucketId.type() == BucketOutputType.GROUP) {
155 dependentGroups.put(bucketId.outGroup(),
156 innermostGroupkey);
157 fullyResolved = false;
158 }
159 }
160
161 if (fullyResolved) {
Sho SHIMIZU8b4b9f02015-05-11 18:37:27 -0700162 List<GroupBucket> outBuckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800163 for (GroupBucketIdentifier bucketId:bucketIds) {
164 DeviceId neighbor = portDeviceMap.
165 get(bucketId.outPort());
166 TrafficTreatment.Builder tBuilder =
167 DefaultTrafficTreatment.builder();
168 tBuilder.setOutput(bucketId.outPort())
169 .setEthDst(deviceConfig.
170 getDeviceMac(neighbor))
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700171 .setEthSrc(nodeMacAddr);
172 if (bucketId.label() != NeighborSet.NO_EDGE_LABEL) {
173 tBuilder.pushMpls()
sangho32a59322015-02-17 12:07:41 -0800174 .setMpls(MplsLabel.mplsLabel(bucketId.label()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700175 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800176 //TODO: BoS
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700177 /*outBuckets.add(DefaultGroupBucket.
178 createSelectGroupBucket(tBuilder.build()));*/
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800179 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700180 /*GroupDescription desc = new
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800181 DefaultGroupDescription(deviceId,
182 GroupDescription.Type.SELECT,
183 new GroupBuckets(outBuckets));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700184 groupService.addGroup(desc);*/
185 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800186 }
187 }
188 return innermostGroupkey;
189 }
190
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700191 //TODO: Use nextObjective APIs to handle the group chains
192 /*@Override
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800193 protected void handleGroupEvent(GroupEvent event) {
194 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
195 if (dependentGroups.get(event.subject().appCookie()) != null) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700196 PolicyGroupIdentifier dependentGroupKey = dependentGroups.get(event.subject().appCookie());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800197 dependentGroups.remove(event.subject().appCookie());
198 boolean fullyResolved = true;
199 for (GroupBucketIdentifier bucketId:
200 dependentGroupKey.bucketIds()) {
201 if (bucketId.type() != BucketOutputType.GROUP) {
202 continue;
203 }
204 if (dependentGroups.containsKey(bucketId.outGroup())) {
205 fullyResolved = false;
206 break;
207 }
208 }
209
210 if (fullyResolved) {
211 List<GroupBucket> outBuckets = new ArrayList<GroupBucket>();
212 for (GroupBucketIdentifier bucketId:
213 dependentGroupKey.bucketIds()) {
214 TrafficTreatment.Builder tBuilder =
215 DefaultTrafficTreatment.builder();
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700216 if (bucketId.label() != NeighborSet.NO_EDGE_LABEL) {
217 tBuilder.pushMpls()
218 .setMpls(MplsLabel.
219 mplsLabel(bucketId.label()));
220 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800221 //TODO: BoS
222 if (bucketId.type() == BucketOutputType.PORT) {
223 DeviceId neighbor = portDeviceMap.
224 get(bucketId.outPort());
225 tBuilder.setOutput(bucketId.outPort())
226 .setEthDst(deviceConfig.
227 getDeviceMac(neighbor))
228 .setEthSrc(nodeMacAddr);
229 } else {
230 if (groupService.
231 getGroup(deviceId,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700232 getGroupKey(bucketId.
233 outGroup())) == null) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800234 throw new IllegalStateException();
235 }
236 GroupId indirectGroupId = groupService.
237 getGroup(deviceId,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700238 getGroupKey(bucketId.
239 outGroup())).id();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800240 tBuilder.group(indirectGroupId);
241 }
242 outBuckets.add(DefaultGroupBucket.
243 createSelectGroupBucket(tBuilder.build()));
244 }
245 GroupDescription desc = new
246 DefaultGroupDescription(deviceId,
247 GroupDescription.Type.SELECT,
248 new GroupBuckets(outBuckets));
249 groupService.addGroup(desc);
250 }
251 }
252 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700253 }*/
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800254
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700255 public PolicyGroupIdentifier generatePolicyGroupKey(String id,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800256 List<PolicyGroupParams> params) {
Sho SHIMIZU8b4b9f02015-05-11 18:37:27 -0700257 List<GroupBucketIdentifier> bucketIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800258 for (PolicyGroupParams param: params) {
259 List<PortNumber> ports = param.getPorts();
260 if (ports == null) {
261 log.warn("generateGroupKey in sw {} with wrong "
262 + "input parameters", deviceId);
263 return null;
264 }
265
266 int labelStackSize = (param.getLabelStack() != null)
267 ? param.getLabelStack().size() : 0;
268
269 if (labelStackSize > 1) {
270 for (PortNumber sp : ports) {
271 PolicyGroupIdentifier previousGroupkey = null;
272 for (int idx = 0; idx < param.getLabelStack().size(); idx++) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -0700273 int label = param.getLabelStack().get(idx);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800274 if (idx == (labelStackSize - 1)) {
275 // Innermost Group
276 GroupBucketIdentifier bucketId =
277 new GroupBucketIdentifier(label,
278 previousGroupkey);
279 bucketIds.add(bucketId);
280 } else if (idx == 0) {
281 // Outermost Group
282 GroupBucketIdentifier bucketId =
283 new GroupBucketIdentifier(label, sp);
284 PolicyGroupIdentifier key = new
285 PolicyGroupIdentifier(id,
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700286 Collections.singletonList(param),
287 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800288 previousGroupkey = key;
289 } else {
290 // Intermediate Groups
291 GroupBucketIdentifier bucketId =
292 new GroupBucketIdentifier(label,
293 previousGroupkey);
294 PolicyGroupIdentifier key = new
295 PolicyGroupIdentifier(id,
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700296 Collections.singletonList(param),
297 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800298 previousGroupkey = key;
299 }
300 }
301 }
302 } else {
303 int label = -1;
304 if (labelStackSize == 1) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -0700305 label = param.getLabelStack().get(0);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800306 }
307 for (PortNumber sp : ports) {
308 GroupBucketIdentifier bucketId =
309 new GroupBucketIdentifier(label, sp);
310 bucketIds.add(bucketId);
311 }
312 }
313 }
314 PolicyGroupIdentifier innermostGroupkey = null;
315 if (!bucketIds.isEmpty()) {
316 innermostGroupkey = new
317 PolicyGroupIdentifier(id,
318 params,
319 bucketIds);
320 }
321 return innermostGroupkey;
322 }
323
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700324 public void removeGroupChain(PolicyGroupIdentifier key) {
Sho SHIMIZU24deb902015-05-11 18:40:48 -0700325 checkArgument(key != null);
Sho SHIMIZU8b4b9f02015-05-11 18:37:27 -0700326 List<PolicyGroupIdentifier> groupsToBeDeleted = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800327 groupsToBeDeleted.add(key);
328
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700329 Iterator<PolicyGroupIdentifier> it =
330 groupsToBeDeleted.iterator();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800331
332 while (it.hasNext()) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700333 PolicyGroupIdentifier innerMostGroupKey = it.next();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800334 for (GroupBucketIdentifier bucketId:
335 innerMostGroupKey.bucketIds()) {
336 if (bucketId.type() != BucketOutputType.GROUP) {
337 groupsToBeDeleted.add(bucketId.outGroup());
338 }
339 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700340 /*groupService.removeGroup(deviceId,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700341 getGroupKey(innerMostGroupKey),
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700342 appId);*/
343 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800344 it.remove();
345 }
346 }
347
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700348}