org.apache.brooklyn.core.effector.EffectorBody Java Examples

The following examples show how to use org.apache.brooklyn.core.effector.EffectorBody. 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: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link EffectorBody} which supplies the implementation for the start effector.
 * <p>
 * Calls {@link #start(Collection)} in this class.
 */
public EffectorBody<Void> newStartEffectorTask() {
    // TODO included anonymous inner class for backwards compatibility with persisted state.
    new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            Collection<? extends Location> locations  = null;

            Object locationsRaw = parameters.getStringKey(LOCATIONS.getName());
            locations = Locations.coerceToCollectionOfLocationsManaged(entity().getManagementContext(), locationsRaw);

            if (locations==null) {
                // null/empty will mean to inherit from parent
                locations = Collections.emptyList();
            }

            start(locations);
            return null;
        }
    };
    return new StartEffectorBody();
}
 
Example #2
Source File: SeaCloudsManagementPolicy.java    From SeaCloudsPlatform with Apache License 2.0 6 votes vote down vote up
private Effector<Void> newStopEffector() {
    return Effectors.effector(Startable.STOP)
            .parameter(SoftwareProcess.StopSoftwareParameters.STOP_PROCESS_MODE)
            .parameter(SoftwareProcess.StopSoftwareParameters.STOP_MACHINE_MODE)
            .impl(new EffectorBody<Void>() {
                @Override
                public Void call(ConfigBag parameters) {
                    LOG.info("Stopping SeaCloudsInitializerPolicy " + "for " + entity.getId());
                    removeSlaAgreement();
                    removeMonitoringRules();
                    removeInfluxDbObservers();
                    removeGrafanaDashboard();

                    // Rewire the original behaviour
                    ((StartableApplication) entity).stop();

                    return null;
                }
            })
            .build();
}
 
Example #3
Source File: SeaCloudsManagementPolicy.java    From SeaCloudsPlatform with Apache License 2.0 6 votes vote down vote up
private Effector<Void> newStartEffector() {
    return Effectors.effector(Startable.START)
            .impl(new EffectorBody<Void>() {
                @Override
                public Void call(ConfigBag parameters) {
                    LOG.info("Starting SeaCloudsInitializerPolicy " + "for " + entity.getId());
                    installSLA();
                    installMonitoringRules();
                    notifyRulesReady();
                    installInfluxDbObservers();
                    installGrafanaDashboards();

                    // Rewire the original behaviour
                    Collection<? extends Location> locations = null;
                    Object locationRaw = parameters.getStringKey(EffectorStartableImpl.StartParameters.LOCATIONS.getName());
                    locations = Locations.coerceToCollectionOfLocationsManaged(getManagementContext(), locationRaw);
                    ((StartableApplication) entity).start(locations);

                    return null;
                }
            })
            .build();
}
 
Example #4
Source File: EffectorTaskTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testEffectorBodyAdded() throws Exception {
    EntityInternal doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class));
    
    // not yet present
    Assert.assertNull( doubler.getEffector("double") );
    
    // add it
    doubler.getMutableEntityType().addEffector(DOUBLE_BODYLESS, new EffectorBody<Integer>() {
        @Override
        public Integer call(ConfigBag parameters) {
            int input = (Integer)parameters.getStringKey("numberToDouble");
            return queue(times(input, 2)).getUnchecked();            
        }
    });
    // now it is present
    Assert.assertNotNull( doubler.getEffector("double") );
    
    Assert.assertEquals(doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)).get(), (Integer)6);
}
 
