Handle all Data Type and DD with complex type
[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.BluePrintDBLibPropertySevice
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.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.controllerblueprints.core.*
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.config.ConfigurableBeanFactory
32 import org.springframework.context.annotation.Scope
33 import org.springframework.stereotype.Service
34 import java.util.*
35
36 /**
37  * DatabaseResourceAssignmentProcessor
38  *
39  * @author Kapil Singal
40  */
41 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db")
42 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
43 open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropertySevice: BluePrintDBLibPropertySevice,
44                                                private val primaryDBLibGenericService: PrimaryDBLibGenericService)
45     : ResourceAssignmentProcessor() {
46
47     private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java)
48
49     override fun getName(): String {
50         return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db"
51     }
52
53     override suspend fun processNB(resourceAssignment: ResourceAssignment) {
54         try {
55             validate(resourceAssignment)
56
57             // Check if It has Input
58             try {
59                 val value = raRuntimeService.getInputValue(resourceAssignment.name)
60                 if (value.returnNullIfMissing() != null) {
61                     logger.info("processor-db source template key (${resourceAssignment.name}) found from input and value is ($value)")
62                     ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
63                 } else {
64                     setValueFromDB(resourceAssignment)
65                 }
66             } catch (e: BluePrintProcessorException) {
67                 setValueFromDB(resourceAssignment)
68             }
69
70             // Check the value has populated for mandatory case
71             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
72         } catch (e: Exception) {
73             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
74             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
75         }
76     }
77
78     private fun setValueFromDB(resourceAssignment: ResourceAssignment) {
79         val dName = resourceAssignment.dictionaryName!!
80         val dSource = resourceAssignment.dictionarySource!!
81         val resourceDefinition = resourceDefinition(dName)
82
83         /** Check Resource Assignment has the source definitions, If not get from Resource Definition **/
84         val resourceSource = resourceAssignment.dictionarySourceDefinition
85                 ?: resourceDefinition?.sources?.get(dSource)
86                 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
87         val resourceSourceProperties = checkNotNull(resourceSource.properties) {
88             "failed to get source properties for $dName "
89         }
90         val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
91
92         val sql = checkNotNull(sourceProperties.query) {
93             "failed to get request query for $dName under $dSource properties"
94         }
95         val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) {
96             "failed to get input-key-mappings for $dName under $dSource properties"
97         }
98
99         logger.info("$dSource dictionary information : ($sql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
100         val jdbcTemplate = blueprintDBLibService(sourceProperties)
101
102         val rows = jdbcTemplate.query(sql, populateNamedParameter(inputKeyMapping))
103         if (rows.isNullOrEmpty()) {
104             logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($sql)")
105         } else {
106             populateResource(resourceAssignment, sourceProperties, rows)
107         }
108     }
109
110     private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): BluePrintDBLibGenericService {
111         return if (isNotEmpty(sourceProperties.endpointSelector)) {
112             val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!)
113             bluePrintDBLibPropertySevice.JdbcTemplate(dbPropertiesJson)
114         } else {
115             primaryDBLibGenericService
116         }
117
118     }
119
120     @Throws(BluePrintProcessorException::class)
121     private fun validate(resourceAssignment: ResourceAssignment) {
122         checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
123         checkNotEmpty(resourceAssignment.dictionaryName) {
124             "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
125         }
126         check(resourceAssignment.dictionarySource in arrayOf(ResourceDictionaryConstants.SOURCE_PROCESSOR_DB, ResourceDictionaryConstants.SOURCE_PRIMARY_DB))
127         {
128             "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PROCESSOR_DB} but it is ${resourceAssignment.dictionarySource}"
129         }
130     }
131
132     private fun populateNamedParameter(inputKeyMapping: Map<String, String>): Map<String, Any> {
133         val namedParameters = HashMap<String, Any>()
134         inputKeyMapping.forEach {
135             val expressionValue = raRuntimeService.getDictionaryStore(it.value).textValue()
136             logger.trace("Reference dictionary key (${it.key}) resulted in value ($expressionValue)")
137             namedParameters[it.key] = expressionValue
138         }
139         logger.info("Parameter information : ($namedParameters)")
140         return namedParameters
141     }
142
143     @Throws(BluePrintProcessorException::class)
144     private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List<Map<String, Any>>) {
145         val dName = resourceAssignment.dictionaryName
146         val dSource = resourceAssignment.dictionarySource
147         val type = nullToEmpty(resourceAssignment.property?.type)
148
149         val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
150             "failed to get output-key-mappings for $dName under $dSource properties"
151         }
152         logger.info("Response processing type($type)")
153
154         val responseNode = checkNotNull(JacksonUtils.getJsonNode(rows)) {
155             "Failed to get database query result into Json node."
156         }
157
158         val parsedResponseNode = ResourceAssignmentUtils.parseResponseNode(responseNode, resourceAssignment,
159                 raRuntimeService, outputKeyMapping)
160         ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, parsedResponseNode)
161     }
162
163     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
164         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
165     }
166 }