7f8f7da7996952be1e60d5475448e2ffa12b830d
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2018-2019 Bell Canada Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults.api
18
19 import com.fasterxml.jackson.annotation.JsonFormat
20 import com.fasterxml.jackson.annotation.JsonInclude
21 import com.fasterxml.jackson.annotation.JsonTypeInfo
22 import com.fasterxml.jackson.annotation.JsonTypeName
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
25 import org.slf4j.LoggerFactory
26 import org.springframework.http.HttpStatus
27 import org.springframework.http.ResponseEntity
28 import org.springframework.orm.jpa.JpaObjectRetrievalFailureException
29 import org.springframework.dao.EmptyResultDataAccessException
30 import org.springframework.web.server.ServerWebInputException
31 import org.springframework.web.bind.annotation.ExceptionHandler
32 import org.springframework.web.bind.annotation.RestControllerAdvice
33 import java.io.Serializable
34 import java.util.*
35
36 /**
37  * Handle exceptions in Resolution Results API and provide relevant HTTP status codes and messages
38  *
39  * @author Serge Simard
40  * @version 1.0
41  */
42 @RestControllerAdvice("org.onap.ccsdk.cds.blueprintsprocessor.resolutionresults")
43 open class ResolutionResultsServiceExceptionHandler {
44
45     private val log = LoggerFactory.getLogger(ResolutionResultsServiceExceptionHandler::class.toString())
46
47     private val debugMsg = "ResolutionResultsService_Error_Message"
48
49     @ExceptionHandler
50     fun ResolutionResultsServiceExceptionHandler(e: BluePrintProcessorException): ResponseEntity<ErrorMessage> {
51         log.error(e.message, e)
52         val errorCode = ErrorCode.BLUEPRINT_PATH_MISSING
53         val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg)
54         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode))
55     }
56
57     @ExceptionHandler
58     fun ResolutionResultsServiceExceptionHandler(e: ServerWebInputException): ResponseEntity<ErrorMessage> {
59         log.error(e.message, e)
60         val errorCode = ErrorCode.INVALID_REQUEST_FORMAT
61         val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg)
62         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode))
63     }
64
65     @ExceptionHandler
66     fun ResolutionResultsServiceExceptionHandler(e: EmptyResultDataAccessException): ResponseEntity<ErrorMessage> {
67         log.error(e.message, e)
68         var errorCode = ErrorCode.RESOURCE_NOT_FOUND
69         val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg)
70         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode))
71     }
72
73     @ExceptionHandler
74     fun ResolutionResultsServiceExceptionHandler(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorMessage> {
75         log.error(e.message, e)
76
77         var errorCode = ErrorCode.RESOURCE_NOT_FOUND
78         val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg)
79         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode))
80     }
81
82     @ExceptionHandler
83     fun ResolutionResultsServiceExceptionHandler(e: Exception): ResponseEntity<ErrorMessage> {
84         log.error(e.message, e)
85         var errorCode = ErrorCode.GENERIC_FAILURE
86         val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg)
87         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode))
88     }
89
90     @ExceptionHandler
91     fun ResolutionResultsServiceExceptionHandler(e: ResourceException): ResponseEntity<ErrorMessage> {
92         log.error(e.message, e)
93         return ResponseEntity(ErrorMessage(e.message, e.code, debugMsg), HttpStatus.resolve(e.code))
94     }
95 }
96
97 @JsonInclude(JsonInclude.Include.NON_NULL)
98 @JsonTypeName("errorMessage")
99 @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
100 class ErrorMessage(var message: String?, var code: Int?, var debugMessage: String?) : Serializable {
101     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
102     var timestamp = Date()
103 }