X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=ONAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Frest%2Futil%2FPolicyValidation.java;fp=ONAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Frest%2Futil%2FPolicyValidation.java;h=223381afe84033dc08225aed1ec3e92cd35c72e5;hb=b3828de54af94f66e3a6e22bcaeb3372d14eab34;hp=0000000000000000000000000000000000000000;hpb=db597fe2163c073e37ad2ddc0d7fe386517e21e0;p=policy%2Fengine.git diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java new file mode 100644 index 000000000..223381afe --- /dev/null +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java @@ -0,0 +1,730 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Engine + * ================================================================================ + * Copyright (C) 2017 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.rest.util; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.lang.StringUtils; +import org.json.JSONObject; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.rest.adapter.ClosedLoopFaultBody; +import org.onap.policy.rest.adapter.ClosedLoopPMBody; +import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.rest.dao.CommonClassDao; +import org.onap.policy.rest.jpa.MicroServiceModels; +import org.onap.policy.rest.jpa.SafePolicyWarning; +import org.onap.policy.utils.PolicyUtils; +import org.onap.policy.xacml.api.XACMLErrorConstants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; + +@Service +public class PolicyValidation { + + private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidation.class); + + public static final String CONFIG_POLICY = "Config"; + public static final String ACTION_POLICY = "Action"; + public static final String DECISION_POLICY = "Decision"; + public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault"; + public static final String CLOSEDLOOP_PM = "ClosedLoop_PM"; + public static final String ENFORCER_CONFIG_POLICY= "Enforcer Config"; + public static final String MICROSERVICES="Micro Service"; + public static final String FIREWALL="Firewall Config"; + + private static Map mapAttribute = new HashMap<>(); + + private static CommonClassDao commonClassDao; + + @Autowired + public PolicyValidation(CommonClassDao commonClassDao){ + PolicyValidation.commonClassDao = commonClassDao; + } + + /* + * This is an empty constructor + */ + public PolicyValidation(){} + + + public StringBuilder validatePolicy(PolicyRestAdapter policyData) throws IOException{ + try{ + boolean valid = true; + StringBuilder responseString = new StringBuilder(); + ObjectMapper mapper = new ObjectMapper(); + + if(policyData.getPolicyName() != null){ + String policyNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getPolicyName()); + if(!policyNameValidate.contains("success")){ + responseString.append("PolicyName:" + policyNameValidate + "
"); + valid = false; + }; + }else{ + responseString.append( "PolicyName: PolicyName Should not be empty" + "
"); + valid = false; + } + if(policyData.getPolicyDescription() != null){ + String descriptionValidate = PolicyUtils.descriptionValidator(policyData.getPolicyDescription()); + if(!descriptionValidate.contains("success")){ + responseString.append("Description:" + descriptionValidate + "
"); + valid = false; + } + } + + if(CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())){ + if ("Base".equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType()) + || CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType()) || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType()) + || MICROSERVICES.equals(policyData.getConfigPolicyType())) { + + if(!Strings.isNullOrEmpty(policyData.getOnapName())) { + String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName()); + if(!onapNameValidate.contains("success")){ + responseString.append("OnapName:" + onapNameValidate + "
"); + valid = false; + } + }else{ + responseString.append("Onap Name: Onap Name Should not be empty" + "
"); + valid = false; + } + } + + if(!Strings.isNullOrEmpty(policyData.getRiskType())) { + String riskTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getRiskType()); + if(!riskTypeValidate.contains("success")){ + responseString.append("RiskType:" + riskTypeValidate + "
"); + valid = false; + } + }else { + responseString.append("Risk Type: Risk Type Should not be Empty" + "
"); + valid = false; + } + + if(!Strings.isNullOrEmpty(policyData.getRiskLevel())) { + String validateRiskLevel = PolicyUtils.policySpecialCharValidator(policyData.getRiskLevel()); + if(!validateRiskLevel.contains("success")){ + responseString.append("RiskLevel:" + validateRiskLevel + "
"); + valid = false; + } + }else { + responseString.append("Risk Level: Risk Level Should not be Empty" + "
"); + valid = false; + } + + if(!Strings.isNullOrEmpty(policyData.getGuard())) { + String validateGuard = PolicyUtils.policySpecialCharValidator(policyData.getGuard()); + if(!validateGuard.contains("success")){ + responseString.append("Guard:" + validateGuard + "
"); + valid = false; + } + }else { + responseString.append("Guard: Guard Value Should not be Empty" + "
"); + valid = false; + } + + if("Base".equalsIgnoreCase(policyData.getConfigPolicyType())){ + if(!Strings.isNullOrEmpty(policyData.getConfigName())) { + String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); + if(!configNameValidate.contains("success")){ + responseString.append("ConfigName:" + configNameValidate + "
"); + valid = false; + } + }else{ + responseString.append("Config Name: Config Name Should not be Empty" + "
"); + valid = false; + } + if(!Strings.isNullOrEmpty(policyData.getConfigType())) { + String configTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigType()); + if(!configTypeValidate.contains("success")){ + responseString.append("ConfigType:" + configTypeValidate + "
"); + valid = false; + } + }else{ + responseString.append("Config Type: Config Type Should not be Empty" + "
"); + valid = false; + } + if(!Strings.isNullOrEmpty(policyData.getConfigBodyData())) { + String configBodyData = policyData.getConfigBodyData(); + String configType = policyData.getConfigType(); + if (configType != null) { + if (configType.equals("JSON")) { + if (!PolicyUtils.isJSONValid(configBodyData)) { + responseString.append("Config Body: JSON Content is not valid" + "
"); + valid = false; + } + } else if (configType.equals("XML")) { + if (!PolicyUtils.isXMLValid(configBodyData)) { + responseString.append("Config Body: XML Content data is not valid" + "
"); + valid = false; + } + } else if (configType.equals("PROPERTIES")) { + if (!PolicyUtils.isPropValid(configBodyData)||configBodyData.equals("")) { + responseString.append("Config Body: Property data is not valid" + "
"); + valid = false; + } + } else if (configType.equals("OTHER")) { + if (configBodyData.equals("")) { + responseString.append("Config Body: Config Body Should not be Empty" + "
"); + valid = false; + } + } + } + }else{ + responseString.append("Config Body: Config Body Should not be Empty" + "
"); + valid = false; + } + } + + if("Firewall Config".equalsIgnoreCase(policyData.getConfigPolicyType())){ + if(policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()){ + String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); + if(!configNameValidate.contains("success")){ + responseString.append("ConfigName:" + configNameValidate + "
"); + valid = false; + } + }else{ + responseString.append("Config Name: Config Name is required" + "
"); + valid = false; + } + if(policyData.getSecurityZone() == null || policyData.getSecurityZone().isEmpty()){ + responseString.append("Security Zone: Security Zone is required" + "
"); + valid = false; + } + } + if("BRMS_Param".equalsIgnoreCase(policyData.getConfigPolicyType())){ + if(policyData.getRuleName() == null || policyData.getRuleName().isEmpty()){ + responseString.append("BRMS Template:BRMS Template is required
"); + valid = false; + } + } + if("BRMS_Raw".equalsIgnoreCase(policyData.getConfigPolicyType())){ + if(policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()){ + String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData()); + + // If there are any error other than Annotations then this is not Valid + if(message.contains("[ERR")){ + responseString.append("Raw Rule Validate:Raw Rule has error"+ message +"
"); + valid = false; + } + }else{ + responseString.append("Raw Rule:Raw Rule is required
"); + valid = false; + } + } + if("ClosedLoop_PM".equalsIgnoreCase(policyData.getConfigPolicyType())){ + try{ + if(Strings.isNullOrEmpty(policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString())){ + responseString.append("ServiceType PolicyName:ServiceType PolicyName is required
"); + valid = false; + } + + }catch(Exception e){ + LOGGER.error("ERROR in ClosedLoop_PM PolicyName" , e); + responseString.append("ServiceType PolicyName:ServiceType PolicyName is required
"); + valid = false; + } + + if(policyData.getJsonBody() != null){ + ClosedLoopPMBody pmBody = (ClosedLoopPMBody)mapper.readValue(policyData.getJsonBody(), ClosedLoopPMBody.class); + if(pmBody.getEmailAddress() != null){ + String result = emailValidation(pmBody.getEmailAddress(), responseString.toString()); + if(result != "success"){ + responseString.append(result + "
"); + valid = false; + } + } + if((pmBody.isGamma() || pmBody.isMcr() || pmBody.isTrinity() || pmBody.isvDNS() || pmBody.isvUSP()) != true){ + responseString.append("D2/Virtualized Services: Select at least one D2/Virtualized Services
"); + valid = false; + } + if(pmBody.getGeoLink() != null && !pmBody.getGeoLink().isEmpty()){ + String result = PolicyUtils.policySpecialCharValidator(pmBody.getGeoLink()); + if(!result.contains("success")){ + responseString.append("GeoLink:" + result + "
"); + valid = false; + }; + } + if(pmBody.getAttributes() != null && !pmBody.getAttributes().isEmpty()){ + for(Entry entry : pmBody.getAttributes().entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); + if(!key.contains("Message")){ + String attributeValidate = PolicyUtils.policySpecialCharValidator(value); + if(!attributeValidate.contains("success")){ + responseString.append("Attributes:" + key + " : value has spaces or invalid characters
"); + valid = false; + }; + } + } + } + }else{ + responseString.append("D2/Virtualized Services:Select atleast one D2/Virtualized Services
"); + valid = false; + } + } + if("ClosedLoop_Fault".equalsIgnoreCase(policyData.getConfigPolicyType())){ + if(policyData.getJsonBody() != null){ + + // For API we need to get the conditions key from the Json request and check it before deserializing to POJO due to the enum + if("API".equals(policyData.getApiflag())){ + JSONObject json = new JSONObject(policyData.getJsonBody()); + if(!json.isNull("conditions")){ + String apiCondition = (String) json.get("conditions"); + if(Strings.isNullOrEmpty(apiCondition)){ + responseString.append("Conditions: Select At least one Condition
"); + return responseString; + } + } else { + responseString.append("Conditions: There were no conditions provided in configBody json
"); + return responseString; + } + } + + ClosedLoopFaultBody faultBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopFaultBody.class); + if(faultBody.getEmailAddress() != null && !faultBody.getEmailAddress().isEmpty()){ + String result = emailValidation(faultBody.getEmailAddress(), responseString.toString()); + if(result != "success"){ + responseString.append(result+ "
"); + valid = false; + } + } + if((faultBody.isGamma() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS() || faultBody.isvUSP()) != true){ + responseString.append("D2/Virtualized Services: Select at least one D2/Virtualized Services
"); + valid = false; + } + if(faultBody.getActions() == null || faultBody.getActions().isEmpty()){ + responseString.append("vPRO Actions: vPRO Actions is required
"); + valid = false; + } + if(faultBody.getClosedLoopPolicyStatus() == null || faultBody.getClosedLoopPolicyStatus().isEmpty()){ + responseString.append("Policy Status: Policy Status is required
"); + valid = false; + } + if(faultBody.getConditions() == null){ + responseString.append("Conditions: Select At least one Condition
"); + valid = false; + } + if(faultBody.getGeoLink() != null && !faultBody.getGeoLink().isEmpty()){ + String result = PolicyUtils.policySpecialCharWithSpaceValidator(faultBody.getGeoLink()); + if(!result.contains("success")){ + responseString.append("GeoLink:" + result + "
"); + valid = false; + } + } + if(faultBody.getAgingWindow() == 0){ + responseString.append("Aging Window: Aging Window is required
"); + valid = false; + } + if(faultBody.getTimeInterval() == 0){ + responseString.append("Time Interval: Time Interval is required
"); + valid = false; + } + if(faultBody.getRetrys() == 0){ + responseString.append("Number of Retries: Number of Retries is required
"); + valid = false; + } + if(faultBody.getTimeOutvPRO() == 0){ + responseString.append("APP-C Timeout: APP-C Timeout is required
"); + valid = false; + } + if(faultBody.getTimeOutRuby() == 0){ + responseString.append("TimeOutRuby: TimeOutRuby is required
"); + valid = false; + } + if(faultBody.getVnfType() == null || faultBody.getVnfType().isEmpty()){ + responseString.append("Vnf Type: Vnf Type is required
"); + valid = false; + } + }else{ + responseString.append("D2/Virtualized Services: Select atleast one D2/Virtualized Services
"); + responseString.append("vPRO Actions: vPRO Actions is required
"); + responseString.append("Aging Window: Aging Window is required
"); + responseString.append("Policy Status: Policy Status is required
"); + responseString.append("Conditions: Select Atleast one Condition
"); + responseString.append("PEP Name: PEP Name is required
"); + responseString.append("PEP Action: PEP Action is required
"); + responseString.append("Time Interval: Time Interval is required
"); + responseString.append("Number of Retries: Number of Retries is required
"); + responseString.append("APP-C Timeout: APP-C Timeout is required
"); + responseString.append("TimeOutRuby: TimeOutRuby is required
"); + responseString.append("Vnf Type: Vnf Type is required
"); + valid = false; + } + } + + if ("Micro Service".equals(policyData.getConfigPolicyType())){ + if(!Strings.isNullOrEmpty(policyData.getServiceType())){ + pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON()); + MicroServiceModels returnModel = new MicroServiceModels(); + String service = null; + String version = null; + if (policyData.getServiceType().contains("-v")){ + service = policyData.getServiceType().split("-v")[0]; + version = policyData.getServiceType().split("-v")[1]; + }else { + service = policyData.getServiceType(); + version = policyData.getVersion(); + } + + if(!Strings.isNullOrEmpty(version)) { + returnModel = getAttributeObject(service, version); + + if(returnModel != null) { + String annotation = returnModel.getAnnotation(); + if (!Strings.isNullOrEmpty(annotation)){ + Map rangeMap = new HashMap<>(); + rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation); + for (Entry rMap : rangeMap.entrySet()){ + if (rMap.getValue().contains("range::")){ + String value = mapAttribute.get(rMap.getKey().trim()); + String[] tempString = rMap.getValue().split("::")[1].split("-"); + int startNum = Integer.parseInt(tempString[0]); + int endNum = Integer.parseInt(tempString[1]); + String returnString = "InvalidreturnModel Range:" + rMap.getKey() + " must be between " + + startNum + " - " + endNum + ","; + + if(value != null) { + if (PolicyUtils.isInteger(value.replace("\"", ""))){ + int result = Integer.parseInt(value.replace("\"", "")); + if (result < startNum || result > endNum){ + responseString.append(returnString); + valid = false; + } + }else { + responseString.append(returnString); + valid = false; + } + } else { + responseString.append(""+rMap.getKey()+":" + rMap.getKey() + + " is required for the MicroService model " + service + "
"); + valid = false; + } + + } + } + } + } else { + responseString.append("Micro Service Model: Invalid Model. The model name, " + service + + " of version, " + version + " was not found in the dictionary
"); + valid = false; + } + } else { + responseString.append("Micro Version: Micro Service Version is required" + "
"); + valid = false; + } + } else { + responseString.append("Micro Service: Micro Service is required" + "
"); + valid = false; + } + + if(Strings.isNullOrEmpty(policyData.getPriority())){ + responseString.append("Priority: Priority is required" + "
"); + valid = false; + } + } + } + if (DECISION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){ + if(!Strings.isNullOrEmpty(policyData.getOnapName())){ + String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName()); + if(!onapNameValidate.contains("success")){ + responseString.append("OnapName:" + onapNameValidate + "
"); + valid = false; + } + }else{ + responseString.append("Onap Name: Onap Name Should not be empty" + "
"); + valid = false; + } + + if("Rainy_Day".equals(policyData.getRuleProvider())){ + if(policyData.getRainyday()==null){ + responseString.append(" Rainy Day Parameters are Required
"); + valid = false; + }else{ + if(Strings.isNullOrEmpty(policyData.getRainyday().getServiceType())){ + responseString.append("Rainy Day Service Type is Required
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getRainyday().getVnfType())){ + responseString.append("Rainy Day VNF Type is Required
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getRainyday().getBbid())){ + responseString.append("Rainy Day Building Block ID is Required
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getRainyday().getWorkstep())){ + responseString.append("Rainy Day Work Step is Required
"); + valid = false; + } + if(!policyData.getRainyday().getTreatmentTableChoices().isEmpty() && + policyData.getRainyday().getTreatmentTableChoices() != null){ + + for(Object treatmentMap: policyData.getRainyday().getTreatmentTableChoices()){ + String errorCode = null; + String treatment = null; + if(treatmentMap instanceof LinkedHashMap){ + + if(((LinkedHashMap) treatmentMap).containsKey("errorcode")){ + errorCode = ((LinkedHashMap) treatmentMap).get("errorcode").toString(); + } + if(((LinkedHashMap) treatmentMap).containsKey("treatment")){ + treatment = ((LinkedHashMap) treatmentMap).get("treatment").toString(); + } + + } + if(Strings.isNullOrEmpty(errorCode) && Strings.isNullOrEmpty(treatment)){ + responseString.append("Rainy Day Error Code and Desired Treatment cannot be empty
"); + valid = false; + break; + } + if(Strings.isNullOrEmpty(errorCode)){ + responseString.append("Rainy Day Error Code is Required for each Desired Treatment
"); + valid = false; + break; + } + if(Strings.isNullOrEmpty(treatment)){ + responseString.append("Rainy Day Desired Treatment is Required for each Error Code
"); + valid = false; + break; + } + } + + } else { + responseString.append("Rainy Day Desired Automated Treatments are Required
"); + valid = false; + } + } + } + + if("GUARD_YAML".equals(policyData.getRuleProvider()) || "GUARD_BL_YAML".equals(policyData.getRuleProvider())){ + if(policyData.getYamlparams()==null){ + responseString.append(" Guard Params are Required " + "
"); + valid = false; + }else{ + if(Strings.isNullOrEmpty(policyData.getYamlparams().getActor())){ + responseString.append("Guard Params Actor is Required " + "
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getYamlparams().getRecipe())){ + responseString.append("Guard Params Recipe is Required " + "
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveStart())){ + responseString.append("Guard Params Guard Active Start is Required " + "
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveEnd())){ + responseString.append("Guard Params Guard Active End is Required " + "
"); + valid = false; + } + if("GUARD_YAML".equals(policyData.getRuleProvider())){ + if(Strings.isNullOrEmpty(policyData.getYamlparams().getLimit())){ + responseString.append(" Guard Params Limit is Required " + "
"); + valid = false; + }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())){ + responseString.append(" Guard Params Limit Should be Integer " + "
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getYamlparams().getTimeWindow())){ + responseString.append("Guard Params Time Window is Required" + "
"); + valid = false; + }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())){ + responseString.append(" Guard Params Time Window Should be Integer " + "
"); + valid = false; + } + if(Strings.isNullOrEmpty(policyData.getYamlparams().getTimeUnits())){ + responseString.append("Guard Params Time Units is Required" + "
"); + valid = false; + } + }else if("GUARD_BL_YAML".equals(policyData.getRuleProvider())){ + if(policyData.getYamlparams().getBlackList()==null || policyData.getYamlparams().getBlackList().isEmpty()){ + responseString.append(" Guard Params BlackList is Required " + "
"); + valid = false; + }else{ + for(String blackList: policyData.getYamlparams().getBlackList()){ + if(blackList==null || !("success".equals(PolicyUtils.policySpecialCharValidator(blackList)))){ + responseString.append(" Guard Params BlackList Should be valid String" + "
"); + valid = false; + break; + } + } + } + } + } + } + } + + if(ACTION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){ + if(!Strings.isNullOrEmpty(policyData.getActionPerformer())){ + String actionPerformer = PolicyUtils.policySpecialCharValidator(policyData.getActionPerformer()); + if(!actionPerformer.contains("success")){ + responseString.append("ActionPerformer:" + actionPerformer + "
"); + valid = false; + } + }else{ + responseString.append("ActionPerformer: ActionPerformer Should not be empty" + "
"); + valid = false; + } + if(policyData.getAttributes() != null){ + for(Object attribute : policyData.getAttributes()){ + if(attribute instanceof LinkedHashMap){ + try{ + //This is for validation check if the value exists or not + String key = ((LinkedHashMap) attribute).get("key").toString(); + String value = ((LinkedHashMap) attribute).get("value").toString(); + if("".equals(key) || "".equals(value)){ + responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "
"); + valid = false; + break; + } + }catch(Exception e){ + LOGGER.error("This is a Policy Validation check" +e); + responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "
"); + valid = false; + break; + } + } + } + }else{ + responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "
"); + valid = false; + } + if(!Strings.isNullOrEmpty(policyData.getActionAttributeValue())){ + String actionAttribute = PolicyUtils.policySpecialCharValidator(policyData.getActionAttributeValue()); + if(!actionAttribute.contains("success")){ + responseString.append("ActionAttribute:" + actionAttribute + "
"); + valid = false; + }; + }else{ + responseString.append("ActionAttribute: ActionAttribute Should not be empty" + "
"); + valid = false; + } + + if(!policyData.getRuleAlgorithmschoices().isEmpty()){ + for(Object attribute : policyData.getRuleAlgorithmschoices()){ + if(attribute instanceof LinkedHashMap){ + try{ + String label = ((LinkedHashMap) attribute).get("id").toString(); + String key = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField1").toString(); + String rule = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmCombo").toString(); + String value = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField2").toString(); + + if(Strings.isNullOrEmpty(label) || Strings.isNullOrEmpty(key) || Strings.isNullOrEmpty(rule) || Strings.isNullOrEmpty(value)){ + responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "
"); + valid = false; + } + }catch(Exception e){ + LOGGER.error("This is a Policy Validation check" +e); + responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "
"); + valid = false; + break; + } + } + } + } + } + + if(CONFIG_POLICY.equals(policyData.getPolicyType())){ + String value = ""; + if(valid){ + if(commonClassDao!=null){ + List spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType", policyData.getRiskType()); + if (!spData.isEmpty()){ + SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) spData.get(0); + value = "Message:" + safePolicyWarningData.getMessage(); + } + } + responseString.append("success" + "@#"+ value); + } + }else{ + if(valid){ + responseString.append("success"); + } + } + + return responseString; + } + catch (Exception e){ + LOGGER.error("Exception Occured during Policy Validation" +e); + return null; + } + } + + protected String emailValidation(String email, String response){ + if(email != null){ + String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", "")); + if(!validateEmail.contains("success")){ + response += "Email:" + validateEmail+ "
"; + }else{ + return "success"; + } + } + return response; + } + + private MicroServiceModels getAttributeObject(String name, String version) { + MicroServiceModels workingModel = null; + try{ + List microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name+":"+version); + if(microServiceModelsData != null){ + workingModel = (MicroServiceModels) microServiceModelsData.get(0); + } + }catch(Exception e){ + String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template. The template name, " + + name + " was not found in the dictionary: "; + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e); + return null; + } + + return workingModel; + } + + private void pullJsonKeyPairs(JsonNode rootNode) { + Iterator> fieldsIterator = rootNode.fields(); + + while (fieldsIterator.hasNext()) { + Map.Entry field = fieldsIterator.next(); + final String key = field.getKey(); + final JsonNode value = field.getValue(); + if (value.isContainerNode() && !value.isArray()) { + pullJsonKeyPairs(value); // RECURSIVE CALL + } else { + if (value.isArray()){ + String newValue = StringUtils.replaceEach(value.toString(), new String[]{"[", "]", "\""}, new String[]{"", "", ""}); + mapAttribute.put(key, newValue); + }else { + mapAttribute.put(key, value.toString().trim()); + } + } + } + } + +}