Merge "Handle Subscription Response from DMI"
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / exceptions / NetworkCmProxyRestExceptionHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2021-2024 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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.exceptions;
22
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.CpsException;
38 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
39 import org.onap.cps.spi.exceptions.DataValidationException;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.http.converter.HttpMessageNotReadableException;
43 import org.springframework.web.bind.annotation.ExceptionHandler;
44 import org.springframework.web.bind.annotation.RestControllerAdvice;
45
46 /**
47  * Exception handler with error message return.
48  */
49 @Slf4j
50 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class, NetworkCmProxyInventoryController.class})
51 @NoArgsConstructor(access = AccessLevel.PACKAGE)
52 public class NetworkCmProxyRestExceptionHandler {
53
54     private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
55
56     /**
57      * Default exception handler.
58      *
59      * @param exception the exception to handle
60      * @return response with response code 500.
61      */
62     @ExceptionHandler
63     public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
64         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
65     }
66
67     @ExceptionHandler({CpsException.class, ServerNcmpException.class})
68     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final Exception exception) {
69         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
70     }
71
72     @ExceptionHandler({HttpClientRequestException.class})
73     public static ResponseEntity<Object> handleClientRequestExceptions(
74             final HttpClientRequestException httpClientRequestException) {
75         return wrapDmiErrorResponse(httpClientRequestException);
76     }
77
78     @ExceptionHandler({DmiRequestException.class, DataValidationException.class, OperationNotSupportedException.class,
79             HttpMessageNotReadableException.class, InvalidTopicException.class, InvalidDatastoreException.class})
80     public static ResponseEntity<Object> handleDmiRequestExceptions(final Exception exception) {
81         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
82     }
83
84     @ExceptionHandler({AlreadyDefinedException.class})
85     public static ResponseEntity<Object> handleAlreadyDefinedExceptions(final Exception exception) {
86         return buildErrorResponse(HttpStatus.CONFLICT, exception);
87     }
88
89     @ExceptionHandler({DataNodeNotFoundException.class})
90     public static ResponseEntity<Object> handleNotFoundExceptions(final Exception exception) {
91         return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
92     }
93
94     @ExceptionHandler({PayloadTooLargeException.class})
95     public static ResponseEntity<Object> handlePayloadTooLargeExceptions(final Exception exception) {
96         return buildErrorResponse(HttpStatus.PAYLOAD_TOO_LARGE, exception);
97     }
98
99     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
100         if (exception.getCause() != null || !(exception instanceof CpsException)) {
101             log.error("Exception occurred", exception);
102         }
103         final var errorMessage = new ErrorMessage();
104         errorMessage.setStatus(status.toString());
105         errorMessage.setMessage(exception.getMessage());
106         if (exception instanceof CpsException) {
107             errorMessage.setDetails(((CpsException) exception).getDetails());
108         } else if (exception instanceof NcmpException) {
109             errorMessage.setDetails(((NcmpException) exception).getDetails());
110         } else {
111             errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
112         }
113         errorMessage.setDetails(
114                 exception instanceof CpsException ? ((CpsException) exception).getDetails() : CHECK_LOGS_FOR_DETAILS);
115         return new ResponseEntity<>(errorMessage, status);
116     }
117
118     private static ResponseEntity<Object> wrapDmiErrorResponse(final HttpClientRequestException
119                                                                      httpClientRequestException) {
120         final var dmiErrorMessage = new DmiErrorMessage();
121         final var dmiErrorResponse = new DmiErrorMessageDmiResponse();
122         dmiErrorResponse.setHttpCode(httpClientRequestException.getHttpStatus());
123         dmiErrorResponse.setBody(httpClientRequestException.getDetails());
124         dmiErrorMessage.setMessage(httpClientRequestException.getMessage());
125         dmiErrorMessage.setDmiResponse(dmiErrorResponse);
126         return new ResponseEntity<>(dmiErrorMessage, HttpStatus.BAD_GATEWAY);
127     }
128 }