21fd51b2f47a019bb6c91db72b9db9e936588261
[ccsdk/cds.git] / ms / error-catalog / services / src / main / kotlin / org / onap / ccsdk / cds / error / catalog / services / ErrorCatalogService.kt
1 /*
2  *  Copyright © 2020 IBM, Bell Canada.
3  *  Modifications Copyright © 2019-2020 AT&T Intellectual Property.
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
18 package org.onap.ccsdk.cds.error.catalog.services
19
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalog
22 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogException
23 import org.onap.ccsdk.cds.error.catalog.core.ErrorMessageLibConstants
24 import org.onap.ccsdk.cds.error.catalog.core.ErrorPayload
25 import org.onap.ccsdk.cds.error.catalog.core.GrpcErrorCodes
26 import org.onap.ccsdk.cds.error.catalog.core.HttpErrorCodes
27 import org.onap.ccsdk.cds.error.catalog.core.utils.ErrorCatalogUtils
28 import org.apache.commons.lang3.exception.ExceptionUtils
29 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
30 import org.springframework.stereotype.Service
31 import javax.annotation.PostConstruct
32
33 @Service
34 @ConditionalOnBean(ErrorCatalogLoadService::class)
35 open class ErrorCatalogService(private var errorCatalogLoadService: ErrorCatalogLoadService) {
36
37     @PostConstruct
38     open fun init() = runBlocking {
39         errorCatalogLoadService.loadErrorCatalog()
40     }
41
42     fun errorPayload(errorCatalogException: ErrorCatalogException): ErrorPayload {
43         val errorCatalog = getErrorCatalog(errorCatalogException)
44         val errorPayload: ErrorPayload
45         if (errorCatalogException.errorPayload == null) {
46             errorPayload = ErrorPayload(errorCatalog.code, errorCatalog.errorId, errorCatalog.getMessage())
47         } else {
48             errorPayload = errorCatalogException.errorPayload!!
49             errorPayload.code = errorCatalog.code
50             errorPayload.message = errorCatalog.getMessage()
51             errorPayload.status = errorCatalog.errorId
52         }
53         if (errorCatalogException.cause != null) {
54             errorPayload.debugMessage = ExceptionUtils.getStackTrace(errorCatalogException.cause)
55         }
56         return errorPayload
57     }
58
59     fun getErrorCatalog(errorCatalogException: ErrorCatalogException): ErrorCatalog {
60         val errorMessage = getMessage(errorCatalogException.domain, errorCatalogException.name)
61         val errorCode =
62             if (errorCatalogException.code == -1) {
63                 getProtocolErrorCode(
64                     errorCatalogException.protocol,
65                     errorCatalogException.name
66                 )
67             } else {
68                 errorCatalogException.code
69             }
70         val action: String
71         val errorCause: String
72         if (errorMessage.isNullOrEmpty()) {
73             action = errorCatalogException.action
74             errorCause = errorCatalogException.message ?: ""
75         } else {
76             action = ErrorCatalogUtils.readErrorActionFromMessage(errorMessage)
77             errorCause = errorCatalogException.message ?: ErrorCatalogUtils.readErrorCauseFromMessage(errorMessage)
78         }
79
80         return ErrorCatalog(
81                 errorCatalogException.name,
82                 errorCatalogException.domain,
83                 errorCode,
84                 action,
85                 errorCause
86         )
87     }
88
89     private fun getProtocolErrorCode(protocol: String, type: String): Int {
90         return when (protocol) {
91             ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC -> GrpcErrorCodes.code(type)
92             else -> HttpErrorCodes.code(type)
93         }
94     }
95
96     private fun getMessage(domain: String, key: String): String? {
97         return errorCatalogLoadService.getErrorMessage(domain, key)
98     }
99 }