Example #5
Source File: CouchbaseNodeImpl.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
public void init() {
    super.init();

    subscriptions().subscribe(this, Attributes.SERVICE_UP, new SensorEventListener<Boolean>() {
        @Override
        public void onEvent(SensorEvent<Boolean> booleanSensorEvent) {
            if (Boolean.TRUE.equals(booleanSensorEvent.getValue())) {
                Integer webPort = getAttribute(CouchbaseNode.COUCHBASE_WEB_ADMIN_PORT);
                Preconditions.checkNotNull(webPort, CouchbaseNode.COUCHBASE_WEB_ADMIN_PORT+" not set for %s; is an acceptable port available?", this);
                String hostAndPort = BrooklynAccessUtils.getBrooklynAccessibleAddress(CouchbaseNodeImpl.this, webPort).toString();
                sensors().set(CouchbaseNode.COUCHBASE_WEB_ADMIN_URL, URI.create(format("http://%s", hostAndPort)));
            }
        }
    });

    getMutableEntityType().addEffector(ADD_REPLICATION_RULE, new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            addReplicationRule(parameters);
            return null;
        }
    });
}
 
Example #6
Source File: TestSensorAndEffectorInitializer.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(@SuppressWarnings("deprecation") org.apache.brooklyn.api.entity.EntityLocal entity) {
    Effector<String> eff = Effectors.effector(String.class, EFFECTOR_SAY_HELLO).parameter(String.class, "name").impl(
        new EffectorBody<String>() {
            @Override
            public String call(ConfigBag parameters) {
                Object name = parameters.getStringKey("name");
                entity().sensors().set(Sensors.newStringSensor(SENSOR_LAST_HELLO), Strings.toString(name));
                return helloWord()+" "+name;
            }
        }).build();
    ((EntityInternal)entity).getMutableEntityType().addEffector(eff);
    
    ((EntityInternal)entity).getMutableEntityType().addSensor(Sensors.newStringSensor(SENSOR_HELLO_DEFINED));
    
    AttributeSensor<String> emitted = Sensors.newStringSensor(SENSOR_HELLO_DEFINED_EMITTED);
    ((EntityInternal)entity).getMutableEntityType().addSensor(emitted);
    entity.sensors().set(emitted, "1");
}
 
Example #7
Source File: CassandraNodeImpl.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
public void init() {
    super.init();
    
    // TODO PERSISTENCE WORKAROUND kept anonymous class in case referenced in persisted state
    new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return executeScript((String)parameters.getStringKey("commands"));
        }
    };
    
    getMutableEntityType().addEffector(EXECUTE_SCRIPT, new ExecuteScriptEffectorBody(this));
    
    Entities.checkRequiredUrl(this, getCassandraConfigTemplateUrl());
    Entities.getRequiredUrlConfig(this, CASSANDRA_RACKDC_CONFIG_TEMPLATE_URL);
    
    connectEnrichers();
}
 
Example #8
Source File: JcloudsLocationReleasePortForwardingDefaultTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void execRelease(final JcloudsLocation loc, final JcloudsSshMachineLocation machine) throws Exception {
    EffectorBody<Void> effectorBody = new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            loc.releasePortForwarding(machine);
            return null;
        }
    };
    Effector<Void> effector = new EffectorAndBody<Void>("myeffector", Void.class, ImmutableList.<ParameterType<?>>of(), "", 
            new EffectorBodyTaskFactory<Void>(effectorBody));
    EntityInternal entity = (EntityInternal) app.createAndManageChild(EntitySpec.create(BasicEntity.class));
    entity.getMutableEntityType().addEffector(effector);
    entity.invoke(effector, ImmutableMap.<String, Object>of()).get();
}
 
Example #9
Source File: MoreEntityImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** Unlike v1, this declares an explicit dependency on SimplePolicy */
@Override
public void init() {
    super.init();
    getMutableEntityType().addEffector(SAY_HI, new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return sayHI((String)parameters.getStringKey("name"));
        }
    });
    policies().add(PolicySpec.create(org.apache.brooklyn.test.osgi.entities.SimplePolicy.class));
}
 
Example #10
Source File: MoreEntityImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** Unlike v1, this declares an explicit dependency on SimplePolicy */
@Override
public void init() {
    super.init();
    getMutableEntityType().addEffector(SAY_HI, new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return sayHI((String)parameters.getStringKey("name"));
        }
    });
    policies().add(PolicySpec.create(org.apache.brooklyn.test.osgi.entities.SimplePolicy.class));
}
 
