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 org.onap.ccsdk.error.catalog.core.ErrorMessageLibConstants
21 import org.onap.ccsdk.error.catalog.core.logger
22 import org.onap.ccsdk.error.catalog.services.domain.ErrorMessageModel
23 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
24 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
25 import org.springframework.stereotype.Service
26 import java.io.FileNotFoundException
27 import java.io.IOException
28 import java.io.InputStream
29 import java.nio.file.Paths
30 import java.util.Properties
32 interface ErrorCatalogLoadService {
34 suspend fun loadErrorCatalog()
36 fun getErrorMessage(domain: String, key: String): String?
38 fun getErrorMessage(attribute: String): String?
42 * Representation of Blueprint Error Message lib from database service to load the properties
45 @ConditionalOnBean(ErrorCatalogDBService::class)
46 open class ErrorCatalogLoadDBService(
47 private var errorCatalogProperties: ErrorCatalogProperties,
48 private var errorCatalogDBService: ErrorCatalogDBService
49 ) : ErrorCatalogLoadService {
51 private var log = logger(ErrorCatalogLoadDBService::class)
53 lateinit var errorMessageInDB: Map<String, ErrorMessageModel>
55 override suspend fun loadErrorCatalog() {
56 log.info("Application ID: ${errorCatalogProperties.applicationId} > Initializing error catalog message from database...")
57 errorMessageInDB = getErrorMessagesFromDB()
60 override fun getErrorMessage(domain: String, key: String): String? {
61 return getErrorMessage("$domain.${key.toLowerCase()}")
64 override fun getErrorMessage(attribute: String): String? {
65 val errorMessage = findErrorMessage(attribute)
66 return prepareErrorMessage(errorMessage)
70 * Parses the error-messages.properties file which contains error messages
72 private suspend fun getErrorMessagesFromDB(): Map<String, ErrorMessageModel> {
73 return errorCatalogDBService.getAllErrorMessagesByApplication(errorCatalogProperties.applicationId)
76 private fun findErrorMessage(attribute: String): ErrorMessageModel? {
77 return if (errorMessageInDB.containsKey(attribute)) {
78 errorMessageInDB[attribute]
84 private fun prepareErrorMessage(errorMessage: ErrorMessageModel?): String? {
85 return if (errorMessage != null) {
86 "cause=${errorMessage.cause}, action=${errorMessage.action}"
94 * Representation of Blueprint Error Message lib property service to load the properties
97 @ConditionalOnProperty(
98 name = [ErrorMessageLibConstants.ERROR_CATALOG_TYPE],
99 havingValue = ErrorMessageLibConstants.ERROR_CATALOG_TYPE_PROPERTIES
101 open class ErrorCatalogLoadPropertyService(private var errorCatalogProperties: ErrorCatalogProperties) :
102 ErrorCatalogLoadService {
104 private val propertyFileName = ErrorMessageLibConstants.ERROR_CATALOG_PROPERTIES_FILENAME
105 private var propertyFileBaseDirectory = Paths.get(errorCatalogProperties.errorDefinitionDir)
106 private var propertyFilePath = propertyFileBaseDirectory.resolve(propertyFileName)
108 private var log = logger(ErrorCatalogLoadPropertyService::class)
110 lateinit var properties: Properties
112 override suspend fun loadErrorCatalog() {
113 log.info("Application ID: ${errorCatalogProperties.applicationId} > Initializing error catalog message from properties...")
114 properties = parseErrorMessagesProps()
117 override fun getErrorMessage(domain: String, key: String): String? {
118 return getErrorMessage("$domain.${key.toLowerCase()}")
121 override fun getErrorMessage(attribute: String): String? {
122 return properties.getProperty(attribute)
126 * Parses the error-messages.properties file which contains error messages
128 private fun parseErrorMessagesProps(): Properties {
129 var inputStream: InputStream? = null
130 val props = Properties()
132 inputStream = propertyFilePath.toFile().inputStream()
133 props.load(inputStream)
134 } catch (e: FileNotFoundException) {
135 log.error("Application ID: ${errorCatalogProperties.applicationId} > Property File '$propertyFileName}' " +
136 "not found in the application directory.")
137 } catch (e: IOException) {
138 log.error("Application ID: ${errorCatalogProperties.applicationId} > Fail to load property file " +
139 "'$propertyFileName}' for message errors.")
141 inputStream!!.close()