2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.inventory;
25 import java.io.IOException;
27 import org.apache.camel.Exchange;
28 import org.apache.camel.component.restlet.RestletConstants;
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.cl.mdc.MdcContext;
34 import org.onap.aai.restclient.client.OperationResult;
35 import org.onap.aai.sparky.dal.elasticsearch.SearchAdapter;
36 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;
37 import org.onap.aai.sparky.logging.AaiUiMsgs;
38 import org.onap.aai.sparky.logging.util.ServletUtils;
39 import org.onap.aai.sparky.util.NodeUtils;
40 import org.restlet.Request;
41 import org.restlet.Response;
42 import org.restlet.data.ClientInfo;
43 import org.restlet.data.Form;
44 import org.restlet.data.MediaType;
45 import org.restlet.data.Parameter;
46 import org.restlet.data.Status;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
52 * The Class GeoVisualizationServlet.
54 public class GeoVisualizationProcessor {
56 private static final Logger LOG =
57 LoggerFactory.getInstance().getLogger(GeoVisualizationProcessor.class);
59 private ObjectMapper mapper;
60 private SearchAdapter search = null;
61 private ElasticSearchConfig elasticConfig = null;
63 private static final String SEARCH_STRING = "_search";
64 private static final String SEARCH_PARAMETER =
65 "?filter_path=hits.hits._source&_source=location&size=5000&q=entityType:";
66 private static final String PARAMETER_KEY = "entity";
69 * Instantiates a new geo visualization processor
71 public GeoVisualizationProcessor() {
72 this.mapper = new ObjectMapper();
75 if (elasticConfig == null) {
76 elasticConfig = ElasticSearchConfig.getConfig();
79 search = new SearchAdapter();
81 this.mapper = new ObjectMapper();
82 } catch (Exception exc) {
87 public void setSearch(SearchAdapter search) {
91 public void setElasticConfig(ElasticSearchConfig elasticConfig) {
92 this.elasticConfig = elasticConfig;
96 * Gets the geo visualization results.
98 * @param response the response
99 * @param entityType the entity type
100 * @return the geo visualization results
101 * @throws Exception the exception
103 protected OperationResult getGeoVisualizationResults(Exchange exchange) throws Exception {
104 OperationResult operationResult = new OperationResult();
107 Object xTransactionId = exchange.getIn().getHeader("X-TransactionId");
108 if (xTransactionId == null) {
109 xTransactionId = NodeUtils.getRandomTxnId();
112 Object partnerName = exchange.getIn().getHeader("X-FromAppId");
113 if (partnerName == null) {
114 partnerName = "Browser";
117 Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
120 * Disables automatic Apache Camel Restlet component logging which prints out an undesirable log
121 * entry which includes client (e.g. browser) information
123 request.setLoggable(false);
125 ClientInfo clientInfo = request.getClientInfo();
126 MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName,
127 clientInfo.getAddress() + ":" + clientInfo.getPort());
129 String entityType = "";
131 Form form = request.getResourceRef().getQueryAsForm();
132 for (Parameter parameter : form) {
133 if (PARAMETER_KEY.equals(parameter.getName())) {
134 entityType = parameter.getName();
138 String parameters = SEARCH_PARAMETER + entityType;
139 String requestString = String.format("/%s/%s/%s", elasticConfig.getTopographicalSearchIndex(),
140 SEARCH_STRING, parameters);
143 final String fullUrlStr = ServletUtils.getFullUrl(elasticConfig, requestString);
144 OperationResult opResult = search.doGet(fullUrlStr, "application/json");
146 JSONObject finalOutputJson = formatOutput(opResult.getResult());
149 exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
150 response.setStatus(Status.SUCCESS_OK);
151 response.setEntity(String.valueOf(finalOutputJson), MediaType.APPLICATION_JSON);
152 exchange.getOut().setBody(response);
154 } catch (Exception exc) {
155 LOG.error(AaiUiMsgs.ERROR_GENERIC, "Error processing Geo Visualization request");
158 return operationResult;
164 * @param results the results
165 * @return the JSON object
167 private JSONObject formatOutput(String results) {
168 JsonNode resultNode = null;
169 JSONObject finalResult = new JSONObject();
170 JSONArray entitiesArr = new JSONArray();
173 resultNode = mapper.readTree(results);
175 final JsonNode hitsNode = resultNode.get("hits").get("hits");
176 if (hitsNode.isArray()) {
178 for (final JsonNode arrayNode : hitsNode) {
179 JsonNode sourceNode = arrayNode.get("_source");
180 if (sourceNode.get("location") != null) {
181 JsonNode locationNode = sourceNode.get("location");
182 if (NodeUtils.isNumeric(locationNode.get("lon").asText())
183 && NodeUtils.isNumeric(locationNode.get("lat").asText())) {
184 JSONObject location = new JSONObject();
185 location.put("longitude", locationNode.get("lon").asText());
186 location.put("latitude", locationNode.get("lat").asText());
188 entitiesArr.put(location);
194 finalResult.put("plotPoints", entitiesArr);
196 } catch (IOException exc) {
197 LOG.warn(AaiUiMsgs.ERROR_BUILDING_SEARCH_RESPONSE, exc.getLocalizedMessage());