Merge "Python executor parameters sorted again."
[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-2019 AT&T, 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 com.fasterxml.jackson.databind.node.ArrayNode
21 import com.fasterxml.jackson.databind.node.MissingNode
22 import com.fasterxml.jackson.databind.node.NullNode
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.RestResourceSource
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
28 import org.onap.ccsdk.cds.controllerblueprints.core.*
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
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 || value is NullNode) {
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 =
75                     checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
76                 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping).toMutableMap()
77
78                 // Resolving content Variables
79                 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
80                 val urlPath =
81                     resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
82                 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
83
84                 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
85                 // Get the Rest Client Service
86                 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
87
88                 val response = restClientService.exchangeResource(verb, urlPath, payload)
89                 val responseStatusCode = response.status
90                 val responseBody = response.body
91                 val outputKeyMapping = sourceProperties.outputKeyMapping
92                 if (responseStatusCode in 200..299 && outputKeyMapping.isNullOrEmpty()) {
93                     logger.info("AS>> outputKeyMapping==null, will not populateResource")
94                 }
95                 else if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
96                     populateResource(resourceAssignment, sourceProperties, responseBody, path)
97                 } else {
98                     val errMsg =
99                         "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)"
100                     logger.warn(errMsg)
101                     throw BluePrintProcessorException(errMsg)
102                 }
103             }
104             // Check the value has populated for mandatory case
105             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
106         } catch (e: Exception) {
107             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
108             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}",
109                 e)
110         }
111     }
112
113     fun blueprintWebClientService(resourceAssignment: ResourceAssignment,
114                                   restResourceSource: RestResourceSource): BlueprintWebClientService {
115         return if (isNotEmpty(restResourceSource.endpointSelector)) {
116             val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
117             blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
118         } else {
119             blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
120         }
121     }
122
123     @Throws(BluePrintProcessorException::class)
124     private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
125                                  restResponse: String, path: String) {
126         val dName = resourceAssignment.dictionaryName
127         val dSource = resourceAssignment.dictionarySource
128         val type = nullToEmpty(resourceAssignment.property?.type)
129         lateinit var entrySchemaType: String
130
131         val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
132             "failed to get output-key-mappings for $dName under $dSource properties"
133         }
134         logger.info("Response processing type($type)")
135
136         val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) {
137             "Failed to find path ($path) in response ($restResponse)"
138         }
139         logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
140
141
142         when (type) {
143             in BluePrintTypes.validPrimitiveTypes() -> {
144                 logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
145                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
146             }
147             in BluePrintTypes.validCollectionTypes() -> {
148                 // Array Types
149                 entrySchemaType = checkNotEmpty(resourceAssignment.property?.entrySchema?.type) {
150                     "Entry schema is not defined for dictionary ($dName) info"
151                 }
152                 val arrayNode = JacksonUtils.objectMapper.createArrayNode()
153
154                 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
155
156                     val responseArrayNode = responseNode.toList()
157                     for (responseSingleJsonNode in responseArrayNode) {
158
159                         val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
160
161                         outputKeyMapping.map {
162                             val responseKeyValue = responseSingleJsonNode.get(it.key)
163                             val propertyTypeForDataType = ResourceAssignmentUtils
164                                 .getPropertyType(raRuntimeService, entrySchemaType, it.key)
165
166                             logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), " +
167                                     "type  ({$propertyTypeForDataType})")
168
169                             JacksonUtils.populateJsonNodeValues(it.value,
170                                 responseKeyValue, propertyTypeForDataType, arrayChildNode)
171                         }
172                         arrayNode.add(arrayChildNode)
173                     }
174                 }
175                 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
176                 // Set the List of Complex Values
177                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
178             }
179             else -> {
180                 // Complex Types
181                 entrySchemaType = checkNotEmpty(resourceAssignment.property?.type) {
182                     "Entry schema is not defined for dictionary ($dName) info"
183                 }
184                 val objectNode = JacksonUtils.objectMapper.createObjectNode()
185                 outputKeyMapping.map {
186                     val responseKeyValue = responseNode.get(it.key)
187                     val propertyTypeForDataType = ResourceAssignmentUtils
188                         .getPropertyType(raRuntimeService, entrySchemaType, it.key)
189
190                     logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type  ({$propertyTypeForDataType})")
191                     JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
192                 }
193
194                 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
195                 // Set the List of Complex Values
196                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
197             }
198         }
199     }
200
201     @Throws(BluePrintProcessorException::class)
202     private fun validate(resourceAssignment: ResourceAssignment) {
203         checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
204         checkNotEmpty(resourceAssignment.dictionaryName) {
205             "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
206         }
207         checkNotEmpty(resourceAssignment.dictionarySource) {
208             "resource assignment dictionary source is not defined for template key (${resourceAssignment.name})"
209         }
210     }
211
212     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
213         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
214     }
215
216 }