private boolean hasSamePortAndStatus(
     AgentInfo agentInfo, AgentControllerIdentityImplementation agentIdentity) {
   if (agentInfo == null) {
     return false;
   }
   AgentManager agentManager = getAgentManager();
   return agentInfo.getPort() == agentManager.getAgentConnectingPort(agentIdentity)
       && agentInfo.getStatus() == agentManager.getAgentState(agentIdentity);
 }
 /**
  * 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;
 }
 /**
  * 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;
 }
 @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;
 }