Java Code Examples for org.apache.brooklyn.core.config.ConfigKeys#newConfigKey()

The following examples show how to use org.apache.brooklyn.core.config.ConfigKeys#newConfigKey() . 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: DslYamlTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeferredDslChainingOnConfigNoFunction() throws Exception {
    final Entity app = createAndStartApplication(
            "services:",
            "- type: " + BasicApplication.class.getName(),
            "  brooklyn.config:",
            "    dest: $brooklyn:config(\"targetValue\").getNonExistent()");
    ConfigKey<TestDslSupplierValue> targetValueKey = ConfigKeys.newConfigKey(TestDslSupplierValue.class, "targetValue");
    app.config().set(targetValueKey, new TestDslSupplierValue());
    try {
        assertEquals(getConfigEventually(app, DEST), app.getId());
        Asserts.shouldHaveFailedPreviously("Expected to fail because method does not exist");
    } catch (Exception e) {
        Asserts.expectedFailureContains(e, "No such function 'getNonExistent'");
        Asserts.expectedFailureDoesNotContain(e, "$brooklyn:$brooklyn:");
    }
}
 
Example 2
Source File: InboundPortsUtilsTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetRequiredOpenPortsGetsDynamicallyAddedPortBasedKeys() {
    TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));

    PortAttributeSensorAndConfigKey newTestConfigKeyPort = ConfigKeys.newPortSensorAndConfigKey("new.test.config.port.string.first", "port", "7777+");
    PortAttributeSensorAndConfigKey newTestConfigKeyPort2 = ConfigKeys.newPortSensorAndConfigKey("new.test.config.port.string.second", "port");

    ConfigKey<Object> newTestConfigKeyObject = ConfigKeys.newConfigKey(Object.class, "new.test.config.object");
    ConfigKey<String> newTestConfigKeyString = ConfigKeys.newStringConfigKey("new.test.config.key.string");
    entity.config().set(newTestConfigKeyPort, PortRanges.fromString("8888+"));
    entity.config().set(newTestConfigKeyPort2, PortRanges.fromInteger(9999));
    entity.config().set(newTestConfigKeyObject, PortRanges.fromInteger(2222));
    entity.config().set(newTestConfigKeyString, "foo.bar");

    Collection<Integer> dynamicRequiredOpenPorts = InboundPortsUtils.getRequiredOpenPorts(entity, ImmutableSet.<ConfigKey<?>>of(), true, null);
    Assert.assertTrue(dynamicRequiredOpenPorts.contains(8888));
    Assert.assertTrue(dynamicRequiredOpenPorts.contains(9999));
    Assert.assertTrue(dynamicRequiredOpenPorts.contains(2222));
}
 
Example 3
Source File: DslAndRebindYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void doTestOnEntityWithSensor(Entity testEntity, Sensor<?> expectedSensor, boolean inTask) throws Exception {
    @SuppressWarnings("rawtypes")
    ConfigKey<Sensor> configKey = ConfigKeys.newConfigKey(Sensor.class, "test.sensor");
    Sensor<?> s;
    s = inTask ? getConfigInTask(testEntity, configKey) : testEntity.getConfig(configKey);
    Assert.assertEquals(s, expectedSensor);
    Entity te2 = rebind(testEntity);
    s = inTask ? getConfigInTask(te2, configKey) : te2.getConfig(configKey);
    Assert.assertEquals(s, expectedSensor);
}
 
Example 4
Source File: RebindEntityTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testRebindNonAnonymousKeyAlsoGivesCorrectType() throws Exception {
    // happens if we write yaml and put an int where a double is expected
    final String doubleKeyName = "double.key";
    final ConfigKey<Object> keyAsObject = ConfigKeys.newConfigKey(Object.class, doubleKeyName);
    final ConfigKey<Double> keyAsDouble = ConfigKeys.newDoubleConfigKey(doubleKeyName);
    // set an int, casting to allow it, but should be recording doubleness internally
    origApp.config().set((ConfigKey<Object>) (ConfigKey<?>) keyAsDouble, (int) 1);
    // get the double when queried
    Asserts.assertInstanceOf(origApp.config().get(keyAsObject), Double.class);
    // also assert the key isn't included in declared list
    Optional<ConfigKey<?>> declaredKey = Iterables.tryFind(getTypeDeclaredKeys(origApp), (k) -> k.getName().equals(doubleKeyName));
    if (declaredKey.isPresent()) Assert.fail("Shouldn't have declared anonymous key, but had: "+declaredKey.get());
    
    newApp = rebind();
    // should know internally it's a double
    Asserts.assertInstanceOf(newApp.config().get(keyAsDouble), Double.class);
    Asserts.assertInstanceOf(newApp.config().get(keyAsObject), Double.class);
    
    // this works because we add the key to the type-declared items on persistence;
    // this has always been the case, even though it introduces an asymmetry,
    // it's the only place we currently have on the persisted entity to store keys
    // (whereas the in-memory can keep dynamic keys in its map, not on the type;
    // ideally the in-memory map would just use strings, and type would be added
    // on set to the dynamic type if it's non-anonymous; see further notes in BasicEntityMemento.isAnonymous)
    Optional<ConfigKey<?>> persistedKey = Iterables.tryFind(getTypeDeclaredKeys(newApp), (k) -> k.getName().equals(doubleKeyName));
    if (!persistedKey.isPresent()) Assert.fail("Should have persisted non-anonymous key: "+persistedKey.get());
}
 
