.transforming(t) .publishing(average) .computing(new Function<Number, Double>() {
@Test public void testTransformingCastsResult() { entity.enrichers().add(Enrichers.builder() .transforming(NUM1) .publishing(LONG1) .computing(Functions.constant(Long.valueOf(1))) .build()); entity.sensors().set(NUM1, 123); EntityAsserts.assertAttributeEqualsEventually(entity, LONG1, Long.valueOf(1)); }
.transforming(STR1) .<Object>publishing(STR2) .computing(new Function<String, Object>() {
.build()); addEnricher(Enrichers.builder().transforming(WEB_CONSOLE_ACCESSIBLE) .computing(Functions.identity()) .publishing(SERVICE_PROCESS_IS_RUNNING)
@Override public void apply(EntitySpec<?> entitySpec, String nodeId, ToscaApplication toscaApplication) { // for tosca.capabilities.Endpoint and derivatives (and alien Compute) entitySpec.enricher(Enrichers.builder().transforming(Attributes.ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("ip_address")).build()); entitySpec.enricher(Enrichers.builder().transforming(Attributes.SUBNET_ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("private_address")).build()); entitySpec.enricher(Enrichers.builder().transforming(Attributes.ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("public_address")).build()); // alien specific entitySpec.enricher(Enrichers.builder().transforming(Attributes.ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("public_ip_address")).build()); // open up any ports that end with "_port" in addition to the default ".port" entitySpec.configure(SoftwareProcess.INBOUND_PORTS_CONFIG_REGEX, "(.*[\\._])?port$"); } }
@Override public void init() { super.init(); getMutableEntityType().addEffector(SelectMasterEffectorBody.SELECT_MASTER); getMutableEntityType().addEffector(BrooklynClusterUpgradeEffectorBody.UPGRADE_CLUSTER); ServiceProblemsLogic.updateProblemsIndicator(this, MASTER_NODE, MSG_NO_MASTER); addFeed(FunctionFeed.builder() .entity(this) .poll(new FunctionPollConfig<Object, BrooklynNode>(MASTER_NODE) .period(Duration.ONE_SECOND) .callable(new MasterChildFinder())) .build()); enrichers().add( Enrichers.builder().transforming(MASTER_NODE) .uniqueTag("master-node-web-uri") .publishing(BrooklynNode.WEB_CONSOLE_URI) .computing(EntityFunctions.attribute(BrooklynNode.WEB_CONSOLE_URI)) .build() ); }
public void testTransformingSuppressDuplicates() { RecordingSensorEventListener<String> record = new RecordingSensorEventListener<>(); app.getManagementContext().getSubscriptionManager().subscribe(entity, STR2, record); entity.enrichers().add(Enrichers.builder() .transforming(STR1) .publishing(STR2) .computing(Functions.<String>identity()) .suppressDuplicates(true) .build()); entity.sensors().set(STR1, "myval"); Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(1)); EntityAsserts.assertAttributeEquals(entity, STR2, "myval"); entity.sensors().set(STR1, "myval2"); entity.sensors().set(STR1, "myval2"); entity.sensors().set(STR1, "myval3"); EntityAsserts.assertAttributeEqualsContinually(entity, STR2, "myval3"); Asserts.assertThat(record.getEvents(), CollectionFunctionals.sizeEquals(3)); }
@Test public void testTransformingFromEvent() { entity.enrichers().add(Enrichers.builder() .transforming(STR1) .publishing(STR2) .computingFromEvent(new Function<SensorEvent<String>, String>() { @Override public String apply(SensorEvent<String> input) { return input.getValue() + "mysuffix"; }}) .build()); entity.sensors().set(STR1, "myval"); EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myvalmysuffix"); }
@Test public void testTransforming() { entity.enrichers().add(Enrichers.builder() .transforming(STR1) .publishing(STR2) .computing(StringFunctions.append("mysuffix")) .build()); entity.sensors().set(STR1, "myval"); EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myvalmysuffix"); }
public static final EnricherSpec<?> newEnricherForServiceUpIfNotUpIndicatorsEmpty() { // The function means: if the serviceNotUpIndicators is not null, then serviceNotUpIndicators.size()==0; // otherwise return the default value. return Enrichers.builder() .transforming(SERVICE_NOT_UP_INDICATORS).<Object>publishing(Attributes.SERVICE_UP) .suppressDuplicates(true) .computing( Functionals.<Map<String,?>> ifNotEquals(null).<Object>apply(Functions.forPredicate(CollectionFunctionals.<String>mapSizeEquals(0))) .defaultValue(Entities.UNCHANGED) ) .uniqueTag(DEFAULT_ENRICHER_UNIQUE_TAG) .build(); }
@Test public void testTransformingEnricher() throws Exception { //ensure previous values get picked up producer.sensors().set(intSensorA, 3); producer.enrichers().add(Enrichers.builder() .transforming(intSensorA) .computing(MathFunctions.times(2)) .publishing(target) // note: if `computing` comes later, we lose some type inference, have to give explicit types or go unchecked //.computing((Function)MathFunctions.times(2)) .build()); EntityAsserts.assertAttributeEqualsEventually(producer, target, 6L); }
@Test public void testFromEntity() { entity.enrichers().add(Enrichers.builder() .transforming(NUM1) .publishing(NUM1) .computing(Functions.<Integer>identity()) .from(entity2) .build()); entity2.sensors().set(NUM1, 2); EntityAsserts.assertAttributeEqualsEventually(entity, NUM1, 2); }