2 * Copyright © 2018 IBM.
3 * Modifications Copyright © 2017-2018 AT&T Intellectual Property.
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.apps.blueprintsprocessor.functions.resource.resolution.processor
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
42 * DatabaseResourceAssignmentProcessor
44 * @author Kapil Singal
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() {
51 private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java)
53 override fun getName(): String {
54 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-primary-db"
57 override fun process(resourceAssignment: ResourceAssignment) {
59 validate(resourceAssignment)
61 // Check if It has Input
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)
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" }
79 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping)
81 val resolvedSql = resolveFromInputKeyMapping(sql, resolvedInputKeyMapping)
83 logger.info("$dSource dictionary information : ($resolvedSql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
84 val jdbcTemplate = blueprintDBLibService(sourceProperties)
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)")
90 populateResource(resourceAssignment, sourceProperties, rows)
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)
102 private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): NamedParameterJdbcTemplate {
103 return if (checkNotEmpty(sourceProperties.endpointSelector)) {
104 val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!)
105 dBLibGenericService.remoteJdbcTemplate(dbPropertiesJson)
107 dBLibGenericService.primaryJdbcTemplate()
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}"
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)
127 val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
128 logger.info("Response processing type($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)
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()
141 if (entrySchemaType in BluePrintTypes.validPrimitiveTypes()) {
142 val dbColumnValue = it[outputKeyMapping[dName]]
144 JacksonUtils.populatePrimitiveValues(dbColumnValue!!, entrySchemaType, arrayNode)
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)
152 arrayNode.add(arrayChildNode)
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)
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)
168 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
169 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
174 override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {