/**
  * Approve/Unapprove the agent on given ip.
  *
  * @param ip ip
  * @param approve true/false
  */
 @CacheEvict(allEntries = true, value = "agents")
 public void approve(String ip, boolean approve) {
   List<AgentInfo> found = agentRepository.findAllByIp(ip);
   for (AgentInfo each : found) {
     each.setApproved(approve);
     agentRepository.save(each);
     agentRepository.findOne(each.getId());
     if (approve) {
       LOGGER.info("agent {} is approved", ip);
     } else {
       LOGGER.info("agent {} is not approved", ip);
     }
   }
 }
 /**
  * Get agents. agent list is obtained from DB and {@link AgentManager}
  *
  * <p>This includes not persisted agent as well.
  *
  * @return agent list
  */
 @Transactional
 public List<AgentInfo> getAgentList() {
   Set<AgentIdentity> allAttachedAgents = agentManager.getAllAttachedAgents();
   List<AgentInfo> agents = agentRepository.findAll();
   List<AgentInfo> agentList = new ArrayList<AgentInfo>(allAttachedAgents.size());
   for (AgentIdentity eachAgentIdentity : allAttachedAgents) {
     AgentControllerIdentityImplementation agentControllerIdentity =
         (AgentControllerIdentityImplementation) eachAgentIdentity;
     agentList.add(creatAgentInfo(agentControllerIdentity, agents));
   }
   return agentList;
 }
 /**
  * Get a agent on given id.
  *
  * @param id agent id
  * @return agent
  */
 public AgentInfo getAgent(long id) {
   AgentInfo agentInfo = agentRepository.findOne(id);
   if (agentInfo == null) {
     return null;
   }
   AgentControllerIdentityImplementation agentIdentity =
       agentManager.getAgentIdentityByIp(agentInfo.getIp());
   if (agentIdentity != null) {
     agentInfo.setStatus(agentManager.getAgentState(agentIdentity));
     agentInfo.setPort(agentManager.getAgentConnectingPort(agentIdentity));
     agentInfo.setHostName(agentIdentity.getName());
     agentInfo.setRegion(agentIdentity.getRegion());
     agentInfo.setAgentIdentity(agentIdentity);
   }
   return agentInfo;
 }
 @CacheEvict(allEntries = true, value = "agents")
 private AgentInfo creatAgentInfo(
     AgentControllerIdentityImplementation agentIdentity, List<AgentInfo> agents) {
   AgentInfo agentInfo = new AgentInfo();
   for (AgentInfo each : agents) {
     if (each != null && StringUtils.equals(each.getIp(), agentIdentity.getIp())) {
       agentInfo = each;
       break;
     }
   }
   if (!StringUtils.equals(agentInfo.getHostName(), agentIdentity.getName())
       || !StringUtils.equals(agentInfo.getRegion(), agentIdentity.getRegion())) {
     agentInfo.setHostName(agentIdentity.getName());
     agentInfo.setRegion(agentIdentity.getRegion());
     agentInfo.setIp(agentIdentity.getIp());
     agentInfo = agentRepository.save(agentInfo);
   }
   agentInfo.setPort(agentManager.getAgentConnectingPort(agentIdentity));
   agentInfo.setStatus(agentManager.getAgentState(agentIdentity));
   // need to save agent info into DB, like ip and port maybe changed.
   return agentInfo;
 }
 /**
  * Get agents. agent list is obtained only from DB
  *
  * @return agent list
  */
 @Cacheable("agents")
 public List<AgentInfo> getAgentListOnDB() {
   return agentRepository.findAll();
 }
 /**
  * Delete agent.
  *
  * @param id agent id to be deleted
  */
 @CacheEvict(allEntries = true, value = "agents")
 public void deleteAgent(long id) {
   agentRepository.delete(id);
 }
 /**
  * Save agent.
  *
  * @param agent saved agent
  */
 @CacheEvict(allEntries = true, value = "agents")
 public void saveAgent(AgentInfo agent) {
   agentRepository.save(agent);
 }