Example 5
Source File: RebindEntityTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testRebindAnonymousKeyDowncastedGivesCorrectType() throws Exception {
    // happens if we write yaml and put an int where a double is expected
    final String doubleKeyName = "double.key";
    final ConfigKey<Object> keyAsObject = ConfigKeys.newConfigKey(Object.class, doubleKeyName);
    final ConfigKey<Double> keyAsDouble = ConfigKeys.newDoubleConfigKey(doubleKeyName);
    // set an int
    origApp.config().set(keyAsObject, (int) 1);
    // get the double when queried
    Asserts.assertInstanceOf(origApp.config().get(keyAsDouble), Double.class);
    // but doesn't actually know it's a double
    Asserts.assertInstanceOf(origApp.config().get(keyAsObject), Integer.class);
    // also assert the key isn't included in declared list
    Optional<ConfigKey<?>> declaredKey = Iterables.tryFind(getTypeDeclaredKeys(origApp), (k) -> k.getName().equals(doubleKeyName));
    if (declaredKey.isPresent()) Assert.fail("Shouldn't have declared anonymous key, but had: "+declaredKey.get());
    
    newApp = rebind();
    // now (2017-11) this works because we check both types on lookup
    Asserts.assertInstanceOf(newApp.config().get(keyAsDouble), Double.class);
    // if not querying double, we get the original type
    Asserts.assertInstanceOf(newApp.config().get(keyAsObject), Integer.class);
    
    // and this also succeeds because because now the anonymous key definition is not persisted
    // (test changed, but confirmed it fails without the new BasicEntityMemento.isAnonymous check)
    Optional<ConfigKey<?>> persistedKey = Iterables.tryFind(getTypeDeclaredKeys(newApp), (k) -> k.getName().equals(doubleKeyName));
    if (persistedKey.isPresent()) Assert.fail("Shouldn't have persisted anonymous key, but had: "+persistedKey.get());
}
 
Example 6
Source File: BrooklynJacksonJsonProvider.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Finds a shared {@link ObjectMapper} or makes a new one, stored against the servlet context;
 * returns null if a shared instance cannot be created.
 */
public static ObjectMapper findSharedObjectMapper(ManagementContext mgmt) {
    checkNotNull(mgmt, "mgmt");
    synchronized (mgmt) {
        ConfigKey<ObjectMapper> key = ConfigKeys.newConfigKey(ObjectMapper.class, BROOKLYN_REST_OBJECT_MAPPER);
        ObjectMapper mapper = (ObjectMapper) mgmt.getScratchpad().get(key);
        if (mapper != null) return mapper;

        mapper = newPrivateObjectMapper(mgmt);
        log.debug("Storing new ObjectMapper against "+mgmt+" because no ServletContext available: "+mapper);
        mgmt.getScratchpad().put(key, mapper);
        return mapper;
    }
}
 
Example 7
Source File: BrooklynDslCommon.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public Maybe<Object> getImmediately() {
    if (obj instanceof Entity) {
        // Shouldn't worry too much about it since DSL can fetch objects from same app only.
        // Just in case check whether it's same app for entities.
        checkState(entity().getApplicationId().equals(((Entity)obj).getApplicationId()));
    }
    String keyNameS = resolveKeyName(true);
    ConfigKey<Object> key = ConfigKeys.newConfigKey(Object.class, keyNameS);
    Maybe<? extends Object> result = ((AbstractConfigurationSupportInternal)obj.config()).getNonBlocking(key);
    return Maybe.<Object>cast(result);
}
 
Example 8
Source File: DslTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigWithDsl() throws Exception {
    ConfigKey<?> configKey = ConfigKeys.newConfigKey(Entity.class, "testConfig");
    BrooklynDslDeferredSupplier<?> dsl = BrooklynDslCommon.config(configKey.getName());
    Supplier<ConfigValuePair> valueSupplier = new Supplier<ConfigValuePair>() {
        @Override public ConfigValuePair get() {
            return new ConfigValuePair(BrooklynDslCommon.root(), app);
        }
    };
    new ConfigTestWorker(app, configKey, valueSupplier, dsl).run();
}
 
