2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.openecomp.policy.pap.xacml.rest.elk.client;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.json.JSONObject;
31 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
32 import org.openecomp.policy.common.logging.flexlogger.Logger;
33 import org.openecomp.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
34 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
35 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
36 import org.openecomp.policy.rest.dao.CommonClassDao;
37 import org.openecomp.policy.rest.jpa.DescriptiveScope;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
43 import com.fasterxml.jackson.databind.DeserializationFeature;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.google.gson.JsonArray;
48 import io.searchbox.client.JestResult;
52 public class PolicySearchController {
54 private static final Logger LOGGER = FlexLogger.getLogger(PolicySearchController.class);
57 CommonClassDao commonClassDao;
59 @RequestMapping(value="/searchPolicy", method= RequestMethod.POST)
60 public void elkTransaction(HttpServletRequest request, HttpServletResponse response) {
62 boolean result = false;
63 boolean policyResult = false;
64 ObjectMapper mapper = new ObjectMapper();
65 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
66 PolicyRestAdapter policyData = new PolicyRestAdapter();
67 PolicyElasticSearchController controller = new PolicyElasticSearchController();
68 Map<String, String> searchKeyValue = new HashMap<String, String>();
69 List<String> policyList = new ArrayList<String>();
70 if(request.getParameter("policyName") != null){
71 String policyName = request.getParameter("policyName");
72 policyData.setNewFileName(policyName);
73 if("delete".equalsIgnoreCase(request.getParameter("action"))){
74 result = controller.deleteElk(policyData);
76 result = controller.updateElk(policyData);
79 if("search".equalsIgnoreCase(request.getParameter("action"))){
81 JsonNode root = mapper.readTree(request.getReader());
82 SearchData searchData = (SearchData)mapper.readValue(root.get("searchdata").toString(), SearchData.class);
84 String policyType = searchData.getPolicyType();
86 String searchText = searchData.getQuery();
87 String descriptivevalue = searchData.getDescriptiveScope();
88 if(descriptivevalue != null){
89 DescriptiveScope dsSearch = (DescriptiveScope) commonClassDao.getEntityItem(DescriptiveScope.class, "descriptiveScopeName", searchData.getDescriptiveScope());
91 String[] descriptiveList = dsSearch.getSearch().split("AND");
92 for(String keyValue : descriptiveList){
93 String[] entry = keyValue.split(":");
94 searchKeyValue.put(entry[0], entry[1]);
99 if(searchData.getClosedLooppolicyType() != null){
100 String closedLoopType;
101 if("Config_Fault".equalsIgnoreCase(searchData.getClosedLooppolicyType())){
102 closedLoopType = "ClosedLoop_Fault";
104 closedLoopType = "ClosedLoop_PM";
106 searchKeyValue.put("configPolicyType", closedLoopType);
108 if(searchData.getEcompName() != null){
109 searchKeyValue.put("ecompName", searchData.getEcompName());
111 if(searchData.getD2Service() != null){
112 searchKeyValue.put("_all", searchData.getD2Service());
114 if(searchData.getVnfType() != null){
115 searchKeyValue.put("_all", searchData.getVnfType());
117 if(searchData.getPolicyStatus() != null){
118 searchKeyValue.put("_all", searchData.getPolicyStatus());
120 if(searchData.getVproAction() != null){
121 searchKeyValue.put("_all", searchData.getVproAction());
123 if(searchData.getServiceType() != null){
124 searchKeyValue.put("_all", searchData.getServiceType());
126 if(searchData.getBindTextSearch() != null){
127 searchKeyValue.put("_all", searchData.getBindTextSearch());
129 PolicyIndexType type = null;
130 if(policyType != null){
131 if(policyType.equalsIgnoreCase("action")){
132 type = ElkConnector.PolicyIndexType.action;
133 }else if(policyType.equalsIgnoreCase("decision")){
134 type = ElkConnector.PolicyIndexType.decision;
135 }else if(policyType.equalsIgnoreCase("config")){
136 type = ElkConnector.PolicyIndexType.config;
138 type = ElkConnector.PolicyIndexType.closedloop;
141 type = ElkConnector.PolicyIndexType.all;
143 JestResult policyResultList = controller.search(type, searchText, searchKeyValue);
144 if(policyResultList.isSucceeded()){
147 JsonArray resultObject = policyResultList.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
148 for(int i =0; i < resultObject.size(); i++){
149 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
150 policyList.add(policyName);
153 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
156 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e);
161 message = "Elastic Server Transaction is success";
163 message = "Elastic Server Transaction is failed, please check the logs";
165 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(message));
166 JSONObject j = new JSONObject(msg);
167 response.setStatus(HttpServletResponse.SC_OK);
168 response.addHeader("success", "success");
170 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
171 response.getWriter().write(k.toString());
173 response.getWriter().write(j.toString());
176 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
177 response.addHeader("error", "Exception Occured While Performing Elastic Transaction");
178 LOGGER.error("Exception Occured While Performing Elastic Transaction"+e.getMessage());