X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FDcaeDispatcherServices.java;h=51e4abcb3c28830d309f84002e3f3335776c0f33;hb=81305e6875977b25afd6320c6870c41fb65e040a;hp=f285dc27cc284965bdcd9843bade70a10b0ea8e3;hpb=da4caf492050e17c1aaa8915064659a16e060d8d;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java index f285dc27..51e4abcb 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java @@ -27,17 +27,7 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.fasterxml.jackson.databind.node.ObjectNode; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; import java.util.Date; -import java.util.stream.Collectors; - -import javax.net.ssl.HttpsURLConnection; -import javax.ws.rs.BadRequestException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -52,11 +42,16 @@ import org.springframework.beans.factory.annotation.Autowired; * */ public class DcaeDispatcherServices { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class); - protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - + protected static final EELFLogger logger = EELFManager.getInstance() + .getLogger(DcaeDispatcherServices.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); @Autowired private RefProp refProp; + private static final String STATUS_URL_LOG = "Status URL extracted: "; + private static final String DCAE_URL_PREFIX = "/dcae-deployments/"; + private static final String DCAE_URL_PROPERTY_NAME = "DCAE_DISPATCHER_URL"; + private static final String DCAE_LINK_FIELD = "links"; + private static final String DCAE_STATUS_FIELD = "status"; /** * Delete the deployment on DCAE. @@ -64,74 +59,27 @@ public class DcaeDispatcherServices { * @param deploymentId * The deployment ID * @return Return the URL Status - * @throws IOException - * In case of issues with the Stream */ - public String deleteDeployment(String deploymentId) throws IOException { - - String statusUrl = null; - InputStream in = null; + public String deleteDeployment(String deploymentId) { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "deleteDeployment"); try { - String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId; - logger.info("Dcae Dispatcher url - " + url); - URL obj = new URL(url); - HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - conn.setRequestMethod("DELETE"); - int responseCode = conn.getResponseCode(); - - boolean requestFailed = true; - logger.info("responseCode=" + responseCode); - if (responseCode == 200 || responseCode == 202) { - requestFailed = false; - } - - InputStream inStream = conn.getErrorStream(); - if (inStream == null) { - inStream = conn.getInputStream(); - } - - String responseStr = null; - if (inStream != null) { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream)); - String inputLine = null; - StringBuilder response = new StringBuilder(); - while ((inputLine = bufferedReader.readLine()) != null) { - response.append(inputLine); - } - responseStr = response.toString(); - } - - if (responseStr != null && requestFailed) { - logger.error("requestFailed - responseStr=" + responseStr); - throw new BadRequestException(responseStr); - } - - logger.debug("response code " + responseCode); - in = conn.getInputStream(); - logger.debug("res:" + responseStr); + String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId; + String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", null, null); JSONParser parser = new JSONParser(); Object obj0 = parser.parse(responseStr); JSONObject jsonObj = (JSONObject) obj0; - JSONObject linksObj = (JSONObject) jsonObj.get("links"); - statusUrl = (String) linksObj.get("status"); - logger.debug("Status URL: " + statusUrl); - + JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD); + String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD); + logger.info(STATUS_URL_LOG + statusUrl); + return statusUrl; } catch (Exception e) { logger.error("Exception occurred during Delete Deployment Operation with DCAE", e); throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e); } finally { - if (in != null) { - in.close(); - } LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("deleteDeployment complete"); } - - return statusUrl; - } /** @@ -140,43 +88,25 @@ public class DcaeDispatcherServices { * @param statusUrl * The URL provided by a previous DCAE Query * @return The status - * @throws IOException - * In case of issues with the Stream * */ - public String getOperationStatus(String statusUrl) throws IOException { - + public String getOperationStatus(String statusUrl) { // Assigning processing status to monitor operation status further String opStatus = "processing"; - InputStream in = null; Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "getOperationStatus"); try { - URL obj = new URL(statusUrl); - HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); - conn.setRequestMethod("GET"); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - int responseCode = conn.getResponseCode(); - logger.debug("Deployment operation status response code - " + responseCode); - if (responseCode == 200) { - in = conn.getInputStream(); - String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")); - JSONParser parser = new JSONParser(); - Object obj0 = parser.parse(res); - JSONObject jsonObj = (JSONObject) obj0; - String operationType = (String) jsonObj.get("operationType"); - String status = (String) jsonObj.get("status"); - logger.debug("Operation Type - " + operationType + ", Status " + status); - opStatus = status; - } + String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null); + JSONParser parser = new JSONParser(); + Object obj0 = parser.parse(responseStr); + JSONObject jsonObj = (JSONObject) obj0; + String operationType = (String) jsonObj.get("operationType"); + String status = (String) jsonObj.get("status"); + logger.info("Operation Type - " + operationType + ", Status " + status); + opStatus = status; } catch (Exception e) { logger.error("Exception occurred during getOperationStatus Operation with DCAE", e); - logger.debug(e.getMessage() - + " : got exception while retrieving status, trying again until we get 200 response code"); } finally { - if (in != null) { - in.close(); - } LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("getOperationStatus complete"); } @@ -186,32 +116,17 @@ public class DcaeDispatcherServices { /** * This method send a getDeployments operation to DCAE. * - * @throws IOException - * In case of issues with the Stream */ - public void getDeployments() throws IOException { - InputStream in = null; + public void getDeployments() { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "getDeployments"); try { - String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments"; - logger.info("Dcae Dispatcher deployments url - " + url); - URL obj = new URL(url); - HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); - conn.setRequestMethod("GET"); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - int responseCode = conn.getResponseCode(); - logger.debug("response code " + responseCode); - in = conn.getInputStream(); - String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")); - logger.debug("res:" + res); + String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX; + DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null); } catch (Exception e) { logger.error("Exception occurred during getDeployments Operation with DCAE", e); throw new DcaeDeploymentException("Exception occurred during getDeployments Operation with DCAE", e); } finally { - if (in != null) { - in.close(); - } LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("getDeployments complete"); } @@ -225,88 +140,32 @@ public class DcaeDispatcherServices { * @param serviceTypeId * Service type ID * @return The status URL - * @throws IOException - * In case of issues with the Stream */ - public String createNewDeployment(String deploymentId, String serviceTypeId) throws IOException { - - String statusUrl = null; - InputStream inStream = null; - BufferedReader in = null; + public String createNewDeployment(String deploymentId, String serviceTypeId) { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "createNewDeployment"); try { ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template"); - ((ObjectNode) rootNode).put("serviceTypeId", serviceTypeId); + rootNode.put("serviceTypeId", serviceTypeId); String apiBodyString = rootNode.toString(); - logger.info("Dcae api Body String - " + apiBodyString); - String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId; - logger.info("Dcae Dispatcher Service url - " + url); - URL obj = new URL(url); - HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); - conn.setRequestMethod("PUT"); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setDoOutput(true); - try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { - wr.writeBytes(apiBodyString); - wr.flush(); - } - - boolean requestFailed = true; - int responseCode = conn.getResponseCode(); - logger.info("responseCode=" + responseCode); - if (responseCode == 200 || responseCode == 202) { - requestFailed = false; - } - - inStream = conn.getErrorStream(); - if (inStream == null) { - inStream = conn.getInputStream(); - } - - String responseStr = null; - if (inStream != null) { - in = new BufferedReader(new InputStreamReader(inStream)); - - String inputLine = null; - - StringBuilder response = new StringBuilder(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - - responseStr = response.toString(); - } - - if (responseStr != null && requestFailed) { - logger.error("requestFailed - responseStr=" + responseStr); - throw new BadRequestException(responseStr); - } - - logger.debug("response code " + responseCode); + String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId; + String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "PUT", apiBodyString, + "application/json"); JSONParser parser = new JSONParser(); Object obj0 = parser.parse(responseStr); JSONObject jsonObj = (JSONObject) obj0; - JSONObject linksObj = (JSONObject) jsonObj.get("links"); - statusUrl = (String) linksObj.get("status"); - logger.debug("Status URL: " + statusUrl); + JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD); + String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD); + logger.info(STATUS_URL_LOG + statusUrl); + return statusUrl; } catch (Exception e) { logger.error("Exception occurred during createNewDeployment Operation with DCAE", e); throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e); } finally { - if (inStream != null) { - inStream.close(); - } - if (in != null) { - in.close(); - } LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("createNewDeployment complete"); } - return statusUrl; } /** @@ -317,53 +176,30 @@ public class DcaeDispatcherServices { * @param serviceTypeId * The service Type ID * @return The status URL - * @throws IOException - * In case of issues with the Stream */ - public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws IOException { - - String statusUrl = null; - InputStream in = null; + public String deleteExistingDeployment(String deploymentId, String serviceTypeId) { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment"); try { String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}"; - logger.debug(apiBodyString); - String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId; - logger.info("Dcae Dispatcher deployments url - " + url); - URL obj = new URL(url); - HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); - conn.setRequestMethod("DELETE"); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setDoOutput(true); - DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); - wr.writeBytes(apiBodyString); - wr.flush(); - - int responseCode = conn.getResponseCode(); - logger.debug("response code " + responseCode); - in = conn.getInputStream(); - String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")); - logger.debug("res:" + res); + logger.info("Dcae api Body String - " + apiBodyString); + String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId; + String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", apiBodyString, + "application/json"); JSONParser parser = new JSONParser(); - Object obj0 = parser.parse(res); + Object obj0 = parser.parse(responseStr); JSONObject jsonObj = (JSONObject) obj0; - JSONObject linksObj = (JSONObject) jsonObj.get("links"); - statusUrl = (String) linksObj.get("status"); - logger.debug("Status URL: " + statusUrl); + JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD); + String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD); + logger.info(STATUS_URL_LOG + statusUrl); + return statusUrl; } catch (Exception e) { logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e); throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE", e); } finally { - if (in != null) { - in.close(); - } LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("deleteExistingDeployment complete"); } - return statusUrl; } - } \ No newline at end of file