Send terminate VNF to VNFM 56/84356/1
authorMichaelMorris <michael.morris@est.tech>
Fri, 5 Apr 2019 14:40:27 +0000 (14:40 +0000)
committerMichaelMorris <michael.morris@est.tech>
Fri, 5 Apr 2019 14:40:27 +0000 (14:40 +0000)
Issue-ID: SO-1633
Change-Id: I2d1010496725a47af7af108a681e1c16c749126c
Signed-off-by: MichaelMorris <michael.morris@est.tech>
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiHelper.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmServiceProvider.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmServiceProviderImpl.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/jobmanagement/JobManager.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/lifecycle/LifecycleManager.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/VnfmAdapterController.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/JobNotFoundException.java [new file with mode: 0644]
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/VnfNotFoundException.java [new file with mode: 0644]
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/VnfmNotFoundException.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/VnfmRequestFailureException.java
adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/VnfmAdapterControllerTest.java

index bacbea1..7795106 100644 (file)
@@ -95,11 +95,21 @@ public class AaiHelper {
      * @return the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
      */
     public EsrVnfm getAssignedVnfm(final GenericVnf vnf) {
-        final Relationship relationship = getRelationship(vnf, "esr-vnfm");
-        final String vnfmId = getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
+        final String vnfmId = getIdOfAssignedVnfm(vnf);
         return vnfmId == null ? null : aaiServiceProvider.invokeGetVnfm(vnfmId);
     }
 
+    /**
+     * Get the ID of the VNFM assigned for use for the given generic VNF.
+     *
+     * @param vnf the generic VNF
+     * @return the ID of the VNFM to use, or <code>null</code> if no VNFM has been assigned yet
+     */
+    public String getIdOfAssignedVnfm(final GenericVnf vnf) {
+        final Relationship relationship = getRelationship(vnf, "esr-vnfm");
+        return getRelationshipKey(relationship, "esr-vnfm.vnfm-id");
+    }
+
     /**
      * Get the tenant assigned for use for the given generic VNF.
      *
index aaf7e46..8af6889 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.base.Optional;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
 
 /**
  * Provides methods for invoking REST calls to a VNFM.
@@ -47,6 +48,15 @@ public interface VnfmServiceProvider {
      */
     String instantiateVnf(final String vnfSelfLink, final InstantiateVnfRequest instantiateVnfRequest);
 
+    /**
+     * Invoke a terminate request for a VNF.
+     *
+     * @param vnfSelfLink the link to he VNF on the VNFM
+     * @param terminateVnfRequest the terminate request
+     * @return the operation ID of the termination operation
+     */
+    String terminateVnf(final String vnfSelfLink, final TerminateVnfRequest terminateVnfRequest);
+
     /**
      * Invoke a get request for a VNFM operation.
      *
index 4a2c7a9..d447bef 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.base.Optional;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
 import org.onap.so.rest.service.HttpRestServiceProvider;
 import org.slf4j.Logger;
@@ -56,13 +57,50 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
     @Override
     public String instantiateVnf(final String vnfSelfLink, final InstantiateVnfRequest instantiateVnfRequest) {
         logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
-        final ResponseEntity<Void> response = httpServiceProvider.getHttpResponse(vnfSelfLink, Void.class);
+
+        ResponseEntity<Void> response = null;
+        try {
+            response = httpServiceProvider.postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
+                    Void.class);
+        } catch (final Exception exception) {
+            final String errorMessage =
+                    "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
+            logger.error(errorMessage, exception);
+            throw new VnfmRequestFailureException(errorMessage, exception);
+        }
         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
-            throw new VnfmRequestFailureException("Instantiate request to " + vnfSelfLink + " return status code: "
-                    + response.getStatusCode() + ", request: " + instantiateVnfRequest);
+            final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
+                    + response.getStatusCode() + ", request: " + instantiateVnfRequest;
+            logger.error(errorMessage);
+            throw new VnfmRequestFailureException(errorMessage);
         }
         final String locationHeader = response.getHeaders().get("Location").iterator().next();
         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
+
+    }
+
+    @Override
+    public String terminateVnf(final String vnfSelfLink, final TerminateVnfRequest terminateVnfRequest) {
+        logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
+
+        ResponseEntity<Void> response = null;
+        try {
+            response = httpServiceProvider.postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate", Void.class);
+        } catch (final Exception exception) {
+            final String errorMessage =
+                    "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
+            logger.error(errorMessage, exception);
+            throw new VnfmRequestFailureException(errorMessage, exception);
+        }
+        if (response.getStatusCode() != HttpStatus.ACCEPTED) {
+            final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
+                    + response.getStatusCode() + ", request: " + terminateVnfRequest;
+            logger.error(errorMessage);
+            throw new VnfmRequestFailureException(errorMessage);
+        }
+        final String locationHeader = response.getHeaders().get("Location").iterator().next();
+        return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
+
     }
 
     @Override
index ac11bce..97a945c 100644 (file)
@@ -27,6 +27,7 @@ import java.util.Map;
 import java.util.UUID;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.JobNotFoundException;
 import org.onap.vnfmadapter.v1.model.OperationEnum;
 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
 import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum;
@@ -82,7 +83,7 @@ public class JobManager {
         final QueryJobResponse response = new QueryJobResponse();
 
         if (vnfmOperation == null) {
-            return null;
+            throw new JobNotFoundException("No job found with ID: " + jobId);
         }
 
         final Optional<InlineResponse200> operationOptional =
index 4bedb47..9249b9e 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.so.adapters.vnfmadapter.lifecycle;
 
 import com.google.common.base.Optional;
-import java.util.UUID;
 import org.onap.aai.domain.yang.EsrVnfm;
 import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
@@ -30,7 +29,11 @@ import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmHelper;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
+import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest.TerminationTypeEnum;
 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfNotFoundException;
+import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
@@ -87,12 +90,6 @@ public class LifecycleManager {
         return response;
     }
 
-    private GenericVnf getGenericVnfFromAai(final String vnfIdInAai) {
-        final GenericVnf genericVnf = aaiServiceProvider.invokeGetGenericVnf(vnfIdInAai);
-        logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
-        return genericVnf;
-    }
-
     private void checkIfVnfAlreadyExistsInVnfm(final GenericVnf genericVnf) {
         if (genericVnf.getSelflink() != null && !genericVnf.getSelflink().isEmpty()) {
             Optional<InlineResponse201> response = Optional.absent();
@@ -132,11 +129,35 @@ public class LifecycleManager {
      * @return the response to the request
      */
     public DeleteVnfResponse deleteVnf(final String vnfIdInAai) {
-        // vnfm ID and operation ID set to random value for now, will be set correctly once we implement
-        // terminate call towards the VNFM
-        final String jobId = jobManager.createJob(UUID.randomUUID().toString(), UUID.randomUUID().toString(), true);
-        final DeleteVnfResponse response = new DeleteVnfResponse();
-        response.setJobId(jobId);
-        return response;
+        final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
+        final String vnfmId = getIdOfAssignedVnfm(genericVnf);
+
+        final String operationId = sendTerminateRequestToVnfm(genericVnf);
+        final String jobId = jobManager.createJob(vnfmId, operationId, true);
+
+        return new DeleteVnfResponse().jobId(jobId);
+    }
+
+    private String sendTerminateRequestToVnfm(final GenericVnf genericVnf) {
+        final TerminateVnfRequest terminateVnfRequest = new TerminateVnfRequest();
+        terminateVnfRequest.setTerminationType(TerminationTypeEnum.FORCEFUL);
+        return vnfmServiceProvider.terminateVnf(genericVnf.getSelflink(), terminateVnfRequest);
+    }
+
+    private GenericVnf getGenericVnfFromAai(final String vnfIdInAai) {
+        final GenericVnf genericVnf = aaiServiceProvider.invokeGetGenericVnf(vnfIdInAai);
+        if (genericVnf == null) {
+            throw new VnfNotFoundException("VNF not found in AAI: " + vnfIdInAai);
+        }
+        logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
+        return genericVnf;
+    }
+
+    private String getIdOfAssignedVnfm(final GenericVnf genericVnf) {
+        final String vnfmId = aaiHelper.getIdOfAssignedVnfm(genericVnf);
+        if (vnfmId == null) {
+            throw new VnfmNotFoundException("No VNFM found in AAI for VNF " + genericVnf.getVnfId());
+        }
+        return vnfmId;
     }
 }
