fix typo that can lead on bugs
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / DecisionPolicy.java
index 47ab458..a254f7a 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017-2018 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.
 
 package org.onap.policy.pap.xacml.rest.components;
 
+import com.att.research.xacml.api.XACML3;
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.std.IdentifierImpl;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
@@ -36,9 +40,9 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
+import javax.script.SimpleBindings;
 import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.onap.policy.common.logging.eelf.MessageCodes;
 import org.onap.policy.common.logging.eelf.PolicyLogger;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
@@ -51,7 +55,6 @@ import org.onap.policy.controlloop.policy.guard.Guard;
 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
 import org.onap.policy.controlloop.policy.guard.MatchParameters;
 import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
-import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
 import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.DecisionSettings;
@@ -60,9 +63,8 @@ import org.onap.policy.utils.PolicyUtils;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 import org.onap.policy.xacml.std.pip.engines.aaf.AAFEngine;
 import org.onap.policy.xacml.util.XACMLPolicyScanner;
-import com.att.research.xacml.api.XACML3;
-import com.att.research.xacml.api.pap.PAPException;
-import com.att.research.xacml.std.IdentifierImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 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;
@@ -82,6 +84,8 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
 
+
+@Component
 public class DecisionPolicy extends Policy {
 
     private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicy.class);
@@ -90,9 +94,12 @@ public class DecisionPolicy extends Policy {
     private static final String AAFPROVIDER = "AAF";
     public static final String GUARD_YAML = "GUARD_YAML";
     public static final String GUARD_BL_YAML = "GUARD_BL_YAML";
+    public static final String GUARD_MIN_MAX = "GUARD_MIN_MAX";
     public static final String RAINY_DAY = "Rainy_Day";
+    public static final String MS_MODEL = "MicroService_Model";
     private static final String XACML_GUARD_TEMPLATE = "Decision_GuardPolicyTemplate.xml";
     private static final String XACML_BLGUARD_TEMPLATE = "Decision_GuardBLPolicyTemplate.xml";
+    private static final String XACML_GUARD_MIN_MAX_TEMPLATE = "Decision_GuardMinMaxPolicyTemplate.xml";
 
     private static final String ONAPNAME = "ONAPName";
     private static final String POLICY_NAME = "PolicyName";
@@ -105,13 +112,18 @@ public class DecisionPolicy extends Policy {
     List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
     List<String> dataTypeList = new LinkedList<>();
 
-    private CommonClassDao commonClassDao;
+    private static CommonClassDao commonClassDao;
 
     public DecisionPolicy() {
         super();
     }
 
-    public DecisionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao) {
+    @Autowired
+    public DecisionPolicy(CommonClassDao commonClassDao) {
+        DecisionPolicy.commonClassDao = commonClassDao;
+    }
+
+    public DecisionPolicy(PolicyRestAdapter policyAdapter) {
         this.policyAdapter = policyAdapter;
         this.commonClassDao = commonClassDao;
     }
@@ -194,8 +206,10 @@ public class DecisionPolicy extends Policy {
         }
         policyName = policyAdapter.getNewFileName();
 
-        if (policyAdapter.getRuleProvider().equals(GUARD_YAML)
-                || policyAdapter.getRuleProvider().equals(GUARD_BL_YAML)) {
+        if(policyAdapter.getRuleProvider().equals(GUARD_YAML) || 
+                policyAdapter.getRuleProvider().equals(GUARD_BL_YAML) || 
+                policyAdapter.getRuleProvider().equals(GUARD_MIN_MAX)){
+            
             Map<String, String> yamlParams = new HashMap<>();
             String blackListEntryType = policyAdapter.getBlackListEntryType() != null
                     ? policyAdapter.getBlackListEntryType() : "Use Manual Entry";
@@ -353,6 +367,12 @@ public class DecisionPolicy extends Policy {
                     }
                     cons.setBlacklist(blackList);
                     break;
+                case GUARD_MIN_MAX:
+                    templateFile = new File(classLoader.getResource(XACML_GUARD_MIN_MAX_TEMPLATE).getFile());
+                    xacmlTemplatePath = templateFile.toPath();
+                    cons = new Constraint(Integer.parseInt(yamlParams.get("min")), 
+                            Integer.parseInt(yamlParams.get("max")), activeTimeRange);
+                    break;
                 default:
                     templateFile = new File(classLoader.getResource(XACML_GUARD_TEMPLATE).getFile());
                     xacmlTemplatePath = templateFile.toPath();
@@ -372,6 +392,7 @@ public class DecisionPolicy extends Policy {
                     cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), timeWindow, activeTimeRange);
                     break;
             }
+            
             builder = builder.addLimitConstraint(policy1.getId(), cons);
             // Build the specification
             Results results = builder.buildSpecification();
@@ -399,6 +420,18 @@ public class DecisionPolicy extends Policy {
                     yamlSpecs.put("twUnits", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst()
                             .getTime_window().get("units"));
                 }
+                
+                if (yamlGuardObject.getGuards().getFirst().getLimit_constraints().
+                        getFirst().getMaxVnfCount() != null) {
+                    yamlSpecs.put("max", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst()
+                            .getMaxVnfCount().toString());
+                }
+                if (yamlGuardObject.getGuards().getFirst().getLimit_constraints().
+                        getFirst().getMinVnfCount() != null) {
+                    yamlSpecs.put("min", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst()
+                            .getMinVnfCount().toString());
+                }
+
                 yamlSpecs.put("guardActiveStart", yamlGuardObject.getGuards().getFirst().getLimit_constraints()
                         .getFirst().getActive_time_range().get("start"));
                 yamlSpecs.put("guardActiveEnd", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst()
@@ -406,9 +439,11 @@ public class DecisionPolicy extends Policy {
                 String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent, yamlSpecs,
                         yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getBlacklist(),
                         yamlGuardObject.getGuards().getFirst().getMatch_parameters().getTargets());
+                
                 // Convert the Policy into Stream input to Policy Adapter.
                 Object policy = XACMLPolicyScanner
                         .readPolicy(new ByteArrayInputStream(xacmlPolicyContent.getBytes(StandardCharsets.UTF_8)));
+                
                 return (PolicyType) policy;
             } catch (IOException e) {
                 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage(),
@@ -422,19 +457,7 @@ public class DecisionPolicy extends Policy {
     }
 
     private DecisionSettings findDecisionSettingsBySettingId(String settingId) {
-        DecisionSettings decisionSetting = null;
-
-        EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
-        Query getDecisionSettings = em.createNamedQuery("DecisionSettings.findAll");
-        List<?> decisionSettingsList = getDecisionSettings.getResultList();
-
-        for (Object id : decisionSettingsList) {
-            decisionSetting = (DecisionSettings) id;
-            if (decisionSetting.getXacmlId().equals(settingId)) {
-                break;
-            }
-        }
-        return decisionSetting;
+        return (DecisionSettings) commonClassDao.getEntityItem(DecisionSettings.class, "xacml_id", settingId);
     }
 
     private void createRule(PolicyType decisionPolicy, boolean permitRule) {
@@ -850,7 +873,7 @@ public class DecisionPolicy extends Policy {
 
     public String getFunctionDefinitionId(String key) {
         FunctionDefinition object =
-                (FunctionDefinition) commonClassDao.getDataById(FunctionDefinition.class, "shortname", key);
+                (FunctionDefinition) commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", key);
         if (object != null) {
             return object.getXacmlid();
         }