X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontroller%2FPolicyExportAndImportController.java;h=cf7e62b46d87f39d67aaaa645095e203ac9c9708;hb=HEAD;hp=6c7a0aff3b9b2cf506ffc6c40024a1898b7217b9;hpb=75d9db3e29232580871c106560016be6492255d6;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java index 6c7a0aff3..cf7e62b46 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyExportAndImportController.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -38,6 +38,7 @@ import java.util.Set; import javax.script.SimpleBindings; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -52,7 +53,7 @@ import org.onap.policy.rest.adapter.PolicyExportAdapter; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.ActionBodyEntity; import org.onap.policy.rest.jpa.ConfigurationDataEntity; -import org.onap.policy.rest.jpa.DCAEuuid; +import org.onap.policy.rest.jpa.DcaeUuid; import org.onap.policy.rest.jpa.GroupPolicyScopeList; import org.onap.policy.rest.jpa.MicroServiceConfigName; import org.onap.policy.rest.jpa.MicroServiceLocation; @@ -61,10 +62,12 @@ import org.onap.policy.rest.jpa.PolicyEditorScopes; import org.onap.policy.rest.jpa.PolicyEntity; import org.onap.policy.rest.jpa.PolicyVersion; import org.onap.policy.rest.jpa.UserInfo; +import org.onap.policy.utils.PolicyUtils; import org.onap.policy.utils.UserUtils.Pair; import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.portalsdk.core.controller.RestrictedBaseController; import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -90,7 +93,6 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private static final String ACTION = "Action_"; private static CommonClassDao commonClassDao; - private PolicyController policyController; public PolicyController getPolicyController() { @@ -113,14 +115,20 @@ public class PolicyExportAndImportController extends RestrictedBaseController { // Empty constructor } + @Autowired + private PolicyExportAndImportController(CommonClassDao commonClassDao) { + PolicyExportAndImportController.commonClassDao = commonClassDao; + } + /** * This is for downloading existing policy. - * + * * @param request HttpServletRequest * @param response HttpServletResponse * @throws IOException error out */ - @RequestMapping(value = {"/policy_download/exportPolicy.htm"}, + @RequestMapping( + value = {"/policy_download/exportPolicy.htm"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) public void exportPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException { try (HSSFWorkbook workBook2 = new HSSFWorkbook()) { @@ -134,9 +142,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController { LinkedHashMap selected = (LinkedHashMap) policyId; String policyWithScope = selected.get(policyName).toString() + "." + selected.get("activeVersion").toString() + ".xml"; - String scope = - policyWithScope.substring(0, policyWithScope.lastIndexOf(File.separator)).replace( - File.separator, "."); + String scope = policyWithScope.substring(0, policyWithScope.lastIndexOf(File.separator)) + .replace(File.separator, "."); String policyNamel = policyWithScope.substring(policyWithScope.lastIndexOf(File.separator) + 1); selectedPolicy.add(policyNamel + ":" + scope); } @@ -152,9 +159,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController { headingRow.createCell(6).setCellValue(BODYSIZE); headingRow.createCell(7).setCellValue(configurationbody); - List entityData = - commonClassDao.getMultipleDataOnAddingConjunction(PolicyEntity.class, "policyName:scope", - selectedPolicy); + List entityData = commonClassDao.getMultipleDataOnAddingConjunction(PolicyEntity.class, + "policyName:scope", selectedPolicy); processEntityData(entityData, sheet, headingRow); // String tmp = System.getProperty("catalina.base") + File.separator + "webapps" + File.separator + "temp"; String deleteCheckPath = tmp + File.separator + "PolicyExport.xls"; @@ -173,14 +179,14 @@ public class PolicyExportAndImportController extends RestrictedBaseController { workBook2.write(fos); fos.flush(); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setContentType(PolicyUtils.APPLICATION_JSON); + request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); PrintWriter out = response.getWriter(); String successMap = file.substring(file.lastIndexOf("webapps") + 8); String responseString = mapper.writeValueAsString(successMap); - JSONObject j = new JSONObject("{data: " + responseString + "}"); - out.write(j.toString()); + JSONObject json = new JSONObject("{data: " + responseString + "}"); + out.write(json.toString()); } catch (Exception e) { logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while Exporting Policies" + e); } @@ -233,8 +239,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController { row.createCell(7).setCellValue(body.substring(index, Math.min(index + 30000, body.length()))); } else { headingRow.createCell(7 + arraySize).setCellValue(configurationbody + arraySize); - row.createCell(7 + arraySize).setCellValue( - body.substring(index, Math.min(index + 30000, body.length()))); + row.createCell(7 + arraySize) + .setCellValue(body.substring(index, Math.min(index + 30000, body.length()))); } index += 30000; arraySize += 1; @@ -247,17 +253,15 @@ public class PolicyExportAndImportController extends RestrictedBaseController { return row; } - - /** * This is to upload a policy and save it to database. - * + * * @param file String * @param request HttpServletRequest * @return JSONObject * @throws IOException error out */ - public JSONObject importRepositoryFile(String file, HttpServletRequest request) throws IOException { + public String importRepositoryFile(String file, HttpServletRequest request) throws IOException { boolean configExists = false; boolean actionExists = false; String configName = null; @@ -269,14 +273,14 @@ public class PolicyExportAndImportController extends RestrictedBaseController { // Check if the Role and Scope Size are Null get the values from db. List userRoles = controller.getRoles(userId); Pair, List> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles); - List roles = pair.u; - Set scopes = pair.t; + List roles = pair.second; + Set scopes = pair.first; try (FileInputStream excelFile = new FileInputStream(new File(file)); HSSFWorkbook workbook = new HSSFWorkbook(excelFile)) { Sheet datatypeSheet = workbook.getSheetAt(0); Iterator rowIterator = datatypeSheet.iterator(); - + String sendResult = null; while (rowIterator.hasNext()) { finalColumn = false; PolicyEntity policyEntity = new PolicyEntity(); @@ -291,6 +295,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { int bodySize = 0; int setBodySize = 0; boolean configurationBodySet = false; + boolean errorFlag = false; while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (policyName.equalsIgnoreCase(getCellHeaderName(cell))) { @@ -314,13 +319,18 @@ public class PolicyExportAndImportController extends RestrictedBaseController { configurationDataEntity = populateConfigurationDataEntity(policyEntity, configurationDataEntity, cell); actionBodyEntity = populateActionBodyObject(policyEntity, actionBodyEntity, cell); - JSONObject response = validatRequiredValue(policyEntity, body, finalColumn, configurationBodySet); - if (response != null) { - return response; + String response = null; + response = validatRequiredValue(policyEntity, body, finalColumn, configurationBodySet); + if (!StringUtils.isBlank(response)) { + sendResult = sendResult + "\n" + response; + errorFlag = true; + } + if (!StringUtils.isBlank(response) && !rowIterator.hasNext()) { + return sendResult; } savePolicyEntiies(finalColumn, configurationBodySet, configurationDataEntity, policyEntity, controller, roles, userInfo, scopes, configName, userId, configExists, actionExists, - actionBodyEntity, body); + actionBodyEntity, body, errorFlag); } } @@ -331,9 +341,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController { } private void writeConfigurationFile(ConfigurationDataEntity configurationDataEntity) { - try (FileWriter fw = - new FileWriter(PolicyController.getConfigHome() + File.separator - + configurationDataEntity.getConfigurationName())) { + try (FileWriter fw = new FileWriter( + PolicyController.getConfigHome() + File.separator + configurationDataEntity.getConfigurationName())) { BufferedWriter bw = new BufferedWriter(fw); bw.write(configurationDataEntity.getConfigBody()); bw.close(); @@ -343,8 +352,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController { } private void writeActionBodyFile(ActionBodyEntity actionBodyEntity) { - try (FileWriter fw = - new FileWriter(PolicyController.getActionHome() + File.separator + actionBodyEntity.getActionBodyName())) { + try (FileWriter fw = new FileWriter( + PolicyController.getActionHome() + File.separator + actionBodyEntity.getActionBodyName())) { BufferedWriter bw = new BufferedWriter(fw); bw.write(actionBodyEntity.getActionBody()); bw.close(); @@ -360,49 +369,46 @@ public class PolicyExportAndImportController extends RestrictedBaseController { /** * This is to validate all matching required fields. - * + * * @param policyName String * @param jsonString String * @return String */ public String validatMatchRequiredFields(String policyName, String jsonString) { - + String errorMsg = ""; try { JSONObject jsonObject = new JSONObject(jsonString); - String configName = jsonObject.getString("configName"); - String uuid = jsonObject.getString("uuid"); - String erorMsg = validConfigName(configName); - if (erorMsg != null) { - return erorMsg; + String confErorMsg = validConfigName(jsonObject.getString("configName")); + if (!confErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + confErorMsg; } - erorMsg = validUuid(uuid); - if (erorMsg != null) { - return erorMsg; + String uuidErorMsg = validUuid(jsonObject.getString("uuid")); + if (!uuidErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + uuidErorMsg; } - String location = jsonObject.getString("location"); - erorMsg = validLocation(location); - if (erorMsg != null) { - return erorMsg; + String locErorMsg = validLocation(jsonObject.getString("location")); + if (!locErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + locErorMsg; } - String policyScope = jsonObject.getString("policyScope"); - erorMsg = validPolicyScope(policyScope); - if (erorMsg != null) { - return erorMsg; + String pScopeErorMsg = validPolicyScope(jsonObject.getString("policyScope")); + if (!pScopeErorMsg.isEmpty()) { + errorMsg = errorMsg + "\n POLICY :" + policyName + " at " + pScopeErorMsg; } String msVersion = jsonObject.getString("version"); String msService = jsonObject.getString("service"); if (!isAttributeObjectFound(msService, msVersion)) { - return POLICY + policyName + " MS Service: " + msService + " and MS Version: " + msVersion + NOTVALID; + errorMsg = errorMsg + "\n POLICY :" + policyName + " at MS Service: " + msService + " and MS Version: " + + msVersion + NOTVALID; } } catch (Exception e) { logger.error("Exception Occured While validating required fields", e); } - return null; + return errorMsg; } - private JSONObject validatRequiredValue(PolicyEntity policyEntity, StringBuilder body, boolean finalColumn, + private String validatRequiredValue(PolicyEntity policyEntity, StringBuilder body, boolean finalColumn, boolean configurationBodySet) { if (finalColumn && configurationBodySet && (policyEntity.getPolicyName().contains(CONFIG_MS))) { String errorMsg = validatMatchRequiredFields(policyEntity.getPolicyName(), body.toString()); @@ -410,7 +416,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { logger.error("errorMsg => " + errorMsg); JSONObject response = new JSONObject(); response.append("error", errorMsg); - return response; + return errorMsg; } } return null; @@ -433,7 +439,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private String validUuid(String uuid) { String message = null; if (uuid != null) { - List uuids = commonClassDao.getDataByColumn(DCAEuuid.class, "name"); + List uuids = commonClassDao.getDataByColumn(DcaeUuid.class, "name"); if (uuids != null && !(uuids.stream().filter(o -> o.equals(uuid)).findFirst().isPresent())) { message = POLICY + policyName + " uuid: " + uuid + NOTVALID; } @@ -565,6 +571,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { return false; } + @SuppressWarnings("rawtypes") private boolean isContinue(List roles, String scope, UserInfo userInfo, Set scopes) { if (roles.contains(admin) || roles.contains(editor)) { if (scopes.isEmpty()) { @@ -605,14 +612,12 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private void savePolicyEntity(PolicyEntity policyEntity, String configName, String userId) { if (configName != null) { if (configName.contains(config) || configName.contains(DECISION_MS)) { - ConfigurationDataEntity configuration = - (ConfigurationDataEntity) commonClassDao.getEntityItem(ConfigurationDataEntity.class, - configurationName, configName); + ConfigurationDataEntity configuration = (ConfigurationDataEntity) commonClassDao + .getEntityItem(ConfigurationDataEntity.class, configurationName, configName); policyEntity.setConfigurationData(configuration); } else { - ActionBodyEntity actionBody = - (ActionBodyEntity) commonClassDao.getEntityItem(ActionBodyEntity.class, "actionBodyName", - configName); + ActionBodyEntity actionBody = (ActionBodyEntity) commonClassDao.getEntityItem(ActionBodyEntity.class, + "actionBodyName", configName); policyEntity.setActionBodyEntity(actionBody); } } @@ -639,8 +644,9 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private int getSetBodySize(PolicyEntity policyEntity, Cell cell, int setBodySize) { int setBodySizel = setBodySize; if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) - && ((policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName() - .contains(DECISION_MS)) && (policyEntity.getPolicyName().contains("Config_BRMS_Param_")))) { + && ((policyEntity.getPolicyName().contains(config) + || policyEntity.getPolicyName().contains(DECISION_MS)) + && (policyEntity.getPolicyName().contains("Config_BRMS_Param_")))) { setBodySizel += 1; } return setBodySizel; @@ -664,29 +670,28 @@ public class PolicyExportAndImportController extends RestrictedBaseController { boolean configurationBodySet) { boolean configurationBodySetl = configurationBodySet; if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) - && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS)) - && (setBodySize == bodySize)) { + && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS)) + && (setBodySize == bodySize)) { configurationBodySetl = true; } if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) - && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS)) - && (setBodySize == 0)) { + && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS)) + && (setBodySize == 0)) { configurationBodySetl = true; } return configurationBodySetl; } - private boolean isConfigExists(PolicyEntity policyEntity, Cell cell, boolean configExists) { boolean configExistsl = configExists; if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) - && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS))) { + && (policyEntity.getPolicyName().contains(config) + || policyEntity.getPolicyName().contains(DECISION_MS))) { configExistsl = true; } return configExistsl; } - private boolean isActionExists(PolicyEntity policyEntity, Cell cell, boolean actionExists) { boolean actionExistsl = actionExists; if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) @@ -706,17 +711,18 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private StringBuilder addCellValue(PolicyEntity policyEntity, Cell cell, StringBuilder body) { if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) - && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS))) { + && (policyEntity.getPolicyName().contains(config) + || policyEntity.getPolicyName().contains(DECISION_MS))) { body.append(cell.getStringCellValue()); } return body; } - private ActionBodyEntity setActionBodyObject(PolicyEntity policyEntity, ActionBodyEntity actionBodyEntity, + private ActionBodyEntity setActionBodyObject(PolicyEntity policyEntity, ActionBodyEntity actionBodyEntity, Cell cell) { if (configurationbody.equalsIgnoreCase(getCellHeaderName(cell)) - && (policyEntity.getPolicyName().contains(ACTION))) { + && (policyEntity.getPolicyName().contains(ACTION))) { actionBodyEntity.setActionBody(cell.getStringCellValue()); } return actionBodyEntity; @@ -734,7 +740,8 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private ConfigurationDataEntity populateConfigurationDataEntity(PolicyEntity policyEntity, ConfigurationDataEntity configurationDataEntity, Cell cell) { if (configurationName.equalsIgnoreCase(getCellHeaderName(cell)) - && (policyEntity.getPolicyName().contains(config) || policyEntity.getPolicyName().contains(DECISION_MS))) { + && (policyEntity.getPolicyName().contains(config) + || policyEntity.getPolicyName().contains(DECISION_MS))) { configurationDataEntity.setConfigurationName(cell.getStringCellValue()); } return configurationDataEntity; @@ -751,9 +758,9 @@ public class PolicyExportAndImportController extends RestrictedBaseController { private void savePolicyEntiies(boolean finalColumn, boolean configurationBodySet, ConfigurationDataEntity configurationDataEntity, PolicyEntity policyEntity, PolicyController controller, List roles, UserInfo userInfo, Set scopes, String configName, String userId, - boolean configExists, boolean actionExists, ActionBodyEntity actionBodyEntity, StringBuilder body) { + boolean configExists, boolean actionExists, ActionBodyEntity actionBodyEntity, StringBuilder body, boolean errorFlagSent) { - if (finalColumn && configurationBodySet) { + if (finalColumn && configurationBodySet && !errorFlagSent) { configurationDataEntity.setConfigBody(body.toString()); String scope = policyEntity.getScope().replace(".", File.separator); String query = "FROM PolicyEntity where policyName = :policyName and scope = :policyScope"; @@ -771,7 +778,7 @@ public class PolicyExportAndImportController extends RestrictedBaseController { saveVersion(policyEntity, scope, userId); // // Notify Other paps regarding Export Policy. PolicyRestController restController = new PolicyRestController(); - restController.notifyOtherPAPSToUpdateConfigurations("exportPolicy", configName, null); + restController.notifyOtherPapsToUpdateConfigurations("exportPolicy", configName, null); } } }