Resource Configuration Snapshots Executor and API
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / configs-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / configs / api / ResourceConfigSnapshotExceptionHandler.kt
1 /*
2  * Copyright © 2019 Bell Canada
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.configs.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.dao.IncorrectResultSizeDataAccessException
31 import org.springframework.web.server.ServerWebInputException
32 import org.springframework.web.bind.annotation.ExceptionHandler
33 import org.springframework.web.bind.annotation.RestControllerAdvice
34 import java.io.Serializable
35 import java.util.*
36
37 /**
38  * Handle exceptions in ResourceConfigSnapshot API and provide relevant HTTP status codes and messages
39  *
40  * @author Serge Simard
41  * @version 1.0
42  */
43 @RestControllerAdvice("org.onap.ccsdk.cds.blueprintsprocessor.configs.api")
44 open class ResourceConfigSnapshotExceptionHandler {
45
46     private val log = LoggerFactory.getLogger(ResourceConfigSnapshotExceptionHandler::class.toString())
47
48     private val debugMsg = "Resource_Config_Snapshot_ExceptionHandler_Error_Message"
49
50     @ExceptionHandler
51     fun resourceConfigSnapshotExceptionHandler(e: BluePrintProcessorException): ResponseEntity<ErrorMessage> {
52         val errorCode = ErrorCode.BLUEPRINT_PATH_MISSING
53         return returnError(e, errorCode)
54     }
55
56     @ExceptionHandler
57     fun resourceConfigSnapshotExceptionHandler(e: ServerWebInputException): ResponseEntity<ErrorMessage> {
58         val errorCode = ErrorCode.INVALID_REQUEST_FORMAT
59         return returnError(e, errorCode, false)
60     }
61
62     @ExceptionHandler
63     fun resourceConfigSnapshotExceptionHandler(e: IllegalArgumentException): ResponseEntity<ErrorMessage> {
64         val errorCode = ErrorCode.INVALID_REQUEST_FORMAT
65         return returnError(e, errorCode, false)
66     }
67
68     @ExceptionHandler
69     fun resourceConfigSnapshotExceptionHandler(e: IncorrectResultSizeDataAccessException): ResponseEntity<ErrorMessage> {
70         val errorCode = ErrorCode.DUPLICATE_DATA
71         return returnError(e, errorCode)
72     }
73
74     @ExceptionHandler
75     fun resourceConfigSnapshotExceptionHandler(e: EmptyResultDataAccessException): ResponseEntity<ErrorMessage> {
76         val errorCode = ErrorCode.RESOURCE_NOT_FOUND
77         return returnError(e, errorCode, false)
78     }
79
80     @ExceptionHandler
81     fun resourceConfigSnapshotExceptionHandler(e: JpaObjectRetrievalFailureException): ResponseEntity<ErrorMessage> {
82         val errorCode = ErrorCode.RESOURCE_NOT_FOUND
83         return returnError(e, errorCode, false)
84     }
85
86     @ExceptionHandler
87     fun resourceConfigSnapshotExceptionHandler(e: Exception): ResponseEntity<ErrorMessage> {
88         val errorCode = ErrorCode.GENERIC_FAILURE
89         return returnError(e, errorCode)
90     }
91
92     @ExceptionHandler
93     fun resourceConfigSnapshotExceptionHandler(e: ResourceConfigSnapshotException): ResponseEntity<ErrorMessage> {
94         val errorCode = ErrorCode.RESOURCE_NOT_FOUND
95         return returnError(e, errorCode, false)
96     }
97
98     fun returnError(e: Exception, errorCode: ErrorCode, toBeLogged: Boolean = true): ResponseEntity<ErrorMessage> {
99         if (toBeLogged) {
100             log.error(e.message, e)
101         } else {
102             log.error(e.message)
103         }
104         val errorMessage =
105             ErrorMessage(errorCode.message(e.message!!),
106                 errorCode.value,
107                 debugMsg)
108         return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)!!)
109     }
110 }
111
112 @JsonInclude(JsonInclude.Include.NON_NULL)
113 @JsonTypeName("errorMessage")
114 @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
115 class ErrorMessage(var message: String?, var code: Int?, var debugMessage: String?) : Serializable {
116     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
117     var timestamp = Date()
118 }