Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / ClosedLoopPolicy.java
index e446c2a..233add1 100644 (file)
@@ -2,14 +2,14 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,6 +20,9 @@
 
 package org.onap.policy.pap.xacml.rest.components;
 
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.std.IdentifierImpl;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -32,16 +35,6 @@ import java.nio.file.Paths;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.commons.io.FilenameUtils;
-import org.onap.policy.common.logging.eelf.MessageCodes;
-import org.onap.policy.common.logging.eelf.PolicyLogger;
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
-import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.rest.adapter.PolicyRestAdapter;
-
-import com.att.research.xacml.api.pap.PAPException;
-import com.att.research.xacml.std.IdentifierImpl;
-
 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;
@@ -54,7 +47,14 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
 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 oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
+
+import org.apache.commons.io.FilenameUtils;
+import org.onap.policy.common.logging.eelf.MessageCodes;
+import org.onap.policy.common.logging.eelf.PolicyLogger;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.rest.adapter.PolicyRestAdapter;
 
 public class ClosedLoopPolicy extends Policy {
 
@@ -64,28 +64,28 @@ public class ClosedLoopPolicy extends Policy {
         super();
     }
 
-    public ClosedLoopPolicy(PolicyRestAdapter policyAdapter){
+    public ClosedLoopPolicy(PolicyRestAdapter policyAdapter) {
         this.policyAdapter = policyAdapter;
     }
 
-    //save configuration of the policy based on the policyname
+    // save configuration of the policy based on the policyname
     private void saveConfigurations(String policyName, String jsonBody) {
 
-            if(policyName.endsWith(".xml")){
-                policyName = policyName.replace(".xml", "");
-            }
-        try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator+ policyName +".json")){
+        if (policyName.endsWith(".xml")) {
+            policyName = policyName.replace(".xml", "");
+        }
+        try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".json")) {
             String body = jsonBody;
-            //Remove the trapMaxAge in Verification Signature
+            // Remove the trapMaxAge in Verification Signature
             body = body.replace(",\"trapMaxAge\":null", "");
             this.policyAdapter.setJsonBody(body);
             out.println(body);
         } catch (Exception e) {
-            LOGGER.error("Exception Occured while writing Configuration Data"+e);
+            LOGGER.error("Exception Occured while writing Configuration Data" + e);
         }
     }
 
