0b04432dd552d5e0ecaff9a10b4f8d90981c3b52
[cps.git] /
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.controller;
22
23 import lombok.AccessLevel;
24 import lombok.NoArgsConstructor;
25 import lombok.extern.slf4j.Slf4j;
26 import org.onap.cps.api.exceptions.AlreadyDefinedException;
27 import org.onap.cps.api.exceptions.CpsException;
28 import org.onap.cps.api.exceptions.DataNodeNotFoundException;
29 import org.onap.cps.api.exceptions.DataValidationException;
30 import org.onap.cps.ncmp.api.data.exceptions.InvalidDatastoreException;
31 import org.onap.cps.ncmp.api.data.exceptions.InvalidOperationException;
32 import org.onap.cps.ncmp.api.data.exceptions.OperationNotSupportedException;
33 import org.onap.cps.ncmp.api.exceptions.CmHandleNotFoundException;
34 import org.onap.cps.ncmp.api.exceptions.DmiClientRequestException;
35 import org.onap.cps.ncmp.api.exceptions.DmiRequestException;
36 import org.onap.cps.ncmp.api.exceptions.InvalidTopicException;
37 import org.onap.cps.ncmp.api.exceptions.NcmpException;
38 import org.onap.cps.ncmp.api.exceptions.PayloadTooLargeException;
39 import org.onap.cps.ncmp.api.exceptions.PolicyExecutorException;
40 import org.onap.cps.ncmp.api.exceptions.ProvMnSException;
41 import org.onap.cps.ncmp.api.exceptions.ServerNcmpException;
42 import org.onap.cps.ncmp.rest.model.DmiErrorMessage;
43 import org.onap.cps.ncmp.rest.model.DmiErrorMessageDmiResponse;
44 import org.onap.cps.ncmp.rest.model.ErrorMessage;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.http.converter.HttpMessageNotReadableException;
48 import org.springframework.web.bind.annotation.ExceptionHandler;
49 import org.springframework.web.bind.annotation.RestControllerAdvice;
50
51 /**
52  * Exception handler with error message return.
53  */
54 @Slf4j
55 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class,
56     NetworkCmProxyInventoryController.class})
57 @NoArgsConstructor(access = AccessLevel.PACKAGE)
58 public class NetworkCmProxyRestExceptionHandler {
59
60     private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
61
62     /**
63      * Default exception handler.
64      *
65      * @param exception the exception to handle
66      * @return response with response code 500.
67      */
68     @ExceptionHandler
69     public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
70         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
71     }
72
73     @ExceptionHandler({CpsException.class, ServerNcmpException.class})
74     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final Exception exception) {
75         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
76     }
77
78     @ExceptionHandler({DmiClientRequestException.class})
79     public static ResponseEntity<Object> handleClientRequestExceptions(
80             final DmiClientRequestException dmiClientRequestException) {
81         return wrapDmiErrorResponse(dmiClientRequestException);
82     }
83
84     @ExceptionHandler({DmiRequestException.class, DataValidationException.class, InvalidOperationException.class,
85         OperationNotSupportedException.class, HttpMessageNotReadableException.class, InvalidTopicException.class,
86         InvalidDatastoreException.class})
87     public static ResponseEntity<Object> handleDmiRequestExceptions(final Exception exception) {
88         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
89     }
90
91     @ExceptionHandler({AlreadyDefinedException.class, PolicyExecutorException.class})
92     public static ResponseEntity<Object> handleConflictExceptions(final Exception exception) {
93         return buildErrorResponse(HttpStatus.CONFLICT, exception);
94     }
95
96     @ExceptionHandler({CmHandleNotFoundException.class, DataNodeNotFoundException.class})
97     public static ResponseEntity<Object> cmHandleNotFoundExceptions(final Exception exception) {
98         return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
99     }
100
101     @ExceptionHandler({PayloadTooLargeException.class})
102     public static ResponseEntity<Object> handlePayloadTooLargeExceptions(final Exception exception) {
103         return buildErrorResponse(HttpStatus.PAYLOAD_TOO_LARGE, exception);
104     }
105
106     @ExceptionHandler({ProvMnSException.class})
107     public static ResponseEntity<Object> invalidPathExceptions(final Exception exception) {
108         return buildErrorResponse(HttpStatus.UNPROCESSABLE_ENTITY, exception);
109     }
110
111     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
112         if (exception.getCause() != null || !(exception instanceof CpsException)) {
113             log.error("Exception occurred", exception);
114         }
115         final var errorMessage = new ErrorMessage();
116         errorMessage.setStatus(status.toString());
117         errorMessage.setMessage(exception.getMessage());
118         if (exception instanceof CpsException) {
119             errorMessage.setDetails(((CpsException) exception).getDetails());
120         } else if (exception instanceof NcmpException) {
121             errorMessage.setDetails(((NcmpException) exception).getDetails());
122         } else {
123             errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
124         }
125         return new ResponseEntity<>(errorMessage, status);
126     }
127
128     private static ResponseEntity<Object> wrapDmiErrorResponse(final DmiClientRequestException
129                                                                        dmiClientRequestException) {
130         final var dmiErrorMessage = new DmiErrorMessage();
131         final var dmiErrorResponse = new DmiErrorMessageDmiResponse();
132         dmiErrorResponse.setHttpCode(dmiClientRequestException.getHttpStatusCode());
133         dmiErrorResponse.setBody(dmiClientRequestException.getResponseBodyAsString());
134         dmiErrorMessage.setMessage(dmiClientRequestException.getMessage());
135         dmiErrorMessage.setDmiResponse(dmiErrorResponse);
136         return new ResponseEntity<>(dmiErrorMessage, HttpStatus.BAD_GATEWAY);
137     }
138 }