class Ec2PublicAccessPortWithS5vulnerabilitiesRule extends BaseRule {

	public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) {
		logger.debug("========Ec2PublicAccessPortWithS5vulnerabilitiesRule started=========");
		Annotation annotation = null;
		String instanceId = null;
		
		String severity = ruleParam.get(PacmanRuleConstants.SEVERITY);
		String category = ruleParam.get(PacmanRuleConstants.CATEGORY);
		String ec2PubAccessPortUrl = null;
		String ec2WithVulnInfoForS5Url = null;
		String ec2PortRuleId = ruleParam.get(PacmanRuleConstants.EC2_PORT_RULE_ID);
		String severityVulnValue = ruleParam.get(PacmanRuleConstants.SEVERITY_VULN);
		String pacmanHost = PacmanUtils.getPacmanHost(PacmanRuleConstants.ES_URI);
        
        if(!StringUtils.isNullOrEmpty(pacmanHost)){
            ec2WithVulnInfoForS5Url =  ruleParam.get(PacmanRuleConstants.ES_EC2_WITH_VULN_INFO_S5_URL);
            ec2PubAccessPortUrl = ruleParam.get(PacmanRuleConstants.ES_EC2_PUB_ACC_PORT_URL);
            ec2WithVulnInfoForS5Url = pacmanHost+ec2WithVulnInfoForS5Url;
            ec2PubAccessPortUrl = pacmanHost+ec2PubAccessPortUrl;
        }
		
		String publicIp=null;
		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(severity,category,ec2PubAccessPortUrl,ec2WithVulnInfoForS5Url,ec2PortRuleId,severityVulnValue)) {
			logger.info(PacmanRuleConstants.MISSING_CONFIGURATION);
			throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION);
		}

		if (resourceAttributes != null) {
			instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.INSTANCEID));
			publicIp   = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.PUBLIC_IP_ADDR));
			boolean isInstanceExists = false;
			try {
				isInstanceExists = PacmanUtils.checkInstanceIdForPortRuleInES(instanceId,ec2PubAccessPortUrl,ec2PortRuleId,"");
				if (isInstanceExists) {
					List<String> severityList = PacmanUtils.getSeverityVulnerabilitiesByInstanceId(instanceId,ec2WithVulnInfoForS5Url,severityVulnValue);
					if(!severityList.isEmpty()){
					annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE);
					annotation.put(PacmanSdkConstants.DESCRIPTION,"An Ec2 instance with remotely exploitable vulnerability (S5) is open to internet found!");
					annotation.put(PacmanRuleConstants.SEVERITY, severity);
					annotation.put(PacmanRuleConstants.CATEGORY, category);
					issue.put(PacmanRuleConstants.VIOLATION_REASON, "ResourceId " + instanceId + " remotely exploitable vulnerability (S5) is open to internet found");
					if(!org.apache.commons.lang.StringUtils.isEmpty(publicIp)){
						issue.put("public_ip", publicIp);
					}else{
						issue.put("public_ip", "Not found");	
					}
					issue.put("voilation_title", String.join(",", severityList));
					issueList.add(issue);
					annotation.put("issueDetails",issueList.toString());
					logger.debug("========Ec2PublicAccessPortWithS5vulnerabilitiesRule ended with an annotation {} : =========",annotation);
					return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation);
					
				}
			}
			} catch (Exception e) {
				logger.error("error", e);
				throw new RuleExecutionFailedExeption(e.getMessage());
			}
			
		}
		logger.debug("========Ec2PublicAccessPortWithS5vulnerabilitiesRule ended=========");
		return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE);
	}

}