-    //Utility to read json data from the existing file to a string
+    // Utility to read json data from the existing file to a string
     static String readFile(String path, Charset encoding) throws IOException {
 
         byte[] encoded = Files.readAllBytes(Paths.get(path));
@@ -93,7 +93,7 @@ public class ClosedLoopPolicy extends Policy {
 
     }
 
-    //create the configuration file based on the policy name on adding the extension as .json
+    // create the configuration file based on the policy name on adding the extension as .json
     private String getConfigFile(String filename) {
         filename = FilenameUtils.removeExtension(filename);
         if (filename.endsWith(".xml")) {
@@ -107,12 +107,12 @@ public class ClosedLoopPolicy extends Policy {
     public Map<String, String> savePolicies() throws PAPException {
 
         Map<String, String> successMap = new HashMap<>();
-        if(isPolicyExists()){
+        if (isPolicyExists()) {
             successMap.put("EXISTS", "This Policy already exist on the PAP");
             return successMap;
         }
 
-        if(!isPreparedToSave()){
+        if (!isPreparedToSave()) {
             prepareToSave();
         }
 
@@ -120,17 +120,17 @@ public class ClosedLoopPolicy extends Policy {
         Path newPolicyPath = null;
         newPolicyPath = Paths.get(policyAdapter.getNewFileName());
 
-        successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject());
+        successMap = createPolicy(newPolicyPath, getCorrectPolicyDataObject());
         return successMap;
     }
 
-    //This is the method for preparing the policy for saving.  We have broken it out
-    //separately because the fully configured policy is used for multiple things
+    // This is the method for preparing the policy for saving. We have broken it out
+    // separately because the fully configured policy is used for multiple things
     @Override
-    public boolean prepareToSave() throws PAPException{
+    public boolean prepareToSave() throws PAPException {
 
-        if(isPreparedToSave()){
-            //we have already done this
+        if (isPreparedToSave()) {
+            // we have already done this
             return true;
         }
 
@@ -166,9 +166,9 @@ public class ClosedLoopPolicy extends Policy {
 
             AllOfType allOfOne = new AllOfType();
             String fileName = policyAdapter.getNewFileName();
-            String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
+            String name = fileName.substring(fileName.lastIndexOf("\\") + 1);
             if ((name == null) || (name.equals(""))) {
-                name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
+                name = fileName.substring(fileName.lastIndexOf("/") + 1);
             }
             allOfOne.getMatch().add(createMatch("PolicyName", name));
             AllOfType allOf = new AllOfType();
@@ -176,17 +176,13 @@ public class ClosedLoopPolicy extends Policy {
             // Match for Onap
             allOf.getMatch().add(createMatch("ONAPName", policyAdapter.getOnapName()));
             // Match for riskType
-            allOf.getMatch().add(
-                    createDynamicMatch("RiskType", policyAdapter.getRiskType()));
+            allOf.getMatch().add(createDynamicMatch("RiskType", policyAdapter.getRiskType()));
             // Match for riskLevel
-            allOf.getMatch().add(
-                    createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
+            allOf.getMatch().add(createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
             // Match for riskguard
-            allOf.getMatch().add(
-                    createDynamicMatch("guard", policyAdapter.getGuard()));
+            allOf.getMatch().add(createDynamicMatch("guard", policyAdapter.getGuard()));
             // Match for ttlDate
-            allOf.getMatch().add(
-                    createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
+            allOf.getMatch().add(createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
 
             AnyOfType anyOf = new AnyOfType();
             anyOf.getAllOf().add(allOfOne);
@@ -215,7 +211,8 @@ public class ClosedLoopPolicy extends Policy {
             try {
                 accessURI = new URI(ACTION_ID);
             } catch (URISyntaxException e) {
-                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "CreateClosedLoopPolicy", "Exception creating ACCESS URI");
+                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "CreateClosedLoopPolicy",
+                        "Exception creating ACCESS URI");
             }
             accessAttributeDesignator.setCategory(CATEGORY_ACTION);
             accessAttributeDesignator.setDataType(STRING_DATATYPE);
@@ -234,7 +231,8 @@ public class ClosedLoopPolicy extends Policy {
             try {
                 closedURI = new URI(RESOURCE_ID);
             } catch (URISyntaxException e) {
-                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "CreateClosedLoopPolicy", "Exception creating closed URI");
+                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "CreateClosedLoopPolicy",
+                        "Exception creating closed URI");
             }
             closedAttributeDesignator.setCategory(CATEGORY_RESOURCE);
             closedAttributeDesignator.setDataType(STRING_DATATYPE);
@@ -291,7 +289,7 @@ public class ClosedLoopPolicy extends Policy {
 
         AttributeValueType AttributeValue = new AttributeValueType();
         AttributeValue.setDataType(URI_DATATYPE);
-        String content = CONFIG_URL +"/Config/" + getConfigFile(policyName);
+        String content = CONFIG_URL + "/Config/" + getConfigFile(policyName);
         System.out.println("URL value :" + content);
         AttributeValue.getContent().add(content);
         assignment2.setExpression(new ObjectFactory().createAttributeValue(AttributeValue));
@@ -306,9 +304,9 @@ public class ClosedLoopPolicy extends Policy {
         attributeValue3.setDataType(STRING_DATATYPE);
         fileName = FilenameUtils.removeExtension(fileName);
         fileName = fileName + ".xml";
-        String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
+        String name = fileName.substring(fileName.lastIndexOf("\\") + 1);
         if ((name == null) || (name.equals(""))) {
-            name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
+            name = fileName.substring(fileName.lastIndexOf("/") + 1);
         }
         attributeValue3.getContent().add(name);
         assignment3.setExpression(new ObjectFactory().createAttributeValue(attributeValue3));
@@ -338,7 +336,7 @@ public class ClosedLoopPolicy extends Policy {
 
         advice.getAttributeAssignmentExpression().add(assignment5);
 
-        //Risk Attributes
+        // Risk Attributes
         AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
         assignment6.setAttributeId("RiskType");
         assignment6.setCategory(CATEGORY_RESOURCE);
@@ -387,8 +385,6 @@ public class ClosedLoopPolicy extends Policy {
 
         advice.getAttributeAssignmentExpression().add(assignment9);
 
-
-
         advices.getAdviceExpression().add(advice);
         return advices;
     }
@@ -398,5 +394,4 @@ public class ClosedLoopPolicy extends Policy {
         return policyAdapter.getPolicyData();
     }
 
-
 }