Merge "Cleanup project's name in Sonar"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / ActionPolicy.java
index 7addf2f..1efe827 100644 (file)
@@ -31,9 +31,6 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
@@ -53,10 +50,12 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
 import org.onap.policy.pap.xacml.rest.util.JPAUtils;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
-import org.onap.policy.rest.jpa.ActionPolicyDict;
 import org.onap.policy.rest.jpa.Datatype;
 import org.onap.policy.rest.jpa.FunctionDefinition;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
+
+import com.att.research.xacml.api.pap.PAPException;
+
 import org.onap.policy.common.logging.eelf.MessageCodes;
 import org.onap.policy.common.logging.eelf.PolicyLogger;
 import org.onap.policy.common.logging.flexlogger.FlexLogger; 
@@ -94,13 +93,10 @@ public class ActionPolicy extends Policy {
        protected Map<String, String> dropDownMap = new HashMap<>();
        
        private static boolean isAttribute = false;
-       private static synchronized boolean getAttribute () {
+       private synchronized static boolean getAttribute () {
                return isAttribute;
 
        }
-       private static synchronized void setAttribute (boolean att) {
-               isAttribute = att;
-       }
        
        public ActionPolicy() {
                super();
@@ -111,7 +107,7 @@ public class ActionPolicy extends Policy {
        }
        
        @Override
-       public Map<String, String> savePolicies() throws Exception {
+       public Map<String, String> savePolicies() throws PAPException {
                
                Map<String, String> successMap = new HashMap<>();
                if(isPolicyExists()){
@@ -139,7 +135,7 @@ public class ActionPolicy extends Policy {
        //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 Exception{
+       public boolean prepareToSave() throws PAPException{
 
                if(isPreparedToSave()){
                        //we have already done this
@@ -165,9 +161,13 @@ public class ActionPolicy extends Policy {
                if (policyAdapter.getData() != null) {
                        // Action body is optional so checking value provided or not
                        String comboDictValue = policyAdapter.getActionAttribute();
-               String actionBody = getActionPolicyDict(comboDictValue).getBody();
+               String actionBody = policyAdapter.getActionBody();
+               isAttribute = false;
+
+               //if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the dictionary
                        if(!(actionBody==null || "".equals(actionBody))){       
                                saveActionBody(policyName, actionBody);
+                               isAttribute = true;
                        } else {
                                if(!getAttribute()){
                                        LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue + " in the ActionPolicyDict table.");
@@ -299,7 +299,7 @@ public class ActionPolicy extends Policy {
 
                AttributeValueType typeAttributeValue = new AttributeValueType();
                typeAttributeValue.setDataType(STRING_DATATYPE);
-               String actionDictType = getActionPolicyDict(comboDictValue).getType();
+               String actionDictType = policyAdapter.getActionDictType();
                typeAttributeValue.getContent().add(actionDictType);
 
                assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
@@ -312,7 +312,7 @@ public class ActionPolicy extends Policy {
 
                AttributeValueType actionURLAttributeValue = new AttributeValueType();
                actionURLAttributeValue.setDataType(URI_DATATYPE);
-               String actionDictUrl = getActionPolicyDict(comboDictValue).getUrl();
+               String actionDictUrl = policyAdapter.getActionDictUrl();
                actionURLAttributeValue.getContent().add(actionDictUrl);
 
                assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
@@ -325,14 +325,14 @@ public class ActionPolicy extends Policy {
 
                AttributeValueType methodAttributeValue = new AttributeValueType();
                methodAttributeValue.setDataType(STRING_DATATYPE);
-               String actionDictMethod = getActionPolicyDict(comboDictValue).getMethod();
+               String actionDictMethod = policyAdapter.getActionDictMethod();
                methodAttributeValue.getContent().add(actionDictMethod);
 
                assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
                obligation.getAttributeAssignmentExpression().add(assignmentMethod);
 
                // Add JSON_URL Assignment:
-               String actionBody = getActionPolicyDict(comboDictValue).getBody();
+               String actionBody = policyAdapter.getActionBody();              
                if (actionBody != null) {
                        AttributeAssignmentExpressionType assignmentJsonURL = new AttributeAssignmentExpressionType();
                        assignmentJsonURL.setAttributeId(BODY_ATTRIBUTEID);
@@ -346,8 +346,8 @@ public class ActionPolicy extends Policy {
                        obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
                }
 
-               if(getActionPolicyDict(comboDictValue).getHeader() != null){
-                       String headerVal = getActionPolicyDict(comboDictValue).getHeader();
+               String headerVal = policyAdapter.getActionDictHeader();
+               if(headerVal != null){
                        if(headerVal != null && !headerVal.equals("")){
                                // parse it on : to get number of headers
                                String[] result = headerVal.split(":");
@@ -507,24 +507,6 @@ public class ActionPolicy extends Policy {
                }
                return dropDownMap;
        }
-       
-       private ActionPolicyDict getActionPolicyDict(String attributeName){
-               ActionPolicyDict retObj = new ActionPolicyDict();
-               EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
-               Query getActionPolicyDicts = em.createNamedQuery("ActionPolicyDict.findAll");   
-               List<?> actionPolicyDicts = getActionPolicyDicts.getResultList();       
-               
-               for (Object id : actionPolicyDicts) {
-                       ActionPolicyDict actionPolicy = (ActionPolicyDict) id;
-                       if(attributeName.equals(actionPolicy.getAttributeName())){
-                               setAttribute(true);
-                               retObj = actionPolicy;
-                               break;
-                       }
-               }
-               em.close();
-               return retObj;
-       }
 
        @Override
        public Object getCorrectPolicyDataObject() {