b8a649eb0f8f4e5ddf3fe9ce63b59564721ba643
[ccsdk/cds.git] / ms / error-catalog / services / src / main / kotlin / org / onap / ccsdk / error / catalog / services / ErrorCatalogLoadService.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 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
31
32 interface ErrorCatalogLoadService {
33
34     suspend fun loadErrorCatalog()
35
36     fun getErrorMessage(domain: String, key: String): String?
37
38     fun getErrorMessage(attribute: String): String?
39 }
40
41 /**
42  * Representation of Blueprint Error Message lib from database service to load the properties
43  */
44 @Service
45 @ConditionalOnBean(ErrorCatalogDBService::class)
46 open class ErrorCatalogLoadDBService(
47     private var errorCatalogProperties: ErrorCatalogProperties,
48     private var errorCatalogDBService: ErrorCatalogDBService
49 ) : ErrorCatalogLoadService {
50
51     private var log = logger(ErrorCatalogLoadDBService::class)
52
53     lateinit var errorMessageInDB: Map<String, ErrorMessageModel>
54
55     override suspend fun loadErrorCatalog() {
56         log.info("Application ID: ${errorCatalogProperties.applicationId} > Initializing error catalog message from database...")
57         errorMessageInDB = getErrorMessagesFromDB()
58     }
59
60     override fun getErrorMessage(domain: String, key: String): String? {
61         return getErrorMessage("$domain.${key.toLowerCase()}")
62     }
63
64     override fun getErrorMessage(attribute: String): String? {
65         val errorMessage = findErrorMessage(attribute)
66         return prepareErrorMessage(errorMessage)
67     }
68
69     /**
70      * Parses the error-messages.properties file which contains error messages
71      */
72     private suspend fun getErrorMessagesFromDB(): Map<String, ErrorMessageModel> {
73         return errorCatalogDBService.getAllErrorMessagesByApplication(errorCatalogProperties.applicationId)
74     }
75
76     private fun findErrorMessage(attribute: String): ErrorMessageModel? {
77         return if (errorMessageInDB.containsKey(attribute)) {
78             errorMessageInDB[attribute]
79         } else {
80             null
81         }
82     }
83
84     private fun prepareErrorMessage(errorMessage: ErrorMessageModel?): String? {
85         return if (errorMessage != null) {
86             "cause=${errorMessage.cause}, action=${errorMessage.action}"
87         } else {
88             null
89         }
90     }
91 }
92
93 /**
94  * Representation of Blueprint Error Message lib property service to load the properties
95  */
96 @Service
97 @ConditionalOnProperty(
98     name = [ErrorMessageLibConstants.ERROR_CATALOG_TYPE],
99     havingValue = ErrorMessageLibConstants.ERROR_CATALOG_TYPE_PROPERTIES
100 )
101 open class ErrorCatalogLoadPropertyService(private var errorCatalogProperties: ErrorCatalogProperties) :
102     ErrorCatalogLoadService {
103
104     private val propertyFileName = ErrorMessageLibConstants.ERROR_CATALOG_PROPERTIES_FILENAME
105     private var propertyFileBaseDirectory = Paths.get(errorCatalogProperties.errorDefinitionDir)
106     private var propertyFilePath = propertyFileBaseDirectory.resolve(propertyFileName)
107
108     private var log = logger(ErrorCatalogLoadPropertyService::class)
109
110     lateinit var properties: Properties
111
112     override suspend fun loadErrorCatalog() {
113         log.info("Application ID: ${errorCatalogProperties.applicationId} > Initializing error catalog message from properties...")
114         properties = parseErrorMessagesProps()
115     }
116
117     override fun getErrorMessage(domain: String, key: String): String? {
118         return getErrorMessage("$domain.${key.toLowerCase()}")
119     }
120
121     override fun getErrorMessage(attribute: String): String? {
122         return properties.getProperty(attribute)
123     }
124
125     /**
126      * Parses the error-messages.properties file which contains error messages
127      */
128     private fun parseErrorMessagesProps(): Properties {
129         var inputStream: InputStream? = null
130         val props = Properties()
131         try {
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.")
140         } finally {
141             inputStream!!.close()
142         }
143         return props
144     }
145 }