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.ArrayNode
21 import com.fasterxml.jackson.databind.node.JsonNodeFactory
22 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
23 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.RestResourceSource
24 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
26 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
29 import org.onap.ccsdk.apps.controllerblueprints.core.checkEqualsOrThrow
30 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty
31 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
32 import org.onap.ccsdk.apps.controllerblueprints.core.nullToEmpty
33 import org.onap.ccsdk.apps.controllerblueprints.core.returnNotEmptyOrThrow
34 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
35 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
36 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
37 import org.slf4j.LoggerFactory
38 import org.springframework.beans.factory.config.ConfigurableBeanFactory
39 import org.springframework.context.annotation.Scope
40 import org.springframework.stereotype.Service
43 * RestResourceResolutionProcessor
45 * @author Kapil Singal
47 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest")
48 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
49 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
50 : ResourceAssignmentProcessor() {
52 private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
54 override fun getName(): String {
55 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
58 override fun process(resourceAssignment: ResourceAssignment) {
60 validate(resourceAssignment)
62 // Check if It has Input
64 val value = raRuntimeService.getInputValue(resourceAssignment.name)
65 logger.info("rest source template key (${resourceAssignment.name}) found from input and value is ($value)")
66 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
67 } catch (e: BluePrintProcessorException) {
68 val dName = resourceAssignment.dictionaryName
69 val dSource = resourceAssignment.dictionarySource
70 val resourceDefinition = resourceDictionaries[dName]
71 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
72 val resourceSource = resourceDefinition.sources[dSource]
73 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
74 val resourceSourceProperties =
75 checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
76 val sourceProperties =
77 JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
78 val path = nullToEmpty(sourceProperties.path)
80 checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
81 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping)
83 // Resolving content Variables
84 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
86 resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
87 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
89 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
90 // Get the Rest Client Service
91 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
93 val response = restClientService.exchangeResource(verb, urlPath, payload)
94 if (response.isBlank()) {
95 logger.warn("Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath)")
97 populateResource(resourceAssignment, sourceProperties, response, path)
100 // Check the value has populated for mandatory case
101 ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
102 } catch (e: Exception) {
103 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
104 throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}",
109 private fun blueprintWebClientService(resourceAssignment: ResourceAssignment,
110 restResourceSource: RestResourceSource): BlueprintWebClientService {
111 return if (checkNotEmpty(restResourceSource.endpointSelector)) {
112 val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
113 blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
115 blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
119 @Throws(BluePrintProcessorException::class)
120 private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
121 restResponse: String, path: String) {
122 val dName = resourceAssignment.dictionaryName
123 val dSource = resourceAssignment.dictionarySource
124 val type = nullToEmpty(resourceAssignment.property?.type)
125 lateinit var entrySchemaType: String
127 val outputKeyMapping =
128 checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
129 logger.info("Response processing type($type)")
132 checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) { "Failed to find path ($path) in response ($restResponse)" }
133 logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
137 in BluePrintTypes.validPrimitiveTypes() -> {
138 logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
139 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
141 in BluePrintTypes.validCollectionTypes() -> {
144 returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
145 val arrayNode = responseNode as ArrayNode
147 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
148 val responseArrayNode = responseNode.toList()
149 for (responseSingleJsonNode in responseArrayNode) {
150 val arrayChildNode = JsonNodeFactory.instance.objectNode()
151 outputKeyMapping.map {
152 val responseKeyValue = responseSingleJsonNode.get(it.key)
153 val propertyTypeForDataType =
154 ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
155 logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})")
156 JacksonUtils.populateJsonNodeValues(it.value,
158 propertyTypeForDataType,
161 arrayNode.add(arrayChildNode)
164 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
165 // Set the List of Complex Values
166 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
171 returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
172 val objectNode = JsonNodeFactory.instance.objectNode()
173 outputKeyMapping.map {
174 val responseKeyValue = responseNode.get(it.key)
175 val propertyTypeForDataType =
176 ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
177 logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})")
178 JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
181 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
182 // Set the List of Complex Values
183 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
188 @Throws(BluePrintProcessorException::class)
189 private fun validate(resourceAssignment: ResourceAssignment) {
190 checkNotEmptyOrThrow(resourceAssignment.name, "resource assignment template key is not defined")
191 checkNotEmptyOrThrow(resourceAssignment.dictionaryName,
192 "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
193 checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA,
194 resourceAssignment.dictionarySource) {
195 "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA} but it is ${resourceAssignment.dictionarySource}"
197 checkNotEmptyOrThrow(resourceAssignment.dictionaryName,
198 "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
201 override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {