Java Code Examples for org.apache.brooklyn.api.entity.Entity#getChildren()

The following examples show how to use org.apache.brooklyn.api.entity.Entity#getChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SeaCloudsMonitoringInitializationPolicies.java    From SeaCloudsPlatform with Apache License 2.0 6 votes vote down vote up
private Optional<Entity> findChildEntityByPlanId(Entity app, String planId) {
    for (Entity child : app.getChildren()) {
        Optional<Entity> subChild = Optional.absent();
        if (isCampPlanIdOfEntity(child, planId)
                || isToscaIdPlanOfEntity(child, planId)) {
            subChild = Optional.of(child);
        }

        if ((child.getChildren() != null)
                && (!child.getChildren().isEmpty())
                && (!subChild.isPresent())) {
            subChild = findChildEntityByPlanId(child, planId);
        }
        if (subChild.isPresent()) {
            return subChild;
        }

    }
    return Optional.absent();
}
 
Example 2
Source File: LocalEntityManager.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private void recursively(Entity e, Predicate<EntityInternal> action) {
    Entity otherPreregistered = preRegisteredEntitiesById.get(e.getId());
    if (otherPreregistered!=null) {
        // if something has been pre-registered, prefer it
        // (e.g. if we recursing through children, we might have a proxy from previous iteration;
        // the most recent will have been pre-registered)
        e = otherPreregistered;
    }
        
    boolean success = action.apply( (EntityInternal)e );
    if (!success) {
        return; // Don't manage children if action false/unnecessary for parent
    }
    for (Entity child : e.getChildren()) {
        recursively(child, action);
    }
}
 
Example 3
Source File: EntityLocationUtils.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected void countLeafEntitiesByLocatedLocations(Entity target, Entity locatedParent, Map<Location, Integer> result) {
    if (isLocatedLocation(target))
        locatedParent = target;
    if (!target.getChildren().isEmpty()) {
        // non-leaf - inspect children
        for (Entity child: target.getChildren()) 
            countLeafEntitiesByLocatedLocations(child, locatedParent, result);
    } else {
        // leaf node - increment location count
        if (locatedParent!=null) {
            for (Location l: locatedParent.getLocations()) {
                Location ll = getMostGeneralLocatedLocation(l);
                if (ll!=null) {
                    Integer count = result.get(ll);
                    if (count==null) count = 1;
                    else count++;
                    result.put(ll, count);
                }
            }
        }
    }
}
 
Example 4
Source File: ReferencedOsgiYamlTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected void assertCatalogReference() throws Exception {
    String entityNameSimple = "YAML -> catalog -> catalog simple (osgi)";
    String entityNameMore = "YAML -> catalog -> catalog more (osgi)";
    Entity app = createAndStartApplication(
        "services:",
        "- name: " + entityNameSimple,
        "  type: yaml.nested.catalog.simple",
        "- name: " + entityNameMore,
        "  type: yaml.nested.catalog.more");
    
    Collection<Entity> children = app.getChildren();
    Assert.assertEquals(children.size(), 2);
    Entity childSimple = Iterables.get(children, 0);
    Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple);
    Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);

    Entity childMore = Iterables.get(children, 1);
    Assert.assertEquals(childMore.getDisplayName(), entityNameMore);
    Assert.assertEquals(childMore.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
}
 
Example 5
Source File: EntitiesYamlTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithInitConfig() throws Exception {
    Entity app = createAndStartApplication(loadYaml("test-entity-with-init-config.yaml"));
    waitForApplicationTasks(app);
    Assert.assertEquals(app.getDisplayName(), "test-entity-with-init-config");
    TestEntityWithInitConfig testWithConfigInit = null;
    TestEntity testEntity = null;
    Assert.assertEquals(app.getChildren().size(), 2);
    for (Entity entity : app.getChildren()) {
        if (entity instanceof TestEntity)
            testEntity = (TestEntity) entity;
        if (entity instanceof TestEntityWithInitConfig)
            testWithConfigInit = (TestEntityWithInitConfig) entity;
    }
    Assert.assertNotNull(testEntity, "Expected app to contain TestEntity child");
    Assert.assertNotNull(testWithConfigInit, "Expected app to contain TestEntityWithInitConfig child");
    Assert.assertEquals(testWithConfigInit.getEntityCachedOnInit(), testEntity);
    log.info("App started:");
    Dumper.dumpInfo(app);
}
 
