2 * Copyright © 2018 IBM.
3 * Modifications Copyright © 2017-2018 AT&T Intellectual Property, Bell Canada.
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.cds.blueprintsprocessor.functions.resource.resolution.processor
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
37 * DatabaseResourceAssignmentProcessor
39 * @author Kapil Singal
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() {
47 private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java)
49 override fun getName(): String {
50 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db"
53 override suspend fun processNB(resourceAssignment: ResourceAssignment) {
55 validate(resourceAssignment)
57 // Check if It has Input
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)
64 setValueFromDB(resourceAssignment)
66 } catch (e: BluePrintProcessorException) {
67 setValueFromDB(resourceAssignment)
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)
78 private fun setValueFromDB(resourceAssignment: ResourceAssignment) {
79 val dName = resourceAssignment.dictionaryName!!
80 val dSource = resourceAssignment.dictionarySource!!
81 val resourceDefinition = resourceDefinition(dName)
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 "
90 val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
92 val sql = checkNotNull(sourceProperties.query) {
93 "failed to get request query for $dName under $dSource properties"
95 val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) {
96 "failed to get input-key-mappings for $dName under $dSource properties"
99 logger.info("$dSource dictionary information : ($sql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
100 val jdbcTemplate = blueprintDBLibService(sourceProperties)
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)")
106 populateResource(resourceAssignment, sourceProperties, rows)
110 private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): BluePrintDBLibGenericService {
111 return if (isNotEmpty(sourceProperties.endpointSelector)) {
112 val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!)
113 bluePrintDBLibPropertySevice.JdbcTemplate(dbPropertiesJson)
115 primaryDBLibGenericService
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})"
126 check(resourceAssignment.dictionarySource in arrayOf(ResourceDictionaryConstants.SOURCE_PROCESSOR_DB, ResourceDictionaryConstants.SOURCE_PRIMARY_DB))
128 "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PROCESSOR_DB} but it is ${resourceAssignment.dictionarySource}"
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
139 logger.info("Parameter information : ($namedParameters)")
140 return namedParameters
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)
149 val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
150 "failed to get output-key-mappings for $dName under $dSource properties"
152 logger.info("Response processing type($type)")
154 val responseNode = checkNotNull(JacksonUtils.getJsonNode(rows)) {
155 "Failed to get database query result into Json node."
158 val parsedResponseNode = ResourceAssignmentUtils.parseResponseNode(responseNode, resourceAssignment,
159 raRuntimeService, outputKeyMapping)
160 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, parsedResponseNode)
163 override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
164 raRuntimeService.getBluePrintError().addError(runtimeException.message!!)