2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.aggregatevnf.search;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.json.JsonObject;
28 import javax.servlet.http.HttpServletRequest;
30 import org.apache.camel.Exchange;
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.restclient.client.OperationResult;
36 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
37 import org.onap.aai.sparky.logging.AaiUiMsgs;
38 import org.onap.aai.sparky.logging.util.ServletUtils;
39 import org.onap.aai.sparky.search.filters.FilterQueryBuilder;
40 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
41 import org.onap.aai.sparky.search.filters.entity.SearchFilter;
42 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
44 public class AggregateSummaryProcessor {
46 private static final Logger LOG =
47 LoggerFactory.getInstance().getLogger(AggregateSummaryProcessor.class);
49 private static final String KEY_FILTERS = "filters";
51 private ElasticSearchAdapter elasticSearchAdapter = null;
53 private String vnfAggregationIndexName;
54 private FiltersConfig filtersConfig;
56 public AggregateSummaryProcessor(ElasticSearchAdapter elasticSearchAdapter,
57 FiltersConfig filtersConfig) {
58 this.elasticSearchAdapter = elasticSearchAdapter;
59 this.filtersConfig = filtersConfig;
62 public void setVnfAggregationIndexName(String vnfAggregationIndexName) {
63 this.vnfAggregationIndexName = vnfAggregationIndexName;
66 public void getFilteredAggregation(Exchange exchange) {
68 HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
69 ServletUtils.setUpMdcContext(exchange, request);
72 String payload = exchange.getIn().getBody(String.class);
74 if (payload == null || payload.isEmpty()) {
76 LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, "Request Payload is empty");
79 * Don't throw back an error, just return an empty set
84 JSONObject parameters = new JSONObject(payload);
86 JSONArray requestFilters = null;
87 if (parameters.has(KEY_FILTERS)) {
88 requestFilters = parameters.getJSONArray(KEY_FILTERS);
91 JSONObject zeroResponsePayload = new JSONObject();
92 zeroResponsePayload.put("count", 0);
93 exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
94 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
95 exchange.getOut().setBody(zeroResponsePayload.toString());
97 LOG.error(AaiUiMsgs.ERROR_FILTERS_NOT_FOUND);
101 if (requestFilters != null && requestFilters.length() > 0) {
102 List<JSONObject> filtersToQuery = new ArrayList<JSONObject>();
103 for (int i = 0; i < requestFilters.length(); i++) {
104 JSONObject filterEntry = requestFilters.getJSONObject(i);
105 filtersToQuery.add(filterEntry);
108 String jsonResponsePayload = getVnfFilterAggregations(filtersToQuery);
109 exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
110 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
111 exchange.getOut().setBody(jsonResponsePayload);
114 String emptyResponse = getEmptyAggResponse();
115 exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
116 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
117 exchange.getOut().setBody(emptyResponse);
118 LOG.error(AaiUiMsgs.ERROR_FILTERS_NOT_FOUND);
121 } catch (Exception exc) {
122 LOG.error(AaiUiMsgs.ERROR_GENERIC,
123 "AggregateSummaryProcessor failed to process request due to error = " + exc.getMessage());
129 private String getEmptyAggResponse() {
130 JSONObject aggPayload = new JSONObject();
131 aggPayload.put("totalChartHits", 0);
132 aggPayload.put("buckets", new JSONArray());
133 JSONObject payload = new JSONObject();
134 payload.append("groupby_aggregation", aggPayload);
136 return payload.toString();
139 private static final String FILTER_ID_KEY = "filterId";
140 private static final String FILTER_VALUE_KEY = "filterValue";
141 private static final int DEFAULT_SHOULD_MATCH_SCORE = 1;
142 private static final String VNF_FILTER_AGGREGATION = "vnfFilterAggregation";
145 private String getVnfFilterAggregations(List<JSONObject> filtersToQuery) throws IOException {
147 List<SearchFilter> searchFilters = new ArrayList<SearchFilter>();
148 for (JSONObject filterEntry : filtersToQuery) {
150 String filterId = filterEntry.getString(FILTER_ID_KEY);
151 if (filterId != null) {
152 SearchFilter filter = new SearchFilter();
153 filter.setFilterId(filterId);
155 if (filterEntry.has(FILTER_VALUE_KEY)) {
156 String filterValue = filterEntry.getString(FILTER_VALUE_KEY);
157 filter.addValue(filterValue);
160 searchFilters.add(filter);
164 // Create query for summary by entity type
165 JsonObject vnfSearch = FilterQueryBuilder.createCombinedBoolAndAggQuery(filtersConfig,
166 searchFilters, DEFAULT_SHOULD_MATCH_SCORE);
168 // Parse response for summary by entity type query
169 OperationResult opResult = elasticSearchAdapter.doPost(
170 elasticSearchAdapter.buildElasticSearchUrlForApi(vnfAggregationIndexName,
171 SparkyConstants.ES_SEARCH_API),
172 vnfSearch.toString(), javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE);
174 if (opResult.wasSuccessful()) {
175 return buildAggregateVnfResponseJson(opResult.getResult());
177 return buildEmptyAggregateVnfResponseJson();
181 private String buildEmptyAggregateVnfResponseJson() {
182 JSONObject finalOutputToFe = new JSONObject();
183 finalOutputToFe.put("total", 0);
184 return finalOutputToFe.toString();
187 private String buildAggregateVnfResponseJson(String responseJsonStr) {
189 JSONObject finalOutputToFe = new JSONObject();
190 JSONObject responseJson = new JSONObject(responseJsonStr);
193 JSONObject hits = responseJson.getJSONObject("hits");
194 int totalHits = hits.getInt("total");
195 finalOutputToFe.put("total", totalHits);
197 JSONObject aggregations = responseJson.getJSONObject("aggregations");
198 String[] aggKeys = JSONObject.getNames(aggregations);
199 JSONObject aggregationsList = new JSONObject();
201 for (String aggName : aggKeys) {
202 JSONObject aggregation = aggregations.getJSONObject(aggName);
203 JSONArray buckets = aggregation.getJSONArray("buckets");
204 aggregationsList.put(aggName, buckets);
207 finalOutputToFe.put("aggregations", aggregationsList);
209 return finalOutputToFe.toString();