@Override protected Policy getInstanceFromId(Optional<Entity> entity, String id) { if (id == null || !entity.isPresent()) return null; for (Policy policy : entity.get().policies()) { if (id.equals(policy.getId())) { return policy; } } return null; } }
/** 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.policies()) { if (policy.equals(p.getId())) return p; } for (Policy p: entity.policies()) { if (policy.equals(p.getDisplayName())) return p; } throw WebResourceUtils.notFound("Cannot find policy '%s' in entity '%s'", policy, entity); }
@Override public Map<String, Boolean> batchConfigRead( String application, String entityToken) { // TODO: add test Entity entity = brooklyn().getEntity(application, entityToken); Map<String, Boolean> result = Maps.newLinkedHashMap(); for (Policy p : entity.policies()) { result.put(p.getId(), !p.isSuspended()); } return result; }
@Override public List<PolicySummary> list( final String application, final String entityToken ) { final Entity entity = brooklyn().getEntity(application, entityToken); return FluentIterable.from(entity.policies()) .transform(new Function<Policy, PolicySummary>() { @Override public PolicySummary apply(Policy policy) { return PolicyTransformer.policySummary(entity, policy, ui.getBaseUriBuilder()); } }) .toSortedList(SummaryComparators.nameComparator()); }
@Override public Integer get() { return child.policies().size(); } }, Predicates.<Integer> equalTo(1));
@Override public Response destroy(String application, String entityToken, String policyToken) { Entity entity = brooklyn().getEntity(application, entityToken); Policy policy = brooklyn().getPolicy(entity, policyToken); policy.suspend(); entity.policies().remove(policy); return Response.status(Response.Status.NO_CONTENT).build(); } }
@Override public synchronized void onUnmanaged(BrooklynObject instance) { if (LOG.isTraceEnabled()) LOG.trace("onUnmanaged: {}", instance); if (!isStopped()) { removeFromCollector(instance); if (instance instanceof Entity) { Entity entity = (Entity) instance; for (BrooklynObject adjunct : entity.policies()) removeFromCollector(adjunct); for (BrooklynObject adjunct : entity.enrichers()) removeFromCollector(adjunct); for (BrooklynObject adjunct : ((EntityInternal)entity).feeds().getFeeds()) removeFromCollector(adjunct); } } }
@Test public void testWithAppPolicy() throws Exception { Entity app = createAndStartApplication(loadYaml("test-app-with-policy.yaml")); waitForApplicationTasks(app); Assert.assertEquals(app.getDisplayName(), "test-app-with-policy"); log.info("App started:"); Entities.dumpInfo(app); Assert.assertEquals(app.policies().size(), 1); Policy policy = app.policies().iterator().next(); Assert.assertTrue(policy instanceof TestPolicy); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_NAME), "Name from YAML"); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_FROM_FUNCTION), "$brooklyn: is a fun place"); Map<?, ?> leftoverProperties = ((TestPolicy) policy).getLeftoverProperties(); Assert.assertEquals(leftoverProperties.get("policyLiteralValue1"), "Hello"); Assert.assertEquals(leftoverProperties.get("policyLiteralValue2"), "World"); Assert.assertEquals(leftoverProperties.size(), 2); }
@Test public void testMoreEntityV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("more-entity-v2-osgi-catalog.yaml")); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); OsgiVersionMoreEntityTest.assertV2EffectorCall(moreEntity); OsgiVersionMoreEntityTest.assertV2MethodCall(moreEntity); Assert.assertEquals(moreEntity.policies().size(), 1, "wrong policies: "+moreEntity.policies()); Policy policy = Iterables.getOnlyElement(moreEntity.policies()); // it was loaded from the java so should have the base more-entity catalog id Assert.assertEquals(policy.getCatalogItemId(), "more-entity:1.0"); }
@Test public void testWithEntityPolicy() throws Exception { Entity app = createAndStartApplication(loadYaml("test-entity-with-policy.yaml")); waitForApplicationTasks(app); Assert.assertEquals(app.getDisplayName(), "test-entity-with-policy"); log.info("App started:"); Entities.dumpInfo(app); Assert.assertEquals(app.policies().size(), 0); Assert.assertEquals(app.getChildren().size(), 1); Entity child = app.getChildren().iterator().next(); Assert.assertEquals(child.policies().size(), 1); Policy policy = child.policies().iterator().next(); Assert.assertNotNull(policy); Assert.assertTrue(policy instanceof TestPolicy, "policy=" + policy + "; type=" + policy.getClass()); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_NAME), "Name from YAML"); Assert.assertEquals(policy.getConfig(TestPolicy.CONF_FROM_FUNCTION), "$brooklyn: is a fun place"); Assert.assertEquals(((TestPolicy) policy).getLeftoverProperties(), ImmutableMap.of("policyLiteralValue1", "Hello", "policyLiteralValue2", "World")); Assert.assertEquals(policy.getConfig(TestPolicy.TEST_ATTRIBUTE_SENSOR), TestEntity.NAME); }
/** TODO we get warnings from {@link BrooklynEntityMatcher#extractValidConfigFlagsOrKeys}; * if we passed the correct loader at that point we could avoid those warnings. */ @Test public void testMoreEntityV1WithPolicy() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("simple-policy-osgi-catalog.yaml")); addCatalogItems(getLocalResource("more-entity-v1-with-policy-osgi-catalog.yaml")); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); Assert.assertEquals(moreEntity.policies().size(), 1, "wrong policies: "+moreEntity.policies()); Policy policy = Iterables.getOnlyElement(moreEntity.policies()); // it was loaded by yaml w ref to catalog, so should have the simple-policy catalog-id Assert.assertEquals(policy.getCatalogItemId(), "simple-policy:1.0"); }
@Test public void testMoreEntitiesV1Policy() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH); RegisteredType c2 = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V1_URL); // test load and instantiate Entity me = addItemFromCatalog(c2); RegisteredType cp = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY, TEST_VERSION, BROOKLYN_TEST_OSGI_ENTITIES_URL); me.policies().add(getPolicySpec(cp)); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); String catalogItemId = Iterables.getOnlyElement( me.policies() ).getCatalogItemId(); Assert.assertNotNull(catalogItemId); // must be the actual source bundle Assert.assertFalse(catalogItemId.equals(me.getCatalogItemId()), "catalog item id is: "+catalogItemId); Assert.assertTrue(catalogItemId.equals(CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY, TEST_VERSION)), "catalog item id is: "+catalogItemId); }
@Test public void testMoreEntitiesV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); RegisteredType c2 = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V2_URL, BROOKLYN_TEST_OSGI_ENTITIES_URL); // test load and instantiate Entity me = addItemFromCatalog(c2); Assert.assertEquals(me.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION)); assertV2MethodCall(me); assertV2EffectorCall(me); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); String catalogItemId = Iterables.getOnlyElement( me.policies() ).getCatalogItemId(); Assert.assertNotNull(catalogItemId); // allow either me's bundle (more) or the actual source bundle Assert.assertTrue(catalogItemId.equals(me.getCatalogItemId()) || catalogItemId.startsWith("brooklyn-test-osgi-entities")); }
@Test public void testMoreEntitiesV1ThenV2GivesV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V1_URL); addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V2_URL, BROOKLYN_TEST_OSGI_ENTITIES_URL); // test load and instantiate Entity me = addItemFromCatalog( mgmt.getTypeRegistry().get(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION) ); assertV2MethodCall(me); assertV2EffectorCall(me); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); }
@Test public void testLaunchApplicationReferencingPolicyTopLevelSyntax() throws Exception { String symbolicName = "my.catalog.policy.id.launch"; addCatalogPolicyLegacySyntax(symbolicName, POLICY_TYPE); Entity app = createAndStartApplication( "services: ", " - type: " + BasicEntity.class.getName(), " brooklyn.policies:", " - type: " + ver(symbolicName), " brooklyn.config:", " config2: config2 override", " config3: config3"); Entity simpleEntity = Iterables.getOnlyElement(app.getChildren()); Policy policy = Iterables.getOnlyElement(simpleEntity.policies()); assertEquals(policy.getPolicyType().getName(), POLICY_TYPE); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config1")), "config1"); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config2")), "config2 override"); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config3")), "config3"); deleteCatalogEntity(symbolicName); }
@Test public void testLaunchApplicationReferencingPolicy() throws Exception { String symbolicName = "my.catalog.policy.id.launch"; addCatalogPolicy(symbolicName, POLICY_TYPE); Entity app = createAndStartApplication( "services: ", " - type: " + BasicEntity.class.getName(), " brooklyn.policies:", " - type: " + ver(symbolicName), " brooklyn.config:", " config2: config2 override", " config3: config3"); Entity simpleEntity = Iterables.getOnlyElement(app.getChildren()); Policy policy = Iterables.getOnlyElement(simpleEntity.policies()); assertEquals(policy.getPolicyType().getName(), POLICY_TYPE); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config1")), "config1"); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config2")), "config2 override"); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config3")), "config3"); deleteCatalogEntity(symbolicName); }
@Test public void testLaunchApplicationReferencingPolicy() throws Exception { String symbolicName = "my.catalog.policy.id.launch"; addCatalogOsgiPolicy(symbolicName, SIMPLE_POLICY_TYPE); Entity app = createAndStartApplication( "name: simple-app-yaml", "location: localhost", "services: ", " - type: " + BasicEntity.class.getName(), " brooklyn.policies:\n" + " - type: " + ver(symbolicName), " brooklyn.config:", " config2: config2 override", " config3: config3"); Entity simpleEntity = Iterables.getOnlyElement(app.getChildren()); Policy policy = Iterables.getOnlyElement(simpleEntity.policies()); assertEquals(policy.getPolicyType().getName(), SIMPLE_POLICY_TYPE); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config1")), "config1"); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config2")), "config2 override"); assertEquals(policy.getConfig(new BasicConfigKey<String>(String.class, "config3")), "config3"); deleteCatalogEntity(symbolicName); }
@SuppressWarnings("unchecked") @Override public PolicySummary addPolicy( String application,String entityToken, String policyTypeName, Map<String, String> config) { Entity entity = brooklyn().getEntity(application, entityToken); Class<? extends Policy> policyType; try { policyType = (Class<? extends Policy>) new ClassLoaderUtils(this, mgmt()).loadClass(policyTypeName); } catch (ClassNotFoundException e) { throw WebResourceUtils.badRequest("No policy with type %s found", policyTypeName); } catch (ClassCastException e) { throw WebResourceUtils.badRequest("No policy with type %s found", policyTypeName); } catch (Exception e) { throw Exceptions.propagate(e); } Policy policy = entity.policies().add(PolicySpec.create(policyType).configure(config)); log.debug("REST API added policy " + policy + " to " + entity); return PolicyTransformer.policySummary(entity, policy, ui.getBaseUriBuilder()); }
@Test public void testDeletesOrphanedPolicies() throws Exception { Entity entity = origApp.addChild(EntitySpec.create(TestEntity.class).impl(MyEntity.class)); MyPolicy policy = entity.policies().add(PolicySpec.create(MyPolicy.class)); MementoTweaker tweaker = new MementoTweaker(new Deletions().entities(entity.getId())); assertTransformDeletes(new Deletions().policies(policy.getId()), tweaker); }