public static Map<String, Object> getSensorMap(String sensor, Iterable<Entity> descs) {
   if (Iterables.isEmpty(descs)) return Collections.emptyMap();
   Map<String, Object> result = MutableMap.of();
   Iterator<Entity> di = descs.iterator();
   Sensor<?> s = null;
   while (di.hasNext()) {
     Entity potentialSource = di.next();
     s = potentialSource.getEntityType().getSensor(sensor);
     if (s != null) break;
   }
   if (s == null) s = Sensors.newSensor(Object.class, sensor);
   if (!(s instanceof AttributeSensor<?>)) {
     log.warn("Cannot retrieve non-attribute sensor " + s + " for entities; returning empty map");
     return result;
   }
   for (Entity e : descs) {
     Object v = null;
     try {
       v = e.getAttribute((AttributeSensor<?>) s);
     } catch (Exception exc) {
       Exceptions.propagateIfFatal(exc);
       log.warn("Error retrieving sensor " + s + " for " + e + " (ignoring): " + exc);
     }
     if (v != null) result.put(e.getId(), v);
   }
   return result;
 }
Example #2
0
 private void configureEnrichers(Entity entity) {
   for (AttributeSensor sensor :
       Iterables.filter(entity.getEntityType().getSensors(), AttributeSensor.class)) {
     if ((DockerUtils.URL_SENSOR_NAMES.contains(sensor.getName())
             || sensor.getName().endsWith(".url")
             || URI.class.isAssignableFrom(sensor.getType()))
         && !DockerUtils.BLACKLIST_URL_SENSOR_NAMES.contains(sensor.getName())) {
       AttributeSensor<String> target = DockerUtils.<String>mappedSensor(sensor);
       entity.addEnricher(
           dockerHost
               .getSubnetTier()
               .uriTransformingEnricher(EntityAndAttribute.create(entity, sensor), target));
       Set<Hint<?>> hints = RendererHints.getHintsFor(sensor);
       for (Hint<?> hint : hints) {
         RendererHints.register(target, (NamedActionWithUrl) hint);
       }
       if (LOG.isDebugEnabled()) {
         LOG.debug("Mapped URL sensor: origin={}, mapped={}", sensor.getName(), target.getName());
       }
     } else if (PortAttributeSensorAndConfigKey.class.isAssignableFrom(sensor.getClass())) {
       AttributeSensor<String> target =
           DockerUtils.mappedPortSensor((PortAttributeSensorAndConfigKey) sensor);
       entity.addEnricher(
           dockerHost
               .getSubnetTier()
               .hostAndPortTransformingEnricher(
                   EntityAndAttribute.create(entity, sensor), target));
       if (LOG.isDebugEnabled()) {
         LOG.debug("Mapped port sensor: origin={}, mapped={}", sensor.getName(), target.getName());
       }
     }
   }
 }
  private ObjectNode entityBase(Entity entity) {
    ObjectNode aRoot = mapper().createObjectNode();
    aRoot.put("name", entity.getDisplayName());
    aRoot.put("id", entity.getId());
    aRoot.put("type", entity.getEntityType().getName());

    Boolean serviceUp = entity.getAttribute(Attributes.SERVICE_UP);
    if (serviceUp != null) aRoot.put("serviceUp", serviceUp);

    Lifecycle serviceState = entity.getAttribute(Attributes.SERVICE_STATE_ACTUAL);
    if (serviceState != null) aRoot.put("serviceState", serviceState.toString());

    String iconUrl = entity.getIconUrl();
    if (iconUrl != null) {
      if (brooklyn().isUrlServerSideAndSafe(iconUrl))
        // route to server if it is a server-side url
        iconUrl = EntityTransformer.entityUri(entity) + "/icon";
      aRoot.put("iconUrl", iconUrl);
    }

    return aRoot;
  }