changed the header license to new license
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / aggregatevnf / search / AggregateVnfSearchProvider.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.aggregatevnf.search;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.json.JsonObject;
27 import javax.ws.rs.core.MediaType;
28
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.restclient.client.OperationResult;
34 import org.onap.aai.sparky.common.search.CommonSearchSuggestion;
35 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
36 import org.onap.aai.sparky.logging.AaiUiMsgs;
37 import org.onap.aai.sparky.search.api.SearchProvider;
38 import org.onap.aai.sparky.search.entity.QuerySearchEntity;
39 import org.onap.aai.sparky.search.entity.SearchSuggestion;
40 import org.onap.aai.sparky.search.filters.entity.UiFilterValueEntity;
41 import org.onap.aai.sparky.util.NodeUtils;
42 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
43
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 public class AggregateVnfSearchProvider implements SearchProvider {
47   
48   private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateVnfSearchProvider.class);
49
50   private ObjectMapper mapper;
51   private ElasticSearchAdapter elasticSearchAdapter = null;
52   private String autoSuggestIndexName;
53   private String vnfSearchSuggestionRoute;
54
55   public AggregateVnfSearchProvider(ElasticSearchAdapter elasticSearchAdapter,
56       String autoSuggestIndexName, String vnfSearchSuggestionRoute) {
57     mapper = new ObjectMapper();
58     this.elasticSearchAdapter = elasticSearchAdapter;
59     this.autoSuggestIndexName = autoSuggestIndexName;
60     this.vnfSearchSuggestionRoute = vnfSearchSuggestionRoute;
61   }
62   
63   public void setAutoSuggestIndexName(String autoSuggestIndexName) {
64     this.autoSuggestIndexName = autoSuggestIndexName;
65   }
66
67   @Override
68   public List<SearchSuggestion> search(QuerySearchEntity queryRequest) {
69
70     List<SearchSuggestion> returnList = new ArrayList<SearchSuggestion>();
71
72     try {
73
74       /* Create suggestions query */
75       JsonObject vnfSearch = VnfSearchQueryBuilder.createSuggestionsQuery(String.valueOf(queryRequest.getMaxResults()), queryRequest.getQueryStr());
76
77       /* Parse suggestions response */
78       OperationResult opResult = elasticSearchAdapter.doPost(
79           elasticSearchAdapter.buildElasticSearchUrlForApi(autoSuggestIndexName,
80               SparkyConstants.ES_SUGGEST_API),
81           vnfSearch.toString(), MediaType.APPLICATION_JSON_TYPE);
82
83       String result = opResult.getResult();
84
85       if (!opResult.wasSuccessful()) {
86         LOG.error(AaiUiMsgs.ERROR_PARSING_JSON_PAYLOAD_VERBOSE, result);
87         return returnList;
88       }
89
90       JSONObject responseJson = new JSONObject(result);
91       String suggestionsKey = "vnfs";
92       JSONArray suggestionsArray = new JSONArray();
93       JSONArray suggestions = responseJson.getJSONArray(suggestionsKey);
94       if (suggestions.length() > 0) {
95         suggestionsArray = suggestions.getJSONObject(0).getJSONArray("options");
96         for (int i = 0; i < suggestionsArray.length(); i++) {
97           JSONObject querySuggestion = suggestionsArray.getJSONObject(i);
98           if (querySuggestion != null) {
99             CommonSearchSuggestion responseSuggestion = new CommonSearchSuggestion();
100             responseSuggestion.setText(querySuggestion.getString("text"));
101             responseSuggestion.setRoute(vnfSearchSuggestionRoute);
102             responseSuggestion.setHashId(NodeUtils.generateUniqueShaDigest(querySuggestion.getString("text")));
103
104             // Extract filter list from JSON and add to response suggestion
105             JSONObject payload = querySuggestion.getJSONObject("payload");
106             if (payload.length() > 0) {
107               JSONArray filterList = payload.getJSONArray("filterList");
108               for (int filter = 0; filter < filterList.length(); filter++) {
109                 String filterValueString = filterList.getJSONObject(filter).toString();
110                 UiFilterValueEntity filterValue = mapper.readValue(filterValueString, UiFilterValueEntity.class);
111                 responseSuggestion.getFilterValues().add(filterValue);
112               }
113             }
114             returnList.add(responseSuggestion);
115           }
116         }
117       }
118     } catch (Exception exc) {
119       LOG.error(AaiUiMsgs.ERROR_GENERIC, "Search failed due to error = " + exc.getMessage());
120     }
121
122     return returnList;
123   }
124   
125 }