Example 6
Source File: AssemblyBrooklynLookup.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public Assembly get(String id) {
    Entity entity = bmc.getEntityManager().getEntity(id);
    if (!(entity instanceof Application))
        throw new IllegalArgumentException("Element for "+id+" is not an Application ("+entity+")");
    Assembly.Builder<? extends Assembly> builder = Assembly.builder()
            .created(new Date(entity.getCreationTime()))
            .id(entity.getId())
            .name(entity.getDisplayName());
    
    builder.customAttribute("externalManagementUri", BrooklynUrlLookup.getUrl(bmc, entity));
    
    for (Entity child: entity.getChildren())
        // FIXME this walks the whole damn tree!
        builder.add( pcs.get(child.getId() ));
    return builder.build();
}
 
Example 7
Source File: EntitiesYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testGrandchildEntities() throws Exception {
    Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml", 
        "  brooklyn.config:",
        "    test.confName: first entity",
        "  brooklyn.children:",
        "  - serviceType: org.apache.brooklyn.core.test.entity.TestEntity",
        "    name: Child Entity",
        "    brooklyn.config:",
        "      test.confName: Name of the first Child",
        "    brooklyn.children:",
        "    - serviceType: org.apache.brooklyn.core.test.entity.TestEntity",
        "      name: Grandchild Entity",
        "      brooklyn.config:",
        "        test.confName: Name of the Grandchild",
        "  - serviceType: org.apache.brooklyn.core.test.entity.TestEntity",
        "    name: Second Child",
        "    brooklyn.config:",
        "      test.confName: Name of the second Child"));
    waitForApplicationTasks(app);
    Assert.assertEquals(app.getChildren().size(), 1);
    Entity firstEntity = app.getChildren().iterator().next();
    Assert.assertEquals(firstEntity.getConfig(TestEntity.CONF_NAME), "first entity");
    Assert.assertEquals(firstEntity.getChildren().size(), 2);
    Entity firstChild = null;
    Entity secondChild = null;
    for (Entity entity : firstEntity.getChildren()) {
        if (entity.getConfig(TestEntity.CONF_NAME).equals("Name of the first Child"))
            firstChild = entity;
        if (entity.getConfig(TestEntity.CONF_NAME).equals("Name of the second Child"))
            secondChild = entity;
    }
    Assert.assertNotNull(firstChild, "Expected a child of 'first entity' with the name 'Name of the first Child'");
    Assert.assertNotNull(secondChild, "Expected a child of 'first entity' with the name 'Name of the second Child'");
    Assert.assertEquals(firstChild.getChildren().size(), 1);
    Entity grandchild = firstChild.getChildren().iterator().next();
    Assert.assertEquals(grandchild.getConfig(TestEntity.CONF_NAME), "Name of the Grandchild");
    Assert.assertEquals(secondChild.getChildren().size(), 0);
}
 
Example 8
Source File: BrooklynRestResourceUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** walks the hierarchy (depth-first) at root (often an Application) looking for
 * an entity matching the given ID or name; returns the first such entity, or null if none found
 **/
public Entity searchForEntityNamed(Entity root, String entity) {
    if (root.getId().equals(entity) || entity.equals(root.getDisplayName())) return root;
    for (Entity child: root.getChildren()) {
        Entity result = searchForEntityNamed(child, entity);
        if (result!=null) return result;
    }
    return null;
}
 
Example 9
Source File: SameServerDriverLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** @return provisioning flags for the given entity */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<String,Object> obtainProvisioningFlags(Entity entity, MachineProvisioningLocation location) {
    Map<String,Object> result = Maps.newLinkedHashMap();
    result.putAll(Maps.newLinkedHashMap(location.getProvisioningFlags(ImmutableList.of(entity.getEntityType().getName()))));
    result.putAll(entity.getConfig(SameServerEntity.PROVISIONING_PROPERTIES));

    for (Entity child : entity.getChildren()) {
        result.putAll(obtainProvisioningFlags(child, location));
    }
    return result;
}
 
Example 10
Source File: SameServerDriverLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void addRequiredOpenPortsRecursively(Entity entity, Set<Integer> ports) {
    ports.addAll(entity.getConfig(SameServerEntity.REQUIRED_OPEN_LOGIN_PORTS));
    Boolean portsAutoInfer = entity.getConfig(SameServerEntity.INBOUND_PORTS_AUTO_INFER);
    String portsRegex = entity.getConfig(SameServerEntity.INBOUND_PORTS_CONFIG_REGEX);
    ports.addAll(InboundPortsUtils.getRequiredOpenPorts(entity, portsAutoInfer, portsRegex));
    LOG.debug("getRequiredOpenPorts detected default {} for {}", ports, entity);

    for (Entity child : entity.getChildren()) {
        addRequiredOpenPortsRecursively(child, ports);
    }
}
 
