Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / PolicyDBDao.java
index eaceb65..b65557e 100644 (file)
@@ -35,7 +35,6 @@ import java.net.MalformedURLException;
 import java.net.ProtocolException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
-import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
@@ -69,8 +68,6 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathFactory;
 
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
@@ -105,6 +102,8 @@ import com.att.research.xacml.api.pap.PDP;
 import com.att.research.xacml.api.pap.PDPPolicy;
 import com.att.research.xacml.util.XACMLProperties;
 
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+
 public class PolicyDBDao {
        private static final Logger logger      = FlexLogger.getLogger(PolicyDBDao.class);              
        private List<?> otherServers;
@@ -213,7 +212,7 @@ public class PolicyDBDao {
         */
        private List<?> getRemotePolicyDBDaoList(){
                logger.debug("getRemotePolicyDBDaoList() as getRemotePolicyDBDaoList() called");
-               List<?> policyDBDaoEntityList = new LinkedList<Object>();
+               List<?> policyDBDaoEntityList = new LinkedList<>();
                EntityManager em = emf.createEntityManager();
                startTransactionSynced(em, 1000);
                try{                                            
@@ -375,7 +374,6 @@ public class PolicyDBDao {
                                        em.close();                                     
                                } catch(Exception e2){
                                        PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "COULD NOT CREATE DATABASELOCK ROW.  WILL TRY ONE MORE TIME");
-                                       e2.printStackTrace();
                                }
                                em = null;
                                em = emf.createEntityManager();
@@ -446,7 +444,7 @@ public class PolicyDBDao {
        }
        public void notifyOthers(long entityId, String entityType, String newGroupId){
                logger.debug("notifyOthers(long entityId, String entityType, long newGroupId) as notifyOthers("+entityId+","+entityType+","+newGroupId+") called");             
-               LinkedList<Thread> notifyThreads = new LinkedList<Thread>();
+               LinkedList<Thread> notifyThreads = new LinkedList<>();
 
                //we're going to run notiftions in parellel threads to speed things up
                for(Object obj : otherServers){
@@ -855,7 +853,7 @@ public class PolicyDBDao {
                //this must always be true since we don't explicitly know when a delete is occuring
                boolean didUpdate = true;
                HashMap<String,PDPPolicy> currentPolicySet = new HashMap<String,PDPPolicy>(oldPdpGroup.getPolicies().size());
-               HashSet<PDPPolicy> newPolicySet = new HashSet<PDPPolicy>();
+               HashSet<PDPPolicy> newPolicySet = new HashSet<>();
                for(PDPPolicy pdpPolicy : oldPdpGroup.getPolicies()){
                        currentPolicySet.put(pdpPolicy.getId(), pdpPolicy);
                }
@@ -872,6 +870,7 @@ public class PolicyDBDao {
                                        try {
                                                policyStream.close();
                                        } catch (IOException e) {
+                                               didUpdate = false;
                                                PolicyLogger.error(e.getMessage());
                                        }
                                }
@@ -1032,7 +1031,7 @@ public class PolicyDBDao {
                String action = "unknown action";
                try {
 
-                       if(policy.isDeleted()){
+                       if(policy != null && policy.isDeleted()){
                                logger.debug("Deleting Policy: " + policy.getPolicyName());
                                action = "delete";
                                Path newPath = Paths.get(policyPath.toString(), policy.getPolicyName());
@@ -1099,7 +1098,7 @@ public class PolicyDBDao {
 
                        }
                } catch (IOException e1) {
-                       PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Error occurred while performing [" + action + "] of Policy File: " +  policy.getPolicyName());
+                       PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Error occurred while performing [" + action + "] of Policy File: " + policy != null ? policy.getPolicyName() : "null");
                }       
        }
 
@@ -1127,7 +1126,7 @@ public class PolicyDBDao {
                        Properties propertyFileProperties = new Properties();
                        String groupList = "";
                        String defaultGroup = "";
-                       HashSet<String> currentGroupPaths = new HashSet<String>();
+                       HashSet<String> currentGroupPaths = new HashSet<>();
                        for(Object o : groups){
                                GroupEntity group = (GroupEntity)o;
                                Path groupPath = Paths.get(pdpsPath.toString(), group.getGroupId());
@@ -1316,11 +1315,12 @@ public class PolicyDBDao {
                String repo = buildPolicyDirectory();
 
                String policyScope = policy.getScope();
+               
                if(policyScope == null){
                        policyScope = "";
                        PolicyLogger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
                } else {
-                       policyScope = policyScope.replace(".", FileSystems.getDefault().getSeparator());        
+                       policyScope = policyScope.replace(".", File.separator); 
                }
                if(policyScope == null){
                        policyScope = "";
@@ -1330,7 +1330,7 @@ public class PolicyDBDao {
                        PolicyLogger.error("buildPolicyScopeDirectory("+policy+") received null repo. Using blank.");
                        repo = "";
                }
-               Path returnPath = Paths.get(repo + FileSystems.getDefault().getSeparator() + policyScope);
+               Path returnPath = Paths.get(repo + File.separator + policyScope);
                if(returnPath !=  null){
                        return returnPath.toString();
                } else {
@@ -1342,9 +1342,7 @@ public class PolicyDBDao {
        }
        private String buildPolicyScopeDirectory(String policyScope){
                String repo = buildPolicyDirectory();           
-               policyScope = policyScope.replace(".", FileSystems.getDefault().getSeparator());
-               return repo + FileSystems.getDefault().getSeparator() + policyScope;
-
+               return repo + File.separator + policyScope.replace(".", File.separator);
        }
 
        private static String buildPolicyDirectory(){
@@ -1629,7 +1627,13 @@ public class PolicyDBDao {
                } catch(Exception e){
                        PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "auditLocalDatabase() error");
                        logger.error("Exception Occured"+e);
-               }               
+               }finally{
+                       try {
+                               Files.walk(webappsPath).close();
+                       } catch (IOException e) {
+                               logger.error("Exception Occured while closing File Stream"+e);
+                       }
+               }
        }
 
        /**
@@ -1663,7 +1667,7 @@ public class PolicyDBDao {
                        getPolicyEntitiesQuery.setParameter("deleted", false);
                        policyEntityList = getPolicyEntitiesQuery.getResultList();
                } catch(Exception e){
-                       policyEntityList = new LinkedList<PolicyEntity>();
+                       policyEntityList = new LinkedList<>();
                }
 
                for (PolicyEntity policy: policyEntityList){
@@ -1971,7 +1975,7 @@ public class PolicyDBDao {
                }
                String gitPath  = getGitPath();
 
-               ArrayList<String> gitPathParts = new ArrayList<String>();
+               ArrayList<String> gitPathParts = new ArrayList<>();
                Iterator<?> gitPathIterator = Paths.get(gitPath).iterator();
                while(gitPathIterator.hasNext()){
                        gitPathParts.add(gitPathIterator.next().toString());
@@ -1981,7 +1985,7 @@ public class PolicyDBDao {
                        for(int j=i;j<gitPathParts.size();j++){
                                testGitPath = Paths.get(testGitPath.toString(),gitPathParts.get(j));
                        }
-                       if(path.contains(testGitPath.toString())){
+                       if(path != null && path.contains(testGitPath.toString())){
                                gitPath = testGitPath.toString();
                                break;
                        }
@@ -1990,7 +1994,7 @@ public class PolicyDBDao {
                        logger.debug("gitPath is null.  Returning");
                        return null;
                }
-               if(gitPath.length() >= path.length()){
+               if(path != null && (gitPath.length() >= path.length())){
                        logger.debug("gitPath length(): " + gitPath.length() + ">= path.length(): " + path.length() + ".  Returning null");
                        return null;
                }
@@ -2528,7 +2532,7 @@ public class PolicyDBDao {
                                                getGroups.setParameter("pid", existingPolicy.getPolicyId());
                                                groups = getGroups.getResultList();
                                        }catch(Exception e){
-                                               groups = new LinkedList<GroupEntity>();
+                                               groups = new LinkedList<>();
                                        }
                                        for(Object o : groups){
                                                GroupEntity group = (GroupEntity)o;
@@ -2997,6 +3001,13 @@ public class PolicyDBDao {
                                        }
                                }
                        }
+                       if(policyXmlStream != null){
+                               try {
+                                       policyXmlStream.close();
+                               } catch (IOException e) {
+                                       logger.error("Exception Occured while closing input stream"+e);
+                               }
+                       }
                        createPolicy(policy.policyAdapter, username, policyScope,finalName,policyDataString);
 
                }
@@ -3104,7 +3115,7 @@ public class PolicyDBDao {
                                        PolicyLogger.error("We cannot get the group from the papEngine to delete policies");
                                } else {
 
-                                       Set<String> newPolicySet = new HashSet<String>(group.getPolicies().size());
+                                       Set<String> newPolicySet = new HashSet<>(group.getPolicies().size());
                                        //a multiple of n runtime is faster than n^2, so I am using a hashset to do the comparison
                                        for(PDPPolicy pol: group.getPolicies()){
                                                newPolicySet.add(pol.getId());