class VulnerabilityController implements Constants {

	@ApiOperation(httpMethod = "POST", value = "Get the cartesian product of vulnerability occureneces of asset and details of assets in an asset group. Filter can receive 'severitylevel' as comma seperated."
			+ "'searchtext' can be used to get  the sorted result based on the search text provided.Sample request: {\"ag\":\"pacman\",\"filter\":{\"severitylevel\":\"3,5\"},\"from\":0,\"searchtext\":\"\",\"size\":25}")
	@PostMapping(value = "/v1/vulnerabilities/occurrences")
	public ResponseEntity<Object> getVulnerabilitiesOccurrences(@RequestBody(required = true) Request request) {

		ResponseWithCount response = null;
		String assetGroup = request.getAg();
		if (Strings.isNullOrEmpty(assetGroup)) {
			return ResponseUtils.buildFailureResponse(new Exception(ASSET_MANDATORY));
		}
		String searchText = request.getSearchtext();
		int from = request.getFrom();
		int size = request.getSize();
		List<String> severitiesList = new ArrayList<>();
		List<String> severities = new ArrayList<>();
		boolean allMatch = false;
		severitiesList.add("3");
		severitiesList.add("4");
		severitiesList.add("5");

		if (from < 0) {
			return ResponseUtils.buildFailureResponse(new Exception("From should not be a negative number"));
		}
		Map<String, String> filter = request.getFilter();
		Map<String, String> severityfilter = new HashMap<>();
		String applicationFilter = null;
		String environmentFilter = null;
		for (Map.Entry<String, String> entry : filter.entrySet()) {
			if (entry.getKey().equals("severitylevel")) {
				severityfilter.put(entry.getKey(), entry.getValue());
			} else if (entry.getKey().equals("tags.Application.keyword")) {
				applicationFilter = entry.getValue();
			} else if (entry.getKey().equals("tags.Environment.keyword")) {
				environmentFilter = entry.getValue();
			}
		}

		if (severityfilter.size() > 0) {

			for (Map.Entry<String, String> entry : severityfilter.entrySet()) {
				severities = Arrays.asList(entry.getValue().split(","));
			}
			allMatch = severities.stream().allMatch(severitiesList::contains);
			if (allMatch != true) {
				return ResponseUtils.buildFailureResponse(new Exception("Severity level can only be 3,4 or 5"));
			}

		}
		if (!(Optional.ofNullable(size).isPresent()) && size != 0) {
			from = 0;
			size = 0;
		}

		try {
			int vulnerabilityOccuranceListCount = vulnerabilityService.vulnerabilityAssetCount(assetGroup,
					severityfilter, applicationFilter, environmentFilter, from, size);
			List<Map<String, Object>> vulnerabilityOccuranceList = vulnerabilityService
					.getAllVulnerabilitiesDetailsByAssetGroup(assetGroup, severityfilter,applicationFilter, environmentFilter, searchText, from, size);

			if (vulnerabilityOccuranceList.isEmpty()) {
				return ResponseUtils
						.buildSucessResponse(new ResponseWithCount(new ArrayList<Map<String, Object>>(), 0));
			}

			response = new ResponseWithCount(vulnerabilityOccuranceList, vulnerabilityOccuranceListCount);

		} catch (Exception e) {
			LOGGER.error(EXE_VULN, e);
			return ResponseUtils.buildFailureResponse(e);
		}

		return ResponseUtils.buildSucessResponse(response);
	}

}