Repository: brooklyn-server Updated Branches: refs/heads/master fc575d77b -> 36b9045b2
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java index 00051ba..4259120 100644 --- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java +++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java @@ -38,16 +38,20 @@ import org.apache.brooklyn.api.location.LocationSpec; import org.apache.brooklyn.api.objs.SpecParameter; import org.apache.brooklyn.api.policy.Policy; import org.apache.brooklyn.api.policy.PolicySpec; +import org.apache.brooklyn.api.sensor.Enricher; +import org.apache.brooklyn.api.sensor.EnricherSpec; import org.apache.brooklyn.api.sensor.Sensor; import org.apache.brooklyn.core.entity.EntityDynamicType; import org.apache.brooklyn.core.mgmt.BrooklynTags; import org.apache.brooklyn.core.objs.BrooklynTypes; import org.apache.brooklyn.rest.api.CatalogApi; +import org.apache.brooklyn.rest.domain.CatalogEnricherSummary; import org.apache.brooklyn.rest.domain.CatalogEntitySummary; import org.apache.brooklyn.rest.domain.CatalogItemSummary; import org.apache.brooklyn.rest.domain.CatalogLocationSummary; import org.apache.brooklyn.rest.domain.CatalogPolicySummary; import org.apache.brooklyn.rest.domain.EffectorSummary; +import org.apache.brooklyn.rest.domain.EnricherConfigSummary; import org.apache.brooklyn.rest.domain.EntityConfigSummary; import org.apache.brooklyn.rest.domain.LocationConfigSummary; import org.apache.brooklyn.rest.domain.PolicyConfigSummary; @@ -67,7 +71,7 @@ import com.google.common.collect.Sets; public class CatalogTransformer { private static final org.slf4j.Logger log = LoggerFactory.getLogger(CatalogTransformer.class); - + public static <T extends Entity> CatalogEntitySummary catalogEntitySummary(BrooklynRestResourceUtils b, CatalogItem<T,EntitySpec<? extends T>> item, UriBuilder ub) { Set<EntityConfigSummary> config = Sets.newLinkedHashSet(); Set<SensorSummary> sensors = Sets.newTreeSet(SummaryComparators.nameComparator()); @@ -116,6 +120,8 @@ public class CatalogTransformer { return catalogEntitySummary(b, item, ub); case POLICY: return catalogPolicySummary(b, item, ub); + case ENRICHER: + return catalogEnricherSummary(b, item, ub); case LOCATION: return catalogLocationSummary(b, item, ub); default: @@ -147,6 +153,23 @@ public class CatalogTransformer { item.tags().getTags(), item.isDeprecated(), makeLinks(item, ub)); } + public static CatalogEnricherSummary catalogEnricherSummary(BrooklynRestResourceUtils b, CatalogItem<? extends Enricher,EnricherSpec<?>> item, UriBuilder ub) { + final Set<EnricherConfigSummary> config = Sets.newLinkedHashSet(); + try{ + final EnricherSpec<?> spec = (EnricherSpec<?>) b.getCatalog().peekSpec(item); + for (final SpecParameter<?> input : spec.getParameters()){ + config.add(EntityTransformer.enricherConfigSummary(input)); + } + }catch (Exception e) { + Exceptions.propagateIfFatal(e); + log.trace("Unable to create policy spec for "+item+": "+e, e); + } + return new CatalogEnricherSummary(item.getSymbolicName(), item.getVersion(), item.getDisplayName(), + item.getJavaType(), item.getCatalogItemType().toString(), item.getPlanYaml(), + item.getDescription(), tidyIconLink(b, item, item.getIconUrl(), ub), config, + item.tags().getTags(), item.isDeprecated(), makeLinks(item, ub)); + } + public static CatalogLocationSummary catalogLocationSummary(BrooklynRestResourceUtils b, CatalogItem<? extends Location,LocationSpec<?>> item, UriBuilder ub) { Set<LocationConfigSummary> config = ImmutableSet.of(); return new CatalogLocationSummary(item.getSymbolicName(), item.getVersion(), item.getDisplayName(), @@ -168,6 +191,8 @@ public class CatalogTransformer { return serviceUriBuilder(ub, CatalogApi.class, "getEntity").build(itemId, item.getVersion()); case POLICY: return serviceUriBuilder(ub, CatalogApi.class, "getPolicy").build(itemId, item.getVersion()); + case ENRICHER: + return serviceUriBuilder(ub, CatalogApi.class, "getEnricher").build(itemId, item.getVersion()); case LOCATION: return serviceUriBuilder(ub, CatalogApi.class, "getLocation").build(itemId, item.getVersion()); default: http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/EntityTransformer.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/EntityTransformer.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/EntityTransformer.java index 78aa6f7..fcbe671 100644 --- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/EntityTransformer.java +++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/EntityTransformer.java @@ -32,6 +32,7 @@ import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.objs.SpecParameter; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.render.RendererHints; +import org.apache.brooklyn.rest.domain.EnricherConfigSummary; import org.apache.brooklyn.rest.domain.EntityConfigSummary; import org.apache.brooklyn.rest.domain.EntitySummary; import org.apache.brooklyn.rest.domain.PolicyConfigSummary; @@ -129,6 +130,10 @@ public class EntityTransformer { return new PolicyConfigSummary(config, label, priority, links); } + public static EnricherConfigSummary enricherConfigSummary(ConfigKey<?> config, String label, Double priority, Map<String, URI> links) { + return new EnricherConfigSummary(config, label, priority, links); + } + /** generates a representation for a given config key, * with label inferred from annoation in the entity class, * and links pointing to the entity and the applicaiton */ @@ -194,4 +199,9 @@ public class EntityTransformer { Double priority = input.isPinned() ? Double.valueOf(1d) : null; return policyConfigSummary(input.getConfigKey(), input.getLabel(), priority, null); } + + public static EnricherConfigSummary enricherConfigSummary(SpecParameter<?> input) { + Double priority = input.isPinned() ? Double.valueOf(1d) : null; + return enricherConfigSummary(input.getConfigKey(), input.getLabel(), priority, null); + } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourcePerformanceTest.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourcePerformanceTest.java b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourcePerformanceTest.java index 6bea38d..c72d190 100644 --- a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourcePerformanceTest.java +++ b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourcePerformanceTest.java @@ -28,7 +28,9 @@ import javax.ws.rs.core.GenericType; import org.apache.brooklyn.core.test.entity.TestEntity; import org.apache.brooklyn.core.test.qa.performance.AbstractPerformanceTest; +import org.apache.brooklyn.enricher.stock.Aggregator; import org.apache.brooklyn.policy.autoscaling.AutoScalerPolicy; +import org.apache.brooklyn.rest.domain.CatalogEnricherSummary; import org.apache.brooklyn.rest.domain.CatalogEntitySummary; import org.apache.brooklyn.rest.domain.CatalogItemSummary; import org.apache.brooklyn.rest.domain.CatalogLocationSummary; @@ -96,7 +98,13 @@ public class CatalogResourcePerformanceTest extends BrooklynRestResourcePerforma " name: My Catalog Policy " + i, " description: My description", " item:", - " type: " + AutoScalerPolicy.class.getName()); + " type: " + AutoScalerPolicy.class.getName(), + " - id: myenricher-" + i, + " itemType: enricher", + " name: My Catalog Enricher " + i, + " description: My description", + " item:", + " type: " + Aggregator.class.getName()); getManagementContext().getCatalog().addItems(yaml, false); } } @@ -115,6 +123,11 @@ public class CatalogResourcePerformanceTest extends BrooklynRestResourcePerforma public void testListAllPolicies() { runListAllPerformanceTest("testListAllPolicicies", "/catalog/policies", new GenericType<List<CatalogPolicySummary>>() {}); } + + @Test(groups={"Integration"}) + public void testListAllEnrichers() { + runListAllPerformanceTest("testListAllEnrichers", "/catalog/enrichers", new GenericType<List<CatalogEnricherSummary>>() {}); + } @Test(groups={"Integration"}) public void testListAllLocations() { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java ---------------------------------------------------------------------- diff --git a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java index 5cfcf4c..e3ad896 100644 --- a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java +++ b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java @@ -53,7 +53,9 @@ import org.apache.brooklyn.core.catalog.internal.CatalogUtils; import org.apache.brooklyn.core.entity.EntityPredicates; import org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest; import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.enricher.stock.Aggregator; import org.apache.brooklyn.policy.autoscaling.AutoScalerPolicy; +import org.apache.brooklyn.rest.domain.CatalogEnricherSummary; import org.apache.brooklyn.rest.domain.CatalogEntitySummary; import org.apache.brooklyn.rest.domain.CatalogItemSummary; import org.apache.brooklyn.rest.domain.CatalogLocationSummary; @@ -354,6 +356,103 @@ public class CatalogResourceTest extends BrooklynRestResourceTest { } @Test + public void testListEnrichers() { + Set<CatalogEnricherSummary> enrichers = client().path("/catalog/enrichers") + .get(new GenericType<Set<CatalogEnricherSummary>>() {}); + + assertTrue(enrichers.size() > 0); + CatalogEnricherSummary asp = null; + for (CatalogEnricherSummary p : enrichers) { + if (Aggregator.class.getName().equals(p.getType())) + asp = p; + } + Assert.assertNotNull(asp, "didn't find Aggregator"); + } + + @Test + public void testEnricherAddGetAndRemove() { + String symbolicName = "my.catalog.enricher.id"; + String enricherType = "org.apache.brooklyn.enricher.stock.Aggregator"; + String yaml = Joiner.on("\n").join( + "brooklyn.catalog:", + " id: " + symbolicName, + " version: " + TEST_VERSION, + " itemType: enricher", + " name: My Catalog Enricher", + " description: My description", + " item:", + " type: " + enricherType); + + // Create location item + Map<String, CatalogEnricherSummary> items = client().path("/catalog") + .post(yaml, new GenericType<Map<String, CatalogEnricherSummary>>() {}); + CatalogEnricherSummary enricherItem = Iterables.getOnlyElement(items.values()); + + Assert.assertNotNull(enricherItem.getPlanYaml()); + Assert.assertTrue(enricherItem.getPlanYaml().contains(enricherType)); + assertEquals(enricherItem.getId(), ver(symbolicName)); + assertEquals(enricherItem.getSymbolicName(), symbolicName); + assertEquals(enricherItem.getVersion(), TEST_VERSION); + + // Retrieve location item + CatalogEnricherSummary enricher = client().path("/catalog/enrichers/"+symbolicName+"/"+TEST_VERSION) + .get(CatalogEnricherSummary.class); + assertEquals(enricher.getSymbolicName(), symbolicName); + + // Retrieve all locations + Set<CatalogEnricherSummary> enrichers = client().path("/catalog/enrichers") + .get(new GenericType<Set<CatalogEnricherSummary>>() {}); + boolean found = false; + for (CatalogEnricherSummary contender : enrichers) { + if (contender.getSymbolicName().equals(symbolicName)) { + found = true; + break; + } + } + Assert.assertTrue(found, "contenders="+enrichers); + + // Delete + Response deleteResponse = client().path("/catalog/enrichers/"+symbolicName+"/"+TEST_VERSION) + .delete(); + assertEquals(deleteResponse.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); + + Response getPostDeleteResponse = client().path("/catalog/enrichers/"+symbolicName+"/"+TEST_VERSION) + .get(); + assertEquals(getPostDeleteResponse.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); + } + + @Test + // osgi may fail in IDE, typically works on mvn CLI though + public void testRegisterOsgiEnricherTopLevelSyntax() { + TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); + + String symbolicName = "my.catalog.enricher.id"; + String enricherType = OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENRICHER; + String bundleUrl = OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL; + + String yaml = Joiner.on("\n").join( + "brooklyn.catalog:", + " id: " + symbolicName, + " version: " + TEST_VERSION, + " itemType: enricher", + " name: My Catalog Enricher", + " description: My description", + " libraries:", + " - url: " + bundleUrl, + " item:", + " type: " + enricherType); + + CatalogEnricherSummary entityItem = Iterables.getOnlyElement( client().path("/catalog") + .post(yaml, new GenericType<Map<String,CatalogEnricherSummary>>() {}).values() ); + + Assert.assertNotNull(entityItem.getPlanYaml()); + Assert.assertTrue(entityItem.getPlanYaml().contains(enricherType)); + assertEquals(entityItem.getId(), ver(symbolicName)); + assertEquals(entityItem.getSymbolicName(), symbolicName); + assertEquals(entityItem.getVersion(), TEST_VERSION); + } + + @Test public void testDeleteCustomEntityFromCatalog() { String symbolicName = "my.catalog.app.id.to.subsequently.delete"; String yaml = Joiner.on("\n").join( http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/utils/common/dependencies/osgi/entities/src/main/java/org/apache/brooklyn/test/osgi/entities/SimpleEnricher.java ---------------------------------------------------------------------- diff --git a/utils/common/dependencies/osgi/entities/src/main/java/org/apache/brooklyn/test/osgi/entities/SimpleEnricher.java b/utils/common/dependencies/osgi/entities/src/main/java/org/apache/brooklyn/test/osgi/entities/SimpleEnricher.java new file mode 100644 index 0000000..3e70821 --- /dev/null +++ b/utils/common/dependencies/osgi/entities/src/main/java/org/apache/brooklyn/test/osgi/entities/SimpleEnricher.java @@ -0,0 +1,42 @@ +/* + * 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.test.osgi.entities; + +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.enricher.AbstractEnricher; +import org.apache.brooklyn.util.core.flags.SetFromFlag; + +class SimpleEnricher extends AbstractEnricher { + @SetFromFlag("config1") + public static final ConfigKey<String> CONFIG1 = ConfigKeys.newStringConfigKey("config1"); + + @SetFromFlag("config2") + public static final ConfigKey<String> CONFIG2 = ConfigKeys.newStringConfigKey("config2"); + + @SetFromFlag("config3") + public static final ConfigKey<String> CONFIG3 = ConfigKeys.newStringConfigKey("config3"); + + public SimpleEnricher() {} + + @Override + protected <T> void doReconfigureConfig(ConfigKey<T> key, T val) { + // no-op; allow any config to be set + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java b/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java index 9cced41..f9f1998 100644 --- a/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java +++ b/utils/common/src/test/java/org/apache/brooklyn/util/osgi/OsgiTestResources.java @@ -49,6 +49,7 @@ public interface OsgiTestResources { public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_APPLICATION = "org.apache.brooklyn.test.osgi.entities.SimpleApplication"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY = "org.apache.brooklyn.test.osgi.entities.SimpleEntity"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY = "org.apache.brooklyn.test.osgi.entities.SimplePolicy"; + public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENRICHER = "org.apache.brooklyn.test.osgi.entities.SimpleEnricher"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_EFFECTOR = "org.apache.brooklyn.test.osgi.entities.SimpleEffectorInitializer"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_OBJECT = "org.apache.brooklyn.test.osgi.entities.SimpleObject"; public static final String BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY_CONFIG_NAME = "simple.config"; http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/02305e71/utils/common/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-entities.jar ---------------------------------------------------------------------- diff --git a/utils/common/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-entities.jar b/utils/common/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-entities.jar index 60fece6..4f00f80 100644 Binary files a/utils/common/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-entities.jar and b/utils/common/src/test/resources/brooklyn/osgi/brooklyn-test-osgi-entities.jar differ
