From fbf6300274558ffe323bf66bb5b23c72c350d3ee Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Thu, 3 Oct 2019 17:45:05 +0100 Subject: Improved error handling Change-Id: Iac436f6a950bf61ac6321ef1d427a7bb14774e30 Issue-ID: SO-2395 Signed-off-by: MichaelMorris --- .../extclients/vnfm/VnfmServiceProviderImpl.java | 15 ++++ .../vnfmadapter/jobmanagement/JobManager.java | 9 +- .../vnfmadapter/lifecycle/LifecycleManager.java | 28 +++++++ .../notificationhandling/NotificationHandler.java | 97 +++++++++++++++------- .../rest/VnfmAdapterControllerTest.java | 35 ++++++++ .../vnfm/tasks/VnfmAdapterServiceProviderImpl.java | 9 +- .../exceptions/HttpResouceNotFoundException.java | 31 +++++++ .../rest/exceptions/RestProcessingException.java | 16 ++++ .../rest/service/HttpRestServiceProviderImpl.java | 18 ++-- .../service/HttpRestServiceProviderImplTest.java | 5 +- .../rest/api/SoMonitoringController.java | 15 ++-- 11 files changed, 230 insertions(+), 48 deletions(-) create mode 100644 common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmServiceProviderImpl.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmServiceProviderImpl.java index 948f5fc269..3bdba368b2 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmServiceProviderImpl.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmServiceProviderImpl.java @@ -26,10 +26,13 @@ import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest; +import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager; import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException; +import org.onap.so.rest.exceptions.RestProcessingException; import org.onap.so.rest.service.HttpRestServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -115,6 +118,18 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider { try { response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate", Void.class); + } catch (final RestProcessingException restProcessingException) { + if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) { + InlineResponse201 vnf = getVnf(vnfm, vnfSelfLink).get(); + if (vnf.getInstantiationState().equals(InstantiationStateEnum.NOT_INSTANTIATED)) { + return JobManager.ALREADY_COMPLETED_OPERATION_ID; + } else { + final String errorMessage = + "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest; + logger.error(errorMessage, restProcessingException); + throw new VnfmRequestFailureException(errorMessage, restProcessingException); + } + } } catch (final Exception exception) { final String errorMessage = "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest; diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/jobmanagement/JobManager.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/jobmanagement/JobManager.java index 68fdb79444..3de94ebe05 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/jobmanagement/JobManager.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/jobmanagement/JobManager.java @@ -29,6 +29,7 @@ import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider; 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.so.rest.exceptions.HttpResouceNotFoundException; import org.onap.vnfmadapter.v1.model.OperationEnum; import org.onap.vnfmadapter.v1.model.OperationStateEnum; import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum; @@ -42,6 +43,7 @@ import org.springframework.stereotype.Component; */ @Component public class JobManager { + public static final String ALREADY_COMPLETED_OPERATION_ID = "alreadyCompleted"; private static final String SEPARATOR = "_"; private static Logger logger = getLogger(JobManager.class); private final Map mapOfJobIdToVnfmOperation = Maps.newConcurrentMap(); @@ -87,6 +89,11 @@ public class JobManager { throw new JobNotFoundException("No job found with ID: " + jobId); } + if (vnfmOperation.getOperationId().equals(ALREADY_COMPLETED_OPERATION_ID)) { + response.setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND); + return response.operationState(OperationStateEnum.COMPLETED); + } + if (vnfmOperation.isVnfDeleted()) { response.setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND); return response.operationState(getOperationState(vnfmOperation, null)); @@ -116,7 +123,7 @@ public class JobManager { response.setVnfInstanceId(operation.getVnfInstanceId()); return response; - } catch (final Exception exception) { + } catch (final HttpResouceNotFoundException exception) { logger.error("Exception encountered trying to get operation status for operation id " + vnfmOperation.getOperationId(), exception); return response.operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.WAITING_FOR_STATUS); diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/lifecycle/LifecycleManager.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/lifecycle/LifecycleManager.java index 461c15a512..a885721b76 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/lifecycle/LifecycleManager.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/lifecycle/LifecycleManager.java @@ -24,6 +24,7 @@ import com.google.common.base.Optional; import java.util.Map; import org.onap.aai.domain.yang.EsrVnfm; import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aai.domain.yang.Relationship; import org.onap.so.adapters.vnfmadapter.extclients.SdcPackageProvider; import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper; import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider; @@ -207,6 +208,10 @@ public class LifecycleManager { final EsrVnfm vnfm = getAssignedVnfm(genericVnf); final String operationId = sendTerminateRequestToVnfm(vnfm, genericVnf); + + if (operationId.equals(JobManager.ALREADY_COMPLETED_OPERATION_ID)) { + sendDeleteRequestToVnfm(genericVnf); + } final String jobId = jobManager.createJob(vnfm.getVnfmId(), operationId, true); return new DeleteVnfResponse().jobId(jobId); @@ -234,4 +239,27 @@ public class LifecycleManager { } return vnfm; } + + private void sendDeleteRequestToVnfm(final GenericVnf genericVnf) { + + vnfmServiceProvider.deleteVnf(aaiHelper.getAssignedVnfm(genericVnf), genericVnf.getSelflink()); + + final GenericVnf genericVnfPatch = new GenericVnf(); + genericVnfPatch.setVnfId(genericVnf.getVnfId()); + genericVnfPatch.setOrchestrationStatus("Assigned"); + genericVnfPatch.setSelflink(""); + aaiServiceProvider.invokePatchGenericVnf(genericVnfPatch); + + for (final Relationship relationship : genericVnf.getRelationshipList().getRelationship()) { + if (relationship.getRelatedTo().equals("vserver")) { + aaiServiceProvider.invokeDeleteVserver( + aaiHelper.getRelationshipData(relationship, "cloud-region.cloud-owner"), + aaiHelper.getRelationshipData(relationship, "cloud-region.cloud-region-id"), + aaiHelper.getRelationshipData(relationship, "tenant.tenant-id"), + aaiHelper.getRelationshipData(relationship, "vserver.vserver-id")); + } + } + + + } } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/notificationhandling/NotificationHandler.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/notificationhandling/NotificationHandler.java index 7a6bbfe0d9..63ec4ccc57 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/notificationhandling/NotificationHandler.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/notificationhandling/NotificationHandler.java @@ -145,31 +145,31 @@ public class NotificationHandler implements Runnable { } private void handleVnfTerminateFailed() { - final GenericVnf genericVnf = aaiServiceProvider - .invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).getGenericVnf().get(0); - deleteVservers(vnfLcmOperationOccurrenceNotification, genericVnf); - jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId(), false); + try { + final GenericVnf genericVnf = aaiServiceProvider + .invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).getGenericVnf().get(0); + deleteVserversFromAai(vnfLcmOperationOccurrenceNotification, genericVnf); + } finally { + jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId(), + false); + } } private void handleVnfTerminateCompleted() { - final GenericVnf genericVnf = aaiServiceProvider - .invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).getGenericVnf().get(0); - deleteVservers(vnfLcmOperationOccurrenceNotification, genericVnf); - - boolean deleteSuccessful = false; + GenericVnf genericVnf = null; + boolean vServersDeletedFromAai = false; + boolean identifierDeletedFromVnfm = false; + boolean genericVnfUpdated = false; try { - vnfmServiceProvider.deleteVnf(aaiHelper.getAssignedVnfm(genericVnf), genericVnf.getSelflink()); - deleteSuccessful = true; + genericVnf = aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()) + .getGenericVnf().get(0); + vServersDeletedFromAai = deleteVserversFromAai(vnfLcmOperationOccurrenceNotification, genericVnf); + identifierDeletedFromVnfm = deleteVnfIdentifierOnVnfm(genericVnf); + genericVnfUpdated = patchVnfInAai(genericVnf.getVnfId(), "Assigned", identifierDeletedFromVnfm ? "" : null); } finally { jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId(), - deleteSuccessful); + vServersDeletedFromAai && identifierDeletedFromVnfm && genericVnfUpdated); jobManager.vnfDeleted(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId()); - - final GenericVnf genericVnfPatch = new GenericVnf(); - genericVnfPatch.setVnfId(genericVnf.getVnfId()); - genericVnfPatch.setOrchestrationStatus("Assigned"); - genericVnfPatch.setSelflink(""); - aaiServiceProvider.invokePatchGenericVnf(genericVnfPatch); } } @@ -194,19 +194,60 @@ public class NotificationHandler implements Runnable { } } - private void deleteVservers(final VnfLcmOperationOccurrenceNotification notification, final GenericVnf vnf) { - for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) { - if (ChangeTypeEnum.REMOVED.equals(vnfc.getChangeType())) { + private boolean deleteVserversFromAai(final VnfLcmOperationOccurrenceNotification notification, + final GenericVnf vnf) { + try { + for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) { + if (ChangeTypeEnum.REMOVED.equals(vnfc.getChangeType())) { - final Relationship relationshipToVserver = aaiHelper.deleteRelationshipWithDataValue(vnf, "vserver", - "vserver.vserver-id", vnfc.getComputeResource().getResourceId()); + final Relationship relationshipToVserver = aaiHelper.deleteRelationshipWithDataValue(vnf, "vserver", + "vserver.vserver-id", vnfc.getComputeResource().getResourceId()); - aaiServiceProvider.invokeDeleteVserver( - aaiHelper.getRelationshipData(relationshipToVserver, "cloud-region.cloud-owner"), - aaiHelper.getRelationshipData(relationshipToVserver, "cloud-region.cloud-region-id"), - aaiHelper.getRelationshipData(relationshipToVserver, "tenant.tenant-id"), - vnfc.getComputeResource().getResourceId()); + aaiServiceProvider.invokeDeleteVserver( + aaiHelper.getRelationshipData(relationshipToVserver, "cloud-region.cloud-owner"), + aaiHelper.getRelationshipData(relationshipToVserver, "cloud-region.cloud-region-id"), + aaiHelper.getRelationshipData(relationshipToVserver, "tenant.tenant-id"), + vnfc.getComputeResource().getResourceId()); + } } + return true; + } catch (final Exception exception) { + logger.error( + "Error encountered deleting vservers based on received notification, AAI may not be updated correctly " + + vnfLcmOperationOccurrenceNotification, + exception); + return false; + } + } + + private boolean deleteVnfIdentifierOnVnfm(GenericVnf genericVnf) { + try { + vnfmServiceProvider.deleteVnf(aaiHelper.getAssignedVnfm(genericVnf), genericVnf.getSelflink()); + return true; + } catch (Exception exception) { + logger.error("Exception deleting the identifier " + genericVnf.getSelflink() + + " from the VNFM. The VNF has been terminated successfully but the identifier will remain on the VNFM.", + exception); + return false; + } + } + + private boolean patchVnfInAai(final String vnfId, final String orchestrationStatus, final String selfLink) { + try { + final GenericVnf genericVnfPatch = new GenericVnf(); + genericVnfPatch.setVnfId(vnfId); + genericVnfPatch.setOrchestrationStatus(orchestrationStatus); + if (selfLink != null) { + genericVnfPatch.setSelflink(selfLink); + } + aaiServiceProvider.invokePatchGenericVnf(genericVnfPatch); + return true; + } catch (final Exception exception) { + logger.error( + "Error encountered setting orchestration status and/or self link based on received notification, AAI may not be updated correctly " + + vnfLcmOperationOccurrenceNotification, + exception); + return false; } } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/VnfmAdapterControllerTest.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/VnfmAdapterControllerTest.java index 297f74433e..6cdb93a25d 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/VnfmAdapterControllerTest.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/VnfmAdapterControllerTest.java @@ -57,6 +57,7 @@ import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.JSON; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf; import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException; @@ -321,6 +322,40 @@ public class VnfmAdapterControllerTest { assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime()); } + @Test + public void deleteVnf_VnfAlreadyTerminated_Returns202AndJobId() throws Exception { + final TestRestTemplate restTemplate = new TestRestTemplate("test", "test"); + + final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType1"); + addSelfLinkToGenericVnf(genericVnf); + addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1"); + setUpVnfmsInMockAai(); + + mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate")) + .andRespond(withStatus(HttpStatus.CONFLICT).contentType(MediaType.APPLICATION_JSON)); + + final InlineResponse201 reponse = new InlineResponse201(); + reponse.setInstantiationState(InstantiationStateEnum.NOT_INSTANTIATED); + mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm"))) + .andRespond(withSuccess(gson.toJson(reponse), MediaType.APPLICATION_JSON)); + + mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")) + .andRespond(withStatus(HttpStatus.NO_CONTENT).contentType(MediaType.APPLICATION_JSON)); + + final RequestEntity 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 = + restTemplate.exchange(request, DeleteVnfResponse.class); + assertEquals(202, deleteVnfResponse.getStatusCode().value()); + assertNotNull(deleteVnfResponse.getBody().getJobId()); + + final ResponseEntity jobQueryResponse = + controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213"); + assertEquals(OperationStateEnum.COMPLETED, jobQueryResponse.getBody().getOperationState()); + } + @Test public void deleteVnf_GenericVnfNotFound_Returns404() throws Exception { final TestRestTemplate restTemplate = new TestRestTemplate("test", "test"); diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/VnfmAdapterServiceProviderImpl.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/VnfmAdapterServiceProviderImpl.java index 32516c1dcb..41649dc838 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/VnfmAdapterServiceProviderImpl.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/vnfm/tasks/VnfmAdapterServiceProviderImpl.java @@ -20,7 +20,9 @@ package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks; +import com.google.common.base.Optional; import org.onap.so.rest.exceptions.InvalidRestRequestException; +import org.onap.so.rest.exceptions.HttpResouceNotFoundException; import org.onap.so.rest.exceptions.RestProcessingException; import org.onap.so.rest.service.HttpRestServiceProvider; import org.onap.vnfmadapter.v1.model.CreateVnfRequest; @@ -33,7 +35,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import com.google.common.base.Optional; /** * @author waqas.ikram@est.tech @@ -82,7 +83,8 @@ public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvide } return Optional.of(createVnfResponse); - } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) { + } catch (final RestProcessingException | InvalidRestRequestException + | HttpResouceNotFoundException httpInvocationException) { LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException); return Optional.absent(); } @@ -144,7 +146,8 @@ public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvide return Optional.absent(); } return Optional.of(response.getBody()); - } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) { + } catch (final RestProcessingException | InvalidRestRequestException + | HttpResouceNotFoundException httpInvocationException) { LOGGER.error("Unexpected error while processing job request", httpInvocationException); throw httpInvocationException; } diff --git a/common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java b/common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java new file mode 100644 index 0000000000..e7b7b72957 --- /dev/null +++ b/common/src/main/java/org/onap/so/rest/exceptions/HttpResouceNotFoundException.java @@ -0,0 +1,31 @@ +/*- + * ============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.rest.exceptions; + + +public class HttpResouceNotFoundException extends RuntimeException { + + private static final long serialVersionUID = 9007892558312387355L; + + public HttpResouceNotFoundException(final String message) { + super(message); + } +} diff --git a/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java b/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java index e8ce00c7e5..5d62d8488a 100644 --- a/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java +++ b/common/src/main/java/org/onap/so/rest/exceptions/RestProcessingException.java @@ -26,12 +26,28 @@ package org.onap.so.rest.exceptions; public class RestProcessingException extends RuntimeException { private static final long serialVersionUID = 16862313537198441L; + private final int statusCode; public RestProcessingException(final String message) { super(message); + statusCode = 0; } public RestProcessingException(final String message, final Throwable cause) { + this(message, cause, 0); + } + + public RestProcessingException(final String message, final Throwable cause, final int statusCode) { super(message, cause); + this.statusCode = statusCode; + } + + /** + * Get the status code from the response to the rest request, if available + * + * @return the status code, or 0 if not available + */ + public int getStatusCode() { + return statusCode; } } diff --git a/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java b/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java index a627e82802..b82d73bbbf 100644 --- a/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java +++ b/common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java @@ -23,6 +23,7 @@ package org.onap.so.rest.service; import com.google.common.base.Optional; import org.onap.so.configuration.rest.BasicHttpHeadersProvider; import org.onap.so.configuration.rest.HttpHeadersProvider; +import org.onap.so.rest.exceptions.HttpResouceNotFoundException; import org.onap.so.rest.exceptions.InvalidRestRequestException; import org.onap.so.rest.exceptions.RestProcessingException; import org.slf4j.Logger; @@ -32,7 +33,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -116,15 +117,18 @@ public class HttpRestServiceProviderImpl implements HttpRestServiceProvider { try { return restTemplate.exchange(url, httpMethod, request, clazz); - } catch (final HttpClientErrorException httpClientErrorException) { + } catch (final HttpStatusCodeException httpStatusCodeException) { final String message = "Unable to invoke HTTP " + httpMethod + " using url: " + url + ", Response: " - + httpClientErrorException.getRawStatusCode(); - LOGGER.error(message, httpClientErrorException); - final int rawStatusCode = httpClientErrorException.getRawStatusCode(); - if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) { + + httpStatusCodeException.getRawStatusCode(); + LOGGER.error(message, httpStatusCodeException); + final int rawStatusCode = httpStatusCodeException.getRawStatusCode(); + if (rawStatusCode == HttpStatus.BAD_REQUEST.value()) { throw new InvalidRestRequestException("No result found for given url: " + url); + } else if (rawStatusCode == HttpStatus.NOT_FOUND.value()) { + throw new HttpResouceNotFoundException("No result found for given url: " + url); } - throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url); + throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url, + httpStatusCodeException, rawStatusCode); } catch (final RestClientException restClientException) { LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException); diff --git a/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java b/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java index 978c016dec..72bacdf2db 100644 --- a/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java +++ b/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java @@ -33,6 +33,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.rest.exceptions.InvalidRestRequestException; +import org.onap.so.rest.exceptions.HttpResouceNotFoundException; import org.onap.so.rest.exceptions.RestProcessingException; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; @@ -116,7 +117,7 @@ public class HttpRestServiceProviderImplTest { } - @Test(expected = InvalidRestRequestException.class) + @Test(expected = HttpResouceNotFoundException.class) public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() { assertGetErrorScenario(HttpStatus.NOT_FOUND); } @@ -239,7 +240,7 @@ public class HttpRestServiceProviderImplTest { } - @Test(expected = InvalidRestRequestException.class) + @Test(expected = HttpResouceNotFoundException.class) public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() { assertPostErrorScenario(HttpStatus.NOT_FOUND); } diff --git a/so-monitoring/so-monitoring-service/src/main/java/org/onap/so/monitoring/rest/api/SoMonitoringController.java b/so-monitoring/so-monitoring-service/src/main/java/org/onap/so/monitoring/rest/api/SoMonitoringController.java index d48d18dad0..1e5f8780d6 100644 --- a/so-monitoring/so-monitoring-service/src/main/java/org/onap/so/monitoring/rest/api/SoMonitoringController.java +++ b/so-monitoring/so-monitoring-service/src/main/java/org/onap/so/monitoring/rest/api/SoMonitoringController.java @@ -19,6 +19,7 @@ */ package org.onap.so.monitoring.rest.api; +import com.google.common.base.Optional; import java.util.List; import java.util.Map; import javax.ws.rs.GET; @@ -39,12 +40,12 @@ import org.onap.so.monitoring.model.ProcessInstanceVariableDetail; import org.onap.so.monitoring.model.SoInfraRequest; import org.onap.so.monitoring.rest.service.CamundaProcessDataServiceProvider; import org.onap.so.rest.exceptions.InvalidRestRequestException; +import org.onap.so.rest.exceptions.HttpResouceNotFoundException; import org.onap.so.rest.exceptions.RestProcessingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import com.google.common.base.Optional; /** * @author waqas.ikram@ericsson.com @@ -85,7 +86,7 @@ public class SoMonitoringController { LOGGER.error("Unable to find process instance id for : " + requestId); return Response.status(Status.NO_CONTENT).build(); - } catch (final InvalidRestRequestException extensions) { + } catch (final InvalidRestRequestException | HttpResouceNotFoundException extensions) { final String message = "Unable to find process instance id for : " + requestId; LOGGER.error(message); return Response.status(Status.BAD_REQUEST).entity(message).build(); @@ -114,7 +115,7 @@ public class SoMonitoringController { LOGGER.error("Unable to find process instance id for : " + processInstanceId); return Response.status(Status.NO_CONTENT).build(); - } catch (final InvalidRestRequestException extensions) { + } catch (final InvalidRestRequestException | HttpResouceNotFoundException extensions) { final String message = "Unable to find process instance id for : " + processInstanceId; LOGGER.error(message); return Response.status(Status.BAD_REQUEST).entity(message).build(); @@ -143,7 +144,7 @@ public class SoMonitoringController { LOGGER.error("Unable to find process definition xml for processDefinitionId: " + processDefinitionId); return Response.status(Status.NO_CONTENT).build(); - } catch (final InvalidRestRequestException extensions) { + } catch (final InvalidRestRequestException | HttpResouceNotFoundException extensions) { final String message = "Unable to find process definition xml for processDefinitionId: {}" + processDefinitionId; return Response.status(Status.BAD_REQUEST).entity(message).build(); @@ -166,7 +167,7 @@ public class SoMonitoringController { final List activityInstanceDetails = camundaProcessDataServiceProvider.getActivityInstance(processInstanceId); return Response.status(Status.OK).entity(activityInstanceDetails).build(); - } catch (final InvalidRestRequestException extensions) { + } catch (final InvalidRestRequestException | HttpResouceNotFoundException extensions) { final String message = "Unable to find activity instance for processInstanceId: " + processInstanceId; LOGGER.error(message); return Response.status(Status.BAD_REQUEST).entity(message).build(); @@ -189,7 +190,7 @@ public class SoMonitoringController { final List processInstanceVariable = camundaProcessDataServiceProvider.getProcessInstanceVariable(processInstanceId); return Response.status(Status.OK).entity(processInstanceVariable).build(); - } catch (final InvalidRestRequestException extensions) { + } catch (final InvalidRestRequestException | HttpResouceNotFoundException extensions) { final String message = "Unable to find process instance variables for processInstanceId: " + processInstanceId; LOGGER.error(message); @@ -217,7 +218,7 @@ public class SoMonitoringController { LOGGER.info("result size: " + requests.size()); return Response.status(Status.OK).entity(requests).build(); - } catch (final InvalidRestRequestException extensions) { + } catch (final InvalidRestRequestException | HttpResouceNotFoundException extensions) { final String message = "Unable to search request for filters: " + filters + ", from: " + startTime + ", to: " + endTime + ", maxResult: " + maxResult; LOGGER.error(message); -- cgit 1.2.3-korg