Policy 1707 Second commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / elk / client / PolicySearchController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.openecomp.policy.pap.xacml.rest.elk.client;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
42
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;
47
48 import io.searchbox.client.JestResult;
49
50 @Controller
51 @RequestMapping("/")
52 public class PolicySearchController {
53         
54         private static final Logger LOGGER = FlexLogger.getLogger(PolicySearchController.class);
55
56         @Autowired
57         CommonClassDao commonClassDao;
58         
59         @RequestMapping(value="/searchPolicy", method= RequestMethod.POST)
60         public void elkTransaction(HttpServletRequest request, HttpServletResponse response) {
61                 try{
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);
75                                 }else{
76                                         result = controller.updateElk(policyData);
77                                 }
78                         }
79                         if("search".equalsIgnoreCase(request.getParameter("action"))){
80                                 try {
81                                         JsonNode root = mapper.readTree(request.getReader());
82                                         SearchData searchData = (SearchData)mapper.readValue(root.get("searchdata").toString(), SearchData.class);
83
84                                         String policyType = searchData.getPolicyType();
85                                         
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());
90                                                 if(dsSearch != null){
91                                                         String[] descriptiveList =  dsSearch.getSearch().split("AND");
92                                                         for(String keyValue : descriptiveList){
93                                                                 String[] entry = keyValue.split(":");
94                                                                 searchKeyValue.put(entry[0], entry[1]);
95                                                         }
96                                                 }
97                                         }
98                                         
99                                         if(searchData.getClosedLooppolicyType() != null){
100                                                 String closedLoopType;
101                                                 if("Config_Fault".equalsIgnoreCase(searchData.getClosedLooppolicyType())){
102                                                         closedLoopType  = "ClosedLoop_Fault";
103                                                 }else{
104                                                         closedLoopType  = "ClosedLoop_PM";
105                                                 }
106                                                 searchKeyValue.put("configPolicyType", closedLoopType);
107                                         }
108                                         if(searchData.getEcompName() != null){
109                                                 searchKeyValue.put("ecompName", searchData.getEcompName());
110                                         }
111                                         if(searchData.getD2Service() != null){
112                                                 searchKeyValue.put("_all", searchData.getD2Service());
113                                         }       
114                                         if(searchData.getVnfType() != null){
115                                                 searchKeyValue.put("_all", searchData.getVnfType());                                    
116                                         }
117                                         if(searchData.getPolicyStatus() != null){
118                                                 searchKeyValue.put("_all", searchData.getPolicyStatus());
119                                         }
120                                         if(searchData.getVproAction() != null){
121                                                 searchKeyValue.put("_all", searchData.getVproAction());
122                                         }
123                                         if(searchData.getServiceType() != null){
124                                                 searchKeyValue.put("_all", searchData.getServiceType());
125                                         }
126                                         if(searchData.getBindTextSearch() != null){
127                                                 searchKeyValue.put("_all", searchData.getBindTextSearch());
128                                         }
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;
137                                                 }else {
138                                                         type = ElkConnector.PolicyIndexType.closedloop;
139                                                 }
140                                         }else{
141                                                 type = ElkConnector.PolicyIndexType.all;
142                                         }
143                                         JestResult policyResultList = controller.search(type, searchText, searchKeyValue);
144                                         if(policyResultList.isSucceeded()){
145                                                 result = true;
146                                                 policyResult = true;
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);
151                                                 }
152                                         }else{
153                                                 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
154                                         }
155                                 }catch(Exception e){
156                                         LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e);
157                                 }
158                         }
159                         String message="";
160                         if(result){
161                                 message = "Elastic Server Transaction is success";
162                         }else{
163                                 message = "Elastic Server Transaction is failed, please check the logs";
164                         }
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"); 
169                         if(policyResult){
170                                 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
171                                 response.getWriter().write(k.toString());
172                         }else{
173                                 response.getWriter().write(j.toString());
174                         }
175                 }catch(Exception e){
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());
179                         e.printStackTrace();
180                 }
181         }
182         
183 }