2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.elk.client;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.gson.JsonArray;
28 import io.searchbox.client.JestResult;
30 import java.io.IOException;
31 import java.io.PrintWriter;
32 import java.io.UnsupportedEncodingException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
41 import org.json.JSONObject;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.pap.xacml.rest.adapters.SearchData;
45 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
46 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
47 import org.onap.policy.rest.adapter.PolicyRestAdapter;
48 import org.onap.policy.rest.dao.CommonClassDao;
49 import org.onap.policy.rest.jpa.ActionPolicyDict;
50 import org.onap.policy.rest.jpa.Attribute;
51 import org.onap.policy.rest.jpa.BrmsParamTemplate;
52 import org.onap.policy.rest.jpa.ClosedLoopD2Services;
53 import org.onap.policy.rest.jpa.ClosedLoopSite;
54 import org.onap.policy.rest.jpa.DcaeUuid;
55 import org.onap.policy.rest.jpa.DecisionSettings;
56 import org.onap.policy.rest.jpa.DescriptiveScope;
57 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
58 import org.onap.policy.rest.jpa.MicroServiceLocation;
59 import org.onap.policy.rest.jpa.MicroServiceModels;
60 import org.onap.policy.rest.jpa.OnapName;
61 import org.onap.policy.rest.jpa.PepOptions;
62 import org.onap.policy.rest.jpa.RiskType;
63 import org.onap.policy.rest.jpa.SafePolicyWarning;
64 import org.onap.policy.rest.jpa.TermList;
65 import org.onap.policy.rest.jpa.VnfType;
66 import org.onap.policy.rest.jpa.VsclAction;
67 import org.onap.policy.rest.jpa.VarbindDictionary;
68 import org.onap.policy.utils.PolicyUtils;
69 import org.onap.policy.xacml.api.XACMLErrorConstants;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.stereotype.Controller;
72 import org.springframework.web.bind.annotation.RequestMapping;
73 import org.springframework.web.bind.annotation.RequestMethod;
74 import org.springframework.web.servlet.ModelAndView;
77 @RequestMapping({"/"})
78 public class PolicyElasticSearchController {
80 private static final Logger LOGGER = FlexLogger.getLogger(PolicyElasticSearchController.class);
83 attribute, onapName, actionPolicy, brmsParam, pepOptions, clSite, clService, clVarbind, clVnf, clVSCL, decision, fwTerm, msDCAEUUID, msConfigName, msLocation, msModels, psGroupPolicy, safeRisk, safePolicyWarning
86 protected static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() {
87 private static final long serialVersionUID = 1L;
90 private static CommonClassDao commonClassDao;
91 private static final String action = "action";
92 private static final String config = "config";
93 private static final String decision = "decision";
94 private static final String pholder = "pholder";
95 private static final String jsonBodyData = "jsonBodyData";
96 private static final String success = "success";
99 public PolicyElasticSearchController(CommonClassDao commonClassDao) {
100 PolicyElasticSearchController.commonClassDao = commonClassDao;
103 public PolicyElasticSearchController() {
107 public ElkConnector.PolicyIndexType toPolicyIndexType(String type) throws IllegalArgumentException {
108 if (type == null || type.isEmpty()) {
109 return PolicyIndexType.all;
111 return PolicyIndexType.valueOf(type);
114 public boolean updateElk(PolicyRestAdapter policyData) {
115 boolean success = true;
117 success = ElkConnector.singleton.update(policyData);
119 if (LOGGER.isWarnEnabled()) {
120 LOGGER.warn("FAILURE to create ELK record created for " + policyData.getNewFileName());
123 if (LOGGER.isInfoEnabled()) {
124 LOGGER.warn("SUCCESS creating ELK record created for " + policyData.getNewFileName());
127 } catch (Exception e) {
128 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
134 public boolean deleteElk(PolicyRestAdapter policyData) {
135 boolean success = true;
137 success = ElkConnector.singleton.delete(policyData);
139 if (LOGGER.isWarnEnabled()) {
140 LOGGER.warn("FAILURE to delete ELK record created for " + policyData.getNewFileName());
143 if (LOGGER.isInfoEnabled()) {
144 LOGGER.warn("SUCCESS deleting ELK record created for " + policyData.getNewFileName());
147 } catch (Exception e) {
148 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
154 @RequestMapping(value = "/searchPolicy", method = RequestMethod.POST)
155 public void searchPolicy(HttpServletRequest request, HttpServletResponse response) {
158 boolean result = false;
159 boolean policyResult = false;
160 boolean validationCheck = true;
161 ObjectMapper mapper = new ObjectMapper();
162 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
163 PolicyRestAdapter policyData = new PolicyRestAdapter();
164 PolicyElasticSearchController controller = new PolicyElasticSearchController();
165 Map<String, String> searchKeyValue = new HashMap<>();
166 List<String> policyList = new ArrayList<>();
167 if (request.getParameter("policyName") != null) {
168 String policyName = request.getParameter("policyName");
169 policyData.setNewFileName(policyName);
170 if ("delete".equalsIgnoreCase(request.getParameter(action))) {
171 result = controller.deleteElk(policyData);
173 result = controller.updateElk(policyData);
176 if ("search".equalsIgnoreCase(request.getParameter(action))) {
178 JsonNode root = mapper.readTree(request.getReader());
179 SearchData searchData = mapper.readValue(root.get("searchdata").toString(), SearchData.class);
181 String policyType = searchData.getPolicyType();
183 String searchText = searchData.getQuery();
184 String descriptivevalue = searchData.getDescriptiveScope();
185 if (descriptivevalue != null) {
186 DescriptiveScope dsSearch = (DescriptiveScope) commonClassDao
187 .getEntityItem(DescriptiveScope.class, "descriptiveScopeName", descriptivevalue);
188 if (dsSearch != null) {
189 String[] descriptiveList = dsSearch.getSearch().split("AND");
190 for (String keyValue : descriptiveList) {
191 String[] entry = keyValue.split(":");
192 if (searchData.getPolicyType() != null
193 && "closedLoop".equals(searchData.getPolicyType())) {
194 if (!PolicyUtils.policySpecialCharValidator(entry[1]).contains(success)) {
196 "The Descriptive Scope Dictionary value contains space and it is invalid for Search : "
198 validationCheck = false;
200 searchKeyValue.put(jsonBodyData, "*" + entry[1] + "*");
202 searchText = entry[1];
208 if (!PolicyUtils.policySpecialCharValidator(searchText).contains(success)) {
209 message = "The Search value contains space and it is invalid for Search : " + searchText;
210 validationCheck = false;
213 if (searchData.getClosedLooppolicyType() != null) {
214 String closedLoopType;
215 if ("Config_Fault".equalsIgnoreCase(searchData.getClosedLooppolicyType())) {
216 closedLoopType = "ClosedLoop_Fault";
218 closedLoopType = "ClosedLoop_PM";
220 searchKeyValue.put("configPolicyType", closedLoopType);
222 if (searchData.getOnapName() != null) {
223 searchKeyValue.put("onapName", searchData.getOnapName());
225 if (searchData.getD2Service() != null) {
226 String d2Service = searchData.getD2Service().trim();
227 if ("Hosted Voice (Trinity)".equalsIgnoreCase(d2Service)) {
228 d2Service = "trinity";
229 } else if ("vUSP".equalsIgnoreCase(d2Service)) {
231 } else if ("MCR".equalsIgnoreCase(d2Service)) {
233 } else if ("Gamma".equalsIgnoreCase(d2Service)) {
235 } else if ("vDNS".equalsIgnoreCase(d2Service)) {
238 searchKeyValue.put("jsonBodyData." + d2Service + "", "true");
240 if (searchData.getVnfType() != null) {
241 searchKeyValue.put(jsonBodyData, "*" + searchData.getVnfType() + "*");
243 if (searchData.getPolicyStatus() != null) {
244 searchKeyValue.put(jsonBodyData, "*" + searchData.getPolicyStatus() + "*");
246 if (searchData.getVproAction() != null) {
247 searchKeyValue.put(jsonBodyData, "*" + searchData.getVproAction() + "*");
249 if (searchData.getServiceType() != null) {
250 searchKeyValue.put("serviceType", searchData.getServiceType());
252 if (searchData.getBindTextSearch() != null) {
253 searchKeyValue.put(searchData.getBindTextSearch(), searchText);
256 PolicyIndexType type = null;
257 if (policyType != null) {
258 if (action.equalsIgnoreCase(policyType)) {
259 type = ElkConnector.PolicyIndexType.action;
260 } else if (decision.equalsIgnoreCase(policyType)) {
261 type = ElkConnector.PolicyIndexType.decision;
262 } else if (config.equalsIgnoreCase(policyType)) {
263 type = ElkConnector.PolicyIndexType.config;
264 } else if ("closedloop".equalsIgnoreCase(policyType)) {
265 type = ElkConnector.PolicyIndexType.closedloop;
267 type = ElkConnector.PolicyIndexType.all;
270 type = ElkConnector.PolicyIndexType.all;
272 if (validationCheck) {
273 JestResult policyResultList = controller.search(type, searchText, searchKeyValue);
274 if (policyResultList.isSucceeded()) {
277 JsonArray resultObject = policyResultList.getJsonObject().get("hits").getAsJsonObject()
278 .get("hits").getAsJsonArray();
279 for (int i = 0; i < resultObject.size(); i++) {
280 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
281 policyList.add(policyName);
285 "Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
288 } catch (Exception e) {
289 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e);
292 if (validationCheck) {
294 message = "Elastic Server Transaction is success";
296 message = "Elastic Server Transaction is failed, please check the logs";
299 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(message));
300 JSONObject j = new JSONObject(msg);
301 response.setStatus(HttpServletResponse.SC_OK);
302 response.addHeader(success, success);
304 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
305 response.getWriter().write(k.toString());
307 response.getWriter().write(j.toString());
309 } catch (Exception e) {
310 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
311 response.addHeader("error", "Exception Occured While Performing Elastic Transaction");
312 LOGGER.error("Exception Occured While Performing Elastic Transaction" + e.getMessage(), e);
317 value = {"/searchDictionary"},
318 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
319 public ModelAndView searchDictionary(HttpServletRequest request, HttpServletResponse response)
320 throws UnsupportedEncodingException, IOException {
322 PolicyIndexType config = PolicyIndexType.config;
323 PolicyIndexType closedloop = PolicyIndexType.closedloop;
324 PolicyIndexType action = PolicyIndexType.action;
325 PolicyIndexType decision = PolicyIndexType.decision;
326 PolicyIndexType all = PolicyIndexType.all;
328 ObjectMapper mapper = new ObjectMapper();
329 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
330 JsonNode root = mapper.readTree(request.getReader());
331 String dictionaryType = root.get("type").textValue();
332 Mode mode = Mode.valueOf(dictionaryType);
334 List<String> policyList = new ArrayList<>();
337 Attribute attributedata = mapper.readValue(root.get("data").toString(), Attribute.class);
338 value = attributedata.getXacmlId();
339 policyList = searchElkDatabase(all, pholder, value);
342 OnapName onapName = mapper.readValue(root.get("data").toString(), OnapName.class);
343 value = onapName.getName();
344 policyList = searchElkDatabase(all, "onapName", value);
347 ActionPolicyDict actionPolicyDict =
348 mapper.readValue(root.get("data").toString(), ActionPolicyDict.class);
349 value = actionPolicyDict.getAttributeName();
350 policyList = searchElkDatabase(action, "actionAttributeValue", value);
353 BrmsParamTemplate bRMSParamTemplate =
354 mapper.readValue(root.get("data").toString(), BrmsParamTemplate.class);
355 value = bRMSParamTemplate.getRuleName();
356 policyList = searchElkDatabase(config, "ruleName", value);
359 PepOptions pEPOptions = mapper.readValue(root.get("data").toString(), PepOptions.class);
360 value = pEPOptions.getPepName();
361 policyList = searchElkDatabase(closedloop, "jsonBodyData.pepName", value);
364 ClosedLoopSite closedLoopSite = mapper.readValue(root.get("data").toString(), ClosedLoopSite.class);
365 value = closedLoopSite.getSiteName();
366 policyList = searchElkDatabase(closedloop, "siteNames", value);
369 ClosedLoopD2Services closedLoopD2Services =
370 mapper.readValue(root.get("data").toString(), ClosedLoopD2Services.class);
371 value = closedLoopD2Services.getServiceName();
372 policyList = searchElkDatabase(closedloop, pholder, value);
375 VarbindDictionary varbindDictionary =
376 mapper.readValue(root.get("data").toString(), VarbindDictionary.class);
377 value = varbindDictionary.getVarbindName();
378 policyList = searchElkDatabase(closedloop, jsonBodyData, "*" + value + "*");
381 VnfType vNFType = mapper.readValue(root.get("data").toString(), VnfType.class);
382 value = vNFType.getType();
383 policyList = searchElkDatabase(closedloop, jsonBodyData, "*" + value + "*");
386 VsclAction vsclAction = mapper.readValue(root.get("data").toString(), VsclAction.class);
387 value = vsclAction.getAction();
388 policyList = searchElkDatabase(closedloop, jsonBodyData, "*" + value + "*");
391 DecisionSettings decisionSettings =
392 mapper.readValue(root.get("data").toString(), DecisionSettings.class);
393 value = decisionSettings.getXacmlId();
394 policyList = searchElkDatabase(decision, pholder, value);
397 TermList term = mapper.readValue(root.get("data").toString(), TermList.class);
398 value = term.getTermName();
399 policyList = searchElkDatabase(config, pholder, value);
402 DcaeUuid dcaeUUID = mapper.readValue(root.get("data").toString(), DcaeUuid.class);
403 value = dcaeUUID.getName();
404 policyList = searchElkDatabase(config, "uuid", value);
407 MicroServiceLocation mslocation =
408 mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
409 value = mslocation.getName();
410 policyList = searchElkDatabase(config, "location", value);
413 MicroServiceModels msModels =
414 mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
415 value = msModels.getModelName();
416 policyList = searchElkDatabase(config, "serviceType", value);
419 GroupPolicyScopeList groupPoilicy =
420 mapper.readValue(root.get("data").toString(), GroupPolicyScopeList.class);
421 value = groupPoilicy.getGroupName();
422 policyList = searchElkDatabase(config, pholder, value);
425 RiskType riskType = mapper.readValue(root.get("data").toString(), RiskType.class);
426 value = riskType.getName();
427 policyList = searchElkDatabase(config, "riskType", value);
429 case safePolicyWarning:
430 SafePolicyWarning safePolicy =
431 mapper.readValue(root.get("data").toString(), SafePolicyWarning.class);
432 value = safePolicy.getName();
433 policyList = searchElkDatabase(config, pholder, value);
438 response.setStatus(HttpServletResponse.SC_OK);
439 response.addHeader(success, success);
440 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
441 response.getWriter().write(k.toString());
442 } catch (Exception e) {
443 response.setCharacterEncoding("UTF-8");
444 request.setCharacterEncoding("UTF-8");
445 PrintWriter out = response.getWriter();
446 out.write(PolicyUtils.CATCH_EXCEPTION);
452 // Search the Elk database
453 public List<String> searchElkDatabase(PolicyIndexType type, String key, String value) {
454 PolicyElasticSearchController controller = new PolicyElasticSearchController();
455 Map<String, String> searchKeyValue = new HashMap<>();
456 if (!pholder.equals(key)) {
457 searchKeyValue.put(key, value);
460 List<String> policyList = new ArrayList<>();
461 JestResult policyResultList = controller.search(type, value, searchKeyValue);
462 if (policyResultList.isSucceeded()) {
463 JsonArray resultObject =
464 policyResultList.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
465 for (int i = 0; i < resultObject.size(); i++) {
466 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
467 policyList.add(policyName);
470 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
475 public JestResult search(PolicyIndexType type, String text, Map<String, String> searchKeyValue) {
476 return ElkConnector.singleton.search(type, text, searchKeyValue);