Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / PAPServices.java
index 5aa3360..fe8c794 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) {         
@@ -879,20 +619,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 {