Refactoring 07/83707/4
authorsebdet <sebastien.determe@intl.att.com>
Fri, 29 Mar 2019 10:52:02 +0000 (11:52 +0100)
committersebdet <sebastien.determe@intl.att.com>
Fri, 29 Mar 2019 11:00:26 +0000 (12:00 +0100)
Small refactoring of the Http library

Issue-ID: CLAMP-333
Change-Id: I90a5488241ab84636bc0cec5849e97c4f98f3a38
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java
src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java
src/main/java/org/onap/clamp/loop/Loop.java
src/main/java/org/onap/clamp/loop/LoopOperation.java [moved from src/main/java/org/onap/clamp/operation/LoopOperation.java with 99% similarity]
src/main/java/org/onap/clamp/policy/PolicyOperation.java
src/main/java/org/onap/clamp/util/HttpConnectionManager.java
src/main/resources/application-noaaf.properties
src/main/resources/application.properties
src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java
src/test/java/org/onap/clamp/clds/it/HttpConnectionManagerItCase.java
src/test/resources/application.properties

index 0f46595..f74af49 100644 (file)
@@ -99,7 +99,7 @@ public class DcaeDispatcherServices {
         Date startTime = new Date();\r
         LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
         try {\r
-            String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(statusUrl, "GET", null, null, "DCAE", null, null);\r
+            String responseStr = dcaeHttpConnectionManager.doHttpRequest(statusUrl, "GET", null, null, "DCAE", null, null);\r
             JSONObject jsonObj = parseResponse(responseStr);\r
             String operationType = (String) jsonObj.get("operationType");\r
             String status = (String) jsonObj.get(DCAE_STATUS_FIELD);\r
@@ -190,7 +190,7 @@ public class DcaeDispatcherServices {
         String nodeAttr) throws IOException, ParseException {\r
         Date startTime = new Date();\r
         try {\r
-            String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(url, requestMethod, payload, contentType, "DCAE", null, null);\r
+            String responseStr = dcaeHttpConnectionManager.doHttpRequest(url, requestMethod, payload, contentType, "DCAE", null, null);\r
             JSONObject jsonObj = parseResponse(responseStr);\r
             JSONObject linksObj = (JSONObject) jsonObj.get(node);\r
             String statusUrl = (String) linksObj.get(nodeAttr);\r
index d8bfeea..dcf0542 100644 (file)
@@ -207,7 +207,7 @@ public class DcaeInventoryServices {
         }\r
         for (int i = 0; i < retryLimit; i++) {\r
             metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");\r
-            String response = httpConnectionManager.doGeneralHttpQuery(fullUrl, "GET", null, null, "DCAE", null, null);\r
+            String response = httpConnectionManager.doHttpRequest(fullUrl, "GET", null, null, "DCAE", null, null);\r
             int totalCount = getTotalCountFromDcaeInventoryResponse(response);\r
             metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount);\r
             if (totalCount > 0) {\r
index cc04ce5..e62874a 100644 (file)
@@ -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<MicroServicePolicy> 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<MicroServicePolicy> getMicroServicePolicies() {
+    Set<MicroServicePolicy> 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", ""));
@@ -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;
index 592338c..edce8ff 100644 (file)
@@ -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);
         }
     }
 
index 4e97f29..6459fa9 100644 (file)
@@ -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() );
-    }
 }
index f54cbe0..82b2a28 100644 (file)
@@ -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
index 3a704eb..b8c6335 100644 (file)
@@ -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
index 549c2d1..e6c634c 100644 (file)
@@ -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\":{}}",
index 4db0e5c..4c23eca 100644 (file)
@@ -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");
     }
index 99d5da8..9f711e3 100644 (file)
@@ -212,7 +212,6 @@ clamp.config.dcae.intentory.retry.limit=1
 clamp.config.dcae.dispatcher.url=http://localhost:${docker.http-cache.port.host}\r
 clamp.config.dcae.dispatcher.retry.interval=100\r
 clamp.config.dcae.dispatcher.retry.limit=1\r
-clamp.config.dcae.header.requestId = X-ECOMP-RequestID\r
 \r
 #Define user permission related parameters, the permission type can be changed but MUST be redefined in clds-users.properties in that case !\r
 clamp.config.security.permission.type.cl=permission-type-cl\r