use hibernate and breakup dbdao and papservlet
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / policycontroller / PolicyCreation.java
index 951f25c..d8a7726 100644 (file)
@@ -2,14 +2,14 @@
  * ============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.
  * 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.
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.policy.pap.xacml.rest.policycontroller;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.io.File;
 import java.util.Date;
 import java.util.HashMap;
@@ -26,15 +28,12 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
 import org.apache.commons.lang.StringUtils;
 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.pap.xacml.rest.XACMLPapServlet;
 import org.onap.policy.pap.xacml.rest.components.ActionPolicy;
 import org.onap.policy.pap.xacml.rest.components.ClosedLoopPolicy;
 import org.onap.policy.pap.xacml.rest.components.ConfigPolicy;
@@ -69,8 +68,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestController;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-
 
 @RestController
 @RequestMapping("/")
@@ -402,46 +399,56 @@ 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){
+                    if (policyData.getRuleProvider() != null
+                            && (policyData.getRuleProvider().equals(DecisionPolicy.GUARD_YAML)
+                                    || policyData.getRuleProvider().equals(DecisionPolicy.GUARD_BL_YAML)
+                                    || policyData.getRuleProvider().equals(DecisionPolicy.GUARD_MIN_MAX))
+                            && policyData.getYamlparams() != null) {
                         attributeMap.put("actor", policyData.getYamlparams().getActor());
                         attributeMap.put("recipe", policyData.getYamlparams().getRecipe());
                         attributeMap.put("clname", policyData.getYamlparams().getClname());
                         attributeMap.put("limit", policyData.getYamlparams().getLimit());
+                        attributeMap.put("min", policyData.getYamlparams().getMin());
+                        attributeMap.put("max", policyData.getYamlparams().getMax());
                         attributeMap.put("timeWindow", policyData.getYamlparams().getTimeWindow());
                         attributeMap.put("timeUnits", policyData.getYamlparams().getTimeUnits());
                         attributeMap.put("guardActiveStart", policyData.getYamlparams().getGuardActiveStart());
                         attributeMap.put("guardActiveEnd", policyData.getYamlparams().getGuardActiveEnd());
-                        if(policyData.getYamlparams().getBlackList()!=null){
+                        if (policyData.getYamlparams().getBlackList() != null) {
                             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()){
+                        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()){
+                            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(),",");
+                        if (policyData.getYamlparams().getTargets() != null) {
+                            String targets = StringUtils.join(policyData.getYamlparams().getTargets(), ",");
                             attributeMap.put("targets", targets);
                         }
                     }
-                    if(policyData.getRuleProvider()!=null && policyData.getRuleProvider().equals(DecisionPolicy.RAINY_DAY)){
+                    if (policyData.getRuleProvider() != null
+                            && policyData.getRuleProvider().equals(DecisionPolicy.RAINY_DAY)) {
                         attributeMap.put("ServiceType", policyData.getRainyday().getServiceType());
                         attributeMap.put("VNFType", policyData.getRainyday().getVnfType());
                         attributeMap.put("BB_ID", policyData.getRainyday().getBbid());
                         attributeMap.put("WorkStep", policyData.getRainyday().getWorkstep());
 
-                        if(policyData.getRainyday().getTreatmentTableChoices()!=null && policyData.getRainyday().getTreatmentTableChoices().size() > 0){
-                            for (Object table : policyData.getRainyday().getTreatmentTableChoices()){
-                                if(table instanceof LinkedHashMap<?,?>){
-                                    String errorcode = ((LinkedHashMap<?,?>) table).get("errorcode").toString();
-                                    String treatment = ((LinkedHashMap<?,?>) table).get("treatment").toString();
+                        if (policyData.getRainyday().getTreatmentTableChoices() != null
+                                && policyData.getRainyday().getTreatmentTableChoices().isEmpty()) {
+                            for (Object table : policyData.getRainyday().getTreatmentTableChoices()) {
+                                if (table instanceof LinkedHashMap<?, ?>) {
+                                    String errorcode = ((LinkedHashMap<?, ?>) table).get("errorcode").toString();
+                                    String treatment = ((LinkedHashMap<?, ?>) table).get("treatment").toString();
                                     treatmentMap.put(errorcode, treatment);
                                 }
                             }
@@ -460,7 +467,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                     policyData.setErrorCodeList(errorCodeList);
                     policyData.setTreatmentList(treatmentList);
                 }
-                newPolicy = new DecisionPolicy(policyData, commonClassDao);
+                newPolicy = new DecisionPolicy(policyData);
             }
 
             if(newPolicy != null){
@@ -475,7 +482,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
 
             PolicyDBDaoTransaction policyDBDaoTransaction = null;
             try{
-                policyDBDao = PolicyDBDao.getPolicyDBDaoInstance(XACMLPapServlet.getEmf());
+                policyDBDao = PolicyDBDao.getPolicyDBDaoInstance();
                 policyDBDaoTransaction = policyDBDao.getNewTransaction();
                 policyDBDaoTransaction.createPolicy(newPolicy, policyData.getUserId());
                 successMap = newPolicy.savePolicies();
@@ -511,7 +518,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                     } else {
                         PolicyLogger.info("SafetyCheckerResponse was empty or null.");
                     }
-                    
+
                 }else if (successMap.containsKey("invalidAttribute")) {
                     String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Action Attribute";
                     LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not fine " + policyData.getActionAttribute() + " in the ActionPolicyDict table.");
@@ -541,7 +548,7 @@ public class PolicyCreation extends AbstractPolicyCreation{
                     response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                     response.addHeader("error", message);
                     response.addHeader("policyName", policyData.getPolicyName());
-                }else {                                                
+                }else {
                     policyDBDaoTransaction.rollbackTransaction();
                     body = "error";
                     status = HttpStatus.INTERNAL_SERVER_ERROR;