Merge "New min/max Guard Policy"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / PolicyExportAndImportController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
21 package org.onap.policy.controller;
22
23
24 import java.io.BufferedWriter;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import java.util.Set;
36
37 import javax.script.SimpleBindings;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import org.apache.poi.hssf.usermodel.HSSFRow;
42 import org.apache.poi.hssf.usermodel.HSSFSheet;
43 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
44 import org.apache.poi.ss.usermodel.Cell;
45 import org.apache.poi.ss.usermodel.Row;
46 import org.apache.poi.ss.usermodel.Sheet;
47 import org.json.JSONObject;
48 import org.onap.policy.admin.PolicyRestController;
49 import org.onap.policy.common.logging.flexlogger.FlexLogger;
50 import org.onap.policy.common.logging.flexlogger.Logger;
51 import org.onap.policy.rest.adapter.PolicyExportAdapter;
52 import org.onap.policy.rest.dao.CommonClassDao;
53 import org.onap.policy.rest.jpa.ActionBodyEntity;
54 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
55 import org.onap.policy.rest.jpa.PolicyEditorScopes;
56 import org.onap.policy.rest.jpa.PolicyEntity;
57 import org.onap.policy.rest.jpa.PolicyVersion;
58 import org.onap.policy.rest.jpa.UserInfo;
59 import org.onap.policy.utils.UserUtils.Pair;
60 import org.onap.policy.xacml.api.XACMLErrorConstants;
61 import org.onap.portalsdk.core.controller.RestrictedBaseController;
62 import org.onap.portalsdk.core.web.support.UserUtils;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.stereotype.Controller;
65 import org.springframework.web.bind.annotation.RequestMapping;
66
67 import com.fasterxml.jackson.databind.DeserializationFeature;
68 import com.fasterxml.jackson.databind.JsonNode;
69 import com.fasterxml.jackson.databind.ObjectMapper;
70
71
72 @Controller
73 @RequestMapping("/")
74 public class PolicyExportAndImportController extends RestrictedBaseController {
75         private static Logger   logger  = FlexLogger.getLogger(PolicyExportAndImportController.class);
76         
77         private static String superAdmin = "super-admin";
78         private static String superEditor = "super-editor";
79         private static String admin = "admin";
80         private static String editor = "editor";
81         private static String policyName = "policyName";
82         private static String configurationName = "configurationName";
83         private static String configurationbody = "configurationbody";
84         private static String config = "Config_";
85
86         private static CommonClassDao commonClassDao;
87
88         
89         private PolicyController policyController;
90         public PolicyController getPolicyController() {
91                 return policyController;
92         }
93
94         public void setPolicyController(PolicyController policyController) {
95                 this.policyController = policyController;
96         }
97
98         public static CommonClassDao getCommonClassDao() {
99                 return commonClassDao;
100         }
101
102         public static void setCommonClassDao(CommonClassDao commonClassDao) {
103                 PolicyExportAndImportController.commonClassDao = commonClassDao;
104         }
105
106         @Autowired
107         private PolicyExportAndImportController(CommonClassDao commonClassDao){
108                 PolicyExportAndImportController.commonClassDao = commonClassDao;
109         }
110
111         public PolicyExportAndImportController(){
112                 // Empty constructor
113         }
114
115         @RequestMapping(value={"/policy_download/exportPolicy.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
116         public void exportPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException{
117                 try(HSSFWorkbook workBook2 = new HSSFWorkbook()){
118                         String file;
119                         ArrayList<String> selectedPolicy = new ArrayList<>();
120                         ObjectMapper mapper = new ObjectMapper();
121                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
122                         JsonNode root = mapper.readTree(request.getReader());
123                         PolicyExportAdapter adapter = mapper.readValue(root.get("exportData").toString(), PolicyExportAdapter.class);
124                         for (Object policyId :  adapter.getPolicyDatas()) {
125                                 LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>)policyId;
126                                 String policyWithScope = selected.get(policyName).toString() + "." + selected.get("activeVersion").toString() + ".xml";
127                                 String scope = policyWithScope.substring(0 , policyWithScope.lastIndexOf(File.separator)).replace(File.separator, ".");
128                                 String policyName = policyWithScope.substring(policyWithScope.lastIndexOf(File.separator)+1);
129                                 selectedPolicy.add(policyName+":"+scope);
130                         }
131                         List<Object> entityData = commonClassDao.getMultipleDataOnAddingConjunction(PolicyEntity.class, "policyName:scope", selectedPolicy);
132
133                         HSSFSheet sheet = workBook2.createSheet("PolicyEntity");
134
135                         HSSFRow headingRow = sheet.createRow(0);
136                         headingRow.createCell(0).setCellValue(policyName);
137                         headingRow.createCell(1).setCellValue("scope");
138                         headingRow.createCell(2).setCellValue("version");
139                         headingRow.createCell(3).setCellValue("policyData");
140                         headingRow.createCell(4).setCellValue("description");
141                         headingRow.createCell(5).setCellValue(configurationName);
142                         headingRow.createCell(6).setCellValue("bodySize");
143                         headingRow.createCell(7).setCellValue(configurationbody);
144
145                         short rowNo = 1;
146                         for (Object object : entityData) {
147                                 PolicyEntity policyEntity = (PolicyEntity) object;
148                                 HSSFRow row = sheet.createRow(rowNo);
149                                 row.createCell(0).setCellValue(policyEntity.getPolicyName());
150                                 row.createCell(1).setCellValue(policyEntity.getScope());
151                                 row.createCell(2).setCellValue(policyEntity.getVersion());
152                                 row.createCell(3).setCellValue(policyEntity.getPolicyData());
153                                 row.createCell(4).setCellValue(policyEntity.getDescription());
154                                 if(!policyEntity.getPolicyName().contains("Decision_")){
155                                         if(policyEntity.getConfigurationData() != null){
156                                                 row.createCell(5).setCellValue(policyEntity.getConfigurationData().getConfigurationName());
157                                                 String body = policyEntity.getConfigurationData().getConfigBody();
158                                                 if(policyEntity.getPolicyName().contains("Config_BRMS_Param_")){
159                                                         int index = 0;
160                                                         int arraySize = 0;
161                                                         while (index < body.length()) {
162                                                                 if(arraySize == 0){
163                                                                         row.createCell(7).setCellValue(body.substring(index, Math.min(index + 30000, body.length())));
164                                                                 }else{
165                                                                         headingRow.createCell(7 + arraySize).setCellValue(configurationbody+arraySize);
166                                                                         row.createCell(7 + arraySize).setCellValue(body.substring(index, Math.min(index + 30000, body.length())));
167                                                                 }
168                                                                 index += 30000;
169                                                                 arraySize += 1;
170                                                         }
171                                                         row.createCell(6).setCellValue(arraySize);
172                                                 }else{  
173                                                         row.createCell(6).setCellValue(0);
174                                                         row.createCell(7).setCellValue(body);
175                                                 }       
176                                         }
177                                         if(policyEntity.getActionBodyEntity() != null){
178                                                 row.createCell(5).setCellValue(policyEntity.getActionBodyEntity().getActionBodyName());
179                                                 row.createCell(6).setCellValue(0);
180                                                 row.createCell(7).setCellValue(policyEntity.getActionBodyEntity().getActionBody());
181                                         }
182                                 }else{
183                                         row.createCell(5).setCellValue("");
184                                         row.createCell(6).setCellValue(0);
185                                         row.createCell(7).setCellValue("");
186                                 }
187                                 rowNo++;
188                         }
189
190                         String tmp = System.getProperty("catalina.base") + File.separator + "webapps" + File.separator + "temp";
191                         String deleteCheckPath = tmp + File.separator + "PolicyExport.xls";
192                         File deleteCheck = new File(deleteCheckPath);
193                         if(deleteCheck.exists() && deleteCheck.delete()){
194                                 logger.info("Deleted the file from system before exporting a new file.");
195                         }
196                         File temPath = new File(tmp);
197                         if(!temPath.exists()){
198                                 temPath.mkdir();
199                         }
200
201                         file =  temPath + File.separator + "PolicyExport.xls";
202                         File filepath = new File(file);
203                         FileOutputStream fos = new FileOutputStream(filepath);
204                         workBook2.write(fos);
205                         fos.flush();
206
207                         response.setCharacterEncoding("UTF-8");
208                         response.setContentType("application / json");
209                         request.setCharacterEncoding("UTF-8");
210
211                         PrintWriter out = response.getWriter();
212                         String successMap = file.substring(file.lastIndexOf("webapps")+8);
213                         String responseString = mapper.writeValueAsString(successMap);
214                         JSONObject j = new JSONObject("{data: " + responseString + "}");
215                         out.write(j.toString());
216                 }catch(Exception e){
217                         logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception Occured while Exporting Policies"+e);
218                 }
219         }
220
221         //Policy Import 
222         public JSONObject importRepositoryFile(String file, HttpServletRequest request) throws IOException{
223                 boolean configExists = false;
224                 boolean actionExists = false;
225                 String configName = null;
226                 String scope;
227                 boolean finalColumn;
228                 PolicyController controller = policyController != null ? getPolicyController() : new PolicyController();
229                 String userId = UserUtils.getUserSession(request).getOrgUserId();
230                 UserInfo userInfo = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", userId);
231
232                 //Check if the Role and Scope Size are Null get the values from db. 
233                 List<Object> userRoles = controller.getRoles(userId);
234                 Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
235                 List<String> roles = pair.u;
236                 Set<String> scopes = pair.t;
237                 
238                 try(FileInputStream excelFile = new FileInputStream(new File(file)); HSSFWorkbook workbook = new HSSFWorkbook(excelFile)){
239                         Sheet datatypeSheet = workbook.getSheetAt(0);
240                         Iterator<Row> rowIterator = datatypeSheet.iterator();
241
242                         while (rowIterator.hasNext()) {
243                                 finalColumn = false;
244                                 PolicyEntity policyEntity = new PolicyEntity();
245                                 ConfigurationDataEntity configurationDataEntity = new ConfigurationDataEntity();
246                                 ActionBodyEntity actionBodyEntity = new ActionBodyEntity();
247                                 PolicyVersion policyVersion = new PolicyVersion();
248                                 Row currentRow = rowIterator.next();
249                                 if (currentRow.getRowNum() == 0) {
250                                         continue;
251                                 }
252                                 Iterator<Cell> cellIterator = currentRow.cellIterator();
253                                 StringBuilder body = new StringBuilder();
254                                 int bodySize = 0;
255                                 int setBodySize = 0;
256                                 boolean configurationBodySet = false;
257                                 while (cellIterator.hasNext()) {
258                                         Cell cell = cellIterator.next();
259                                         if (policyName.equalsIgnoreCase(getCellHeaderName(cell))) {
260                                                 policyEntity.setPolicyName(cell.getStringCellValue());
261                                                 finalColumn = false; 
262                                                 configurationBodySet = false;
263                                                 configExists = false;
264                                                 actionExists = false;
265                                         }
266                                         if ("scope".equalsIgnoreCase(getCellHeaderName(cell))) {
267                                                 policyEntity.setScope(cell.getStringCellValue());
268                                         }
269                                         if ("policyData".equalsIgnoreCase(getCellHeaderName(cell))) {
270                                                 policyEntity.setPolicyData(cell.getStringCellValue());
271                                         }
272                                         if ("description".equalsIgnoreCase(getCellHeaderName(cell))) {
273                                                 policyEntity.setDescription(cell.getStringCellValue());
274                                         }
275                                         if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell))) {
276                                                 if(policyEntity.getPolicyName().contains(config)){
277                                                         if(policyEntity.getPolicyName().contains("Config_BRMS_Param_")){
278                                                                 setBodySize += 1;
279                                                         }
280                                                         if(setBodySize == bodySize){
281                                                                 finalColumn = true;
282                                                                 configurationBodySet = true;
283                                                         }else if(setBodySize == 0){
284                                                                 configurationBodySet = true;
285                                                         }
286                                                         configExists = true;
287                                                         body.append(cell.getStringCellValue());
288                                                 }else if(policyEntity.getPolicyName().contains("Action_")){
289                                                         actionExists = true;
290                                                         actionBodyEntity.setActionBody(cell.getStringCellValue());
291                                                 }       
292                                         }
293                                         if ("bodySize".equalsIgnoreCase(getCellHeaderName(cell))) {
294                                                 if(cell.getNumericCellValue() < 1){
295                                                         finalColumn = true;
296                                                 }else{
297                                                         bodySize = (int) cell.getNumericCellValue();
298                                                 }
299                                         }
300                                         if (configurationName.equalsIgnoreCase(getCellHeaderName(cell))) {
301                                                 configName = cell.getStringCellValue();
302                                                 if(policyEntity.getPolicyName().contains(config)){
303                                                         configurationDataEntity.setConfigurationName(cell.getStringCellValue());
304                                                 }else if(policyEntity.getPolicyName().contains("Action_")){
305                                                         actionBodyEntity.setActionBodyName(cell.getStringCellValue());
306                                                 }       
307                                         }
308
309                                         if(finalColumn && configurationBodySet){
310                                                 configurationDataEntity.setConfigBody(body.toString());
311                                                 scope = policyEntity.getScope().replace(".", File.separator);
312                                                 String query = "FROM PolicyEntity where policyName = :policyName and scope = :policyScope";
313                                                 SimpleBindings params = new SimpleBindings();
314                                                 params.put(policyName, policyEntity.getPolicyName());
315                                                 params.put("policyScope", policyEntity.getScope());
316                                                 List<Object> queryData = controller.getDataByQuery(query, params);
317                                                 if(!queryData.isEmpty()){
318                                                         continue;
319                                                 }
320                                                 if (roles.contains(superAdmin) || roles.contains(superEditor)) {
321                                                         //1. if Role contains super admin create scope.
322                                                         //2. if Role contains super editor don't create new scope and add to list to show to user.
323
324                                                         PolicyEditorScopes policyEditorScope = (PolicyEditorScopes) commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", scope);
325                                                         if(policyEditorScope == null){
326                                                                 if(roles.contains(superAdmin)){
327                                                                         PolicyEditorScopes policyEditorScopeEntity = new PolicyEditorScopes();
328                                                                         policyEditorScopeEntity.setScopeName(scope);
329                                                                         policyEditorScopeEntity.setUserCreatedBy(userInfo);
330                                                                         policyEditorScopeEntity.setUserModifiedBy(userInfo);
331                                                                         commonClassDao.save(policyEditorScopeEntity);
332                                                                 }else{
333                                                                         //Add Error Message a new Scope Exists, contact super-admin to create a new scope
334                                                                         continue;
335                                                                 }
336                                                         }
337                                                 }
338                                                 if (roles.contains(admin) || roles.contains(editor)) {
339                                                         if(scopes.isEmpty()){
340                                                                 logger.error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
341                                                         }else{
342                                                                 //1. if Role contains admin, then check if parent scope has role admin, if not don't create a scope and add to list.
343                                                                 if(roles.contains(admin)){
344                                                                         String scopeCheck = scope.substring(0, scope.lastIndexOf('.'));
345                                                                         if(scopes.contains(scopeCheck)){
346                                                                                 PolicyEditorScopes policyEditorScopeEntity = new PolicyEditorScopes();
347                                                                                 policyEditorScopeEntity.setScopeName(scope);
348                                                                                 policyEditorScopeEntity.setUserCreatedBy(userInfo);
349                                                                                 policyEditorScopeEntity.setUserModifiedBy(userInfo);
350                                                                                 commonClassDao.save(policyEditorScopeEntity);
351                                                                         }else{
352                                                                                 continue;
353                                                                         }
354                                                                 }else{
355                                                                         continue;
356                                                                 }
357                                                         }
358                                                 }       
359
360                                                 if(configExists){
361                                                         if(configName.endsWith("json")){
362                                                                 configurationDataEntity.setConfigType("JSON");
363                                                         }else if(configName.endsWith("txt")){
364                                                                 configurationDataEntity.setConfigType("OTHER");
365                                                         }else if(configName.endsWith("xml")){
366                                                                 configurationDataEntity.setConfigType("XML");
367                                                         }else if(configName.endsWith("properties")){
368                                                                 configurationDataEntity.setConfigType("PROPERTIES");
369                                                         }
370                                                         configurationDataEntity.setDeleted(false);
371                                                         configurationDataEntity.setCreatedBy(userId);
372                                                         configurationDataEntity.setModifiedBy(userId);
373                                                         commonClassDao.save(configurationDataEntity);
374                                                         writeConfigurationFile(configurationDataEntity);
375                                                 }
376                                                 if(actionExists){
377                                                         actionBodyEntity.setDeleted(false);
378                                                         actionBodyEntity.setCreatedBy(userId);
379                                                         actionBodyEntity.setModifiedBy(userId);
380                                                         commonClassDao.save(actionBodyEntity);
381                                                         writeActionBodyFile(actionBodyEntity);
382                                                 }
383                                                 if(configName != null){
384                                                         if(configName.contains(config)){
385                                                                 ConfigurationDataEntity configuration = (ConfigurationDataEntity) commonClassDao.getEntityItem(ConfigurationDataEntity.class, configurationName, configName);
386                                                                 policyEntity.setConfigurationData(configuration);
387                                                         }else{
388                                                                 ActionBodyEntity actionBody = (ActionBodyEntity) commonClassDao.getEntityItem(ActionBodyEntity.class, "actionBodyName", configName);
389                                                                 policyEntity.setActionBodyEntity(actionBody);
390                                                         }
391                                                 }
392                                                 policyEntity.setCreatedBy(userId);
393                                                 policyEntity.setModifiedBy(userId);
394                                                 policyEntity.setDeleted(false);
395                                                 commonClassDao.save(policyEntity);
396
397                                                 policyVersion = new PolicyVersion();
398                                                 String policyName = policyEntity.getPolicyName().replace(".xml", "");
399                                                 int version = Integer.parseInt(policyName.substring(policyName.lastIndexOf('.')+1));
400                                                 policyName = policyName.substring(0, policyName.lastIndexOf('.'));
401
402                                                 policyVersion.setPolicyName(scope.replace(".", File.separator) + File.separator + policyName);
403                                                 policyVersion.setActiveVersion(version);
404                                                 policyVersion.setHigherVersion(version);
405                                                 policyVersion.setCreatedBy(userId);
406                                                 policyVersion.setModifiedBy(userId);
407                                                 commonClassDao.save(policyVersion);
408
409                                                 //Notify Other paps regarding Export Policy.
410                                                 PolicyRestController restController = new PolicyRestController();
411                                                 restController.notifyOtherPAPSToUpdateConfigurations("exportPolicy", configName, null);
412                                         }
413                                 }
414                         }
415                 }catch(IOException e){
416                         logger.error("Exception Occured While importing the Policy"+e);
417                 }
418                 return null;
419         }
420
421         private void writeConfigurationFile(ConfigurationDataEntity configurationDataEntity){
422                 try(FileWriter fw = new FileWriter(PolicyController.getConfigHome() + File.separator + configurationDataEntity.getConfigurationName())){
423                         BufferedWriter bw = new BufferedWriter(fw);
424                         bw.write(configurationDataEntity.getConfigBody());
425                         bw.close();
426                 } catch (IOException e) {
427                         logger.error("Exception Occured While cloning the configuration file",e);
428                 }
429         }
430         
431         private void writeActionBodyFile(ActionBodyEntity actionBodyEntity){
432                 try(FileWriter fw = new FileWriter(PolicyController.getActionHome() + File.separator + actionBodyEntity.getActionBodyName())) {
433                         BufferedWriter bw = new BufferedWriter(fw);
434                         bw.write(actionBodyEntity.getActionBody());
435                         bw.close();
436                 } catch (IOException e) {
437                         logger.error("Exception Occured While cloning the configuration file",e);
438                 }
439         }
440         
441         //return the column header name value
442         private String getCellHeaderName(Cell cell){
443                 return cell.getSheet().getRow(0).getCell(cell.getColumnIndex()).getRichStringCellValue().toString();
444         }
445 }