index 055b8e0..65bc450 100644 (file)
@@ -87,9 +87,12 @@ public class VnfmAdapterController {
 
         logger.info("REST request vnfCreate with body: {}", createVnfRequest);
 
-        final CreateVnfResponse createVnfResponse = lifecycleManager.createVnf(vnfId, createVnfRequest);
-        clearLoggingMDCs();
-        return new ResponseEntity<>(createVnfResponse, HttpStatus.ACCEPTED);
+        try {
+            final CreateVnfResponse createVnfResponse = lifecycleManager.createVnf(vnfId, createVnfRequest);
+            return new ResponseEntity<>(createVnfResponse, HttpStatus.ACCEPTED);
+        } finally {
+            clearLoggingMDCs();
+        }
     }
 
     @DeleteMapping(value = "/vnfs/{vnfId}")
@@ -113,9 +116,12 @@ public class VnfmAdapterController {
 
         logger.info("REST request vnfDelete for VNF: {}", vnfId);
 
-        final DeleteVnfResponse response = lifecycleManager.deleteVnf(vnfId);
-        clearLoggingMDCs();
-        return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
+        try {
+            final DeleteVnfResponse response = lifecycleManager.deleteVnf(vnfId);
+            return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
+        } finally {
+            clearLoggingMDCs();
+        }
     }
 
     @GetMapping(value = "/jobs/{jobId}")
