blob: 52242b89f1f40dcd8c8f5d53562aafff0ed5f2c4 [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());
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070046 private HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier> dependentGroups =
47 new HashMap<PolicyGroupIdentifier, PolicyGroupIdentifier>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080048
49 /**
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070050 * Policy group handler constructor.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080051 *
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 Vavilapallif5b234a2015-04-21 13:04:13 -070056 * @param flowObjService flow objective service object
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080057 */
58 public PolicyGroupHandler(DeviceId deviceId,
59 ApplicationId appId,
60 DeviceProperties config,
61 LinkService linkService,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070062 FlowObjectiveService flowObjService) {
63 super(deviceId, appId, config, linkService, flowObjService);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080064 }
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++) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -070085 int label = param.getLabelStack().get(idx);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080086 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 SHIMIZU98ffca82015-05-11 08:39:24 -070099 Collections.singletonList(param),
100 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800101 TrafficTreatment.Builder tBuilder =
102 DefaultTrafficTreatment.builder();
103 tBuilder.setOutput(sp)
104 .setEthDst(deviceConfig.
105 getDeviceMac(neighbor))
106 .setEthSrc(nodeMacAddr)
107 .pushMpls()
sangho32a59322015-02-17 12:07:41 -0800108 .setMpls(MplsLabel.mplsLabel(label));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700109 /*outBuckets.add(DefaultGroupBucket.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800110 createSelectGroupBucket(tBuilder.build()));
111 GroupDescription desc = new
112 DefaultGroupDescription(deviceId,
113 GroupDescription.Type.INDIRECT,
114 new GroupBuckets(outBuckets));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700115 //TODO: BoS*/
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800116 previousGroupkey = key;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700117 //groupService.addGroup(desc);
118 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800119 } else {
120 // Intermediate Groups
121 GroupBucketIdentifier bucketId =
122 new GroupBucketIdentifier(label,
123 previousGroupkey);
124 PolicyGroupIdentifier key = new
125 PolicyGroupIdentifier(id,
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700126 Collections.singletonList(param),
127 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800128 // 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) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -0700137 label = param.getLabelStack().get(0);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800138 }
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))
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700172 .setEthSrc(nodeMacAddr);
173 if (bucketId.label() != NeighborSet.NO_EDGE_LABEL) {
174 tBuilder.pushMpls()
sangho32a59322015-02-17 12:07:41 -0800175 .setMpls(MplsLabel.mplsLabel(bucketId.label()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700176 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800177 //TODO: BoS
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700178 /*outBuckets.add(DefaultGroupBucket.
179 createSelectGroupBucket(tBuilder.build()));*/
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800180 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700181 /*GroupDescription desc = new
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800182 DefaultGroupDescription(deviceId,
183 GroupDescription.Type.SELECT,
184 new GroupBuckets(outBuckets));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700185 groupService.addGroup(desc);*/
186 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800187 }
188 }
189 return innermostGroupkey;
190 }
191
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700192 //TODO: Use nextObjective APIs to handle the group chains
193 /*@Override
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800194 protected void handleGroupEvent(GroupEvent event) {
195 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
196 if (dependentGroups.get(event.subject().appCookie()) != null) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700197 PolicyGroupIdentifier dependentGroupKey = dependentGroups.get(event.subject().appCookie());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800198 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();
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700217 if (bucketId.label() != NeighborSet.NO_EDGE_LABEL) {
218 tBuilder.pushMpls()
219 .setMpls(MplsLabel.
220 mplsLabel(bucketId.label()));
221 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800222 //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,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700233 getGroupKey(bucketId.
234 outGroup())) == null) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800235 throw new IllegalStateException();
236 }
237 GroupId indirectGroupId = groupService.
238 getGroup(deviceId,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700239 getGroupKey(bucketId.
240 outGroup())).id();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800241 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 Vavilapallif5b234a2015-04-21 13:04:13 -0700254 }*/
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800255
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700256 public PolicyGroupIdentifier generatePolicyGroupKey(String id,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800257 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++) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -0700274 int label = param.getLabelStack().get(idx);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800275 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 SHIMIZU98ffca82015-05-11 08:39:24 -0700287 Collections.singletonList(param),
288 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800289 previousGroupkey = key;
290 } else {
291 // Intermediate Groups
292 GroupBucketIdentifier bucketId =
293 new GroupBucketIdentifier(label,
294 previousGroupkey);
295 PolicyGroupIdentifier key = new
296 PolicyGroupIdentifier(id,
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700297 Collections.singletonList(param),
298 Collections.singletonList(bucketId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800299 previousGroupkey = key;
300 }
301 }
302 }
303 } else {
304 int label = -1;
305 if (labelStackSize == 1) {
Sho SHIMIZUbaddcbe2015-05-11 18:42:18 -0700306 label = param.getLabelStack().get(0);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800307 }
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
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700325 public void removeGroupChain(PolicyGroupIdentifier key) {
Sho SHIMIZU24deb902015-05-11 18:40:48 -0700326 checkArgument(key != null);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700327 List<PolicyGroupIdentifier> groupsToBeDeleted =
328 new ArrayList<PolicyGroupIdentifier>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800329 groupsToBeDeleted.add(key);
330
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700331 Iterator<PolicyGroupIdentifier> it =
332 groupsToBeDeleted.iterator();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800333
334 while (it.hasNext()) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700335 PolicyGroupIdentifier innerMostGroupKey = it.next();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800336 for (GroupBucketIdentifier bucketId:
337 innerMostGroupKey.bucketIds()) {
338 if (bucketId.type() != BucketOutputType.GROUP) {
339 groupsToBeDeleted.add(bucketId.outGroup());
340 }
341 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700342 /*groupService.removeGroup(deviceId,
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700343 getGroupKey(innerMostGroupKey),
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700344 appId);*/
345 //TODO: Use nextObjective APIs here
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800346 it.remove();
347 }
348 }
349
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700350}