Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / rest / exceptions / EtsiSubscriptionNotificationControllerExceptionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.so.adapters.vnfmadapter.rest.exceptions;
22
23 import static org.slf4j.LoggerFactory.getLogger;
24 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
25 import org.onap.so.adapters.vnfmadapter.rest.EtsiSubscriptionNotificationController;
26 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
27 import org.onap.so.rest.exceptions.InvalidRestRequestException;
28 import org.onap.so.rest.exceptions.RestProcessingException;
29 import org.slf4j.Logger;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.ControllerAdvice;
33 import org.springframework.web.bind.annotation.ExceptionHandler;
34
35 /**
36  * Exception Handler for the Etsi Subscription Notification Controller {@link EtsiSubscriptionNotificationController
37  * EtsiSubscriptionNotificationController}
38  * 
39  * @author Andrew Lamb (andrew.a.lamb@est.tech)
40  */
41 @ControllerAdvice(assignableTypes = EtsiSubscriptionNotificationController.class)
42 public class EtsiSubscriptionNotificationControllerExceptionHandler {
43
44     private static final Logger logger = getLogger(EtsiSubscriptionNotificationControllerExceptionHandler.class);
45
46     @ExceptionHandler(InvalidRestRequestException.class)
47     public ResponseEntity<ProblemDetails> handleInvalidRestRequestException(
48             final InvalidRestRequestException invalidRestRequestException) {
49         final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
50                 + HttpStatus.BAD_REQUEST + ".\n" + invalidRestRequestException.getMessage();
51         logger.error(errorMessage);
52         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
53     }
54
55     @ExceptionHandler(HttpResouceNotFoundException.class)
56     public ResponseEntity<ProblemDetails> handleHttpResourceNotFoundException(
57             final HttpResouceNotFoundException httpResourceNotFoundException) {
58         final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
59                 + HttpStatus.NOT_FOUND + ".\n" + httpResourceNotFoundException.getMessage();
60         logger.error(errorMessage);
61         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
62     }
63
64     @ExceptionHandler(RestProcessingException.class)
65     public ResponseEntity<ProblemDetails> handleRestProcessingException(
66             final RestProcessingException restProcessingException) {
67         final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
68                 + restProcessingException.getStatusCode() + ".\n" + restProcessingException.getMessage();
69         logger.error(errorMessage);
70         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
71     }
72
73     @ExceptionHandler(InternalServerErrorException.class)
74     public ResponseEntity<ProblemDetails> handleInternalServerErrorException(
75             final InternalServerErrorException internalServerErrorException) {
76         logger.error(internalServerErrorException.getMessage());
77         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
78                 .body(new ProblemDetails().detail(internalServerErrorException.getMessage()));
79     }
80
81     @ExceptionHandler(AuthenticationTypeNotSupportedException.class)
82     public ResponseEntity<ProblemDetails> handleAuthenticationTypeNotSupportedException(
83             final AuthenticationTypeNotSupportedException authenticationTypeNotSupportedException) {
84         logger.error(authenticationTypeNotSupportedException.getMessage());
85         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
86                 .body(new ProblemDetails().detail(authenticationTypeNotSupportedException.getMessage()));
87     }
88
89     @ExceptionHandler(ConversionFailedException.class)
90     public ResponseEntity<ProblemDetails> handleConversionFailedException(
91             final ConversionFailedException conversionFailedException) {
92         logger.error(conversionFailedException.getMessage());
93         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
94                 .body(new ProblemDetails().detail(conversionFailedException.getMessage()));
95     }
96
97     @ExceptionHandler(NotificationTypeNotSupportedException.class)
98     public ResponseEntity<ProblemDetails> handleNotificationTypeNotSupportedException(
99             final NotificationTypeNotSupportedException notificationTypeNotSupportedException) {
100         logger.error(notificationTypeNotSupportedException.getMessage());
101         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
102                 .body(new ProblemDetails().detail(notificationTypeNotSupportedException.getMessage()));
103     }
104
105     @ExceptionHandler(SubscriptionNotFoundException.class)
106     public ResponseEntity<ProblemDetails> handleSubscriptionNotFoundException(
107             final SubscriptionNotFoundException subscriptionNotFoundException) {
108         logger.error(subscriptionNotFoundException.getMessage());
109         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
110                 .body(new ProblemDetails().detail(subscriptionNotFoundException.getMessage()));
111     }
112 }