ES to SDS conversion
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / aggregatevnf / search / AggregateSummaryProcessor.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 javax.servlet.http.HttpServletRequest;
24
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;
35
36 import com.google.gson.Gson;
37 import com.google.gson.JsonArray;
38 import com.google.gson.JsonObject;
39
40 public class AggregateSummaryProcessor {
41
42   private static final Logger LOG = LoggerFactory.getInstance().getLogger(AggregateSummaryProcessor.class);
43
44   private static final String KEY_FILTERS = "filters";
45   private static final String TOTAL = "total";
46
47   private SearchServiceAdapter searchServiceAdapter = null;
48
49   private String vnfAggregationIndexName;
50
51   private FilterQueryAndResponseBuilder filterQueryAndResponseBuilder;
52   private Gson converter;
53
54   public AggregateSummaryProcessor(SearchServiceAdapter searchServiceAdapter,
55       FiltersConfig filtersConfig) {
56     this.searchServiceAdapter = searchServiceAdapter;
57     this.filterQueryAndResponseBuilder = new FilterQueryAndResponseBuilder(filtersConfig);
58     this.converter = new Gson();
59   }
60
61   public void setVnfAggregationIndexName(String vnfAggregationIndexName) {
62     this.vnfAggregationIndexName = vnfAggregationIndexName;
63   }
64
65   public void getFilteredAggregation(Exchange exchange) {
66
67     HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
68     ServletUtils.setUpMdcContext(exchange, request);
69
70     try {
71       String payload = exchange.getIn().getBody(String.class);
72
73       if (payload == null || payload.isEmpty()) {
74
75         LOG.error(AaiUiMsgs.SEARCH_SERVLET_ERROR, "Request Payload is empty");
76
77         /*
78          * Don't throw back an error, just return an empty set
79          */
80
81       } else {
82         JsonObject payloadObj = converter.fromJson(payload, JsonObject.class);
83         if (payloadObj.has(KEY_FILTERS)) {
84
85           String jsonResponsePayload = getVnfFilterAggregations(payload);
86           
87           exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
88           exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
89           exchange.getOut().setBody(jsonResponsePayload);
90
91         } else {
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);
97         }
98       }
99     } catch (Exception exc) {
100       LOG.error(AaiUiMsgs.ERROR_GENERIC,
101           "AggregateSummaryProcessor failed to process request due to error = " + exc.getMessage());
102     }
103   }
104
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);
111
112     return payload.toString();
113   }
114
115   private String getVnfFilterAggregations(String payload) {
116     String query = filterQueryAndResponseBuilder.createFileBasedFilterQuery(payload);
117     
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);
121
122     if (opResult.wasSuccessful()) {
123       return filterQueryAndResponseBuilder.formatAggregationsResponse(opResult.getResult());
124     } else {
125       return buildEmptyAggregateVnfResponseJson();
126     }
127   }
128
129   private String buildEmptyAggregateVnfResponseJson() {
130     JsonObject finalOutputToFe = new JsonObject();
131     finalOutputToFe.addProperty(TOTAL, 0);
132     return finalOutputToFe.toString();
133   }
134 }