Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / policycontroller / PolicyCreation.java
index a987e9a..3437dab 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.lang.StringUtils;
 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
 import org.openecomp.policy.common.logging.flexlogger.Logger;
@@ -93,8 +94,8 @@ public class PolicyCreation extends AbstractPolicyCreation{
        public ResponseEntity<String> savePolicy(@RequestBody PolicyRestAdapter policyData, HttpServletResponse response) throws Exception{
                String body = null;
                HttpStatus status = HttpStatus.BAD_REQUEST;
-               Map<String, String> successMap = new HashMap<String, String>();
-               Map<String, String> attributeMap = new HashMap<String, String>();
+               Map<String, String> successMap = new HashMap<>();
+               Map<String, String> attributeMap = new HashMap<>();
                PolicyVersion policyVersionDao;
                try {
                
@@ -161,8 +162,8 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                highestVersion = policyVersion.getHigherVersion();
                        }
                        
-                       if(highestVersion != 0){
-                               if(policyData.isEditPolicy){
+                       if(highestVersion != 0 && policyVersion != null){
+                               if(policyData.isEditPolicy()){
                                        version = highestVersion +1;
                                        if(userId ==null){
                                                modifiedBy = "API";
@@ -239,7 +240,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                }else if (policyConfigType.equalsIgnoreCase("BRMS_Param")) {
                                        policyData.setEcompName("DROOLS");
                                        policyData.setConfigName("BRMS_PARAM_RULE");
-                                       Map<String, String> drlRuleAndUIParams = new HashMap<String, String>();
+                                       Map<String, String> drlRuleAndUIParams = new HashMap<>();
                                        if(policyData.getApiflag() == null){
                                                // If there is any dynamic field create the matches here
                                                String key="templateName";
@@ -288,10 +289,10 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                }
                        }else if(policyType.equalsIgnoreCase("Action")) {
                                if(policyData.getApiflag() == null){
-                                       List<String> dynamicRuleAlgorithmLabels = new LinkedList<String>();
-                                       List<String> dynamicRuleAlgorithmCombo = new LinkedList<String>();
-                                       List<String> dynamicRuleAlgorithmField1 = new LinkedList<String>();
-                                       List<String> dynamicRuleAlgorithmField2 = new LinkedList<String>();
+                                       List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
+                                       List<String> dynamicRuleAlgorithmCombo = new LinkedList<>();
+                                       List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
+                                       List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
 
 
                                        if(policyData.getRuleAlgorithmschoices().size() > 0){
@@ -332,13 +333,13 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                newPolicy = new ActionPolicy(policyData);
                        } else if (policyType.equalsIgnoreCase("Decision")) {
                                if(policyData.getApiflag() == null){
-                                       Map<String, String> settingsMap = new HashMap<String, String>();
-                                       List<String> dynamicRuleAlgorithmLabels = new LinkedList<String>();
-                                       List<String> dynamicRuleAlgorithmCombo = new LinkedList<String>();
-                                       List<String> dynamicRuleAlgorithmField1 = new LinkedList<String>();
-                                       List<String> dynamicRuleAlgorithmField2 = new LinkedList<String>();
-                                       List<Object> dynamicVariableList = new LinkedList<Object>();
-                                       List<String> dataTypeList = new LinkedList<String>();
+                                       Map<String, String> settingsMap = new HashMap<>();
+                                       List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
+                                       List<String> dynamicRuleAlgorithmCombo = new LinkedList<>();
+                                       List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
+                                       List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
+                                       List<Object> dynamicVariableList = new LinkedList<>();
+                                       List<String> dataTypeList = new LinkedList<>();
 
                                        if(policyData.getSettings().size() > 0){
                                                for(Object settingsData : policyData.getSettings()){
@@ -349,7 +350,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                                        }
                                                }
                                        }
-                                       if(policyData.getRuleAlgorithmschoices().size() > 0){
+                                       if(policyData.getRuleAlgorithmschoices()!=null && policyData.getRuleAlgorithmschoices().size() > 0){
                                                for(Object attribute : policyData.getRuleAlgorithmschoices()){
                                                        if(attribute instanceof LinkedHashMap<?, ?>){
                                                                String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
@@ -363,7 +364,18 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                                        }
                                                }
                                        }
-
+                                       if(policyData.getRuleProvider()!=null && (policyData.getRuleProvider().equals(DecisionPolicy.GUARD_YAML)|| policyData.getRuleProvider().equals(DecisionPolicy.GUARD_BL_YAML)) 
+                                                       && policyData.getYamlparams()!=null){   attributeMap.put("actor", policyData.getYamlparams().getActor());
+                                               attributeMap.put("recipe", policyData.getYamlparams().getRecipe());
+                                               attributeMap.put("limit", policyData.getYamlparams().getLimit());
+                                               attributeMap.put("timeWindow", policyData.getYamlparams().getTimeWindow());
+                                               attributeMap.put("guardActiveStart", policyData.getYamlparams().getGuardActiveStart());
+                                               attributeMap.put("guardActiveEnd", policyData.getYamlparams().getGuardActiveEnd());
+                                               if(policyData.getYamlparams().getBlackList()!=null){
+                                                       String blackList = StringUtils.join(policyData.getYamlparams().getBlackList(), ",");
+                                                       attributeMap.put("blackList", blackList);
+                                               }
+                                       }
                                        policyData.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
                                        policyData.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
                                        policyData.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
@@ -376,7 +388,15 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                newPolicy = new DecisionPolicy(policyData);
                        }
 
-                       newPolicy.prepareToSave();
+                       if(newPolicy != null){
+                               newPolicy.prepareToSave();
+                       }else{
+                               body = "error";
+                               status = HttpStatus.INTERNAL_SERVER_ERROR;
+                               response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);                                                               
+                               response.addHeader("error", "error");
+                               return new ResponseEntity<String>(body, status);
+                       }
                        
                        PolicyDBDaoTransaction policyDBDaoTransaction = null;
                        try{
@@ -386,7 +406,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                successMap = newPolicy.savePolicies();
                                if(successMap.containsKey("success")){
                                        policyDBDaoTransaction.commitTransaction();
-                                       if(policyData.isEditPolicy){
+                                       if(policyData.isEditPolicy()){
                                                commonClassDao.update(policyVersionDao);
                                        }else{
                                                commonClassDao.save(policyVersionDao);
@@ -454,12 +474,13 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                        response.addHeader("error", "error");                                                   
                                }
                        }catch(Exception e){
-                               policyDBDaoTransaction.rollbackTransaction();
+                               if(policyDBDaoTransaction != null){
+                                       policyDBDaoTransaction.rollbackTransaction();
+                               }
                        }
-
                }
                catch (Exception e){
-                       e.printStackTrace();
+                       LOGGER.error("Exception Occured"+e);
                }
                return new ResponseEntity<String>(body, status);
        }