/**
   * Register an Agent with the server. This method calls out to verify it can talk to the agent,
   * and then adds it to the database. With all the connection info used to make further
   * connections.
   */
  private RegisterAgent_result cmdRegisterAgent(LatherContext ctx, RegisterAgent_args args)
      throws LatherRemoteException {
    try {
      checkUserCanManageAgent(ctx, args.getUser(), args.getPword(), "register");
    } catch (PermissionException exc) {
      return new RegisterAgent_result("Permission denied");
    }
    Collection<Integer> ids = new ArrayList<Integer>();

    RegisterAgent_result result = registerAgent(args, ids);

    /**
     * Reschedule all metrics on a platform when it is started for the first time. This allows the
     * schedule to be updated immediately on either agent updates, or if the user removes the agent
     * data directory.
     */
    if (!ids.isEmpty()) {
      try {
        List<ResourceRefreshZevent> zevents = new ArrayList<ResourceRefreshZevent>();
        ResourceRefreshZevent zevent;
        AuthzSubject overlord = authzSubjectManager.getOverlordPojo();
        for (Integer id : ids) {

          Platform platform = platformManager.findPlatformById(id);

          zevent = new ResourceRefreshZevent(overlord, platform.getEntityId());
          zevents.add(zevent);

          Collection<Server> servers = platform.getServers();
          for (Server server : servers) {

            zevent = new ResourceRefreshZevent(overlord, server.getEntityId());
            zevents.add(zevent);

            Collection<Service> services = server.getServices();
            for (Service service : services) {

              zevent = new ResourceRefreshZevent(overlord, service.getEntityId());
              zevents.add(zevent);
            }
          }
        }

        zeventManager.enqueueEventsAfterCommit(zevents);

      } catch (Exception e) {
        // Not fatal, the metrics will eventually be rescheduled...
        log.error("Unable to refresh agent schedule", e);
      }
    }
    return result;
  }
 private void removeOrphanedPlatforms(AuthzSubject overlord) {
   final Collection<Platform> platforms = platformManager.getOrphanedPlatforms();
   if (!platforms.isEmpty()) {
     log.info("cleaning up " + platforms.size() + " orphaned platforms");
   }
   for (Platform platform : platforms) {
     try {
       platform = platformManager.getPlatformById(platform.getId());
       platformManager.removePlatform(overlord, platform);
     } catch (ObjectNotFoundException e) {
       log.warn(e);
       log.debug(e, e);
     } catch (PlatformNotFoundException e) {
       log.warn(e);
       log.debug(e, e);
     } catch (PermissionException e) {
       log.warn(e);
       log.debug(e, e);
     } catch (VetoException e) {
       log.warn(e);
       log.debug(e, e);
     }
   }
 }
  @SuppressWarnings("unchecked")
  private void removeDeletedResources(
      Map<Integer, List<AppdefEntityID>> agentCache, Collection<String> typeNames)
      throws ApplicationException, VetoException {
    final boolean debug = log.isDebugEnabled();
    final StopWatch watch = new StopWatch();
    final AuthzSubject subject = authzSubjectManager.findSubjectById(AuthzConstants.overlordId);
    if (debug) watch.markTimeBegin("unscheduleMeasurementsForAsyncDelete");
    unscheduleMeasurementsForAsyncDelete(agentCache);
    if (debug) watch.markTimeEnd("unscheduleMeasurementsForAsyncDelete");

    // Look through services, servers, platforms, applications, and groups
    if (debug) watch.markTimeBegin("removeApplications");
    Collection<Application> applications = applicationManager.findDeletedApplications();
    removeApplications(subject, applications);
    if (debug) watch.markTimeEnd("removeApplications");

    if (debug) watch.markTimeBegin("removeResourceGroups");
    Collection<ResourceGroup> groups = resourceGroupManager.findDeletedGroups();
    removeResourceGroups(subject, groups);
    if (debug) watch.markTimeEnd("removeResourceGroups");

    typeNames = (typeNames == null) ? Collections.EMPTY_LIST : typeNames;
    if (debug) watch.markTimeBegin("removeGroupsCompatibleWith");
    for (String name : typeNames) {
      resourceGroupManager.removeGroupsCompatibleWith(name);
    }
    if (debug) watch.markTimeEnd("removeGroupsCompatibleWith");

    Collection<Service> services = serviceManager.findDeletedServices();
    removeServices(subject, services);

    Collection<Server> servers = serverManager.findDeletedServers();
    removeServers(subject, servers);

    if (debug) watch.markTimeBegin("removePlatforms");
    Collection<Platform> platforms = platformManager.findDeletedPlatforms();
    removePlatforms(subject, platforms);
    if (debug) watch.markTimeEnd("removePlatforms");
    if (debug) log.debug("removeDeletedResources: " + watch);
  }
  /**
   * Registers the agent according to the provided RegisterAgent_args under a new transaction
   *
   * @param args - the RegisterAgent_args
   * @param ids - if the is an existing agent, this collection will contain the ids of the platforms
   *     monitored by this agent after this method execution.
   * @return - RegisterAgent_result
   */
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  private RegisterAgent_result registerAgent(RegisterAgent_args args, Collection<Integer> ids) {
    String agentIP = args.getAgentIP();
    int port = args.getAgentPort();
    String version = args.getVersion();
    boolean isNewTransportAgent = args.isNewTransportAgent();
    boolean unidirectional = args.isUnidirectional();
    boolean acceptValidation = args.isAcceptCertificates();
    String errRes =
        testAgentConn(
            agentIP,
            port,
            args.getAuthToken(),
            isNewTransportAgent,
            unidirectional,
            acceptValidation);

    if (errRes != null) {
      return new RegisterAgent_result(errRes);
    }

    boolean isOldAgentToken = true;
    String agentToken = args.getAgentToken();

    if (agentToken == null) {
      // Generate a unique agent token
      agentToken = SecurityUtil.generateRandomToken();

      while (!agentManager.isAgentTokenUnique(agentToken)) {
        agentToken = SecurityUtil.generateRandomToken();
      }

      isOldAgentToken = false;
    }

    // Check the to see if the agent already exists.
    // Lookup the agent by agent token (if it exists). Otherwise, use the
    // agent IP and port.
    try {
      Agent origAgent;

      if (isOldAgentToken) {
        origAgent = agentManager.getAgent(agentToken);
      } else {
        origAgent = agentManager.getAgent(agentIP, port);
      }

      try {
        ids.addAll(
            platformManager.getPlatformPksByAgentToken(
                authzSubjectManager.getOverlordPojo(), origAgent.getAgentToken()));
      } catch (Exception e) {
        // No platforms found, no a big deal
      }

      log.info(
          "Found preexisting agent during agent registration. "
              + "Updating agent information for "
              + agentIP
              + ":"
              + port
              + "; new transport="
              + isNewTransportAgent
              + "; unidirectional="
              + unidirectional);

      if (isOldAgentToken) {
        if (isNewTransportAgent) {
          agentManager.updateNewTransportAgent(
              agentToken, agentIP, port, args.getAuthToken(), version, unidirectional);
        } else {
          agentManager.updateLegacyAgent(agentToken, agentIP, port, args.getAuthToken(), version);
        }
      } else {
        if (isNewTransportAgent) {
          agentManager.updateNewTransportAgent(
              agentIP, port, args.getAuthToken(), agentToken, version, unidirectional);
        } else {
          agentManager.updateLegacyAgent(agentIP, port, args.getAuthToken(), agentToken, version);
        }
      }
    } catch (AgentNotFoundException exc) {
      log.info(
          "Registering agent at "
              + agentIP
              + ":"
              + port
              + ""
              + "; new transport="
              + isNewTransportAgent
              + "; unidirectional="
              + unidirectional);
      try {
        if (isNewTransportAgent) {
          agentManager.createNewTransportAgent(
              agentIP, new Integer(port), args.getAuthToken(), agentToken, version, unidirectional);
        } else {
          agentManager.createLegacyAgent(
              agentIP, new Integer(port), args.getAuthToken(), agentToken, version);
        }

      } catch (AgentCreateException oexc) {
        log.error("Error creating agent", oexc);
        return new RegisterAgent_result("Error creating agent: " + oexc.getMessage());
      } catch (SystemException oexc) {
        log.error("Error creating agent", oexc);
        return new RegisterAgent_result("Error creating agent:  " + "Internal system error");
      }
    } catch (SystemException exc) {
      log.error("Error updating agent", exc);
      return new RegisterAgent_result("Error updating agent:  " + "Internal system error");
    }

    RegisterAgent_result result = new RegisterAgent_result("token:" + agentToken);
    return result;
  }