Decision BlackList Guard Enhancements
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / policycontroller / PolicyCreation.java
index 64324d4..a6cda5e 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -20,6 +20,7 @@
 package org.onap.policy.pap.xacml.rest.policycontroller;
 
 import java.io.File;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
@@ -43,6 +44,7 @@ import org.onap.policy.pap.xacml.rest.components.CreateClosedLoopPerformanceMetr
 import org.onap.policy.pap.xacml.rest.components.DecisionPolicy;
 import org.onap.policy.pap.xacml.rest.components.FirewallConfigPolicy;
 import org.onap.policy.pap.xacml.rest.components.MicroServiceConfigPolicy;
+import org.onap.policy.pap.xacml.rest.components.OptimizationConfigPolicy;
 import org.onap.policy.pap.xacml.rest.components.Policy;
 import org.onap.policy.pap.xacml.rest.components.PolicyDBDao;
 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTransaction;
@@ -122,33 +124,35 @@ public class PolicyCreation extends AbstractPolicyCreation{
                        String policyType = policyData.getPolicyType();
 
                        String filePrefix = null;
-                       if (policyType.equalsIgnoreCase("Config")) {
+                       if ("Config".equalsIgnoreCase(policyType)) {
                                policyConfigType = policyData.getConfigPolicyType();
-                               if (policyConfigType.equalsIgnoreCase("Firewall Config")) {
+                               if ("Firewall Config".equalsIgnoreCase(policyConfigType)) {
                                        filePrefix = "Config_FW_";
-                               }else if (policyConfigType.equalsIgnoreCase("ClosedLoop_Fault")) {
+                               }else if ("ClosedLoop_Fault".equalsIgnoreCase(policyConfigType)) {
                                        filePrefix = "Config_Fault_";
-                               }else if (policyConfigType.equalsIgnoreCase("ClosedLoop_PM")) {
+                               }else if ("ClosedLoop_PM".equalsIgnoreCase(policyConfigType)) {
                                        filePrefix = "Config_PM_";
-                               }else if (policyConfigType.equalsIgnoreCase("Micro Service")) {
+                               }else if ("Micro Service".equalsIgnoreCase(policyConfigType)) {
                                        filePrefix = "Config_MS_";
-                               }else if (policyConfigType.equalsIgnoreCase("BRMS_Raw")) {
+                               }else if ("Optimization".equalsIgnoreCase(policyConfigType)) {
+                                       filePrefix = "Config_OOF_";
+                               }else if ("BRMS_Raw".equalsIgnoreCase(policyConfigType)) {
                                        filePrefix = "Config_BRMS_Raw_";
-                               }else if (policyConfigType.equalsIgnoreCase("BRMS_Param")) {
+                               }else if ("BRMS_Param".equalsIgnoreCase(policyConfigType)) {
                                        filePrefix = "Config_BRMS_Param_";
                                }else {
                                        filePrefix = "Config_"; 
                                }
-                       } else if (policyType.equalsIgnoreCase("Action")) {
+                       } else if ("Action".equalsIgnoreCase(policyType)) {
                                filePrefix = "Action_";
-                       } else if (policyType.equalsIgnoreCase("Decision")) {
+                       } else if ("Decision".equalsIgnoreCase(policyType)) {
                                filePrefix = "Decision_";
                        }
 
                        int version = 0;
                        int highestVersion = 0;
-                       String createdBy = "";
-                       String modifiedBy = userId;
+                       String createdBy;
+                       String modifiedBy;
                        String scopeCheck = policyData.getDomainDir().replace(".", File.separator);
                        PolicyEditorScopes policyEditorScope = (PolicyEditorScopes) commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", scopeCheck);
                        if(policyEditorScope == null){
@@ -184,15 +188,25 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                        policyVersionDao.setActiveVersion(version);
                                        policyVersionDao.setHigherVersion(version);
                                        policyVersionDao.setModifiedBy(modifiedBy);
+                                       policyVersionDao.setModifiedDate(new Date());
                                }else{
                                        body = "policyExists";
                                        status = HttpStatus.CONFLICT;
                                        response.setStatus(HttpServletResponse.SC_CONFLICT);
                                        response.addHeader("error", "policyExists");
                                        response.addHeader("policyName", policyData.getPolicyName());
-                                       return new ResponseEntity<String>(body, status);
+                                       return new ResponseEntity<>(body, status);
                                }               
                        }else{
+                               // if policy does not exist and the request is updatePolicy return error
+                               if(policyData.isEditPolicy()){
+                                       body = "policyNotAvailableForEdit";
+                                       status = HttpStatus.NOT_FOUND;
+                                       response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                                       response.addHeader("error", body);
+                                       response.addHeader("message", policyData.getPolicyName() + " does not exist on the PAP and cannot be updated.");
+                                       return new ResponseEntity<>(body, status);
+                               }
                                version = 1;
                                if(userId == null){
                                        createdBy = "API";
@@ -220,14 +234,12 @@ public class PolicyCreation extends AbstractPolicyCreation{
                        policyData.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides");
                        if(policyData.getApiflag() == null){
                                //set the Rule Combining Algorithm Id to be sent to PAP-REST via JSON   
-                               if(policyData.getAttributes() != null){
-                                       if(policyData.getAttributes().size() > 0){
-                                               for(Object attribute : policyData.getAttributes()){
-                                                       if(attribute instanceof LinkedHashMap<?, ?>){
-                                                               String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
-                                                               String value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
-                                                               attributeMap.put(key, value);   
-                                                       }
+                               if(policyData.getAttributes() != null && !policyData.getAttributes().isEmpty()){
+                                       for(Object attribute : policyData.getAttributes()){
+                                               if(attribute instanceof LinkedHashMap<?, ?>){
+                                                       String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
+                                                       String value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
+                                                       attributeMap.put(key, value);   
                                                }
                                        }
                                }
@@ -238,21 +250,21 @@ public class PolicyCreation extends AbstractPolicyCreation{
                        policyData.setHighestVersion(version);
 
                        // Calling Component class per policy type
-                       if (policyType.equalsIgnoreCase("Config")) {
-                               if (policyConfigType.equalsIgnoreCase("Firewall Config")) {
+                       if ("Config".equalsIgnoreCase(policyType)) {
+                               if ("Firewall Config".equalsIgnoreCase(policyConfigType)) {
                                        newPolicy = new FirewallConfigPolicy(policyData);
-                               }else if (policyConfigType.equalsIgnoreCase("BRMS_Raw")) { 
+                               }else if ("BRMS_Raw".equalsIgnoreCase(policyConfigType)) { 
                                        policyData.setOnapName("DROOLS");
                                        policyData.setConfigName("BRMS_RAW_RULE");
                                        newPolicy = new CreateBrmsRawPolicy(policyData);
-                               }else if (policyConfigType.equalsIgnoreCase("BRMS_Param")) {
+                               }else if ("BRMS_Param".equalsIgnoreCase(policyConfigType)) {
                                        policyData.setOnapName("DROOLS");
                                        policyData.setConfigName("BRMS_PARAM_RULE");
                                        Map<String, String> drlRuleAndUIParams = new HashMap<>();
                                        if(policyData.getApiflag() == null){
                                                // If there is any dynamic field create the matches here
                                                String key="templateName";
-                                               String value=(String) policyData.getRuleName();
+                                               String value= policyData.getRuleName();
                                                drlRuleAndUIParams.put(key, value);
                                                if(policyData.getRuleData().size() > 0){
                                                        for(Object keyValue: policyData.getRuleData().keySet()){
@@ -278,11 +290,11 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                }
                                        }               
                                        newPolicy = new CreateBrmsParamPolicy(policyData);
-                               }else if (policyConfigType.equalsIgnoreCase("Base")) {
+                               }else if ("Base".equalsIgnoreCase(policyConfigType)) {
                                        newPolicy =  new ConfigPolicy(policyData);
-                               }else if (policyConfigType.equalsIgnoreCase("ClosedLoop_Fault")) {
+                               }else if ("ClosedLoop_Fault".equalsIgnoreCase(policyConfigType)) {
                                        newPolicy = new ClosedLoopPolicy(policyData);
-                               }else if (policyConfigType.equalsIgnoreCase("ClosedLoop_PM")) {
+                               }else if ("ClosedLoop_PM".equalsIgnoreCase(policyConfigType)) {
                                        if(policyData.getApiflag() == null){
                                                policyData.setServiceType(policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString());
                                                ObjectMapper jsonMapper = new ObjectMapper();
@@ -292,10 +304,12 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                                policyData.setJsonBody(jsonBody);
                                        }
                                        newPolicy = new CreateClosedLoopPerformanceMetrics(policyData);
-                               }else if (policyConfigType.equalsIgnoreCase("Micro Service")) {
+                               }else if ("Micro Service".equalsIgnoreCase(policyConfigType)) {
                                        newPolicy = new MicroServiceConfigPolicy(policyData);
+                               }else if ("Optimization".equalsIgnoreCase(policyConfigType)) {
+                                       newPolicy = new OptimizationConfigPolicy(policyData);
                                }
-                       }else if(policyType.equalsIgnoreCase("Action")) {
+                       }else if("Action".equalsIgnoreCase(policyType)) {
                                if(policyData.getApiflag() == null){
                                        List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
                                        List<String> dynamicRuleAlgorithmCombo = new LinkedList<>();
@@ -303,7 +317,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                        List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
 
 
-                                       if(policyData.getRuleAlgorithmschoices().size() > 0){
+                                       if(!policyData.getRuleAlgorithmschoices().isEmpty()){
                                                for(Object attribute : policyData.getRuleAlgorithmschoices()){
                                                        if(attribute instanceof LinkedHashMap<?, ?>){
                                                                String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
@@ -320,26 +334,39 @@ public class PolicyCreation extends AbstractPolicyCreation{
 
                                        String actionDictValue = policyData.getActionAttributeValue();
                                        ActionPolicyDict jsonData = ((ActionPolicyDict) commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", actionDictValue));
-                                       String actionBodyString = jsonData.getBody();
-                                       String actionDictHeader = jsonData.getHeader();
-                                       String actionDictType = jsonData.getType();
-                                       String actionDictUrl = jsonData.getUrl();
-                                       String actionDictMethod = jsonData.getMethod();
-                                       policyData.setActionDictHeader(actionDictHeader);
-                                       policyData.setActionDictType(actionDictType);
-                                       policyData.setActionDictUrl(actionDictUrl);
-                                       policyData.setActionDictMethod(actionDictMethod);
+                                       if(jsonData!=null){
+                                               String actionBodyString = jsonData.getBody();
+                                               String actionDictHeader = jsonData.getHeader();
+                                               String actionDictType = jsonData.getType();
+                                               String actionDictUrl = jsonData.getUrl();
+                                               String actionDictMethod = jsonData.getMethod();
+                                               policyData.setActionDictHeader(actionDictHeader);
+                                               policyData.setActionDictType(actionDictType);
+                                               policyData.setActionDictUrl(actionDictUrl);
+                                               policyData.setActionDictMethod(actionDictMethod);
+                                               if (actionBodyString != null) {
+                                                       policyData.setActionBody(actionBodyString);
+                                               }
+                                       }
                                        policyData.setActionAttribute(actionDictValue);
                                        policyData.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
                                        policyData.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
                                        policyData.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
                                        policyData.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
-                                       if (actionBodyString != null) {
-                                               policyData.setActionBody(actionBodyString);
+                               }else{
+                                       // API request. 
+                                       String comboDictValue = policyData.getActionAttribute();
+                                       ActionPolicyDict jsonData = ((ActionPolicyDict) commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", comboDictValue));
+                                       if(jsonData!=null){
+                                               policyData.setActionBody(jsonData.getBody());
+                                               policyData.setActionDictHeader(jsonData.getHeader());
+                                               policyData.setActionDictType(jsonData.getType());
+                                               policyData.setActionDictUrl(jsonData.getUrl());
+                                               policyData.setActionDictMethod(jsonData.getMethod());
                                        }
                                }
-                               newPolicy = new ActionPolicy(policyData);
-                       } else if (policyType.equalsIgnoreCase("Decision")) {
+                               newPolicy = new ActionPolicy(policyData, commonClassDao);
+                       } else if ("Decision".equalsIgnoreCase(policyType)) {
                                if(policyData.getApiflag() == null){
                                        Map<String, String> settingsMap = new HashMap<>();
                                        Map<String, String> treatmentMap = new HashMap<>();
@@ -352,7 +379,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                        List<String> errorCodeList = new LinkedList<>();
                                        List<String> treatmentList = new LinkedList<>();
 
-                                       if(policyData.getSettings().size() > 0){
+                                       if(!policyData.getSettings().isEmpty()){
                                                for(Object settingsData : policyData.getSettings()){
                                                        if(settingsData instanceof LinkedHashMap<?, ?>){
                                                                String key = ((LinkedHashMap<?, ?>) settingsData).get("key").toString();
@@ -389,6 +416,16 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                                        String blackList = StringUtils.join(policyData.getYamlparams().getBlackList(), ",");
                                                        attributeMap.put("blackList", blackList);
                                                }
+                                               if(DecisionPolicy.GUARD_BL_YAML.equals(policyData.getRuleProvider()) && "Use File Upload".equals(policyData.getBlackListEntryType())){
+                                                       if(policyData.getBlackListEntries() != null && !policyData.getBlackListEntries().isEmpty()){
+                                                               String blackList = StringUtils.join(policyData.getBlackListEntries(), ",");
+                                                               attributeMap.put("blackList", blackList);
+                                                       }
+                                                       if(policyData.getAppendBlackListEntries() != null && !policyData.getAppendBlackListEntries().isEmpty()){
+                                                               String blackList = StringUtils.join(policyData.getAppendBlackListEntries(), ",");
+                                                               attributeMap.put("appendBlackList", blackList);
+                                                       }
+                                               }
                                                if(policyData.getYamlparams().getTargets()!=null){
                                                        String targets = StringUtils.join(policyData.getYamlparams().getTargets(),",");
                                                        attributeMap.put("targets", targets);
@@ -423,7 +460,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                        policyData.setErrorCodeList(errorCodeList);
                                        policyData.setTreatmentList(treatmentList);
                                }
-                               newPolicy = new DecisionPolicy(policyData);
+                               newPolicy = new DecisionPolicy(policyData, commonClassDao);
                        }
 
                        if(newPolicy != null){
@@ -433,7 +470,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                                status = HttpStatus.INTERNAL_SERVER_ERROR;
                                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);                                                               
                                response.addHeader("error", "error");
-                               return new ResponseEntity<String>(body, status);
+                               return new ResponseEntity<>(body, status);
                        }
                        
                        PolicyDBDaoTransaction policyDBDaoTransaction = null;