class AmazonRDSIdleDBInstancesRule extends BaseRule {

	@Override
	public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) {
		
		logger.debug("========AmazonRDSIdleDBInstancesRule started=========");
		Annotation annotation = null;
		String resourceId = null;
		String region = 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 formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM);
        
        if(!StringUtils.isNullOrEmpty(formattedUrl)){
            serviceEsURL =  formattedUrl;
        }
		
		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) {
			region = resourceAttributes.get(PacmanRuleConstants.REGION);
			accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID);
			resourceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.DB_INSTANCE_IDENTIFIER));
			Map<String, String> rdsIdleDbMap = new HashMap<>();
			try {
				rdsIdleDbMap = PacmanUtils.getRDSDetailsForCheckId(checkId,resourceId,serviceEsURL,region,accountId);
			} catch (Exception e) {
				logger.error("unable to determine",e);
				throw new RuleExecutionFailedExeption("unable to determine"+e);
			}
			if (!rdsIdleDbMap.isEmpty()) {
				annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE);
				annotation.put(PacmanSdkConstants.DESCRIPTION,"Amazon RDS idle DB instance found !!");
				annotation.put(PacmanRuleConstants.SEVERITY, severity);
				annotation.put(PacmanRuleConstants.CATEGORY, category);
				annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, rdsIdleDbMap.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS));
				annotation.put(PacmanRuleConstants.INSTANCETYPE, rdsIdleDbMap.get(PacmanRuleConstants.INSTANCETYPE));
				annotation.put("multi-AZ", rdsIdleDbMap.get("multi-AZ"));
				annotation.put("storageProvisioned", rdsIdleDbMap.get("storageProvisioned"));
				annotation.put("daysSinceLastConnection", rdsIdleDbMap.get("daysSinceLastConnection"));
				
				issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon RDS idle DB instance found");
				issue.put(PacmanRuleConstants.CHECKID, checkId);
				issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor");
				issueList.add(issue);
				
				annotation.put("issueDetails",issueList.toString());
				
				logger.debug("========AmazonRDSIdleDBInstancesRule ended with an annotation {} :=========",annotation);
				return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation);
			}
			}
		logger.debug("========AmazonRDSIdleDBInstancesRule ended=========");
		return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE);
	}

}