@Override public void init() { Entity delegate = getConfig(DELEGATE_ENTITY); Preconditions.checkNotNull(delegate, "delegate"); // Propagate all sensors from the delegate entity enrichers().add(Enrichers.builder() .propagatingAll() .from(delegate) .build()); // Publish the entity as an attribute for linking sensors().set(DELEGATE_ENTITY, delegate); sensors().set(DELEGATE_ENTITY_LINK, EntityUrl.entityUrl().apply(delegate)); } }
@Test public void testPropagatingAsEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(METRIC1, METRIC2)) .from(origEntity) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); EntityAsserts.assertAttributeEqualsEventually(newApp, METRIC2, "myval"); }
@Override public void init() { super.init(); ConfigToAttributes.apply(this, MESOS_SLAVE_ID); EnricherSpec<?> serviceUp = Enrichers.builder() .propagating(ImmutableMap.of(SLAVE_ACTIVE, SERVICE_UP)) .suppressDuplicates(true) .from(this) .build(); enrichers().add(serviceUp); }
app.enrichers().add(Enrichers.builder() .propagating(Sensors.newSensor(Object.class, TestEntity.NAME.getName())) .from(entity) .build()); app.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(sourceSensorFromYaml, targetSensor)) .from(app) .build()); EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, entity.sensors().get(TestEntity.NAME));
@Test public void testPropagatesAllStaticSensors() { app.enrichers().add(Enrichers.builder() .propagatingAll() .from(entity) .build()); // all attributes propagated entity.sensors().set(TestEntity.NAME, "foo"); entity.sensors().set(TestEntity.SEQUENCE, 2); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.SEQUENCE, 2); // notification-sensor propagated final AtomicReference<Integer> notif = new AtomicReference<Integer>(); app.subscriptions().subscribe(app, TestEntity.MY_NOTIF, new SensorEventListener<Integer>() { @Override public void onEvent(SensorEvent<Integer> event) { notif.set(event.getValue()); }}); entity.sensors().emit(TestEntity.MY_NOTIF, 7); Asserts.eventually(AtomicReferences.supplier(notif), Predicates.equalTo(7)); }
@Test public void testPropagatesAllSensorsIncludesDynamicallyAdded() { AttributeSensor<String> dynamicAttribute = Sensors.newStringSensor("test.dynamicsensor.strattrib"); BasicNotificationSensor<String> dynamicNotificationSensor = new BasicNotificationSensor<String>(String.class, "test.dynamicsensor.strnotif"); app.enrichers().add(Enrichers.builder() .propagatingAll() .from(entity) .build()); entity.sensors().set(dynamicAttribute, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, dynamicAttribute, "foo"); // notification-sensor propagated final AtomicReference<String> notif = new AtomicReference<String>(); app.subscriptions().subscribe(app, dynamicNotificationSensor, new SensorEventListener<String>() { @Override public void onEvent(SensorEvent<String> event) { notif.set(event.getValue()); }}); entity.sensors().emit(dynamicNotificationSensor, "mynotifval"); Asserts.eventually(AtomicReferences.supplier(notif), Predicates.equalTo("mynotifval")); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { localManagementContext = newPersistingManagementContext(); if (persister==null) { persister = localManagementContext.getRebindManager().getPersister(); } if (objectStore==null && persister instanceof BrooklynMementoPersisterToObjectStore) { objectStore = ((BrooklynMementoPersisterToObjectStore)persister).getObjectStore(); } EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) .location(LocationSpec.create(SshMachineLocation.class).configure("address", "localhost")); app = localManagementContext.getEntityManager().createEntity(appSpec); location = Iterables.getOnlyElement( app.getLocations() ); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); enricher = app.enrichers().add(Enrichers.builder().propagatingAll().from(entity).build()); policy = app.policies().add(PolicySpec.create(TestPolicy.class)); }
@Test public void testPropagatesAllBut() { app.enrichers().add(Enrichers.builder() .propagatingAllBut(TestEntity.SEQUENCE) .from(entity) .build()); // name propagated entity.sensors().set(TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); // sequence not propagated entity.sensors().set(TestEntity.SEQUENCE, 2); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); }
@Test public void testPropagatesSpecificSensor() { app.enrichers().add(Enrichers.builder() .propagating(TestEntity.NAME) .from(entity) .build()); // name propagated entity.sensors().set(TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); // sequence not propagated entity.sensors().set(TestEntity.SEQUENCE, 2); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); }
@Test public void testProducerUsingDeferredSupplier() throws Exception { producer = app.createAndManageChild(EntitySpec.create(TestEntity.class) .configure(TAG, "myproducer")); target = app.createAndManageChild(EntitySpec.create(TestEntity.class)); sensor = new BasicAttributeSensor<Integer>(Integer.class, "int.sensor.a"); app.start(ImmutableList.of(new SimulatedLocation())); producer.sensors().set(sensor, 3); target.enrichers().add(Enrichers.builder() .propagating(sensor) .from(new EntityDeferredSupplier("myproducer").newTask()) .build()); EntityAsserts.assertAttributeEqualsEventually(target, sensor, 3); }
void connectSensors() { // FIXME no longer needed enrichers().add(Enrichers.builder() .propagatingAllButUsualAnd(Attributes.MAIN_URI, ROOT_URL, GROUP_MEMBERS, GROUP_SIZE) .from(getCluster()) .build()); enrichers().add(Enrichers.builder() // include hostname and address of controller (need both in case hostname only resolves to internal/private ip) .propagating(LoadBalancer.HOSTNAME, Attributes.ADDRESS, Attributes.MAIN_URI, ROOT_URL) .from(getController()) .build()); }
@Test public void testPropagatingAndRenaming() { entity.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(STR1, STR2)) .from(entity2) .build()); entity2.sensors().set(STR1, "myval"); EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myval"); }
@Test public void testPropagatingAsDifferentSensor() { final AttributeSensor<String> ANOTHER_ATTRIBUTE = Sensors.newStringSensor("another.attribute", ""); app.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(TestEntity.NAME, ANOTHER_ATTRIBUTE)) .from(entity) .build()); // name propagated as different attribute entity.sensors().set(TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, ANOTHER_ATTRIBUTE, "foo"); }
@Test public void testPropagatesCurrentValue() { entity.sensors().set(TestEntity.NAME, "foo"); app.enrichers().add(Enrichers.builder() .propagating(TestEntity.NAME) .from(entity) .build()); // name propagated EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); }
@Test public void testPropagatingAllEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .propagatingAll() .from(origEntity) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); EntityAsserts.assertAttributeEqualsEventually(newApp, METRIC1, "myval"); }
@Test public void testPropagatingEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .propagating(METRIC1) .from(origEntity) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); EntityAsserts.assertAttributeEqualsEventually(newApp, METRIC1, "myval"); }
@Test public void testSensorPropagatedWhenMappingUsedSameNameButDifferentType() throws Exception { AttributeSensor<String> origSensor = Sensors.newSensor(String.class, "origSensor"); AttributeSensor<Object> sourceSensorFromYaml = Sensors.newSensor(Object.class, "origSensor"); AttributeSensor<Object> targetSensor = Sensors.newSensor(Object.class, "newSensor"); app.enrichers().add(Enrichers.builder() .propagating(ImmutableMap.of(sourceSensorFromYaml, targetSensor)) .from(entity) .build()); entity.sensors().set(origSensor, "myval"); EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, "myval"); }
@Test public void testPropagating() { entity.enrichers().add(Enrichers.builder() .propagating(ImmutableList.of(STR1)) .from(entity2) .build()); entity2.sensors().set(STR1, "myval"); EntityAsserts.assertAttributeEqualsEventually(entity, STR1, "myval"); entity2.sensors().set(STR1, null); EntityAsserts.assertAttributeEqualsEventually(entity, STR1, null); }