List<AgentInfo> removedAgents = newArrayList(0);
Set<AgentIdentity> allAttachedAgents = getAgentManager().getAllAttachedAgents();
Map<String, AgentControllerIdentityImplementation> attachedAgentMap = Maps.newHashMap();
for (AgentIdentity agentIdentity : allAttachedAgents) {
AgentControllerIdentityImplementation agentControllerIdentity = cast(agentIdentity);
attachedAgentMap.put(createKey(agentControllerIdentity), agentControllerIdentity);
}
// If region is not specified retrieved all
Map<String, AgentInfo> agentInDBMap = newHashMap();
// step1. check all agents in DB, whether they are attached to
// controller.
for (AgentInfo each : getAllLocal()) {
final String agentKey = createKey(each);
if (!agentInDBMap.containsKey(agentKey)) {
agentInDBMap.put(agentKey, each);
} else {
removedAgents.add(each);
}
}
for (Map.Entry<String, AgentInfo> each : agentInDBMap.entrySet()) {
String agentKey = each.getKey();
AgentInfo agentInfo = each.getValue();
AgentControllerIdentityImplementation agentIdentity = attachedAgentMap.remove(agentKey);
if (agentIdentity == null) {
// this agent is not attached to controller
agentInfo.setState(AgentControllerState.INACTIVE);
stateUpdatedAgents.add(agentInfo);
} else if (!hasSameInfo(agentInfo, agentIdentity)) {
agentInfo.setRegion(agentIdentity.getRegion());
agentInfo.setPort(agentManager.getAgentConnectingPort(agentIdentity));
agentInfo.setVersion(agentManager.getAgentVersion(agentIdentity));
updatedAgents.add(agentInfo);
} else if (!hasSameState(agentInfo, agentIdentity)) {
agentInfo.setState(agentManager.getAgentState(agentIdentity));