brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [18/50] [abbrv] git commit: Add enricher memento for rebind
Date Fri, 23 May 2014 16:57:03 GMT
Add enricher memento for rebind


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/34d4f8a4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/34d4f8a4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/34d4f8a4

Branch: refs/pull/1413/merge
Commit: 34d4f8a40c11ef5367c3058cdfd3b89a0ecbdedc
Parents: f54024f
Author: Andrew Kennedy <andrew.kennedy@cloudsoftcorp.com>
Authored: Thu May 22 15:20:31 2014 +0100
Committer: Andrew Kennedy <andrew.kennedy@cloudsoftcorp.com>
Committed: Thu May 22 15:20:31 2014 +0100

----------------------------------------------------------------------
 .../java/brooklyn/mementos/BrooklynMemento.java |  6 ++
 .../mementos/BrooklynMementoManifest.java       |  3 +
 .../mementos/BrooklynMementoPersister.java      |  2 +
 .../java/brooklyn/mementos/EnricherMemento.java | 16 +++++
 .../java/brooklyn/mementos/EntityMemento.java   |  5 ++
 .../rebind/BasicEnricherRebindSupport.java      | 50 ++++++++++++++
 .../entity/rebind/BasicPolicyRebindSupport.java |  8 +--
 .../entity/rebind/dto/BasicEnricherMemento.java | 68 ++++++++++++++++++++
 .../entity/rebind/dto/BasicEntityMemento.java   | 10 +++
 .../entity/rebind/dto/MementosGenerators.java   | 32 ++++++++-
 .../rebind/dto/MutableBrooklynMemento.java      | 39 ++++++++++-
 11 files changed, 230 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/api/src/main/java/brooklyn/mementos/BrooklynMemento.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/mementos/BrooklynMemento.java b/api/src/main/java/brooklyn/mementos/BrooklynMemento.java
