2 * Copyright © 2020 IBM, Bell Canada.
3 * Modifications Copyright © 2019-2020 AT&T Intellectual Property.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.error.catalog.services
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
34 @ConditionalOnBean(ErrorCatalogLoadService::class)
35 open class ErrorCatalogService(private var errorCatalogLoadService: ErrorCatalogLoadService) {
38 open fun init() = runBlocking {
39 errorCatalogLoadService.loadErrorCatalog()
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())
48 errorPayload = errorCatalogException.errorPayload!!
49 errorPayload.code = errorCatalog.code
50 errorPayload.message = errorCatalog.getMessage()
51 errorPayload.status = errorCatalog.errorId
53 if (errorCatalogException.cause != null) {
54 errorPayload.debugMessage = ExceptionUtils.getStackTrace(errorCatalogException.cause)
59 fun getErrorCatalog(errorCatalogException: ErrorCatalogException): ErrorCatalog {
60 val errorMessage = getMessage(errorCatalogException.domain, errorCatalogException.name)
62 if (errorCatalogException.code == -1) {
64 errorCatalogException.protocol,
65 errorCatalogException.name
68 errorCatalogException.code
71 val errorCause: String
72 if (errorMessage.isNullOrEmpty()) {
73 action = errorCatalogException.action
74 errorCause = errorCatalogException.message ?: ""
76 action = ErrorCatalogUtils.readErrorActionFromMessage(errorMessage)
77 errorCause = errorCatalogException.message ?: ErrorCatalogUtils.readErrorCauseFromMessage(errorMessage)
81 errorCatalogException.name,
82 errorCatalogException.domain,
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)
96 private fun getMessage(domain: String, key: String): String? {
97 return errorCatalogLoadService.getErrorMessage(domain, key)