Example 11
Source File: EntitiesYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testComponent() throws Exception {
    Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml",
        "  brooklyn.config:",
        "    test.confName: first entity",
        "  id: te1",
        "- serviceType: org.apache.brooklyn.core.test.entity.TestEntity",
        "  name: second entity",
        "  brooklyn.config:",
        "    test.confObject: $brooklyn:component(\"te1\")"));
    waitForApplicationTasks(app);
    Entity firstEntity = null;
    Entity secondEntity = null;
    Assert.assertEquals(app.getChildren().size(), 2);
    for (Entity entity : app.getChildren()) {
        if (entity.getDisplayName().equals("testentity"))
            firstEntity = entity;
        else if (entity.getDisplayName().equals("second entity"))
            secondEntity = entity;
    }
    final Entity[] entities = {firstEntity, secondEntity};
    Assert.assertNotNull(entities[0], "Expected app to contain child named 'testentity'");
    Assert.assertNotNull(entities[1], "Expected app to contain child named 'second entity'");
    Object object = ((EntityInternal)app).getExecutionContext().submit(MutableMap.of(), new Callable<Object>() {
        @Override
        public Object call() {
            return entities[1].getConfig(TestEntity.CONF_OBJECT);
        }}).get();
    Assert.assertNotNull(object);
    Assert.assertEquals(object, firstEntity, "Expected second entity's test.confObject to contain first entity");
}
 
Example 12
Source File: ReferencedOsgiYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void checkChildEntitySpec(Entity app, String entityName) {
    Collection<Entity> children = app.getChildren();
    Assert.assertEquals(children.size(), 1);
    Entity child = Iterables.getOnlyElement(children);
    Assert.assertEquals(child.getDisplayName(), entityName);
    Assert.assertEquals(child.getEntityType().getName(), BasicEntity.class.getName());
}
 
Example 13
Source File: ReferencedOsgiYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testCatalogReferenceSeesPreviousItems() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

    addCatalogItems(
        "brooklyn.catalog:",
        "  brooklyn.libraries:",
        "  - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
        "  items:",
        "  - id: yaml.nested.catalog.simple",
        "    itemType: entity",
        "    item:",
        "      type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY,
        "  - include: classpath://yaml-ref-back-catalog.bom");

    String entityNameSimple = "YAML -> catalog -> catalog (osgi)";
    Entity app = createAndStartApplication(
        "services:",
        "- name: " + entityNameSimple,
        "  type: back-reference");
    
    Collection<Entity> children = app.getChildren();
    Assert.assertEquals(children.size(), 1);
    Entity childSimple = Iterables.getOnlyElement(children);
    Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple);
    Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);
}
 
Example 14
Source File: ReferencedOsgiYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a YAML referenced by URL from a catalog item
 * will have access to the catalog item's bundles.
 */
@Test
public void testCatalogLeaksBundlesToReferencedYaml() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);

    String parentCatalogId = "my.catalog.app.id.url.parent";
    addCatalogItems(
        "brooklyn.catalog:",
        "  id: " + parentCatalogId,
        "  version: " + TEST_VERSION,
        "  itemType: entity",
        "  libraries:",
        "  - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
        "  item:",
        "    type: classpath://yaml-ref-bundle-without-libraries.yaml");

    Entity app = createAndStartApplication(
        "services:",
            "- type: " + ver(parentCatalogId));
    
    Collection<Entity> children = app.getChildren();
    Assert.assertEquals(children.size(), 1);
    Entity child = Iterables.getOnlyElement(children);
    Assert.assertEquals(child.getEntityType().getName(), "org.apache.brooklyn.test.osgi.entities.SimpleEntity");

    deleteCatalogEntity(parentCatalogId);
}
 
Example 15
Source File: ReferencedYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void checkGrandchildEntitySpec(Entity createAndStartApplication, String entityName) {
    Collection<Entity> children = createAndStartApplication.getChildren();
    Assert.assertEquals(children.size(), 1);
    Entity child = Iterables.getOnlyElement(children);
    Collection<Entity> grandChildren = child.getChildren();
    Assert.assertEquals(grandChildren.size(), 1);
    Entity grandChild = Iterables.getOnlyElement(grandChildren);
    Assert.assertEquals(grandChild.getDisplayName(), entityName);
    Assert.assertEquals(grandChild.getEntityType().getName(), BasicEntity.class.getName());
}
 
