2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2023 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.rest.exceptions;
23 import lombok.AccessLevel;
24 import lombok.NoArgsConstructor;
25 import lombok.extern.slf4j.Slf4j;
26 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException;
27 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
28 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
29 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
30 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
31 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyController;
32 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyInventoryController;
33 import org.onap.cps.ncmp.rest.model.DmiErrorMessage;
34 import org.onap.cps.ncmp.rest.model.DmiErrorMessageDmiResponse;
35 import org.onap.cps.ncmp.rest.model.ErrorMessage;
36 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
37 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
38 import org.onap.cps.spi.exceptions.CpsException;
39 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
40 import org.onap.cps.spi.exceptions.DataValidationException;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.http.converter.HttpMessageNotReadableException;
44 import org.springframework.web.bind.annotation.ExceptionHandler;
45 import org.springframework.web.bind.annotation.RestControllerAdvice;
48 * Exception handler with error message return.
51 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class, NetworkCmProxyInventoryController.class})
52 @NoArgsConstructor(access = AccessLevel.PACKAGE)
53 public class NetworkCmProxyRestExceptionHandler {
55 private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
58 * Default exception handler.
60 * @param exception the exception to handle
61 * @return response with response code 500.
64 public static ResponseEntity<Object> handleInternalServerErrorExceptions(
65 final Exception exception) {
66 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
69 @ExceptionHandler({CpsException.class, ServerNcmpException.class})
70 public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final Exception exception) {
71 return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
74 @ExceptionHandler({HttpClientRequestException.class})
75 public static ResponseEntity<Object> handleClientRequestExceptions(
76 final HttpClientRequestException httpClientRequestException) {
77 return wrapDmiErrorResponse(HttpStatus.BAD_GATEWAY, httpClientRequestException);
80 @ExceptionHandler({DmiRequestException.class, DataValidationException.class, OperationNotSupportedException.class,
81 HttpMessageNotReadableException.class, InvalidTopicException.class, InvalidDatastoreException.class})
82 public static ResponseEntity<Object> handleDmiRequestExceptions(final Exception exception) {
83 return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
86 @ExceptionHandler({AlreadyDefinedException.class, AlreadyDefinedExceptionBatch.class })
87 public static ResponseEntity<Object> handleAlreadyDefinedExceptions(final Exception exception) {
88 return buildErrorResponse(HttpStatus.CONFLICT, exception);
91 @ExceptionHandler({DataNodeNotFoundException.class})
92 public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
93 return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
96 private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
97 if (exception.getCause() != null || !(exception instanceof CpsException)) {
98 log.error("Exception occurred", exception);
100 final var errorMessage = new ErrorMessage();
101 errorMessage.setStatus(status.toString());
102 errorMessage.setMessage(exception.getMessage());
103 if (exception instanceof CpsException) {
104 errorMessage.setDetails(((CpsException) exception).getDetails());
105 } else if (exception instanceof NcmpException) {
106 errorMessage.setDetails(((NcmpException) exception).getDetails());
108 errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
110 errorMessage.setDetails(
111 exception instanceof CpsException ? ((CpsException) exception).getDetails() : CHECK_LOGS_FOR_DETAILS);
112 return new ResponseEntity<>(errorMessage, status);
115 private static ResponseEntity<Object> wrapDmiErrorResponse(
116 final HttpStatus httpStatus,
117 final HttpClientRequestException httpClientRequestException) {
118 final var dmiErrorMessage = new DmiErrorMessage();
119 final var dmiErrorResponse = new DmiErrorMessageDmiResponse();
120 dmiErrorResponse.setHttpCode(httpClientRequestException.getHttpStatus());
121 dmiErrorResponse.setBody(httpClientRequestException.getDetails());
122 dmiErrorMessage.setMessage(httpClientRequestException.getMessage());
123 dmiErrorMessage.setDmiResponse(dmiErrorResponse);
124 return new ResponseEntity<>(dmiErrorMessage, httpStatus);