You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by gr...@apache.org on 2016/12/12 18:17:40 UTC

[1/4] brooklyn-server git commit: Fix order of provisioner check and add tests for LocationEntity

Repository: brooklyn-server
Updated Branches:
  refs/heads/master e997d9abd -> 7f1846c0f


Fix order of provisioner check and add tests for LocationEntity


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/70baabbd
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/70baabbd
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/70baabbd

Branch: refs/heads/master
Commit: 70baabbd9b4b0a2b6c4af0bcf265d48f9b5aa125
Parents: 6615ddf
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Tue Nov 22 13:52:00 2016 +0000
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Tue Nov 22 13:52:15 2016 +0000

----------------------------------------------------------------------
 .../entity/stock/LocationEntityImpl.java        |  16 +-
 .../entity/stock/LocationEntityTest.java        | 182 +++++++++++++++++++
 2 files changed, 190 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/70baabbd/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
index fbfcdc5..eee147e 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
@@ -79,17 +79,17 @@ public class LocationEntityImpl extends BasicStartableImpl implements LocationEn
             if (child == null && specMap.size() > 0) {
                 // Determine provisioning location
                 MachineProvisioningLocation<?> provisioner = null;
-                Maybe<? extends Location> location = Machines.findUniqueElement(locations, MachineLocation.class);
+                Maybe<? extends Location> location = Machines.findUniqueElement(locations, MachineProvisioningLocation.class);
                 if (location.isPresent()) {
-                    Location parent = location.get().getParent();
-                    while (parent != null && ! (parent instanceof MachineProvisioningLocation)) {
-                        parent = parent.getParent();
-                    }
-                    provisioner = (MachineProvisioningLocation<?>) parent;
+                    provisioner = (MachineProvisioningLocation<?>) location.get();
                 } else {
-                    location = Machines.findUniqueElement(locations, MachineProvisioningLocation.class);
+                    location = Machines.findUniqueElement(locations, MachineLocation.class);
                     if (location.isPresent()) {
-                        provisioner = (MachineProvisioningLocation<?>) location.get();
+                        Location parent = location.get().getParent();
+                        while (parent != null && ! (parent instanceof MachineProvisioningLocation)) {
+                            parent = parent.getParent();
+                        }
+                        provisioner = (MachineProvisioningLocation<?>) parent;
                     }
                 }
                 if (provisioner == null) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/70baabbd/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java b/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java
new file mode 100644
index 0000000..009841f
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.entity.stock;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Map;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.core.entity.EntityAsserts;
+import org.apache.brooklyn.core.entity.trait.Startable;
+import org.apache.brooklyn.core.location.LocationConfigKeys;
+import org.apache.brooklyn.core.location.SimulatedLocation;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+
+public class LocationEntityTest extends BrooklynAppUnitTestSupport {
+
+    private SimulatedLocation loc1, loc2, loc3;
+    private LocationEntity entity;
+
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        loc1 = mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class));
+        loc2 = mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class).configure(LocationConfigKeys.CLOUD_PROVIDER, "cloud"));
+        loc3 = mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class).configure(LocationConfigKeys.ISO_3166, ImmutableSet.of("UK", "US")));
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithType() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, EntitySpec<?>>builder()
+                        .put("OtherLocation", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .put("SimulatedLocation", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Simulated"))
+                        .put("default", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "Simulated");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithDefault() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, EntitySpec<?>>builder()
+                        .put("default", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "Default");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithWrongTypeAndDefault() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, EntitySpec<?>>builder()
+                        .put("OtherLocation", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .put("default", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "Default");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithWrongTypeAndNoDefault() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, EntitySpec<?>>builder()
+                        .put("OtherLocation", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertTrue(entity.getChildren().isEmpty());
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_ENTITY, null);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithProvider() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, EntitySpec<?>>builder()
+                        .put("cloud", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Cloud"))
+                        .put("other", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .put("default", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc2));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_PROVIDER, "cloud");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "Cloud");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithCountry() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, EntitySpec<?>>builder()
+                        .put("UK", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "United Kingdom"))
+                        .put("DE", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Germany"))
+                        .put("default", EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc3));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_COUNTRY_CODES, ImmutableSet.of("UK", "US"));
+        EntityAsserts.assertAttributeEqualsEventually(entity, LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "United Kingdom");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+}


[2/4] brooklyn-server git commit: Save location information as sensor data on LocationEntity

