Multicloud URL refactoring 93/122593/5
authorGrzegorz Wielgosinski <g.wielgosins@samsung.com>
Tue, 13 Jul 2021 11:23:41 +0000 (13:23 +0200)
committerGrzegorz Wielgosinski <g.wielgosins@samsung.com>
Wed, 14 Jul 2021 14:08:49 +0000 (16:08 +0200)
Issue-ID: SO-3711

Signed-off-by: Grzegorz Wielgosinski <g.wielgosins@samsung.com>
Change-Id: Id1ee3f40c703b480d1296e657e1f7977cdd60f08

so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/MulticloudConfiguration.java [new file with mode: 0644]
so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/rest/CnfAdapterRest.java
so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/service/CnfAdapterService.java
so-cnf-adapter-application/src/main/resources/application.yaml
so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/CnfAdapterRestTest.java [deleted file]
so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/rest/CnfAdapterRestTest.java
so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/service/CnfAdapterServiceTest.java

diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/MulticloudConfiguration.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/MulticloudConfiguration.java
new file mode 100644 (file)
index 0000000..6107681
--- /dev/null
@@ -0,0 +1,20 @@
+package org.onap.so.adapters.cnf;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class MulticloudConfiguration {
+
+    @Value("${multicloud.protocol}")
+    private String protocol;
+    @Value("${multicloud.host}")
+    private String host;
+    @Value("${multicloud.port}")
+    private Integer port;
+
+    public String getMulticloudUrl() {
+        return protocol + "://" + host + ":" + port;
+    }
+}
index 430a5e5..2036f46 100644 (file)
@@ -36,6 +36,7 @@ import org.apache.http.entity.mime.content.FileBody;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
+import org.onap.so.adapters.cnf.MulticloudConfiguration;
 import org.onap.so.adapters.cnf.model.BpmnInstanceRequest;
 import org.onap.so.adapters.cnf.model.ConfigTemplateEntity;
 import org.onap.so.adapters.cnf.model.ConfigurationEntity;
@@ -66,10 +67,14 @@ public class CnfAdapterRest {
 
     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterRest.class);
     private final CloseableHttpClient httpClient = HttpClients.createDefault();
-    private String uri = "http://multicloud-k8s:9015";
+    private final CnfAdapterService cnfAdapterService;
+    private final String uri;
 
     @Autowired
