X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PDP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpdp%2Frest%2Fapi%2Fservices%2FPAPServices.java;h=3c0ea3894b6da14919440f06a8e33014642e5ad7;hb=cc7dca9a21765dca0ba336f27659a3852f6a15d9;hp=e7216e1527ae4234a3075436f8a0d9fe7f4670d4;hpb=073cc188efe9abb4c010cf674e34e2cf46ef1c52;p=policy%2Fengine.git diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java index e7216e152..3c0ea3894 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PDP-REST * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 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. @@ -40,6 +40,7 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.pdp.rest.config.PDPApiAuth; import org.onap.policy.rest.XACMLRestProperties; +import org.onap.policy.utils.CryptoUtils; import org.onap.policy.xacml.api.XACMLErrorConstants; import org.onap.policy.xacml.std.pap.StdPDPPolicy; @@ -53,12 +54,16 @@ public class PAPServices { private int responseCode = 0; private static String environment = "DEVL"; - private static Boolean junit = false; + private static Boolean isJunit = false; private static List paps = null; - private static final Object papResourceLock = new Object(); + private static final Object papResourceLock = new Object(); private String operation = null; private String requestMethod = null; - private String encoding = null; + private String encoding = null; + + public static void setJunit(boolean isJunit) { + PAPServices.isJunit = isJunit; + } public PAPServices() { environment = PDPApiAuth.getEnvironment(); @@ -76,7 +81,7 @@ public class PAPServices { private String getPAPEncoding(){ if(encoding == null){ String userID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID); - String pass = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS); + String pass =CryptoUtils.decryptTxtNoExStr(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)); Base64.Encoder encoder = Base64.getEncoder(); encoding = encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)); } @@ -96,6 +101,10 @@ public class PAPServices { } return result; } + + public static void setPaps(List paps) { + PAPServices.paps = paps; + } public int getResponseCode() { return responseCode; @@ -111,7 +120,7 @@ public class PAPServices { // This makes it Real-Time to change the list depending on their // availability. if (paps == null || paps.isEmpty()) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PAPs List is Empty."; + String message = XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAPs List is Empty."; LOGGER.error(message); throw new PolicyException(message); } @@ -159,7 +168,7 @@ public class PAPServices { } else if(content != null){ // the content is an object to be encoded in JSON ObjectMapper mapper = new ObjectMapper(); - if (!junit) { + if (!isJunit) { mapper.writeValue(connection.getOutputStream(), content); } @@ -169,7 +178,7 @@ public class PAPServices { responseCode = connection.getResponseCode(); // If Connected to PAP then break from the loop and continue // with the Request - if (connection.getResponseCode() > 0 || junit) { + if (connection.getResponseCode() > 0 || isJunit) { connected = true; break; } else { @@ -178,7 +187,7 @@ public class PAPServices { } } catch (Exception e) { // This means that the PAP is not working - if (junit) { + if (isJunit) { connected = true; break; } @@ -207,7 +216,7 @@ public class PAPServices { XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Decoding the result ", e); } - if (junit) { + if (isJunit) { response = SUCCESS; } }else{ @@ -215,7 +224,7 @@ public class PAPServices { } return response; } else { - response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + response = XACMLErrorConstants.ERROR_DATA_ISSUE + "Unable to get valid response from PAP(s) " + paps; return response; } @@ -227,120 +236,104 @@ public class PAPServices { String [] parameters = {"apiflag=version","policyScope="+policyScope, "filePrefix="+filePrefix, "policyName="+policyName}; 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() + e); - } - }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); + }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; } - - 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); + fullURL += "?" + queryString.substring(1); + } - - //set the method and headers - connection.setRequestMethod("GET"); - connection.setUseCaches(false); - connection.setInstanceFollowRedirects(false); - connection.setDoOutput(true); - connection.setDoInput(true); - connection.setRequestProperty("X-ECOMP-RequestID", requestID.toString()); - - //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; + URL url = new URL (fullURL); - } 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(); + //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); + connection.setRequestProperty("X-ECOMP-RequestID", requestID.toString()); + + //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"); } - papsCount++; + } catch (Exception e) { + // This means that the PAP is not working + LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAP connection Error : " + e); + rotatePAPList(); } - - if (connected) { - //Read the Response - LOGGER.debug("connected to the PAP : " + getPAP()); - LOGGER.debug("--- Response: ---"); - Map> 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 - version = connection.getHeaderField("version"); - LOGGER.debug("ActiveVersion from the Header: " + version); - } else if (connection.getResponseCode() == 403) { - LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is " - + connection.getResponseCode() + ". PEP is not Authorized for making this Request!! \n Contact Administrator for this Scope. "); - version = "pe100"; - } else if (connection.getResponseCode() == 404) { - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is " - + connection.getResponseCode() + ". This indicates a problem with getting the version from the PAP"); - version = "pe300"; - } else { - LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST: Error occured while getting the version 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() + e1); - } - } - - } 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() + e); - } - } + papsCount++; } - return version; + + if (connected) { + //Read the Response + LOGGER.debug("connected to the PAP : " + getPAP()); + LOGGER.debug("--- Response: ---"); + Map> 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 + version = connection.getHeaderField("version"); + LOGGER.debug("ActiveVersion from the Header: " + version); + } else if (connection.getResponseCode() == 403) { + LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is " + + connection.getResponseCode() + ". PEP is not Authorized for making this Request!! \n Contact Administrator for this Scope. "); + version = "pe100"; + } else if (connection.getResponseCode() == 404) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is " + + connection.getResponseCode() + ". This indicates a problem with getting the version from the PAP"); + version = "pe300"; + } else { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST: Error occured while getting the version from the PAP. The request may be incorrect."); + } + } catch (IOException e) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e); + } + } else { + LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to get valid response from PAP(s) " + paps); + } + } + return version; } private String checkResponse(HttpURLConnection connection, UUID requestID) throws IOException { String response = null; - if (responseCode == 200 || junit) { + if (responseCode == 200 || isJunit) { // Check for successful creation of policy String isSuccess = null; - if (!junit) { // is this a junit test? + if (!isJunit) { // is this a junit test? isSuccess = connection.getHeaderField("successMapKey"); operation = connection.getHeaderField("operation"); } else { @@ -431,11 +424,14 @@ public class PAPServices { + ". PEP is not Authorized for making this Request!! \n Contact Administrator for this Scope. "; LOGGER.error(response); } else if (connection.getResponseCode() == 404 && connection.getHeaderField("error") != null) { - if ("unknownGroupId".equals(connection.getHeaderField("error"))) { + if ("UnknownGroup".equals(connection.getHeaderField("error"))) { response = XACMLErrorConstants.ERROR_DATA_ISSUE + connection.getHeaderField("message") - + " Please check the pdpGroup you are requesting to move the policy to."; + + " Please check the pdpGroup you are requesting to push the policy to."; LOGGER.error(response); + } else if ("policyNotAvailableForEdit".equals(connection.getHeaderField("error"))) { + response = XACMLErrorConstants.ERROR_DATA_ISSUE + + connection.getHeaderField("message"); } } else if (connection.getResponseCode() == 409 && connection.getHeaderField("error") != null) { if ("modelExistsDB".equals(connection.getHeaderField("error"))) { @@ -503,13 +499,16 @@ public class PAPServices { 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. " + connection.getHeaderField("error"); + response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + + "Error occured while attempting perform this operation.. " + + "the request may be incorrect or the PAP is unreachable. " + + connection.getHeaderField("error"); } LOGGER.error(response); } else { - response = XACMLErrorConstants.ERROR_DATA_ISSUE - + "BAD REQUEST: Error occured while attempting perform this operation.. the request may be incorrect."; + response = XACMLErrorConstants.ERROR_SYSTEM_ERROR + + "Error occured while attempting perform this operation.. " + + "the request may be incorrect or the PAP is unreachable."; LOGGER.error(response); } return response; @@ -557,11 +556,11 @@ public class PAPServices { + "\"policyName\": \""+policyName+"\"," + "\"clientScope\": \""+clientScope+"\"," + "\"pdpGroup\": \""+pdpGroup+"\"}"; - //String response = null; - HttpURLConnection connection = null; + + HttpURLConnection connection = null; responseCode = 0; if (paps == null || paps.isEmpty()) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PAPs List is Empty."; + String message = XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PAPs List is Empty."; LOGGER.error(message); throw new PolicyException(message); } @@ -608,7 +607,7 @@ public class PAPServices { responseCode = connection.getResponseCode(); // If Connected to PAP then break from the loop and continue // with the Request - if (connection.getResponseCode() > 0 || junit) { + if (connection.getResponseCode() > 0 || isJunit) { connected = true; break; } else { @@ -617,7 +616,7 @@ public class PAPServices { } } catch (Exception e) { // This means that the PAP is not working - if (junit) { + if (isJunit) { connected = true; break; }