From b4922d319d293894fddd512d29b5f0d1411915d9 Mon Sep 17 00:00:00 2001 From: ARULNA Date: Mon, 12 Jun 2017 16:41:12 -0400 Subject: Initial commit for AAI-UI(sparky-backend) Change-Id: I785397ed4197663cdf0c1351041d2f708ed08763 Signed-off-by: ARULNA --- .../openecomp/sparky/search/EntityTypeSummary.java | 53 ++++ .../sparky/search/EntityTypeSummaryBucket.java | 46 +++ .../sparky/search/SearchEntityProperties.java | 49 +++ .../org/openecomp/sparky/search/Suggestion.java | 59 ++++ .../openecomp/sparky/search/SuggestionList.java | 72 +++++ .../sparky/search/VnfSearchQueryBuilder.java | 200 ++++++++++++ .../openecomp/sparky/search/VnfSearchService.java | 348 +++++++++++++++++++++ .../sparky/search/config/SuggestionConfig.java | 143 +++++++++ 8 files changed, 970 insertions(+) create mode 100644 src/main/java/org/openecomp/sparky/search/EntityTypeSummary.java create mode 100644 src/main/java/org/openecomp/sparky/search/EntityTypeSummaryBucket.java create mode 100644 src/main/java/org/openecomp/sparky/search/SearchEntityProperties.java create mode 100644 src/main/java/org/openecomp/sparky/search/Suggestion.java create mode 100644 src/main/java/org/openecomp/sparky/search/SuggestionList.java create mode 100644 src/main/java/org/openecomp/sparky/search/VnfSearchQueryBuilder.java create mode 100644 src/main/java/org/openecomp/sparky/search/VnfSearchService.java create mode 100644 src/main/java/org/openecomp/sparky/search/config/SuggestionConfig.java (limited to 'src/main/java/org/openecomp/sparky/search') diff --git a/src/main/java/org/openecomp/sparky/search/EntityTypeSummary.java b/src/main/java/org/openecomp/sparky/search/EntityTypeSummary.java new file mode 100644 index 0000000..aa79f3d --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/EntityTypeSummary.java @@ -0,0 +1,53 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +import java.util.ArrayList; +import java.util.List; + +public class EntityTypeSummary { + private int totalChartHits; + private List buckets = new ArrayList<>(); + + public int getTotalChartHits() { + return totalChartHits; + } + + public List getBuckets() { + return buckets; + } + + public void setTotalChartHits(int totalChartHits) { + this.totalChartHits = totalChartHits; + } + + public void setBuckets(List buckets) { + this.buckets = buckets; + } + + public void addBucket(EntityTypeSummaryBucket bucket) { + this.buckets.add(bucket); + } +} diff --git a/src/main/java/org/openecomp/sparky/search/EntityTypeSummaryBucket.java b/src/main/java/org/openecomp/sparky/search/EntityTypeSummaryBucket.java new file mode 100644 index 0000000..540b300 --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/EntityTypeSummaryBucket.java @@ -0,0 +1,46 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +public class EntityTypeSummaryBucket { + private int count; + private String key; + + public int getCount() { + return count; + } + + public String getKey() { + return key; + } + + public void setCount(int count) { + this.count = count; + } + + public void setKey(String key) { + this.key = key; + } +} diff --git a/src/main/java/org/openecomp/sparky/search/SearchEntityProperties.java b/src/main/java/org/openecomp/sparky/search/SearchEntityProperties.java new file mode 100644 index 0000000..bcf46f9 --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/SearchEntityProperties.java @@ -0,0 +1,49 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +import java.util.HashMap; +import java.util.Map; + +public class SearchEntityProperties { + private String type; + private Map fields = new HashMap<>(); + + public String getType() { + return type; + } + + public Map getFields() { + return fields; + } + + public void setType(String type) { + this.type = type; + } + + public void setFields(Map field) { + this.fields = field; + } +} diff --git a/src/main/java/org/openecomp/sparky/search/Suggestion.java b/src/main/java/org/openecomp/sparky/search/Suggestion.java new file mode 100644 index 0000000..79eb240 --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/Suggestion.java @@ -0,0 +1,59 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +public class Suggestion { + private String entityType; + private String searchTags; + private SearchEntityProperties properties; + + public Suggestion(SearchEntityProperties properties) { + this.properties = properties; + } + + public String getEntityType() { + return entityType; + } + + public String getSearchTags() { + return searchTags; + } + + public SearchEntityProperties getProperties() { + return properties; + } + + public void setEntityType(String entityType) { + this.entityType = entityType; + } + + public void setSearchTags(String searchTags) { + this.searchTags = searchTags; + } + + public void setProperties(SearchEntityProperties properties) { + this.properties = properties; + } +} diff --git a/src/main/java/org/openecomp/sparky/search/SuggestionList.java b/src/main/java/org/openecomp/sparky/search/SuggestionList.java new file mode 100644 index 0000000..cd56099 --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/SuggestionList.java @@ -0,0 +1,72 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +import java.util.LinkedList; +import java.util.List; + +public class SuggestionList { + // TODO: verify which data type these fields should be + private Long processingTimeInMs; + private Long totalFound; + private Long numReturned; + private List suggestions = new LinkedList<>(); + + public void addSuggestion(Suggestion suggestion) { + suggestions.add(suggestion); + } + + public List getSuggestions() { + return suggestions; + } + + public void setSuggestions(List suggestions) { + this.suggestions = suggestions; + } + + public Long getProcessingTimeInMs() { + return processingTimeInMs; + } + + public Long getTotalFound() { + return totalFound; + } + + public Long getNumReturned() { + return numReturned; + } + + public void setProcessingTimeInMs(Long processingTimeInMs) { + this.processingTimeInMs = processingTimeInMs; + } + + public void setTotalFound(Long totalFound) { + this.totalFound = totalFound; + } + + public void setNumReturned(Long numReturned) { + this.numReturned = numReturned; + } +} \ No newline at end of file diff --git a/src/main/java/org/openecomp/sparky/search/VnfSearchQueryBuilder.java b/src/main/java/org/openecomp/sparky/search/VnfSearchQueryBuilder.java new file mode 100644 index 0000000..55d003e --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/VnfSearchQueryBuilder.java @@ -0,0 +1,200 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +import java.util.Date; +import java.util.Map; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; + +/** + * Build a JSON payload to send to elastic search to get vnf search data. + */ + +public class VnfSearchQueryBuilder { + + /** + * Creates the suggestions query. + * + * @param maxResults maximum number of suggestions to fetch + * @param queryStr query string + * @return the json object + */ + + /* + * { "vnfs" : { "text" : "VNFs", "completion" : { "field" : "entity_suggest", "size": 1 } } } + */ + public static JsonObject createSuggestionsQuery(String maxResults, String queryStr) { + JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); + + JsonObjectBuilder completionBlob = Json.createObjectBuilder(); + completionBlob.add("field", "entity_suggest"); + completionBlob.add("size", maxResults); + + JsonObjectBuilder jsonAllBuilder = Json.createObjectBuilder(); + jsonAllBuilder.add("text", queryStr); + jsonAllBuilder.add("completion", completionBlob); + + jsonBuilder.add("vnfs", jsonAllBuilder.build()); + return jsonBuilder.build(); + } + + public static JsonObject getTermBlob(String key, String value) { + JsonObjectBuilder termBlobBuilder = Json.createObjectBuilder(); + JsonObjectBuilder jsonBuilder = Json.createObjectBuilder().add(key, value); + return termBlobBuilder.add("term", jsonBuilder.build()).build(); + } + + public static void getSummaryAggsBlob(JsonObjectBuilder aggsBlobBuilder, String aggsKey, + int resultSize) { + JsonObjectBuilder fieldBuilder = + Json.createObjectBuilder().add("field", aggsKey).add("size", resultSize); + JsonObject aggsFieldBlob = fieldBuilder.build(); + JsonObjectBuilder defaultBlobBuilder = Json.createObjectBuilder().add("terms", aggsFieldBlob); + JsonObject defaultBlob = defaultBlobBuilder.build(); + aggsBlobBuilder.add("default", defaultBlob); + } + + public static void buildSingleTermCountQuery(JsonObjectBuilder jsonBuilder, String key, + String value) { + jsonBuilder.add("query", getTermBlob(key, value)); + } + + public static void buildSingleTermSummaryQuery(JsonObjectBuilder jsonBuilder, String key, + String value, String groupByKey) { + JsonObjectBuilder queryBlobBuilder = Json.createObjectBuilder(); + JsonObjectBuilder aggsBlobBuilder = Json.createObjectBuilder(); + + queryBlobBuilder.add("constant_score", + Json.createObjectBuilder().add("filter", getTermBlob(key, value))); + + getSummaryAggsBlob(aggsBlobBuilder, groupByKey, 0); + + jsonBuilder.add("query", queryBlobBuilder.build()); + jsonBuilder.add("aggs", aggsBlobBuilder.build()); + } + + public static void buildMultiTermSummaryQuery(JsonObjectBuilder jsonBuilder, + Map attributes, String groupByKey) { + JsonObjectBuilder queryBlobBuilder = Json.createObjectBuilder(); + JsonObjectBuilder aggsBlobBuilder = Json.createObjectBuilder(); + JsonArrayBuilder mustBlobBuilder = Json.createArrayBuilder(); + for (String key : attributes.keySet()) { + mustBlobBuilder.add(getTermBlob(key, attributes.get(key))); + } + JsonArray mustBlob = mustBlobBuilder.build(); + + queryBlobBuilder.add("constant_score", Json.createObjectBuilder().add("filter", + Json.createObjectBuilder().add("bool", Json.createObjectBuilder().add("must", mustBlob)))); + + getSummaryAggsBlob(aggsBlobBuilder, groupByKey, 0); + + jsonBuilder.add("query", queryBlobBuilder.build()); + jsonBuilder.add("aggs", aggsBlobBuilder.build()); + } + + public static void buildZeroTermSummaryQuery(JsonObjectBuilder jsonBuilder, String groupByKey) { + JsonObjectBuilder aggsBlobBuilder = Json.createObjectBuilder(); + + getSummaryAggsBlob(aggsBlobBuilder, groupByKey, 0); + + jsonBuilder.add("aggs", aggsBlobBuilder.build()); + } + + public static void buildMultiTermCountQuery(JsonObjectBuilder jsonBuilder, + Map attributes) { + JsonArrayBuilder mustBlobBuilder = Json.createArrayBuilder(); + for (String key : attributes.keySet()) { + mustBlobBuilder.add(getTermBlob(key, attributes.get(key))); + } + jsonBuilder.add("query", Json.createObjectBuilder().add("bool", + Json.createObjectBuilder().add("must", mustBlobBuilder))); + } + + + + public static JsonObject createSummaryByEntityTypeQuery(Map attributes, + String groupByKey) { + JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); + jsonBuilder.add("size", "0"); // avoid source data + if (attributes.size() == 0) { + buildZeroTermSummaryQuery(jsonBuilder, groupByKey); + } else if (attributes.size() == 1) { + Map.Entry entry = attributes.entrySet().iterator().next(); + buildSingleTermSummaryQuery(jsonBuilder, entry.getKey(), entry.getValue(), groupByKey); + } else { + buildMultiTermSummaryQuery(jsonBuilder, attributes, groupByKey); + } + return jsonBuilder.build(); + } + + public static JsonObject createEntityCountsQuery(Map attributes) { + JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(); + if (attributes.size() == 1) { + Map.Entry entry = attributes.entrySet().iterator().next(); + buildSingleTermCountQuery(jsonBuilder, entry.getKey(), entry.getValue()); + } else { + buildMultiTermCountQuery(jsonBuilder, attributes); + } + return jsonBuilder.build(); + } + + public static JsonArray getSortCriteria(String sortFieldName, String sortOrder) { + JsonArrayBuilder jsonBuilder = Json.createArrayBuilder(); + jsonBuilder.add(Json.createObjectBuilder().add(sortFieldName, + Json.createObjectBuilder().add("order", sortOrder))); + + return jsonBuilder.build(); + } + + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + Date start = new Date(System.currentTimeMillis() - Integer.MAX_VALUE); + Date end = new Date(); + String timezone = "-05:00"; + // JsonObject arr = createDateHistogramQuery(start, end, timezone); + + // System.out.println(arr.toString()); + + + // JsonObject table = createTableQuery(start, end, timezone, 0, 25); + // JsonObject aggre = createAuditQuery(start, end, timezone, "entityType", null, null); + + // System.out.println(arr.toString()); + // System.out.println(table.toString()); + // System.out.println(aggre.toString()); + + + } +} diff --git a/src/main/java/org/openecomp/sparky/search/VnfSearchService.java b/src/main/java/org/openecomp/sparky/search/VnfSearchService.java new file mode 100644 index 0000000..1cef43c --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/VnfSearchService.java @@ -0,0 +1,348 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MediaType; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.sparky.dal.elasticsearch.HashQueryResponse; +import org.openecomp.sparky.dal.elasticsearch.SearchAdapter; +import org.openecomp.sparky.dal.elasticsearch.config.ElasticSearchConfig; +import org.openecomp.sparky.dal.rest.OperationResult; +import org.openecomp.sparky.logging.AaiUiMsgs; +import org.openecomp.sparky.suggestivesearch.SuggestionEntity; +import org.openecomp.sparky.util.NodeUtils; +import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants; +import org.openecomp.sparky.viewandinspect.entity.QuerySearchEntity; + + +/** + * From the given HTTP request, create vnf-search query for document store, and process document + * store response. + */ + +public class VnfSearchService { + + private static final String APP_JSON = MediaType.APPLICATION_JSON; + + private static ElasticSearchConfig esConfig = null; + + private static final Logger LOG = LoggerFactory.getInstance().getLogger(VnfSearchService.class); + + private static SearchAdapter search = null; + private static final String ES_SUGGEST_API = TierSupportUiConstants.ES_SUGGEST_API; + private static final String ES_COUNT_API = TierSupportUiConstants.ES_COUNT_API; + private static final String ES_SEARCH_API = TierSupportUiConstants.ES_SEARCH_API; + + private static final String ENTITY_TYPE = "generic-vnf"; + + /** + * Get Full URL for search using elastic search configuration. + * + * @param api the api + * @return the full url + */ + private static String getFullUrl(String indexName, String api) { + + final String host = esConfig.getIpAddress(); + final String port = esConfig.getHttpPort(); + return String.format("http://%s:%s/%s/%s", host, port, indexName, api); + } + + /** + * Process operation result. + * + * @param api the api + * @param response the response + * @param opResult the op result + * @throws IOException Signals that an I/O exception has occurred. + */ + private static void buildVnfQuerySearchResponse(String apiKey, HttpServletResponse response, + OperationResult opResult) throws IOException { + int resonseCode = opResult.getResultCode(); + String result = opResult.getResult(); + + if (resonseCode > 300) { + setServletResponse(true, resonseCode, response, result); + return; + } + + if (result != null) { + JSONObject finalOutputToFe = new JSONObject(); + JSONObject responseJson = new JSONObject(result); + + if (apiKey.equalsIgnoreCase(ES_SUGGEST_API)) { // process suggestion results + try { + String suggestionsKey = "vnfs"; + int total = 0; + JSONArray suggestionsArray = new JSONArray(); + JSONArray suggestions = responseJson.getJSONArray(suggestionsKey); + if (suggestions.length() > 0) { + suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options"); + for (int i = 0; i < suggestionsArray.length(); i++) { + suggestionsArray.getJSONObject(i).remove("score"); // FE doesn't like this noise: 'score' + } + + total = suggestionsArray.length(); + } + finalOutputToFe.put("totalFound", total); + finalOutputToFe.put("suggestions", suggestionsArray); + } catch (Exception e) { + LOG.error(AaiUiMsgs.ERROR_GENERIC, + "Error parsing response from suggestions index. Response: " + result); + } + } else if (apiKey.equalsIgnoreCase(ES_COUNT_API)) { + try { + String shardsKey = "_shards"; + responseJson.remove(shardsKey); + finalOutputToFe = responseJson; + } catch (Exception e) { + LOG.error(AaiUiMsgs.ERROR_GENERIC, + "Error fetching total count response from aggregation index. Response: " + result); + } + } else if (apiKey.equalsIgnoreCase(ES_SEARCH_API)) { + try { + JSONArray bucketsArray = (responseJson.getJSONObject("aggregations") + .getJSONObject("default").getJSONArray("buckets")); + int count = 0; + for (int i=0; i< bucketsArray.length(); i++) { + count += bucketsArray.getJSONObject(i).getInt("doc_count"); + } + JSONObject content = new JSONObject(); + content.put("totalChartHits", count); + content.put("buckets", bucketsArray); + finalOutputToFe.put("groupby_aggregation", content); + } catch (Exception e) { + LOG.error(AaiUiMsgs.ERROR_GENERIC, + "Error fetching group-by query response from aggregation index. Response: " + result); + } + } + + setServletResponse(false, resonseCode, response, finalOutputToFe.toString()); + } + } + + /** + * Sets the servlet response. + * + * @param isError the is error + * @param responseCode the response code + * @param response the response + * @param postPayload the post payload + * @throws IOException Signals that an I/O exception has occurred. + */ + public static void setServletResponse(boolean isError, int responseCode, + HttpServletResponse response, String postPayload) throws IOException { + + if (isError) { + LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, postPayload); + } + + response.setStatus(responseCode); + + if (postPayload != null) { + response.setContentType(APP_JSON); + PrintWriter out = response.getWriter(); + out.println(postPayload); + out.close(); + } + } + + /** + * Instantiates a new vnf search service. + */ + public VnfSearchService() { + try { + if (esConfig == null) { + esConfig = ElasticSearchConfig.getConfig(); + } + + if (search == null) { + search = new SearchAdapter(); + } + } catch (Exception exc) { + LOG.error(AaiUiMsgs.CONFIGURATION_ERROR, "Search"); + } + } + + + /** + * Gets the suggestions results. + * + * @param response the response + * @param maxResults maximum number of suggestions + * @param queryStr query string + * @return the suggestions results + * @throws IOException Signals that an I/O exception has occurred. + */ + public List getSuggestionsResults(QuerySearchEntity querySearchEntity, int resultCountLimit) throws IOException { + List returnList = new ArrayList(); + + /* Create suggestions query */ + JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(String.valueOf(resultCountLimit), querySearchEntity.getQueryStr()); + + /* Parse suggestions response */ + OperationResult opResult = search.doPost(getFullUrl(esConfig.getAutosuggestIndexname(), ES_SUGGEST_API), vnfSearch.toString(), APP_JSON); + + String result = opResult.getResult(); + + if (!opResult.wasSuccessful()) { + LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result); + return returnList; + } + + JSONObject responseJson = new JSONObject(result); + String suggestionsKey = "vnfs"; + JSONArray suggestionsArray = new JSONArray(); + JSONArray suggestions = responseJson.getJSONArray(suggestionsKey); + if (suggestions.length() > 0) { + suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options"); + for (int i = 0; i < suggestionsArray.length(); i++) { + JSONObject querySuggestion = suggestionsArray.getJSONObject(i); + if (querySuggestion != null) { + SuggestionEntity responseSuggestion = new SuggestionEntity(); + responseSuggestion.setText(querySuggestion.getString("text")); + responseSuggestion.setRoute("vnfSearch"); // TODO -> Read route from suggestive-search.properties instead of hard coding + responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(querySuggestion.getString("text"))); + returnList.add(responseSuggestion); + } + } + } + return returnList; + } + + + /** + * This method sets server response if lookup in ES has 0 count + * TODO: Change the response code to appropriate when FE-BE contract is finalized + * @param response + */ + public void setZeroCountResponse(HttpServletResponse response) throws IOException { + JSONObject payload = new JSONObject(); + payload.put("count", 0); + setServletResponse(false, 200, response, payload.toString() ); + } + + /** + * This method sets server response if lookup in ES for an aggregation has 0 results + * TODO: Change the response code to appropriate when FE-BE contract is finalized + * @param response + */ + public void setEmptyAggResponse(HttpServletResponse response) throws IOException { + JSONObject aggPayload = new JSONObject(); + aggPayload.put("totalChartHits", 0); + aggPayload.put("buckets", new JSONArray()); + JSONObject payload = new JSONObject(); + payload.append("groupby_aggregation", aggPayload); + setServletResponse(false, 200, response, payload.toString() ); + } + + public HashQueryResponse getJSONPayloadFromHash(String hashId) { + + HashQueryResponse hashQueryResponse = new HashQueryResponse(); + JsonObjectBuilder hashSearch = Json.createObjectBuilder(); + VnfSearchQueryBuilder.buildSingleTermCountQuery(hashSearch, "_id", hashId); + String hashSearchQuery = hashSearch.build().toString(); + OperationResult opResult = search.doPost( + getFullUrl(esConfig.getAutosuggestIndexname(), ES_SEARCH_API), + hashSearchQuery, APP_JSON); + hashQueryResponse.setOpResult(opResult); + + if(opResult != null && opResult.wasSuccessful()){ + String result = opResult.getResult(); + if (result != null) { + JSONObject responseJson = new JSONObject(result); + JSONArray hits = responseJson.getJSONObject("hits").getJSONArray("hits"); + if(hits != null && hits.length() > 0){ + hashQueryResponse.setJsonPayload (hits.getJSONObject(0).getJSONObject("_source") + .getJSONObject("entity_suggest").toString()); + } + } + } + return hashQueryResponse; + } + + public void getEntityCountResults(HttpServletResponse response, Map attributes) + throws IOException { + // Create entity counts query + JsonObject vnfSearch = VnfSearchQueryBuilder.createEntityCountsQuery(attributes); + + // Parse response for entity counts query + OperationResult opResult = search.doPost( + getFullUrl(TierSupportUiConstants.getAggregationIndexName(ENTITY_TYPE), ES_COUNT_API), + vnfSearch.toString(), APP_JSON); + buildVnfQuerySearchResponse(ES_COUNT_API, response, opResult); + } + + public void getSummaryByEntityType(HttpServletResponse response, Map attributes, + String groupByKey) throws IOException { + // Create query for summary by entity type + JsonObject vnfSearch = + VnfSearchQueryBuilder.createSummaryByEntityTypeQuery(attributes, groupByKey); + + // Parse response for summary by entity type query + OperationResult opResult = search.doPost( + getFullUrl(TierSupportUiConstants.getAggregationIndexName(ENTITY_TYPE), ES_SEARCH_API), + vnfSearch.toString(), APP_JSON); + buildVnfQuerySearchResponse(ES_SEARCH_API, response, opResult); + } + + public SearchAdapter getSearch() { + return search; + } + + public void setSearch(SearchAdapter search) { + VnfSearchService.search = search; + } + + public static ElasticSearchConfig getEsConfig() { + return esConfig; + } + + public static void setEsConfig(ElasticSearchConfig esConfig) { + VnfSearchService.esConfig = esConfig; + } + + public static void main(String agrs[]) { + VnfSearchService vnfs = new VnfSearchService(); + Date start = new Date(); + Date end = start; + } + +} diff --git a/src/main/java/org/openecomp/sparky/search/config/SuggestionConfig.java b/src/main/java/org/openecomp/sparky/search/config/SuggestionConfig.java new file mode 100644 index 0000000..c9dbc6e --- /dev/null +++ b/src/main/java/org/openecomp/sparky/search/config/SuggestionConfig.java @@ -0,0 +1,143 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.sparky.search.config; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import org.openecomp.sparky.util.ConfigHelper; +import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants; + +public class SuggestionConfig { + public static final String CONFIG_FILE = + TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION + "suggestive-search.properties"; + + private static SuggestionConfig config; + private static final String INDEX_SEARCH_MAPPER_DEFAULT = "elasticsearch.autosuggestIndexname:SearchServiceWrapper,elasticsearch.indexName:VnfSearchService"; + + private Map searchIndexToSearchService; + + private static final String CALLED_PAIRING_KEY_DEFAULT = "volume-group-id,volume-group-name,physical-location-id,data-center-code,complex-name,tenant-id,tenant-name,vserver-id,vserver-name,vserver-name2,hostname,pserver-name2,pserver-id,global-customer-id,subscriber-name,service-instance-id,service-instance-name,link-name,vpn-id,vpn-name,vpe-id,vnf-id,vnf-name,vnf-name2,vnfc-name,network-id,network-name,network-policy-id,vf-module-id,vf-module-name,vnf-id2,pnf-name,circuit-id"; + private static final String CALLED_PAIRING_VALUE_DEFAULT = "called"; + private static final String AT_PAIRING_KEY_DEFAULT = "street1,street2,postal-code,ipv4-oam-address,network-policy-fqdn"; + private static final String AT_PAIRING_VALUE_DEFAULT = "at"; + private static final String DEFAULT_PAIRING_DEFAULT_VALUE = "with"; + private String conjunctionForAt; + Map pairingList; + private Collection stopWords; + private String defaultPairingValue; + + + private SuggestionConfig() {} + + /** + * Returns initialized instance as per singleton pattern. + * + * @return initialized SuggestionConfig instance + */ + public static SuggestionConfig getConfig() { + if (config == null) { + config = new SuggestionConfig(); + config.initializeConfigProperties(); + } + return config; + } + + public void initializeConfigProperties() { + + Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE); + Properties suggestionProps = ConfigHelper.getConfigWithPrefix("suggestion", props); + + String indexSearchMapper = suggestionProps.getProperty("routing", INDEX_SEARCH_MAPPER_DEFAULT); + String[] indexesToSearchClassesArray = indexSearchMapper.split(","); + searchIndexToSearchService = new HashMap(); + for (String pair : indexesToSearchClassesArray) { + String[] subPair = pair.split(":"); + searchIndexToSearchService.put(subPair[0], subPair[1]); + } + + defaultPairingValue=suggestionProps.getProperty("pairing.default.value", DEFAULT_PAIRING_DEFAULT_VALUE); + String calledValue = suggestionProps.getProperty("pairing.called.value", CALLED_PAIRING_VALUE_DEFAULT); + String[] calledPairingArray = suggestionProps.getProperty("pairing.called.key", CALLED_PAIRING_KEY_DEFAULT).split(","); + pairingList = new HashMap(); + for(String calledField: calledPairingArray){ + pairingList.put(calledField, calledValue); + } + + this.conjunctionForAt = suggestionProps.getProperty("pairing.at.value", AT_PAIRING_VALUE_DEFAULT); + String[] atPairingArray = suggestionProps.getProperty("pairing.at.key", AT_PAIRING_KEY_DEFAULT).split(","); + for(String atField: atPairingArray){ + pairingList.put(atField, conjunctionForAt); + } + + stopWords = Arrays.asList(suggestionProps.getProperty("stopwords", "").split(",")); + + } + + public void setSearchIndexToSearchService(Map searchIndexToSearchService) { + this.searchIndexToSearchService = searchIndexToSearchService; + } + + public Map getSearchIndexToSearchService() { + return searchIndexToSearchService; + } + + public Collection getStopWords() { + return stopWords; + } + + public void setStopWords(Collection stopWords) { + this.stopWords = stopWords; + } + + public Map getPairingList() { + return pairingList; + } + + public void setPairingList(Map pairingList) { + this.pairingList = pairingList; + } + + public String getDefaultPairingValue() { + return defaultPairingValue; + } + + public void setDefaultPairingValue(String defaultPairingValue) { + this.defaultPairingValue = defaultPairingValue; + } + + public String getConjunctionForAt() { + return conjunctionForAt; + } + + public void setConjunctionForAt(String conjunctionForAt) { + this.conjunctionForAt = conjunctionForAt; + } + + +} -- cgit 1.2.3-korg