Posted by gr...@apache.org.
Save location information as sensor data on LocationEntity


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/6615ddf7
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/6615ddf7
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/6615ddf7

Branch: refs/heads/master
Commit: 6615ddf70d63a7c71e93d6b25a7987ca269b2f02
Parents: b119889
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Fri Nov 11 12:17:25 2016 +0000
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Tue Nov 22 13:52:15 2016 +0000

----------------------------------------------------------------------
 .../org/apache/brooklyn/entity/stock/LocationEntity.java  | 10 ++++++++++
 .../apache/brooklyn/entity/stock/LocationEntityImpl.java  |  3 +++
 2 files changed, 13 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6615ddf7/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
index 4e8cfd1..9a72bee 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.entity.stock;
 
 import java.util.Collection;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
@@ -39,6 +40,10 @@ import com.google.common.reflect.TypeToken;
  * <p>
  * Can use the class name of the {@link MachineProvisioningLocation} or the value of the
  * {@code provider} or {@code iso3166} configuration keys on the provisioning location.
+ * The values used to make the decision are set as sensors on this entity. The decision
+ * is made based on the first match found, checking the class name first, then the
+ * provider and finally the country codes, and map keys representing any or all of these
+ * can be used at the same time.
  * <pre>
  * - type: org.apache.brooklyn.entity.stock.LocationEntity
  *   brooklyn.config:
