[SO] Pending Create changes for SO-API and BPMN-INFRA to support CNF's through ASD
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / CnfmHttpServiceProviderImpl.java
index 3f78896..1035f43 100644 (file)
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
-
 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
 
 import static org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks.CnfmHttpServiceConfiguration.CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN;
+import java.net.URI;
 import java.util.Optional;
 import org.onap.so.cnfm.lcm.model.AsInstance;
+import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
+import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
 import org.onap.so.rest.exceptions.InvalidRestRequestException;
 import org.onap.so.rest.exceptions.RestProcessingException;
@@ -35,7 +37,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
-import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
 
 @Service
 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
@@ -83,24 +84,54 @@ public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
     }
 
     @Override
-    public void invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
+    public Optional<URI> invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
         try {
 
             final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
             final ResponseEntity<AsInstance> response =
                     httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
-
             final HttpStatus httpStatus = response.getStatusCode();
-            if (httpStatus.is2xxSuccessful() && !(response.hasBody())) {
-                LOGGER.error("Response received without body: {}", response);
+            if (httpStatus.is2xxSuccessful()) {
+                URI statusUri = response.getHeaders().getLocation();
+                if (statusUri == null) {
+                    LOGGER.error("Received response without status URL for instance ID: {}", asInstanceId);
+                    return Optional.empty();
+                }
+                return Optional.of(statusUri);
             }
             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
+            return Optional.empty();
 
         } catch (final RestProcessingException | InvalidRestRequestException
                 | HttpResouceNotFoundException httpInvocationException) {
             LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
+            return Optional.empty();
         }
 
     }
 
+    @Override
+    public Optional<AsLcmOpOcc> getInstantiateOperationJobStatus(final String url) {
+        try {
+            final ResponseEntity<AsLcmOpOcc> response = httpServiceProvider.getHttpResponse(url, AsLcmOpOcc.class);
+
+            final HttpStatus httpStatus = response.getStatusCode();
+
+            if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
+                LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
+                return Optional.empty();
+            }
+
+            if (!response.hasBody()) {
+                LOGGER.error("CNFM status response recieved without body: {}", response);
+                return Optional.empty();
+            }
+            return Optional.of(response.getBody());
+        } catch (final RestProcessingException | InvalidRestRequestException
+                | HttpResouceNotFoundException httpInvocationException) {
+            LOGGER.error("Unexpected error while processing job request", httpInvocationException);
+            throw httpInvocationException;
+        }
+    }
+
 }