blob: d141ed1a526192fc49184ea92b0f394868a42c14 [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
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.HashMap;
23import java.util.Iterator;
24import java.util.List;
25
sangho32a59322015-02-17 12:07:41 -080026import org.onlab.packet.MplsLabel;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080027import org.onosproject.core.ApplicationId;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070028import org.onosproject.segmentrouting.grouphandler.GroupBucketIdentifier.BucketOutputType;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080029import org.onosproject.net.DeviceId;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070033import org.onosproject.net.flowobjective.FlowObjectiveService;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080034import org.onosproject.net.group.GroupBucket;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080035import org.onosproject.net.link.LinkService;
36import org.slf4j.Logger;
37
38/**
39 * A module to create group chains based on the specified device
40 * ports and label stack to be applied on each port.
41 */
42public class PolicyGroupHandler extends DefaultGroupHandler {
43
44 private final Logger log = getLogger(getClass());
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070045 private HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier> dependentGroups =
46 new HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier>();
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) {
67 List<GroupBucketIdentifier> bucketIds = new ArrayList<GroupBucketIdentifier>();
68 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++) {
84 int label = param.getLabelStack().get(idx).intValue();
85 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
93 List<GroupBucket> outBuckets = new ArrayList<GroupBucket>();
94 GroupBucketIdentifier bucketId =
95 new GroupBucketIdentifier(label, sp);
96 PolicyGroupIdentifier key = new
97 PolicyGroupIdentifier(id,
98 Arrays.asList(param),
99 Arrays.asList(bucketId));
100 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,
125 Arrays.asList(param),
126 Arrays.asList(bucketId));
127 // 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) {
136 label = param.getLabelStack().get(0).intValue();
137 }
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) {
162 List<GroupBucket> outBuckets = new ArrayList<GroupBucket>();
163 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) {
257 List<GroupBucketIdentifier> bucketIds = new ArrayList<GroupBucketIdentifier>();
258 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++) {
273 int label = param.getLabelStack().get(idx).intValue();
274 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,
286 Arrays.asList(param),
287 Arrays.asList(bucketId));
288 previousGroupkey = key;
289 } else {
290 // Intermediate Groups
291 GroupBucketIdentifier bucketId =
292 new GroupBucketIdentifier(label,
293 previousGroupkey);
294 PolicyGroupIdentifier key = new
295 PolicyGroupIdentifier(id,
296 Arrays.asList(param),
297 Arrays.asList(bucketId));
298 previousGroupkey = key;
299 }
300 }
301 }
302 } else {
303 int label = -1;
304 if (labelStackSize == 1) {
305 label = param.getLabelStack().get(0).intValue();
306 }
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) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800325 if (!(key instanceof PolicyGroupIdentifier)) {
326 throw new IllegalArgumentException();
327 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700328 List<PolicyGroupIdentifier> groupsToBeDeleted =
329 new ArrayList<PolicyGroupIdentifier>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800330 groupsToBeDeleted.add(key);
331
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700332 Iterator<PolicyGroupIdentifier> it =
333 groupsToBeDeleted.iterator();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800334
335 while (it.hasNext()) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700336 PolicyGroupIdentifier innerMostGroupKey = it.next();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800337 for (GroupBucketIdentifier bucketId:
338 innerMostGroupKey.bucketIds()) {
339 if (bucketId.type() != BucketOutputType.GROUP) {
340 groupsToBeDeleted.add(bucketId.outGroup());
341 }
342 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700343 /*groupService.removeGroup(deviceId,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700344 getGroupKey(innerMostGroupKey),
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700345 appId);*/
346 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800347 it.remove();
348 }
349 }
350
351}