@@ -74,6 +79,11 @@ public interface LocationEntity extends BasicStartable {
     ConfigKey<Collection<AttributeSensor<?>>> LOCATION_ENTITY_SENSOR_LIST = ConfigKeys.newConfigKey(new TypeToken<Collection<AttributeSensor<?>>>() { },
             "location.entity.sensors", "Collection of sensors that are to be propagated from the child entity (all usual sensors if not set, or empty)");
 
+    AttributeSensor<String> LOCATION_TYPE = Sensors.newStringSensor("location.entity.type", "The class name of the entity location");
+    AttributeSensor<String> LOCATION_PROVIDER = Sensors.newStringSensor("location.entity.provider", "The provider name for the entity location");
+    AttributeSensor<Set<String>> LOCATION_COUNTRY_CODES = Sensors.newSensor(new TypeToken<Set<String>>() { },
+            "location.entity.countryCode", "The ISO 3166 country codes for the entity location");
+
     AttributeSensor<Entity> LOCATION_ENTITY = Sensors.newSensor(Entity.class,
             "location.entity", "The created entity");
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6615ddf7/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
index 95c9efa..fbfcdc5 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
@@ -100,6 +100,9 @@ public class LocationEntityImpl extends BasicStartableImpl implements LocationEn
                 String locationType = provisioner.getClass().getSimpleName();
                 String provider = provisioner.config().get(LocationConfigKeys.CLOUD_PROVIDER);
                 Set<String> countryCodes = MutableSet.copyOf(provisioner.config().get(LocationConfigKeys.ISO_3166));
+                sensors().set(LOCATION_TYPE, locationType);
+                sensors().set(LOCATION_PROVIDER, provider);
+                sensors().set(LOCATION_COUNTRY_CODES, countryCodes);
                 EntitySpec<?> spec = specMap.get(DEFAULT);
                 if (specMap.containsKey(locationType)) {
                     LOG.debug("Matched location type {} for entity: {}", locationType, this);


[3/4] brooklyn-server git commit: Adding new LocationEntity for creating location specific entities

Posted by gr...@apache.org.
Adding new LocationEntity for creating location specific entities


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

Branch: refs/heads/master
Commit: b119889b02545955fbdfe93f346a1b729afcee44
Parents: adfa95d
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Fri Nov 11 00:45:58 2016 +0000
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Tue Nov 22 13:52:15 2016 +0000

----------------------------------------------------------------------
 .../brooklyn/entity/stock/LocationEntity.java   |  80 ++++++++++
 .../entity/stock/LocationEntityImpl.java        | 158 +++++++++++++++++++
 2 files changed, 238 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b119889b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
new file mode 100644
index 0000000..4e8cfd1
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntity.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.entity.stock;
+
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.entity.ImplementedBy;
+import org.apache.brooklyn.api.location.MachineProvisioningLocation;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.util.core.flags.SetFromFlag;
+
+import com.google.common.annotations.Beta;
+import com.google.common.reflect.TypeToken;
+
+/**
+ * An entity that creates an optional child, based on the provisioning location properties.
+ * <p>
+ * Can use the class name of the {@link MachineProvisioningLocation} or the value of the
+ * {@code provider} or {@code iso3166} configuration keys on the provisioning location.
+ * <pre>
+ * - type: org.apache.brooklyn.entity.stock.LocationEntity
+ *   brooklyn.config:
+ *     location.entity.spec.default:
+ *       $brooklyn:entitySpec:
+ *         type: traefik-load-balancer
+ *     location.entity.spec.aws-ec2:
+ *       $brooklyn:entitySpec:
+ *         type: elastic-load-balancer
+ *     location.entity.spec.gce:
+ *       $brooklyn:entitySpec:
+ *         type: google-load-balancer
+ * </pre>
+ *
+ * @see {@link ConditionalEntity} for an explanation of sensor propagation configuration
+ */
+@Beta
+@ImplementedBy(LocationEntityImpl.class)
+public interface LocationEntity extends BasicStartable {
+
+    String DEFAULT = "default";
+
+    @SetFromFlag("entitySpec")
+    ConfigKey<Map<String,EntitySpec<?>>> LOCATION_ENTITY_SPEC_MAP = ConfigKeys.newConfigKey(new TypeToken<Map<String,EntitySpec<?>>>() { },
+            "location.entity.spec", "The mapping of location properties to the entity specification that will be created. "
+                    + "Use the key 'default' to specify an entity specification to use if no match is found.");
+
+    @SetFromFlag("propagateSensors")
+    ConfigKey<Boolean> PROPAGATE_LOCATION_ENTITY_SENSORS = ConfigKeys.newBooleanConfigKey(
+            "location.entity.propagate", "Whether sensors are to be propagated from the child entity", Boolean.TRUE);
+
+    @SetFromFlag("sensorsToPropagate")
+    ConfigKey<Collection<AttributeSensor<?>>> LOCATION_ENTITY_SENSOR_LIST = ConfigKeys.newConfigKey(new TypeToken<Collection<AttributeSensor<?>>>() { },
+            "location.entity.sensors", "Collection of sensors that are to be propagated from the child entity (all usual sensors if not set, or empty)");
+
+    AttributeSensor<Entity> LOCATION_ENTITY = Sensors.newSensor(Entity.class,
+            "location.entity", "The created entity");
+
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b119889b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
new file mode 100644
index 0000000..95c9efa
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.entity.stock;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.api.location.MachineLocation;
+import org.apache.brooklyn.api.location.MachineProvisioningLocation;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.core.entity.Attributes;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
+import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
+import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
+import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.ComputeServiceIndicatorsFromChildrenAndMembers;
+import org.apache.brooklyn.core.entity.trait.Startable;
+import org.apache.brooklyn.core.location.LocationConfigKeys;
+import org.apache.brooklyn.core.location.Locations;
+import org.apache.brooklyn.core.location.Machines;
+import org.apache.brooklyn.enricher.stock.Enrichers;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.collections.MutableSet;
+import org.apache.brooklyn.util.collections.QuorumCheck;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.time.Duration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LocationEntityImpl extends BasicStartableImpl implements LocationEntity {
+
+    private static final Logger LOG = LoggerFactory.getLogger(BasicStartableImpl.class);
+
+    @Override
+    public void start(Collection<? extends Location> locations) {
+        try {
+            ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
+            sensors().set(Attributes.SERVICE_UP, false);
+
+            // Block startup until other dependent components are available
+            Object val = config().get(START_LATCH);
+            if (val != null) {
+                LOG.debug("Finished waiting for start-latch in {}", this);
+            }
+
+            Entity child = sensors().get(LOCATION_ENTITY);
+            Map<String,EntitySpec<?>> specMap = MutableMap.copyOf(config().get(LOCATION_ENTITY_SPEC_MAP));
+
+            Boolean propagate = config().get(PROPAGATE_LOCATION_ENTITY_SENSORS);
+            Set<AttributeSensor<?>> sensors = MutableSet.copyOf(config().get(LOCATION_ENTITY_SENSOR_LIST));
+            Duration timeout = config().get(BrooklynConfigKeys.START_TIMEOUT);
+
+            addLocations(locations);
+            locations = Locations.getLocationsCheckingAncestors(locations, this);
+            LOG.info("Starting entity {}: {}", this, locations);
+
+            // Child not yet created and Entity spec is available
+            if (child == null && specMap.size() > 0) {
+                // Determine provisioning location
+                MachineProvisioningLocation<?> provisioner = null;
+                Maybe<? extends Location> location = Machines.findUniqueElement(locations, MachineLocation.class);
+                if (location.isPresent()) {
+                    Location parent = location.get().getParent();
+                    while (parent != null && ! (parent instanceof MachineProvisioningLocation)) {
+                        parent = parent.getParent();
+                    }
+                    provisioner = (MachineProvisioningLocation<?>) parent;
+                } else {
+                    location = Machines.findUniqueElement(locations, MachineProvisioningLocation.class);
+                    if (location.isPresent()) {
+                        provisioner = (MachineProvisioningLocation<?>) location.get();
+                    }
+                }
+                if (provisioner == null) {
+                    throw new IllegalStateException("Cannot determine provisioning location for entity: " + this);
+                }
+
+                // Determine which entity spec to use
+                String locationType = provisioner.getClass().getSimpleName();
+                String provider = provisioner.config().get(LocationConfigKeys.CLOUD_PROVIDER);
+                Set<String> countryCodes = MutableSet.copyOf(provisioner.config().get(LocationConfigKeys.ISO_3166));
+                EntitySpec<?> spec = specMap.get(DEFAULT);
+                if (specMap.containsKey(locationType)) {
+                    LOG.debug("Matched location type {} for entity: {}", locationType, this);
+                    spec = specMap.get(locationType);
+                } else if (specMap.containsKey(provider)) {
+                    LOG.debug("Matched provider {} for entity: {}", provider, this);
+                    spec = specMap.get(provider);
+                } else for (String country : countryCodes) {
+                    if (specMap.containsKey(country)) {
+                        LOG.debug("Matched country code {} for entity: {}", country, this);
+                        spec = specMap.get(country);
+                    }
+                }
+
+                // Instantiate spec as child entity if set
+                if (spec != null) {
+                    child = addChild(EntitySpec.create(spec));
+
+                    sensors().set(LOCATION_ENTITY, child);
+
+                    // Add enrichers for sensor propagation
+                    enrichers().add(Enrichers.builder().propagating(Startable.SERVICE_UP).from(child).build());
+                    enrichers().add(ServiceStateLogic.newEnricherFromChildrenState()
+                            .suppressDuplicates(true)
+                            .configure(ComputeServiceIndicatorsFromChildrenAndMembers.RUNNING_QUORUM_CHECK, QuorumCheck.QuorumChecks.all()));
+                    if (Boolean.TRUE.equals(propagate)) {
+                        if (sensors.isEmpty()) {
+                            enrichers().add(Enrichers.builder().propagatingAllButUsualAnd().from(child).build());
+                        } else {
+                            enrichers().add(Enrichers.builder().propagating(sensors).from(child).build());
+                        }
+                    }
+                }
+            }
+
+            // Start child if present; otherwise just set service.isUp
+            if (child != null) {
+                LOG.info("Starting child {}: {}", child, locations);
+                if (Entities.invokeEffectorWithArgs(this, child, Startable.START, locations).blockUntilEnded(timeout)) {
+                    LOG.debug("Successfully started {} by {}", child, this);
+                } else {
+                    throw new IllegalStateException(String.format("Timed out while %s was starting %s", this, child));
+                }
+            } else {
+                LOG.debug("No child created, setting SERVICE_UP to true");
+                sensors().set(Attributes.SERVICE_UP, true);
+            }
+
+            ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
+        } catch (Throwable t) {
+            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
+            throw Exceptions.propagate(t);
+        }
+    }
+}
+


[4/4] brooklyn-server git commit: This closes #428

Posted by gr...@apache.org.
This closes #428

* github/pr/428:
  Fix order of provisioner check and add tests for LocationEntity
  Save location information as sensor data on LocationEntity
  Adding new LocationEntity for creating location specific entities


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/7f1846c0
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/7f1846c0
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/7f1846c0

Branch: refs/heads/master
Commit: 7f1846c0f48f19cf586bdbf98ad8e35be54f3ca1
Parents: e997d9a 70baabb
Author: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Authored: Mon Dec 12 18:17:31 2016 +0000
Committer: Andrew Donald Kennedy <an...@cloudsoftcorp.com>
Committed: Mon Dec 12 18:17:31 2016 +0000

----------------------------------------------------------------------
 .../brooklyn/entity/stock/LocationEntity.java   |  90 +++++++++
 .../entity/stock/LocationEntityImpl.java        | 161 ++++++++++++++++
 .../entity/stock/LocationEntityTest.java        | 182 +++++++++++++++++++
 3 files changed, 433 insertions(+)
----------------------------------------------------------------------