Merge "Secure Kafka Authentication"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / DatabaseResourceAssignmentProcessor.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *  Modifications Copyright © 2017-2018 AT&T Intellectual Property, Bell Canada.
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.cds.blueprintsprocessor.functions.resource.resolution.processor
19
20 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibGenericService
21 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.BluePrintDBLibPropertyService
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDBLibGenericService
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.DatabaseResourceSource
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServiceDomains
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.isNotEmpty
29 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
30 import org.onap.ccsdk.cds.controllerblueprints.core.nullToEmpty
31 import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.KeyIdentifier
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
35 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
36 import org.slf4j.LoggerFactory
37 import org.springframework.beans.factory.config.ConfigurableBeanFactory
38 import org.springframework.context.annotation.Scope
39 import org.springframework.stereotype.Service
40 import java.util.HashMap
41
42 /**
43  * DatabaseResourceAssignmentProcessor
44  *
45  * @author Kapil Singal
46  */
47 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db")
48 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
49 open class DatabaseResourceAssignmentProcessor(
50     private val bluePrintDBLibPropertyService: BluePrintDBLibPropertyService,
51     private val primaryDBLibGenericService: PrimaryDBLibGenericService
52 ) : ResourceAssignmentProcessor() {
53
54     private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java)
55
56     override fun getName(): String {
57         return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db"
58     }
59
60     override suspend fun processNB(resourceAssignment: ResourceAssignment) {
61         try {
62             validate(resourceAssignment)
63             // Check if It has Input
64             if (!setFromInput(resourceAssignment)) {
65                 setValueFromDB(resourceAssignment)
66             }
67             // Check the value has populated for mandatory case
68             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
69         } catch (e: BluePrintProcessorException) {
70             val errorMsg = "Failed to process Database resource resolution in template key ($resourceAssignment) assignments."
71             throw e.updateErrorMessage(ExecutionServiceDomains.RESOURCE_RESOLUTION, errorMsg,
72                     "Wrong resource definition or DB resolution failed.")
73         } catch (e: Exception) {
74             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
75             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
76         }
77     }
78
79     private fun setValueFromDB(resourceAssignment: ResourceAssignment) {
80         val dName = resourceAssignment.dictionaryName!!
81         val dSource = resourceAssignment.dictionarySource!!
82         val resourceDefinition = resourceDefinition(dName)
83
84         /** Check Resource Assignment has the source definitions, If not get from Resource Definition **/
85         val resourceSource = resourceAssignment.dictionarySourceDefinition
86             ?: resourceDefinition?.sources?.get(dSource)
87             ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
88         val resourceSourceProperties = checkNotNull(resourceSource.properties) {
89             "failed to get source properties for $dName "
90         }
91         val sourceProperties =
92             JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
93
94         val sql = checkNotNull(sourceProperties.query) {
95             "failed to get request query for $dName under $dSource properties"
96         }
97         val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) {
98             "failed to get input-key-mappings for $dName under $dSource properties"
99         }
100
101         sourceProperties.inputKeyMapping
102                 ?.mapValues { raRuntimeService.getDictionaryStore(it.value) }
103                 ?.map { KeyIdentifier(it.key, it.value) }
104                 ?.let { resourceAssignment.keyIdentifiers.addAll(it) }
105
106         logger.info(
107             "DatabaseResource ($dSource) dictionary information: " +
108                     "Query:($sql), input-key-mapping:($inputKeyMapping), output-key-mapping:(${sourceProperties.outputKeyMapping})"
109         )
110         val jdbcTemplate = blueprintDBLibService(sourceProperties, dSource)
111
112         val rows = jdbcTemplate.query(sql, populateNamedParameter(inputKeyMapping))
113         if (rows.isNullOrEmpty()) {
114             logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($sql)")
115         } else {
116             populateResource(resourceAssignment, sourceProperties, rows)
117         }
118     }
119
120     private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource, selector: String): BluePrintDBLibGenericService {
121         return if (isNotEmpty(sourceProperties.endpointSelector)) {
122             val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!)
123             bluePrintDBLibPropertyService.JdbcTemplate(dbPropertiesJson)
124         } else {
125             bluePrintDBLibPropertyService.JdbcTemplate(selector)
126         }
127     }
128
129     @Throws(BluePrintProcessorException::class)
130     private fun validate(resourceAssignment: ResourceAssignment) {
131         checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
132         checkNotEmpty(resourceAssignment.dictionaryName) {
133             "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
134         }
135         check(resourceAssignment.dictionarySource in getListOfDBSources()) {
136             "resource assignment source is not ${ResourceDictionaryConstants.PROCESSOR_DB} but it is ${resourceAssignment.dictionarySource}"
137         }
138     }
139
140     // placeholder to get the list of DB sources.
141     // TODO: This will be replaced with a DB
142     private fun getListOfDBSources(): Array<String> = arrayOf(ResourceDictionaryConstants.PROCESSOR_DB)
143
144     private fun populateNamedParameter(inputKeyMapping: Map<String, String>): Map<String, Any> {
145         val namedParameters = HashMap<String, Any>()
146         inputKeyMapping.forEach {
147             val expressionValue = raRuntimeService.getResolutionStore(it.value).textValue()
148             logger.trace("Reference dictionary key (${it.key}) resulted in value ($expressionValue)")
149             namedParameters[it.key] = expressionValue
150         }
151         if (namedParameters.isNotEmpty()) {
152             logger.info("Parameter information : ($namedParameters)")
153         }
154         return namedParameters
155     }
156
157     @Throws(BluePrintProcessorException::class)
158     private fun populateResource(
159         resourceAssignment: ResourceAssignment,
160         sourceProperties: DatabaseResourceSource,
161         rows: List<Map<String, Any>>
162     ) {
163         val dName = resourceAssignment.dictionaryName
164         val dSource = resourceAssignment.dictionarySource
165         val type = nullToEmpty(resourceAssignment.property?.type)
166
167         val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
168             "failed to get output-key-mappings for $dName under $dSource properties"
169         }
170         logger.info("Response processing type ($type)")
171
172         val responseNode = checkNotNull(JacksonUtils.getJsonNode(rows)) {
173             "Failed to get database query result into Json node."
174         }
175
176         val parsedResponseNode = ResourceAssignmentUtils.parseResponseNode(
177             responseNode, resourceAssignment,
178             raRuntimeService, outputKeyMapping
179         )
180         ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, parsedResponseNode)
181     }
182
183     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
184         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
185     }
186 }