Handle invalid operations on merged datastore endpoint
[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-2022 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.NcmpException;
29 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
30 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyController;
31 import org.onap.cps.ncmp.rest.controller.NetworkCmProxyInventoryController;
32 import org.onap.cps.ncmp.rest.model.DmiErrorMessage;
33 import org.onap.cps.ncmp.rest.model.DmiErrorMessageDmiresponse;
34 import org.onap.cps.ncmp.rest.model.ErrorMessage;
35 import org.onap.cps.spi.exceptions.CpsException;
36 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
37 import org.onap.cps.spi.exceptions.DataValidationException;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.http.converter.HttpMessageNotReadableException;
41 import org.springframework.web.bind.annotation.ExceptionHandler;
42 import org.springframework.web.bind.annotation.RestControllerAdvice;
43
44 /**
45  * Exception handler with error message return.
46  */
47 @Slf4j
48 @RestControllerAdvice(assignableTypes = {NetworkCmProxyController.class, NetworkCmProxyInventoryController.class})
49 @NoArgsConstructor(access = AccessLevel.PACKAGE)
50 public class NetworkCmProxyRestExceptionHandler {
51
52     private static final String CHECK_LOGS_FOR_DETAILS = "Check logs for details.";
53
54     /**
55      * Default exception handler.
56      *
57      * @param exception the exception to handle
58      * @return response with response code 500.
59      */
60     @ExceptionHandler
61     public static ResponseEntity<Object> handleInternalServerErrorExceptions(
62             final Exception exception) {
63         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
64     }
65
66     @ExceptionHandler({CpsException.class, ServerNcmpException.class})
67     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final Exception exception) {
68         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
69     }
70
71     @ExceptionHandler({HttpClientRequestException.class})
72     public static ResponseEntity<Object> handleClientRequestExceptions(
73             final HttpClientRequestException httpClientRequestException) {
74         return wrapDmiErrorResponse(HttpStatus.BAD_GATEWAY, httpClientRequestException);
75     }
76
77     @ExceptionHandler({DmiRequestException.class, DataValidationException.class,
78             HttpMessageNotReadableException.class, InvalidTopicException.class, InvalidDatastoreException.class})
79     public static ResponseEntity<Object> handleDmiRequestExceptions(final Exception exception) {
80         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
81     }
82
83     @ExceptionHandler({DataNodeNotFoundException.class})
84     public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
85         return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
86     }
87
88     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
89         if (exception.getCause() != null || !(exception instanceof CpsException)) {
90             log.error("Exception occurred", exception);
91         }
92         final var errorMessage = new ErrorMessage();
93         errorMessage.setStatus(status.toString());
94         errorMessage.setMessage(exception.getMessage());
95         if (exception instanceof CpsException) {
96             errorMessage.setDetails(((CpsException) exception).getDetails());
97         } else if (exception instanceof NcmpException) {
98             errorMessage.setDetails(((NcmpException) exception).getDetails());
99         } else {
100             errorMessage.setDetails(CHECK_LOGS_FOR_DETAILS);
101         }
102         errorMessage.setDetails(
103                 exception instanceof CpsException ? ((CpsException) exception).getDetails() : CHECK_LOGS_FOR_DETAILS);
104         return new ResponseEntity<>(errorMessage, status);
105     }
106
107     private static ResponseEntity<Object> wrapDmiErrorResponse(
108             final HttpStatus httpStatus,
109             final HttpClientRequestException httpClientRequestException) {
110         final var dmiErrorMessage = new DmiErrorMessage();
111         final var dmiErrorResponse = new DmiErrorMessageDmiresponse();
112         dmiErrorResponse.setHttpCode(httpClientRequestException.getHttpStatus());
113         dmiErrorResponse.setBody(httpClientRequestException.getDetails());
114         dmiErrorMessage.setMessage(httpClientRequestException.getMessage());
115         dmiErrorMessage.setDmiResponse(dmiErrorResponse);
116         return new ResponseEntity<>(dmiErrorMessage, httpStatus);
117     }
118 }