X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcomponents%2FHumanPolicyComponent.java;h=23392bdd20f7da56f6568195aa8b3419a843ebb9;hb=9bf8e18042746f52b96155bb2654dfb472f09e14;hp=a0f1ba86d936aec29234a6a41504e6392a40d5a7;hpb=073cc188efe9abb4c010cf674e34e2cf46ef1c52;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/components/HumanPolicyComponent.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/components/HumanPolicyComponent.java index a0f1ba86d..23392bdd2 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/components/HumanPolicyComponent.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/components/HumanPolicyComponent.java @@ -21,10 +21,8 @@ package org.onap.policy.components; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.file.Path; @@ -43,7 +41,6 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.controller.PolicyController; import org.onap.policy.rest.jpa.FunctionDefinition; -import org.onap.policy.utils.XACMLPolicyWriterWithPapNotify; import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.util.XACMLPolicyScanner; @@ -131,13 +128,11 @@ public class HumanPolicyComponent{ return null; } - private static String processPolicy() throws IllegalArgumentException { - if (LOGGER.isTraceEnabled()) + private static String processPolicy() { + if (LOGGER.isTraceEnabled()) { LOGGER.trace("ENTER"); - - FileInputStream pIS = null; - try { - pIS = new FileInputStream(policyFile); + } + try (FileInputStream pIS = new FileInputStream(policyFile)){ Object policy = XACMLPolicyScanner.readPolicy(pIS); if (policy == null) throw new IllegalArgumentException("Policy File " + policyFile.getName() + @@ -160,14 +155,6 @@ public class HumanPolicyComponent{ ": " + e.getMessage(); LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + msg, e); throw new IllegalArgumentException(msg); - } finally { - if (pIS != null) { - try { - pIS.close(); - } catch (IOException e) { - LOGGER.warn(e.getMessage(), e); - } - } } } @@ -202,15 +189,14 @@ class HtmlProcessor extends SimpleCallback { combiningAlgo2human.put("only-one-applicable", "to honour the result of the first successfully evaluated $placeholder$ in order"); } - private Map attributeIdentifiersMap = new HashMap(); + private Map attributeIdentifiersMap = new HashMap<>(); private final StringWriter stringWriter = new StringWriter(); private final PrintWriter htmlOut = new PrintWriter(stringWriter); private final String policyName; private final Object rootPolicyObject; - public HtmlProcessor(File policyFile, Object policyObject) - throws IllegalArgumentException { + public HtmlProcessor(File policyFile, Object policyObject) { if (LOGGER.isTraceEnabled()) LOGGER.trace("ENTER"); @@ -315,29 +301,6 @@ class HtmlProcessor extends SimpleCallback { super.onFinishScan(root); } - @SuppressWarnings("unused") - private void writeRawXACML() { - if (LOGGER.isTraceEnabled()) - LOGGER.trace("ENTER"); - - htmlOut.println("
"); - htmlOut.println("

Raw XACML:

"); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - if (rootPolicyObject instanceof PolicySetType) { - XACMLPolicyWriterWithPapNotify.writePolicyFile(bos, (PolicySetType) rootPolicyObject); - } else if (rootPolicyObject instanceof PolicyType) { - XACMLPolicyWriterWithPapNotify.writePolicyFile(bos, (PolicyType) rootPolicyObject); - } - - String xacml = bos.toString(); - xacml = xacml.replaceAll("<", "<"); - xacml = xacml.replaceAll(">", ">"); - htmlOut.println("
");
-		htmlOut.println(xacml);
-		htmlOut.println("
"); - } - @Override public CallbackResult onPreVisitPolicySet(PolicySetType parent, PolicySetType policySet) { if (LOGGER.isTraceEnabled()) @@ -357,7 +320,7 @@ class HtmlProcessor extends SimpleCallback { else policySet(policySet, "li"); - if (policySet.getPolicySetOrPolicyOrPolicySetIdReference().size() > 0) + if (!policySet.getPolicySetOrPolicyOrPolicySetIdReference().isEmpty()) htmlOut.println("
    "); return super.onPreVisitPolicySet(parent, policySet); @@ -377,7 +340,7 @@ class HtmlProcessor extends SimpleCallback { LOGGER.trace("PolicySet: " + policySet.getPolicySetId() + " Description: " + policySet.getDescription()); - if (policySet.getPolicySetOrPolicyOrPolicySetIdReference().size() > 0) + if (!policySet.getPolicySetOrPolicyOrPolicySetIdReference().isEmpty()) htmlOut.println("
"); htmlOut.println("

"); @@ -409,7 +372,7 @@ class HtmlProcessor extends SimpleCallback { if (policySet.getTarget() == null || policySet.getTarget().getAnyOf() == null || - policySet.getTarget().getAnyOf().size() <= 0) { + policySet.getTarget().getAnyOf().isEmpty()) { htmlOut.println("

This policy set applies to all requests.

"); } else { htmlOut.print("

"); @@ -421,7 +384,7 @@ class HtmlProcessor extends SimpleCallback { } if (policySet.getPolicySetOrPolicyOrPolicySetIdReference() != null && - policySet.getPolicySetOrPolicyOrPolicySetIdReference().size() > 0) { + !policySet.getPolicySetOrPolicyOrPolicySetIdReference().isEmpty()) { String algoDesc = combiningAlgo2human.get(combiningAlgorithm); if (algoDesc != null) { algoDesc = algoDesc.replace("$placeholder$", "policy") + " (" + "" + combiningAlgorithm + ")"; @@ -449,7 +412,7 @@ class HtmlProcessor extends SimpleCallback { policy(policy); - if (policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().size() > 0) + if (!policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().isEmpty()) htmlOut.println("

    "); return super.onPreVisitPolicy(parent, policy); @@ -464,7 +427,7 @@ class HtmlProcessor extends SimpleCallback { LOGGER.trace("PolicySet: " + policy.getPolicyId() + "Parent PolicySet: " + parent.getPolicySetId() + " Version: " + parent.getVersion()); - if (policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().size() > 0) + if (!policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().isEmpty()) htmlOut.println("
"); htmlOut.println("

"); @@ -494,7 +457,7 @@ class HtmlProcessor extends SimpleCallback { if (policy.getTarget() == null || policy.getTarget().getAnyOf() == null || - policy.getTarget().getAnyOf().size() <= 0) { + policy.getTarget().getAnyOf().isEmpty()) { htmlOut.println("

This policy applies to all requests.

"); } else { htmlOut.print("

"); @@ -506,7 +469,7 @@ class HtmlProcessor extends SimpleCallback { } if (policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition() != null && - policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().size() > 0) { + !policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().isEmpty()) { String algoDesc = combiningAlgo2human.get(combiningAlgorithm); if (algoDesc != null) { algoDesc = algoDesc.replace("$placeholder$", "rule") + " (" + combiningAlgorithm + ")"; @@ -566,7 +529,7 @@ class HtmlProcessor extends SimpleCallback { if (rule.getTarget() == null || rule.getTarget().getAnyOf() == null || - rule.getTarget().getAnyOf().size() <= 0) { + rule.getTarget().getAnyOf().isEmpty()) { htmlOut.print(" for all requests"); } else { List anyOf_s = rule.getTarget().getAnyOf(); @@ -690,7 +653,7 @@ class HtmlProcessor extends SimpleCallback { if (anyOfList != null) { Iterator iterAnyOf = anyOfList.iterator(); - String targetInHuman = ""; + StringBuilder targetInHuman = new StringBuilder(); while (iterAnyOf.hasNext()) { AnyOfType anyOf = iterAnyOf.next(); List allOfList = anyOf.getAllOf(); @@ -702,7 +665,7 @@ class HtmlProcessor extends SimpleCallback { if (matchList != null) { Iterator iterMatch = matchList.iterator(); if (matchList.size() > 1) - targetInHuman += "("; + targetInHuman.append("("); while (iterMatch.hasNext()) { MatchType match = iterMatch.next(); // @@ -710,7 +673,7 @@ class HtmlProcessor extends SimpleCallback { // StdAttribute attribute = null; AttributeValueType value = match.getAttributeValue(); - String attributeDataType = null; + String attributeDataType; if (match.getAttributeDesignator() != null && value != null) { AttributeDesignatorType designator = match.getAttributeDesignator(); attribute = new StdAttribute(new IdentifierImpl(designator.getCategory()), @@ -741,7 +704,7 @@ class HtmlProcessor extends SimpleCallback { attribute.getAttributeId().stringValue()); this.attributeIdentifiersMap.put(succintIdentifier,ai); - targetInHuman += "" + succintIdentifier + " " + functionName + " "; + targetInHuman.append("" + succintIdentifier + " " + functionName + " "); int numAttributes = attribute.getValues().size(); int count = 0; @@ -753,38 +716,39 @@ class HtmlProcessor extends SimpleCallback { int countValues = 0; for (Object o : value_s) { countValues++; - targetInHuman += " " + o + ""; + targetInHuman.append(" " + o + ""); if (countValues < numValues) { - targetInHuman += ", or"; + targetInHuman.append(", or"); } } } else { - targetInHuman += " " + v.getValue() + ""; + targetInHuman.append(" " + v.getValue() + ""); if (count < numAttributes) { - targetInHuman += ", or "; + targetInHuman.append(", or "); } } } } if (iterMatch.hasNext()) { - targetInHuman += " and "; + targetInHuman.append(" and "); } } // end iterMatch if (matchList.size() > 1) { - targetInHuman += ")"; + targetInHuman.append(")"); } } if (iterAllOf.hasNext()) { - targetInHuman += " or "; + targetInHuman.append(" or "); } } // end iterAllOf } if (iterAnyOf.hasNext()) { - targetInHuman = "(" + targetInHuman + ")" + " or "; + targetInHuman = new StringBuilder(); + targetInHuman.append("(" + targetInHuman + ")" + " or "); } else { if (anyOfList.size() > 1) { - targetInHuman += ")"; + targetInHuman.append(")"); } } } // end iterAnyOf @@ -822,11 +786,12 @@ class HtmlProcessor extends SimpleCallback { } private String removePrimitives(String in) { - in = in.replace("string-", ""); - in = in.replace("integer-", ""); - in = in.replace("double-", ""); - in = in.replace("boolean-", ""); - return in; + String newIn = in; + newIn = newIn.replace("string-", ""); + newIn = newIn.replace("integer-", ""); + newIn = newIn.replace("double-", ""); + newIn = newIn.replace("boolean-", ""); + return newIn; } private String stringifyCondition(ConditionType condition) { @@ -854,21 +819,20 @@ class HtmlProcessor extends SimpleCallback { } List> exps = apply.getExpression(); - if (exps == null || exps.size() == 0) + if (exps == null || exps.isEmpty()) return ""; else { - String forResult = ""; + StringBuilder forResult = new StringBuilder(); for (JAXBElement e : exps) { Object v = e.getValue(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("one-and-only children: " + v); } if (v != null) { - // C: return stringifyExpression(v, result); - forResult += stringifyExpression(v); + forResult.append(stringifyExpression(v)); } } - return forResult; + return forResult.toString(); } } @@ -883,20 +847,20 @@ class HtmlProcessor extends SimpleCallback { if (LOGGER.isDebugEnabled()) { LOGGER.debug(functionName + " 1 expression: " + numExpr); } - String applySubresult = ""; + StringBuilder applySubresult = new StringBuilder(); for (JAXBElement e : apply.getExpression()) { Object v = e.getValue(); if (v != null) { - applySubresult += this.stringifyExpression(e.getValue()); + applySubresult.append(this.stringifyExpression(e.getValue())); } } - return " " + removePrimitives(functionName) + " (" + applySubresult + ")"; + return " " + removePrimitives(functionName) + " (" + applySubresult.toString() + ")"; } else { // > 1 arguments if (LOGGER.isDebugEnabled()) { LOGGER.debug(functionName + " > 1 expressions: " + numExpr); } - String applySubresult = ""; + StringBuilder applySubresult = new StringBuilder(); int exprCount = 0; for (JAXBElement e : apply.getExpression()) { exprCount++; @@ -904,20 +868,20 @@ class HtmlProcessor extends SimpleCallback { if (ev != null) { if (ev instanceof ApplyType) { if (((ApplyType) ev).getFunctionId().contains("one-and-only")) { - applySubresult += this.stringifyExpression(e.getValue()); + applySubresult.append(this.stringifyExpression(e.getValue())); } else { - applySubresult += "(" + this.stringifyExpression(e.getValue()) + ")"; + applySubresult.append("(" + this.stringifyExpression(e.getValue()) + ")"); } } else { - applySubresult += this.stringifyExpression(e.getValue()); + applySubresult.append(this.stringifyExpression(e.getValue())); } if (exprCount < numExpr) { - applySubresult += " " + removePrimitives(functionName) + " "; + applySubresult.append(" " + removePrimitives(functionName) + " "); } } } - return applySubresult; + return applySubresult.toString(); } } if (expression instanceof AttributeDesignatorType) { @@ -952,11 +916,11 @@ class HtmlProcessor extends SimpleCallback { if (expression instanceof AttributeValueType) { AttributeValueType avt = (AttributeValueType) expression; List content = avt.getContent(); - String value_s = ""; + StringBuilder value_s = new StringBuilder(); for (Object o: content) { - value_s += " " + o.toString(); + value_s.append(" " + o.toString()); } - return " " + value_s; + return " " + value_s.toString(); } if (expression instanceof VariableReferenceType) { //