Mesh input-key-mapping using velocity
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / functions / resource / resolution / processor / DatabaseResourceAssignmentProcessor.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *  Modifications Copyright © 2017-2018 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.apps.blueprintsprocessor.functions.resource.resolution.processor
19
20 import com.fasterxml.jackson.databind.node.JsonNodeFactory
21 import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.DBLibGenericService
22 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.DatabaseResourceSource
23 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
24 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
27 import org.onap.ccsdk.apps.controllerblueprints.core.checkEqualsOrThrow
28 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty
29 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
30 import org.onap.ccsdk.apps.controllerblueprints.core.nullToEmpty
31 import org.onap.ccsdk.apps.controllerblueprints.core.returnNotEmptyOrThrow
32 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
34 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
35 import org.slf4j.LoggerFactory
36 import org.springframework.beans.factory.config.ConfigurableBeanFactory
37 import org.springframework.context.annotation.Scope
38 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
39 import org.springframework.stereotype.Service
40
41 /**
42  * DatabaseResourceAssignmentProcessor
43  *
44  * @author Kapil Singal
45  */
46 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-primary-db")
47 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
48 open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: DBLibGenericService)
49     : ResourceAssignmentProcessor() {
50
51     private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java)
52
53     override fun getName(): String {
54         return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-primary-db"
55     }
56
57     override fun process(resourceAssignment: ResourceAssignment) {
58         try {
59             validate(resourceAssignment)
60
61             // Check if It has Input
62             try {
63                 val value = raRuntimeService.getInputValue(resourceAssignment.name)
64                 logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)")
65                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
66             } catch (e: BluePrintProcessorException) {
67                 val dName = resourceAssignment.dictionaryName
68                 val dSource = resourceAssignment.dictionarySource
69                 val resourceDefinition = resourceDictionaries[dName]
70                         ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
71                 val resourceSource = resourceDefinition.sources[dSource]
72                         ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
73                 val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
74                 val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
75
76                 val sql = checkNotNull(sourceProperties.query) { "failed to get request query for $dName under $dSource properties" }
77                 val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
78
79                 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping)
80
81                 val resolvedSql = resolveFromInputKeyMapping(sql, resolvedInputKeyMapping)
82
83                 logger.info("$dSource dictionary information : ($resolvedSql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
84                 val jdbcTemplate = blueprintDBLibService(sourceProperties)
85
86                 val rows = jdbcTemplate.queryForList(resolvedSql, resolvedInputKeyMapping)
87                 if (rows.isNullOrEmpty()) {
88                     logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($resolvedSql)")
89                 } else {
90                     populateResource(resourceAssignment, sourceProperties, rows)
91                 }
92             }
93
94             // Check the value has populated for mandatory case
95             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
96         } catch (e: Exception) {
97             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
98             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
99         }
100     }
101
102     private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): NamedParameterJdbcTemplate {
103         return if (checkNotEmpty(sourceProperties.endpointSelector)) {
104             val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!)
105             dBLibGenericService.remoteJdbcTemplate(dbPropertiesJson)
106         } else {
107             dBLibGenericService.primaryJdbcTemplate()
108         }
109
110     }
111
112     @Throws(BluePrintProcessorException::class)
113     private fun validate(resourceAssignment: ResourceAssignment) {
114         checkNotEmptyOrThrow(resourceAssignment.name, "resource assignment template key is not defined")
115         checkNotEmptyOrThrow(resourceAssignment.dictionaryName, "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
116         checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PRIMARY_DB, resourceAssignment.dictionarySource) {
117             "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_DB} but it is ${resourceAssignment.dictionarySource}"
118         }
119     }
120
121     @Throws(BluePrintProcessorException::class)
122     private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List<Map<String, Any>>) {
123         val dName = resourceAssignment.dictionaryName
124         val dSource = resourceAssignment.dictionarySource
125         val type = nullToEmpty(resourceAssignment.property?.type)
126
127         val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
128         logger.info("Response processing type($type)")
129
130         // Primitive Types
131         when(type) {
132             in BluePrintTypes.validPrimitiveTypes() -> {
133                 val dbColumnValue = rows[0][outputKeyMapping[dName]]
134                 logger.info("For template key (${resourceAssignment.name}) setting value as ($dbColumnValue)")
135                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, dbColumnValue)
136             }
137             in BluePrintTypes.validCollectionTypes() -> {
138                 val entrySchemaType = returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
139                 var arrayNode = JsonNodeFactory.instance.arrayNode()
140                 rows.forEach {
141                     if (entrySchemaType in BluePrintTypes.validPrimitiveTypes()) {
142                         val dbColumnValue = it[outputKeyMapping[dName]]
143                         // Add Array JSON
144                         JacksonUtils.populatePrimitiveValues(dbColumnValue!!, entrySchemaType, arrayNode)
145                     } else {
146                         val arrayChildNode = JsonNodeFactory.instance.objectNode()
147                         for (mapping in outputKeyMapping.entries) {
148                             val dbColumnValue = checkNotNull(it[mapping.key])
149                             val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, mapping.key)
150                             JacksonUtils.populatePrimitiveValues(mapping.key, dbColumnValue, propertyTypeForDataType, arrayChildNode)
151                         }
152                         arrayNode.add(arrayChildNode)
153                     }
154                 }
155                 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
156                 // Set the List of Complex Values
157                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
158             }
159             else -> {
160                 // Complex Types
161                 val row = rows[0]
162                 var objectNode = JsonNodeFactory.instance.objectNode()
163                 for (mapping in outputKeyMapping.entries) {
164                     val dbColumnValue = checkNotNull(row[mapping.key])
165                     val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, type, mapping.key)
166                     JacksonUtils.populatePrimitiveValues(mapping.key, dbColumnValue, propertyTypeForDataType, objectNode)
167                 }
168                 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
169                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
170             }
171         }
172     }
173
174     override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
175     }
176 }