2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.admin;
23 import java.io.ByteArrayInputStream;
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;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Base64;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
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;
80 public class PolicyRestController extends RestrictedBaseController{
82 private static final Logger policyLogger = FlexLogger.getLogger(PolicyRestController.class);
84 private static final String model = "model";
85 private static final String importDictionary = "import_dictionary";
87 private static CommonClassDao commonClassDao;
89 public PolicyRestController(){
94 private PolicyRestController(CommonClassDao commonClassDao){
95 PolicyRestController.commonClassDao = commonClassDao;
98 public static CommonClassDao getCommonClassDao() {
99 return commonClassDao;
102 public static void setCommonClassDao(CommonClassDao commonClassDao) {
103 PolicyRestController.commonClassDao = commonClassDao;
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);
114 JsonNode root = mapper.readTree(request.getReader());
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("***********************************************************************************************************************************");
120 PolicyRestAdapter policyData = mapper.readValue(root.get(PolicyController.getPolicydata()).get("policy").toString(), PolicyRestAdapter.class);
122 if("file".equals(root.get(PolicyController.getPolicydata()).get(model).get("type").toString().replace("\"", ""))){
123 policyData.setEditPolicy(true);
125 if(root.get(PolicyController.getPolicydata()).get(model).get("path").size() != 0){
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;
130 if(policyData.isEditPolicy()){
131 policyData.setDomainDir(dirName.substring(0, dirName.lastIndexOf(File.separator)));
133 policyData.setDomainDir(dirName + root.get(PolicyController.getPolicydata()).get(model).get("name").toString().replace("\"", ""));
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);
140 domain = domain.replace("\"", "");
141 policyData.setDomainDir(domain);
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);
156 policyData.setUserId(userId);
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);
182 result = "Response is null from PAP";
185 response.setCharacterEncoding(PolicyController.getCharacterencoding());
186 response.setContentType(PolicyController.getContenttype());
187 request.setCharacterEncoding(PolicyController.getCharacterencoding());
189 PrintWriter out = response.getWriter();
190 String responseString = mapper.writeValueAsString(result);
191 JSONObject j = new JSONObject("{policyData: " + responseString + "}");
192 out.write(j.toString());
194 policyLogger.error("Exception Occured while saving policy" , e);
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));
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());
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);
218 uri = "onap" + uri.substring(uri.indexOf('/'));
220 result = restTemplate.exchange(papUrl + uri, method, requestEntity, String.class);
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);
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);
233 if(exception.getStatusCode().equals(HttpStatus.BAD_REQUEST)){
234 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + ":"+exception.getStatusCode()+":" + exception.getResponseBodyAsString();
235 policyLogger.error(message);
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);
241 String message = XACMLErrorConstants.ERROR_PROCESS_FLOW + ":"+exception.getStatusCode()+":" + exception.getResponseBodyAsString();
242 policyLogger.error(message);
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));
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());
261 HttpURLConnection connection = null;
262 List<FileItem> items;
263 FileItem item = null;
265 if(uri.contains(importDictionary)){
267 items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
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);
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);
287 if(!uri.contains("searchPolicy?action=delete&")){
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);
295 ObjectMapper mapper1 = new ObjectMapper();
296 mapper1.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
298 Object obj = mapper1.treeToValue(root, Object.class);
299 String json = mapper1.writeValueAsString(obj);
301 Object content = new ByteArrayInputStream(json.getBytes());
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);
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);
319 boundary = "===" + System.currentTimeMillis() + "===";
320 connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
321 try (OutputStream os = connection.getOutputStream()) {
323 IOUtils.copy((InputStream) item.getInputStream(), os);
329 return doConnect(connection);
330 } catch (Exception e) {
331 policyLogger.error("Exception Occured"+e);
333 if(file != null && file.exists() && file.delete()){
334 policyLogger.info("File Deleted Successfully");
336 if (connection != null) {
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();
344 } catch (IOException ex) {
345 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to close connection: " + ex, ex);
347 connection.disconnect();
353 private JsonNode getJsonNode(HttpServletRequest request, ObjectMapper mapper) {
354 JsonNode root = null;
356 root = mapper.readTree(request.getReader());
357 }catch (Exception e1) {
358 policyLogger.error("Exception Occured while calling PAP"+e1);
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);
382 policyLogger.info("JSON response from PAP: " + responseJson);
388 @RequestMapping(value={"/getDictionary/*"}, method={RequestMethod.GET})
389 public void getDictionaryController(HttpServletRequest request, HttpServletResponse response){
390 String uri = request.getRequestURI().replace("/getDictionary", "");
392 ResponseEntity<?> responseEntity = sendToPAP(null, uri, HttpMethod.GET);
393 if(responseEntity != null){
394 body = responseEntity.getBody().toString();
399 response.getWriter().write(body);
400 } catch (IOException e) {
401 policyLogger.error("Exception occured while getting Dictionary entries", e);
405 @RequestMapping(value={"/saveDictionary/*/*"}, method={RequestMethod.POST})
406 public void saveDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException{
408 String uri = request.getRequestURI().replace("/saveDictionary", "");
409 if(uri.startsWith("/")){
410 uri = uri.substring(uri.indexOf('/')+1);
412 uri = "/onap" + uri.substring(uri.indexOf('/'));
413 if(uri.contains(importDictionary)){
414 userId = UserUtils.getUserSession(request).getOrgUserId();
415 uri = uri+ "?userId=" +userId;
418 policyLogger.info("****************************************Logging UserID while Saving Dictionary*****************************************************");
419 policyLogger.info("UserId: " + userId);
420 policyLogger.info("***********************************************************************************************************************************");
422 String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
423 if(body != null && !body.isEmpty()){
424 response.getWriter().write(body);
426 response.getWriter().write("Failed");
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);
436 uri = "/onap" + uri.substring(uri.indexOf('/'));
438 String userId = UserUtils.getUserSession(request).getOrgUserId();
439 policyLogger.info("****************************************Logging UserID while Deleting Dictionary*****************************************************");
440 policyLogger.info("UserId: " + userId);
441 policyLogger.info("*************************************************************************************************************************************");
443 String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
444 if(body != null && !body.isEmpty()){
445 response.getWriter().write(body);
447 response.getWriter().write("Failed");
451 @RequestMapping(value={"/searchDictionary"}, method={RequestMethod.POST})
452 public ModelAndView searchDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
454 String uri = request.getRequestURI();
455 if(uri.startsWith("/")){
456 uri = uri.substring(uri.indexOf('/')+1);
458 uri = "/onap" + uri.substring(uri.indexOf('/'));
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");
466 JSONObject json = new JSONObject(body);
467 resultList = json.get("policyresult");
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");
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());
484 @RequestMapping(value={"/searchPolicy"}, method={RequestMethod.POST})
485 public ModelAndView searchPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException{
487 String uri = request.getRequestURI()+"?action=search";
488 if(uri.startsWith("/")){
489 uri = uri.substring(uri.indexOf('/')+1);
491 uri = "/onap" + uri.substring(uri.indexOf('/'));
492 String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
494 JSONObject json = new JSONObject(body);
496 resultList = json.get("policyresult");
498 List<String> data = new ArrayList<>();
499 resultList = json.get("data");
500 data.add("Exception");
501 data.add(resultList.toString());
503 policyLogger.error("Exception Occured while searching for Policy in Elastic Database" +e);
506 response.setCharacterEncoding("UTF-8");
507 response.setContentType("application / json");
508 request.setCharacterEncoding("UTF-8");
510 PrintWriter out = response.getWriter();
511 JSONObject j = new JSONObject("{result: " + resultList + "}");
512 out.write(j.toString());
516 public void deleteElasticData(String fileName){
517 String uri = "searchPolicy?action=delete&policyName='"+fileName+"'";
518 callPAP(null, "POST", uri.trim());
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());