public static EntityConfigSummary entityConfigSummary(SpecParameter<?> input, AtomicInteger paramPriorityCnt) { // Increment the priority because the config container is a set. Server-side we are using an ordered set // which results in correctly ordered items on the wire (as a list). Clients which use the java bindings // though will push the items in an unordered set - so give them means to recover the correct order. Double priority = input.isPinned() ? Double.valueOf(paramPriorityCnt.incrementAndGet()) : null; return entityConfigSummary(input.getConfigKey(), input.getLabel(), priority, input.isPinned(), null); }
/** used if yaml specifies a parameter, but possibly incomplete, and a spec supertype has a parameter */ @SuppressWarnings("unchecked") SpecParameter<?> resolveWithAncestor(SpecParameter<?> ancestor) { if (ancestor==null) return new BasicSpecParameter<>(getLabel(), isPinned(), getConfigKey(), getSensor()); return new BasicSpecParameter( hasLabelSet ? getLabel() : ancestor.getLabel(), hasPinnedSet ? isPinned() : ancestor.isPinned(), resolveWithAncestorConfigKey(ancestor.getConfigKey()), hasType ? getSensor() : ancestor.getSensor()); }
@Test public void testOnlyName() { String name = "minRam"; SpecParameter<?> input = parseSpecParameterDefinition(ImmutableMap.of("name", name)); assertEquals(input.getLabel(), name); assertEquals(input.getConfigKey().getName(), name); assertEquals(input.getConfigKey().getTypeToken(), TypeToken.of(String.class)); }
@Override public boolean apply(SpecParameter<?> input) { return input.getConfigKey().getName().equals(param.getConfigKey().getName()); }
@Override public boolean apply(SpecParameter<?> input) { return input.getLabel().equals(label); }
private void addSpecParameters(EntitySpec<?> spec, EntityDynamicType edType) { // if coming from a catalog item, parsed by CAMP, then the spec list of parameters is canonical, // the parent item has had its config keys set as parameters here with those non-inheritable // via type definition removed, so wipe those on the EDT to make sure non-inheritable ones are removed; // OTOH if item is blank, it was set as a java type, not inheriting it, // and the config keys on the dynamic type are the correct ones to use, and usually there is nothing in spec.parameters, // except what is being added programmatically. // (this logic could get confused if catalog item ID referred to some runtime-inherited context, // but those semantics should no longer be used -- https://issues.apache.org/jira/browse/BROOKLYN-445) if (Strings.isNonBlank(spec.getCatalogItemId())) { edType.clearConfigKeys(); } for (SpecParameter<?> param : spec.getParameters()) { edType.addConfigKey(param.getConfigKey()); if (param.getSensor()!=null) edType.addSensor(param.getSensor()); } }
@Test public void testDefaultPinned() { String name = "pinned"; String label = "Is pinned"; String description = "Is pinned description"; SpecParameter<?> input = parseSpecParameterDefinition(ImmutableMap.of( "name", name, "label", label, "description", description)); assertTrue(input.isPinned()); }
@Override public boolean apply(SpecParameter<?> input) { return input.getConfigKey().getName().equals(name); }
@Override public int compare(WeightedParameter o1, WeightedParameter o2) { if (o1.getWeight() == null && o2.getWeight() == null) { return o1.getInput().getLabel().compareTo(o2.getInput().getLabel()); } else if (o1.getWeight() == null) { return 1; } else if (o2.getWeight() == null) { return -1; } else { return -Double.compare(o1.getWeight(), o2.getWeight()); } } }
public static EnricherConfigSummary enricherConfigSummary(SpecParameter<?> input) { Double priority = input.isPinned() ? Double.valueOf(1d) : null; return enricherConfigSummary(input.getConfigKey(), input.getLabel(), priority, null); } }
/** gets all the keys in the given config bag which are applicable to the given list of parameters */ public static List<FlagConfigKeyAndValueRecord> findAllParameterConfigKeys(List<SpecParameter<?>> parameters, ConfigBag input) { List<FlagConfigKeyAndValueRecord> output = new ArrayList<FlagUtils.FlagConfigKeyAndValueRecord>(); for (SpecParameter<?> param : parameters) { FlagConfigKeyAndValueRecord record = getFlagConfigKeyRecord(null, param.getConfigKey(), input); if (record.isValuePresent()) output.add(record); } return output; }
@Test public void testAppRootParameters() throws Exception { EntitySpec<? extends Application> spec = createAppSpec( "brooklyn.parameters:", "- simple", "services:", "- type: " + BasicApplication.class.getName()); final int NUM_CONFIG_KEYS_FROM_TEST_BLUEPRINT = 1; List<SpecParameter<?>> params = spec.getParameters(); assertEquals(params.size(), NUM_APP_DEFAULT_CONFIG_KEYS + NUM_CONFIG_KEYS_FROM_TEST_BLUEPRINT, "params="+params); SpecParameter<?> firstInput = params.get(0); assertEquals(firstInput.getLabel(), "simple"); }
public static PolicyConfigSummary policyConfigSummary(SpecParameter<?> input) { Double priority = input.isPinned() ? Double.valueOf(1d) : null; return policyConfigSummary(input.getConfigKey(), input.getLabel(), priority, null); }
/** merge parameters against other parameters and known and type-inherited config keys */ private static Collection<SpecParameter<?>> resolveParameters(Collection<? extends SpecParameter<?>> newParams, AbstractBrooklynObjectSpec<?,?> spec) { Collection<? extends SpecParameter<?>> existingReferenceParams = spec.getParameters(); Map<String,SpecParameter<?>> existingToKeep = MutableMap.of(); if (existingReferenceParams!=null) { for (SpecParameter<?> p: existingReferenceParams) { if (ConfigInheritances.isKeyReinheritable(p.getConfigKey(), InheritanceContext.TYPE_DEFINITION)) { existingToKeep.put(p.getConfigKey().getName(), p); } } } List<SpecParameter<?>> result = MutableList.<SpecParameter<?>>of(); if (newParams!=null) { for (SpecParameter<?> p: newParams) { final SpecParameter<?> existingP = existingToKeep.remove(p.getConfigKey().getName()); if (p instanceof SpecParameterIncludingDefinitionForInheritance) { // config keys should be transformed to parameters and so should be found; // so the code below is correct whether existingP is set or is null p = ((SpecParameterIncludingDefinitionForInheritance<?>)p).resolveWithAncestor(existingP); } else { // shouldn't happen; all calling logic should get SpecParameterForInheritance; log.warn("Found non-definitional spec parameter: "+p+" adding to "+spec); } result.add(p); } } result.addAll(existingToKeep.values()); return result; }
@Test public void testAppServiceParameters() throws Exception { EntitySpec<? extends Application> spec = createAppSpec( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.parameters:", " - simple"); final int NUM_CONFIG_KEYS_FROM_TEST_BLUEPRINT = 1; List<SpecParameter<?>> params = spec.getParameters(); assertEquals(params.size(), NUM_APP_DEFAULT_CONFIG_KEYS + NUM_CONFIG_KEYS_FROM_TEST_BLUEPRINT, "params="+params); SpecParameter<?> firstInput = params.get(0); assertEquals(firstInput.getLabel(), "simple"); }
private void assertInput(SpecParameter<?> input, String label, boolean pinned, ConfigKey<?> type) { assertEquals(input.getLabel(), label); assertEquals(input.isPinned(), pinned); assertEquals(input.getConfigKey(), type); }
@Test public void testJavaType() { String name = "minRam"; SpecParameter<?> input = parseSpecParameterDefinition(ImmutableMap.of( "name", name, "type", BasicSpecParameterFromListTest.class.getName())); assertEquals(input.getConfigKey().getTypeToken(), TypeToken.of(BasicSpecParameterFromListTest.class)); }
for (SpecParameter<?> param: spec.getParameters()) { Double priority; if (param.isPinned()) { priority = priorityCounter; priorityCounter++; EntityConfigSummary entityConfigSummary = EntityTransformer.entityConfigSummary(param.getConfigKey(), param.getLabel(), priority, param.isPinned(), MutableMap.<String,URI>of()); config.add(entityConfigSummary);
@Test public void testConstraintAsArray() { String name = "minRam"; String constraint = "required"; SpecParameter<?> input = parseSpecParameterDefinition(ImmutableMap.of( "name", name, "constraints", ImmutableList.of(constraint))); ConfigKey<?> type = input.getConfigKey(); assertConstraint(type.getConstraint(), StringPredicates.isNonBlank()); }
@Test public void testUnexpectedType() { String name = "1234"; String label = "1234"; String description = "5678.56"; String defaultValue = "444.12"; SpecParameter<?> input = parseSpecParameterDefinition(ImmutableMap.of( "name", name, "label", label, "description", description, "default", defaultValue)); assertEquals(input.getLabel(), name); assertTrue(input.isPinned()); ConfigKey<?> type = input.getConfigKey(); assertEquals(type.getName(), name); assertEquals(type.getDefaultValue(), defaultValue); assertEquals(type.getDescription(), description); assertTrue(type.getInheritanceByContext().values().isEmpty(), "Unexpected inheritance: "+type.getInheritanceByContext()); }