-    private CnfAdapterService cnfAdapterService;
+    public CnfAdapterRest(CnfAdapterService cnfAdapterService, MulticloudConfiguration multicloudConfiguration) {
+        this.cnfAdapterService = cnfAdapterService;
+        this.uri = multicloudConfiguration.getMulticloudUrl();
+    }
 
     @ResponseBody
     @RequestMapping(value = {"/api/cnf-adapter/v1/healthcheck"}, method = RequestMethod.GET,
index bfb84c3..b50658f 100644 (file)
@@ -26,6 +26,7 @@ import java.util.List;
 import javax.persistence.EntityNotFoundException;
 import javax.ws.rs.core.UriBuilder;
 import org.apache.http.HttpStatus;
+import org.onap.so.adapters.cnf.MulticloudConfiguration;
 import org.onap.so.adapters.cnf.model.BpmnInstanceRequest;
 import org.onap.so.adapters.cnf.model.MulticloudInstanceRequest;
 import org.slf4j.Logger;
@@ -46,11 +47,19 @@ import com.fasterxml.jackson.databind.JsonMappingException;
 @Service
 public class CnfAdapterService {
     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterService.class);
-    @Autowired
-    private RestTemplate restTemplate;
     private static final String INSTANCE_CREATE_PATH = "/v1/instance";
     private static final String HEALTH_CHECK = "/v1/healthcheck";
 
+    private final RestTemplate restTemplate;
+    private final String uri;
+
+    @Autowired
+    public CnfAdapterService(RestTemplate restTemplate,
+                             MulticloudConfiguration multicloudConfiguration) {
+        this.restTemplate = restTemplate;
+        this.uri = multicloudConfiguration.getMulticloudUrl();
+    }
+
     public String healthCheck() {
 
         logger.info("CnfAdapterService healthCheck called");
@@ -60,7 +69,6 @@ public class CnfAdapterService {
             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
             // This needs to be added as well
             // for configuration
-            String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
             String endpoint = UriBuilder.fromUri(uri).path(HEALTH_CHECK).build().toString();
             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
             result = restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, String.class);
@@ -103,7 +111,6 @@ public class CnfAdapterService {
             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
             // This needs to be added as well
             // for configuration
-            String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString();
             HttpEntity<?> entity = getHttpEntity(multicloudInstanceRequest);
             instanceResponse = restTemplate.exchange(endpoint, HttpMethod.POST, entity, String.class);
@@ -130,7 +137,6 @@ public class CnfAdapterService {
             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
             // This needs to be added as well
             // for configuration
-            String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
             String path = "/v1/instance/" + instanceId;
             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
@@ -158,7 +164,6 @@ public class CnfAdapterService {
             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
             // This needs to be added as well
             // for configuration
-            String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
             String path = "/v1/instance/" + instanceId + "/status";
             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
@@ -181,7 +186,6 @@ public class CnfAdapterService {
         logger.info("CnfAdapterService getInstanceQueryByInstanceId called");
         ResponseEntity<String> instanceResponse = null;
         try {
-            String uri = "http://multicloud-k8s:9015";
             String path = "/v1/instance/" + instanceId + "/query";
             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
@@ -205,7 +209,6 @@ public class CnfAdapterService {
             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
             // This needs to be added as well
             // for configuration
-            String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
             String path =
                     "/v1/instance" + "?rb-name=" + rbName + "&rb-version=" + rbVersion + "&profile-name=" + profileName;
             String endPoint = uri + path;
@@ -234,7 +237,6 @@ public class CnfAdapterService {
             // String uri = env.getRequiredProperty("multicloud.endpoint"); //TODO:
             // This needs to be added as well
             // for configuration
-            String uri = "http://multicloud-k8s:9015"; // TODO: What is the correct uri?
             String path = "/v1/instance/" + instanceId;
             String endpoint = UriBuilder.fromUri(uri).path(path).build().toString();
             HttpEntity<?> requestEntity = new HttpEntity<>(getHttpHeaders());
index 5a9adbf..50e667a 100644 (file)
@@ -42,6 +42,11 @@ server:
   tomcat:
     max-threads: 50
 
+multicloud:
+  protocol: "http"
+  host: "multicloud-k8s"
+  port: 9015
+
 #mso:
  # key: 07a7159d3bf51a0e53be7a8f89699be7
  # site-name: localSite
diff --git a/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/CnfAdapterRestTest.java b/so-cnf-adapter-application/src/test/java/org/onap/so/adapters/cnf/CnfAdapterRestTest.java
deleted file mode 100644 (file)
index ee7a771..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP - SO
- * ================================================================================
- * Copyright (C) 2020 Huawei Technologies Co., Ltd. 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.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
-
-package org.onap.so.adapters.cnf;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import java.util.HashMap;
-import java.util.Map;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.onap.so.adapters.cnf.model.BpmnInstanceRequest;
-import org.onap.so.adapters.cnf.rest.CnfAdapterRest;
-import org.springframework.test.context.junit4.SpringRunner;
-
-@RunWith(SpringRunner.class)
-public class CnfAdapterRestTest {
-
-    @InjectMocks
-    CnfAdapterRest cnfAdapterRest;
-
-    @Test
-    public void createInstanceTest() throws Exception {
-
-        Map<String, String> labels = new HashMap<String, String>();
-        labels.put("custom-label-1", "label1");
-        Map<String, String> overrideValues = new HashMap<String, String>();
-        labels.put("image.tag", "latest");
-        labels.put("dcae_collector_ip", "1.2.3.4");
-        BpmnInstanceRequest bpmnInstanceRequest = new BpmnInstanceRequest();
-        bpmnInstanceRequest.setCloudRegionId("v1");
-        bpmnInstanceRequest.setLabels(labels);
-        bpmnInstanceRequest.setModelInvariantId("krd");
-        bpmnInstanceRequest.setModelVersionId("p1");
-        bpmnInstanceRequest.setOverrideValues(overrideValues);
-        bpmnInstanceRequest.setVfModuleUUID("20200824");
-
-        String mockedResponse = "K8sRBProfileName is required";
-        String actualResponse = cnfAdapterRest.createInstance(bpmnInstanceRequest);
-        assertNotNull(actualResponse);
-        assertEquals(mockedResponse, actualResponse);
-    }
-
-}
-*/
index cfd6ba1..979f13b 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
+import org.onap.so.adapters.cnf.MulticloudConfiguration;
 import org.onap.so.adapters.cnf.model.*;
 import org.onap.so.adapters.cnf.service.CnfAdapterService;
 import org.springframework.http.HttpStatus;
@@ -49,6 +50,9 @@ public class CnfAdapterRestTest {
     @Mock
     CnfAdapterService cnfAdapterService;
 
+    @Mock
+    private MulticloudConfiguration multicloudConfiguration;
+
     @Mock
     ResponseEntity<InstanceMiniResponseList> instacneMiniResponseList;
 
index 130f6f0..bdad347 100644 (file)
@@ -24,6 +24,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
+import org.onap.so.adapters.cnf.MulticloudConfiguration;
 import org.onap.so.adapters.cnf.model.BpmnInstanceRequest;
 import org.springframework.http.ResponseEntity;
 import org.springframework.test.context.junit4.SpringRunner;
@@ -33,7 +34,6 @@ import java.util.HashMap;
 import java.util.Map;
 
 @RunWith(SpringRunner.class)
-
 public class CnfAdapterServiceTest {
     private static final String INSTANCE_CREATE_PATH = "/v1/instance";
     @Mock
@@ -42,6 +42,9 @@ public class CnfAdapterServiceTest {
     @InjectMocks
     CnfAdapterService cnfAdapterService;
 
+    @Mock
+    private MulticloudConfiguration multicloudConfiguration;
+
     @Mock
     ResponseEntity<String> instanceResponse;