2 * Copyright © 2018 IBM.
3 * Modifications Copyright © 2017-2019 AT&T, 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.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.RestResourceSource
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
25 import org.onap.ccsdk.cds.controllerblueprints.core.*
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.annotation.Scope
31 import org.springframework.stereotype.Service
34 * RestResourceResolutionProcessor
36 * @author Kapil Singal
38 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest")
39 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
40 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
41 : ResourceAssignmentProcessor() {
43 private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
45 override fun getName(): String {
46 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
49 override suspend fun processNB(resourceAssignment: ResourceAssignment) {
51 validate(resourceAssignment)
53 // Check if It has Input
54 val value = raRuntimeService.getInputValue(resourceAssignment.name)
55 if (ResourceAssignmentUtils.checkIfInputWasProvided(resourceAssignment, value)) {
56 ResourceAssignmentUtils.setInputValueIfProvided(resourceAssignment, raRuntimeService, value)
59 val dName = resourceAssignment.dictionaryName!!
60 val dSource = resourceAssignment.dictionarySource!!
61 val resourceDefinition = resourceDefinition(dName)
63 /** Check Resource Assignment has the source definitions, If not get from Resource Definitions **/
64 val resourceSource = resourceAssignment.dictionarySourceDefinition
65 ?: resourceDefinition?.sources?.get(dSource)
66 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
68 val resourceSourceProperties =
69 checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
71 val sourceProperties =
72 JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
74 val path = nullToEmpty(sourceProperties.path)
76 checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
77 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping).toMutableMap()
79 // Resolving content Variables
80 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
82 resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
83 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
85 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
86 // Get the Rest Client Service
87 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
89 val response = restClientService.exchangeResource(verb, urlPath, payload)
90 val responseStatusCode = response.status
91 val responseBody = response.body
92 val outputKeyMapping = sourceProperties.outputKeyMapping
93 if (responseStatusCode in 200..299 && outputKeyMapping.isNullOrEmpty()) {
94 logger.info("AS>> outputKeyMapping==null, will not populateResource")
95 } else if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
96 populateResource(resourceAssignment, sourceProperties, responseBody, path)
99 "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)"
101 throw BluePrintProcessorException(errMsg)
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}",
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)
119 blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
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 val metadata = resourceAssignment.property!!.metadata
131 val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
132 "failed to get output-key-mappings for $dName under $dSource properties"
134 logger.info("Response processing type($type)")
136 val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) {
137 "Failed to find path ($path) in response ($restResponse)"
140 val valueToPrint = ResourceAssignmentUtils.getValueToLog(metadata, responseNode)
141 logger.info("populating value for output mapping ($outputKeyMapping), from json ($valueToPrint)")
143 val parsedResponseNode = ResourceAssignmentUtils.parseResponseNode(responseNode, resourceAssignment,
144 raRuntimeService, outputKeyMapping)
146 // Set the List of Complex Values
147 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, parsedResponseNode)
150 @Throws(BluePrintProcessorException::class)
151 private fun validate(resourceAssignment: ResourceAssignment) {
152 checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
153 checkNotEmpty(resourceAssignment.dictionaryName) {
154 "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
156 checkNotEmpty(resourceAssignment.dictionarySource) {
157 "resource assignment dictionary source is not defined for template key (${resourceAssignment.name})"
161 override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
162 raRuntimeService.getBluePrintError().addError(runtimeException.message!!)