@Override protected T customReadBody(String type, Map<Object, Object> values, JsonParser jp, DeserializationContext ctxt) throws IOException { Optional<String> entityId = Optional.fromNullable((String) values.get("entityId")); Optional<Entity> entity = Optional.fromNullable(entityId.isPresent() ? null: mgmt.getEntityManager().getEntity(entityId.get())); String id = (String) values.get("id"); return getInstanceFromId(entity, id); } protected Optional<String> getEntityId(T value) {
protected void createApps() { for (EntitySpec<? extends Application> spec : appSpecsToManage) { Application app = managementContext.getEntityManager().createEntity(spec); apps.add(app); } for (String blueprint : yamlAppsToManage) { Application app = EntityManagementUtils.createUnstarted(managementContext, blueprint); // Note: BrooklynAssemblyTemplateInstantiator automatically puts applications under management. apps.add(app); } }
@Override public Collection<Entity> getEntities() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntities(); } else { return Collections.emptyList(); } }
@Override public Iterable<String> getEntityIds() { if (isInitialManagementContextReal()) { return ((EntityManagerInternal)initialManagementContext.getEntityManager()).getEntityIds(); } else { return Collections.emptyList(); } }
@Override public <T extends Policy> T createPolicy(PolicySpec<T> spec) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().createPolicy(spec); } else { throw new IllegalStateException("Non-deployment context "+this+" (with no initial management context supplied) is not valid for this operation."); } }
@Override public EntityTypeRegistry getEntityTypeRegistry() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntityTypeRegistry(); } else { throw new IllegalStateException("Non-deployment context "+this+" (with no initial management context supplied) is not valid for this operation."); } }
@Override public <T extends Entity> T createEntity(EntitySpec<T> spec) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().createEntity(spec); } else { throw new IllegalStateException("Non-deployment context "+this+" (with no initial management context supplied) is not valid for this operation."); } }
@Override public Collection<Entity> findEntities(Predicate<? super Entity> filter) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().findEntities(filter); } else { return Collections.emptyList(); } }
@Override public ManagementTransitionMode getLastManagementTransitionMode(String itemId) { if (isInitialManagementContextReal()) { return ((EntityManagerInternal)initialManagementContext.getEntityManager()).getLastManagementTransitionMode(itemId); } else { return null; } }
@Test public void testLoadEntityFromClasspath() throws Exception { String entityName = ExampleEntity.class.getName(); Object appSpec = loadApplicationFromClasspathOrParse(entityName); assertTrue(appSpec instanceof EntitySpec, "app="+appSpec); mgmt = LocalManagementContextForTests.newInstance(); app = (Application) mgmt.getEntityManager().createEntity((EntitySpec<?>)appSpec); Collection<Entity> entities = app.getChildren(); assertEquals(entities.size(), 1, "entities="+entities); assertTrue(Iterables.getOnlyElement(entities) instanceof ExampleEntity, "entities="+entities+"; ifs="+Iterables.getOnlyElement(entities).getClass().getInterfaces()); assertTrue(Iterables.getOnlyElement(entities) instanceof EntityProxy, "entities="+entities); }
@Override public Entity call() { EntityInternal entity = entity(); Collection<Entity> entitiesToSearch = entity.getManagementContext().getEntityManager().getEntities(); Optional<Entity> result = Iterables.tryFind(entitiesToSearch, EntityPredicates.configEqualTo(TAG, tag)); if (result.isPresent()) { return result.get(); } else { throw new NoSuchElementException("No entity matching id " + tag+" in "+entitiesToSearch); } }}) .build();
@Override public InetAddress getNextAgentAddress(String agentId) { Entity agent = getManagementContext().getEntityManager().getEntity(agentId); String address = agent.sensors().get(CalicoNode.DOCKER_HOST).sensors().get(Attributes.SUBNET_ADDRESS); try { return InetAddress.getByName(address); } catch (UnknownHostException uhe) { throw Exceptions.propagate(uhe); } }
@SuppressWarnings("unchecked") protected <T extends Entity> T rebind(T entity) throws Exception { rebind(); Entity result = mgmt().getEntityManager().getEntity(entity.getId()); assertNotNull(result, "no entity found after rebind with id " + entity.getId()); return (T) result; }
@Override public Void apply(ManagementContext mgmt) { TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); SshMachineLocation loc = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); SshMachineLocation loc2 = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); app.addLocations(ImmutableList.of(loc)); appToKeep.set(app); locToKeep.set(loc); locToDelete.set(loc2); return null; }});
private TestEntity createAndManageChildOf(Entity parent) { EntityManager entityManager = app.getManagementContext().getEntityManager(); TestEntity result = entityManager.createEntity(EntitySpec.create(TestEntity.class).parent(parent)); return result; }
@Test public void testRebindToMachineEntity() throws Exception { MachineEntity entity = origApp.createAndManageChild(EntitySpec.create(MachineEntity.class) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)); origApp.start(ImmutableList.of(origMachine)); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); rebind(); Entity newEntity = mgmt().getEntityManager().getEntity(entity.getId()); EntityAsserts.assertAttributeEqualsEventually(newEntity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); }
@Override public void setEntity(EntityLocal entity) { assert entity instanceof Group; super.setEntity(entity); subscriptions().subscribe(null, Startable.SERVICE_UP, handler); for (Entity e : ((EntityInternal) entity).getManagementContext().getEntityManager().getEntities()) { if (Objects.equal(e.getApplicationId(), entity.getApplicationId())) updateMembership(e); } }
protected Entity createAndStartApplication(String input, Map<String,?> startParameters) throws Exception { EntitySpec<?> spec = mgmt().getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, input, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class); final Entity app = mgmt().getEntityManager().createEntity(spec); getLogger().info("Test created app, and will now start " + app); // start the app (happens automatically if we use camp to instantiate, but not if we use crate spec approach) app.invoke(Startable.START, startParameters).get(); return app; }
@Test(groups={"Integration", "WIP", "Broken"}) public void testJavaWebWithMemberSpecRebind() throws Exception { Reader input = Streams.reader(new ResourceUtils(this).getResourceFromUrl("test-java-web-app-spec-and-db-with-function.yaml")); AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input); Assembly assembly = at.getInstantiator().newInstance().instantiate(at, platform); final Application app = (Application) mgmt().getEntityManager().getEntity(assembly.getId()); Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), app); for (Task<?> t: tasks) t.blockUntilEnded(); Entities.dumpInfo(app); Application app2 = rebind(app); Assert.assertEquals(app2.getChildren().size(), 2); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { mgmt = LocalManagementContextForTests.builder(false).build(); app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) .child(EntitySpec.create(TestEntity.class))); entity = (TestEntity) Iterables.getOnlyElement(app.getChildren()); useServerForTest(baseLauncher() .managementContext(mgmt) .forceUseOfDefaultCatalogWithJavaClassPath(true) .start()); }