Code & Workflow Enhancements for CNF - Upgrade
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / adapter / cnf / CnfAdapterClient.java
index f44c15a..82c0e49 100644 (file)
@@ -27,6 +27,8 @@ import javax.ws.rs.core.UriBuilder;
 import org.apache.http.HttpStatus;
 import org.onap.so.client.adapter.cnf.entities.InstanceRequest;
 import org.onap.so.client.adapter.cnf.entities.InstanceResponse;
+import org.onap.so.client.adapter.cnf.entities.UpgradeInstanceResponse;
+import org.onap.so.client.adapter.cnf.entities.UpgradeInstanceRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -34,6 +36,7 @@ import org.springframework.core.env.Environment;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.retry.annotation.Backoff;
 import org.springframework.retry.annotation.Retryable;
@@ -53,14 +56,14 @@ public class CnfAdapterClient {
     @Autowired
     private Environment env;
 
-    private static final String INSTANCE_CREATE_PATH = "/api/multicloud-k8s/v1/v1/instance";
+    private static final String INSTANCE_CREATE_PATH = "/api/cnf-adapter/v1/instance";
 
     @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
     public InstanceResponse createVfModule(InstanceRequest request) throws CnfAdapterClientException {
         try {
             // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
             // for configuration
-            String uri = "https://localhost:32780"; // TODO: What is the correct uri?
+            String uri = "http://so-cnf-adapter:8090";
             String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString();
             HttpEntity<?> entity = getHttpEntity(request);
             ResponseEntity<InstanceResponse> result =
@@ -75,11 +78,71 @@ public class CnfAdapterClient {
         }
     }
 
+
+    @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
+    public void deleteVfModule(String heatStackId) throws CnfAdapterClientException {
+        try {
+            // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
+            // for configuration
+            String uri = "http://so-cnf-adapter:8090";
+            String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH + "/" + heatStackId).build().toString();
+            HttpEntity<?> entity = new HttpEntity<>(getHttpHeaders());
+            restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class);
+        } catch (HttpClientErrorException e) {
+            logger.error("Error Calling CNF Adapter, e");
+            if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
+                throw new EntityNotFoundException(e.getResponseBodyAsString());
+            }
+            throw e;
+        }
+    }
+
+    @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
+    public InstanceResponse healthcheck() throws CnfAdapterClientException {
+        try {
+            // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well
+            // for configuration
+            String uri = "http://so-cnf-adapter:8090";
+            String endpoint = UriBuilder.fromUri(uri).path("/api/cnf-adapter/v1/healthcheck").build().toString();
+            HttpEntity<?> entity = new HttpEntity<>(getHttpHeaders());
+            ResponseEntity<InstanceResponse> result =
+                    restTemplate.exchange(endpoint, HttpMethod.GET, entity, InstanceResponse.class);
+            return result.getBody();
+        } catch (HttpClientErrorException e) {
+            logger.error("Error Calling CNF Adapter, e");
+            if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
+                throw new EntityNotFoundException(e.getResponseBodyAsString());
+            }
+            throw e;
+        }
+    }
+
+    @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
+    public UpgradeInstanceResponse upgradeVfModule(UpgradeInstanceRequest request, String heatStackId)
+            throws CnfAdapterClientException {
+        try {
+            String uri = "http://so-cnf-adapter:8090";
+            String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH + "/" + heatStackId + "/upgrade")
+                    .build().toString();
+            HttpEntity<?> entity = getHttpEntity(request);
+            ResponseEntity<UpgradeInstanceResponse> result =
+                    restTemplate.exchange(endpoint, HttpMethod.POST, entity, UpgradeInstanceResponse.class);
+            return result.getBody();
+        } catch (HttpClientErrorException e) {
+            logger.error("Error Calling CNF Adapter, e");
+            if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
+                throw new EntityNotFoundException(e.getResponseBodyAsString());
+            }
+            throw e;
+        }
+    }
+
     protected HttpHeaders getHttpHeaders() {
         HttpHeaders headers = new HttpHeaders();
-        List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>();
-        acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON);
+        List<MediaType> acceptableMediaTypes = new ArrayList<>();
+        acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
         headers.setAccept(acceptableMediaTypes);
+        headers.setContentType(MediaType.APPLICATION_JSON);
         /*
          * try { String userCredentials = CryptoUtils.decrypt(env.getRequiredProperty("mso.cnf.adapter.auth"),
          * env.getRequiredProperty("mso.msoKey")); if (userCredentials != null) { headers.add(HttpHeaders.AUTHORIZATION,
@@ -94,4 +157,8 @@ public class CnfAdapterClient {
         return new HttpEntity<>(request, headers);
     }
 
+    protected HttpEntity<?> getHttpEntity(UpgradeInstanceRequest request) {
+        HttpHeaders headers = getHttpHeaders();
+        return new HttpEntity<>(request, headers);
+    }
 }