blob: 21802ea5370b1cac68cc73efa810d66305709584 [file] [log] [blame]
Yi Tsengef19de12017-04-24 11:33:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengef19de12017-04-24 11:33:05 -07003 *
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
17package org.onosproject.driver.pipeline.ofdpa;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import com.google.common.collect.Sets;
22import org.onlab.packet.VlanId;
23import org.onosproject.core.GroupId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.behaviour.NextGroup;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.flow.instructions.Instruction;
31import org.onosproject.net.flow.instructions.Instructions;
Saurav Das7bcbe702017-06-13 15:35:54 -070032import org.onosproject.net.flow.instructions.L2ModificationInstruction;
33import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Yi Tsengef19de12017-04-24 11:33:05 -070034import org.onosproject.net.flowobjective.NextObjective;
35import org.onosproject.net.group.DefaultGroupBucket;
36import org.onosproject.net.group.DefaultGroupKey;
37import org.onosproject.net.group.Group;
38import org.onosproject.net.group.GroupBucket;
39import org.onosproject.net.group.GroupDescription;
40import org.onosproject.net.group.GroupKey;
41import org.onosproject.net.group.GroupService;
42import org.slf4j.Logger;
43
Saurav Dasceccf242017-08-03 18:30:35 -070044import java.util.ArrayList;
Yi Tsengef19de12017-04-24 11:33:05 -070045import java.util.Deque;
46import java.util.List;
47import java.util.Objects;
48import java.util.Set;
49import java.util.concurrent.atomic.AtomicInteger;
50import java.util.stream.Collectors;
51
52import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.isNotMplsBos;
53import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.OfdpaMplsGroupSubType.OFDPA_GROUP_TYPE_SHIFT;
54import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.OfdpaMplsGroupSubType.OFDPA_MPLS_SUBTYPE_SHIFT;
55import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
56import static org.slf4j.LoggerFactory.getLogger;
57
58public final class OfdpaGroupHandlerUtility {
59 /*
60 * OFDPA requires group-id's to have a certain form.
61 * L2 Interface Groups have <4bits-0><12bits-vlanId><16bits-portId>
62 * L3 Unicast Groups have <4bits-2><28bits-index>
63 * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index>
64 * L3 ECMP Groups have <4bits-7><28bits-index>
65 * L2 Flood Groups have <4bits-4><12bits-vlanId><16bits-index>
66 * L3 VPN Groups have <4bits-9><4bits-2><24bits-index>
67 */
Ray Milkey9c9cde42018-01-12 14:22:06 -080068 static final int L2_INTERFACE_TYPE = 0x00000000;
69 static final int L3_INTERFACE_TYPE = 0x50000000;
70 static final int L3_UNICAST_TYPE = 0x20000000;
71 static final int L3_MULTICAST_TYPE = 0x60000000;
72 static final int MPLS_INTERFACE_TYPE = 0x90000000;
73 static final int MPLS_L3VPN_SUBTYPE = 0x92000000;
74 static final int L3_ECMP_TYPE = 0x70000000;
75 static final int L2_FLOOD_TYPE = 0x40000000;
Yi Tsengef19de12017-04-24 11:33:05 -070076
Ray Milkey9c9cde42018-01-12 14:22:06 -080077 static final int TYPE_MASK = 0x0fffffff;
78 static final int SUBTYPE_MASK = 0x00ffffff;
79 static final int TYPE_VLAN_MASK = 0x0000ffff;
Yi Tsengef19de12017-04-24 11:33:05 -070080
Ray Milkey9c9cde42018-01-12 14:22:06 -080081 static final int THREE_BIT_MASK = 0x0fff;
82 static final int FOUR_BIT_MASK = 0xffff;
83 static final int PORT_LEN = 16;
Yi Tsengef19de12017-04-24 11:33:05 -070084
Ray Milkey9c9cde42018-01-12 14:22:06 -080085 static final int PORT_LOWER_BITS_MASK = 0x3f;
86 static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Yi Tsengef19de12017-04-24 11:33:05 -070087
Ray Milkey9c9cde42018-01-12 14:22:06 -080088 static final String HEX_PREFIX = "0x";
89 private static final Logger log = getLogger(OfdpaGroupHandlerUtility.class);
Yi Tsengef19de12017-04-24 11:33:05 -070090
91 private OfdpaGroupHandlerUtility() {
92 // Utility classes should not have a public or default constructor.
93 }
94
95 /**
96 * Returns the outport in a traffic treatment.
97 *
98 * @param tt the treatment
99 * @return the PortNumber for the outport or null
100 */
Ray Milkey9c9cde42018-01-12 14:22:06 -0800101 static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
Yi Tsengef19de12017-04-24 11:33:05 -0700102 for (Instruction ins : tt.allInstructions()) {
103 if (ins.type() == Instruction.Type.OUTPUT) {
104 return ((Instructions.OutputInstruction) ins).port();
105 }
106 }
107 return null;
108 }
109
110 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700111 * Returns the MPLS label-id in a traffic treatment.
112 *
113 * @param tt the traffic treatment
114 * @return an integer representing the MPLS label-id, or -1 if not found
115 */
Ray Milkey9c9cde42018-01-12 14:22:06 -0800116 static int readLabelFromTreatment(TrafficTreatment tt) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700117 for (Instruction ins : tt.allInstructions()) {
118 if (ins.type() == Instruction.Type.L2MODIFICATION) {
119 L2ModificationInstruction insl2 = (L2ModificationInstruction) ins;
120 if (insl2.subtype() == L2SubType.MPLS_LABEL) {
121 return ((L2ModificationInstruction.ModMplsLabelInstruction) insl2)
122 .label().id();
123 }
124 }
125 }
126 return -1;
127 }
128
129 /**
Yi Tsengef19de12017-04-24 11:33:05 -0700130 * Helper enum to handle the different MPLS group
131 * types.
132 */
133 public enum OfdpaMplsGroupSubType {
134 MPLS_INTF((short) 0),
135 L2_VPN((short) 1),
136 L3_VPN((short) 2),
137 MPLS_TUNNEL_LABEL_1((short) 3),
138 MPLS_TUNNEL_LABEL_2((short) 4),
139 MPLS_SWAP_LABEL((short) 5),
140 MPLS_ECMP((short) 8);
141
142 private short value;
143 public static final int OFDPA_GROUP_TYPE_SHIFT = 28;
144 public static final int OFDPA_MPLS_SUBTYPE_SHIFT = 24;
145
146 OfdpaMplsGroupSubType(short value) {
147 this.value = value;
148 }
149
150 /**
151 * Gets the value as an short.
152 *
153 * @return the value as an short
154 */
155 public short getValue() {
156 return this.value;
157 }
158
159 }
160
161 /**
162 * Creates MPLS Label group id given a sub type and
163 * the index.
164 *
165 * @param subType the MPLS Label group sub type
166 * @param index the index of the group
167 * @return the OFDPA group id
168 */
169 public static Integer makeMplsLabelGroupId(OfdpaMplsGroupSubType subType, int index) {
170 index = index & 0x00FFFFFF;
171 return index | (9 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
172 }
173
174 /**
175 * Creates MPLS Forwarding group id given a sub type and
176 * the index.
177 *
178 * @param subType the MPLS forwarding group sub type
179 * @param index the index of the group
180 * @return the OFDPA group id
181 */
182 public static Integer makeMplsForwardingGroupId(OfdpaMplsGroupSubType subType, int index) {
183 index = index & 0x00FFFFFF;
184 return index | (10 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
185 }
186
187 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700188 * Returns the set of existing output ports in the group represented by
189 * allActiveKeys.
Yi Tsengef19de12017-04-24 11:33:05 -0700190 *
191 * @param allActiveKeys list of group key chain
192 * @param groupService the group service to get group information
193 * @param deviceId the device id to get group
194 * @return a set of output port from the list of group key chain
195 */
196 public static Set<PortNumber> getExistingOutputPorts(List<Deque<GroupKey>> allActiveKeys,
197 GroupService groupService,
198 DeviceId deviceId) {
199 Set<PortNumber> existingPorts = Sets.newHashSet();
200
201 allActiveKeys.forEach(keyChain -> {
202 GroupKey ifaceGroupKey = keyChain.peekLast();
203 Group ifaceGroup = groupService.getGroup(deviceId, ifaceGroupKey);
204 if (ifaceGroup != null && !ifaceGroup.buckets().buckets().isEmpty()) {
205 ifaceGroup.buckets().buckets().forEach(bucket -> {
206 PortNumber portNumber = readOutPortFromTreatment(bucket.treatment());
207 if (portNumber != null) {
208 existingPorts.add(portNumber);
209 }
210 });
211 }
212 });
213 return existingPorts;
214 }
215
216 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700217 * Returns a list of all indices in the allActiveKeys list (that represents
218 * a group) if the list element (a bucket or group-chain) has treatments
219 * that match the given outport and label.
Saurav Das7bcbe702017-06-13 15:35:54 -0700220 *
221 * @param allActiveKeys the representation of the group
222 * @param groupService groups service for querying group information
223 * @param deviceId the device id for the device that contains the group
224 * @param portToMatch the port to match in the group buckets
225 * @param labelToMatch the MPLS label-id to match in the group buckets
Saurav Dasceccf242017-08-03 18:30:35 -0700226 * @return a list of indexes in the allActiveKeys list where the list element
227 * has treatments that match the given portToMatch and labelToMatch.
228 * Could be empty if no list elements were found to match the given
229 * port and label.
Saurav Das7bcbe702017-06-13 15:35:54 -0700230 */
Saurav Dasceccf242017-08-03 18:30:35 -0700231 public static List<Integer> existingPortAndLabel(
232 List<Deque<GroupKey>> allActiveKeys,
Saurav Das7bcbe702017-06-13 15:35:54 -0700233 GroupService groupService,
234 DeviceId deviceId,
235 PortNumber portToMatch,
236 int labelToMatch) {
Saurav Dasceccf242017-08-03 18:30:35 -0700237 List<Integer> indices = new ArrayList<>();
238 int index = 0;
Saurav Das7bcbe702017-06-13 15:35:54 -0700239 for (Deque<GroupKey> keyChain : allActiveKeys) {
240 GroupKey ifaceGroupKey = keyChain.peekLast();
241 Group ifaceGroup = groupService.getGroup(deviceId, ifaceGroupKey);
242 if (ifaceGroup != null && !ifaceGroup.buckets().buckets().isEmpty()) {
243 PortNumber portNumber = readOutPortFromTreatment(
244 ifaceGroup.buckets().buckets().iterator().next().treatment());
245 if (portNumber != null && portNumber.equals(portToMatch)) {
246 // check for label in the 2nd group of this chain
247 GroupKey secondKey = (GroupKey) keyChain.toArray()[1];
248 Group secondGroup = groupService.getGroup(deviceId, secondKey);
249 if (secondGroup != null &&
250 !secondGroup.buckets().buckets().isEmpty()) {
251 int label = readLabelFromTreatment(
252 secondGroup.buckets().buckets()
253 .iterator().next().treatment());
254 if (label == labelToMatch) {
Saurav Dasceccf242017-08-03 18:30:35 -0700255 indices.add(index);
Saurav Das7bcbe702017-06-13 15:35:54 -0700256 }
257 }
258 }
259 }
Saurav Dasceccf242017-08-03 18:30:35 -0700260 index++;
Saurav Das7bcbe702017-06-13 15:35:54 -0700261 }
262
Saurav Dasceccf242017-08-03 18:30:35 -0700263 return indices;
Saurav Das7bcbe702017-06-13 15:35:54 -0700264 }
265
Pier Luigiec6ac422018-01-29 10:30:59 +0100266
267 /**
268 * Get indices to remove comparing next group with next objective.
269 *
270 * @param allActiveKeys the representation of the group
271 * @param nextObjective the next objective to verify
272 * @param groupService groups service for querying group information
273 * @param deviceId the device id for the device that contains the group
274 * @return a list of indexes in the allActiveKeys to remove.
275 */
276 public static List<Integer> indicesToRemoveFromNextGroup(List<Deque<GroupKey>> allActiveKeys,
277 NextObjective nextObjective,
278 GroupService groupService,
279 DeviceId deviceId) {
280 List<Integer> indicesToRemove = Lists.newArrayList();
281 int index = 0;
282 // Iterate over the chain in the next data
283 for (Deque<GroupKey> keyChain : allActiveKeys) {
284 // Valid chain should have at least two elements
285 if (keyChain.size() >= 2) {
286 // Get last group (l2if) and retrieve port number
287 GroupKey ifaceGroupKey = keyChain.peekLast();
288 Group ifaceGroup = groupService.getGroup(deviceId, ifaceGroupKey);
289 if (ifaceGroup != null && !ifaceGroup.buckets().buckets().isEmpty()) {
290 PortNumber portNumber = readOutPortFromTreatment(
291 ifaceGroup.buckets().buckets().iterator().next().treatment());
292 // If there is not a port number continue
293 if (portNumber != null) {
294 // check for label in the 2nd group of this chain
295 GroupKey secondKey = (GroupKey) keyChain.toArray()[1];
296 Group secondGroup = groupService.getGroup(deviceId, secondKey);
297 // If there is not a second group or there are no buckets continue
298 if (secondGroup != null && !secondGroup.buckets().buckets().isEmpty()) {
299 // Get label or -1
300 int label = readLabelFromTreatment(
301 secondGroup.buckets().buckets()
302 .iterator().next().treatment());
303 // Iterate over the next treatments looking for the port and the label
304 boolean matches = false;
305 for (TrafficTreatment t : nextObjective.next()) {
306 PortNumber tPort = readOutPortFromTreatment(t);
307 int tLabel = readLabelFromTreatment(t);
308 if (tPort != null && tPort.equals(portNumber) && tLabel == label) {
309 // We found it, exit
310 matches = true;
311 break;
312 }
313 }
314 // Not found, we have to remove it
315 if (!matches) {
316 indicesToRemove.add(index);
317 }
318 }
319 }
320 }
321 }
322 index++;
323 }
324 return indicesToRemove;
325 }
326
Saurav Das7bcbe702017-06-13 15:35:54 -0700327 /**
Yi Tsengef19de12017-04-24 11:33:05 -0700328 * The purpose of this function is to verify if the hashed next
329 * objective is supported by the current pipeline.
330 *
331 * @param nextObjective the hashed objective to verify
332 * @return true if the hashed objective is supported. Otherwise false.
333 */
334 public static boolean verifyHashedNextObjective(NextObjective nextObjective) {
335 // if it is not hashed, there is something wrong;
336 if (nextObjective.type() != HASHED) {
337 return false;
338 }
339 // The case non supported is the MPLS-ECMP. For now, we try
340 // to create a MPLS-ECMP for the transport of a VPWS. The
341 // necessary info are contained in the meta selector. In particular
342 // we are looking for the case of BoS==False;
343 TrafficSelector metaSelector = nextObjective.meta();
344 if (metaSelector != null && isNotMplsBos(metaSelector)) {
345 return false;
346 }
347
348 return true;
349 }
350
351 /**
352 * Generates a list of group buckets from given list of group information
353 * and group bucket type.
354 *
355 * @param groupInfos a list of group information
356 * @param bucketType group bucket type
357 * @return list of group bucket generate from group information
358 */
Ray Milkey9c9cde42018-01-12 14:22:06 -0800359 static List<GroupBucket> generateNextGroupBuckets(List<GroupInfo> groupInfos,
Yi Tsengef19de12017-04-24 11:33:05 -0700360 GroupDescription.Type bucketType) {
361 List<GroupBucket> newBuckets = Lists.newArrayList();
362
363 groupInfos.forEach(groupInfo -> {
364 GroupDescription groupDesc = groupInfo.nextGroupDesc();
365 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
366 treatmentBuilder.group(new GroupId(groupDesc.givenGroupId()));
367 GroupBucket newBucket = null;
368 switch (bucketType) {
369 case ALL:
370 newBucket =
371 DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
372 break;
373 case INDIRECT:
374 newBucket =
375 DefaultGroupBucket.createIndirectGroupBucket(treatmentBuilder.build());
376 break;
377 case SELECT:
378 newBucket =
379 DefaultGroupBucket.createSelectGroupBucket(treatmentBuilder.build());
380 break;
381 case FAILOVER:
382 // TODO: support failover bucket type
383 default:
384 log.warn("Unknown bucket type: {}", bucketType);
385 break;
386 }
387
388 if (newBucket != null) {
389 newBuckets.add(newBucket);
390 }
391
392 });
393
394 return ImmutableList.copyOf(newBuckets);
395 }
396
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900397 static List<GroupBucket> createL3MulticastBucket(List<GroupInfo> groupInfos) {
398 List<GroupBucket> l3McastBuckets = new ArrayList<>();
399 // For each inner group
400 groupInfos.forEach(groupInfo -> {
401 // Points to L3 interface group if there is one.
402 // Otherwise points to L2 interface group directly.
403 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
404 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
405 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
406 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
407 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
408 l3McastBuckets.add(abucket);
409 });
410 // Done return the new list of buckets
411 return l3McastBuckets;
412 }
413
414 static Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
415 DeviceId deviceId,
416 GroupService groupService,
417 int nextid) {
418 GroupKey topLevelGroupKey;
419 if (!allActiveKeys.isEmpty()) {
420 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
421 } else {
422 log.warn("Could not determine top level group while processing"
423 + "next:{} in dev:{}", nextid, deviceId);
424 return null;
425 }
426 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
427 if (topGroup == null) {
428 log.warn("Could not find top level group while processing "
429 + "next:{} in dev:{}", nextid, deviceId);
430 }
431 return topGroup;
432 }
433
Yi Tsengef19de12017-04-24 11:33:05 -0700434 /**
435 * Extracts VlanId from given group ID.
436 *
437 * @param groupId the group ID
438 * @return vlan id of the group
439 */
440 public static VlanId extractVlanIdFromGroupId(int groupId) {
441 // Extract the 9th to 20th bit from group id as vlan id.
442 short vlanId = (short) ((groupId & 0x0fff0000) >> 16);
443 return VlanId.vlanId(vlanId);
444 }
445
446 public static GroupKey l2FloodGroupKey(VlanId vlanId, DeviceId deviceId) {
447 int hash = Objects.hash(deviceId, vlanId);
448 hash = L2_FLOOD_TYPE | TYPE_MASK & hash;
449 return new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(hash));
450 }
451
452 public static int l2GroupId(VlanId vlanId, long portNum) {
453 return L2_INTERFACE_TYPE | (vlanId.toShort() << 16) | (int) portNum;
454 }
455
456 /**
457 * Returns a hash as the L2 Interface Group Key.
458 *
459 * Keep the lower 6-bit for port since port number usually smaller than 64.
460 * Hash other information into remaining 28 bits.
461 *
462 * @param deviceId Device ID
463 * @param vlanId VLAN ID
464 * @param portNumber Port number
465 * @return L2 interface group key
466 */
467 public static int l2InterfaceGroupKey(DeviceId deviceId, VlanId vlanId, long portNumber) {
468 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
469 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
470 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
471 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
472 }
473
474 /**
475 * Utility class for moving group information around.
476 *
477 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
478 * A is the top level group, and D is the inner-most group, typically L2 Interface.
479 * The innerMostGroupDesc is always D. At various stages of the creation
480 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
481 * inform the referencing group about which group it needs to point to,
482 * and wait for. In some cases the group chain may simply be A-B. In this case,
483 * both innerMostGroupDesc and nextGroupDesc will be B.
484 */
485 public static class GroupInfo {
486 /**
487 * Description of the inner-most group of the group chain.
488 * It is always an L2 interface group.
489 */
490 private GroupDescription innerMostGroupDesc;
491
492 /**
493 * Description of the next group in the group chain.
494 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
495 * It is possible that nextGroupDesc is the same as the innerMostGroup.
496 */
497 private GroupDescription nextGroupDesc;
498
499 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
500 this.innerMostGroupDesc = innerMostGroupDesc;
501 this.nextGroupDesc = nextGroupDesc;
502 }
503
504 /**
505 * Getter for innerMostGroupDesc.
506 *
507 * @return the inner most group description
508 */
509 public GroupDescription innerMostGroupDesc() {
510 return innerMostGroupDesc;
511 }
512
513 /**
514 * Getter for the next group description.
515 *
516 * @return the next group description
517 */
518 public GroupDescription nextGroupDesc() {
519 return nextGroupDesc;
520 }
521
522 /**
523 * Setter of nextGroupDesc.
524 *
525 * @param nextGroupDesc the given value to set
526 */
527 public void nextGroupDesc(GroupDescription nextGroupDesc) {
528 this.nextGroupDesc = nextGroupDesc;
529 }
530 }
531
532 /**
533 * Represents an entire group-chain that implements a Next-Objective from
534 * the application. The objective is represented as a list of deques, where
535 * each deque is a separate chain of groups.
536 * <p>
537 * For example, an ECMP group with 3 buckets, where each bucket points to
538 * a group chain of L3 Unicast and L2 interface groups will look like this:
539 * <ul>
540 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
541 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
542 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
543 * </ul>
544 * where the first element of each deque is the same, representing the
545 * top level ECMP group, while every other element represents a unique groupKey.
546 * <p>
547 * Also includes information about the next objective that
548 * resulted in these group-chains.
549 *
550 */
551 public static class OfdpaNextGroup implements NextGroup {
552 private final NextObjective nextObj;
553 private final List<Deque<GroupKey>> gkeys;
554
555 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
556 this.nextObj = nextObj;
557 this.gkeys = gkeys;
558 }
559
560 public NextObjective nextObjective() {
561 return nextObj;
562 }
563
Saurav Dasc88d4662017-05-15 15:34:25 -0700564 public List<Deque<GroupKey>> allKeys() {
565 return gkeys;
566 }
567
Yi Tsengef19de12017-04-24 11:33:05 -0700568 @Override
569 public byte[] data() {
570 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
571 }
572 }
573
574 /**
575 * Represents a group element that is part of a chain of groups.
576 * Stores enough information to create a Group Description to add the group
577 * to the switch by requesting the Group Service. Objects instantiating this
578 * class are meant to be temporary and live as long as it is needed to wait for
579 * referenced groups in the group chain to be created.
580 */
581 public static class GroupChainElem {
582 private GroupDescription groupDescription;
583 private AtomicInteger waitOnGroups;
584 private boolean addBucketToGroup;
585 private DeviceId deviceId;
586
587 public GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
588 boolean addBucketToGroup, DeviceId deviceId) {
589 this.groupDescription = groupDescription;
590 this.waitOnGroups = new AtomicInteger(waitOnGroups);
591 this.addBucketToGroup = addBucketToGroup;
592 this.deviceId = deviceId;
593 }
594
595 /**
596 * This method atomically decrements the counter for the number of
597 * groups this GroupChainElement is waiting on, for notifications from
598 * the Group Service. When this method returns a value of 0, this
599 * GroupChainElement is ready to be processed.
600 *
601 * @return integer indication of the number of notifications being waited on
602 */
603 int decrementAndGetGroupsWaitedOn() {
604 return waitOnGroups.decrementAndGet();
605 }
606
607 public GroupDescription groupDescription() {
608 return groupDescription;
609 }
610
611 public boolean addBucketToGroup() {
612 return addBucketToGroup;
613 }
614
615 @Override
616 public String toString() {
617 return (Integer.toHexString(groupDescription.givenGroupId()) +
618 " groupKey: " + groupDescription.appCookie() +
619 " waiting-on-groups: " + waitOnGroups.get() +
620 " addBucketToGroup: " + addBucketToGroup +
621 " device: " + deviceId);
622 }
623 }
624
625 public static class GroupChecker implements Runnable {
Ray Milkey9c9cde42018-01-12 14:22:06 -0800626 final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700627 private Ofdpa2GroupHandler groupHandler;
628
629 public GroupChecker(Ofdpa2GroupHandler groupHandler) {
630 this.groupHandler = groupHandler;
631 }
632
633 @Override
634 public void run() {
Pier Luigi07532ab2018-01-12 16:03:49 +0100635 // GroupChecker execution needs to be protected
636 // from unhandled exceptions
637 try {
638 if (groupHandler.pendingGroups().size() != 0) {
639 log.debug("pending groups being checked: {}", groupHandler.pendingGroups().asMap().keySet());
640 }
641 if (groupHandler.pendingAddNextObjectives().size() != 0) {
642 log.debug("pending add-next-obj being checked: {}",
643 groupHandler.pendingAddNextObjectives().asMap().keySet());
644 }
645 Set<GroupKey> keys = groupHandler.pendingGroups().asMap().keySet().stream()
646 .filter(key -> groupHandler.groupService.getGroup(groupHandler.deviceId, key) != null)
647 .collect(Collectors.toSet());
648 Set<GroupKey> otherkeys = groupHandler.pendingAddNextObjectives().asMap().keySet().stream()
649 .filter(otherkey -> groupHandler.groupService.getGroup(groupHandler.deviceId, otherkey) != null)
650 .collect(Collectors.toSet());
651 keys.addAll(otherkeys);
Yi Tsengef19de12017-04-24 11:33:05 -0700652
Pier Luigi07532ab2018-01-12 16:03:49 +0100653 keys.forEach(key -> groupHandler.processPendingAddGroupsOrNextObjs(key, false));
654 } catch (Exception exception) {
655 // Just log. It is safe for now.
656 log.warn("Uncaught exception is detected: {}", exception.getMessage());
657 log.debug("Uncaught exception is detected (full stack trace): ", exception);
658 }
Yi Tsengef19de12017-04-24 11:33:05 -0700659 }
660 }
661}