diff options
author | krishnajinka <kris.jinka@samsung.com> | 2018-08-09 07:35:57 +0900 |
---|---|---|
committer | krishnajinka <kris.jinka@samsung.com> | 2018-08-10 08:42:56 +0900 |
commit | 6ed1731fa2ce340d745dc2d39ffc6a066f50fbc2 (patch) | |
tree | 426db83bf48fd4f14fd7d3ca73a2a42bd1b3f687 /ONAP-PDP-REST/src/main/java | |
parent | 5e8b7f573f0f7f08be242ee017ecf1243317f1b5 (diff) |
Add PAP Policy parameter builder xacml policy
Fix Sonar flagged major issue about 14 params instead of 9
in the constructor of std pap policy class. Commit5 for
modify constrctor.fix test
Issue-ID: POLICY-1016
Change-Id: I79c8482b7fc4e203cb18402ba2b5a68a120c57b1
Signed-off-by: krisjinka <kris.jinka@samsung.com>
Diffstat (limited to 'ONAP-PDP-REST/src/main/java')
10 files changed, 787 insertions, 727 deletions
diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ActionPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ActionPolicyService.java index 66920e0db..5016e0690 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ActionPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ActionPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -30,76 +30,79 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * Action Policy Implementation. - * + * Action Policy Implementation. + * * @version 0.1 */ public class ActionPolicyService { - private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyService.class.getName()); - private PAPServices papServices = null; - - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private Map<String,String> componentAttributes = null; - private String actionAttribute = null; - private String actionPerformer = null; - - public ActionPolicyService(String policyScope, String policyName, - PolicyParameters policyParameters) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - papServices = new PAPServices(); - } + private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyService.class.getName()); + private PAPServices papServices = null; - public Boolean getValidation() { - if(policyParameters.getAttributes()==null || policyParameters.getAttributes().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given."; - return false; - } - componentAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); - if (componentAttributes==null||componentAttributes.isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given."; - return false; - } - actionAttribute = policyParameters.getActionAttribute(); - if (actionAttribute==null||actionAttribute.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Attribute given."; - return false; - } - actionPerformer = policyParameters.getActionPerformer(); - if (actionPerformer==null||actionPerformer.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Performer given."; - return false; - } - if(!"PEP".equalsIgnoreCase(actionPerformer)&& !"PDP".equalsIgnoreCase(actionPerformer)){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Action Performer given."; - return false; - } - return true; - } + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private Map<String, String> componentAttributes = null; + private String actionAttribute = null; + private String actionPerformer = null; - public String getMessage() { - return message; - } + public ActionPolicyService(String policyScope, String policyName, + PolicyParameters policyParameters) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - // Create Policy - StdPAPPolicy newPAPPolicy = new StdPAPPolicy(policyName, policyParameters.getPolicyDescription(), - componentAttributes, policyParameters.getDynamicRuleAlgorithmLabels(), policyParameters.getDynamicRuleAlgorithmFunctions(), - policyParameters.getDynamicRuleAlgorithmField1(), policyParameters.getDynamicRuleAlgorithmField2(), actionPerformer, actionAttribute, updateFlag, policyScope, 0); - // send Json to PAP - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Action"}, policyParameters.getRequestID(), "Action"); - LOGGER.info(response); - return response; - } + public Boolean getValidation() { + if (policyParameters.getAttributes() == null || policyParameters.getAttributes().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given."; + return false; + } + componentAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); + if (componentAttributes == null || componentAttributes.isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given."; + return false; + } + actionAttribute = policyParameters.getActionAttribute(); + if (actionAttribute == null || actionAttribute.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Attribute given."; + return false; + } + actionPerformer = policyParameters.getActionPerformer(); + if (actionPerformer == null || actionPerformer.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Performer given."; + return false; + } + if (!"PEP".equalsIgnoreCase(actionPerformer) && !"PDP".equalsIgnoreCase(actionPerformer)) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Action Performer given."; + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + // Create Policy + StdPAPPolicy newPAPPolicy = new StdPAPPolicy(policyName, policyParameters.getPolicyDescription(), + componentAttributes, policyParameters.getDynamicRuleAlgorithmLabels(), + policyParameters.getDynamicRuleAlgorithmFunctions(), + policyParameters.getDynamicRuleAlgorithmField1(), policyParameters.getDynamicRuleAlgorithmField2(), + actionPerformer, actionAttribute, updateFlag, policyScope, 0); + // send Json to PAP + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Action"}, policyParameters.getRequestID(), "Action"); + LOGGER.info(response); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSParamPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSParamPolicyService.java index dd2bd452e..29b181238 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSParamPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSParamPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -31,66 +31,68 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * BRMS Param Policy Implementation. - * + * BRMS Param Policy Implementation. + * * @version 0.1 */ -public class BRMSParamPolicyService{ - private static final Logger LOGGER = FlexLogger.getLogger(BRMSParamPolicyService.class.getName()); - private PAPServices papServices = null; - - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private Map<AttributeType, Map<String, String>> drlRuleAndUIParams = null; - - public BRMSParamPolicyService(String policyName, String policyScope, - PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } +public class BRMSParamPolicyService { + private static final Logger LOGGER = FlexLogger.getLogger(BRMSParamPolicyService.class.getName()); + private PAPServices papServices = null; - public Boolean getValidation() { - boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if(!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - return false; - } - drlRuleAndUIParams = policyParameters.getAttributes(); - if(drlRuleAndUIParams==null || drlRuleAndUIParams.isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Rule Attributes given."; - return false; - } - return true; - } + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private Map<AttributeType, Map<String, String>> drlRuleAndUIParams = null; - public String getMessage() { - return message; - } + public BRMSParamPolicyService(String policyName, String policyScope, + PolicyParameters policyParameters, String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - // Create Policy - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param",policyName, policyParameters.getPolicyDescription(), - "BRMS_PARAM_RULE",updateFlag,policyScope, - drlRuleAndUIParams.get(AttributeType.MATCHING), 0, "DROOLS", - null, drlRuleAndUIParams.get(AttributeType.RULE), policyParameters.getRiskLevel(), - policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date, policyParameters.getControllerName(), policyParameters.getDependencyNames()); - // Send JSON to PAP - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsParam"); - LOGGER.info(response); - return response; - } + public Boolean getValidation() { + boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + return false; + } + drlRuleAndUIParams = policyParameters.getAttributes(); + if (drlRuleAndUIParams == null || drlRuleAndUIParams.isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Rule Attributes given."; + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + // Create Policy + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param", policyName, policyParameters.getPolicyDescription(), + "BRMS_PARAM_RULE", updateFlag, policyScope, + drlRuleAndUIParams.get(AttributeType.MATCHING), 0, "DROOLS", + null, drlRuleAndUIParams.get(AttributeType.RULE), policyParameters.getRiskLevel(), + policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date, + policyParameters.getControllerName(), policyParameters.getDependencyNames()); + // Send JSON to PAP + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsParam"); + LOGGER.info(response); + return response; + } }
\ No newline at end of file diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSRawPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSRawPolicyService.java index ea17529ba..df3f0356f 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSRawPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/BRMSRawPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -32,75 +32,77 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * BRMS RAW Policy Implementation. - * + * BRMS RAW Policy Implementation. + * * @version 0.1 */ -public class BRMSRawPolicyService{ - private static Logger LOGGER = FlexLogger.getLogger(BRMSRawPolicyService.class.getName()); - private static PAPServices papServices = null; - - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private boolean levelCheck = false; - private String brmsRawBody = null; - - public BRMSRawPolicyService(String policyName, String policyScope, - PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } +public class BRMSRawPolicyService { + private static Logger LOGGER = FlexLogger.getLogger(BRMSRawPolicyService.class.getName()); + private static PAPServices papServices = null; - public Boolean getValidation() { - brmsRawBody = policyParameters.getConfigBody(); - if(brmsRawBody==null || brmsRawBody.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Rule Body given"; - return false; - } - message = PolicyUtils.brmsRawValidate(brmsRawBody); - if(message.contains("[ERR")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Raw rule given is invalid" +message; - return false; - } - levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if(!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - return false; - } - return true; - } + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private boolean levelCheck = false; + private String brmsRawBody = null; - public String getMessage() { - return message; - } + public BRMSRawPolicyService(String policyName, String policyScope, + PolicyParameters policyParameters, String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - Map<String,String> ruleAttributes = null; - if(policyParameters.getAttributes()!=null){ - ruleAttributes = policyParameters.getAttributes().get(AttributeType.RULE); - } - // Create Policy - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw",policyName,policyParameters.getPolicyDescription(), - "BRMS_RAW_RULE",updateFlag,policyScope, ruleAttributes, 0, "DROOLS", - brmsRawBody, policyParameters.getRiskLevel(), - policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date, policyParameters.getControllerName(), policyParameters.getDependencyNames()); - // Send JSON to PAP - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsRaw"); - LOGGER.info(response); - return response; - } + public Boolean getValidation() { + brmsRawBody = policyParameters.getConfigBody(); + if (brmsRawBody == null || brmsRawBody.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Rule Body given"; + return false; + } + message = PolicyUtils.brmsRawValidate(brmsRawBody); + if (message.contains("[ERR")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Raw rule given is invalid" + message; + return false; + } + levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + Map<String, String> ruleAttributes = null; + if (policyParameters.getAttributes() != null) { + ruleAttributes = policyParameters.getAttributes().get(AttributeType.RULE); + } + // Create Policy + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw", policyName, policyParameters.getPolicyDescription(), + "BRMS_RAW_RULE", updateFlag, policyScope, ruleAttributes, 0, "DROOLS", + brmsRawBody, policyParameters.getRiskLevel(), + policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date, + policyParameters.getControllerName(), policyParameters.getDependencyNames()); + // Send JSON to PAP + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "ConfigBrmsRaw"); + LOGGER.info(response); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopFaultPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopFaultPolicyService.java index 5817d7410..6879b6e2f 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopFaultPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopFaultPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -31,91 +31,97 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * Closed Loop Fault Policy Implementation. - * - * @version 0.1 + * Closed Loop Fault Policy Implementation. + * + * @version 0.1 */ -public class ClosedLoopFaultPolicyService{ - private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopFaultPolicyService.class.getName()); - private PAPServices papServices = null; - - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private JsonObject configBody = null; - - public ClosedLoopFaultPolicyService(String policyName, String policyScope, - PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } +public class ClosedLoopFaultPolicyService { + private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopFaultPolicyService.class.getName()); + private PAPServices papServices = null; - public Boolean getValidation() { - if(policyParameters.getConfigBody()==null){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Config Body Present"; - return false; - } - if(!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - return false; - } - try{ - configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); - } catch(JsonException| IllegalStateException e){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - LOGGER.error("Json Parse Exception.", e); - return false; - } - return true; - } + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private JsonObject configBody = null; - public String getMessage() { - return message; - } + public ClosedLoopFaultPolicyService(String policyName, String policyScope, + PolicyParameters policyParameters, String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - String oldPolicyName = null; - if (updateFlag){ - operation = "update"; - if (policyName.endsWith("_Draft")) { - oldPolicyName = policyName + "_Draft.1"; - } - } else { - operation = "create"; - } - // get values and attributes from the JsonObject - if(!configBody.containsKey("onapname")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; - LOGGER.error(message); - return message; - } - String onapName = configBody.get("onapname").toString().trim().replace("\"", ""); - if (onapName==null||onapName.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; - LOGGER.error(message); - return message; - } - boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if (!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - LOGGER.error(message); - return message; - } - String jsonBody = configBody.toString(); - // Create Policy. - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_Fault", policyName, policyParameters.getPolicyDescription(), onapName, - jsonBody, false, oldPolicyName, null, updateFlag, policyScope, 0, policyParameters.getRiskLevel(), - policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); - //send JSON object to PAP - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop"); - return response; - } + public Boolean getValidation() { + if (policyParameters.getConfigBody() == null) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Config Body Present"; + return false; + } + if (!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + return false; + } + try { + configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); + } catch (JsonException | IllegalStateException e) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + LOGGER.error("Json Parse Exception.", e); + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + String oldPolicyName = null; + if (updateFlag) { + operation = "update"; + if (policyName.endsWith("_Draft")) { + oldPolicyName = policyName + "_Draft.1"; + } + } else { + operation = "create"; + } + // get values and attributes from the JsonObject + if (!configBody.containsKey("onapname")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; + LOGGER.error(message); + return message; + } + String onapName = configBody.get("onapname").toString().trim().replace("\"", ""); + if (onapName == null || onapName.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; + LOGGER.error(message); + return message; + } + boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + LOGGER.error(message); + return message; + } + String jsonBody = configBody.toString(); + // Create Policy. + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_Fault", policyName, + policyParameters.getPolicyDescription(), onapName, + jsonBody, false, oldPolicyName, null, updateFlag, policyScope, 0, policyParameters.getRiskLevel(), + policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); + //send JSON object to PAP + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop"); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopPMPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopPMPolicyService.java index 74c4bd71d..3f0416492 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopPMPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ClosedLoopPMPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -31,92 +31,98 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * Closed Loop PM policy Implementation. - * + * Closed Loop PM policy Implementation. + * * @version 0.1 */ -public class ClosedLoopPMPolicyService{ - private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopPMPolicyService.class.getName()); - - private PAPServices papServices = null; - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private JsonObject configBody = null; - - public ClosedLoopPMPolicyService(String policyName, String policyScope, - PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } +public class ClosedLoopPMPolicyService { + private static final Logger LOGGER = FlexLogger.getLogger(ClosedLoopPMPolicyService.class.getName()); - public Boolean getValidation() { - if(policyParameters.getConfigBody()==null){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Config Body Present"; - return false; - } - if(!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - return false; - } - try{ - configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); - } catch(JsonException| IllegalStateException e){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - LOGGER.error("Error during parsing JSON config body for Closed loop PM policy " , e); - return false; - } - return true; - } + private PAPServices papServices = null; + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private JsonObject configBody = null; - public String getMessage() { - return message; - } + public ClosedLoopPMPolicyService(String policyName, String policyScope, + PolicyParameters policyParameters, String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException{ - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - // get values and attributes from the JsonObject - if(!configBody.containsKey("onapname")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; - LOGGER.error(message); - return message; - } - if(!configBody.containsKey("serviceTypePolicyName")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Service Type Policy Name given."; - LOGGER.error(message); - return message; - } - String onapName = configBody.get("onapname").toString().trim().replace("\"", ""); - if (onapName==null||onapName.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; - LOGGER.error(message); - return message; - } - boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if (!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - LOGGER.error(message); - return message; - } - String jsonBody = configBody.toString(); - String serviceType = configBody.get("serviceTypePolicyName").toString().replace("\"", ""); - // Create Policy. - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", policyName, policyParameters.getPolicyDescription(), onapName, - jsonBody, false, null, serviceType, updateFlag, policyScope, 0, policyParameters.getRiskLevel(), - policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); - //send JSON object to PAP - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop"); - return response; - } + public Boolean getValidation() { + if (policyParameters.getConfigBody() == null) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Config Body Present"; + return false; + } + if (!PolicyApiUtils.validateNONASCIICharactersAndAllowSpaces(policyParameters.getConfigBody())) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + return false; + } + try { + configBody = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); + } catch (JsonException | IllegalStateException e) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + LOGGER.error("Error during parsing JSON config body for Closed loop PM policy ", e); + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + // get values and attributes from the JsonObject + if (!configBody.containsKey("onapname")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; + LOGGER.error(message); + return message; + } + if (!configBody.containsKey("serviceTypePolicyName")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Service Type Policy Name given."; + LOGGER.error(message); + return message; + } + String onapName = configBody.get("onapname").toString().trim().replace("\"", ""); + if (onapName == null || onapName.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; + LOGGER.error(message); + return message; + } + boolean levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + LOGGER.error(message); + return message; + } + String jsonBody = configBody.toString(); + String serviceType = configBody.get("serviceTypePolicyName").toString().replace("\"", ""); + // Create Policy. + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", policyName, + policyParameters.getPolicyDescription(), onapName, + jsonBody, false, null, serviceType, updateFlag, policyScope, 0, policyParameters.getRiskLevel(), + policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); + //send JSON object to PAP + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "ConfigClosedLoop"); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ConfigPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ConfigPolicyService.java index 690e94a5e..46f968aac 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ConfigPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ConfigPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -30,107 +30,124 @@ import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils; import org.onap.policy.utils.PolicyUtils; import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; +import org.onap.policy.xacml.std.pap.StdPAPPolicyParams; /** - * Config Base Policy Implementation. - * + * Config Base Policy Implementation. + * * @version 0.1 */ public class ConfigPolicyService { - private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName()); - private PAPServices papServices = null; - - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private String onapName = null; - private String configName = null; - - public ConfigPolicyService(String policyName, String policyScope, - PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } + private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName()); + private PAPServices papServices = null; - public Boolean getValidation() { - if(policyParameters.getConfigBody()==null || policyParameters.getConfigBody().trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "No Config Body given."; - return false; - } - if(policyParameters.getConfigBodyType()==null){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "No Config Body Type given."; - return false; - } - boolean levelCheck = false; - levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if (!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - return false; - } - onapName = policyParameters.getOnapName(); - configName = policyParameters.getConfigName(); - if(onapName==null || onapName.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given."; - return false; - } - if(configName==null || configName.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given."; - return false; - } - message = PolicyUtils.policySpecialCharValidator(onapName); - if(!message.contains("success")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message; + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private String onapName = null; + private String configName = null; + + public ConfigPolicyService(String policyName, String policyScope, + PolicyParameters policyParameters, String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } + + public Boolean getValidation() { + if (policyParameters.getConfigBody() == null || policyParameters.getConfigBody().trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given."; + return false; + } + if (policyParameters.getConfigBodyType() == null) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body Type given."; + return false; + } + boolean levelCheck = false; + levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + return false; + } + onapName = policyParameters.getOnapName(); + configName = policyParameters.getConfigName(); + if (onapName == null || onapName.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given."; + return false; + } + if (configName == null || configName.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given."; + return false; + } + message = PolicyUtils.policySpecialCharValidator(onapName); + if (!message.contains("success")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + message; return false; } message = PolicyUtils.policySpecialCharValidator(configName); - if(!message.contains("success")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message; + if (!message.contains("success")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + message; return false; } - return true; - } + return true; + } - public String getMessage() { - return message; - } + public String getMessage() { + return message; + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - String configType = policyParameters.getConfigBodyType().toString(); - String body = policyParameters.getConfigBody(); - String configBody = null; - //check body for JSON form and remove single quotes if present - if ("JSON".equalsIgnoreCase(configType)) { - if (body.contains("'")) { - configBody = body.replace("'", "\""); - } else { - configBody = body; - } - } else { - configBody = body; - } - Map<String,String> configAttributes = null; - if(policyParameters.getAttributes()!=null){ - configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); - } - // create Policy. - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", policyName, policyParameters.getPolicyDescription(), onapName, configName, configAttributes, configType, - configBody, updateFlag, policyScope,0, policyParameters.getRiskLevel(),policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); - // Send Json to PAP. - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "Config"); - LOGGER.info(response); - return response; - } + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + String configType = policyParameters.getConfigBodyType().toString(); + String body = policyParameters.getConfigBody(); + String configBody = null; + //check body for JSON form and remove single quotes if present + if ("JSON".equalsIgnoreCase(configType)) { + if (body.contains("'")) { + configBody = body.replace("'", "\""); + } else { + configBody = body; + } + } else { + configBody = body; + } + Map<String, String> configAttributes = null; + if (policyParameters.getAttributes() != null) { + configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); + } + // create Policy. + StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder() + .configPolicyType("Base") + .policyName(policyName) + .description(policyParameters.getPolicyDescription()) + .onapName(onapName) + .configName(configName) + .attributes(configAttributes) + .configType(configType) + .configBodyData(configBody) + .editPolicy(updateFlag) + .domain(policyScope) + .highestVersion(0) + .riskLevel(policyParameters.getRiskLevel()) + .riskType(policyParameters.getRiskType()) + .guard(String.valueOf(policyParameters.getGuard())) + .ttlDate(date) + .build()); + // Send Json to PAP. + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "Config"); + LOGGER.info(response); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DecisionPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DecisionPolicyService.java index 915e3b30f..7bdf1dc71 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DecisionPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/DecisionPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -31,73 +31,83 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * Decision Policy Implementation - * - * @version 0.1 + * Decision Policy Implementation + * + * @version 0.1 */ -public class DecisionPolicyService{ - private static Logger LOGGER = FlexLogger.getLogger(DecisionPolicyService.class.getName()); - private static PAPServices papServices = null; - - private String message = null; - private String policyScope = null; - private String policyName = null; - private PolicyParameters policyParameters = null; - private String onapName = null; - - public DecisionPolicyService(String policyScope, String policyName, - PolicyParameters policyParameters) { - this.policyScope = policyScope; - this.policyName = policyName; - this.policyParameters = policyParameters; - papServices = new PAPServices(); - } +public class DecisionPolicyService { + private static Logger LOGGER = FlexLogger.getLogger(DecisionPolicyService.class.getName()); + private static PAPServices papServices = null; - public Boolean getValidation() { - onapName = policyParameters.getOnapName(); - if (onapName==null||onapName.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given."; - return false; - } - return true; - } + private String message = null; + private String policyScope = null; + private String policyName = null; + private PolicyParameters policyParameters = null; + private String onapName = null; - public String getMessage() { - return message; - } + public DecisionPolicyService(String policyScope, String policyName, + PolicyParameters policyParameters) { + this.policyScope = policyScope; + this.policyName = policyName; + this.policyParameters = policyParameters; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - RuleProvider ruleProvider = policyParameters.getRuleProvider(); - if (ruleProvider==null) { - ruleProvider = RuleProvider.CUSTOM ; - } - Map<String,String> matchingAttributes = null; - Map<String,String> settingsAttributes = null; - - //Get the MATCHING and/or SETTINGS attributes - if (policyParameters.getAttributes()!=null && policyParameters.getAttributes().containsKey(AttributeType.MATCHING) && policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) { - matchingAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); - settingsAttributes = policyParameters.getAttributes().get(AttributeType.SETTINGS); - }else if(policyParameters.getAttributes()!=null && !policyParameters.getAttributes().containsKey(AttributeType.MATCHING) && policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)){ - settingsAttributes = policyParameters.getAttributes().get(AttributeType.SETTINGS); - }else if(policyParameters.getAttributes()!=null && policyParameters.getAttributes().containsKey(AttributeType.MATCHING) && !policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)){ - matchingAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); - } - // Create StdPAPPolicy object used to send policy data to PAP-REST. - StdPAPPolicy newPAPPolicy = new StdPAPPolicy(policyName, policyParameters.getPolicyDescription(), onapName, ruleProvider.toString(), matchingAttributes, settingsAttributes, - policyParameters.getTreatments(), policyParameters.getDynamicRuleAlgorithmLabels(), policyParameters.getDynamicRuleAlgorithmFunctions(), - policyParameters.getDynamicRuleAlgorithmField1(), policyParameters.getDynamicRuleAlgorithmField2(), null, null, null, updateFlag, policyScope, 0); - // Send JSON to PAP. - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Decision"}, policyParameters.getRequestID(), "Decision"); - LOGGER.info(message); - return response; - } + public Boolean getValidation() { + onapName = policyParameters.getOnapName(); + if (onapName == null || onapName.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given."; + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + RuleProvider ruleProvider = policyParameters.getRuleProvider(); + if (ruleProvider == null) { + ruleProvider = RuleProvider.CUSTOM; + } + Map<String, String> matchingAttributes = null; + Map<String, String> settingsAttributes = null; + + //Get the MATCHING and/or SETTINGS attributes + if (policyParameters.getAttributes() != null && + policyParameters.getAttributes().containsKey(AttributeType.MATCHING) && + policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) { + matchingAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); + settingsAttributes = policyParameters.getAttributes().get(AttributeType.SETTINGS); + } else if (policyParameters.getAttributes() != null && + !policyParameters.getAttributes().containsKey(AttributeType.MATCHING) && + policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) { + settingsAttributes = policyParameters.getAttributes().get(AttributeType.SETTINGS); + } else if (policyParameters.getAttributes() != null && + policyParameters.getAttributes().containsKey(AttributeType.MATCHING) && + !policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) { + matchingAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING); + } + // Create StdPAPPolicy object used to send policy data to PAP-REST. + StdPAPPolicy newPAPPolicy = new StdPAPPolicy(policyName, policyParameters.getPolicyDescription(), onapName, + ruleProvider.toString(), matchingAttributes, settingsAttributes, + policyParameters.getTreatments(), policyParameters.getDynamicRuleAlgorithmLabels(), + policyParameters.getDynamicRuleAlgorithmFunctions(), + policyParameters.getDynamicRuleAlgorithmField1(), policyParameters.getDynamicRuleAlgorithmField2(), + null, null, null, updateFlag, policyScope, 0); + // Send JSON to PAP. + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Decision"}, policyParameters.getRequestID(), "Decision"); + LOGGER.info(message); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/FirewallPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/FirewallPolicyService.java index 8f4ba9a79..45deeb49f 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/FirewallPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/FirewallPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -31,83 +31,88 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * Firewall Policy Implementation. - * + * Firewall Policy Implementation. + * * @version 0.1 */ public class FirewallPolicyService { - private static final Logger LOGGER = FlexLogger.getLogger(FirewallPolicyService.class.getName()); - - private PAPServices papServices = null; - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private JsonObject firewallJson = null; - - public FirewallPolicyService(String policyName, String policyScope, - PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } + private static final Logger LOGGER = FlexLogger.getLogger(FirewallPolicyService.class.getName()); - public Boolean getValidation() { - if(policyParameters.getConfigBody()==null){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "No Config Body given."; - return false; - } - try{ - firewallJson = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); - } catch(JsonException| IllegalStateException e){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - LOGGER.error("Error while parsing JSON body for creating Firewall Policy " , e); - return false; - } - if(firewallJson==null|| firewallJson.isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config-Body given."; - return false; - } - boolean levelCheck = false; - levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if (!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - return false; - } - return true; - } + private PAPServices papServices = null; + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private JsonObject firewallJson = null; - public String getMessage() { - return message; - } + public FirewallPolicyService(String policyName, String policyScope, + PolicyParameters policyParameters, String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } - public String getResult(boolean updateFlag) throws PolicyException { - String response = null; - String operation = null; - if (updateFlag){ - operation = "update"; - } else { - operation = "create"; - } - //set values for basic policy information - if(!firewallJson.containsKey("configName")){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No configName given in firwall JSON."; - LOGGER.error(message); - return message; - } - String configName = firewallJson.get("configName").toString(); - String configDescription = ""; - String json = firewallJson.toString(); - // Create Policy. - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", policyName, configDescription, configName, updateFlag, policyScope, json, 0, - policyParameters.getRiskLevel(),policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); - // Send Json to PAP. - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigFirewall"); - LOGGER.info(response); - return response; - } + public Boolean getValidation() { + if (policyParameters.getConfigBody() == null) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Body given."; + return false; + } + try { + firewallJson = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); + } catch (JsonException | IllegalStateException e) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + LOGGER.error("Error while parsing JSON body for creating Firewall Policy ", e); + return false; + } + if (firewallJson == null || firewallJson.isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config-Body given."; + return false; + } + boolean levelCheck = false; + levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; + String operation = null; + if (updateFlag) { + operation = "update"; + } else { + operation = "create"; + } + //set values for basic policy information + if (!firewallJson.containsKey("configName")) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No configName given in firwall JSON."; + LOGGER.error(message); + return message; + } + String configName = firewallJson.get("configName").toString(); + String configDescription = ""; + String json = firewallJson.toString(); + // Create Policy. + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", policyName, configDescription, configName, + updateFlag, policyScope, json, 0, + policyParameters.getRiskLevel(), policyParameters.getRiskType(), + String.valueOf(policyParameters.getGuard()), date); + // Send Json to PAP. + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "ConfigFirewall"); + LOGGER.info(response); + return response; + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/MicroServicesPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/MicroServicesPolicyService.java index 44740391e..9ca149240 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/MicroServicesPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/MicroServicesPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -31,64 +31,67 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * MicroServices Policy implementation. - * + * MicroServices Policy implementation. + * * @version 0.1 */ -public class MicroServicesPolicyService{ - private static final Logger LOGGER = FlexLogger.getLogger(MicroServicesPolicyService.class.getName()); - - private PAPServices papServices = null; - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; - private String onapName = null; - private JsonObject microServiceAttributes = null; - - public MicroServicesPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } +public class MicroServicesPolicyService { + private static final Logger LOGGER = FlexLogger.getLogger(MicroServicesPolicyService.class.getName()); - public Boolean getValidation() { - if(policyParameters.getConfigBody()==null){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Micro Service or Attributes Config Body Present"; - return false; - } - try{ - microServiceAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); - } catch(JsonException| IllegalStateException e){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e); - return false; - } - onapName = policyParameters.getOnapName(); - if (onapName==null||onapName.trim().isEmpty()){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; - return false; - } - boolean levelCheck = false; - levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); - if (!levelCheck){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; - return false; - } - return true; - } + private PAPServices papServices = null; + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + private String onapName = null; + private JsonObject microServiceAttributes = null; - public String getMessage() { - return message; - } - - public String getResult(boolean updateFlag) throws PolicyException{ - String response = null; + public MicroServicesPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, + String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } + + public Boolean getValidation() { + if (policyParameters.getConfigBody() == null) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + " No Micro Service or Attributes Config Body Present"; + return false; + } + try { + microServiceAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); + } catch (JsonException | IllegalStateException e) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e); + return false; + } + onapName = policyParameters.getOnapName(); + if (onapName == null || onapName.trim().isEmpty()) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Onap Name given."; + return false; + } + boolean levelCheck = false; + levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel()); + if (!levelCheck) { + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given."; + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; String operation = null; - if (updateFlag){ + if (updateFlag) { operation = "update"; } else { operation = "create"; @@ -98,39 +101,40 @@ public class MicroServicesPolicyService{ String msLocation = null; String configName = null; String microService = null; - String policyDescription=null; - String priority=null; - String version=null; - - if (microServiceAttributes.get("service")!=null){ - microService = microServiceAttributes.get("service").toString().replace("\"", ""); + String policyDescription = null; + String priority = null; + String version = null; + + if (microServiceAttributes.get("service") != null) { + microService = microServiceAttributes.get("service").toString().replace("\"", ""); } - if (microServiceAttributes.get("uuid")!=null){ + if (microServiceAttributes.get("uuid") != null) { uuid = microServiceAttributes.get("uuid").toString().replace("\"", ""); } - if (microServiceAttributes.get("location")!=null){ + if (microServiceAttributes.get("location") != null) { msLocation = microServiceAttributes.get("location").toString().replace("\"", ""); } - if (microServiceAttributes.get("configName")!=null){ + if (microServiceAttributes.get("configName") != null) { configName = microServiceAttributes.get("configName").toString().replace("\"", ""); } - if(microServiceAttributes.containsKey("description")){ - policyDescription = microServiceAttributes.get("description").toString().replace("\"", ""); + if (microServiceAttributes.containsKey("description")) { + policyDescription = microServiceAttributes.get("description").toString().replace("\"", ""); } - if(microServiceAttributes.containsKey("priority")){ - priority = microServiceAttributes.get("priority").toString().replace("\"", ""); + if (microServiceAttributes.containsKey("priority")) { + priority = microServiceAttributes.get("priority").toString().replace("\"", ""); } - if(microServiceAttributes.containsKey("version")){ - version = microServiceAttributes.get("version").toString().replace("\"", ""); + if (microServiceAttributes.containsKey("version")) { + version = microServiceAttributes.get("version").toString().replace("\"", ""); } // Create Policy. - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Micro Service", policyName, policyDescription, onapName, - configName, microService, uuid, msLocation, microServiceAttributes.toString(), priority, + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Micro Service", policyName, policyDescription, onapName, + configName, microService, uuid, msLocation, microServiceAttributes.toString(), priority, version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(), - policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); + policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); // Send JSON Object to PAP. - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigMS"); + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, policyParameters.getRequestID(), "ConfigMS"); LOGGER.info("Policy MS created Response: " + response); return response; - } + } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/OptimizationPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/OptimizationPolicyService.java index 61038d955..7df90ed8b 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/OptimizationPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/OptimizationPolicyService.java @@ -7,9 +7,9 @@ * 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. @@ -21,6 +21,7 @@ package org.onap.policy.pdp.rest.api.services; import javax.json.JsonException; import javax.json.JsonObject; + import org.onap.policy.api.PolicyException; import org.onap.policy.api.PolicyParameters; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -30,81 +31,85 @@ import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPAPPolicy; /** - * Optimization Policy implementation. - * + * Optimization Policy implementation. + * * @version 0.1 */ -public class OptimizationPolicyService{ - private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.class.getName()); - - private PAPServices papServices = null; - private PolicyParameters policyParameters = null; - private String message = null; - private String policyName = null; - private String policyScope = null; - private String date = null; +public class OptimizationPolicyService { + private static final Logger LOGGER = FlexLogger.getLogger(OptimizationPolicyService.class.getName()); + + private PAPServices papServices = null; + private PolicyParameters policyParameters = null; + private String message = null; + private String policyName = null; + private String policyScope = null; + private String date = null; + + public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, + String date) { + this.policyParameters = policyParameters; + this.policyName = policyName; + this.policyScope = policyScope; + this.date = date; + papServices = new PAPServices(); + } - public OptimizationPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, String date) { - this.policyParameters = policyParameters; - this.policyName = policyName; - this.policyScope = policyScope; - this.date = date; - papServices = new PAPServices(); - } + public String getMessage() { + return message; + } - public String getMessage() { - return message; - } - - public String getResult(boolean updateFlag) throws PolicyException{ - String response = null; + public String getResult(boolean updateFlag) throws PolicyException { + String response = null; String operation = null; - - if (updateFlag){ + + if (updateFlag) { operation = "update"; } else { operation = "create"; } - + // get values and attributes from the JsonObject String servicModel = null; - String policyDescription=null; - String priority=null; - String version=null; - - String onapName = policyParameters.getOnapName(); - JsonObject optimizationAttributes = null; - try{ - optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); - } catch(JsonException| IllegalStateException e){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody(); - LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e); - return null; - } - - if (optimizationAttributes.get("service")!=null){ - servicModel = optimizationAttributes.get("service").toString().replace("\"", ""); + String policyDescription = null; + String priority = null; + String version = null; + + String onapName = policyParameters.getOnapName(); + JsonObject optimizationAttributes = null; + try { + optimizationAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody()); + } catch (JsonException | IllegalStateException e) { + message = + XACMLErrorConstants.ERROR_DATA_ISSUE + " improper JSON object : " + + policyParameters.getConfigBody(); + LOGGER.error("Error while parsing JSON body for MicroService Policy creation. ", e); + return null; + } + + if (optimizationAttributes.get("service") != null) { + servicModel = optimizationAttributes.get("service").toString().replace("\"", ""); } - if(optimizationAttributes.containsKey("description")){ - policyDescription = optimizationAttributes.get("description").toString().replace("\"", ""); + if (optimizationAttributes.containsKey("description")) { + policyDescription = optimizationAttributes.get("description").toString().replace("\"", ""); } - if(optimizationAttributes.containsKey("priority")){ - priority = optimizationAttributes.get("priority").toString().replace("\"", ""); + if (optimizationAttributes.containsKey("priority")) { + priority = optimizationAttributes.get("priority").toString().replace("\"", ""); } - if(optimizationAttributes.containsKey("version")){ - version = optimizationAttributes.get("version").toString().replace("\"", ""); + if (optimizationAttributes.containsKey("version")) { + version = optimizationAttributes.get("version").toString().replace("\"", ""); } - + // Create Policy Object - StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Optimization", policyName, policyDescription, onapName, - null, servicModel, null, null, optimizationAttributes.toString(), priority, + StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Optimization", policyName, policyDescription, onapName, + null, servicModel, null, null, optimizationAttributes.toString(), priority, version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(), - policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); - + policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); + // Send JSON Object to PAP - response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, - policyParameters.getRequestID(), "ConfigOptimization"); + response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api", + "policyType=Config"}, + policyParameters.getRequestID(), "ConfigOptimization"); LOGGER.info("Response: " + response); return response; - } + } } |