public static PolicySummary policySummary(Entity entity, Policy policy) { String applicationUri = "/v1/applications/" + entity.getApplicationId(); String entityUri = applicationUri + "/entities/" + entity.getId(); Map<String, URI> links = ImmutableMap.<String, URI>builder() .put("self", URI.create(entityUri + "/policies/" + policy.getId())) .put("config", URI.create(entityUri + "/policies/" + policy.getId() + "/config")) .put("start", URI.create(entityUri + "/policies/" + policy.getId() + "/start")) .put("stop", URI.create(entityUri + "/policies/" + policy.getId() + "/stop")) .put("destroy", URI.create(entityUri + "/policies/" + policy.getId() + "/destroy")) .put("application", URI.create(applicationUri)) .put("entity", URI.create(entityUri)) .build(); return new PolicySummary(policy.getId(), policy.getName(), ApplicationTransformer.statusFromLifecycle(Policies.getPolicyStatus(policy)), links); }
protected PolicySummary(Entity entity, Policy policy) { this.id = policy.getId(); this.name = policy.getName(); this.state = Policies.getPolicyStatus(policy); String applicationUri = "/v1/applications/" + entity.getApplicationId(); String entityUri = applicationUri + "/entities/" + entity.getId(); this.links = ImmutableMap.<String, URI>builder() .put("self", URI.create(entityUri + "/policies/" + policy.getId())) .put("config", URI.create(entityUri + "/policies/" + policy.getId() + "/config")) .put("start", URI.create(entityUri + "/policies/" + policy.getId() + "/start")) .put("stop", URI.create(entityUri + "/policies/" + policy.getId() + "/stop")) .put("destroy", URI.create(entityUri + "/policies/" + policy.getId() + "/destroy")) .put("application", URI.create(applicationUri)) .put("entity", URI.create(entityUri)) .build(); }
public PolicyConfigSummary(EntityLocal entity, Policy policy, ConfigKey<?> config) { super(config); String applicationUri = "/v1/applications/" + entity.getApplicationId(); String entityUri = applicationUri + "/entities/" + entity.getId(); String policyUri = entityUri + "/policies/" + policy.getId(); this.links = ImmutableMap.<String, URI>builder() .put("self", URI.create(policyUri + "/config/" + config.getName())) .put("application", URI.create(applicationUri)) .put("entity", URI.create(entityUri)) .put("policy", URI.create(policyUri)) .build(); }
public static BasicPolicyMemento.Builder newPolicyMementoBuilder(Policy policy) { BasicPolicyMemento.Builder builder = BasicPolicyMemento.builder(); builder.type = policy.getClass().getName(); builder.id = policy.getId(); builder.displayName = policy.getName(); builder.flags.putAll(FlagUtils.getFieldsWithFlagsExcludingModifiers(policy, Modifier.STATIC ^ Modifier.TRANSIENT)); return builder; } }
@Override public Map<String, Boolean> batchConfigRead( String application, String entityToken) { // TODO: add test EntityLocal entity = brooklyn().getEntity(application, entityToken); Map<String, Boolean> result = Maps.newLinkedHashMap(); for (Policy p : entity.getPolicies()) { result.put(p.getId(), !p.isSuspended()); } return result; }
@Override public String addPolicy( String application,String entityToken, String policyTypeName, // TODO would like to make this optional but jersey complains if we do Map<String, String> config ) { EntityLocal entity = brooklyn().getEntity(application, entityToken); try { Class<?> policyType = Class.forName(policyTypeName); Policy policy = (Policy) policyType.newInstance(); if (config != null && !config.isEmpty()) { // TODO support this: //policy.setConfig(config); policyType.getMethod("setConfig", Map.class).invoke(policy, config); } log.debug("REST API adding policy " + policy + " to " + entity); entity.addPolicy(policy); return policy.getId(); } catch (Exception e) { throw Exceptions.propagate(e); } }
/** finds the policy indicated by the given ID or name. * @see {@link getPolicy(String,String,String)}. * <p> * * @throws 404 or 412 (unless input is null in which case output is null) */ public Policy getPolicy(Entity entity, String policy) { if (policy==null) return null; for (Policy p: entity.getPolicies()) { if (policy.equals(p.getId())) return p; } for (Policy p: entity.getPolicies()) { if (policy.equals(p.getName())) return p; } throw WebResourceUtils.notFound("Cannot find policy '%s' in entity '%s'", policy, entity); }
/** finds the policy indicated by the given ID or name. * @see {@link getPolicy(String,String,String)}. * <p> * * @throws 404 or 412 (unless input is null in which case output is null) */ public Policy getPolicy(Entity entity, String policy) { if (policy==null) return null; for (Policy p: entity.getPolicies()) { if (policy.equals(p.getId())) return p; } for (Policy p: entity.getPolicies()) { if (policy.equals(p.getName())) return p; } throw WebResourceUtils.notFound("Cannot find policy '%s' in entity '%s'", policy, entity); }
public static PolicyConfigSummary policyConfigSummary(BrooklynRestResourceUtils utils, EntityLocal entity, Policy policy, ConfigKey<?> config) { String applicationUri = "/v1/applications/" + entity.getApplicationId(); String entityUri = applicationUri + "/entities/" + entity.getId(); String policyUri = entityUri + "/policies/" + policy.getId(); Map<String, URI> links = ImmutableMap.<String, URI>builder() .put("self", URI.create(policyUri + "/config/" + config.getName())) .put("application", URI.create(applicationUri)) .put("entity", URI.create(entityUri)) .put("policy", URI.create(policyUri)) .build(); return new PolicyConfigSummary(config.getName(), config.getTypeName(), config.getDescription(), PolicyConfigResource.getStringValueForDisplay(utils, policy, config.getDefaultValue()), config.isReconfigurable(), links); }
public PolicyDescriptor(Policy policy) { this.id = policy.getId(); this.type = policy.getPolicyType().getName(); this.name = policy.getName(); } public String getId() {
@GET @Path("/current-state") @ApiOperation(value = "Fetch policy states in batch", notes="Returns a map of policy ID to whether it is active") public Map<String, Boolean> batchConfigRead( @ApiParam(value = "Application ID or name", required = true) @PathParam("application") String application, @ApiParam(value = "Entity ID or name", required = true) @PathParam("entity") String entityToken) { // TODO: add test EntityLocal entity = brooklyn().getEntity(application, entityToken); Map<String, Boolean> result = Maps.newLinkedHashMap(); for (Policy p: entity.getPolicies()) { result.put(p.getId(), !p.isSuspended()); } return result; }
return policy.getId(); } catch (Exception e) { throw Exceptions.propagate(e);