Merge "Decision BlackList Guard Enhancements"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21 package org.onap.policy.admin;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.PrintWriter;
29 import java.net.HttpURLConnection;
30 import java.net.URL;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Base64;
34 import java.util.List;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38
39 import org.apache.commons.fileupload.FileItem;
40 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
41 import org.apache.commons.fileupload.servlet.ServletFileUpload;
42 import org.apache.commons.io.IOUtils;
43 import org.json.JSONObject;
44 import org.onap.policy.common.logging.flexlogger.FlexLogger;
45 import org.onap.policy.common.logging.flexlogger.Logger;
46 import org.onap.policy.controller.CreateClosedLoopFaultController;
47 import org.onap.policy.controller.CreateDcaeMicroServiceController;
48 import org.onap.policy.controller.CreateFirewallController;
49 import org.onap.policy.controller.CreateOptimizationController;
50 import org.onap.policy.controller.PolicyController;
51 import org.onap.policy.rest.XACMLRestProperties;
52 import org.onap.policy.rest.adapter.PolicyRestAdapter;
53 import org.onap.policy.rest.dao.CommonClassDao;
54 import org.onap.policy.rest.jpa.PolicyVersion;
55 import org.onap.policy.utils.PolicyUtils;
56 import org.onap.policy.xacml.api.XACMLErrorConstants;
57 import org.onap.portalsdk.core.controller.RestrictedBaseController;
58 import org.onap.portalsdk.core.web.support.UserUtils;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.http.HttpEntity;
61 import org.springframework.http.HttpHeaders;
62 import org.springframework.http.HttpMethod;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.RequestMethod;
67 import org.springframework.web.bind.annotation.RestController;
68 import org.springframework.web.client.HttpClientErrorException;
69 import org.springframework.web.client.RestTemplate;
70 import org.springframework.web.servlet.ModelAndView;
71 import org.onap.policy.utils.CryptoUtils;
72 import com.att.research.xacml.util.XACMLProperties;
73 import com.fasterxml.jackson.databind.DeserializationFeature;
74 import com.fasterxml.jackson.databind.JsonNode;
75 import com.fasterxml.jackson.databind.ObjectMapper;
76 import com.fasterxml.jackson.databind.SerializationFeature;
77
78 @RestController
79 @RequestMapping("/")
80 public class PolicyRestController extends RestrictedBaseController{
81
82     private static final Logger policyLogger = FlexLogger.getLogger(PolicyRestController.class);
83
84     private static final String model = "model";
85     private static final String importDictionary = "import_dictionary";
86
87     private static CommonClassDao commonClassDao;
88
89     public PolicyRestController(){
90         //default constructor
91     }
92
93     @Autowired
94     private PolicyRestController(CommonClassDao commonClassDao){
95         PolicyRestController.commonClassDao = commonClassDao;
96     }
97
98     public static CommonClassDao getCommonClassDao() {
99         return commonClassDao;
100     }
101
102     public static void setCommonClassDao(CommonClassDao commonClassDao) {
103         PolicyRestController.commonClassDao = commonClassDao;
104     }
105
106
107
108     @RequestMapping(value={"/policycreation/save_policy"}, method={RequestMethod.POST})
109     public void policyCreationController(HttpServletRequest request, HttpServletResponse response) {
110         String userId = UserUtils.getUserSession(request).getOrgUserId();
111         ObjectMapper mapper = new ObjectMapper();
112         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
113         try{
114             JsonNode root = mapper.readTree(request.getReader());
115
116             policyLogger.info("****************************************Logging UserID while Create/Update Policy**************************************************");
117             policyLogger.info("UserId:  " + userId + "Policy Data Object:  "+ root.get(PolicyController.getPolicydata()).get("policy").toString());
118             policyLogger.info("***********************************************************************************************************************************");
119
120             PolicyRestAdapter policyData = mapper.readValue(root.get(PolicyController.getPolicydata()).get("policy").toString(), PolicyRestAdapter.class);
121
122             if("file".equals(root.get(PolicyController.getPolicydata()).get(model).get("type").toString().replace("\"", ""))){
123                 policyData.setEditPolicy(true);
124             }
125             if(root.get(PolicyController.getPolicydata()).get(model).get("path").size() != 0){
126                 String dirName = "";
127                 for(int i = 0; i < root.get(PolicyController.getPolicydata()).get(model).get("path").size(); i++){
128                     dirName = dirName.replace("\"", "") + root.get(PolicyController.getPolicydata()).get(model).get("path").get(i).toString().replace("\"", "") + File.separator;
129                 }
130                 if(policyData.isEditPolicy()){
131                     policyData.setDomainDir(dirName.substring(0, dirName.lastIndexOf(File.separator)));
132                 }else{
133                     policyData.setDomainDir(dirName + root.get(PolicyController.getPolicydata()).get(model).get("name").toString().replace("\"", ""));
134                 }
135             }else{
136                 String domain = root.get(PolicyController.getPolicydata()).get(model).get("name").toString();
137                 if(domain.contains("/")){
138                     domain = domain.substring(0, domain.lastIndexOf('/')).replace("/", File.separator);
139                 }
140                 domain = domain.replace("\"", "");
141                 policyData.setDomainDir(domain);
142             }
143
144             if(policyData.getConfigPolicyType() != null){
145                 if("ClosedLoop_Fault".equalsIgnoreCase(policyData.getConfigPolicyType())){
146                     policyData = new CreateClosedLoopFaultController().setDataToPolicyRestAdapter(policyData, root);
147                 }else if("Firewall Config".equalsIgnoreCase(policyData.getConfigPolicyType())){
148                     policyData = new CreateFirewallController().setDataToPolicyRestAdapter(policyData);
149                 }else if("Micro Service".equalsIgnoreCase(policyData.getConfigPolicyType())){
150                     policyData = new CreateDcaeMicroServiceController().setDataToPolicyRestAdapter(policyData, root);
151                 }else if("Optimization".equalsIgnoreCase(policyData.getConfigPolicyType())){
152                     policyData = new CreateOptimizationController().setDataToPolicyRestAdapter(policyData, root);
153                 }
154             }
155
156             policyData.setUserId(userId);
157
158             String result;
159             String body = PolicyUtils.objectToJsonString(policyData);
160             String uri = request.getRequestURI();
161             ResponseEntity<?> responseEntity = sendToPAP(body, uri, HttpMethod.POST);
162             if(responseEntity != null && responseEntity.getBody().equals(HttpServletResponse.SC_CONFLICT)){
163                 result = "PolicyExists";
164             }else if(responseEntity != null){
165                 result =  responseEntity.getBody().toString();
166                 String policyName = responseEntity.getHeaders().get("policyName").get(0);
167                 if(policyData.isEditPolicy() && "success".equalsIgnoreCase(result)){
168                     PolicyNotificationMail email = new PolicyNotificationMail();
169                     String mode = "EditPolicy";
170                     String watchPolicyName = policyName.replace(".xml", "");
171                     String version = watchPolicyName.substring(watchPolicyName.lastIndexOf('.')+1);
172                     watchPolicyName = watchPolicyName.substring(0, watchPolicyName.lastIndexOf('.')).replace(".", File.separator);
173                     String policyVersionName = watchPolicyName.replace(".", File.separator);
174                     watchPolicyName = watchPolicyName + "." + version + ".xml";
175                     PolicyVersion entityItem = new PolicyVersion();
176                     entityItem.setPolicyName(policyVersionName);
177                     entityItem.setActiveVersion(Integer.parseInt(version));
178                     entityItem.setModifiedBy(userId);
179                     email.sendMail(entityItem, watchPolicyName, mode, commonClassDao);
180                 }
181             }else{
182                 result =  "Response is null from PAP";
183             }
184
185             response.setCharacterEncoding(PolicyController.getCharacterencoding());
186             response.setContentType(PolicyController.getContenttype());
187             request.setCharacterEncoding(PolicyController.getCharacterencoding());
188
189             PrintWriter out = response.getWriter();
190             String responseString = mapper.writeValueAsString(result);
191             JSONObject j = new JSONObject("{policyData: " + responseString + "}");
192             out.write(j.toString());
193         }catch(Exception e){
194             policyLogger.error("Exception Occured while saving policy" , e);
195         }
196     }
197
198
199     private ResponseEntity<?> sendToPAP(String body, String requestURI, HttpMethod method){
200         String papUrl = PolicyController.getPapUrl();
201         String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
202         String papPass = CryptoUtils.decryptTxtNoExStr(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS));
203
204         Base64.Encoder encoder = Base64.getEncoder();
205         String encoding = encoder.encodeToString((papID+":"+papPass).getBytes(StandardCharsets.UTF_8));
206         HttpHeaders headers = new HttpHeaders();
207         headers.set("Authorization", "Basic " + encoding);
208         headers.set("Content-Type", PolicyController.getContenttype());
209
210         RestTemplate restTemplate = new RestTemplate();
211         HttpEntity<?> requestEntity = new HttpEntity<>(body, headers);
212         ResponseEntity<?> result = null;
213         HttpClientErrorException exception = null;
214         String uri = requestURI;
215         if(uri.startsWith("/")){
216             uri = uri.substring(uri.indexOf('/')+1);
217         }
218         uri = "onap" + uri.substring(uri.indexOf('/'));
219         try{
220             result = restTemplate.exchange(papUrl + uri, method, requestEntity, String.class);
221         }catch(Exception e){
222             policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while connecting to " + papUrl, e);
223             exception = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
224             if("409 Conflict".equals(e.getMessage())){
225                 return ResponseEntity.ok(HttpServletResponse.SC_CONFLICT);
226             }
227         }
228         if(exception != null && exception.getStatusCode()!=null){
229             if(exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED)){
230                 String message = XACMLErrorConstants.ERROR_PERMISSIONS +":"+exception.getStatusCode()+":" + "ERROR_AUTH_GET_PERM" ;
231                 policyLogger.error(message);
232             }
233             if(exception.getStatusCode().equals(HttpStatus.BAD_REQUEST)){
234                 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + ":"+exception.getStatusCode()+":" + exception.getResponseBodyAsString();
235                 policyLogger.error(message);
236             }
237             if(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)){
238                 String message = XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while connecting to " + papUrl + exception;
239                 policyLogger.error(message);
240             }
241             String message = XACMLErrorConstants.ERROR_PROCESS_FLOW + ":"+exception.getStatusCode()+":" + exception.getResponseBodyAsString();
242             policyLogger.error(message);
243         }
244         return result;
245     }
246
247     private String callPAP(HttpServletRequest request , String method, String uriValue){
248         String uri = uriValue;
249         String boundary = null;
250         String papUrl = PolicyController.getPapUrl();
251         String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
252         String papPass = CryptoUtils.decryptTxtNoExStr(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS));
253
254         Base64.Encoder encoder = Base64.getEncoder();
255         String encoding = encoder.encodeToString((papID+":"+papPass).getBytes(StandardCharsets.UTF_8));
256         HttpHeaders headers = new HttpHeaders();
257         headers.set("Authorization", "Basic " + encoding);
258         headers.set("Content-Type", PolicyController.getContenttype());
259
260
261         HttpURLConnection connection = null;
262         List<FileItem> items;
263         FileItem item = null;
264         File file = null;
265         if(uri.contains(importDictionary)){
266             try {
267                 items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
268                 item = items.get(0);
269                 file = new File(item.getName());
270                 String newFile = file.toString();
271                 uri = uri +"&dictionaryName="+newFile;
272             } catch (Exception e2) {
273                 policyLogger.error("Exception Occured while calling PAP with import dictionary request"+e2);
274             }
275         }
276
277         try {
278             URL url = new URL(papUrl + uri);
279             connection = (HttpURLConnection)url.openConnection();
280             connection.setRequestMethod(method);
281             connection.setUseCaches(false);
282             connection.setInstanceFollowRedirects(false);
283             connection.setRequestProperty("Authorization", "Basic " + encoding);
284             connection.setDoOutput(true);
285             connection.setDoInput(true);
286
287             if(!uri.contains("searchPolicy?action=delete&")){
288
289                 if(!(uri.endsWith("set_BRMSParamData") || uri.contains(importDictionary))){
290                     connection.setRequestProperty("Content-Type",PolicyController.getContenttype());
291                     ObjectMapper mapper = new ObjectMapper();
292                     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
293                     JsonNode root = getJsonNode(request, mapper);
294
295                     ObjectMapper mapper1 = new ObjectMapper();
296                     mapper1.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
297
298                     Object obj = mapper1.treeToValue(root, Object.class);
299                     String json = mapper1.writeValueAsString(obj);
300
301                     Object content =  new ByteArrayInputStream(json.getBytes());
302
303                     if (content instanceof InputStream) {
304                         // send current configuration
305                         try (OutputStream os = connection.getOutputStream()) {
306                             int count = IOUtils.copy((InputStream) content, os);
307                             if (policyLogger.isDebugEnabled()) {
308                                 policyLogger.debug("copied to output, bytes=" + count);
309                             }
310                         }
311                     }
312                 }else{
313                     if(uri.endsWith("set_BRMSParamData")){
314                         connection.setRequestProperty("Content-Type",PolicyController.getContenttype());
315                         try (OutputStream os = connection.getOutputStream()) {
316                             IOUtils.copy((InputStream) request.getInputStream(), os);
317                         }
318                     }else{
319                         boundary = "===" + System.currentTimeMillis() + "===";
320                         connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
321                         try (OutputStream os = connection.getOutputStream()) {
322                             if(item != null){
323                                 IOUtils.copy((InputStream) item.getInputStream(), os);
324                             }
325                         }
326                     }
327                 }
328             }
329             return doConnect(connection);
330         } catch (Exception e) {
331             policyLogger.error("Exception Occured"+e);
332         }finally{
333             if(file != null && file.exists() && file.delete()){
334                 policyLogger.info("File Deleted Successfully");
335             }
336             if (connection != null) {
337                 try {
338                     // For some reason trying to get the inputStream from the connection
339                     // throws an exception rather than returning null when the InputStream does not exist.
340                     InputStream is = connection.getInputStream();
341                     if (is != null) {
342                         is.close();
343                     }
344                 } catch (IOException ex) {
345                     policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to close connection: " + ex, ex);
346                 }
347                 connection.disconnect();
348             }
349         }
350         return null;
351     }
352
353     private JsonNode getJsonNode(HttpServletRequest request, ObjectMapper mapper) {
354         JsonNode root = null;
355         try {
356             root = mapper.readTree(request.getReader());
357         }catch (Exception e1) {
358             policyLogger.error("Exception Occured while calling PAP"+e1);
359         }
360         return root;
361     }
362
363     private String doConnect(final HttpURLConnection connection) throws IOException{
364         connection.connect();
365         int responseCode = connection.getResponseCode();
366         if(responseCode == 200){
367             // get the response content into a String
368             String responseJson = null;
369             // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
370             try(java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream())) {
371                 scanner.useDelimiter("\\A");
372                 responseJson = scanner.hasNext() ? scanner.next() : "";
373             } catch (Exception e){
374                 //Reason for rethrowing the exception is if any exception occurs during reading of inputsteam
375                 //then the exception handling is done by the outer block without returning the response immediately
376                 //Also finally block is existing only in outer block and not here so all exception handling is
377                 //done in only one place
378                 policyLogger.error("Exception Occured"+e);
379                 throw e;
380             }
381
382             policyLogger.info("JSON response from PAP: " + responseJson);
383             return responseJson;
384         }
385         return null;
386     }
387
388     @RequestMapping(value={"/getDictionary/*"}, method={RequestMethod.GET})
389     public void getDictionaryController(HttpServletRequest request, HttpServletResponse response){
390         String uri = request.getRequestURI().replace("/getDictionary", "");
391         String body;
392         ResponseEntity<?> responseEntity = sendToPAP(null, uri, HttpMethod.GET);
393         if(responseEntity != null){
394             body = responseEntity.getBody().toString();
395         }else{
396             body = "";
397         }
398         try {
399             response.getWriter().write(body);
400         } catch (IOException e) {
401             policyLogger.error("Exception occured while getting Dictionary entries", e);
402         }
403     }
404
405     @RequestMapping(value={"/saveDictionary/*/*"}, method={RequestMethod.POST})
406     public void saveDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException{
407         String userId = "";
408         String uri = request.getRequestURI().replace("/saveDictionary", "");
409         if(uri.startsWith("/")){
410             uri = uri.substring(uri.indexOf('/')+1);
411         }
412         uri = "/onap" + uri.substring(uri.indexOf('/'));
413         if(uri.contains(importDictionary)){
414             userId = UserUtils.getUserSession(request).getOrgUserId();
415             uri = uri+ "?userId=" +userId;
416         }
417
418         policyLogger.info("****************************************Logging UserID while Saving Dictionary*****************************************************");
419         policyLogger.info("UserId:  " + userId);
420         policyLogger.info("***********************************************************************************************************************************");
421
422         String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
423         if(body != null && !body.isEmpty()){
424             response.getWriter().write(body);
425         }else{
426             response.getWriter().write("Failed");
427         }
428     }
429
430     @RequestMapping(value={"/deleteDictionary/*/*"}, method={RequestMethod.POST})
431     public void deletetDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
432         String uri = request.getRequestURI().replace("/deleteDictionary", "");
433         if(uri.startsWith("/")){
434             uri = uri.substring(uri.indexOf('/')+1);
435         }
436         uri = "/onap" + uri.substring(uri.indexOf('/'));
437
438         String userId = UserUtils.getUserSession(request).getOrgUserId();
439         policyLogger.info("****************************************Logging UserID while Deleting Dictionary*****************************************************");
440         policyLogger.info("UserId:  " + userId);
441         policyLogger.info("*************************************************************************************************************************************");
442
443         String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
444         if(body != null && !body.isEmpty()){
445             response.getWriter().write(body);
446         }else{
447             response.getWriter().write("Failed");
448         }
449     }
450
451     @RequestMapping(value={"/searchDictionary"}, method={RequestMethod.POST})
452     public ModelAndView searchDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
453         Object resultList;
454         String uri = request.getRequestURI();
455         if(uri.startsWith("/")){
456             uri = uri.substring(uri.indexOf('/')+1);
457         }
458         uri = "/onap" + uri.substring(uri.indexOf('/'));
459         try{
460             String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
461             if(body.contains("CouldNotConnectException")){
462                 List<String> data = new ArrayList<>();
463                 data.add("Elastic Search Server is down");
464                 resultList = data;
465             }else{
466                 JSONObject json = new JSONObject(body);
467                 resultList = json.get("policyresult");
468             }
469         }catch(Exception e){
470             policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Exception Occured while querying Elastic Search: " + e);
471             List<String> data = new ArrayList<>();
472             data.add("Elastic Search Server is down");
473             resultList = data;
474         }
475
476         response.setCharacterEncoding(PolicyController.getCharacterencoding());
477         response.setContentType(PolicyController.getContenttype());
478         PrintWriter out = response.getWriter();
479         JSONObject j = new JSONObject("{result: " + resultList + "}");
480         out.write(j.toString());
481         return null;
482     }
483
484     @RequestMapping(value={"/searchPolicy"}, method={RequestMethod.POST})
485     public ModelAndView searchPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException{
486         Object resultList;
487         String uri = request.getRequestURI()+"?action=search";
488         if(uri.startsWith("/")){
489             uri = uri.substring(uri.indexOf('/')+1);
490         }
491         uri = "/onap" + uri.substring(uri.indexOf('/'));
492         String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
493
494         JSONObject json = new JSONObject(body);
495         try{
496             resultList = json.get("policyresult");
497         }catch(Exception e){
498             List<String> data = new ArrayList<>();
499             resultList = json.get("data");
500             data.add("Exception");
501             data.add(resultList.toString());
502             resultList = data;
503             policyLogger.error("Exception Occured while searching for Policy in Elastic Database" +e);
504         }
505
506         response.setCharacterEncoding("UTF-8");
507         response.setContentType("application / json");
508         request.setCharacterEncoding("UTF-8");
509
510         PrintWriter out = response.getWriter();
511         JSONObject j = new JSONObject("{result: " + resultList + "}");
512         out.write(j.toString());
513         return null;
514     }
515
516     public void deleteElasticData(String fileName){
517         String uri = "searchPolicy?action=delete&policyName='"+fileName+"'";
518         callPAP(null, "POST", uri.trim());
519     }
520
521     public String notifyOtherPAPSToUpdateConfigurations(String mode, String newName, String oldName){
522         String uri = "onap/notifyOtherPAPs?action="+mode+"&newPolicyName="+newName+"&oldPolicyName="+oldName+"";
523         return callPAP(null, "POST", uri.trim());
524     }
525
526 }