Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / main / java / org / onap / policy / xacml / util / XACMLPolicyWriter.java
index fb65bb7..f28d829 100644 (file)
@@ -30,13 +30,13 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Iterator;
 import java.util.List;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
-import org.onap.policy.common.logging.eelf.MessageCodes;
-import org.onap.policy.common.logging.eelf.PolicyLogger;
+
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
@@ -52,6 +52,9 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 
+import org.onap.policy.common.logging.eelf.MessageCodes;
+import org.onap.policy.common.logging.eelf.PolicyLogger;
+
 /**
  * Helper static class for policy writing.
  *
@@ -128,7 +131,6 @@ public class XACMLPolicyWriter {
         }
     }
 
-
     /**
      * Helper static class that does the work to write a policy to a file on disk.
      *
@@ -148,6 +150,7 @@ public class XACMLPolicyWriter {
 
     /**
      * Helper static class that reads the JAXB element and return policy input stream.
+     * 
      * @param policyElement
      * @param className (PolicyType or PolicySetType ?).
      * @return ByteArrayInputStream.
@@ -203,21 +206,24 @@ public class XACMLPolicyWriter {
         }
     }
 
-    @SuppressWarnings({ "unchecked", "rawtypes" })
+    @SuppressWarnings({"unchecked", "rawtypes"})
     public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
 
         String extension = "";
         String domain = null;
         String repository = "repository";
-        if(filename.toString().contains("Config_")){
-            domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Config_"));
-        }else if(filename.toString().contains("Action_")){
-            domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Action_"));
-        }else if(filename.toString().contains("Decision_")){
-            domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Decision_"));
+        if (filename.toString().contains("Config_")) {
+            domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length() + 1),
+                    filename.toString().indexOf("Config_"));
+        } else if (filename.toString().contains("Action_")) {
+            domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length() + 1),
+                    filename.toString().indexOf("Action_"));
+        } else if (filename.toString().contains("Decision_")) {
+            domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length() + 1),
+                    filename.toString().indexOf("Decision_"));
         }
-        if(domain.contains(File.separator)){
-            domain =   domain.replace(File.separator, ".");
+        if (domain.contains(File.separator)) {
+            domain = domain.replace(File.separator, ".");
         }
         try {
             JAXBContext context = JAXBContext.newInstance(PolicyType.class);
@@ -227,73 +233,100 @@ public class XACMLPolicyWriter {
             if (policyType != null) {
                 TargetType targetType = policyType.getTarget();
                 List<AnyOfType> anyOfTypes = targetType.getAnyOf();
-                for( Iterator anyOfIte = anyOfTypes.iterator(); anyOfIte.hasNext(); ){
+                for (Iterator anyOfIte = anyOfTypes.iterator(); anyOfIte.hasNext();) {
                     AnyOfType anyOfType = (AnyOfType) anyOfIte.next();
                     List<AllOfType> allOf = anyOfType.getAllOf();
-                    for( Iterator allOfIte = allOf.iterator(); allOfIte.hasNext(); ){
+                    for (Iterator allOfIte = allOf.iterator(); allOfIte.hasNext();) {
                         AllOfType allOfType = (AllOfType) allOfIte.next();
                         List<MatchType> match = allOfType.getMatch();
-                        forIterator matchIte = match.iterator(); matchIte.hasNext();) {
-                            MatchType  matchType = (MatchType) matchIte.next();
-                            if("PolicyName".equals(matchType.getAttributeDesignator().getAttributeId())){
+                        for (Iterator matchIte = match.iterator(); matchIte.hasNext();) {
+                            MatchType matchType = (MatchType) matchIte.next();
+                            if ("PolicyName".equals(matchType.getAttributeDesignator().getAttributeId())) {
                                 AttributeValueType attributeValueType = matchType.getAttributeValue();
                                 List<Object> contents = attributeValueType.getContent();
                                 if (contents != null && !contents.isEmpty()) {
-                                    String tmp = filename.getFileName()+"";
+                                    String tmp = filename.getFileName() + "";
                                     String newName = tmp.substring(0, tmp.lastIndexOf("."));
                                     attributeValueType.getContent().clear();
-                                    attributeValueType.getContent().add(domain + newName  + "." + "xml");
+                                    attributeValueType.getContent().add(domain + newName + "." + "xml");
                                 }
                             }
                         }
                     }
                 }
-                if(filename.toString().contains("Config_") || filename.toString().contains("Action_")){
-                    List<Object> objects = policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
+                if (filename.toString().contains("Config_") || filename.toString().contains("Action_")) {
+                    List<Object> objects =
+                            policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
                     if (objects != null && !objects.isEmpty()) {
                         for (Iterator ite = objects.iterator(); ite.hasNext();) {
 
-                            RuleType  ruleType = (RuleType ) ite.next();
+                            RuleType ruleType = (RuleType) ite.next();
                             AdviceExpressionsType adviceExpressionsType = ruleType.getAdviceExpressions();
                             if (adviceExpressionsType != null) {
-                                List<AdviceExpressionType> adviceExpressionTypes = adviceExpressionsType.getAdviceExpression();
+                                List<AdviceExpressionType> adviceExpressionTypes =
+                                        adviceExpressionsType.getAdviceExpression();
                                 if (adviceExpressionTypes != null && !adviceExpressionTypes.isEmpty()) {
-                                    for (Iterator iterator = adviceExpressionTypes
-                                            .iterator(); iterator.hasNext();) {
-                                        AdviceExpressionType adviceExpressionType = (AdviceExpressionType) iterator
-                                                .next();
-                                        if (adviceExpressionType.getAdviceId() != null && !"".equals(adviceExpressionType.getAdviceId()) && ("configID".equals(adviceExpressionType.getAdviceId())
-                                                || "faultID".equals(adviceExpressionType.getAdviceId()) || "PMID".equals(adviceExpressionType.getAdviceId())||"firewallConfigID".equals(adviceExpressionType.getAdviceId()) || "OptimizationID".equals(adviceExpressionType.getAdviceId())
-                                                || "MSID".equals(adviceExpressionType.getAdviceId())) || "GocID".equals(adviceExpressionType.getAdviceId())||"GocHPID".equals(adviceExpressionType.getAdviceId())||"BRMSRAWID".equals(adviceExpressionType.getAdviceId())
-                                                || "BRMSPARAMID".equals(adviceExpressionType.getAdviceId())|| "HPSuppID".equals(adviceExpressionType.getAdviceId()) || "HPFlapID".equals(adviceExpressionType.getAdviceId()) || "HPOverID".equals(adviceExpressionType.getAdviceId()))
-                                        {
-                                            List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = adviceExpressionType.getAttributeAssignmentExpression();
-                                            if (attributeAssignmentExpressionTypes != null && !attributeAssignmentExpressionTypes.isEmpty()) {
-                                                for (Iterator iterator2 = attributeAssignmentExpressionTypes
-                                                        .iterator(); iterator2.hasNext();) {
-                                                    AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
-                                                            .next();
-                                                    if ("URLID".equals(attributeAssignmentExpressionType.getAttributeId())) {
-                                                        JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
-                                                        AttributeValueType attributeValueType1 = attributeValueType.getValue();
+                                    for (Iterator iterator = adviceExpressionTypes.iterator(); iterator.hasNext();) {
+                                        AdviceExpressionType adviceExpressionType =
+                                                (AdviceExpressionType) iterator.next();
+                                        if (adviceExpressionType.getAdviceId() != null
+                                                && !"".equals(adviceExpressionType.getAdviceId())
+                                                && ("configID".equals(adviceExpressionType.getAdviceId())
+                                                        || "faultID".equals(adviceExpressionType.getAdviceId())
+                                                        || "PMID".equals(adviceExpressionType.getAdviceId())
+                                                        || "firewallConfigID".equals(adviceExpressionType.getAdviceId())
+                                                        || "OptimizationID".equals(adviceExpressionType.getAdviceId())
+                                                        || "MSID".equals(adviceExpressionType.getAdviceId()))
+                                                || "GocID".equals(adviceExpressionType.getAdviceId())
+                                                || "GocHPID".equals(adviceExpressionType.getAdviceId())
+                                                || "BRMSRAWID".equals(adviceExpressionType.getAdviceId())
+                                                || "BRMSPARAMID".equals(adviceExpressionType.getAdviceId())
+                                                || "HPSuppID".equals(adviceExpressionType.getAdviceId())
+                                                || "HPFlapID".equals(adviceExpressionType.getAdviceId())
+                                                || "HPOverID".equals(adviceExpressionType.getAdviceId())) {
+                                            List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes =
+                                                    adviceExpressionType.getAttributeAssignmentExpression();
+                                            if (attributeAssignmentExpressionTypes != null
+                                                    && !attributeAssignmentExpressionTypes.isEmpty()) {
+                                                for (Iterator iterator2 =
+                                                        attributeAssignmentExpressionTypes.iterator(); iterator2
+                                                                .hasNext();) {
+                                                    AttributeAssignmentExpressionType attributeAssignmentExpressionType =
+                                                            (AttributeAssignmentExpressionType) iterator2.next();
+                                                    if ("URLID".equals(
+                                                            attributeAssignmentExpressionType.getAttributeId())) {
+                                                        JAXBElement<AttributeValueType> attributeValueType =
+                                                                (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType
+                                                                        .getExpression();
+                                                        AttributeValueType attributeValueType1 =
+                                                                attributeValueType.getValue();
                                                         String configUrl = "$URL";
-                                                        String urlVal = (String) attributeValueType1.getContent().get(0);
-                                                        String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
+                                                        String urlVal =
+                                                                (String) attributeValueType1.getContent().get(0);
+                                                        String origExtension =
+                                                                urlVal.substring(urlVal.lastIndexOf('.') + 1).trim();
                                                         extension = origExtension;
                                                         attributeValueType1.getContent().clear();
                                                         String txtFileName = filename.getFileName().toString();
-                                                        txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
-                                                        txtFileName = configUrl+ File.separator + "Config" + File.separator + domain + txtFileName;
+                                                        txtFileName = txtFileName.substring(0,
+                                                                txtFileName.lastIndexOf(".") + 1) + origExtension;
+                                                        txtFileName = configUrl + File.separator + "Config"
+                                                                + File.separator + domain + txtFileName;
                                                         attributeValueType1.getContent().add(txtFileName);
-                                                    } else if ("PolicyName".equals(attributeAssignmentExpressionType.getAttributeId())) {
-                                                        JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
-                                                        AttributeValueType attributeValueType1 = attributeValueType.getValue();
+                                                    } else if ("PolicyName".equals(
+                                                            attributeAssignmentExpressionType.getAttributeId())) {
+                                                        JAXBElement<AttributeValueType> attributeValueType =
+                                                                (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType
+                                                                        .getExpression();
+                                                        AttributeValueType attributeValueType1 =
+                                                                attributeValueType.getValue();
                                                         List<Object> contents = attributeValueType1.getContent();
                                                         if (contents != null && !contents.isEmpty()) {
-                                                            String tmp = filename.getFileName()+"";
+                                                            String tmp = filename.getFileName() + "";
                                                             String newName = tmp.substring(0, tmp.lastIndexOf("."));
                                                             attributeValueType1.getContent().clear();
-                                                            attributeValueType1.getContent().add(domain + newName + "." + "xml");
+                                                            attributeValueType1.getContent()
+                                                                    .add(domain + newName + "." + "xml");
                                                         }
 
                                                     }
@@ -308,33 +341,47 @@ public class XACMLPolicyWriter {
                         if (objects != null && !objects.isEmpty()) {
                             for (Iterator ite1 = objects.iterator(); ite1.hasNext();) {
 
-                                RuleType  ruleType1 = (RuleType ) ite1.next();
-                                ObligationExpressionsType obligationExpressionsType = ruleType1.getObligationExpressions();
+                                RuleType ruleType1 = (RuleType) ite1.next();
+                                ObligationExpressionsType obligationExpressionsType =
+                                        ruleType1.getObligationExpressions();
                                 if (obligationExpressionsType != null) {
-                                    List<ObligationExpressionType> obligationExpressionType = obligationExpressionsType.getObligationExpression();
+                                    List<ObligationExpressionType> obligationExpressionType =
+                                            obligationExpressionsType.getObligationExpression();
                                     if (obligationExpressionType != null && !obligationExpressionType.isEmpty()) {
-                                        for (Iterator iterator = obligationExpressionType
-                                                .iterator(); iterator.hasNext();) {
-                                            ObligationExpressionType obligationExpressionTypes = (ObligationExpressionType) iterator
-                                                    .next();
-                                            if (obligationExpressionTypes.getObligationId() != null && !"".equals(obligationExpressionTypes.getObligationId())) {
-                                                List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = obligationExpressionTypes.getAttributeAssignmentExpression();
-                                                if (attributeAssignmentExpressionTypes != null && !attributeAssignmentExpressionTypes.isEmpty()) {
-                                                    for (Iterator iterator2 = attributeAssignmentExpressionTypes
-                                                            .iterator(); iterator2.hasNext();) {
-                                                        AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
-                                                                .next();
-                                                        if ("body".equals(attributeAssignmentExpressionType.getAttributeId())) {
-                                                            JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
-                                                            AttributeValueType attributeValueType1 = attributeValueType.getValue();
+                                        for (Iterator iterator = obligationExpressionType.iterator(); iterator
+                                                .hasNext();) {
+                                            ObligationExpressionType obligationExpressionTypes =
+                                                    (ObligationExpressionType) iterator.next();
+                                            if (obligationExpressionTypes.getObligationId() != null
+                                                    && !"".equals(obligationExpressionTypes.getObligationId())) {
+                                                List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes =
+                                                        obligationExpressionTypes.getAttributeAssignmentExpression();
+                                                if (attributeAssignmentExpressionTypes != null
+                                                        && !attributeAssignmentExpressionTypes.isEmpty()) {
+                                                    for (Iterator iterator2 =
+                                                            attributeAssignmentExpressionTypes.iterator(); iterator2
+                                                                    .hasNext();) {
+                                                        AttributeAssignmentExpressionType attributeAssignmentExpressionType =
+                                                                (AttributeAssignmentExpressionType) iterator2.next();
+                                                        if ("body".equals(
+                                                                attributeAssignmentExpressionType.getAttributeId())) {
+                                                            JAXBElement<AttributeValueType> attributeValueType =
+                                                                    (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType
+                                                                            .getExpression();
+                                                            AttributeValueType attributeValueType1 =
+                                                                    attributeValueType.getValue();
                                                             String configUrl = "$URL";
-                                                            String urlVal = (String) attributeValueType1.getContent().get(0);
-                                                            String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
+                                                            String urlVal =
+                                                                    (String) attributeValueType1.getContent().get(0);
+                                                            String origExtension = urlVal
+                                                                    .substring(urlVal.lastIndexOf('.') + 1).trim();
                                                             extension = "json";
                                                             attributeValueType1.getContent().clear();
                                                             String txtFileName = filename.getFileName().toString();
-                                                            txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
-                                                            txtFileName = configUrl+ File.separator + "Action" + File.separator + domain + txtFileName;
+                                                            txtFileName = txtFileName.substring(0,
+                                                                    txtFileName.lastIndexOf(".") + 1) + origExtension;
+                                                            txtFileName = configUrl + File.separator + "Action"
+                                                                    + File.separator + domain + txtFileName;
                                                             attributeValueType1.getContent().add(txtFileName);
                                                         }
 
@@ -352,7 +399,7 @@ public class XACMLPolicyWriter {
                 }
                 writePolicyFile(filename, policyType);
             }
-        }catch (JAXBException e) {
+        } catch (JAXBException e) {
             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
         }