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.error.catalog.services
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
33 @ConditionalOnBean(ErrorCatalogLoadService::class)
34 open class ErrorCatalogService(private var errorCatalogLoadService: ErrorCatalogLoadService) {
37 open fun init() = runBlocking {
38 errorCatalogLoadService.loadErrorCatalog()
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()
51 fun getErrorCatalog(errorCatalogException: ErrorCatalogException): ErrorCatalog {
52 val errorMessage = getMessage(errorCatalogException.domain, errorCatalogException.name)
54 if (errorCatalogException.code == -1) {
56 errorCatalogException.protocol,
57 errorCatalogException.name
60 errorCatalogException.code
63 val errorCause: String
64 if (errorMessage.isNullOrEmpty()) {
65 action = errorCatalogException.action
66 errorCause = errorCatalogException.message ?: ""
68 action = ErrorCatalogUtils.readErrorActionFromMessage(errorMessage)
69 errorCause = errorCatalogException.message ?: ErrorCatalogUtils.readErrorCauseFromMessage(errorMessage)
73 errorCatalogException.name,
74 errorCatalogException.domain,
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)
88 private fun getMessage(domain: String, key: String): String? {
89 return errorCatalogLoadService.getErrorMessage(domain, key)