class AmazonEC2ReservedInstanceLeaseExpirationRule extends BaseRule {

	@Override
	public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) {
		
		logger.debug("========AmazonEC2ReservedInstanceLeaseExpirationRule 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 = ruleParam.get(PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM);
		
		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> amazonEc2Map = new HashMap<>();
			try {
				amazonEc2Map = PacmanUtils.getAmazonEC2ReservedInstanceLeaseExpiration(checkId,resourceId,serviceEsURL,null,accountId);
			} catch (Exception e) {
				logger.error("unable to determine",e);
				throw new RuleExecutionFailedExeption("unable to determine"+e);
			}
			if (!amazonEc2Map.isEmpty()) {
				annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE);
				annotation.put(PacmanSdkConstants.DESCRIPTION,"Amazon EC2 Reserved instances that are scheduled to expire within the next 30 days or have expired in the preceding 30 days is found!!");
				annotation.put(PacmanRuleConstants.SEVERITY, severity);
				annotation.put(PacmanRuleConstants.CATEGORY, category);
				annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, amazonEc2Map.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS));
				annotation.put("instanceCount", amazonEc2Map.get("instanceCount"));
				annotation.put("reason", amazonEc2Map.get("reason"));
				
				annotation.put("status", amazonEc2Map.get("status"));
				annotation.put("platform", amazonEc2Map.get("platform"));
				annotation.put("currentMonthlyCost", amazonEc2Map.get("currentMonthlyCost"));
				annotation.put("expirationDate", amazonEc2Map.get("expirationDate"));
				annotation.put("instanceType", amazonEc2Map.get("instanceType"));
				annotation.put("zone", amazonEc2Map.get("zone"));
				
				issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon EC2 Reserved instances that are scheduled to expire within the next 30 days or have expired in the preceding 30 days is found!!");
				issue.put(PacmanRuleConstants.CHECKID, checkId);
				issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor");
				issueList.add(issue);
				annotation.put("issueDetails",issueList.toString());
				
				logger.debug("========AmazonEC2ReservedInstanceLeaseExpirationRule ended with an annotation {} : =========", annotation);
				return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation);
			}
			}
		logger.debug("========AmazonEC2ReservedInstanceLeaseExpirationRule ended=========");
		return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE);
	}

}