protected Multimap<Location, Entity> getMembersByLocation() { Multimap<Location, Entity> result = LinkedHashMultimap.create(); for (Entity member : getMembers()) { Collection<Location> memberLocs = member.getLocations(); Location memberLoc = Iterables.getFirst(memberLocs, null); if (memberLoc != null) { result.put(memberLoc, member); } } return result; }
public static String getUniqueContainerName(Entity target) { return Optional.fromNullable(target.sensors().get(DockerContainer.DOCKER_CONTAINER_NAME)) .or(Optional.fromNullable(target.config().get(DockerContainer.DOCKER_CONTAINER_NAME))) .or(Optional.fromNullable(getContainerNameFromPlan(target))) .or(Optional.fromNullable(target.config().get(DockerContainer.DOCKER_IMAGE_NAME))) .or(Optional.fromNullable(target.getId())) .transform(DockerUtils.ALLOWED) .orNull(); }
@Override public PortForwarder get() { PortForwarder result = (PortForwarder) entity.getAttribute(attributeOnEntity); if (result==null) throw new IllegalStateException("No PortForwarder available via "+attributeOnEntity+" on "+entity+" (returned null)"); return result; } });
@Override public String apply(Entity input) { if (input==null) return null; Entity entity = input; String url = String.format("#/v1/applications/%s/entities/%s", entity.getApplicationId(), entity.getId()); return url; } };
/** 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; }
public JmxHelper(Entity entity) { this(toJmxUrl(entity), entity, entity.getAttribute(UsesJmx.JMX_USER), entity.getAttribute(UsesJmx.JMX_PASSWORD)); if (entity.getAttribute(UsesJmx.JMX_URL) == null) { entity.sensors().set(UsesJmx.JMX_URL, url); } }
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()); } }
protected void deleteKubernetesResourceLocation(Entity entity) { final String namespace = entity.sensors().get(KubernetesPod.KUBERNETES_NAMESPACE); final String resourceType = entity.sensors().get(KubernetesResource.RESOURCE_TYPE); final String resourceName = entity.sensors().get(KubernetesResource.RESOURCE_NAME); if (!handleResourceDelete(resourceType, resourceName, namespace)) { LOG.warn("Resource {}: {} not deleted", resourceName, resourceType); } }
public static Maybe<String> findSubnetIp(Entity entity) { String sh = entity.getAttribute(Attributes.SUBNET_ADDRESS); if (sh!=null) return Maybe.of(sh); return findSubnetIp(entity.getLocations()); }
@Test public void testRefToSelf() throws Exception { Entity app = createAndStartApplication( "services:", "- type: " + TestEntity.class.getName(), " test.confObject: $brooklyn:self()", " test.confName: $brooklyn:self().attributeWhenReady(\"mysensor\")"); Entity entity = Iterables.getOnlyElement(app.getChildren()); assertEquals(entity.getConfig(TestEntity.CONF_OBJECT), entity); entity.sensors().set(Sensors.newStringSensor("mysensor"), "myval"); assertEquals(entity.getConfig(TestEntity.CONF_NAME), "myval"); }
@Test public void testConfigAppliedToCatalogItem() throws Exception { addCatalogEntity("test", TestEntity.class.getName()); String val = "test-applies-config-on-catalog-item"; Entity app = createAndStartApplication( "services:", "- type: test", " brooklyn.config:", " test.confName: " + val); Entity testEntity = Iterables.getOnlyElement(app.getChildren()); assertEquals(testEntity.config().get(TestEntity.CONF_NAME), val); }
@Override public boolean apply(@Nullable Entity input) { return (input != null) && Objects.equal(input.getId(), val); } };
/** @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; }
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); } }
@Test public void testDeferredDslObjectAsFirstArgument() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " location: localhost", " brooklyn.config:", " dest: $brooklyn:attributeWhenReady(\"targetValue\").config(\"spec.final\")"); AttributeSensor<Location> targetValueSensor = Sensors.newSensor(Location.class, "targetValue"); app.sensors().set(targetValueSensor, Iterables.getOnlyElement(app.getLocations())); assertEquals(getConfigEventually(app, DEST), "localhost"); }
@Test public void testWrapsAppIfNameAtTopLevelAndOnApp() throws Exception { String yaml = Joiner.on("\n").join( "name: myTopLevelName", "services:", "- serviceType: org.apache.brooklyn.core.test.entity.TestApplication", " name: myEntityName"); Entity app = createStartWaitAndLogApplication(yaml); assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER)); assertEquals(app.getDisplayName(), "myTopLevelName"); assertEquals(app.getChildren().size(), 0); }
@Test public void testRegexReplacementWithAttributeWhenReady() throws Exception { Entity testEntity = setupAndCheckTestEntityInBasicYamlWith( " brooklyn.config:", " test.regex.config: $brooklyn:regexReplacement($brooklyn:attributeWhenReady(\"test.regex.source\"), $brooklyn:attributeWhenReady(\"test.regex.pattern\"), $brooklyn:attributeWhenReady(\"test.regex.replacement\"))" ); testEntity.sensors().set(Sensors.newStringSensor("test.regex.source"), "somefooname"); testEntity.sensors().set(Sensors.newStringSensor("test.regex.pattern"), "foo"); testEntity.sensors().set(Sensors.newStringSensor("test.regex.replacement"), "bar"); Assert.assertEquals("somebarname", testEntity.getConfig(ConfigKeys.newStringConfigKey("test.regex.config"))); }
private Entity toRealEntity(Entity e) { checkNotNull(e, "entity"); if (e instanceof AbstractEntity) { return e; } else { Entity result = toRealEntityOrNull(e.getId()); if (result == null) { throw new IllegalStateException("No concrete entity known for entity "+e+" ("+e.getId()+", "+e.getEntityType().getName()+")"); } return result; } }
@Test public void testConvertsTimeUnits() { @SuppressWarnings("unchecked") TimeFractionDeltaEnricher<Integer> enricher = producer.enrichers().add(EnricherSpec.create(TimeFractionDeltaEnricher.class) .configure("producer", producer) .configure("source", intSensor) .configure("target", fractionSensor) .configure("durationPerOrigUnit", Duration.micros(1))); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 0, 1000000L)); enricher.onEvent(new BasicSensorEvent<Integer>(intSensor, producer, 1000000, 1001000L)); assertEquals(producer.getAttribute(fractionSensor), 1d); }