Example 16
Source File: ReferencedYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void checkChildEntitySpec(Entity app, String entityName) {
    Collection<Entity> children = app.getChildren();
    Assert.assertEquals(children.size(), 1);
    Entity child = Iterables.getOnlyElement(children);
    Assert.assertEquals(child.getDisplayName(), entityName);
    Assert.assertEquals(child.getEntityType().getName(), BasicEntity.class.getName());
}
 
Example 17
Source File: PlatformComponentBrooklynLookup.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public PlatformComponent get(String id) {
    Entity entity = bmc.getEntityManager().getEntity(id);
    Builder<? extends PlatformComponent> builder = PlatformComponent.builder()
        .created(new Date(entity.getCreationTime()))
        .id(entity.getId())
        .name(entity.getDisplayName())
        .externalManagementUri(BrooklynUrlLookup.getUrl(bmc, entity));
    
    for (Entity child: entity.getChildren())
        // FIXME this walks the whole damn tree!
        builder.add( get(child.getId() ));
    return builder.build();
}
 
Example 18
Source File: ApplicationResource.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/** depth 0 means no detail even at root; negative means infinite; positive means include details for that many levels 
 * (ie 1 means this entity but no details of descendants) */
private EntitySummary fromEntity(Entity entity, boolean includeTags, int detailDepth, List<String> extraSensorGlobs, List<String> extraConfigGlobs) {
    if (detailDepth==0) {
        return new EntitySummary(
            entity.getId(), 
            entity.getDisplayName(),
            entity.getEntityType().getName(),
            entity.getCatalogItemId(),
            MutableMap.of("self", EntityTransformer.entityUri(entity, ui.getBaseUriBuilder())) );
    }

    Boolean serviceUp = entity.getAttribute(Attributes.SERVICE_UP);

    Lifecycle serviceState = entity.getAttribute(Attributes.SERVICE_STATE_ACTUAL);

    String iconUrl = RegisteredTypes.getIconUrl(entity);
    if (iconUrl!=null) {
        if (brooklyn().isUrlServerSideAndSafe(iconUrl))
            // route to server if it is a server-side url
            iconUrl = EntityTransformer.entityUri(entity, ui.getBaseUriBuilder())+"/icon";
    }

    List<EntitySummary> children = Lists.newArrayList();
    if (!entity.getChildren().isEmpty()) {
        for (Entity child : entity.getChildren()) {
            if (Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, child)) {
                children.add(fromEntity(child, includeTags, detailDepth-1, extraSensorGlobs, extraConfigGlobs));
            }
        }
    }

    String parentId = null;
    if (entity.getParent()!= null) {
        parentId = entity.getParent().getId();
    }

    List<String> groupIds = Lists.newArrayList();
    if (!entity.groups().isEmpty()) {
        groupIds.addAll(entitiesIdAsArray(entity.groups()));
    }

    List<Map<String, String>> members = Lists.newArrayList();
    if (entity instanceof Group) {
        // use attribute instead of method in case it is read-only
        Collection<Entity> memberEntities = entity.getAttribute(AbstractGroup.GROUP_MEMBERS);
        if (memberEntities != null && !memberEntities.isEmpty())
            members.addAll(entitiesIdAndNameAsList(memberEntities));
    }

    EntityDetail result = new EntityDetail(
            entity.getApplicationId(),
            entity.getId(),
            parentId,
            entity.getDisplayName(),
            entity.getEntityType().getName(),
            serviceUp,
            serviceState,
            iconUrl,
            entity.getCatalogItemId(),
            children,
            groupIds,
            members,
            MutableMap.of("self", EntityTransformer.entityUri(entity, ui.getBaseUriBuilder())) );
    
    if (includeTags) {
        result.setExtraField("tags", resolving(MutableList.copyOf(entity.tags().getTags())).preferJson(true).resolve() );
    }
    result.setExtraField("creationTimeUtc", entity.getCreationTime());
    addSensorsByGlobs(result, entity, extraSensorGlobs);
    addConfigByGlobs(result, entity, extraConfigGlobs);
    
    return result;
}
 
Example 19
Source File: BrooklynRestResourceUtils.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private static void gatherAllDescendants(Entity e, List<Entity> result) {
    if (result.add(e)) {
        for (Entity ee: e.getChildren())
            gatherAllDescendants(ee, result);
    }
}