class SSMAgentCheckRule extends BaseRule {

	public RuleResult execute(final Map<String, String> ruleParam,
			Map<String, String> resourceAttributes) {

		logger.debug("========SSMAgentCheckRule started=========");
		Annotation annotation = null;
		String resourceId = null;
		String accountId = null;
		String region = null;
		String severity = ruleParam.get(PacmanRuleConstants.SEVERITY);
		String online = ruleParam.get(PacmanRuleConstants.ONLINE);
		String category = ruleParam.get(PacmanRuleConstants.CATEGORY);
		String ssmAgentWithInstanceUrl = null;
		
		String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_SSM_WITH_INSTANCE_URL);

		String firstDiscoveredOn = resourceAttributes.get(PacmanRuleConstants.FIRST_DISCOVERED_ON);
        String discoveredDaysRange = ruleParam.get(PacmanRuleConstants.DISCOVERED_DAYS_RANGE);
        if (!StringUtils.isNullOrEmpty(firstDiscoveredOn)) {
            firstDiscoveredOn = firstDiscoveredOn.substring(0,firstDiscoveredOn.length() - 3);
        }

        
        if(!StringUtils.isNullOrEmpty(formattedUrl)){
            ssmAgentWithInstanceUrl =  formattedUrl;
        }
		
		String desc = null;
		MDC.put("executionId", ruleParam.get("executionId"));
		MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID));
		List<LinkedHashMap<String, Object>> issueList = new ArrayList<>();
		LinkedHashMap<String, Object> issue = new LinkedHashMap<>();
		if (!PacmanUtils.doesAllHaveValue(severity, category,
				ssmAgentWithInstanceUrl,discoveredDaysRange)) {
            logger.info(PacmanRuleConstants.MISSING_CONFIGURATION);
            throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION);
		}
		if(null != resourceAttributes && PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATE_NAME))) {
		    if(PacmanUtils.calculateLaunchedDuration(firstDiscoveredOn)>Long.parseLong(discoveredDaysRange)){
			resourceId = StringUtils.trim(resourceAttributes
					.get(PacmanSdkConstants.RESOURCE_ID));
			accountId = StringUtils.trim(resourceAttributes
					.get(PacmanRuleConstants.ACCOUNTID));
			region = StringUtils.trim(resourceAttributes
					.get(PacmanRuleConstants.REGION_ATTR));
			boolean isSSMAgentOnline = false;
			try {
				isSSMAgentOnline = PacmanUtils.checkSSMAgent(resourceId,
						ssmAgentWithInstanceUrl,
						PacmanRuleConstants.INSTANCEID, region, accountId,
						online);
			} catch (Exception e) {
				logger.error("unable to determine" , e);
				throw new RuleExecutionFailedExeption("unable to determine" + e);
			}
			if (!isSSMAgentOnline) {
				desc = "On EC2 Instance " + "<b>" + resourceId + "</b>"
						+ " SSM Agent is not installed/online";
				annotation = Annotation.buildAnnotation(ruleParam,
						Annotation.Type.ISSUE);
				annotation.put(PacmanSdkConstants.DESCRIPTION, desc);
				annotation.put(PacmanRuleConstants.SEVERITY, severity);
				annotation.put(PacmanRuleConstants.CATEGORY, category);
				issue.put(PacmanRuleConstants.VIOLATION_REASON, desc);
				issueList.add(issue);
				annotation.put("issueDetails", issueList.toString());
				logger.debug("========SSMAgentCheckRule ended with an annotation {} : =========", annotation);
				return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,
						PacmanRuleConstants.FAILURE_MESSAGE, annotation);
			}
		}
		}
		logger.debug("========SSMAgentCheckRule ended=========");
		return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,
				PacmanRuleConstants.SUCCESS_MESSAGE);
	}

}