Policy 1707 commit to LF
[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 javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.json.JSONObject;
26 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
27 import org.openecomp.policy.common.logging.flexlogger.Logger;
28 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
29 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
30 import org.springframework.stereotype.Controller;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33
34 import com.fasterxml.jackson.databind.DeserializationFeature;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 @Controller
38 @RequestMapping("/")
39 public class PolicySearchController {
40         
41         private static final Logger LOGGER = FlexLogger.getLogger(PolicySearchController.class);
42
43         @RequestMapping(value="/searchPolicy", method= RequestMethod.POST)
44         public void elkTransaction(HttpServletRequest request, HttpServletResponse response) {
45                 try{
46                         boolean result = false;
47                         ObjectMapper mapper = new ObjectMapper();
48                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
49                         PolicyRestAdapter policyData = new PolicyRestAdapter();
50                         if(request.getParameter("policyName") != null){
51                                 String policyName = request.getParameter("policyName");
52                                 policyData.setNewFileName(policyName);
53                                 PolicyElasticSearchController controller = new PolicyElasticSearchController();
54                                 if("delete".equalsIgnoreCase(request.getParameter("action"))){
55                                         result = controller.deleteElk(policyData);
56                                 }else{
57                                         result = controller.updateElk(policyData);
58                                 }
59                         }
60                         String message="";
61                         if(result){
62                                 message = "Elastic Server Transaction is success";
63                         }else{
64                                 message = "Elastic Server Transaction is failed, please check the logs";
65                         }
66                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(message));
67                         JSONObject j = new JSONObject(msg);
68                         response.setStatus(HttpServletResponse.SC_OK);
69                         response.addHeader("success", "success"); 
70                         response.getWriter().write(j.toString());
71                 }catch(Exception e){
72                          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
73                          response.addHeader("error", "Exception Occured While Performing Elastic Transaction");
74                         LOGGER.error("Exception Occured While Performing Elastic Transaction"+e.getMessage());
75                         e.printStackTrace();
76                 }
77         }
78         
79 }