This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit fbd9cb88e7b25c42fdc7cd88dfa7b54964dd255a Author: Alex Heneveld <[email protected]> AuthorDate: Tue Aug 16 12:15:26 2022 +0100 move AppGroupTraverser to core project for more access --- .../camp/brooklyn/spi/dsl/AppGroupTraverser.java | 109 +-------------------- .../brooklyn/spi/dsl/methods/DslComponent.java | 2 +- .../brooklyn/spi/dsl/AppGroupTraverserTest.java | 1 + .../core/mgmt/internal}/AppGroupTraverser.java | 4 +- 4 files changed, 7 insertions(+), 109 deletions(-) diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java index 6f1bdebf53..545a95d481 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java @@ -18,122 +18,19 @@ */ package org.apache.brooklyn.camp.brooklyn.spi.dsl; -import org.apache.brooklyn.api.entity.Application; import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.util.collections.MutableSet; import java.util.List; -import java.util.Set; import java.util.function.Predicate; -import java.util.stream.Collectors; -// progressively look up and down to application (topology) boundaries - -// ie first look subject to application boundaries, ie up to ancestor which is an application, and through descendants which are applications -// then look past all of those to next app boundary - -// TODO would be nice to have tests which look at parent, children, grandchildren, older and younger nephews - -// TODO ideally would be enhanced to look at the "depth in ancestor" tag, to work based on entity definition boundaries rather than app boundaries - -/** - * - * Progressively expands groups based on "application" boundaries, returning each distal group. - * Useful e.g. to find nodes matching an ID included in a particular application definition and not children. - <code> - - APP1 - - ENT1 - - APP2 - - ENT2 - - ENT3 - - APP3 - - ENT4 - - ENT5 - - APP4 - - ENT6 - - ENT7 - - APP5 - - ENT8 - </code> - - if this is initialized with ENT4, it will start with that in {@link #visitedThisTime} and {@link #visited}. - one invocation of {@link #next()} will return an instance where {@link #visitedThisTime} is {APP3,ENT5,APP4,ENT7,APP5}, and {@link #visited} contains that and ENT4; - it will not go below APP4 or APP5 because those are {@link Application} boundaries, nor will it go above APP3, on that pass. - an invocation of {@link #next()} on that instance will then return {@link #visitedThisTime} containing {APP1,ENT1,APP2,ENT6,ENT8}, - i.e. the items above APP3 (but not above the next highest ancestor implementing {@link Application}, - and its {@link #visited} will (as it always does) contain those items plus the items previously visited. - an invocation of {@link #next()} on that instance will then return {@link #visitedThisTime} empty. - <p> - see {@link #findFirstGroupOfMatches(Entity, Predicate)}. -*/ +@Deprecated +/** @deprecated since 1.1 use {@link org.apache.brooklyn.core.mgmt.internal.AppGroupTraverser */ public class AppGroupTraverser { - int depth = -1; - Set<Entity> visited = MutableSet.of(); - Set<Entity> visitedThisTime = MutableSet.of(); - // _parent_ of the last node we have visited; - // after first iteration this should have at least (exactly?) one visited child which is a topology template (not a node) - Entity ancestorBound = null; - // children whom we have not yet visited, because they are part of a new topology template - Set<Entity> descendantBounds = MutableSet.of(); - - protected AppGroupTraverser() { - } - - AppGroupTraverser(Entity source) { - this.visitedThisTime.add(source); - this.visited.add(source); - this.ancestorBound = source.getParent(); - this.descendantBounds.addAll(source.getChildren()); - } - - AppGroupTraverser next() { - AppGroupTraverser result = new AppGroupTraverser(); - result.visited.addAll(visited); - result.depth = depth + 1; - descendantBounds.forEach(c -> result.visitDescendants(c, true)); - if (ancestorBound != null) result.visitAncestorsAndTheirDescendants(ancestorBound); - return result; - } - - protected void visitAncestorsAndTheirDescendants(Entity ancestor) { - // go to the top of the containing topology template / application boundary - Entity appAncestor = ancestor; - while (!(appAncestor instanceof Application) && appAncestor.getParent() != null) - appAncestor = appAncestor.getParent(); - visitDescendants(appAncestor, true); - ancestorBound = appAncestor.getParent() != null ? appAncestor.getParent() : null; - } - - protected void visitDescendants(Entity node, boolean isFirst) { - if (!isFirst && !visited.add(node)) { - // already visited - return; - } - // normal entity - visitedThisTime.add(node); - - if (!isFirst && node instanceof Application) { - descendantBounds.add(node); - } else { - node.getChildren().forEach(c -> this.visitDescendants(c, false)); - } - } - - boolean hasNext() { - return ancestorBound != null || !descendantBounds.isEmpty(); - } - - AppGroupTraverser expandUntilMatchesFound(Predicate<Entity> test) { - if (visitedThisTime.stream().anyMatch(test) || visitedThisTime.isEmpty()) return this; - return next().expandUntilMatchesFound(test); - } - /** Progressively expands across {@link org.apache.brooklyn.api.entity.Application} boundaries until one or more matching entities are found. */ public static List<Entity> findFirstGroupOfMatches(Entity source, Predicate<Entity> test) { - AppGroupTraverser traversed = new AppGroupTraverser(source).expandUntilMatchesFound(test); - return traversed.visitedThisTime.stream().filter(test).collect(Collectors.toList()); + return org.apache.brooklyn.core.mgmt.internal.AppGroupTraverser.findFirstGroupOfMatches(source, test); } } diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java index f9e3b5b460..10e1802c13 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java +++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslComponent.java @@ -46,7 +46,7 @@ import org.apache.brooklyn.api.objs.BrooklynObject; import org.apache.brooklyn.api.sensor.AttributeSensor; import org.apache.brooklyn.api.sensor.Sensor; import org.apache.brooklyn.camp.brooklyn.BrooklynCampConstants; -import org.apache.brooklyn.camp.brooklyn.spi.dsl.AppGroupTraverser; +import org.apache.brooklyn.core.mgmt.internal.AppGroupTraverser; import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier; import org.apache.brooklyn.camp.brooklyn.spi.dsl.DslAccessible; import org.apache.brooklyn.camp.brooklyn.spi.dsl.DslFunctionSource; diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverserTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverserTest.java index b1d01f47d9..f2f1c56798 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverserTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverserTest.java @@ -26,6 +26,7 @@ import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest; import org.apache.brooklyn.camp.brooklyn.BrooklynCampConstants; import org.apache.brooklyn.core.entity.EntityPredicates; +import org.apache.brooklyn.core.mgmt.internal.AppGroupTraverser; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AppGroupTraverser.java similarity index 96% copy from camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java copy to core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AppGroupTraverser.java index 6f1bdebf53..77c96bfcd9 100644 --- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/AppGroupTraverser.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/internal/AppGroupTraverser.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.brooklyn.camp.brooklyn.spi.dsl; +package org.apache.brooklyn.core.mgmt.internal; import org.apache.brooklyn.api.entity.Application; import org.apache.brooklyn.api.entity.Entity; @@ -130,7 +130,7 @@ public class AppGroupTraverser { return next().expandUntilMatchesFound(test); } - /** Progressively expands across {@link org.apache.brooklyn.api.entity.Application} boundaries until one or more matching entities are found. */ + /** Progressively expands across {@link Application} boundaries until one or more matching entities are found. */ public static List<Entity> findFirstGroupOfMatches(Entity source, Predicate<Entity> test) { AppGroupTraverser traversed = new AppGroupTraverser(source).expandUntilMatchesFound(test); return traversed.visitedThisTime.stream().filter(test).collect(Collectors.toList());
