Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / api / advice / CertificationExceptionAdvice.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.api.advice;
22
23 import org.onap.oom.certservice.api.CertificationController;
24 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
25 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
26 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
27 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
28 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
29 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.ExceptionHandler;
35 import org.springframework.web.bind.annotation.RestControllerAdvice;
36
37 @RestControllerAdvice(assignableTypes = CertificationController.class)
38 public final class CertificationExceptionAdvice {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationExceptionAdvice.class);
41
42     @ExceptionHandler(value = CsrDecryptionException.class)
43     public ResponseEntity<ErrorResponseModel> handle(CsrDecryptionException exception) {
44         LOGGER.error("Exception occurred during decoding certificate sign request:", exception);
45         return getErrorResponseEntity(
46                 "Wrong certificate signing request (CSR) format",
47                 HttpStatus.BAD_REQUEST
48         );
49     }
50
51     @ExceptionHandler(value = KeyDecryptionException.class)
52     public ResponseEntity<ErrorResponseModel> handle(KeyDecryptionException exception) {
53         LOGGER.error("Exception occurred during decoding key:", exception);
54         return getErrorResponseEntity(
55                 "Wrong key (PK) format",
56                 HttpStatus.BAD_REQUEST
57         );
58     }
59
60     @ExceptionHandler(value = Cmpv2ServerNotFoundException.class)
61     public ResponseEntity<ErrorResponseModel> handle(Cmpv2ServerNotFoundException exception) {
62         LOGGER.error("Exception occurred selecting CMPv2 server:", exception);
63         return getErrorResponseEntity(
64                 "Certification authority not found for given CAName",
65                 HttpStatus.NOT_FOUND
66         );
67     }
68
69     @ExceptionHandler(value = RuntimeException.class)
70     public ResponseEntity<ErrorResponseModel> handle(RuntimeException exception) throws CmpClientException {
71         throw new CmpClientException("Runtime exception occurred calling cmp client business logic", exception);
72     }
73
74     @ExceptionHandler(value = CmpClientException.class)
75     public ResponseEntity<ErrorResponseModel> handle(CmpClientException exception) {
76         LOGGER.error("Exception occurred calling cmp client:", exception);
77         return getErrorResponseEntity(
78                 "Exception occurred during call to cmp client",
79                 HttpStatus.INTERNAL_SERVER_ERROR
80         );
81     }
82
83     @ExceptionHandler(value = Cmpv2ClientAdapterException.class)
84     public ResponseEntity<ErrorResponseModel> handle(Cmpv2ClientAdapterException exception) {
85         LOGGER.error("Exception occurred parsing cmp client response:", exception);
86         return getErrorResponseEntity(
87                 "Exception occurred parsing cmp client response",
88                 HttpStatus.INTERNAL_SERVER_ERROR
89         );
90     }
91
92     private ResponseEntity<ErrorResponseModel> getErrorResponseEntity(String errorMessage, HttpStatus status) {
93         ErrorResponseModel errorResponse = new ErrorResponseModel(errorMessage);
94         return new ResponseEntity<>(
95                 errorResponse,
96                 status
97         );
98     }
99
100 }