class LowUtilizationAmazonEC2InstancesRule extends BaseRule {

	@Override
	public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) {
		
		logger.debug("========LowUtilizationAmazonEC2InstancesRule started=========");
		Annotation annotation = null;
		String resourceId = null;
		String accountId = null;
		String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID));
		
		String severity = ruleParam.get(PacmanRuleConstants.SEVERITY);
		String category = ruleParam.get(PacmanRuleConstants.CATEGORY);
		
		String serviceEsURL = null;
		
		String pacmanHost = PacmanUtils.getPacmanHost(PacmanRuleConstants.ES_URI);
		logger.debug("========pacmanHost {}  =========",pacmanHost);
		
		if(!StringUtils.isNullOrEmpty(pacmanHost)){
		    serviceEsURL = ruleParam.get(PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM);
		    serviceEsURL = pacmanHost+serviceEsURL;
		}
		
		logger.debug("========service URL after concatination param {}  =========",serviceEsURL);
		
		MDC.put("executionId", ruleParam.get("executionId")); // this is the logback Mapped Diagnostic Contex
		MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); // this is the logback Mapped Diagnostic Contex
		
		List<LinkedHashMap<String,Object>>issueList = new ArrayList<>();
		LinkedHashMap<String,Object>issue = new LinkedHashMap<>();
		
		if (!PacmanUtils.doesAllHaveValue(checkId,severity,category,serviceEsURL)) {
			logger.info(PacmanRuleConstants.MISSING_CONFIGURATION);
			throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION);
		}
		
		if (resourceAttributes != null) {
			accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID);
			resourceId = StringUtils.trim(resourceAttributes.get(PacmanSdkConstants.RESOURCE_ID));
			Map<String, String> lowUtilEc2Map = new HashMap<>();
			try {
				lowUtilEc2Map = PacmanUtils.getLowUtilizationEc2Details(checkId,resourceId,serviceEsURL,null,accountId);
			} catch (Exception e) {
				logger.error("unable to determine",e);
				throw new RuleExecutionFailedExeption("unable to determine"+e);
			}
			if (!lowUtilEc2Map.isEmpty()) {
				annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE);
				annotation.put(PacmanSdkConstants.DESCRIPTION,"Low utilization amazon ec2 instance found !!");
				annotation.put(PacmanRuleConstants.SEVERITY, severity); 
				annotation.put(PacmanRuleConstants.CATEGORY, category);
				annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, lowUtilEc2Map.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS));
				annotation.put(PacmanRuleConstants.NO_OF_LOW_UTILIZATION, lowUtilEc2Map.get(PacmanRuleConstants.NO_OF_LOW_UTILIZATION));
				
				issue.put(PacmanRuleConstants.VIOLATION_REASON, "Low utilization amazon ec2 instance found");
				issue.put(PacmanRuleConstants.CHECKID, checkId);
				issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor");
				issueList.add(issue);
				annotation.put("issueDetails",issueList.toString());
				logger.debug("========LowUtilizationAmazonEC2InstancesRule ended with an annotation {} : =========",annotation);
				return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation);
			}
			}
		logger.debug("========LowUtilizationAmazonEC2InstancesRule ended=========");
		return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE);
	}

}