Error Catalog Management Core Library.
[ccsdk/cds.git] / ms / error-catalog / services / src / main / kotlin / org / onap / ccsdk / 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.error.catalog.services
19
20 import kotlinx.coroutines.runBlocking
21 import org.onap.ccsdk.error.catalog.core.ErrorCatalog
22 import org.onap.ccsdk.error.catalog.core.ErrorCatalogException
23 import org.onap.ccsdk.error.catalog.core.ErrorMessageLibConstants
24 import org.onap.ccsdk.error.catalog.core.ErrorPayload
25 import org.onap.ccsdk.error.catalog.core.GrpcErrorCodes
26 import org.onap.ccsdk.error.catalog.core.HttpErrorCodes
27 import org.onap.ccsdk.error.catalog.services.utils.ErrorCatalogUtils
28 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
29 import org.springframework.stereotype.Service
30 import javax.annotation.PostConstruct
31
32 @Service
33 @ConditionalOnBean(ErrorCatalogLoadService::class)
34 open class ErrorCatalogService(private var errorCatalogLoadService: ErrorCatalogLoadService) {
35
36     @PostConstruct
37     open fun init() = runBlocking {
38         errorCatalogLoadService.loadErrorCatalog()
39     }
40
41     fun errorPayload(errorCatalogException: ErrorCatalogException): ErrorPayload {
42         val errorCatalog = getErrorCatalog(errorCatalogException)
43         val errorPayload = ErrorPayload(errorCatalog.code, errorCatalog.errorId, errorCatalog.getMessage())
44         errorPayload.subErrors.addAll(errorCatalogException.errorPayload!!.subErrors)
45         if (errorCatalogException.cause != null) {
46             errorPayload.debugMessage = errorCatalogException.cause!!.printStackTrace().toString()
47         }
48         return errorPayload
49     }
50
51     fun getErrorCatalog(errorCatalogException: ErrorCatalogException): ErrorCatalog {
52         val errorMessage = getMessage(errorCatalogException.domain, errorCatalogException.name)
53         val errorCode =
54             if (errorCatalogException.code == -1) {
55                 getProtocolErrorCode(
56                     errorCatalogException.protocol,
57                     errorCatalogException.name
58                 )
59             } else {
60                 errorCatalogException.code
61             }
62         val action: String
63         val errorCause: String
64         if (errorMessage.isNullOrEmpty()) {
65             action = errorCatalogException.action
66             errorCause = errorCatalogException.message ?: ""
67         } else {
68             action = ErrorCatalogUtils.readErrorActionFromMessage(errorMessage)
69             errorCause = errorCatalogException.message ?: ErrorCatalogUtils.readErrorCauseFromMessage(errorMessage)
70         }
71
72         return ErrorCatalog(
73             errorCatalogException.name,
74             errorCatalogException.domain,
75             errorCode,
76             action,
77             errorCause
78         )
79     }
80
81     private fun getProtocolErrorCode(protocol: String, type: String): Int {
82         return when (protocol) {
83             ErrorMessageLibConstants.ERROR_CATALOG_PROTOCOL_GRPC -> GrpcErrorCodes.code(type)
84             else -> HttpErrorCodes.code(type)
85         }
86     }
87
88     private fun getMessage(domain: String, key: String): String? {
89         return errorCatalogLoadService.getErrorMessage(domain, key)
90     }
91 }