2 * Copyright © 2018 - 2020 IBM.
3 * Modifications Copyright © 2017-2020 AT&T, Bell Canada
4 * Modifications Copyright © 2022 Deutche Telekom AG
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
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.blueprintsprocessor.services.execution.ExecutionServiceDomains
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
32 import org.onap.ccsdk.cds.controllerblueprints.core.isNotEmpty
33 import org.onap.ccsdk.cds.controllerblueprints.core.nullToEmpty
34 import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
36 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.KeyIdentifier
37 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
38 import org.slf4j.LoggerFactory
39 import org.springframework.beans.factory.config.ConfigurableBeanFactory
40 import org.springframework.context.annotation.Scope
41 import org.springframework.stereotype.Service
42 import java.util.HashMap
45 * RestResourceResolutionProcessor
47 * @author Kapil Singal
49 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest")
50 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
51 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService) :
52 ResourceAssignmentProcessor() {
54 private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
56 override fun getName(): String {
57 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
60 override suspend fun processNB(resourceAssignment: ResourceAssignment) {
62 validate(resourceAssignment)
64 // Check if It has Input
65 if (!setFromInput(resourceAssignment)) {
66 val dName = resourceAssignment.dictionaryName!!
67 val dSource = resourceAssignment.dictionarySource!!
68 val resourceDefinition = resourceDefinition(dName)
70 /** Check Resource Assignment has the source definitions, If not get from Resource Definitions **/
71 val resourceSource = resourceAssignment.dictionarySourceDefinition
72 ?: resourceDefinition?.sources?.get(dSource)
73 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
74 if ((resourceAssignment.property?.type).isNullOrEmpty())
75 throw BluePrintProcessorException("Couldn't get data dictionary type for dictionary name (${resourceAssignment.name})")
76 val resourceSourceProperties =
77 checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
79 val sourceProperties =
80 JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
83 checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
84 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping).toMutableMap()
86 resolvedInputKeyMapping.map { KeyIdentifier(it.key, it.value) }.let {
87 resourceAssignment.keyIdentifiers.addAll(it)
90 // Resolving content Variables
91 val path = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.path), resolvedInputKeyMapping)
92 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
93 resourceSourceProperties["resolved-payload"] = JacksonUtils.jsonNode(payload)
95 resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
96 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
97 var outputKeyMapping = sourceProperties.outputKeyMapping
98 if (!outputKeyMapping.isNullOrEmpty()) {
99 outputKeyMapping = outputKeyMapping.mapValues { (_, value) ->
100 resolveFromInputKeyMapping(value, resolvedInputKeyMapping)
104 "RestResource ($dSource) dictionary information: " +
105 "URL:($urlPath), path:($path), input-key-mapping:($inputKeyMapping), output-key-mapping:($outputKeyMapping)"
107 val requestHeaders = sourceProperties.headers.mapValues { (_, value) ->
108 resolveFromInputKeyMapping(value, resolvedInputKeyMapping)
110 logger.info("$dSource dictionary information : ($urlPath), path:($path), ($inputKeyMapping), ($outputKeyMapping)")
111 // Get the Rest Client Service
112 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
114 val response = restClientService.exchangeResource(verb, urlPath, payload, requestHeaders.toMap())
115 val responseStatusCode = response.status
116 val responseBody = response.body
117 if (responseStatusCode in 200..299 && outputKeyMapping == null) {
118 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
119 logger.info("AS>> outputKeyMapping==null, will not populateResource")
120 } else if (responseStatusCode in 200..299) {
121 populateResource(resourceAssignment, sourceProperties, responseBody, path, outputKeyMapping)
124 "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)"
126 throw BluePrintProcessorException(errMsg)
129 // Check the value has populated for mandatory case
130 ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
131 } catch (e: BluePrintProcessorException) {
132 val errorMsg = "Failed to process REST resource resolution in template key ($resourceAssignment) assignments."
133 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, errorMsg)
134 throw e.updateErrorMessage(
135 ExecutionServiceDomains.RESOURCE_RESOLUTION, errorMsg,
136 "Wrong resource definition or resolution failed."
138 } catch (e: Exception) {
139 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
140 throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
144 fun blueprintWebClientService(
145 resourceAssignment: ResourceAssignment,
146 restResourceSource: RestResourceSource
147 ): BlueprintWebClientService {
148 return if (isNotEmpty(restResourceSource.endpointSelector)) {
149 val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
150 blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
152 blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
156 @Throws(BluePrintProcessorException::class)
157 private fun populateResource(
158 resourceAssignment: ResourceAssignment,
159 sourceProperties: RestResourceSource,
160 restResponse: String,
162 outputKeyMapping: MutableMap<String, String>?
164 val dName = resourceAssignment.dictionaryName
165 val dSource = resourceAssignment.dictionarySource
166 val type = nullToEmpty(resourceAssignment.property?.type)
167 val metadata = resourceAssignment.property!!.metadata
169 logger.info("Response processing type ($type)")
171 // we check null in processNB
172 var outputKeyMapping = sourceProperties.outputKeyMapping!!
174 var responseNode: JsonNode? = if (outputKeyMapping.isEmpty()) {
175 var tmpResponseNode = if (type == BluePrintConstants.DATA_TYPE_JSON || type == BluePrintConstants.DATA_TYPE_MAP)
176 JacksonUtils.jsonNode(restResponse).at(path)
178 JacksonUtils.convertPrimitiveResourceValue(type, restResponse).at(path)
179 if (type == BluePrintConstants.DATA_TYPE_JSON || type == BluePrintConstants.DATA_TYPE_MAP) {
180 if (tmpResponseNode.isObject) {
181 logger.info("Creating substitute outputKeyMapping for $type type")
182 outputKeyMapping = HashMap<String, String>()
183 tmpResponseNode.fieldNames().forEach {
184 outputKeyMapping[it] = it
189 "Failed to get $dSource result for dictionary name ($dName): response is not a JSON object"
191 throw BluePrintProcessorException(errMsg)
194 logger.info("Wrapping output for the dictionary name (${resourceAssignment.name})")
195 val newNode = jacksonObjectMapper().createObjectNode()
196 newNode.replace(dName, tmpResponseNode)
197 outputKeyMapping[dName!!] = dName
201 JacksonUtils.jsonNode(restResponse).at(path)
202 responseNode = checkNotNull(responseNode) {
203 "Failed to find path ($path) in response ($responseNode)"
206 val valueToPrint = ResourceAssignmentUtils.getValueToLog(metadata, responseNode)
207 logger.info("populating value for output mapping ($outputKeyMapping), from json ($valueToPrint)")
209 val parsedResponseNode = ResourceAssignmentUtils.parseResponseNode(
210 responseNode, resourceAssignment,
211 raRuntimeService, outputKeyMapping
214 // Set the List of Complex Values
215 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, parsedResponseNode)
218 @Throws(BluePrintProcessorException::class)
219 private fun validate(resourceAssignment: ResourceAssignment) {
220 checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
221 checkNotEmpty(resourceAssignment.dictionaryName) {
222 "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
224 checkNotEmpty(resourceAssignment.dictionarySource) {
225 "resource assignment dictionary source is not defined for template key (${resourceAssignment.name})"
229 override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
230 addError(runtimeException.message!!)