@VisibleForTesting public <T extends Entity> T createEntityProxy(EntitySpec<T> spec, T entity) { Set<Class<?>> interfaces = Sets.newLinkedHashSet(); if (spec.getType().isInterface()) { interfaces.add(spec.getType()); } else { log.warn("EntitySpec declared in terms of concrete type "+spec.getType()+"; should be supplied in terms of interface"); interfaces.addAll(Reflections.getAllInterfaces(spec.getType())); } interfaces.addAll(spec.getAdditionalInterfaces()); return createEntityProxy(interfaces, entity); }
/** As {@link #canUnwrapEntity(EntitySpec)} * but additionally requiring that the wrapped item is an {@link Application}, * for use when the context requires an {@link Application} ie a root of a spec. * @see #WRAPPER_APP_MARKER */ public static boolean canUnwrapApplication(EntitySpec<? extends Application> wrapperApplication) { if (!canUnwrapEntity(wrapperApplication)) return false; EntitySpec<?> childSpec = Iterables.getOnlyElement(wrapperApplication.getChildren()); return (childSpec.getType()!=null && Application.class.isAssignableFrom(childSpec.getType())); } /** @deprecated since 0.9.0 use {@link #canUnwrapApplication(EntitySpec)} */ @Deprecated
protected boolean isProxied() { String memberType = config().get(MEMBER_SPEC).getType().getSimpleName(); return memberType.contains("Proxy"); }
/** * Copies entity spec so its configuration can be overridden without modifying the * original entity spec. */ public static <T extends Entity> EntitySpec<T> create(EntitySpec<T> spec) { return create(spec.getType()).copyFrom(spec); }
public EntitySpec<T> impl(Class<? extends T> val) { checkMutable(); checkIsImplementation(checkNotNull(val, "impl"), getType()); checkIsNewStyleImplementation(val); impl = val; return this; }
protected void execOnErrorSpec() { EntitySpec<?> onErrorSpec = config().get(ON_ERROR_SPEC); if (onErrorSpec != null) { LOG.info("Creating and starting on-error child entity {} for {}", onErrorSpec.getType().getSimpleName(), this); Entity onErrorEntity = addChild(onErrorSpec); if (onErrorEntity instanceof Startable){ ((Startable) onErrorEntity).start(getLocations()); } } else { LOG.debug("No on-error spec for {}", this); } } }
private <T extends Entity> Class<? extends T> getImplementedBy(EntitySpec<T> spec) { if (spec.getImplementation() != null) { return spec.getImplementation(); } else { return entityTypeRegistry.getImplementedBy(spec.getType()); } } }
private static Set<Object> makeTags(EntitySpec<?> spec, Set<Object> tags) { // Combine tags on item with an InterfacesTag. if (spec != null) { Class<?> type; if (spec.getImplementation() != null) { type = spec.getImplementation(); } else { type = spec.getType(); } if (type != null) { tags.add(new BrooklynTags.TraitsTag(Reflections.getAllInterfaces(type))); } } return tags; }
/** * Searches for config keys in the type, additional interfaces and the implementation (if specified) */ private Collection<FlagUtils.FlagConfigKeyAndValueRecord> findAllFlagsAndConfigKeys(EntitySpec<? extends Entity> spec, ConfigBag bagFlags) { Set<FlagUtils.FlagConfigKeyAndValueRecord> allKeys = MutableSet.of(); allKeys.addAll(FlagUtils.findAllFlagsAndConfigKeys(null, spec.getType(), bagFlags)); if (spec.getImplementation() != null) { allKeys.addAll(FlagUtils.findAllFlagsAndConfigKeys(null, spec.getImplementation(), bagFlags)); } for (Class<?> iface : spec.getAdditionalInterfaces()) { allKeys.addAll(FlagUtils.findAllFlagsAndConfigKeys(null, iface, bagFlags)); } return allKeys; }
@SuppressWarnings({ "unchecked" }) private FindItemAndClass inferFrom(String type) { RegisteredType item = mgmt.getTypeRegistry().get(type); if (item != null) { return setAs( mgmt.getTypeRegistry().createSpec(item, null, org.apache.brooklyn.api.entity.EntitySpec.class).getType(), item.getId()); } else { try { setAs( (Class<? extends Entity>) getCatalog().getRootClassLoader().loadClass(type), null); log.info("Catalog does not contain item for type {}; loaded class directly instead", type); return this; } catch (ClassNotFoundException e2) { log.warn("No catalog item for type {}, and could not load class directly; rethrowing", type); throw new NoSuchElementException("Unable to find catalog item for type "+type); } } }
@Test public void testCreateSpec() { EntitySpec<?> spec = mgmt.getTypeRegistry().createSpec( RegisteredTypes.spec(specId, "1", new BasicTypeImplementationPlan(StaticTypePlanTransformer.FORMAT, null), Entity.class), null, EntitySpec.class); Assert.assertEquals(spec.getDisplayName(), DISPLAY_NAME); Assert.assertEquals(spec.getType(), BasicEntity.class); }
@Test public void testMetadataOnSpecCreatedFromItem() throws Exception { addCatalogItem("t1", TestEntity.class.getName()); EntitySpec<? extends Application> spec = EntityManagementUtils.createEntitySpecForApplication( mgmt(), Joiner.on("\n").join( "location: localhost", "services:", "- type: t1")); List<NamedStringTag> yamls = BrooklynTags.findAll(BrooklynTags.YAML_SPEC_KIND, spec.getTags()); Assert.assertEquals(yamls.size(), 1, "Expected 1 yaml tag; instead had: "+yamls); String yaml = Iterables.getOnlyElement(yamls).getContents(); Asserts.assertStringContains(yaml, "services:", "t1", "localhost"); EntitySpec<?> child = Iterables.getOnlyElement( spec.getChildren() ); Assert.assertEquals(child.getType().getName(), TestEntity.class.getName()); Assert.assertEquals(child.getCatalogItemId(), "t1:"+TEST_VERSION); }
@Test public void testMetadataOnSpecCreatedFromItem() throws Exception { makeItem("t1", SIMPLE_ENTITY_TYPE); EntitySpec<? extends Application> spec = EntityManagementUtils.createEntitySpecForApplication(mgmt(), "services: [ { type: t1 } ]\n" + "location: localhost"); List<NamedStringTag> yamls = BrooklynTags.findAll(BrooklynTags.YAML_SPEC_KIND, spec.getTags()); Assert.assertEquals(yamls.size(), 1, "Expected 1 yaml tag; instead had: "+yamls); String yaml = Iterables.getOnlyElement(yamls).getContents(); Asserts.assertStringContains(yaml, "services:", "t1", "localhost"); EntitySpec<?> child = Iterables.getOnlyElement( spec.getChildren() ); Assert.assertEquals(child.getType().getName(), SIMPLE_ENTITY_TYPE); Assert.assertEquals(child.getCatalogItemId(), "t1:"+TEST_VERSION); }
@Test public void testEntitySpecInUnmatchedConfig() throws Exception { String yaml = "services:\n"+ "- serviceType: org.apache.brooklyn.core.test.entity.TestEntity\n"+ " brooklyn.config:\n"+ " key.does.not.match:\n"+ " $brooklyn:entitySpec:\n"+ " type: org.apache.brooklyn.core.test.entity.TestEntity\n"+ " brooklyn.config:\n"+ " test.confName: inchildspec\n"; Application app = (Application) createStartWaitAndLogApplication(yaml); TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren()); EntitySpec<?> entitySpec = (EntitySpec<?>) entity.config().getBag().getStringKey("key.does.not.match"); assertEquals(entitySpec.getType(), TestEntity.class); assertEquals(entitySpec.getConfig(), ImmutableMap.of(TestEntity.CONF_NAME, "inchildspec")); }
@SuppressWarnings("unchecked") @Override public EntitySpec<? extends Application> createApplicationSpec(String plan) { Document dom = parseXml(plan); EntitySpec<?> result = toEntitySpec(dom, 0); if (Application.class.isAssignableFrom(result.getType())) { return (EntitySpec<Application>) result; } else { return EntityManagementUtils.newWrapperApp().child(result); } }
@Override public void apply(EntitySpec<?> entitySpec, String nodeId, ToscaApplication toscaApplication) { if (!entitySpec.getType().isAssignableFrom(VanillaSoftwareProcess.class)) { return; } // If the entity spec is of type VanillaSoftwareProcess, we assume that it's running. The operations should // then take care of setting up the correct scripts. // We add .getName to these ConfigKeys to use them as Flags because later we will potentially // override the configured value with a BrooklynDslDeferredSupplier object. This overridden // value would not have been used since it would be treated as a Flag while the ConfigKey would // take precedence. entitySpec.configure(VanillaSoftwareProcess.LAUNCH_COMMAND.getName(), "true"); entitySpec.configure(VanillaSoftwareProcess.STOP_COMMAND.getName(), "true"); entitySpec.configure(VanillaSoftwareProcess.CHECK_RUNNING_COMMAND.getName(), "true"); // Applying operations final Iterable<String> operations = getToscaFacade().getInterfaceOperations(nodeId, toscaApplication); for (String opKey : operations) { String computeName = toscaApplication.getNodeName(nodeId).or(String.valueOf(entitySpec.getFlags().get(ApplicationSpecsBuilder.TOSCA_TEMPLATE_ID))); final Optional<Object> script = getToscaFacade().getScript(opKey, nodeId, toscaApplication, computeName, EXPANDED_FOLDER); if (script.isPresent()) { entitySpec.configure(getToscaFacade().getLifeCycle(opKey).getName(), script.get()); } } } }
@Test public void testConfigParameterWithEntitySpecAsDefault() throws Exception { addCatalogItems( "brooklyn.catalog:", " itemType: entity", " items:", " - id: entity-with-keys", " item:", " type: "+TestEntity.class.getName(), " brooklyn.parameters:", " - name: my.param.key", " type: "+EntitySpec.class.getName(), " default: ", " $brooklyn:entitySpec:", " - type: "+BasicApplication.class.getName()); String yaml = Joiner.on("\n").join( "services:", "- type: entity-with-keys"); Entity app = createStartWaitAndLogApplication(yaml); TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren()); Object defaultVal = entity.config().get(entity.getEntityType().getConfigKey("my.param.key")); assertTrue(defaultVal instanceof EntitySpec, "defaultVal="+defaultVal); assertEquals(((EntitySpec<?>)defaultVal).getType(), BasicApplication.class, "defaultVal="+defaultVal); Entity child = entity.addChild((EntitySpec<?>)defaultVal); assertTrue(child instanceof BasicApplication, "child="+child); }
private org.apache.brooklyn.api.entity.EntitySpec<? extends Entity> toCoreEntitySpec(org.apache.brooklyn.rest.domain.EntitySpec spec) { String type = spec.getType(); String name = spec.getName(); Map<String, String> config = (spec.getConfig() == null) ? Maps.<String,String>newLinkedHashMap() : Maps.newLinkedHashMap(spec.getConfig()); FindItemAndClass itemAndClass = new FindItemAndClass().inferFrom(type); final Class<? extends Entity> clazz = itemAndClass.clazz; org.apache.brooklyn.api.entity.EntitySpec<? extends Entity> result; if (clazz.isInterface()) { result = org.apache.brooklyn.api.entity.EntitySpec.create(clazz); } else { result = org.apache.brooklyn.api.entity.EntitySpec.create(Entity.class).impl(clazz).additionalInterfaces(Reflections.getAllInterfaces(clazz)); } result.catalogItemId(itemAndClass.catalogItemId); if (!Strings.isEmpty(name)) result.displayName(name); result.configure( convertFlagsToKeys(result.getType(), config) ); configureRenderingMetadata(spec, result); return result; }
private Response launch(String yaml, EntitySpec<? extends Application> spec, Optional<String> entityId) { try { Application app = EntityManagementUtils.createUnstarted(mgmt(), spec, entityId); boolean isEntitled = Entitlements.isEntitled( mgmt().getEntitlementManager(), Entitlements.INVOKE_EFFECTOR, EntityAndItem.of(app, StringAndArgument.of(Startable.START.getName(), null))); if (!isEntitled) { throw WebResourceUtils.forbidden("User '%s' is not authorized to start application %s", Entitlements.getEntitlementContext().user(), spec.getType()); } CreationResult<Application,Void> result = EntityManagementUtils.start(app); waitForStart(app, Duration.millis(100)); log.info("Launched from YAML: " + yaml + " -> " + app + " (" + result.task() + ")"); URI ref = serviceAbsoluteUriBuilder(ui.getBaseUriBuilder(), ApplicationApi.class, "get").build(app.getApplicationId()); ResponseBuilder response = created(ref); if (result.task() != null) response.entity(TaskTransformer.fromTask(ui.getBaseUriBuilder()).apply(result.task())); return response.build(); } catch (ConstraintViolationException e) { throw new UserFacingException(e); } catch (Exception e) { throw Exceptions.propagate(e); } }
if (RegisteredTypes.isSubtypeOf(item, Entity.class)) { assertTrue(spec instanceof EntitySpec, "Not an EntitySpec: " + spec); BrooklynTypes.getDefinedEntityType(((EntitySpec<?>)spec).getType()); match++;