From 3772c8513f618f9b76fc7bfa55fa140eb60f2a4b Mon Sep 17 00:00:00 2001 From: sebdet Date: Fri, 29 Mar 2019 11:52:02 +0100 Subject: [PATCH] Refactoring Small refactoring of the Http library Issue-ID: CLAMP-333 Change-Id: I90a5488241ab84636bc0cec5849e97c4f98f3a38 Signed-off-by: sebdet --- .../clamp/clds/client/DcaeDispatcherServices.java | 4 +-- .../clamp/clds/client/DcaeInventoryServices.java | 2 +- src/main/java/org/onap/clamp/loop/Loop.java | 40 ++++++++++++---------- .../clamp/{operation => loop}/LoopOperation.java | 5 +-- .../org/onap/clamp/policy/PolicyOperation.java | 4 +-- .../org/onap/clamp/util/HttpConnectionManager.java | 37 +++++++------------- src/main/resources/application-noaaf.properties | 1 - src/main/resources/application.properties | 1 - .../clds/client/DcaeDispatcherServicesTest.java | 12 +++---- .../clamp/clds/it/HttpConnectionManagerItCase.java | 10 +++--- src/test/resources/application.properties | 1 - 11 files changed, 51 insertions(+), 66 deletions(-) rename src/main/java/org/onap/clamp/{operation => loop}/LoopOperation.java (99%) 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 0f465959..f74af49f 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java @@ -99,7 +99,7 @@ public class DcaeDispatcherServices { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "getOperationStatus"); try { - String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(statusUrl, "GET", null, null, "DCAE", null, null); + String responseStr = dcaeHttpConnectionManager.doHttpRequest(statusUrl, "GET", null, null, "DCAE", null, null); JSONObject jsonObj = parseResponse(responseStr); String operationType = (String) jsonObj.get("operationType"); String status = (String) jsonObj.get(DCAE_STATUS_FIELD); @@ -190,7 +190,7 @@ public class DcaeDispatcherServices { String nodeAttr) throws IOException, ParseException { Date startTime = new Date(); try { - String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(url, requestMethod, payload, contentType, "DCAE", null, null); + String responseStr = dcaeHttpConnectionManager.doHttpRequest(url, requestMethod, payload, contentType, "DCAE", null, null); JSONObject jsonObj = parseResponse(responseStr); JSONObject linksObj = (JSONObject) jsonObj.get(node); String statusUrl = (String) linksObj.get(nodeAttr); diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java index d8bfeeae..dcf05423 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -207,7 +207,7 @@ public class DcaeInventoryServices { } for (int i = 0; i < retryLimit; i++) { metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory"); - String response = httpConnectionManager.doGeneralHttpQuery(fullUrl, "GET", null, null, "DCAE", null, null); + String response = httpConnectionManager.doHttpRequest(fullUrl, "GET", null, null, "DCAE", null, null); int totalCount = getTotalCountFromDcaeInventoryResponse(response); metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount); if (totalCount > 0) { diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index cc04ce5d..e62874a8 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -105,8 +105,7 @@ public class Loop implements Serializable { @Expose @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), - inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id")) + @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id")) private Set microServicePolicies = new HashSet<>(); @Expose @@ -135,23 +134,23 @@ public class Loop implements Serializable { this.name = name; } - public String getDcaeDeploymentId() { + String getDcaeDeploymentId() { return dcaeDeploymentId; } - public void setDcaeDeploymentId(String dcaeDeploymentId) { + void setDcaeDeploymentId(String dcaeDeploymentId) { this.dcaeDeploymentId = dcaeDeploymentId; } - public String getDcaeDeploymentStatusUrl() { + String getDcaeDeploymentStatusUrl() { return dcaeDeploymentStatusUrl; } - public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) { + void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) { this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl; } - public String getSvgRepresentation() { + String getSvgRepresentation() { return svgRepresentation; } @@ -159,7 +158,7 @@ public class Loop implements Serializable { this.svgRepresentation = svgRepresentation; } - public String getBlueprint() { + String getBlueprint() { return blueprint; } @@ -167,11 +166,11 @@ public class Loop implements Serializable { this.blueprint = blueprint; } - public LoopState getLastComputedState() { + LoopState getLastComputedState() { return lastComputedState; } - public void setLastComputedState(LoopState lastComputedState) { + void setLastComputedState(LoopState lastComputedState) { this.lastComputedState = lastComputedState; } @@ -183,7 +182,7 @@ public class Loop implements Serializable { this.operationalPolicies = operationalPolicies; } - public Set getMicroServicePolicies() { + Set getMicroServicePolicies() { return microServicePolicies; } @@ -222,7 +221,7 @@ public class Loop implements Serializable { log.setLoop(this); } - public String getDcaeBlueprintId() { + String getDcaeBlueprintId() { return dcaeBlueprintId; } @@ -230,7 +229,7 @@ public class Loop implements Serializable { this.dcaeBlueprintId = dcaeBlueprintId; } - public JsonObject getModelPropertiesJson() { + JsonObject getModelPropertiesJson() { return modelPropertiesJson; } @@ -240,13 +239,18 @@ public class Loop implements Serializable { /** * Generate the loop name. - * @param serviceName The service name - * @param serviceVersion The service version - * @param resourceName The resource name - * @param blueprintFileName The blueprint file name + * + * @param serviceName + * The service name + * @param serviceVersion + * The service version + * @param resourceName + * The resource name + * @param blueprintFileName + * The blueprint file name * @return The generated loop name */ - public static String generateLoopName(String serviceName, String serviceVersion, String resourceName, + static String generateLoopName(String serviceName, String serviceVersion, String resourceName, String blueprintFilename) { StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion) .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", "")); diff --git a/src/main/java/org/onap/clamp/operation/LoopOperation.java b/src/main/java/org/onap/clamp/loop/LoopOperation.java similarity index 99% rename from src/main/java/org/onap/clamp/operation/LoopOperation.java rename to src/main/java/org/onap/clamp/loop/LoopOperation.java index bdb4b3f0..7def783b 100644 --- a/src/main/java/org/onap/clamp/operation/LoopOperation.java +++ b/src/main/java/org/onap/clamp/loop/LoopOperation.java @@ -21,7 +21,7 @@ * */ -package org.onap.clamp.operation; +package org.onap.clamp.loop; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; @@ -45,9 +45,6 @@ import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.util.LoggingUtils; import org.onap.clamp.clds.util.ONAPLogConstants; import org.onap.clamp.exception.OperationException; -import org.onap.clamp.loop.Loop; -import org.onap.clamp.loop.LoopService; -import org.onap.clamp.loop.LoopState; import org.onap.clamp.policy.PolicyOperation; import org.onap.clamp.util.HttpConnectionManager; import org.slf4j.event.Level; diff --git a/src/main/java/org/onap/clamp/policy/PolicyOperation.java b/src/main/java/org/onap/clamp/policy/PolicyOperation.java index 592338c4..edce8ff5 100644 --- a/src/main/java/org/onap/clamp/policy/PolicyOperation.java +++ b/src/main/java/org/onap/clamp/policy/PolicyOperation.java @@ -83,7 +83,7 @@ public class PolicyOperation { String url = refProp.getStringValue(POLICY_URL_PROPERTY_NAME) + policyType + POLICY_URL_SUFFIX; String userName = refProp.getStringValue(POLICY_USER_NAME); String encodedPass = refProp.getStringValue(POLICY_PASSWORD); - httpConnectionManager.doGeneralHttpQuery(url, "POST", payload.toString(), "application/json", "POLICY", userName, encodedPass); + httpConnectionManager.doHttpRequest(url, "POST", payload.toString(), "application/json", "POLICY", userName, encodedPass); } } @@ -93,7 +93,7 @@ public class PolicyOperation { String url = refProp.getStringValue(POLICY_URL_PROPERTY_NAME) + policyType + POLICY_URL_SUFFIX + "/" + msPolicy.getName(); String userName = refProp.getStringValue(POLICY_USER_NAME); String encodedPass = refProp.getStringValue(POLICY_PASSWORD); - httpConnectionManager.doGeneralHttpQuery(url, "POST", null, null, "POLICY", userName, encodedPass); + httpConnectionManager.doHttpRequest(url, "POST", null, null, "POLICY", userName, encodedPass); } } diff --git a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java index 4e97f29b..6459fa97 100644 --- a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java +++ b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java @@ -27,22 +27,19 @@ package org.onap.clamp.util; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import sun.misc.BASE64Encoder; - import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import java.security.GeneralSecurityException; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import javax.net.ssl.HttpsURLConnection; import javax.ws.rs.BadRequestException; -import org.apache.commons.codec.DecoderException; import org.apache.commons.io.IOUtils; -import org.onap.clamp.clds.util.CryptoUtils; import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.stereotype.Component; @@ -55,14 +52,16 @@ public class HttpConnectionManager { protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); private static final String REQUEST_FAILED_LOG = "Request Failed - response payload="; - private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { + private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target, + String userName, String password) throws IOException { LoggingUtils utils = new LoggingUtils(logger); logger.info("Using HTTPS URL:" + url.toString()); HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection(); secureConnection = utils.invokeHttps(secureConnection, target, requestMethod); secureConnection.setRequestMethod(requestMethod); if (userName != null && password != null) { - secureConnection.setRequestProperty("Authorization", "Basic " + generateBasicAuth(userName, password)); + secureConnection.setRequestProperty("Authorization", "Basic " + + Base64.getEncoder().encodeToString((userName + ":" + password).getBytes(StandardCharsets.UTF_8))); } if (payload != null && contentType != null) { secureConnection.setRequestProperty("Content-Type", contentType); @@ -91,14 +90,16 @@ public class HttpConnectionManager { } } - private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { + private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target, + String userName, String password) throws IOException { LoggingUtils utils = new LoggingUtils(logger); logger.info("Using HTTP URL:" + url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection = utils.invoke(connection, target, requestMethod); connection.setRequestMethod(requestMethod); if (userName != null && password != null) { - connection.setRequestProperty("Authorization", "Basic " + generateBasicAuth(userName, password)); + connection.setRequestProperty("Authorization", "Basic " + + Base64.getEncoder().encodeToString((userName + ":" + password).getBytes(StandardCharsets.UTF_8))); } if (payload != null && contentType != null) { connection.setRequestProperty("Content-Type", contentType); @@ -144,8 +145,8 @@ public class HttpConnectionManager { * @throws IOException * In case of issue with the streams */ - public String doGeneralHttpQuery(String url, String requestMethod, String payload, String contentType, String target, String userName, String password) - throws IOException { + public String doHttpRequest(String url, String requestMethod, String payload, String contentType, String target, + String userName, String password) throws IOException { URL urlObj = new URL(url); if (url.contains("https://")) { // Support for HTTPS return doHttpsQuery(urlObj, requestMethod, payload, contentType, target, userName, password); @@ -153,18 +154,4 @@ public class HttpConnectionManager { return doHttpQuery(urlObj, requestMethod, payload, contentType, target, userName, password); } } - - private String generateBasicAuth(String userName, String encodedPassword) { - String password = ""; - try { - password = CryptoUtils.decrypt(encodedPassword); - } catch (GeneralSecurityException e) { - logger.error("Unable to decrypt the password", e); - } catch (DecoderException e) { - logger.error("Exception caught when decoding the HEX String Key for encryption", e); - } - BASE64Encoder enc = new sun.misc.BASE64Encoder(); - String userpassword = userName + ":" + password; - return enc.encode( userpassword.getBytes() ); - } } diff --git a/src/main/resources/application-noaaf.properties b/src/main/resources/application-noaaf.properties index f54cbe09..82b2a283 100644 --- a/src/main/resources/application-noaaf.properties +++ b/src/main/resources/application-noaaf.properties @@ -206,7 +206,6 @@ clamp.config.dcae.intentory.retry.limit=5 clamp.config.dcae.dispatcher.url=http://dcae.api.simpledemo.onap.org:8188 clamp.config.dcae.dispatcher.retry.interval=20000 clamp.config.dcae.dispatcher.retry.limit=30 -clamp.config.dcae.header.requestId = X-ECOMP-RequestID #Define user permission related parameters, the permission type can be changed but MUST be redefined in clds-users.properties in that case ! clamp.config.security.permission.type.cl=org.onap.clamp.clds.cl diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3a704ebc..b8c63356 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -221,7 +221,6 @@ clamp.config.dcae.intentory.retry.limit=5 clamp.config.dcae.dispatcher.url=http://dcae.api.simpledemo.onap.org:8188 clamp.config.dcae.dispatcher.retry.interval=20000 clamp.config.dcae.dispatcher.retry.limit=30 -clamp.config.dcae.header.requestId = X-ECOMP-RequestID #Define user permission related parameters, the permission type can be changed but MUST be redefined in clds-users.properties in that case ! clamp.config.security.permission.type.cl=org.onap.clamp.clds.cl diff --git a/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java b/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java index 549c2d11..e6c634cb 100644 --- a/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java +++ b/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java @@ -85,7 +85,7 @@ public class DcaeDispatcherServicesTest { @Test public void shouldReturnDcaeOperationSataus() throws IOException { //given - Mockito.when(httpConnectionManager.doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null)) + Mockito.when(httpConnectionManager.doHttpRequest(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null)) .thenReturn(STATUS_RESPONSE_PROCESSING); //when String operationStatus = dcaeDispatcherServices.getOperationStatus(DEPLOYMENT_STATUS_URL); @@ -97,7 +97,7 @@ public class DcaeDispatcherServicesTest { @Test public void shouldTryMultipleTimesWhenProcessing() throws IOException, InterruptedException { //given - Mockito.when(httpConnectionManager.doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", + Mockito.when(httpConnectionManager.doHttpRequest(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null)) .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_ACTIVE); //when @@ -106,7 +106,7 @@ public class DcaeDispatcherServicesTest { //then Assertions.assertThat(operationStatus).isEqualTo("succeeded"); Mockito.verify(httpConnectionManager, Mockito.times(3)) - .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null); + .doHttpRequest(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null); } @@ -114,7 +114,7 @@ public class DcaeDispatcherServicesTest { public void shouldTryOnlyAsManyTimesAsConfigured() throws IOException, InterruptedException { //given Mockito.when(httpConnectionManager - .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null)) + .doHttpRequest(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null)) .thenReturn(STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING, STATUS_RESPONSE_PROCESSING); //when @@ -123,7 +123,7 @@ public class DcaeDispatcherServicesTest { //then Assertions.assertThat(operationStatus).isEqualTo("processing"); Mockito.verify(httpConnectionManager, Mockito.times(3)) - .doGeneralHttpQuery(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null); + .doHttpRequest(DEPLOYMENT_STATUS_URL, "GET", null, null, "DCAE", null, null); } @@ -136,7 +136,7 @@ public class DcaeDispatcherServicesTest { .thenReturn(new JsonObject()); Mockito.when(httpConnectionManager - .doGeneralHttpQuery(DCAE_URL + .doHttpRequest(DCAE_URL + "/dcae-deployments/closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId", "PUT", "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}", diff --git a/src/test/java/org/onap/clamp/clds/it/HttpConnectionManagerItCase.java b/src/test/java/org/onap/clamp/clds/it/HttpConnectionManagerItCase.java index 4db0e5c2..4c23eca0 100644 --- a/src/test/java/org/onap/clamp/clds/it/HttpConnectionManagerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/HttpConnectionManagerItCase.java @@ -110,7 +110,7 @@ public class HttpConnectionManagerItCase { @Test public void testHttpGet() throws Exception { String response = httpConnectionManager - .doGeneralHttpQuery("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null, "DCAE", null, null); + .doHttpRequest("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null, "DCAE", null, null); assertNotNull(response); // Should be a redirection so 302, so empty assertTrue(response.isEmpty()); @@ -119,7 +119,7 @@ public class HttpConnectionManagerItCase { @Test public void testHttpsGet() throws Exception { String response = httpConnectionManager - .doGeneralHttpQuery("https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null, "DCAE", null, null); + .doHttpRequest("https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null, "DCAE", null, null); assertNotNull(response); // Should contain something assertTrue(!response.isEmpty()); @@ -127,21 +127,21 @@ public class HttpConnectionManagerItCase { @Test(expected = BadRequestException.class) public void testHttpsGet404() throws IOException { - httpConnectionManager.doGeneralHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html", + httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/designer/index1.html", "GET", null, null, "DCAE", null, null); fail("Should have raised an BadRequestException"); } @Test(expected = BadRequestException.class) public void testHttpsPost404() throws IOException { - httpConnectionManager.doGeneralHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html", + httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/designer/index1.html", "POST", "", "application/json", "DCAE", null, null); fail("Should have raised an BadRequestException"); } @Test(expected = BadRequestException.class) public void testHttpException() throws IOException { - httpConnectionManager.doGeneralHttpQuery("http://localhost:" + this.httpsPort + "/designer/index.html", "GET", + httpConnectionManager.doHttpRequest("http://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null, "DCAE", null, null); fail("Should have raised an BadRequestException"); } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 99d5da8b..9f711e3e 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -212,7 +212,6 @@ clamp.config.dcae.intentory.retry.limit=1 clamp.config.dcae.dispatcher.url=http://localhost:${docker.http-cache.port.host} clamp.config.dcae.dispatcher.retry.interval=100 clamp.config.dcae.dispatcher.retry.limit=1 -clamp.config.dcae.header.requestId = X-ECOMP-RequestID #Define user permission related parameters, the permission type can be changed but MUST be redefined in clds-users.properties in that case ! clamp.config.security.permission.type.cl=permission-type-cl -- 2.16.6