Example #11
Source File: MoreEntityImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public void init() {
    super.init();
    getMutableEntityType().addEffector(SAY_HI, new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return sayHI((String)parameters.getStringKey("name"));
        }
    });
}
 
Example #12
Source File: MariaDbNodeImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
public void init() {
    super.init();
    getMutableEntityType().addEffector(EXECUTE_SCRIPT, new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return executeScript((String)parameters.getStringKey("commands"));
        }
    });
}
 
Example #13
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link #restart(ConfigBag)}.
 *
 * @see {@link #newStartEffectorTask()}
 */
public EffectorBody<Void> newRestartEffectorTask() {
    // TODO included anonymous inner class for backwards compatibility with persisted state.
    new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            restart(parameters);
            return null;
        }
    };
    return new RestartEffectorBody();
}
 
Example #14
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link #stop(ConfigBag)}.
 *
 * @see {@link #newStartEffectorTask()}
 */
public EffectorBody<Void> newStopEffectorTask() {
    // TODO included anonymous inner class for backwards compatibility with persisted state.
    new EffectorBody<Void>() {
        @Override
        public Void call(ConfigBag parameters) {
            stop(parameters);
            return null;
        }
    };
    return new StopEffectorBody();
}
 
Example #15
Source File: SoftwareProcessEntityLatchTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected EntityInitializer createFailingEffectorInitializer(String name) {
    return new AddEffector(AddEffector.newEffectorBuilder(Void.class,
                    ConfigBag.newInstance(ImmutableMap.of(AddEffector.EFFECTOR_NAME, name)))
            .impl(new EffectorBody<Void>() {
                @Override
                public Void call(ConfigBag parameters) {
                    throw new IllegalStateException("Failed to start");
                }
            }).build());
}
 
Example #16
Source File: RebindEntityDynamicTypeInfoTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test(enabled=false)
public void testRestoresEffectorAnonymousClass() throws Exception {
    origApp.getMutableEntityType().addEffector(Effectors.effector(String.class, "say_hi")
        .parameter(SayHiBody.NAME_KEY)
        .description("says hello")
        .impl(new EffectorBody<String>() {
            @Override
            public String call(ConfigBag parameters) {
                return "hello "+parameters.get(SayHiBody.NAME_KEY);
            }
        }).build());
    checkEffectorWithRebind();
    
}
 
Example #17
Source File: MySqlNodeImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
public void init() {
    super.init();
    getMutableEntityType().addEffector(EXECUTE_SCRIPT, new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return executeScript((String)parameters.getStringKey("commands"));
        }
    });
    getMutableEntityType().addEffector(MySqlNodeEffectors.EXPORT_DUMP);
    getMutableEntityType().addEffector(MySqlNodeEffectors.IMPORT_DUMP);
    getMutableEntityType().addEffector(MySqlNodeEffectors.CHANGE_PASSWORD);
}
 
Example #18
Source File: PostgreSqlNodeImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
public void init() {
    super.init();
    getMutableEntityType().addEffector(EXECUTE_SCRIPT, new EffectorBody<String>() {
        @Override
        public String call(ConfigBag parameters) {
            return executeScript((String) parameters.getStringKey("commands"));
        }
    });
}
 
Example #19
Source File: EntityDynamicType.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * Adds an effector with an explicit body to this entity.
 */
@Beta
public <T> void addEffector(Effector<T> effector, EffectorBody<T> body) {
    addEffector(effector, new EffectorBodyTaskFactory<T>(body));
}
 
Example #20
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 2 votes vote down vote up
/**
 * Calls {@link #suspend(ConfigBag)}.
 *
 * @see {@link #newStartEffectorTask()}
 */
public EffectorBody<Void> newSuspendEffectorTask() {
    return new SuspendEffectorBody();
}