2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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=========================================================
21 package org.onap.policy.pap.xacml.rest;
23 import com.att.research.xacml.util.XACMLProperties;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.BufferedWriter;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Base64;
33 import java.util.HashMap;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import org.json.JSONObject;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.pap.xacml.rest.adapters.UpdateObjectData;
42 import org.onap.policy.pap.xacml.rest.components.Policy;
43 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
44 import org.onap.policy.rest.XACMLRestProperties;
45 import org.onap.policy.rest.dao.CommonClassDao;
46 import org.onap.policy.rest.jpa.ActionBodyEntity;
47 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
48 import org.onap.policy.rest.jpa.PolicyDBDaoEntity;
49 import org.onap.policy.utils.PeCryptoUtils;
50 import org.onap.policy.xacml.api.XACMLErrorConstants;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.http.HttpEntity;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.http.HttpMethod;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.stereotype.Controller;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestMethod;
60 import org.springframework.web.bind.annotation.ResponseBody;
61 import org.springframework.web.client.HttpClientErrorException;
62 import org.springframework.web.client.RestTemplate;
65 public class UpdateOthersPAPS {
67 private static final Logger policyLogger = FlexLogger.getLogger(UpdateOthersPAPS.class);
69 private static CommonClassDao commonClassDao;
71 private static final String contentType = "application/json";
72 private static String configType =".Config_";
73 private static String actionType =".Action_";
74 private static String error ="error";
75 public static CommonClassDao getCommonClassDao() {
76 return commonClassDao;
79 public static void setCommonClassDao(CommonClassDao commonClassDao) {
80 UpdateOthersPAPS.commonClassDao = commonClassDao;
84 private UpdateOthersPAPS(CommonClassDao commonClassDao) {
85 UpdateOthersPAPS.commonClassDao = commonClassDao;
88 public UpdateOthersPAPS() {
92 @RequestMapping(value="/notifyOtherPAPs", method= RequestMethod.POST)
93 public void notifyOthersPAPsToUpdateConfigurations(HttpServletRequest request, HttpServletResponse response){
94 Map<String, Object> model = new HashMap<>();
95 ObjectMapper mapper = new ObjectMapper();
96 UpdateObjectData body = new UpdateObjectData();
97 body.setAction(request.getParameter("action"));
98 body.setNewPolicyName(request.getParameter("newPolicyName"));
99 body.setOldPolicyName(request.getParameter("oldPolicyName"));
101 String currentPap = XACMLRestProperties.getProperty("xacml.rest.pap.url");
102 List<Object> getPAPUrls = commonClassDao.getData(PolicyDBDaoEntity.class);
103 if(getPAPUrls != null && !getPAPUrls.isEmpty()){
104 for(int i = 0; i < getPAPUrls.size(); i++){
105 PolicyDBDaoEntity papId = (PolicyDBDaoEntity) getPAPUrls.get(i);
106 String papUrl = papId.getPolicyDBDaoUrl();
107 if(!papUrl.equals(currentPap)){
108 String userName = papId.getUsername();
109 String password = papId.getPassword();
110 Base64.Encoder encoder = Base64.getEncoder();
113 PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XACMLRestProperties.PROP_AES_KEY));
114 txt = PeCryptoUtils.decrypt(password);
115 } catch (Exception e) {
116 policyLogger.debug(e);
117 //if we can't decrypt, might as well try it anyway
120 String encoding = encoder.encodeToString((userName + ":" + txt).getBytes(StandardCharsets.UTF_8));
121 HttpHeaders headers = new HttpHeaders();
122 headers.set("Authorization", "Basic " + encoding);
123 headers.set("Content-Type", contentType);
125 RestTemplate restTemplate = new RestTemplate();
126 HttpEntity<?> requestEntity = new HttpEntity<>(body, headers);
127 HttpClientErrorException exception = null;
130 restTemplate.exchange(papUrl + "onap/updateConfiguration", HttpMethod.POST, requestEntity, String.class);
132 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while connecting to " + papUrl, e);
133 exception = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
134 if("409 Conflict".equals(e.getMessage())){
135 policyLogger.error(e.getMessage());
136 response.addHeader(error, e.getMessage());
139 if(exception != null && exception.getStatusCode()!=null){
141 if(exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED)){
142 message = XACMLErrorConstants.ERROR_PERMISSIONS +":"+exception.getStatusCode()+":" + "ERROR_AUTH_GET_PERM" ;
143 policyLogger.error(message);
144 }else if(exception.getStatusCode().equals(HttpStatus.BAD_REQUEST)){
145 message = XACMLErrorConstants.ERROR_DATA_ISSUE + ":"+exception.getStatusCode()+":" + exception.getResponseBodyAsString();
146 policyLogger.error(message);
147 }else if(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)){
148 message = XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while connecting to " + papUrl + exception;
149 policyLogger.error(message);
151 message = XACMLErrorConstants.ERROR_PROCESS_FLOW + ":"+exception.getStatusCode()+":" + exception.getResponseBodyAsString();
152 policyLogger.error(message);
154 model.put(papUrl, message);
156 model.put(papUrl, "Success");
162 msg = new JsonMessage(mapper.writeValueAsString(model));
163 JSONObject j = new JSONObject(msg);
164 response.getWriter().write(j.toString());
165 } catch (Exception e) {
166 policyLogger.error("Exception Occured"+e);
171 @RequestMapping(value="/updateConfiguration", method= RequestMethod.POST)
173 public void updateConfiguration(@RequestBody UpdateObjectData data, HttpServletResponse response){
174 String action = data.getAction();
175 String newPolicyName = data.getNewPolicyName();
176 String oldPolicyName = data.getOldPolicyName();
178 if("rename".equals(action)){
179 if(oldPolicyName.contains(configType) || oldPolicyName.contains(actionType)){
181 if(oldPolicyName.contains(configType)){
182 file = new File(Policy.getConfigHome() + File.separator + oldPolicyName);
184 file = new File(Policy.getActionHome() + File.separator + oldPolicyName);
188 if(oldPolicyName.contains(configType)){
189 renamefile = new File(Policy.getConfigHome() + File.separator + newPolicyName);
191 renamefile = new File(Policy.getActionHome() + File.separator + newPolicyName);
193 if(file.renameTo(renamefile)){
194 policyLogger.info("Policy has been renamed Successfully"+newPolicyName);
195 response.addHeader("rename", "Success");
197 response.addHeader("rename", "Failure");
201 }else if("delete".equals(action)){
202 if(oldPolicyName.contains(configType)){
203 Files.deleteIfExists(Paths.get(Policy.getConfigHome() + File.separator + oldPolicyName));
204 }else if(oldPolicyName.contains("Action_")){
205 Files.deleteIfExists(Paths.get(Policy.getActionHome() + File.separator + oldPolicyName));
207 }else if("clonePolicy".equals(action) || "exportPolicy".equals(action)){
208 if(newPolicyName.contains(configType)){
209 ConfigurationDataEntity configEntiy = (ConfigurationDataEntity) commonClassDao.getEntityItem(ConfigurationDataEntity.class, "configurationName", newPolicyName);
210 saveConfigurationData(configEntiy, newPolicyName);
211 }else if(newPolicyName.contains(actionType)){
212 ActionBodyEntity actionEntiy = (ActionBodyEntity) commonClassDao.getEntityItem(ActionBodyEntity.class, "actionBodyName", newPolicyName);
213 saveActionBodyData(actionEntiy, newPolicyName);
216 } catch (IOException e) {
217 policyLogger.error("Exception Occured While updating Configuration"+e);
221 private void saveConfigurationData(ConfigurationDataEntity configEntiy, String newPolicyName){
222 try(FileWriter fw = new FileWriter(Policy.getConfigHome() + File.separator + newPolicyName)){
223 BufferedWriter bw = new BufferedWriter(fw);
224 bw.write(configEntiy.getConfigBody());
226 }catch (IOException e) {
227 policyLogger.error("Exception Occured While closing the File input stream"+e);
231 private void saveActionBodyData(ActionBodyEntity actionEntiy , String newPolicyName){
232 try(FileWriter fw = new FileWriter(Policy.getActionHome() + File.separator + newPolicyName)){
233 BufferedWriter bw = new BufferedWriter(fw);
234 bw.write(actionEntiy.getActionBody());
236 }catch (IOException e) {
237 policyLogger.error("Exception Occured While closing the File input stream"+e);