blob: e9c97bc61f1346eb19dfe98e8ac12fd69afa13e3 [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
397 /**
398 * Extracts VlanId from given group ID.
399 *
400 * @param groupId the group ID
401 * @return vlan id of the group
402 */
403 public static VlanId extractVlanIdFromGroupId(int groupId) {
404 // Extract the 9th to 20th bit from group id as vlan id.
405 short vlanId = (short) ((groupId & 0x0fff0000) >> 16);
406 return VlanId.vlanId(vlanId);
407 }
408
409 public static GroupKey l2FloodGroupKey(VlanId vlanId, DeviceId deviceId) {
410 int hash = Objects.hash(deviceId, vlanId);
411 hash = L2_FLOOD_TYPE | TYPE_MASK & hash;
412 return new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(hash));
413 }
414
415 public static int l2GroupId(VlanId vlanId, long portNum) {
416 return L2_INTERFACE_TYPE | (vlanId.toShort() << 16) | (int) portNum;
417 }
418
419 /**
420 * Returns a hash as the L2 Interface Group Key.
421 *
422 * Keep the lower 6-bit for port since port number usually smaller than 64.
423 * Hash other information into remaining 28 bits.
424 *
425 * @param deviceId Device ID
426 * @param vlanId VLAN ID
427 * @param portNumber Port number
428 * @return L2 interface group key
429 */
430 public static int l2InterfaceGroupKey(DeviceId deviceId, VlanId vlanId, long portNumber) {
431 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
432 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
433 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
434 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
435 }
436
437 /**
438 * Utility class for moving group information around.
439 *
440 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
441 * A is the top level group, and D is the inner-most group, typically L2 Interface.
442 * The innerMostGroupDesc is always D. At various stages of the creation
443 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
444 * inform the referencing group about which group it needs to point to,
445 * and wait for. In some cases the group chain may simply be A-B. In this case,
446 * both innerMostGroupDesc and nextGroupDesc will be B.
447 */
448 public static class GroupInfo {
449 /**
450 * Description of the inner-most group of the group chain.
451 * It is always an L2 interface group.
452 */
453 private GroupDescription innerMostGroupDesc;
454
455 /**
456 * Description of the next group in the group chain.
457 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
458 * It is possible that nextGroupDesc is the same as the innerMostGroup.
459 */
460 private GroupDescription nextGroupDesc;
461
462 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
463 this.innerMostGroupDesc = innerMostGroupDesc;
464 this.nextGroupDesc = nextGroupDesc;
465 }
466
467 /**
468 * Getter for innerMostGroupDesc.
469 *
470 * @return the inner most group description
471 */
472 public GroupDescription innerMostGroupDesc() {
473 return innerMostGroupDesc;
474 }
475
476 /**
477 * Getter for the next group description.
478 *
479 * @return the next group description
480 */
481 public GroupDescription nextGroupDesc() {
482 return nextGroupDesc;
483 }
484
485 /**
486 * Setter of nextGroupDesc.
487 *
488 * @param nextGroupDesc the given value to set
489 */
490 public void nextGroupDesc(GroupDescription nextGroupDesc) {
491 this.nextGroupDesc = nextGroupDesc;
492 }
493 }
494
495 /**
496 * Represents an entire group-chain that implements a Next-Objective from
497 * the application. The objective is represented as a list of deques, where
498 * each deque is a separate chain of groups.
499 * <p>
500 * For example, an ECMP group with 3 buckets, where each bucket points to
501 * a group chain of L3 Unicast and L2 interface groups will look like this:
502 * <ul>
503 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
504 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
505 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
506 * </ul>
507 * where the first element of each deque is the same, representing the
508 * top level ECMP group, while every other element represents a unique groupKey.
509 * <p>
510 * Also includes information about the next objective that
511 * resulted in these group-chains.
512 *
513 */
514 public static class OfdpaNextGroup implements NextGroup {
515 private final NextObjective nextObj;
516 private final List<Deque<GroupKey>> gkeys;
517
518 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
519 this.nextObj = nextObj;
520 this.gkeys = gkeys;
521 }
522
523 public NextObjective nextObjective() {
524 return nextObj;
525 }
526
Saurav Dasc88d4662017-05-15 15:34:25 -0700527 public List<Deque<GroupKey>> allKeys() {
528 return gkeys;
529 }
530
Yi Tsengef19de12017-04-24 11:33:05 -0700531 @Override
532 public byte[] data() {
533 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
534 }
535 }
536
537 /**
538 * Represents a group element that is part of a chain of groups.
539 * Stores enough information to create a Group Description to add the group
540 * to the switch by requesting the Group Service. Objects instantiating this
541 * class are meant to be temporary and live as long as it is needed to wait for
542 * referenced groups in the group chain to be created.
543 */
544 public static class GroupChainElem {
545 private GroupDescription groupDescription;
546 private AtomicInteger waitOnGroups;
547 private boolean addBucketToGroup;
548 private DeviceId deviceId;
549
550 public GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
551 boolean addBucketToGroup, DeviceId deviceId) {
552 this.groupDescription = groupDescription;
553 this.waitOnGroups = new AtomicInteger(waitOnGroups);
554 this.addBucketToGroup = addBucketToGroup;
555 this.deviceId = deviceId;
556 }
557
558 /**
559 * This method atomically decrements the counter for the number of
560 * groups this GroupChainElement is waiting on, for notifications from
561 * the Group Service. When this method returns a value of 0, this
562 * GroupChainElement is ready to be processed.
563 *
564 * @return integer indication of the number of notifications being waited on
565 */
566 int decrementAndGetGroupsWaitedOn() {
567 return waitOnGroups.decrementAndGet();
568 }
569
570 public GroupDescription groupDescription() {
571 return groupDescription;
572 }
573
574 public boolean addBucketToGroup() {
575 return addBucketToGroup;
576 }
577
578 @Override
579 public String toString() {
580 return (Integer.toHexString(groupDescription.givenGroupId()) +
581 " groupKey: " + groupDescription.appCookie() +
582 " waiting-on-groups: " + waitOnGroups.get() +
583 " addBucketToGroup: " + addBucketToGroup +
584 " device: " + deviceId);
585 }
586 }
587
588 public static class GroupChecker implements Runnable {
Ray Milkey9c9cde42018-01-12 14:22:06 -0800589 final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700590 private Ofdpa2GroupHandler groupHandler;
591
592 public GroupChecker(Ofdpa2GroupHandler groupHandler) {
593 this.groupHandler = groupHandler;
594 }
595
596 @Override
597 public void run() {
Pier Luigi07532ab2018-01-12 16:03:49 +0100598 // GroupChecker execution needs to be protected
599 // from unhandled exceptions
600 try {
601 if (groupHandler.pendingGroups().size() != 0) {
602 log.debug("pending groups being checked: {}", groupHandler.pendingGroups().asMap().keySet());
603 }
604 if (groupHandler.pendingAddNextObjectives().size() != 0) {
605 log.debug("pending add-next-obj being checked: {}",
606 groupHandler.pendingAddNextObjectives().asMap().keySet());
607 }
608 Set<GroupKey> keys = groupHandler.pendingGroups().asMap().keySet().stream()
609 .filter(key -> groupHandler.groupService.getGroup(groupHandler.deviceId, key) != null)
610 .collect(Collectors.toSet());
611 Set<GroupKey> otherkeys = groupHandler.pendingAddNextObjectives().asMap().keySet().stream()
612 .filter(otherkey -> groupHandler.groupService.getGroup(groupHandler.deviceId, otherkey) != null)
613 .collect(Collectors.toSet());
614 keys.addAll(otherkeys);
Yi Tsengef19de12017-04-24 11:33:05 -0700615
Pier Luigi07532ab2018-01-12 16:03:49 +0100616 keys.forEach(key -> groupHandler.processPendingAddGroupsOrNextObjs(key, false));
617 } catch (Exception exception) {
618 // Just log. It is safe for now.
619 log.warn("Uncaught exception is detected: {}", exception.getMessage());
620 log.debug("Uncaught exception is detected (full stack trace): ", exception);
621 }
Yi Tsengef19de12017-04-24 11:33:05 -0700622 }
623 }
624}