public static BasicPolicyMemento.Builder newPolicyMementoBuilder(Policy policy) {
    BasicPolicyMemento.Builder builder = BasicPolicyMemento.builder();

    builder.type = policy.getClass().getName();
    builder.typeClass = policy.getClass();
    builder.id = policy.getId();
    builder.displayName = policy.getName();

    // TODO persist config keys as well? Or only support those defined on policy class;
    // current code will lose the ConfigKey type on rebind for anything not defined on class.
    // Whereas entities support that.
    // TODO Do we need the "nonPersistableFlagNames" that locations use?
    Map<ConfigKey<?>, Object> config = ((AbstractPolicy) policy).getConfigMap().getAllConfig();
    for (Map.Entry<ConfigKey<?>, Object> entry : config.entrySet()) {
      ConfigKey<?> key = checkNotNull(entry.getKey(), "config=%s", config);
      Object value = configValueToPersistable(entry.getValue());
      builder.config.put(key.getName(), value);
    }

    Map<String, Object> persistableFlags =
        MutableMap.<String, Object>builder()
            .putAll(
                FlagUtils.getFieldsWithFlagsExcludingModifiers(
                    policy, Modifier.STATIC ^ Modifier.TRANSIENT))
            .remove("id")
            .remove("name")
            .build();
    builder.config.putAll(persistableFlags);

    return builder;
  }
 @Override
 public void onRebindPolicyFailed(Policy policy, Exception e) {
   rebindPolicyFailures.put(
       policy,
       new IllegalStateException(
           "problem rebinding plicy " + policy.getId() + " (" + policy + ")", e));
   super.onRebindPolicyFailed(policy, e);
 }
  @Test
  public void testCheckPointAndLoadMemento()
      throws IOException, TimeoutException, InterruptedException {
    BrooklynMemento reloadedMemento = loadMemento();

    assertNotNull(reloadedMemento);
    assertTrue(Iterables.contains(reloadedMemento.getEntityIds(), entity.getId()));
    assertEquals(Iterables.getOnlyElement(reloadedMemento.getLocationIds()), location.getId());
    assertEquals(Iterables.getOnlyElement(reloadedMemento.getPolicyIds()), policy.getId());
    assertEquals(Iterables.getOnlyElement(reloadedMemento.getEnricherIds()), enricher.getId());
  }
  /**
   * 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 BasicEntityMemento.Builder newEntityMementoBuilder(Entity entity) {
    EntityDynamicType definedType = EntityTypes.getDefinedEntityType(entity.getClass());
    BasicEntityMemento.Builder builder = BasicEntityMemento.builder();

    builder.id = entity.getId();
    builder.displayName = entity.getDisplayName();
    builder.type = entity.getClass().getName();
    builder.typeClass = entity.getClass();

    // TODO the dynamic attributeKeys and configKeys are computed in the BasicEntityMemento
    // whereas effectors are computed here -- should be consistent!
    // (probably best to compute attrKeys and configKeys here)
    builder.effectors.addAll(entity.getEntityType().getEffectors());
    builder.effectors.removeAll(definedType.getEffectors().values());

    builder.isTopLevelApp = (entity instanceof Application && entity.getParent() == null);

    Map<ConfigKey<?>, Object> localConfig =
        ((EntityInternal) entity).getConfigMap().getLocalConfig();
    for (Map.Entry<ConfigKey<?>, Object> entry : localConfig.entrySet()) {
      ConfigKey<?> key = checkNotNull(entry.getKey(), localConfig);
      Object value = configValueToPersistable(entry.getValue());
      builder.config.put(key, value);
    }

    Map<String, Object> localConfigUnmatched =
        MutableMap.copyOf(
            ((EntityInternal) entity).getConfigMap().getLocalConfigBag().getAllConfig());
    for (ConfigKey<?> key : localConfig.keySet()) {
      localConfigUnmatched.remove(key.getName());
    }
    for (Map.Entry<String, Object> entry : localConfigUnmatched.entrySet()) {
      String key = checkNotNull(entry.getKey(), localConfig);
      Object value = entry.getValue();
      // TODO Not transforming; that code is deleted in another pending PR anyway!
      builder.configUnmatched.put(key, value);
    }

    @SuppressWarnings("rawtypes")
    Map<AttributeSensor, Object> allAttributes = ((EntityInternal) entity).getAllAttributes();
    for (@SuppressWarnings("rawtypes")
    Map.Entry<AttributeSensor, Object> entry : allAttributes.entrySet()) {
      AttributeSensor<?> key = checkNotNull(entry.getKey(), allAttributes);
      Object value = entry.getValue();
      builder.attributes.put((AttributeSensor<?>) key, value);
    }

    for (Location location : entity.getLocations()) {
      builder.locations.add(location.getId());
    }

    for (Entity child : entity.getChildren()) {
      builder.children.add(child.getId());
    }

    for (Policy policy : entity.getPolicies()) {
      builder.policies.add(policy.getId());
    }

    for (Enricher enricher : entity.getEnrichers()) {
      builder.enrichers.add(enricher.getId());
    }

    Entity parentEntity = entity.getParent();
    builder.parent = (parentEntity != null) ? parentEntity.getId() : null;

    if (entity instanceof Group) {
      for (Entity member : ((Group) entity).getMembers()) {
        builder.members.add(member.getId());
      }
    }

    return builder;
  }