From de97424b0bfa5d289ecff841749d460ce692fbc0 Mon Sep 17 00:00:00 2001 From: Kanchana Date: Wed, 6 Mar 2019 13:29:29 +0530 Subject: [PATCH] Tagging count missmatch issue --- .../repository/TaggingRepositoryImpl.java | 547 +++++----- .../service/TaggingServiceImpl.java | 976 +++++++++--------- 2 files changed, 764 insertions(+), 759 deletions(-) diff --git a/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/repository/TaggingRepositoryImpl.java b/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/repository/TaggingRepositoryImpl.java index a5a7a16a5..7296e35a8 100644 --- a/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/repository/TaggingRepositoryImpl.java +++ b/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/repository/TaggingRepositoryImpl.java @@ -1,272 +1,275 @@ -/******************************************************************************* - * Copyright 2018 T Mobile, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - ******************************************************************************/ -package com.tmobile.pacman.api.compliance.repository; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.annotation.PostConstruct; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Repository; - -import com.google.common.base.Strings; -import com.google.common.collect.HashMultimap; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.tmobile.pacman.api.commons.Constants; -import com.tmobile.pacman.api.commons.exception.DataException; -import com.tmobile.pacman.api.commons.repo.ElasticSearchRepository; -import com.tmobile.pacman.api.commons.repo.PacmanRdsRepository; -import com.tmobile.pacman.api.commons.utils.CommonUtils; -import com.tmobile.pacman.api.commons.utils.PacHttpUtils; -import com.tmobile.pacman.api.compliance.client.AssetServiceClient; -import com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest; -import com.tmobile.pacman.api.compliance.service.ComplianceService; - -/** - * The Class TaggingRepositoryImpl. - */ -@Repository -public class TaggingRepositoryImpl implements TaggingRepository, Constants { - - /** The logger. */ - protected final Log logger = LogFactory.getLog(getClass()); - - /** The elastic search repository. */ - @Autowired - private ElasticSearchRepository elasticSearchRepository; - - /** The rdsepository. */ - @Autowired - private PacmanRdsRepository rdsepository; - - /** The asset service client. */ - @Autowired - private AssetServiceClient assetServiceClient; - - /** The compliance service client. */ - @Autowired - private ComplianceService complianceServiceClient; - - /** The complaince repository. */ - @Autowired - private ComplianceRepository complainceRepository; - - /** The mandatory tags. */ - @Value("${tagging.mandatoryTags}") - private String mandatoryTags; - - /** The es host. */ - @Value("${elastic-search.host}") - private String esHost; - - /** The es port. */ - @Value("${elastic-search.port}") - private int esPort; - - /** The Constant PROTOCOL. */ - static final String PROTOCOL = "http"; - - /** The es url. */ - private String esUrl; - - /** The page size. */ - private Integer pageSize = TEN_THOUSAND; - - /** - * Inits the. - */ - @PostConstruct - void init() { - esUrl = PROTOCOL + "://" + esHost + ":" + esPort; - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getUntaggedIssuesByapplicationFromES(java.lang.String, java.lang.String, java.lang.String, int, int) - */ - public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, - String mandatoryTags, String searchText, int from, int size) - throws DataException { - List mandatoryTagsList = Arrays - .asList(mandatoryTags.split(",")); - String responseJson = null; - JsonParser jsonParser; - JsonObject resultJson; - StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") - .append(assetGroup).append("/").append(SEARCH); - StringBuilder requestBody = new StringBuilder( - "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); - if (!Strings.isNullOrEmpty(searchText)) { - requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" - + searchText + "\"" + "}}"); - } - - requestBody - .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" - + pageSize - + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); - - for (String tags : mandatoryTagsList) { - if (!"Application".equalsIgnoreCase(tags)) { - requestBody.append("\"" + tags + "\"" - + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" - + "\"" + tags + "\"" + "}}]}},"); - - } - } - requestBody.setLength(requestBody.length() - 1); - requestBody.append("}}}}}}}"); - try{ - responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), - requestBody.toString()); - } catch (Exception e) { - throw new DataException(e); - } - jsonParser = new JsonParser(); - resultJson = (JsonObject) jsonParser.parse(responseJson); - JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( - AGGREGATIONS).toString()); - return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getRuleParamsFromDbByPolicyId(java.lang.String) - */ - public List> getRuleParamsFromDbByPolicyId( - String policyId) throws DataException { - String ruleIdQuery = "SELECT rule.ruleParams FROM cf_RuleInstance rule LEFT JOIN cf_Policy policy ON rule.policyId = policy.policyId WHERE rule.status = 'ENABLED' AND policy.policyId ='" - + policyId + "' GROUP BY rule.policyId"; - return rdsepository.getDataFromPacman(ruleIdQuery); - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getUntaggedIssues(java.lang.String, java.lang.String) - */ - public Long getUntaggedIssues(String assetGroup, String mandatoryTag) - throws DataException { - Map mustFilter = new HashMap<>(); - Map mustNotFilter = new HashMap<>(); - Map mustTermsFilter = new HashMap<>(); - HashMultimap shouldFilter = HashMultimap.create(); - Map> matchPhrasePrefix = new HashMap<>(); - List mandatoryTagsList = new ArrayList<>(); - if (mandatoryTag != null) { - mandatoryTagsList.add(mandatoryTag); - } - mustFilter.put(CommonUtils.convertAttributetoKeyword(TYPE), ISSUE); - mustFilter.put(CommonUtils.convertAttributetoKeyword(POLICYID), - TAGGIG_POLICY); - matchPhrasePrefix.put(MISSING_TAGS, mandatoryTagsList); - shouldFilter.put(CommonUtils.convertAttributetoKeyword(ISSUE_STATUS), - OPEN); - try{ - return elasticSearchRepository - .getTotalDistributionForIndexAndTypeWithMatchPhrase(assetGroup, - null, mustFilter, mustNotFilter, shouldFilter, null, - mustTermsFilter, matchPhrasePrefix); - } catch (Exception e) { - throw new DataException(e); - } - } - - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getRuleTargetTypesFromDbByPolicyId(java.lang.String) - */ - public List> getRuleTargetTypesFromDbByPolicyId( - String policyId) throws DataException { - String ruleIdQuery = "SELECT rule.targetType FROM cf_RuleInstance rule LEFT JOIN cf_Policy policy ON rule.policyId = policy.policyId WHERE rule.status = 'ENABLED' AND policy.policyId ='" - + policyId + "'"; - return rdsepository.getDataFromPacman(ruleIdQuery); - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getUntaggedTargetTypeIssues(com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest, java.util.List) - */ - public String getUntaggedTargetTypeIssues( - UntaggedTargetTypeRequest request, List tagsList) - throws DataException { - String assetGroup = request.getAg(); - StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") - .append(assetGroup).append("/").append(SEARCH); - StringBuilder requestBody = null; - String body = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"" - + TAGGIG_POLICY - + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}]"; - - if (!tagsList.isEmpty()) { - body = body + ",\"should\":["; - /* filtering by tags */ - for (String tag : tagsList) { - body = body + "{\"match_phrase_prefix\":{\"missingTags\":\"" - + tag + "\"}},"; - } - body = body.substring(0, body.length() - 1); - body = body + "]"; - body = body + ",\"minimum_should_match\":1"; - } - body = body - + "}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"targetType.keyword\",\"size\":1000}}}}"; - requestBody = new StringBuilder(body); - try{ return PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), - requestBody.toString()); - } catch (Exception e) { - logger.error(e.getMessage()); - throw new DataException(e); - } - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getTaggingByApplication(java.lang.String, java.lang.String) - */ - @Override - public String getTaggingByApplication(String ag, String targetType) - throws DataException { - StringBuilder requestBody = new StringBuilder(); - StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( - ag); - if (!StringUtils.isEmpty(targetType)) { - urlToQuery.append("/").append(targetType); - urlToQuery.append("/").append(UNDERSCORE_COUNT); - requestBody - .append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}],\"must_not\":[{\"exists\":{\"field\":\"tags.Application\"}}]}}}"); - } else { - urlToQuery.append("/").append(SEARCH); - requestBody - .append("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}],\"must_not\":[{\"exists\":{\"field\":\"tags.Application\"}}]}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"_entitytype.keyword\",\"size\":" - + TEN_THOUSAND + "}}}}"); - } - - String responseJson = ""; - try { - responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), - requestBody.toString()); - } catch (Exception e) { - logger.error(e.getMessage()); - throw new DataException(e); - } - return responseJson; - } -} +/******************************************************************************* + * Copyright 2018 T Mobile, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ +package com.tmobile.pacman.api.compliance.repository; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.annotation.PostConstruct; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Repository; + +import com.google.common.base.Strings; +import com.google.common.collect.HashMultimap; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.tmobile.pacman.api.commons.Constants; +import com.tmobile.pacman.api.commons.exception.DataException; +import com.tmobile.pacman.api.commons.repo.ElasticSearchRepository; +import com.tmobile.pacman.api.commons.repo.PacmanRdsRepository; +import com.tmobile.pacman.api.commons.utils.CommonUtils; +import com.tmobile.pacman.api.commons.utils.PacHttpUtils; +import com.tmobile.pacman.api.compliance.client.AssetServiceClient; +import com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest; +import com.tmobile.pacman.api.compliance.service.ComplianceService; + +/** + * The Class TaggingRepositoryImpl. + */ +@Repository +public class TaggingRepositoryImpl implements TaggingRepository, Constants { + + /** The logger. */ + protected final Log logger = LogFactory.getLog(getClass()); + + /** The elastic search repository. */ + @Autowired + private ElasticSearchRepository elasticSearchRepository; + + /** The rdsepository. */ + @Autowired + private PacmanRdsRepository rdsepository; + + /** The asset service client. */ + @Autowired + private AssetServiceClient assetServiceClient; + + /** The compliance service client. */ + @Autowired + private ComplianceService complianceServiceClient; + + /** The complaince repository. */ + @Autowired + private ComplianceRepository complainceRepository; + + /** The mandatory tags. */ + @Value("${tagging.mandatoryTags}") + private String mandatoryTags; + + /** The es host. */ + @Value("${elastic-search.host}") + private String esHost; + + /** The es port. */ + @Value("${elastic-search.port}") + private int esPort; + + /** The Constant PROTOCOL. */ + static final String PROTOCOL = "http"; + + /** The es url. */ + private String esUrl; + + /** The page size. */ + private Integer pageSize = TEN_THOUSAND; + + /** + * Inits the. + */ + @PostConstruct + void init() { + esUrl = PROTOCOL + "://" + esHost + ":" + esPort; + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getUntaggedIssuesByapplicationFromES(java.lang.String, java.lang.String, java.lang.String, int, int) + */ + public JsonArray getUntaggedIssuesByapplicationFromES(String assetGroup, + String mandatoryTags, String searchText, int from, int size) + throws DataException { + List mandatoryTagsList = new ArrayList<>(); + if(!com.amazonaws.util.StringUtils.isNullOrEmpty(mandatoryTags)){ + mandatoryTagsList = Arrays + .asList(mandatoryTags.split(",")); + } + String responseJson = null; + JsonParser jsonParser; + JsonObject resultJson; + StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") + .append(assetGroup).append("/").append(SEARCH); + StringBuilder requestBody = new StringBuilder( + "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"PacMan_TaggingRule_version-1\"}}}"); + if (!Strings.isNullOrEmpty(searchText)) { + requestBody.append("," + "{\"match_phrase_prefix\":{\"_all\":\"" + + searchText + "\"" + "}}"); + } + + requestBody + .append("],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + + pageSize + + "},\"aggs\":{\"tags\":{\"filters\":{\"filters\":{"); + + for (String tags : mandatoryTagsList) { + if (!"Application".equalsIgnoreCase(tags)) { + requestBody.append("\"" + tags + "\"" + + ":{\"bool\":{\"must\":[{\"match\":{\"missingTags\":" + + "\"" + tags + "\"" + "}}]}},"); + + } + } + requestBody.setLength(requestBody.length() - 1); + requestBody.append("}}}}}}}"); + try{ + responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), + requestBody.toString()); + } catch (Exception e) { + throw new DataException(e); + } + jsonParser = new JsonParser(); + resultJson = (JsonObject) jsonParser.parse(responseJson); + JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( + AGGREGATIONS).toString()); + return aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS); + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getRuleParamsFromDbByPolicyId(java.lang.String) + */ + public List> getRuleParamsFromDbByPolicyId( + String policyId) throws DataException { + String ruleIdQuery = "SELECT rule.ruleParams FROM cf_RuleInstance rule LEFT JOIN cf_Policy policy ON rule.policyId = policy.policyId WHERE rule.status = 'ENABLED' AND policy.policyId ='" + + policyId + "' GROUP BY rule.policyId"; + return rdsepository.getDataFromPacman(ruleIdQuery); + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getUntaggedIssues(java.lang.String, java.lang.String) + */ + public Long getUntaggedIssues(String assetGroup, String mandatoryTag) + throws DataException { + Map mustFilter = new HashMap<>(); + Map mustNotFilter = new HashMap<>(); + Map mustTermsFilter = new HashMap<>(); + HashMultimap shouldFilter = HashMultimap.create(); + Map> matchPhrasePrefix = new HashMap<>(); + List mandatoryTagsList = new ArrayList<>(); + if (mandatoryTag != null) { + mandatoryTagsList.add(mandatoryTag); + } + mustFilter.put(CommonUtils.convertAttributetoKeyword(TYPE), ISSUE); + mustFilter.put(CommonUtils.convertAttributetoKeyword(POLICYID), + TAGGIG_POLICY); + matchPhrasePrefix.put(MISSING_TAGS, mandatoryTagsList); + shouldFilter.put(CommonUtils.convertAttributetoKeyword(ISSUE_STATUS), + OPEN); + try{ + return elasticSearchRepository + .getTotalDistributionForIndexAndTypeWithMatchPhrase(assetGroup, + null, mustFilter, mustNotFilter, shouldFilter, null, + mustTermsFilter, matchPhrasePrefix); + } catch (Exception e) { + throw new DataException(e); + } + } + + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getRuleTargetTypesFromDbByPolicyId(java.lang.String) + */ + public List> getRuleTargetTypesFromDbByPolicyId( + String policyId) throws DataException { + String ruleIdQuery = "SELECT rule.targetType FROM cf_RuleInstance rule LEFT JOIN cf_Policy policy ON rule.policyId = policy.policyId WHERE rule.status = 'ENABLED' AND policy.policyId ='" + + policyId + "'"; + return rdsepository.getDataFromPacman(ruleIdQuery); + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getUntaggedTargetTypeIssues(com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest, java.util.List) + */ + public String getUntaggedTargetTypeIssues( + UntaggedTargetTypeRequest request, List tagsList) + throws DataException { + String assetGroup = request.getAg(); + StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") + .append(assetGroup).append("/").append(SEARCH); + StringBuilder requestBody = null; + String body = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"policyId.keyword\":{\"value\":\"" + + TAGGIG_POLICY + + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}]"; + + if (!tagsList.isEmpty()) { + body = body + ",\"should\":["; + /* filtering by tags */ + for (String tag : tagsList) { + body = body + "{\"match_phrase_prefix\":{\"missingTags\":\"" + + tag + "\"}},"; + } + body = body.substring(0, body.length() - 1); + body = body + "]"; + body = body + ",\"minimum_should_match\":1"; + } + body = body + + "}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"targetType.keyword\",\"size\":1000}}}}"; + requestBody = new StringBuilder(body); + try{ return PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), + requestBody.toString()); + } catch (Exception e) { + logger.error(e.getMessage()); + throw new DataException(e); + } + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.repository.TaggingRepository#getTaggingByApplication(java.lang.String, java.lang.String) + */ + @Override + public String getTaggingByApplication(String ag, String targetType) + throws DataException { + StringBuilder requestBody = new StringBuilder(); + StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( + ag); + if (!StringUtils.isEmpty(targetType)) { + urlToQuery.append("/").append(targetType); + urlToQuery.append("/").append(UNDERSCORE_COUNT); + requestBody + .append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}],\"must_not\":[{\"exists\":{\"field\":\"tags.Application\"}}]}}}"); + } else { + urlToQuery.append("/").append(SEARCH); + requestBody + .append("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}],\"must_not\":[{\"exists\":{\"field\":\"tags.Application\"}}]}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"_entitytype.keyword\",\"size\":" + + TEN_THOUSAND + "}}}}"); + } + + String responseJson = ""; + try { + responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), + requestBody.toString()); + } catch (Exception e) { + logger.error(e.getMessage()); + throw new DataException(e); + } + return responseJson; + } +} diff --git a/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/service/TaggingServiceImpl.java b/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/service/TaggingServiceImpl.java index 1fc59d6b9..4ff85241e 100644 --- a/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/service/TaggingServiceImpl.java +++ b/api/pacman-api-compliance/src/main/java/com/tmobile/pacman/api/compliance/service/TaggingServiceImpl.java @@ -1,487 +1,489 @@ -/******************************************************************************* - * Copyright 2018 T Mobile, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - ******************************************************************************/ -package com.tmobile.pacman.api.compliance.service; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -import com.google.common.collect.HashMultimap; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.tmobile.pacman.api.commons.Constants; -import com.tmobile.pacman.api.commons.exception.DataException; -import com.tmobile.pacman.api.commons.exception.ServiceException; -import com.tmobile.pacman.api.commons.repo.ElasticSearchRepository; -import com.tmobile.pacman.api.compliance.domain.Request; -import com.tmobile.pacman.api.compliance.domain.ResponseWithCount; -import com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest; -import com.tmobile.pacman.api.compliance.repository.ComplianceRepository; -import com.tmobile.pacman.api.compliance.repository.TaggingRepository; - -/** - * The Class TaggingServiceImpl. - */ -@Service -public class TaggingServiceImpl implements TaggingService, Constants { - - /** The mandatory tags. */ - @Value("${tagging.mandatoryTags}") - private String mandatoryTags; - - /** The repository. */ - @Autowired - private TaggingRepository repository; - - /** The complaince repository. */ - @Autowired - private ComplianceRepository complainceRepository; - - /** The elastic search repository. */ - @Autowired - private ElasticSearchRepository elasticSearchRepository; - - /** The logger. */ - protected final Log logger = LogFactory.getLog(getClass()); - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.service.TaggingService#getUnTaggedAssetsByApplication(com.tmobile.pacman.api.compliance.domain.Request) - */ - @Override - public ResponseWithCount getUnTaggedAssetsByApplication(Request request) throws ServiceException { - String assetGroup = request.getAg(); - String searchText = request.getSearchtext(); - int from = request.getFrom(); - int size = request.getSize(); - - List> masterDetailList = new ArrayList<>(); - LinkedHashMap app; - JsonArray buckets; - try { - buckets = repository.getUntaggedIssuesByapplicationFromES(assetGroup, getMandatoryTags(), searchText, from, - size); - } catch (DataException e) { - throw new ServiceException(e); - } - JsonObject tagsObj = null; - JsonObject bucketsTags = null; - JsonObject tagsJson = null; - - JsonElement tagsDocId = null; - String tags = null; - String tagName = null; - int total = 0; - for (int i = 0; i < buckets.size(); i++) { - total = buckets.size(); - app = new LinkedHashMap<>(); - app.put("application", buckets.get(i).getAsJsonObject().get("key").getAsString()); - tagsObj = (JsonObject) buckets.get(i).getAsJsonObject().get("tags"); - bucketsTags = (JsonObject) tagsObj.get("buckets"); - Iterator it = bucketsTags.keySet().iterator(); - - while (it.hasNext()) { - tags = it.next(); - tagsJson = (JsonObject) bucketsTags.get(tags); - tagsDocId = tagsJson.get("doc_count"); - StringBuilder sb = new StringBuilder(tags); - sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); - tagName = sb.toString(); - app.put(tagName + "Untagged", tagsDocId.getAsLong()); - } - masterDetailList.add(app); - } - - if (masterDetailList.isEmpty()) { - throw new ServiceException(NO_DATA_FOUND); - } - - if (from >= masterDetailList.size()) { - throw new ServiceException("From exceeds the size of list"); - } - - int endIndex = 0; - - if ((from + size) > masterDetailList.size()) { - endIndex = masterDetailList.size(); - } else { - endIndex = from + size; - } - List> subDetailList; - - if (from == 0 && size == 0) { - subDetailList = masterDetailList; - } else { - subDetailList = masterDetailList.subList(from, endIndex); - } - if (buckets.size() > 0) { - return new ResponseWithCount(subDetailList, total); - } else { - throw new ServiceException("No records found!!"); - } - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.service.TaggingService#getTaggingSummary(java.lang.String) - */ - public Map getTaggingSummary(String assetGroup) throws ServiceException { - List mandatoryTagsList = new ArrayList<>(); - if (!StringUtils.isEmpty(getMandatoryTags())) { - mandatoryTagsList = Arrays.asList(getMandatoryTags().split(",")); - } - Map totalMap = new HashMap<>(); - List> unTagsList = new ArrayList<>(); - Map tagMap; - try { - tagMap = complainceRepository.getTagging(assetGroup, null); - } catch (DataException e) { - throw new ServiceException(e); - } - unTagsList = getUnTaggedListData(unTagsList, assetGroup, mandatoryTagsList, tagMap); - totalMap.put("untaggedList", unTagsList); - totalMap.put("overallCompliance", tagMap.get("compliance")); - - return totalMap; - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.service.TaggingService#getUntaggingByTargetTypes(com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest) - */ - public List> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) - throws ServiceException { - long assetCount ; - long untaggedCount ; - long taggedCount ; - double compliancePercentage; - String type; - - List tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); - List> targetTypes; - try { - targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); - } catch (DataException e) { - throw new ServiceException(e); - } - if (targetTypes.isEmpty()) { - throw new ServiceException(NO_DATA_FOUND); - } - - Map filterTags = request.getFilter(); - - List> unTagsList = new ArrayList<>(); - Map assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null); - Map untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); - // process records to format the response - for (Map targetType : targetTypes) { - type = targetType.get(TARGET_TYPE).toString(); - - Map data = new HashMap<>(); - if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() - && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { - continue; - } - assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) - : 0l; - if (assetCount > 0) { - untaggedCount = getUntaggedAsset(untaggedCountMap, type); - if (untaggedCount > assetCount) { - untaggedCount = assetCount; - } - taggedCount = assetCount - untaggedCount; - compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; - compliancePercentage = Math.floor(compliancePercentage); - - data.put("name", type); - data.put("untagged", untaggedCount); - data.put("tagged", taggedCount); - data.put("assetCount", assetCount); - data.put(COMP_PERCENTAGE, compliancePercentage); - unTagsList.add(data); - } - } - if (unTagsList.isEmpty()) { - throw new ServiceException(NO_DATA_FOUND); - } - return unTagsList; - } - - /* (non-Javadoc) - * @see com.tmobile.pacman.api.compliance.service.TaggingService#taggingByApplication(java.lang.String, java.lang.String) - */ - @Override - public List> taggingByApplication(String assetGroup, String targetType) throws ServiceException { - List> taggsApplication = new ArrayList<>(); - List tagsList = new ArrayList<>(); - tagsList.add(""); - tagsList.add("unknown"); - Map tagsMap = new HashMap<>(); - Map mustTermsFilter = new HashMap<>(); - String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); - List targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); - Map mustNotFilter = new HashMap<>(); - HashMultimap shouldFilter = HashMultimap.create(); - String aggsEntityType = "_entitytype.keyword"; - mustTermsFilter.put("tags.Application.keyword", tagsList); - Map mustFilter = new HashMap<>(); - mustFilter.put("latest", true); - Map assetWithoutTag; - Map emptyOrUnkownAssets; - Long emptyOrUnkownAssetsLong; - Long assetWithoutTagLong = 0l; - String type = null; - if (!StringUtils.isEmpty(targetType)) { - type = "'" + targetType + "'"; - if (targetTypesList.contains(type)) { - assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); - try { - emptyOrUnkownAssetsLong = elasticSearchRepository - .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, - mustFilter, null, null, null, mustTermsFilter, null, null); - } catch (Exception e) { - throw new ServiceException(e); - } - if (assetWithoutTag.containsKey(targetType)) { - assetWithoutTagLong = assetWithoutTag.get(targetType); - } - tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); - } - } else { - assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); - try { - emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, - mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); - } catch (Exception e) { - throw new ServiceException(e); - } - getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); - } - taggsApplication.add(tagsMap); - return taggsApplication; - } - - /** - * Gets the mandatory tags. - * - * @return the mandatory tags - * @throws ServiceException the service exception - */ - private String getMandatoryTags() throws ServiceException { - String mand = "mandatoryTags"; - char ch = '"'; - String mandTags = ch + "" + mand + "" + ch; - JsonObject paramObj = null; - JsonObject paramDet = null; - JsonArray array = null; - JsonParser parser = new JsonParser(); - List> ruleParams; - try { - ruleParams = repository.getRuleParamsFromDbByPolicyId(TAGGIG_POLICY); - } catch (DataException e) { - throw new ServiceException(e); - } - for (Map params : ruleParams) { - String rParams = params.get("ruleParams").toString(); - paramObj = parser.parse(rParams).getAsJsonObject(); - array = paramObj.get("params").getAsJsonArray(); - - for (JsonElement jsonElement : array) { - - paramDet = jsonElement.getAsJsonObject(); - if (paramDet.get("key").toString().equals(mandTags)) { - mandatoryTags = paramDet.get("value").getAsString(); - } - } - } - return mandatoryTags; - } - - /** - * Gets the untagged asset. - * - * @param bucketMap the bucket map - * @param type the type - * @return the untagged asset - */ - private long getUntaggedAsset(Map bucketMap, String type) { - if (bucketMap.get(type) != null) { - return bucketMap.get(type); - } else { - return 0; - } - } - - /** - * Gets the asset count without application tag. - * - * @param assetGroup the asset group - * @param targetType the target type - * @return the asset count without application tag - * @throws ServiceException the service exception - */ - private Map getAssetCountWithoutApplicationTag(String assetGroup, String targetType) - throws ServiceException { - Map assetWithoutTagsMap = new HashMap<>(); - String responseJson; - try { - responseJson = repository.getTaggingByApplication(assetGroup, targetType); - } catch (DataException e) { - throw new ServiceException(e); - } - JsonParser jsonParser = new JsonParser(); - JsonObject resultJson = jsonParser.parse(responseJson).getAsJsonObject(); - if (StringUtils.isEmpty(targetType)) { - JsonObject aggs = (JsonObject) resultJson.get(AGGREGATIONS); - JsonObject name = (JsonObject) aggs.get("NAME"); - JsonArray buckets = name.get(BUCKETS).getAsJsonArray(); - // convert Json Array to Map object - for (JsonElement bucket : buckets) { - assetWithoutTagsMap.put(bucket.getAsJsonObject().get("key").getAsString(), bucket.getAsJsonObject() - .get(DOC_COUNT).getAsLong()); - } - } else { - if(resultJson.has(COUNT)){ - assetWithoutTagsMap.put(targetType, resultJson.get(COUNT).getAsLong()); - } - } - return assetWithoutTagsMap; - } - - /** - * Gets the untagged target type issues. - * - * @param request the request - * @param tagsList the tags list - * @return the untagged target type issues - * @throws ServiceException the service exception - */ - private Map getUntaggedTargetTypeIssues(UntaggedTargetTypeRequest request, List tagsList) - throws ServiceException { - JsonParser parser = new JsonParser(); - Map untaggedCountMap = new HashMap<>(); - String responseDetails; - try { - responseDetails = repository.getUntaggedTargetTypeIssues(request, tagsList); - } catch (DataException e) { - throw new ServiceException(e); - } - JsonObject responseJson = parser.parse(responseDetails).getAsJsonObject(); - JsonObject aggs = (JsonObject) responseJson.get(AGGREGATIONS); - JsonObject name = (JsonObject) aggs.get("NAME"); - JsonArray buckets = name.get(BUCKETS).getAsJsonArray(); - // convert Json Array to Map object - for (JsonElement bucket : buckets) { - untaggedCountMap.put(bucket.getAsJsonObject().get("key").getAsString(), - bucket.getAsJsonObject().get(DOC_COUNT).getAsLong()); - } - return untaggedCountMap; - } - - /** - * Gets the total assets. - * - * @param tagMap the tag map - * @return the total assets - */ - private long getTotalAssets(Map tagMap){ - Long totalAssets = 0l; - for (Map.Entry asset : tagMap.entrySet()) { - if ("assets".equalsIgnoreCase(asset.getKey())) { - totalAssets = asset.getValue(); - } - } - return totalAssets; - } - - /** - * Gets the un tagged list data. - * - * @param unTagsList the un tags list - * @param assetGroup the asset group - * @param mandatoryTagsList the mandatory tags list - * @param tagMap the tag map - * @return the un tagged list data - * @throws ServiceException the service exception - */ - private List> getUnTaggedListData(List> unTagsList,String assetGroup,List mandatoryTagsList, Map tagMap) throws ServiceException{ - Long docCount; - for (String mandatoryTag : mandatoryTagsList) { - Map data = new HashMap<>(); - Long totalAssets = getTotalAssets(tagMap); - try { - docCount = repository.getUntaggedIssues(assetGroup, mandatoryTag); - } catch (DataException e) { - throw new ServiceException(e); - } - data.put("name", mandatoryTag); - data.put("untagged", docCount); - data.put("tagged", (totalAssets - docCount)); - - if (totalAssets < docCount) { - totalAssets = docCount; - } - - if (docCount == 0 && totalAssets == 0) { - data.put(COMP_PERCENTAGE, INT_HUNDRED); - } - - if (totalAssets > 0) { - data.put( - COMP_PERCENTAGE, - Math.floor(((totalAssets - Double.parseDouble(String.valueOf(docCount))) / totalAssets) - * INT_HUNDRED)); - } - unTagsList.add(data); - } - return unTagsList; - } - - /** - * Gets the no tags data. - * - * @param tagsMap the tags map - * @param targetTypesList the target types list - * @param assetWithoutTag the asset without tag - * @param emptyOrUnkownAssets the empty or unkown assets - * @return the no tags data - */ - private Map getNoTagsData(Map tagsMap,List targetTypesList, Map assetWithoutTag, Map emptyOrUnkownAssets){ - for (String resourceType : targetTypesList) { - Long assetWithoutTagLong = 0l; - Long emptyOrUnkownAssetsLong = 0l; - resourceType = resourceType.replaceAll("\'", ""); - if (assetWithoutTag.containsKey(resourceType)) { - assetWithoutTagLong = assetWithoutTag.get(resourceType); - } - if (emptyOrUnkownAssets.containsKey(resourceType)) { - emptyOrUnkownAssetsLong = emptyOrUnkownAssets.get(resourceType); - } - tagsMap.put(resourceType, emptyOrUnkownAssetsLong + assetWithoutTagLong); - } - return tagsMap; - } -} +/******************************************************************************* + * Copyright 2018 T Mobile, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + ******************************************************************************/ +package com.tmobile.pacman.api.compliance.service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import com.google.common.collect.HashMultimap; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.tmobile.pacman.api.commons.Constants; +import com.tmobile.pacman.api.commons.exception.DataException; +import com.tmobile.pacman.api.commons.exception.ServiceException; +import com.tmobile.pacman.api.commons.repo.ElasticSearchRepository; +import com.tmobile.pacman.api.compliance.domain.Request; +import com.tmobile.pacman.api.compliance.domain.ResponseWithCount; +import com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest; +import com.tmobile.pacman.api.compliance.repository.ComplianceRepository; +import com.tmobile.pacman.api.compliance.repository.TaggingRepository; + +/** + * The Class TaggingServiceImpl. + */ +@Service +public class TaggingServiceImpl implements TaggingService, Constants { + + /** The mandatory tags. */ + @Value("${tagging.mandatoryTags}") + private String mandatoryTags; + + /** The repository. */ + @Autowired + private TaggingRepository repository; + + /** The complaince repository. */ + @Autowired + private ComplianceRepository complainceRepository; + + /** The elastic search repository. */ + @Autowired + private ElasticSearchRepository elasticSearchRepository; + + /** The logger. */ + protected final Log logger = LogFactory.getLog(getClass()); + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.service.TaggingService#getUnTaggedAssetsByApplication(com.tmobile.pacman.api.compliance.domain.Request) + */ + @Override + public ResponseWithCount getUnTaggedAssetsByApplication(Request request) throws ServiceException { + String assetGroup = request.getAg(); + String searchText = request.getSearchtext(); + int from = request.getFrom(); + int size = request.getSize(); + + List> masterDetailList = new ArrayList<>(); + LinkedHashMap app; + JsonArray buckets; + try { + buckets = repository.getUntaggedIssuesByapplicationFromES(assetGroup, getMandatoryTags(), searchText, from, + size); + } catch (DataException e) { + throw new ServiceException(e); + } + JsonObject tagsObj = null; + JsonObject bucketsTags = null; + JsonObject tagsJson = null; + + JsonElement tagsDocId = null; + String tags = null; + String tagName = null; + int total = 0; + for (int i = 0; i < buckets.size(); i++) { + total = buckets.size(); + app = new LinkedHashMap<>(); + app.put("application", buckets.get(i).getAsJsonObject().get("key").getAsString()); + tagsObj = (JsonObject) buckets.get(i).getAsJsonObject().get("tags"); + bucketsTags = (JsonObject) tagsObj.get("buckets"); + Iterator it = bucketsTags.keySet().iterator(); + + while (it.hasNext()) { + tags = it.next(); + tagsJson = (JsonObject) bucketsTags.get(tags); + tagsDocId = tagsJson.get("doc_count"); + StringBuilder sb = new StringBuilder(tags); + sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); + tagName = sb.toString(); + app.put(tagName + "Untagged", tagsDocId.getAsLong()); + } + masterDetailList.add(app); + } + + if (masterDetailList.isEmpty()) { + throw new ServiceException(NO_DATA_FOUND); + } + + if (from >= masterDetailList.size()) { + throw new ServiceException("From exceeds the size of list"); + } + + int endIndex = 0; + + if ((from + size) > masterDetailList.size()) { + endIndex = masterDetailList.size(); + } else { + endIndex = from + size; + } + List> subDetailList; + + if (from == 0 && size == 0) { + subDetailList = masterDetailList; + } else { + subDetailList = masterDetailList.subList(from, endIndex); + } + if (buckets.size() > 0) { + return new ResponseWithCount(subDetailList, total); + } else { + throw new ServiceException("No records found!!"); + } + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.service.TaggingService#getTaggingSummary(java.lang.String) + */ + public Map getTaggingSummary(String assetGroup) throws ServiceException { + List mandatoryTagsList = new ArrayList<>(); + String ruleMandatoryTags = getMandatoryTags(); + if (!StringUtils.isEmpty(ruleMandatoryTags)) { + mandatoryTagsList = Arrays.asList(ruleMandatoryTags.split(",")); + } + Map totalMap = new HashMap<>(); + List> unTagsList = new ArrayList<>(); + Map tagMap; + try { + tagMap = complainceRepository.getTagging(assetGroup, null); + } catch (DataException e) { + throw new ServiceException(e); + } + unTagsList = getUnTaggedListData(unTagsList, assetGroup, mandatoryTagsList, tagMap); + totalMap.put("untaggedList", unTagsList); + totalMap.put("overallCompliance", tagMap.get("compliance")); + + return totalMap; + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.service.TaggingService#getUntaggingByTargetTypes(com.tmobile.pacman.api.compliance.domain.UntaggedTargetTypeRequest) + */ + public List> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) + throws ServiceException { + long assetCount ; + long untaggedCount ; + long taggedCount ; + double compliancePercentage; + String type; + + List tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); + List> targetTypes; + try { + targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); + } catch (DataException e) { + throw new ServiceException(e); + } + if (targetTypes.isEmpty()) { + throw new ServiceException(NO_DATA_FOUND); + } + + Map filterTags = request.getFilter(); + + List> unTagsList = new ArrayList<>(); + Map assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null); + Map untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); + // process records to format the response + for (Map targetType : targetTypes) { + type = targetType.get(TARGET_TYPE).toString(); + + Map data = new HashMap<>(); + if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() + && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { + continue; + } + assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) + : 0l; + if (assetCount > 0) { + untaggedCount = getUntaggedAsset(untaggedCountMap, type); + if (untaggedCount > assetCount) { + untaggedCount = assetCount; + } + taggedCount = assetCount - untaggedCount; + compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; + compliancePercentage = Math.floor(compliancePercentage); + + data.put("name", type); + data.put("untagged", untaggedCount); + data.put("tagged", taggedCount); + data.put("assetCount", assetCount); + data.put(COMP_PERCENTAGE, compliancePercentage); + unTagsList.add(data); + } + } + if (unTagsList.isEmpty()) { + throw new ServiceException(NO_DATA_FOUND); + } + return unTagsList; + } + + /* (non-Javadoc) + * @see com.tmobile.pacman.api.compliance.service.TaggingService#taggingByApplication(java.lang.String, java.lang.String) + */ + @Override + public List> taggingByApplication(String assetGroup, String targetType) throws ServiceException { + List> taggsApplication = new ArrayList<>(); + List tagsList = new ArrayList<>(); + tagsList.add(""); + tagsList.add("unknown"); + Map tagsMap = new HashMap<>(); + Map mustTermsFilter = new HashMap<>(); + String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); + List targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); + Map mustNotFilter = new HashMap<>(); + HashMultimap shouldFilter = HashMultimap.create(); + String aggsEntityType = "_entitytype.keyword"; + mustTermsFilter.put("tags.Application.keyword", tagsList); + Map mustFilter = new HashMap<>(); + mustFilter.put("latest", true); + Map assetWithoutTag; + Map emptyOrUnkownAssets; + Long emptyOrUnkownAssetsLong; + Long assetWithoutTagLong = 0l; + String type = null; + if (!StringUtils.isEmpty(targetType)) { + type = "'" + targetType + "'"; + if (targetTypesList.contains(type)) { + assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); + try { + emptyOrUnkownAssetsLong = elasticSearchRepository + .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, + mustFilter, null, null, null, mustTermsFilter, null, null); + } catch (Exception e) { + throw new ServiceException(e); + } + if (assetWithoutTag.containsKey(targetType)) { + assetWithoutTagLong = assetWithoutTag.get(targetType); + } + tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); + } + } else { + assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); + try { + emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, + mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); + } catch (Exception e) { + throw new ServiceException(e); + } + getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); + } + taggsApplication.add(tagsMap); + return taggsApplication; + } + + /** + * Gets the mandatory tags. + * + * @return the mandatory tags + * @throws ServiceException the service exception + */ + private String getMandatoryTags() throws ServiceException { + String mand = "mandatoryTags"; + String mandatoryTags = null; + char ch = '"'; + String mandTags = ch + "" + mand + "" + ch; + JsonObject paramObj = null; + JsonObject paramDet = null; + JsonArray array = null; + JsonParser parser = new JsonParser(); + List> ruleParams; + try { + ruleParams = repository.getRuleParamsFromDbByPolicyId(TAGGIG_POLICY); + } catch (DataException e) { + throw new ServiceException(e); + } + for (Map params : ruleParams) { + String rParams = params.get("ruleParams").toString(); + paramObj = parser.parse(rParams).getAsJsonObject(); + array = paramObj.get("params").getAsJsonArray(); + + for (JsonElement jsonElement : array) { + + paramDet = jsonElement.getAsJsonObject(); + if (paramDet.get("key").toString().equals(mandTags)) { + mandatoryTags = paramDet.get("value").getAsString(); + } + } + } + return mandatoryTags; + } + + /** + * Gets the untagged asset. + * + * @param bucketMap the bucket map + * @param type the type + * @return the untagged asset + */ + private long getUntaggedAsset(Map bucketMap, String type) { + if (bucketMap.get(type) != null) { + return bucketMap.get(type); + } else { + return 0; + } + } + + /** + * Gets the asset count without application tag. + * + * @param assetGroup the asset group + * @param targetType the target type + * @return the asset count without application tag + * @throws ServiceException the service exception + */ + private Map getAssetCountWithoutApplicationTag(String assetGroup, String targetType) + throws ServiceException { + Map assetWithoutTagsMap = new HashMap<>(); + String responseJson; + try { + responseJson = repository.getTaggingByApplication(assetGroup, targetType); + } catch (DataException e) { + throw new ServiceException(e); + } + JsonParser jsonParser = new JsonParser(); + JsonObject resultJson = jsonParser.parse(responseJson).getAsJsonObject(); + if (StringUtils.isEmpty(targetType)) { + JsonObject aggs = (JsonObject) resultJson.get(AGGREGATIONS); + JsonObject name = (JsonObject) aggs.get("NAME"); + JsonArray buckets = name.get(BUCKETS).getAsJsonArray(); + // convert Json Array to Map object + for (JsonElement bucket : buckets) { + assetWithoutTagsMap.put(bucket.getAsJsonObject().get("key").getAsString(), bucket.getAsJsonObject() + .get(DOC_COUNT).getAsLong()); + } + } else { + if(resultJson.has(COUNT)){ + assetWithoutTagsMap.put(targetType, resultJson.get(COUNT).getAsLong()); + } + } + return assetWithoutTagsMap; + } + + /** + * Gets the untagged target type issues. + * + * @param request the request + * @param tagsList the tags list + * @return the untagged target type issues + * @throws ServiceException the service exception + */ + private Map getUntaggedTargetTypeIssues(UntaggedTargetTypeRequest request, List tagsList) + throws ServiceException { + JsonParser parser = new JsonParser(); + Map untaggedCountMap = new HashMap<>(); + String responseDetails; + try { + responseDetails = repository.getUntaggedTargetTypeIssues(request, tagsList); + } catch (DataException e) { + throw new ServiceException(e); + } + JsonObject responseJson = parser.parse(responseDetails).getAsJsonObject(); + JsonObject aggs = (JsonObject) responseJson.get(AGGREGATIONS); + JsonObject name = (JsonObject) aggs.get("NAME"); + JsonArray buckets = name.get(BUCKETS).getAsJsonArray(); + // convert Json Array to Map object + for (JsonElement bucket : buckets) { + untaggedCountMap.put(bucket.getAsJsonObject().get("key").getAsString(), + bucket.getAsJsonObject().get(DOC_COUNT).getAsLong()); + } + return untaggedCountMap; + } + + /** + * Gets the total assets. + * + * @param tagMap the tag map + * @return the total assets + */ + private long getTotalAssets(Map tagMap){ + Long totalAssets = 0l; + for (Map.Entry asset : tagMap.entrySet()) { + if ("assets".equalsIgnoreCase(asset.getKey())) { + totalAssets = asset.getValue(); + } + } + return totalAssets; + } + + /** + * Gets the un tagged list data. + * + * @param unTagsList the un tags list + * @param assetGroup the asset group + * @param mandatoryTagsList the mandatory tags list + * @param tagMap the tag map + * @return the un tagged list data + * @throws ServiceException the service exception + */ + private List> getUnTaggedListData(List> unTagsList,String assetGroup,List mandatoryTagsList, Map tagMap) throws ServiceException{ + Long docCount; + for (String mandatoryTag : mandatoryTagsList) { + Map data = new HashMap<>(); + Long totalAssets = getTotalAssets(tagMap); + try { + docCount = repository.getUntaggedIssues(assetGroup, mandatoryTag); + } catch (DataException e) { + throw new ServiceException(e); + } + data.put("name", mandatoryTag); + data.put("untagged", docCount); + data.put("tagged", (totalAssets - docCount)); + + if (totalAssets < docCount) { + totalAssets = docCount; + } + + if (docCount == 0 && totalAssets == 0) { + data.put(COMP_PERCENTAGE, INT_HUNDRED); + } + + if (totalAssets > 0) { + data.put( + COMP_PERCENTAGE, + Math.floor(((totalAssets - Double.parseDouble(String.valueOf(docCount))) / totalAssets) + * INT_HUNDRED)); + } + unTagsList.add(data); + } + return unTagsList; + } + + /** + * Gets the no tags data. + * + * @param tagsMap the tags map + * @param targetTypesList the target types list + * @param assetWithoutTag the asset without tag + * @param emptyOrUnkownAssets the empty or unkown assets + * @return the no tags data + */ + private Map getNoTagsData(Map tagsMap,List targetTypesList, Map assetWithoutTag, Map emptyOrUnkownAssets){ + for (String resourceType : targetTypesList) { + Long assetWithoutTagLong = 0l; + Long emptyOrUnkownAssetsLong = 0l; + resourceType = resourceType.replaceAll("\'", ""); + if (assetWithoutTag.containsKey(resourceType)) { + assetWithoutTagLong = assetWithoutTag.get(resourceType); + } + if (emptyOrUnkownAssets.containsKey(resourceType)) { + emptyOrUnkownAssetsLong = emptyOrUnkownAssets.get(resourceType); + } + tagsMap.put(resourceType, emptyOrUnkownAssetsLong + assetWithoutTagLong); + } + return tagsMap; + } +}