index a528440..1717b3c 100644
--- a/api/src/main/java/brooklyn/mementos/BrooklynMemento.java
+++ b/api/src/main/java/brooklyn/mementos/BrooklynMemento.java
@@ -25,6 +25,8 @@ public interface BrooklynMemento extends Serializable {
     
     public PolicyMemento getPolicyMemento(String id);
     
+    public EnricherMemento getEnricherMemento(String id);
+    
     public Collection<String> getApplicationIds();
     
     public Collection<String> getTopLevelLocationIds();
@@ -35,9 +37,13 @@ public interface BrooklynMemento extends Serializable {
 
     public Collection<String> getPolicyIds();
 
+    public Collection<String> getEnricherIds();
+
     public Map<String, EntityMemento> getEntityMementos();
 
     public Map<String, LocationMemento> getLocationMementos();
 
     public Map<String, PolicyMemento> getPolicyMementos();
+
+    public Map<String, EnricherMemento> getEnricherMementos();
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/api/src/main/java/brooklyn/mementos/BrooklynMementoManifest.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/mementos/BrooklynMementoManifest.java b/api/src/main/java/brooklyn/mementos/BrooklynMementoManifest.java
index 2f3e83f..0bc10c0 100644
--- a/api/src/main/java/brooklyn/mementos/BrooklynMementoManifest.java
+++ b/api/src/main/java/brooklyn/mementos/BrooklynMementoManifest.java
@@ -15,4 +15,7 @@ public interface BrooklynMementoManifest extends Serializable {
     public Map<String, String> getLocationIdToType();
 
     public Map<String, String> getPolicyIdToType();
+
+    public Map<String, String> getEnricherIdToType();
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/api/src/main/java/brooklyn/mementos/BrooklynMementoPersister.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/mementos/BrooklynMementoPersister.java b/api/src/main/java/brooklyn/mementos/BrooklynMementoPersister.java
index ada8135..e29e70e 100644
--- a/api/src/main/java/brooklyn/mementos/BrooklynMementoPersister.java
+++ b/api/src/main/java/brooklyn/mementos/BrooklynMementoPersister.java
@@ -49,8 +49,10 @@ public interface BrooklynMementoPersister {
         Collection<LocationMemento> locations();
         Collection<EntityMemento> entities();
         Collection<PolicyMemento> policies();
+        Collection<EnricherMemento> enrichers();
         Collection<String> removedLocationIds();
         Collection<String> removedEntityIds();
         Collection<String> removedPolicyIds();
+        Collection<String> removedEnricherIds();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/api/src/main/java/brooklyn/mementos/EnricherMemento.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/mementos/EnricherMemento.java b/api/src/main/java/brooklyn/mementos/EnricherMemento.java
new file mode 100644
index 0000000..4213d40
--- /dev/null
+++ b/api/src/main/java/brooklyn/mementos/EnricherMemento.java
@@ -0,0 +1,16 @@
+package brooklyn.mementos;
+
+import java.util.Map;
+
+import brooklyn.entity.rebind.RebindSupport;
+
+/**
+ * Represents the state of an enricher, so that it can be reconstructed (e.g. after restarting
brooklyn).
+ * 
+ * @see RebindSupport
+ */
+public interface EnricherMemento extends Memento {
+
+    Map<String, Object> getFlags();
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/api/src/main/java/brooklyn/mementos/EntityMemento.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/mementos/EntityMemento.java b/api/src/main/java/brooklyn/mementos/EntityMemento.java
index 59501e2..966bedd 100644
--- a/api/src/main/java/brooklyn/mementos/EntityMemento.java
+++ b/api/src/main/java/brooklyn/mementos/EntityMemento.java
@@ -49,5 +49,10 @@ public interface EntityMemento extends Memento, TreeNode {
      * The ids of the policies of this entity.
      */
     public Collection<String> getPolicies();
+
+    /**
+     * The ids of the enrichers of this entity.
+     */
+    public Collection<String> getEnrichers();
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/core/src/main/java/brooklyn/entity/rebind/BasicEnricherRebindSupport.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/BasicEnricherRebindSupport.java b/core/src/main/java/brooklyn/entity/rebind/BasicEnricherRebindSupport.java
new file mode 100644
index 0000000..9d3a50e
--- /dev/null
+++ b/core/src/main/java/brooklyn/entity/rebind/BasicEnricherRebindSupport.java
@@ -0,0 +1,50 @@
+package brooklyn.entity.rebind;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import brooklyn.entity.rebind.dto.MementosGenerators;
+import brooklyn.mementos.PolicyMemento;
+import brooklyn.policy.basic.AbstractPolicy;
+
+public class BasicEnricherRebindSupport implements RebindSupport<PolicyMemento> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(BasicEnricherRebindSupport.class);
+    
+    private final AbstractPolicy policy;
+    
+    public BasicEnricherRebindSupport(AbstractPolicy policy) {
+        this.policy = policy;
+    }
+    
+    @Override
+    public PolicyMemento getMemento() {
+        return getMementoWithProperties(Collections.<String,Object>emptyMap());
+    }
+
+    protected PolicyMemento getMementoWithProperties(Map<String,?> props) {
+        PolicyMemento memento = MementosGenerators.newPolicyMementoBuilder(policy).customFields(props).build();
+        if (LOG.isTraceEnabled()) LOG.trace("Creating memento for policy: {}", memento.toVerboseString());
+        return memento;
+    }
+
+    @Override
+    public void reconstruct(RebindContext rebindContext, PolicyMemento memento) {
+        if (LOG.isTraceEnabled()) LOG.trace("Reconstructing policy: {}", memento.toVerboseString());
+
+        // Note that the flags have been set in the constructor
+        policy.setName(memento.getDisplayName());
+        
+        doReconsruct(rebindContext, memento);
+    }
+
+    /**
+     * For overriding, to give custom reconsruct behaviour.
+     */
+    protected void doReconsruct(RebindContext rebindContext, PolicyMemento memento) {
+        // default is no-op
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/core/src/main/java/brooklyn/entity/rebind/BasicPolicyRebindSupport.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/BasicPolicyRebindSupport.java b/core/src/main/java/brooklyn/entity/rebind/BasicPolicyRebindSupport.java
index acf0ebb..2c33702 100644
--- a/core/src/main/java/brooklyn/entity/rebind/BasicPolicyRebindSupport.java
+++ b/core/src/main/java/brooklyn/entity/rebind/BasicPolicyRebindSupport.java
@@ -27,15 +27,15 @@ public class BasicPolicyRebindSupport implements RebindSupport<PolicyMemento>
{
 
     protected PolicyMemento getMementoWithProperties(Map<String,?> props) {
         PolicyMemento memento = MementosGenerators.newPolicyMementoBuilder(policy).customFields(props).build();
-    	if (LOG.isTraceEnabled()) LOG.trace("Creating memento for policy: {}", memento.toVerboseString());
-    	return memento;
+        if (LOG.isTraceEnabled()) LOG.trace("Creating memento for policy: {}", memento.toVerboseString());
+        return memento;
     }
 
     @Override
     public void reconstruct(RebindContext rebindContext, PolicyMemento memento) {
-    	if (LOG.isTraceEnabled()) LOG.trace("Reconstructing policy: {}", memento.toVerboseString());
+        if (LOG.isTraceEnabled()) LOG.trace("Reconstructing policy: {}", memento.toVerboseString());
 
-    	// Note that the flags have been set in the constructor
+        // Note that the flags have been set in the constructor
         policy.setName(memento.getDisplayName());
         
         doReconsruct(rebindContext, memento);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/core/src/main/java/brooklyn/entity/rebind/dto/BasicEnricherMemento.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/dto/BasicEnricherMemento.java b/core/src/main/java/brooklyn/entity/rebind/dto/BasicEnricherMemento.java
new file mode 100644
index 0000000..b1d7052
--- /dev/null
+++ b/core/src/main/java/brooklyn/entity/rebind/dto/BasicEnricherMemento.java
@@ -0,0 +1,68 @@
+package brooklyn.entity.rebind.dto;
+
+import java.io.Serializable;
+import java.util.Map;
+
+import brooklyn.entity.basic.Entities;
+import brooklyn.mementos.EnricherMemento;
+
+import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.collect.Maps;
+
+/**
+ * The persisted state of a location.
+ *
+ * @author aled
+ */
+public class BasicEnricherMemento extends AbstractMemento implements EnricherMemento, Serializable
{
+
+    private static final long serialVersionUID = -1; // FIXME
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static class Builder extends AbstractMemento.Builder<Builder> {
+        protected Map<String,Object> flags = Maps.newLinkedHashMap();
+
+        public Builder from(EnricherMemento other) {
+            super.from(other);
+            flags.putAll(other.getFlags());
+            return this;
+        }
+        public Builder flags(Map<String,?> vals) {
+            flags.putAll(vals); return this;
+        }
+        public EnricherMemento build() {
+            return new BasicEnricherMemento(this);
+        }
+    }
+
+    private Map<String,Object> flags;
+    private Map<String, Object> fields;
+
+    // Trusts the builder to not mess around with mutability after calling build()
+    protected BasicEnricherMemento(Builder builder) {
+        flags = toPersistedMap(builder.flags);
+    }
+
+    @Override
+    protected void setCustomFields(Map<String, Object> fields) {
+        this.fields = toPersistedMap(fields);
+    }
+
+    @Override
+    public Map<String, Object> getCustomFields() {
+        return fromPersistedMap(fields);
+    }
+
+    @Override
+    public Map<String, Object> getFlags() {
+        return fromPersistedMap(flags);
+    }
+
+    @Override
+    protected ToStringHelper newVerboseStringHelper() {
+        return super.newVerboseStringHelper().add("flags", Entities.sanitize(getFlags()));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/core/src/main/java/brooklyn/entity/rebind/dto/BasicEntityMemento.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/dto/BasicEntityMemento.java b/core/src/main/java/brooklyn/entity/rebind/dto/BasicEntityMemento.java
index 7377629..9ded4f7 100644
--- a/core/src/main/java/brooklyn/entity/rebind/dto/BasicEntityMemento.java
+++ b/core/src/main/java/brooklyn/entity/rebind/dto/BasicEntityMemento.java
@@ -49,6 +49,7 @@ public class BasicEntityMemento extends AbstractTreeNodeMemento implements
Entit
         protected Map<AttributeSensor<?>, Object> attributes = Maps.newLinkedHashMap();
         protected List<String> locations = Lists.newArrayList();
         protected List<String> policies = Lists.newArrayList();
+        protected List<String> enrichers = Lists.newArrayList();
         protected List<String> members = Lists.newArrayList();
         protected List<Effector<?>> effectors = Lists.newArrayList();
         
@@ -61,6 +62,7 @@ public class BasicEntityMemento extends AbstractTreeNodeMemento implements
Entit
             attributes.putAll(other.getAttributes());
             locations.addAll(other.getLocations());
             policies.addAll(other.getPolicies());
+            enrichers.addAll(other.getEnrichers());
             members.addAll(other.getMembers());
             effectors.addAll(other.getEffectors());
             return this;
@@ -81,6 +83,7 @@ public class BasicEntityMemento extends AbstractTreeNodeMemento implements
Entit
     private List<String> members;
     private Map<String, Object> attributes;
     private List<String> policies;
+    private List<String> enrichers;
     
     // TODO can we move some of these to entity type, or remove/re-insert those which are
final statics?
     private Map<String, ConfigKey<?>> configKeys;
@@ -107,6 +110,7 @@ public class BasicEntityMemento extends AbstractTreeNodeMemento implements
Entit
         
         locations = toPersistedList(builder.locations);
         policies = toPersistedList(builder.policies);
+        enrichers = toPersistedList(builder.enrichers);
         members = toPersistedList(builder.members);
         
         effectors = toPersistedList(builder.effectors);
@@ -243,6 +247,11 @@ public class BasicEntityMemento extends AbstractTreeNodeMemento implements
Entit
     }
     
     @Override
+    public List<String> getEnrichers() {
+        return fromPersistedList(enrichers);
+    }
+    
+    @Override
     public List<String> getMembers() {
         return fromPersistedList(members);
     }
@@ -260,6 +269,7 @@ public class BasicEntityMemento extends AbstractTreeNodeMemento implements
Entit
                 .add("configUnmatched", Entities.sanitize(getConfigUnmatched()))
                 .add("attributes", Entities.sanitize(getAttributes()))
                 .add("policies", getPolicies())
+                .add("enrichers", getEnrichers())
                 .add("locations", getLocations());
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java b/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
index 3eaada3..c77fb34 100644
--- a/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
+++ b/core/src/main/java/brooklyn/entity/rebind/dto/MementosGenerators.java
@@ -21,9 +21,11 @@ import brooklyn.location.basic.LocationInternal;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.Task;
 import brooklyn.mementos.BrooklynMemento;
+import brooklyn.mementos.EnricherMemento;
 import brooklyn.mementos.EntityMemento;
 import brooklyn.mementos.LocationMemento;
 import brooklyn.mementos.PolicyMemento;
+import brooklyn.policy.Enricher;
 import brooklyn.policy.Policy;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
@@ -143,9 +145,13 @@ public class MementosGenerators {
         // (e.g. AbstractController registering a AbstractMembershipTrackingPolicy)
         // Also, the entity constructor often re-creates the policy
         // Also see RebindManagerImpl.CheckpointingChangeListener.onChanged(Entity)
-//        for (Policy policy : entity.getPolicies()) {
-//            builder.policies.add(policy.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;
@@ -219,4 +225,24 @@ public class MementosGenerators {
         
         return builder;
     }
+    
+    /**
+     * Given an enricher, extracts its state for serialization.
+     */
+    public static EnricherMemento newEnricherMemento(Enricher enricher) {
+        return newEnricherMementoBuilder(enricher).build();
+    }
+    
+    public static BasicEnricherMemento.Builder newEnricherMementoBuilder(Enricher enricher)
{
+        BasicEnricherMemento.Builder builder = BasicEnricherMemento.builder();
+        
+        builder.type = enricher.getClass().getName();
+        builder.typeClass = enricher.getClass();
+        builder.id = enricher.getId();
+        builder.displayName = enricher.getName();
+
+        builder.flags.putAll(FlagUtils.getFieldsWithFlagsExcludingModifiers(enricher, Modifier.STATIC
^ Modifier.TRANSIENT));
+        
+        return builder;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/34d4f8a4/core/src/main/java/brooklyn/entity/rebind/dto/MutableBrooklynMemento.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/rebind/dto/MutableBrooklynMemento.java b/core/src/main/java/brooklyn/entity/rebind/dto/MutableBrooklynMemento.java
index 1b9e4e7..61bbb8c 100644
--- a/core/src/main/java/brooklyn/entity/rebind/dto/MutableBrooklynMemento.java
+++ b/core/src/main/java/brooklyn/entity/rebind/dto/MutableBrooklynMemento.java
@@ -10,6 +10,7 @@ import org.codehaus.jackson.annotate.JsonAutoDetect;
 import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
 
 import brooklyn.mementos.BrooklynMemento;
+import brooklyn.mementos.EnricherMemento;
 import brooklyn.mementos.EntityMemento;
 import brooklyn.mementos.LocationMemento;
 import brooklyn.mementos.PolicyMemento;
@@ -36,6 +37,7 @@ public class MutableBrooklynMemento implements BrooklynMemento {
     private final Map<String, EntityMemento> entities = Maps.newLinkedHashMap();
     private final Map<String, LocationMemento> locations = Maps.newLinkedHashMap();
     private final Map<String, PolicyMemento> policies = Maps.newLinkedHashMap();
+    private final Map<String, EnricherMemento> enrichers = Maps.newLinkedHashMap();
 
     public MutableBrooklynMemento() {
     }
@@ -67,6 +69,10 @@ public class MutableBrooklynMemento implements BrooklynMemento {
         updatePolicyMementos(ImmutableSet.of(memento));
     }
     
+    public void updateEnricherMemento(EnricherMemento memento) {
+        updateEnricherMementos(ImmutableSet.of(memento));
+    }
+    
     public void updateEntityMementos(Collection<EntityMemento> mementos) {
         for (EntityMemento memento : mementos) {
             entities.put(memento.getId(), memento);
@@ -93,6 +99,12 @@ public class MutableBrooklynMemento implements BrooklynMemento {
         }
     }
     
+    public void updateEnricherMementos(Collection<EnricherMemento> mementos) {
+        for (EnricherMemento memento : mementos) {
+            enrichers.put(memento.getId(), memento);
+        }
+    }
+    
     /**
      * Removes the entities with the given ids.
      */
@@ -102,7 +114,7 @@ public class MutableBrooklynMemento implements BrooklynMemento {
     }
     
     /**
-     * Removes the entities with the given ids.
+     * Removes the locations with the given ids.
      */
     public void removeLocations(Collection<String> ids) {
         locations.keySet().removeAll(ids);
@@ -110,12 +122,19 @@ public class MutableBrooklynMemento implements BrooklynMemento {
     }
 
     /**
-     * Removes the entities with the given ids.
+     * Removes the policies with the given ids.
      */
     public void removePolicies(Collection<String> ids) {
         policies.keySet().removeAll(ids);
     }
 
+    /**
+     * Removes the enrichers with the given ids.
+     */
+    public void removeEnrichers(Collection<String> ids) {
+        enrichers.keySet().removeAll(ids);
+    }
+
     @Override
     public EntityMemento getEntityMemento(String id) {
         return entities.get(id);
@@ -130,6 +149,12 @@ public class MutableBrooklynMemento implements BrooklynMemento {
     public PolicyMemento getPolicyMemento(String id) {
         return policies.get(id);
     }
+
+    
+    @Override
+    public EnricherMemento getEnricherMemento(String id) {
+        return enrichers.get(id);
+    }
     
     @Override
     public Collection<String> getApplicationIds() {
@@ -153,6 +178,11 @@ public class MutableBrooklynMemento implements BrooklynMemento {
     }
     
     @Override
+    public Collection<String> getEnricherIds() {
+        return Collections.unmodifiableSet(enrichers.keySet());
+    }
+    
+    @Override
     public Collection<String> getTopLevelLocationIds() {
         return Collections.unmodifiableCollection(topLevelLocationIds);
     }
@@ -171,4 +201,9 @@ public class MutableBrooklynMemento implements BrooklynMemento {
     public Map<String, PolicyMemento> getPolicyMementos() {
         return ImmutableMap.copyOf(policies);
     }
+
+    @Override
+    public Map<String, EnricherMemento> getEnricherMementos() {
+        return ImmutableMap.copyOf(enrichers);
+    }
 }


Mime
View raw message