private String derriveTargetAgent()

in api/service/src/main/java/org/apache/airavata/mft/api/handler/MFTApiHandler.java [487:513]


    private String derriveTargetAgent(GetResourceMetadataRequest directRequest) throws Exception {

        String targetAgent = "";

        if (directRequest.getStorage().getStorageCase() == StorageWrapper.StorageCase.LOCAL) {
            targetAgent = directRequest.getStorage().getLocal().getAgentId();
        }

        if (targetAgent.isEmpty()) {
            List<String> liveAgentIds = mftConsulClient.getLiveAgentIds();
            if (liveAgentIds.isEmpty()) {
                throw new Exception("No agent is available to perform the operation");
            }
            if (liveAgentIds.stream().anyMatch(id -> id.equals("local-agent"))) {
                targetAgent = "local-agent";
            } else {
                targetAgent = liveAgentIds.get(0);
            }
            logger.info("Using agent {} for processing the operation", targetAgent);
        } else {
            Optional<AgentInfo> agentInfo = mftConsulClient.getAgentInfo(targetAgent);
            if (agentInfo.isEmpty()) {
                throw new Exception("Target agent " + targetAgent + " is not available");
            }
        }
        return targetAgent;
    }