@@ -136,12 +142,12 @@ public class VnfmAdapterController {
 
         setLoggingMDCs(requestId, partnerName, invocationId);
 
-        final QueryJobResponse response = jobManager.getVnfmOperation(jobId);
-        if (response == null) {
-            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+        try {
+            final QueryJobResponse response = jobManager.getVnfmOperation(jobId);
+            return new ResponseEntity<>(response, HttpStatus.OK);
+        } finally {
+            clearLoggingMDCs();
         }
-        return new ResponseEntity<>(response, HttpStatus.OK);
-
     }
 
 
diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/JobNotFoundException.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/JobNotFoundException.java
new file mode 100644 (file)
index 0000000..1fd3902
--- /dev/null
@@ -0,0 +1,39 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.adapters.vnfmadapter.rest.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ * Exception for job not found.
+ */
+@ResponseStatus(HttpStatus.NOT_FOUND)
+public class JobNotFoundException extends RuntimeException {
+
+    private static final long serialVersionUID = 6398018034431666933L;
+
+    public JobNotFoundException(final String message) {
+        super(message);
+    }
+
+}
+
diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/VnfNotFoundException.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/VnfNotFoundException.java
new file mode 100644 (file)
index 0000000..1413f99
--- /dev/null
@@ -0,0 +1,39 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.adapters.vnfmadapter.rest.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ * Exception for VNF not found.
+ */
+@ResponseStatus(HttpStatus.NOT_FOUND)
+public class VnfNotFoundException extends RuntimeException {
+
+    private static final long serialVersionUID = 6398018034431666933L;
+
+    public VnfNotFoundException(final String message) {
+        super(message);
+    }
+
+}
+
index 100aa17..1ce6cca 100644 (file)
 
 package org.onap.so.adapters.vnfmadapter.rest.exceptions;
 
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
 /**
  * Exception for VNFM not found.
  */
+@ResponseStatus(HttpStatus.BAD_REQUEST)
 public class VnfmNotFoundException extends RuntimeException {
 
     private static final long serialVersionUID = 6398018034431666933L;
index ae2e280..64ce25c 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.so.adapters.vnfmadapter.rest;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.verify;
@@ -34,7 +35,6 @@ import java.net.URI;
 import java.util.Optional;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
-import org.hamcrest.core.StringStartsWith;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -123,7 +123,7 @@ public class VnfmAdapterControllerTest {
         setUpVnfmsInMockAai();
         setUpVimInMockAai();
 
-        mockRestServer.expect(requestTo("http://dummy.value/until/create/implememted/vnfId"))
+        mockRestServer.expect(requestTo("http://dummy.value/until/create/implememted/vnfId/instantiate"))
                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
 
@@ -184,13 +184,8 @@ public class VnfmAdapterControllerTest {
         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
 
-        final GenericVnf genericVnf = new GenericVnf();
-        genericVnf.setVnfId("myTestVnfId");
-        genericVnf.setNfType("vnfmType1");
-        genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
-
-        doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class),
-                MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId")));
+        final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType1");
+        addSelfLinkToGenericVnf(genericVnf);
 
         final InlineResponse201 reponse = new InlineResponse201();
         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
@@ -215,11 +210,12 @@ public class VnfmAdapterControllerTest {
         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
 
-        setUpGenericVnfWithVnfmRelationshipInMockAai("vnfmType2", "vnfm1");
+        final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType2");
+        addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
         setUpVnfmsInMockAai();
         setUpVimInMockAai();
 
-        mockRestServer.expect(requestTo("http://dummy.value/until/create/implememted/vnfId"))
+        mockRestServer.expect(requestTo("http://dummy.value/until/create/implememted/vnfId/instantiate"))
                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
 
@@ -248,8 +244,31 @@ public class VnfmAdapterControllerTest {
     @Test
     public void deleteVnf_ValidRequest_Returns202AndJobId() throws Exception {
         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
+
+        final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
+        addSelfLinkToGenericVnf(genericVnf);
+        addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
+
+        mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
+                .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
+                        .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/1234567")));
+
+        final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
+                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
+                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
+        mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
+                .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
+
+
+        final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
+                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
+                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
+        mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
+                .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
+
+
         final RequestEntity<Void> request = RequestEntity
-                .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
+                .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
@@ -268,19 +287,6 @@ public class VnfmAdapterControllerTest {
         doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/...")));
 
-        final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
-                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
-                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
-        mockRestServer.expect(requestTo(new StringStartsWith("http://vnfm:8080/vnf_lcm_op_occs")))
-                .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
-
-
-        final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
-                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
-                org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
-        mockRestServer.expect(requestTo(new StringStartsWith("http://vnfm:8080/vnf_lcm_op_occs")))
-                .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
-
 
         final ResponseEntity<QueryJobResponse> firstJobQueryResponse =
                 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
@@ -297,6 +303,59 @@ public class VnfmAdapterControllerTest {
         assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime());
     }
 
+    @Test
+    public void deleteVnf_GenericVnfNotFound_Returns404() throws Exception {
+        final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
+
+        final RequestEntity<Void> request = RequestEntity
+                .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myNonExistingVnfId"))
+                .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
+                .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
+        final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
+                restTemplate.exchange(request, DeleteVnfResponse.class);
+        assertEquals(404, deleteVnfResponse.getStatusCode().value());
+        assertNull(deleteVnfResponse.getBody().getJobId());
+    }
+
+    @Test
+    public void deleteVnf_NoAssignedVnfm_Returns400() throws Exception {
+        final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
+
+        setUpGenericVnfInMockAai("vnfmType");
+
+        final RequestEntity<Void> request = RequestEntity
+                .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
+                .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
+                .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
+        final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
+                restTemplate.exchange(request, DeleteVnfResponse.class);
+        assertEquals(400, deleteVnfResponse.getStatusCode().value());
+        assertNull(deleteVnfResponse.getBody().getJobId());
+    }
+
+    @Test
+    public void deleteVnf_ErrorStatusCodeFromVnfm_Returns500() throws Exception {
+        final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
+
+        final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
+        addSelfLinkToGenericVnf(genericVnf);
+        addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
+
+        mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
+                .andRespond(withStatus(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON));
+
+
+        final RequestEntity<Void> request = RequestEntity
+                .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
+                .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
+                .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
+        final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
+                restTemplate.exchange(request, DeleteVnfResponse.class);
+        assertEquals(500, deleteVnfResponse.getStatusCode().value());
+        assertNull(deleteVnfResponse.getBody().getJobId());
+
+    }
+
     private InlineResponse200 createOperationQueryResponse(
             final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum operation,
             final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum operationState) {
@@ -317,16 +376,19 @@ public class VnfmAdapterControllerTest {
         return genericVnf;
     }
 
-    private void setUpGenericVnfInMockAai(final String type) {
+    private GenericVnf setUpGenericVnfInMockAai(final String type) {
         final GenericVnf genericVnf = createGenericVnf(type);
 
         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class),
                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId")));
+        return genericVnf;
     }
 
-    private void setUpGenericVnfWithVnfmRelationshipInMockAai(final String type, final String vnfmId) {
-        final GenericVnf genericVnf = createGenericVnf(type);
+    private void addSelfLinkToGenericVnf(final GenericVnf vnf) {
+        vnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
+    }
 
+    private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
         final Relationship relationshipToVnfm = new Relationship();
         relationshipToVnfm.setRelatedLink(
                 "/aai/v15/external-system/esr-vnfm-li//        final InlineResponse201 vnfInstance = new InlineResponse201();\n"
@@ -343,9 +405,6 @@ public class VnfmAdapterControllerTest {
         final RelationshipList relationshipList = new RelationshipList();
         relationshipList.getRelationship().add(relationshipToVnfm);
         genericVnf.setRelationshipList(relationshipList);
-
-        doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class),
-                MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId")));
     }
 
     private void setUpVnfmsInMockAai() {