Improve data type handling
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / RestResourceResolutionProcessor.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.cds.blueprintsprocessor.functions.resource.resolution.processor
19
20 import com.fasterxml.jackson.databind.node.ArrayNode
21 import com.fasterxml.jackson.databind.node.MissingNode
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.RestResourceSource
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.*
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
31 import org.slf4j.LoggerFactory
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory
33 import org.springframework.context.annotation.Scope
34 import org.springframework.stereotype.Service
35
36 /**
37  * RestResourceResolutionProcessor
38  *
39  * @author Kapil Singal
40  */
41 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest")
42 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
43 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
44     : ResourceAssignmentProcessor() {
45
46     private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
47
48     override fun getName(): String {
49         return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
50     }
51
52     override suspend fun processNB(resourceAssignment: ResourceAssignment) {
53         try {
54             validate(resourceAssignment)
55
56             // Check if It has Input
57             val value = getFromInput(resourceAssignment)
58             if (value == null || value is MissingNode) {
59                 val dName = resourceAssignment.dictionaryName
60                 val dSource = resourceAssignment.dictionarySource
61                 val resourceDefinition = resourceDictionaries[dName]
62                         ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
63
64                 val resourceSource = resourceDefinition.sources[dSource]
65                         ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
66
67                 val resourceSourceProperties =
68                         checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
69
70                 val sourceProperties =
71                         JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
72
73                 val path = nullToEmpty(sourceProperties.path)
74                 val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) {
75                     "failed to get input-key-mappings for $dName under $dSource properties"
76                 }
77                 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping)
78
79                 // Resolving content Variables
80                 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
81                 val urlPath =
82                         resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
83                 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
84
85                 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
86                 // Get the Rest Client Service
87                 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
88
89                 val response = restClientService.exchangeResource(verb, urlPath, payload)
90                 if (response.isBlank()) {
91                     logger.warn("Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath)")
92                 } else {
93                     populateResource(resourceAssignment, sourceProperties, response, path)
94                 }
95             }
96             // Check the value has populated for mandatory case
97             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
98         } catch (e: Exception) {
99             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
100             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}",
101                     e)
102         }
103     }
104
105     private fun blueprintWebClientService(resourceAssignment: ResourceAssignment,
106                                           restResourceSource: RestResourceSource): BlueprintWebClientService {
107         return if (isNotEmpty(restResourceSource.endpointSelector)) {
108             val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
109             blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
110         } else {
111             blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
112         }
113     }
114
115     @Throws(BluePrintProcessorException::class)
116     private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
117                                  restResponse: String, path: String) {
118         val dName = resourceAssignment.dictionaryName
119         val dSource = resourceAssignment.dictionarySource
120         val type = nullToEmpty(resourceAssignment.property?.type)
121         lateinit var entrySchemaType: String
122
123         val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
124             "failed to get output-key-mappings for $dName under $dSource properties"
125         }
126         logger.info("Response processing type($type)")
127
128         val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) {
129             "Failed to find path ($path) in response ($restResponse)"
130         }
131         logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
132
133
134         when (type) {
135             in BluePrintTypes.validPrimitiveTypes() -> {
136                 logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
137                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
138             }
139             in BluePrintTypes.validCollectionTypes() -> {
140                 // Array Types
141                 entrySchemaType = checkNotEmpty(resourceAssignment.property?.entrySchema?.type) {
142                     "Entry schema is not defined for dictionary ($dName) info"
143                 }
144                 val arrayNode = responseNode as ArrayNode
145
146                 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
147
148                     val responseArrayNode = responseNode.toList()
149                     for (responseSingleJsonNode in responseArrayNode) {
150
151                         val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
152
153                         outputKeyMapping.map {
154                             val responseKeyValue = responseSingleJsonNode.get(it.key)
155                             val propertyTypeForDataType = ResourceAssignmentUtils
156                                     .getPropertyType(raRuntimeService, entrySchemaType, it.key)
157
158                             logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), " +
159                                     "type  ({$propertyTypeForDataType})")
160
161                             JacksonUtils.populateJsonNodeValues(it.value,
162                                     responseKeyValue, propertyTypeForDataType, arrayChildNode)
163                         }
164                         arrayNode.add(arrayChildNode)
165                     }
166                 }
167                 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
168                 // Set the List of Complex Values
169                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
170             }
171             else -> {
172                 // Complex Types
173                 entrySchemaType = checkNotEmpty(resourceAssignment.property?.type) {
174                     "Entry schema is not defined for dictionary ($dName) info"
175                 }
176                 val objectNode = JacksonUtils.objectMapper.createObjectNode()
177                 outputKeyMapping.map {
178                     val responseKeyValue = responseNode.get(it.key)
179                     val propertyTypeForDataType = ResourceAssignmentUtils
180                             .getPropertyType(raRuntimeService, entrySchemaType, it.key)
181
182                     logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type  ({$propertyTypeForDataType})")
183                     JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
184                 }
185
186                 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
187                 // Set the List of Complex Values
188                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
189             }
190         }
191     }
192
193     @Throws(BluePrintProcessorException::class)
194     private fun validate(resourceAssignment: ResourceAssignment) {
195         checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
196         checkNotEmpty(resourceAssignment.dictionaryName) {
197             "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
198         }
199         checkEquals(ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA, resourceAssignment.dictionarySource) {
200             "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA} but it is " +
201                     "${resourceAssignment.dictionarySource}"
202         }
203         checkNotEmpty(resourceAssignment.dictionaryName) {
204             "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
205         }
206     }
207
208     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
209         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
210     }
211
212
213 }