public static void aggregateOpenGammaServerSensors(Entity cluster) {

    List<? extends List<? extends AttributeSensor<? extends Number>>> summingEnricherSetup =
        ImmutableList.of(
            ImmutableList.of(PROCESSING_TIME_PER_SECOND_LAST, PROCESSING_TIME_PER_SECOND_LAST),
            ImmutableList.of(
                PROCESSING_TIME_PER_SECOND_IN_WINDOW, PROCESSING_TIME_PER_SECOND_IN_WINDOW),
            ImmutableList.of(VIEW_PROCESSES_COUNT, VIEW_PROCESSES_COUNT),
            ImmutableList.of(
                PROCESS_CPU_TIME_FRACTION_IN_WINDOW, PROCESS_CPU_TIME_FRACTION_IN_WINDOW));

    for (List<? extends AttributeSensor<? extends Number>> es : summingEnricherSetup) {
      AttributeSensor<? extends Number> t = es.get(0);
      AttributeSensor<? extends Number> total = es.get(1);
      CustomAggregatingEnricher<?, ?> totaller =
          CustomAggregatingEnricher.newSummingEnricher(
              MutableMap.of("allMembers", true), t, total, null, null);
      cluster.addEnricher(totaller);
    }

    cluster.addEnricher(
        Enrichers.builder()
            .aggregating(PROCESSING_TIME_PER_SECOND_LAST)
            .fromMembers()
            .publishing(PROCESSING_TIME_PER_SECOND_LAST_PER_NODE)
            .computingAverage()
            .defaultValueForUnreportedSensors(null)
            .build());
    cluster.addEnricher(
        Enrichers.builder()
            .aggregating(PROCESSING_TIME_PER_SECOND_IN_WINDOW)
            .fromMembers()
            .publishing(PROCESSING_TIME_PER_SECOND_IN_WINDOW_PER_NODE)
            .computingAverage()
            .defaultValueForUnreportedSensors(null)
            .build());
    cluster.addEnricher(
        Enrichers.builder()
            .aggregating(PROCESS_CPU_TIME_FRACTION_IN_WINDOW)
            .fromMembers()
            .publishing(PROCESS_CPU_TIME_FRACTION_IN_WINDOW_PER_NODE)
            .computingAverage()
            .defaultValueForUnreportedSensors(null)
            .build());
    cluster.addEnricher(
        Enrichers.builder()
            .aggregating(VIEW_PROCESSES_COUNT)
            .fromMembers()
            .publishing(VIEW_PROCESSES_COUNT_PER_NODE)
            .computingAverage()
            .defaultValueForUnreportedSensors(0)
            .build());
  }
  @Override
  public void init() {
    super.init();
    // Enricher attribute setup.  A way of automatically discovering these (but avoiding
    // averaging things like HTTP port and response codes) would be neat.
    List<? extends List<? extends AttributeSensor<? extends Number>>> summingEnricherSetup =
        ImmutableList.of(
            ImmutableList.of(REQUEST_COUNT, REQUEST_COUNT),
            ImmutableList.of(ERROR_COUNT, ERROR_COUNT),
            ImmutableList.of(REQUESTS_PER_SECOND_LAST, REQUESTS_PER_SECOND_LAST),
            ImmutableList.of(REQUESTS_PER_SECOND_IN_WINDOW, REQUESTS_PER_SECOND_IN_WINDOW),
            ImmutableList.of(TOTAL_PROCESSING_TIME, TOTAL_PROCESSING_TIME),
            ImmutableList.of(
                PROCESSING_TIME_FRACTION_IN_WINDOW, PROCESSING_TIME_FRACTION_IN_WINDOW));

    List<? extends List<? extends AttributeSensor<? extends Number>>> averagingEnricherSetup =
        ImmutableList.of(
            ImmutableList.of(REQUEST_COUNT, REQUEST_COUNT_PER_NODE),
            ImmutableList.of(ERROR_COUNT, ERROR_COUNT_PER_NODE),
            ImmutableList.of(REQUESTS_PER_SECOND_LAST, REQUESTS_PER_SECOND_LAST_PER_NODE),
            ImmutableList.of(REQUESTS_PER_SECOND_IN_WINDOW, REQUESTS_PER_SECOND_IN_WINDOW_PER_NODE),
            ImmutableList.of(TOTAL_PROCESSING_TIME, TOTAL_PROCESSING_TIME_PER_NODE),
            ImmutableList.of(
                PROCESSING_TIME_FRACTION_IN_WINDOW, PROCESSING_TIME_FRACTION_IN_WINDOW_PER_NODE));

    for (List<? extends AttributeSensor<? extends Number>> es : summingEnricherSetup) {
      AttributeSensor<? extends Number> t = es.get(0);
      AttributeSensor<? extends Number> total = es.get(1);
      addEnricher(
          Enrichers.builder()
              .aggregating(t)
              .publishing(total)
              .fromMembers()
              .computingSum()
              .build());
    }

    for (List<? extends AttributeSensor<? extends Number>> es : averagingEnricherSetup) {
      @SuppressWarnings("unchecked")
      AttributeSensor<Number> t = (AttributeSensor<Number>) es.get(0);
      @SuppressWarnings("unchecked")
      AttributeSensor<Double> average = (AttributeSensor<Double>) es.get(1);
      addEnricher(
          Enrichers.builder()
              .aggregating(t)
              .publishing(average)
              .fromMembers()
              .computingAverage()
              .defaultValueForUnreportedSensors(0)
              .build());
    }
  }
 @BeforeMethod(alwaysRun = true)
 public void setUp() throws Exception {
   localManagementContext = newPersistingManagementContext();
   if (persister == null) {
     persister = localManagementContext.getRebindManager().getPersister();
   }
   if (objectStore == null && persister instanceof BrooklynMementoPersisterToObjectStore) {
     objectStore = ((BrooklynMementoPersisterToObjectStore) persister).getObjectStore();
   }
   app =
       ApplicationBuilder.newManagedApp(
           EntitySpec.create(TestApplication.class), localManagementContext);
   location =
       localManagementContext
           .getLocationManager()
           .createLocation(
               LocationSpec.create(SshMachineLocation.class).configure("address", "localhost"));
   entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).location(location));
   enricher = app.addEnricher(Enrichers.builder().propagatingAll().from(entity).build());
   app.addPolicy(policy = new TestPolicy());
 }
  @SuppressWarnings("unchecked")
  public Application create(ApplicationSpec spec) {
    log.debug("REST creating application instance for {}", spec);

    if (!Entitlements.isEntitled(
        mgmt.getEntitlementManager(), Entitlements.DEPLOY_APPLICATION, spec)) {
      throw WebResourceUtils.unauthorized(
          "User '%s' is not authorized to deploy application %s",
          Entitlements.getEntitlementContext().user(), spec);
    }

    final String type = spec.getType();
    final String name = spec.getName();
    final Map<String, String> configO = spec.getConfig();
    final Set<EntitySpec> entities =
        (spec.getEntities() == null) ? ImmutableSet.<EntitySpec>of() : spec.getEntities();

    final Application instance;

    // Load the class; first try to use the appropriate catalog item; but then allow anything that
    // is on the classpath
    final Class<? extends Entity> clazz;
    if (Strings.isEmpty(type)) {
      clazz = BasicApplication.class;
    } else {
      Class<? extends Entity> tempclazz;
      try {
        tempclazz = getCatalog().loadClassByType(type, Entity.class);
      } catch (NoSuchElementException e) {
        try {
          tempclazz = (Class<? extends Entity>) getCatalog().getRootClassLoader().loadClass(type);
          log.info(
              "Catalog does not contain item for type {}; loaded class directly instead", type);
        } catch (ClassNotFoundException e2) {
          log.warn(
              "No catalog item for type {}, and could not load class directly; rethrowing", type);
          throw e;
        }
      }
      clazz = tempclazz;
    }
    if (Entitlements.isEntitled(mgmt.getEntitlementManager(), Entitlements.INVOKE_EFFECTOR, null)) {

      try {
        if (ApplicationBuilder.class.isAssignableFrom(clazz)) {
          Constructor<?> constructor = clazz.getConstructor();
          ApplicationBuilder appBuilder = (ApplicationBuilder) constructor.newInstance();
          if (!Strings.isEmpty(name)) appBuilder.appDisplayName(name);
          if (entities.size() > 0)
            log.warn(
                "Cannot supply additional entities when using an ApplicationBuilder; ignoring in spec {}",
                spec);

          log.info("REST placing '{}' under management", spec.getName());
          appBuilder.configure(convertFlagsToKeys(appBuilder.getType(), configO));
          configureRenderingMetadata(spec, appBuilder);
          instance = appBuilder.manage(mgmt);

        } else if (Application.class.isAssignableFrom(clazz)) {
          brooklyn.entity.proxying.EntitySpec<?> coreSpec = toCoreEntitySpec(clazz, name, configO);
          configureRenderingMetadata(spec, coreSpec);
          instance = (Application) mgmt.getEntityManager().createEntity(coreSpec);
          for (EntitySpec entitySpec : entities) {
            log.info("REST creating instance for entity {}", entitySpec.getType());
            instance.addChild(mgmt.getEntityManager().createEntity(toCoreEntitySpec(entitySpec)));
          }

          log.info(
              "REST placing '{}' under management", spec.getName() != null ? spec.getName() : spec);
          Entities.startManagement(instance, mgmt);

        } else if (Entity.class.isAssignableFrom(clazz)) {
          if (entities.size() > 0)
            log.warn(
                "Cannot supply additional entities when using a non-application entity; ignoring in spec {}",
                spec);

          brooklyn.entity.proxying.EntitySpec<?> coreSpec =
              toCoreEntitySpec(BasicApplication.class, name, configO);
          configureRenderingMetadata(spec, coreSpec);

          instance = (Application) mgmt.getEntityManager().createEntity(coreSpec);

          final Class<? extends Entity> eclazz =
              getCatalog().loadClassByType(spec.getType(), Entity.class);
          Entity soleChild =
              mgmt.getEntityManager().createEntity(toCoreEntitySpec(eclazz, name, configO));
          instance.addChild(soleChild);
          instance.addEnricher(Enrichers.builder().propagatingAll().from(soleChild).build());

          log.info("REST placing '{}' under management", spec.getName());
          Entities.startManagement(instance, mgmt);

        } else {
          throw new IllegalArgumentException(
              "Class " + clazz + " must extend one of ApplicationBuilder, Application or Entity");
        }

        return instance;

      } catch (Exception e) {
        log.error("REST failed to create application: " + e, e);
        throw Exceptions.propagate(e);
      }
    }
    throw WebResourceUtils.unauthorized(
        "User '%s' is not authorized to create application from applicationSpec %s",
        Entitlements.getEntitlementContext().user(), spec);
  }