@Test
 public void testWrapsEntity() throws Exception {
   Entity app = createAndStartApplication("services:", "- type: " + BasicEntity.class.getName());
   assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicEntity);
 }
 @Test
 public void testDoesNotWrapApp() throws Exception {
   Entity app =
       createAndStartApplication("services:", "- type: " + BasicApplication.class.getName());
   assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertTrue(app.getChildren().isEmpty());
 }
 @Test
 public void testDoesNotWrapsEntityIfNoNameOnService() throws Exception {
   Entity app =
       createAndStartApplication(
           "name: topLevel", "services:", "- type: " + BasicApplication.class.getName());
   assertNull(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertTrue(app.getChildren().isEmpty());
   assertEquals(app.getDisplayName(), "topLevel");
 }
 @Test
 public void testWrapsEntityIfDifferentTopLevelName() throws Exception {
   Entity app =
       createAndStartApplication(
           "name: topLevel",
           "services:",
           "- type: " + BasicApplication.class.getName(),
           "  name: bottomLevel");
   assertTrue(app.getConfig(EntityManagementUtils.WRAPPER_APP_MARKER));
   assertTrue(app instanceof BasicApplication);
   assertEquals(app.getDisplayName(), "topLevel");
   assertTrue(Iterables.getOnlyElement(app.getChildren()) instanceof BasicApplication);
   assertTrue(Iterables.getOnlyElement(app.getChildren()).getChildren().isEmpty());
   assertEquals(Iterables.getOnlyElement(app.getChildren()).getDisplayName(), "bottomLevel");
 }
 public static ChefModes detectChefMode(Entity entity) {
   ChefModes mode = entity.getConfig(ChefConfig.CHEF_MODE);
   if (mode == ChefModes.AUTODETECT) {
     // TODO server via API
     ProcessTaskWrapper<Boolean> installCheck =
         DynamicTasks.queue(ChefServerTasks.isKnifeInstalled());
     mode = installCheck.get() ? ChefModes.KNIFE : ChefModes.SOLO;
     log.debug(
         "Using Chef in "
             + mode
             + " mode due to autodetect exit code "
             + installCheck.getExitCode());
   }
   Preconditions.checkNotNull(
       mode, "Non-null " + ChefConfig.CHEF_MODE + " required for " + entity);
   return mode;
 }
  public static HostAndPort getBrooklynAccessibleAddress(Entity entity, int port) {
    String host;

    // look up port forwarding
    PortForwardManager pfw = entity.getConfig(PORT_FORWARDING_MANAGER);
    if (pfw != null) {
      Collection<Location> ll = entity.getLocations();

      synchronized (BrooklynAccessUtils.class) {
        // TODO finer-grained synchronization

        for (MachineLocation machine : Iterables.filter(ll, MachineLocation.class)) {
          HostAndPort hp = pfw.lookup(machine, port);
          if (hp != null) {
            log.debug(
                "BrooklynAccessUtils found port-forwarded address {} for entity {}, port {}, using machine {}",
                new Object[] {hp, entity, port, machine});
            return hp;
          }
        }

        Maybe<SupportsPortForwarding> supportPortForwardingLoc =
            Machines.findUniqueElement(ll, SupportsPortForwarding.class);
        if (supportPortForwardingLoc.isPresent()) {
          Cidr source = entity.getConfig(MANAGEMENT_ACCESS_CIDR);
          SupportsPortForwarding loc = supportPortForwardingLoc.get();
          if (source != null) {
            log.debug(
                "BrooklynAccessUtils requesting new port-forwarding rule to access "
                    + port
                    + " on "
                    + entity
                    + " (at "
                    + loc
                    + ", enabled for "
                    + source
                    + ")");
            // TODO discuss, is this the best way to do it
            // (will probably _create_ the port forwarding rule!)
            HostAndPort hp = loc.getSocketEndpointFor(source, port);
            if (hp != null) {
              log.debug(
                  "BrooklynAccessUtils created port-forwarded address {} for entity {}, port {}, using {}",
                  new Object[] {hp, entity, port, loc});
              return hp;
            }
          } else {
            log.warn(
                "No "
                    + MANAGEMENT_ACCESS_CIDR.getName()
                    + " configured for "
                    + entity
                    + ", so cannot forward "
                    + "port "
                    + port
                    + " "
                    + "even though "
                    + PORT_FORWARDING_MANAGER.getName()
                    + " was supplied, and "
                    + "have location supporting port forwarding "
                    + loc);
          }
        }
      }
    }

    host = entity.getAttribute(Attributes.HOSTNAME);
    if (host != null) return HostAndPort.fromParts(host, port);

    throw new IllegalStateException(
        "Cannot find way to access port "
            + port
            + " on "
            + entity
            + " from Brooklyn (no host.name)");
  }