Refactoring

Small refactoring of the Http library

Issue-ID: CLAMP-333
Change-Id: I90a5488241ab84636bc0cec5849e97c4f98f3a38
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
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 0f46595..f74af49 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 @@
         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 @@
         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 d8bfeea..dcf0542 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 @@
         }

         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 cc04ce5..e62874a 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 @@
 
     @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 @@
         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 @@
         this.svgRepresentation = svgRepresentation;
     }
 
-    public String getBlueprint() {
+    String getBlueprint() {
         return blueprint;
     }
 
@@ -167,11 +166,11 @@
         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 @@
         this.operationalPolicies = operationalPolicies;
     }
 
-    public Set<MicroServicePolicy> getMicroServicePolicies() {
+    Set<MicroServicePolicy> getMicroServicePolicies() {
         return microServicePolicies;
     }
 
@@ -222,7 +221,7 @@
         log.setLoop(this);
     }
 
-    public String getDcaeBlueprintId() {
+    String getDcaeBlueprintId() {
         return dcaeBlueprintId;
     }
 
@@ -230,7 +229,7 @@
         this.dcaeBlueprintId = dcaeBlueprintId;
     }
 
-    public JsonObject getModelPropertiesJson() {
+    JsonObject getModelPropertiesJson() {
         return modelPropertiesJson;
     }
 
@@ -240,13 +239,18 @@
 
     /**
      * 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 98%
rename from src/main/java/org/onap/clamp/operation/LoopOperation.java
rename to src/main/java/org/onap/clamp/loop/LoopOperation.java
index bdb4b3f..7def783 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.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 592338c..edce8ff 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 @@
             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 @@
             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 4e97f29..6459fa9 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 @@
 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 @@
     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 @@
         }
     }
 
-    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 @@
      * @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 @@
             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 f54cbe0..82b2a28 100644
--- a/src/main/resources/application-noaaf.properties
+++ b/src/main/resources/application-noaaf.properties
@@ -206,7 +206,6 @@
 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 3a704eb..b8c6335 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -221,7 +221,6 @@
 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 549c2d1..e6c634c 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 @@
     @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 @@
     @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 @@
         //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 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 @@
         //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 @@
                 .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 4db0e5c..4c23eca 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 @@
     @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 @@
     @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 @@
 
     @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 99d5da8..9f711e3 100644
--- a/src/test/resources/application.properties
+++ b/src/test/resources/application.properties
@@ -212,7 +212,6 @@
 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