Example 9
Source File: DslYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferredDslChainingWithNestedEvaluation() throws Exception {
    final Entity app = createAndStartApplication(
            "services:",
            "- type: " + BasicApplication.class.getName(),
            "  brooklyn.config:",
            "    dest: $brooklyn:config(\"customCallableWrapper\").getSupplier().isSupplierCallable()");
    ConfigKey<TestDslSupplier> customCallableWrapperKey = ConfigKeys.newConfigKey(TestDslSupplier.class, "customCallableWrapper");
    app.config().set(customCallableWrapperKey, new TestDslSupplier(new DslTestSupplierWrapper(new DslTestCallable())));
    assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
}
 
Example 10
Source File: DslYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferredDslChainingWithCustomCallable() throws Exception {
    final Entity app = createAndStartApplication(
            "services:",
            "- type: " + BasicApplication.class.getName(),
            "  brooklyn.config:",
            "    dest: $brooklyn:config(\"customCallableWrapper\").getSupplier().isSupplierCallable()");
    ConfigKey<DslTestSupplierWrapper> customCallableWrapperKey = ConfigKeys.newConfigKey(DslTestSupplierWrapper.class, "customCallableWrapper");
    app.config().set(customCallableWrapperKey, new DslTestSupplierWrapper(new DslTestCallable()));
    assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
}
 
Example 11
Source File: DslYamlTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeferredDslChainingWithCustomSupplier() throws Exception {
    final Entity app = createAndStartApplication(
            "services:",
            "- type: " + BasicApplication.class.getName(),
            "  brooklyn.config:",
            "    dest: $brooklyn:config(\"customSupplierWrapper\").getSupplier().isSupplierEvaluated()");
    ConfigKey<DslTestSupplierWrapper> customSupplierWrapperKey = ConfigKeys.newConfigKey(DslTestSupplierWrapper.class, "customSupplierWrapper");
    app.config().set(customSupplierWrapperKey, new DslTestSupplierWrapper(new TestDslSupplier(new TestDslSupplierValue())));
    assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
}
 
Example 12
Source File: EntityRefsYamlTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected ConfigKey<Object> newConfigKey(String name) {
    return ConfigKeys.newConfigKey(Object.class, name);
}
 
Example 13
Source File: EntityPredicates.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static Predicate<Entity> configSatisfies(final String configKeyName, final Predicate<Object> condition) {
    return new ConfigKeySatisfies<Object>(ConfigKeys.newConfigKey(Object.class, configKeyName), condition);
}
 
Example 14
Source File: EffectorTasks.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static <T> ConfigKey<T> asConfigKey(ParameterType<T> t) {
    return ConfigKeys.newConfigKey(t.getParameterType(), t.getName());
}
 
Example 15
Source File: Effectors.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static <V> ConfigKey<V> asConfigKey(ParameterType<V> paramType) {
    return ConfigKeys.newConfigKey(paramType.getParameterType(), paramType.getName(), paramType.getDescription(), paramType.getDefaultValue());
}
 
Example 16
Source File: SpecParameterUnwrappingTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test
public void testParametersCoercedOnSetAndReferences() throws Exception {
    Integer testValue = Integer.valueOf(55);
    addCatalogItems(
            "brooklyn.catalog:",
            "  id: " + SYMBOLIC_NAME,
            "  version: " + TEST_VERSION,
            "  itemType: entity",
            "  item:",
            "    type: " + BasicApplication.class.getName(),
            "    brooklyn.parameters:",
            "    - name: num",
            "      type: integer",
            "    brooklyn.children:",
            "    - type: " + ConfigEntityForTest.class.getName(),
            "      brooklyn.config:",
            "        refConfig: $brooklyn:scopeRoot().config(\"num\")",
            "    - type: " + ConfigEntityForTest.class.getName(),
            "      brooklyn.config:",
            "        refConfig: $brooklyn:config(\"num\")"); //inherited config

    Entity app = createAndStartApplication(
            "services:",
            "- type: " + BasicApplication.class.getName(),
            "  brooklyn.children:",
            "  - type: " + ver(SYMBOLIC_NAME),
            "    brooklyn.config:",
            "      num: \"" + testValue + "\"");

    Entity scopeRoot = Iterables.getOnlyElement(app.getChildren());

    ConfigKey<Object> numKey = ConfigKeys.newConfigKey(Object.class, "num");
    assertEquals(scopeRoot.config().get(numKey), testValue);

    ConfigKey<Object> refConfigKey = ConfigKeys.newConfigKey(Object.class, "refConfig");

    Iterator<Entity> childIter = scopeRoot.getChildren().iterator();
    Entity c1 = childIter.next();
    assertEquals(c1.config().get(refConfigKey), testValue);
    Entity c2 = childIter.next();
    assertEquals(c2.config().get(refConfigKey), testValue);
    assertFalse(childIter.hasNext());
}
 
Example 17
Source File: ConfigInheritanceYamlTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private void assertAnonymousKeyValueEquals(Entity entity, String keyName, Object expected) {
    ConfigKey<?> key2 = ConfigKeys.newConfigKey(Object.class, keyName);
    assertEquals(entity.config().get(key2), expected, "key="+keyName);
}