class AccessKeyRotatedRule extends BaseRule {

	public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) {
		logger.debug("========AccessKeyRotatedRule started=========");
		Map<String, String> temp = new HashMap<>();
		temp.putAll(ruleParam);
		temp.put("region", "us-west-2");
		
		Map<String, Object> map = null;
		AmazonIdentityManagementClient iamClient = null;
		String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING);
		String userName = ruleParam.get(PacmanSdkConstants.RESOURCE_ID);
		
		String severity = ruleParam.get(PacmanRuleConstants.SEVERITY);
		String category = ruleParam.get(PacmanRuleConstants.CATEGORY);
		
		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)) {
			logger.info(PacmanRuleConstants.MISSING_CONFIGURATION);
			throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION);
		}
		
		Annotation annotation = null;		
		List<AccessKeyMetadata> accessKeyMetadatas;
		String message =  null;
		try {
			map = getClientFor(AWSService.IAM, roleIdentifyingString, temp);
			iamClient = (AmazonIdentityManagementClient) map.get(PacmanSdkConstants.CLIENT);
			
			 accessKeyMetadatas = IAMUtils.getAccessKeyInformationForUser(userName, iamClient);
		        if(!CollectionUtils.isNullOrEmpty(accessKeyMetadatas)){
		            if(anyAccessKeysNotRotatedForLong(accessKeyMetadatas)){
		                
		                message = "Iam access keys for " + userName + " are NOT ROTATED for more than "+PacmanRuleConstants.ACCESSKEY_ROTATION_DURATION+" days";
		                logger.info(message);       
		                
		                annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE);
		                annotation.put(PacmanSdkConstants.DESCRIPTION,"access keys not rotated for more than " +PacmanRuleConstants.ACCESSKEY_ROTATION_DURATION+" days found");
		                annotation.put(PacmanRuleConstants.SEVERITY, severity);
		                annotation.put(PacmanRuleConstants.CATEGORY, category);
		                
		                issue.put(PacmanRuleConstants.VIOLATION_REASON, "access keys not rotated for more than " +PacmanRuleConstants.ACCESSKEY_ROTATION_DURATION+" days found");
		                issueList.add(issue);
		                annotation.put("issueDetails",issueList.toString());
		                logger.debug("========AccessKeyRotatedRule ended with annotation {} :=========",annotation);
		                return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE,annotation);
		            }else{
		                logger.info(userName,"Access key is already rotated for username ");
		            }
		        } else{
		            logger.info(userName,"Access key metadata is empty for username ");
		        }
		} catch (UnableToCreateClientException e) {
			logger.error("unable to get client for following input", e);
			throw new InvalidInputException(e.toString());
		}	
		
		logger.debug("========AccessKeyRotatedRule ended=========");
		return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE);
	}

}