Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / PAPServices.java
index b00346d..4a705f5 100644 (file)
@@ -25,7 +25,6 @@ import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
-import java.net.URI;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
@@ -193,22 +192,26 @@ public class PAPServices {
             // Read the Response
             LOGGER.debug("connected to the PAP : " + getPAP());
             LOGGER.debug("--- Response: ---");
-            Map<String, List<String>> headers = connection.getHeaderFields();
-            for (String key : headers.keySet()) {
-                LOGGER.debug("Header :" + key + "  Value: " + headers.get(key));
-            }
-                
-            try {
-                response = checkResponse(connection, requestID);
-            } catch (IOException e) {
-                LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
-                response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + e;
-                throw new PolicyException(
-                        XACMLErrorConstants.ERROR_SYSTEM_ERROR
-                                + "Decoding the result ", e);
-            }
-            if (junit) {
-                response = SUCCESS;
+            if(connection != null){
+               Map<String, List<String>> headers = connection.getHeaderFields();
+               for (String key : headers.keySet()) {
+                       LOGGER.debug("Header :" + key + "  Value: " + headers.get(key));
+               }
+
+               try {
+                       response = checkResponse(connection, requestID);
+               } catch (IOException e) {
+                       LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
+                       response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + e;
+                       throw new PolicyException(
+                                       XACMLErrorConstants.ERROR_SYSTEM_ERROR
+                                       + "Decoding the result ", e);
+               }
+               if (junit) {
+                       response = SUCCESS;
+               }
+            }else{
+               response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + "connection is null";
             }
             return response;
         } else {
@@ -266,11 +269,8 @@ public class PAPServices {
                         connection.setInstanceFollowRedirects(false);
                         connection.setDoOutput(true);
                         connection.setDoInput(true);
-                        // mb1915 - begin
-                        // mb1915 - set requestID in header properties to be used to send to PAP on the GET request so PAP won't generate another
                         connection.setRequestProperty("X-ECOMP-RequestID", requestID.toString());
-                        // mb1915 - end
-        
+  
                         //DO the connect
                         connection.connect();
         
@@ -335,266 +335,6 @@ public class PAPServices {
             return version;
     }
     
-    public StdPDPPolicy getGitPath(String policyScope, String filePrefix, String policyName, String activeVersion, String clientScope, UUID requestID, String id) throws PolicyException{
-        String gitPath = null;
-        Boolean isValid = false;
-        String policyId= null;
-        String description = null;
-        String pushVersion = null;
-        HttpURLConnection connection = null;
-        String [] parameters = {"apiflag=gitPath", "policyScope="+policyScope, "filePrefix="+filePrefix, 
-                                    "policyName="+policyName, "activeVersion="+activeVersion};
-        if (paps == null || paps.isEmpty()) {
-            LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "PAPs List is Empty.");
-            try {
-                throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE +"PAPs List is empty.");
-            } catch (Exception e) {
-                LOGGER.error(e.getMessage());
-            }
-            }else {
-                int papsCount = 0;
-                boolean connected = false;
-                while (papsCount < paps.size()) {
-                    try {
-                        String fullURL = getPAP();
-                        if (parameters != null && parameters.length > 0) {
-                            String queryString = "";
-                            for (String p : parameters) {
-                                queryString += "&" + p;
-                            }
-                            fullURL += "?" + queryString.substring(1);
-                        }
-        
-                        URL url = new URL (fullURL);
-                        
-                        //Open the connection
-                        connection = (HttpURLConnection)url.openConnection();
-                        
-                        // Setting Content-Type
-                        connection.setRequestProperty("Content-Type",
-                                "application/json");
-                        
-                        // Adding Authorization
-                        connection.setRequestProperty("Authorization", "Basic "
-                                +getPAPEncoding());
-                        
-                        connection.setRequestProperty("Environment", environment);
-                        connection.setRequestProperty("ClientScope", clientScope);
-                        
-                        //set the method and headers
-                        connection.setRequestMethod("GET");
-                        connection.setUseCaches(false);
-                        connection.setInstanceFollowRedirects(false);
-                        connection.setDoOutput(true);
-                        connection.setDoInput(true);
-                        // mb1915 - begin
-                        // mb1915 - set requestID in header properties to be used to send to PAP on the GET request so PAP won't generate another
-                        connection.setRequestProperty("X-ECOMP-RequestID", requestID.toString());
-                        // mb1915 - end
-        
-                        //DO the connect
-                        connection.connect();
-        
-                        // If Connected to PAP then break from the loop and continue with the Request 
-                        if (connection.getResponseCode() > 0) {
-                            connected = true;
-                            break;
-
-                        } else {
-                            LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP connection Error");
-                        }
-                    } catch (Exception e) {
-                        // This means that the PAP is not working 
-                        LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP connection Error : " + e);
-                        rotatePAPList();
-                    }
-                    papsCount++;
-                }
-        
-                if (connected) {
-                    //Read the Response
-                    LOGGER.debug("connected to the PAP : " + getPAP());
-                    LOGGER.debug("--- Response: ---");
-                    Map<String, List<String>> headers = connection.getHeaderFields();
-                    for (String key : headers.keySet()) {
-                        LOGGER.debug("Header :" + key + "  Value: " + headers.get(key));
-                    }
-                    try {
-                        if (connection.getResponseCode() == 200) {
-                            // Check for successful creation of policy
-                            gitPath = connection.getHeaderField("gitPath");
-                            policyId = connection.getHeaderField("policyId");
-                            description = connection.getHeaderField("description");
-                            pushVersion = connection.getHeaderField("version");
-                            isValid = Boolean.parseBoolean(connection.getHeaderField("isValid"));
-                            
-                            LOGGER.debug("GitPath from Header: " + gitPath);
-                            LOGGER.debug("policyId from Header: " + policyId);
-                            LOGGER.debug("description from Header: " + description);
-                            LOGGER.debug("version from Header: " + pushVersion);
-                            LOGGER.debug("isValid from Header: " + isValid);
-                            
-                            /*if (gitPath != null && !gitPath.equalsIgnoreCase("")) {
-                                return gitPath;
-                            }*/ if (gitPath == null || gitPath.trim().isEmpty()) {
-                                LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the gitPath from the PAP");
-                            }
-                        } else if (connection.getResponseCode() == 404) {
-                            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "response code of the URL is " 
-                                        + connection.getResponseCode() + ". This indicates a problem with getting the gitPath from the PAP");
-                        } else {
-                            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "BAD REQUEST:  Error occured while getting the gitPath from the PAP. The request may be incorrect.");
-                        }
-                    } catch (IOException e) {
-                        LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
-                        try {
-                            throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE +"ERROR in connecting to the PAP ", e);
-                        } catch (Exception e1) {
-                            LOGGER.error(e1.getMessage());
-                        }
-                    } 
-        
-                } else {
-                    LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get valid response from PAP(s) " + paps);
-                    try {
-                        throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE +"ERROR in connecting to the PAP ");
-                    } catch (Exception e) {
-                        LOGGER.error(e.getMessage());
-                    }
-                }   
-            }
-        LOGGER.debug("Full gitPath policy xml file: " + gitPath);
-        URI selectedURI = getSelectedURI(gitPath, clientScope, requestID);
-        LOGGER.debug("The selectedURI is : " + selectedURI.toString());
-        String name = filePrefix+policyName;
-                
-        StdPDPPolicy selectedPolicy;
-        try {
-            selectedPolicy = new StdPDPPolicy(id, true, name, selectedURI, isValid, policyId, description, pushVersion);
-        } catch (IOException e) {
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
-            throw new PolicyException(e);
-        }
-        return selectedPolicy;
-    }
-    
-    private URI getSelectedURI(String gitPath, String clientScope, UUID requestID){
-        URI selectedURI = null;
-        HttpURLConnection connection = null;
-        String [] parameters = {"apiflag=uri", "gitPath="+gitPath};
-        if (paps == null || paps.isEmpty()) {
-            LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "PAPs List is Empty.");
-            try {
-                throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE +"PAPs List is empty.");
-            } catch (Exception e) {
-                LOGGER.error(e.getMessage());
-            }
-            }else {
-                int papsCount = 0;
-                boolean connected = false;
-                while (papsCount < paps.size()) {
-                    try {
-                        String fullURL = getPAP();
-                        if (parameters != null && parameters.length > 0) {
-                            String queryString = "";
-                            for (String p : parameters) {
-                                queryString += "&" + p;
-                            }
-                            fullURL += "?" + queryString.substring(1);
-                        }
-        
-                        URL url = new URL (fullURL);
-                        
-                        //Open the connection
-                        connection = (HttpURLConnection)url.openConnection();
-                        
-                        // Setting Content-Type
-                        connection.setRequestProperty("Content-Type",
-                                "application/json");
-                        
-                        // Adding Authorization
-                        connection.setRequestProperty("Authorization", "Basic "
-                                + getPAPEncoding());
-                        
-                        connection.setRequestProperty("Environment", environment);
-                        connection.setRequestProperty("ClientScope", clientScope);
-                        
-                        //set the method and headers
-                        connection.setRequestMethod("GET");
-                        connection.setUseCaches(false);
-                        connection.setInstanceFollowRedirects(false);
-                        connection.setDoOutput(true);
-                        connection.setDoInput(true);
-                        // mb1915 - begin  
-                        // mb1915 - set requestID in header properties to be used to send to PAP on the GET request so PAP won't generate another
-                        connection.setRequestProperty("X-ECOMP-RequestID", requestID.toString());
-                        // mb1915 - end
-        
-                        //DO the connect
-                        connection.connect();
-                        responseCode = connection.getResponseCode();
-                        // If Connected to PAP then break from the loop and continue with the Request 
-                        if (connection.getResponseCode() > 0) {
-                            connected = true;
-                            break;
-
-                        } else {
-                            LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP connection Error");
-                        }
-                    } catch (Exception e) {
-                        // This means that the PAP is not working 
-                        LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP connection Error : " + e);
-                        rotatePAPList();
-                    }
-                    papsCount++;
-                }
-        
-                if (connected) {
-                    //Read the Response
-                    LOGGER.debug("connected to the PAP : " + getPAP());
-                    LOGGER.debug("--- Response: ---");
-                    Map<String, List<String>> headers = connection.getHeaderFields();
-                    for (String key : headers.keySet()) {
-                        LOGGER.debug("Header :" + key + "  Value: " + headers.get(key));
-                    }
-                    try {
-                        if (connection.getResponseCode() == 200) {
-                            // Check for successful creation of policy
-                            String uri = connection.getHeaderField("selectedURI");
-                            LOGGER.debug("URI from Header: " + uri);
-                            if (uri != null && !uri.equalsIgnoreCase("")) {
-                                selectedURI = URI.create(uri);
-                                return selectedURI;
-                            } else {
-                                LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the gitPath from the PAP");
-                            }
-                        } else if (connection.getResponseCode() == 404) {
-                            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "response code of the URL is " 
-                                        + connection.getResponseCode() + ". This indicates a problem with getting the gitPath from the PAP");
-                        } else {
-                            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "BAD REQUEST:  Error occured while getting the gitPath from the PAP. The request may be incorrect.");
-                        }
-                    } catch (IOException e) {
-                        LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
-                        try {
-                            throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE +"ERROR in connecting to the PAP ", e);
-                        } catch (Exception e1) {
-                            LOGGER.error(e1.getMessage());
-                        }
-                    } 
-        
-                } else {
-                    LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get valid response from PAP(s) " + paps);
-                    try {
-                        throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE +"ERROR in connecting to the PAP ");
-                    } catch (Exception e) {
-                        LOGGER.error(e.getMessage());
-                    }
-                }   
-            }
-            return selectedURI;
-    }
-    
     private String checkResponse(HttpURLConnection connection, UUID requestID) throws IOException {
         String response = null;
         if (responseCode == 200 || junit) {         
@@ -611,19 +351,21 @@ public class PAPServices {
                     response = "Transaction ID: " + requestID + " --Policy with the name "+ connection.getHeaderField("policyName")
                             + " was successfully updated. ";
                     if (connection.getHeaderField("safetyChecker")!=null) {
-                       response = response + "\nPolicy Safety Checker Warning: This closedLoopControlName (" 
-                                                       + connection.getHeaderField("newCLName") + ") " + "is potentially in conflict with " + connection.getHeaderField("conflictCLName") 
-                                                       +  "that already exists." + "See detailed information on ClosedLoop Pairs below: "
-                                                       +"\n"+connection.getHeaderField("safetyChecker");
+                       response = response 
+                                               + "\n\nPolicy Safety Checker Warning: This closedLoopControlName "
+                                               + "is potentially in conflict with " + connection.getHeaderField("conflictCLName") 
+                                               +  " that already exists." + " See detailed information on ClosedLoop Pairs below: "
+                                               +"\n\n"+connection.getHeaderField("safetyChecker");
                     }
                 } else if ("create".equals(operation)) {
                        response = "Transaction ID: " + requestID + " --Policy with the name "+ connection.getHeaderField("policyName")
                             + " was successfully created.";
                     if (connection.getHeaderField("safetyChecker")!=null) {
-                       response = response + "\nPolicy Safety Checker Warning: This closedLoopControlName (" 
-                                                       + connection.getHeaderField("newCLName") + ") " + "is potentially in conflict with " + connection.getHeaderField("conflictCLName") 
-                                                       +  "that already exists." + "See detailed information on ClosedLoop Pairs below: "
-                                                       +"\n"+connection.getHeaderField("safetyChecker");
+                       response = response 
+                                               + "\n\nPolicy Safety Checker Warning: This closedLoopControlName "
+                                               + "is potentially in conflict with " + connection.getHeaderField("conflictCLName") 
+                                               +  " that already exists. " + "See detailed information on ClosedLoop Pairs below: "
+                                               +"\n\n"+connection.getHeaderField("safetyChecker");
                     }
                 } else if ("delete".equals(operation)) {
                     response = "Transaction ID: " + requestID + " --The policy was successfully deleted.";
@@ -748,9 +490,16 @@ public class PAPServices {
                         + connection.getHeaderField("message");
             } else if ("addGroupError".equals(connection.getHeaderField("error"))) {
                 response = connection.getHeaderField("message");
+            } else if ("validation".equals(connection.getHeaderField("error"))){
+                response = XACMLErrorConstants.ERROR_DATA_ISSUE
+                        + "Validation errors during policy engine " + connection.getHeaderField("operation") +
+                        " for " + connection.getHeaderField("service");
             } else if ("error".equals(connection.getHeaderField("error"))) {
                 response = XACMLErrorConstants.ERROR_UNKNOWN
                         + "Could not create or update the policy for and unknown reason";
+            }else{
+                response = XACMLErrorConstants.ERROR_DATA_ISSUE
+                        + "BAD REQUEST:  Error occured while attempting perform this operation.. the request may be incorrect.";
             }
             LOGGER.error(response);
         } else {
@@ -877,20 +626,22 @@ public class PAPServices {
             // Read the Response
             LOGGER.debug("connected to the PAP : " + getPAP());
             LOGGER.debug("--- Response: ---");
-            Map<String, List<String>> headers = connection.getHeaderFields();
-            for (String key : headers.keySet()) {
-                LOGGER.debug("Header :" + key + "  Value: " + headers.get(key));
-            }
-            try {
-                if(responseCode==202){
-                       StdPDPPolicy policy = (StdPDPPolicy) new ObjectInputStream(connection.getInputStream()).readObject();
-                       return policy;
+            if(connection != null){
+               Map<String, List<String>> headers = connection.getHeaderFields();
+                for (String key : headers.keySet()) {
+                    LOGGER.debug("Header :" + key + "  Value: " + headers.get(key));
                 }
-            } catch (IOException | ClassNotFoundException e) {
-                LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
-                throw new PolicyException(
-                        XACMLErrorConstants.ERROR_SYSTEM_ERROR
-                                + "Decoding the result ", e);
+                try {
+                    if(responseCode==202){
+                       StdPDPPolicy policy = (StdPDPPolicy) new ObjectInputStream(connection.getInputStream()).readObject();
+                       return policy;
+                    }
+                } catch (IOException | ClassNotFoundException e) {
+                    LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
+                    throw new PolicyException(
+                            XACMLErrorConstants.ERROR_SYSTEM_ERROR
+                                    + "Decoding the result ", e);
+                }      
             }
             return null;
         } else {