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 javax.servlet.http.HttpServletRequest;
25 import org.apache.camel.Exchange;
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.restclient.client.OperationResult;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30 import org.onap.aai.sparky.logging.util.ServletUtils;
31 import org.onap.aai.sparky.search.SearchServiceAdapter;
32 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
33 import org.onap.aai.sparky.search.filters.searchservice.FilterQueryAndResponseBuilder;
34 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
36 import com.google.gson.Gson;
37 import com.google.gson.JsonArray;
38 import com.google.gson.JsonObject;
40 public class AggregateSummaryProcessor {
42 private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateSummaryProcessor.class);
44 private static final String KEY_FILTERS = "filters";
45 private static final String TOTAL = "total";
47 private SearchServiceAdapter searchServiceAdapter = null;
49 private String vnfAggregationIndexName;
51 private FilterQueryAndResponseBuilder filterQueryAndResponseBuilder;
52 private Gson converter;
54 public AggregateSummaryProcessor(SearchServiceAdapter searchServiceAdapter,
55 FiltersConfig filtersConfig) {
56 this.searchServiceAdapter = searchServiceAdapter;
57 this.filterQueryAndResponseBuilder = new FilterQueryAndResponseBuilder(filtersConfig);
58 this.converter = new Gson();
61 public void setVnfAggregationIndexName(String vnfAggregationIndexName) {
62 this.vnfAggregationIndexName = vnfAggregationIndexName;
65 public void getFilteredAggregation(Exchange exchange) {
67 HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
68 ServletUtils.setUpMdcContext(exchange, request);
71 String payload = exchange.getIn().getBody(String.class);
73 if (payload == null || payload.isEmpty()) {
75 LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, "Request Payload is empty");
78 * Don't throw back an error, just return an empty set
82 JsonObject payloadObj = converter.fromJson(payload, JsonObject.class);
83 if (payloadObj.has(KEY_FILTERS)) {
85 String jsonResponsePayload = getVnfFilterAggregations(payload);
87 exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
88 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
89 exchange.getOut().setBody(jsonResponsePayload);
92 String emptyResponse = getEmptyAggResponse();
93 exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
94 exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
95 exchange.getOut().setBody(emptyResponse);
96 LOG.error(AaiUiMsgs.ERROR_FILTERS_NOT_FOUND);
99 } catch (Exception exc) {
100 LOG.error(AaiUiMsgs.ERROR_GENERIC,
101 "AggregateSummaryProcessor failed to process request due to error = " + exc.getMessage());
105 private String getEmptyAggResponse() {
106 JsonObject aggPayload = new JsonObject();
107 aggPayload.addProperty("totalChartHits", 0);
108 aggPayload.add("buckets", new JsonArray());
109 JsonObject payload = new JsonObject();
110 payload.add("groupby_aggregation", aggPayload);
112 return payload.toString();
115 private String getVnfFilterAggregations(String payload) {
116 String query = filterQueryAndResponseBuilder.createFileBasedFilterQuery(payload);
118 // Parse response for summary by entity type query
119 String searchUrl = searchServiceAdapter.buildSearchServiceUrlForApi(vnfAggregationIndexName, SparkyConstants.SS_QUERY_API);
120 OperationResult opResult = searchServiceAdapter.doPost(searchUrl, query);
122 if (opResult.wasSuccessful()) {
123 return filterQueryAndResponseBuilder.formatAggregationsResponse(opResult.getResult());
125 return buildEmptyAggregateVnfResponseJson();
129 private String buildEmptyAggregateVnfResponseJson() {
130 JsonObject finalOutputToFe = new JsonObject();
131 finalOutputToFe.addProperty(TOTAL, 0);
132 return finalOutputToFe.toString();