private void addMeasurementConfig(
      List<MeasurementConfigEntity> ents, AppdefEntityID id, String typeName) {
    MeasurementConfigEntity ent = new MeasurementConfigEntity();
    ConfigResponse response;
    byte[] config;

    try {
      response =
          configManager.getMergedConfigResponse(
              authzSubjectManager.getOverlordPojo(), ProductPlugin.TYPE_MEASUREMENT, id, true);

      // Only send the configuration to the agent if log or
      // config file tracking has been enabled.
      if (ConfigTrackPlugin.isEnabled(response, id.getType())
          || LogTrackPlugin.isEnabled(response, id.getType())) {

        config = response.encode();

        ent.setPluginName(id.getAppdefKey());
        ent.setPluginType(typeName);
        ent.setConfig(config);

        ents.add(ent);
      }

    } catch (Exception exc) {
      return; // Not a fatal condition
    }
  }
 public void processEvents(List<ResourcesCleanupZevent> events) {
   log.info("starting removeDeletedResources");
   final StopWatch watch = new StopWatch();
   final Collection<String> typeNames = new ArrayList<String>();
   for (final ResourcesCleanupZevent e : events) {
     if (e instanceof ResourceTypeCleanupZevent) {
       typeNames.addAll(((ResourceTypeCleanupZevent) e).getTypeNames());
     }
   }
   if (events != null && !events.isEmpty()) {
     try {
       Map<Integer, List<AppdefEntityID>> agentCache = buildAsyncDeleteAgentCache(events);
       removeDeletedResources(agentCache, typeNames);
       final AuthzSubject overlord = authzSubjectManager.getOverlordPojo();
       if (!typeNames.isEmpty()) {
         resourceManager.removeResourceTypes(typeNames);
       }
       removeOrphanedPlatforms(overlord);
       removeOrphanedServers(overlord);
       removeOrphanedServices(overlord);
       removeOrphanedAlertDefs();
       removeOrphanedResourceGroupMembers();
       removeOrphanedAudits();
       removeOrphanedEscalationStates(overlord);
       removeOrphanedResources(overlord);
     } catch (Exception e) {
       log.error("removeDeletedResources failed", e);
     }
   }
   log.info("completed removeDeletedResources " + watch);
 }
  /**
   * 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;
  }
  /** Get config information about all the entities which an agent is servicing. */
  private MeasurementGetConfigs_result cmdMeasurementGetConfigs(MeasurementGetConfigs_args args)
      throws LatherRemoteException {
    ResourceTree tree;
    AuthzSubject overlord = authzSubjectManager.getOverlordPojo();
    try {
      tree = agentManager.getEntitiesForAgent(overlord, args.getAgentToken());
    } catch (PermissionException exc) {
      throw new SystemException("Overlord unable to get resource " + "tree list");
    } catch (AgentNotFoundException exc) {
      throw new SystemException("Validated an agent which could " + "not be found");
    }
    ArrayList<MeasurementConfigEntity> ents = new ArrayList<MeasurementConfigEntity>();
    for (Iterator<PlatformNode> p = tree.getPlatformIterator(); p.hasNext(); ) {
      PlatformNode pNode = p.next();
      addMeasurementConfig(ents, pNode.getPlatform());
      try {
        AppdefEntityValue aeval =
            new AppdefEntityValue(pNode.getPlatform().getEntityId(), overlord);
        List<AppdefResourceValue> services = aeval.getAssociatedServices(PageControl.PAGE_ALL);
        for (int i = 0; i < services.size(); i++) {
          ServiceValue val = (ServiceValue) services.get(i);

          AppdefEntityID id = val.getEntityId();

          addMeasurementConfig(ents, id, val.getServiceType().getName());
        }
      } catch (Exception e) {
        // Shouldn't happen
        log.error("Encountered exception looking up platform " + "services: " + e.getMessage(), e);
      }

      for (Iterator<ServerNode> s = pNode.getServerIterator(); s.hasNext(); ) {
        ServerNode sNode = s.next();

        addMeasurementConfig(ents, sNode.getServer());

        for (Iterator<ServiceNode> v = sNode.getServiceIterator(); v.hasNext(); ) {
          ServiceNode vNode = v.next();

          addMeasurementConfig(ents, vNode.getService());
        }
      }
    }

    MeasurementConfigList cList = new MeasurementConfigList();
    cList.setEntities(ents.toArray(new MeasurementConfigEntity[ents.size()]));
    MeasurementGetConfigs_result res = new MeasurementGetConfigs_result();
    res.setConfigs(cList);
    return res;
  }
  /** @see org.hyperic.hq.zevents.ZeventListener#processEvents(java.util.List) */
  public void processEvents(List<TransferAgentBundleZevent> events) {
    AuthzSubject overlord = authzSubjectManager.getOverlordPojo();

    for (TransferAgentBundleZevent zevent : events) {

      try {
        agentManager.transferAgentBundle(overlord, zevent.getAgent(), zevent.getAgentBundleFile());
      } catch (Exception e) {
        _log.warn(
            "Failed to transfer agent bundle "
                + zevent.getAgentBundleFile()
                + " to agent "
                + zevent.getAgent().getID(),
            e);
      }
    }
  }
  /** Receive status information about a previous control action */
  private NullLatherValue cmdControlSendCommandResult(ControlSendCommandResult_args args) {
    controlManager.sendCommandResult(
        args.getId(), args.getResult(), args.getStartTime(), args.getEndTime(), args.getMessage());

    // Get live measurements on the resource
    String name = args.getName();
    if (name != null) {
      log.info("Getting live measurements for " + name);
      AppdefEntityID id = new AppdefEntityID(name);
      try {
        measurementManager.getLiveMeasurementValues(authzSubjectManager.getOverlordPojo(), id);
      } catch (Exception e) {
        log.error("Unable to fetch live measurements: " + e, e);
      }
    } else {
      log.error("No plugin name found, not fetching live measurements");
    }

    return NullLatherValue.INSTANCE;
  }
  /**
   * 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;
  }