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.*
21 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.RestResourceSource
22 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
23 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
24 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BlueprintWebClientService
25 import org.onap.ccsdk.apps.controllerblueprints.core.*
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
29 import org.slf4j.LoggerFactory
30 import org.springframework.beans.factory.config.ConfigurableBeanFactory
31 import org.springframework.context.annotation.Scope
32 import org.springframework.stereotype.Service
35 * RestResourceResolutionProcessor
37 * @author Kapil Singal
39 @Service("rr-processor-source-rest")
40 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
41 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
42 : ResourceAssignmentProcessor() {
44 private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
46 override fun getName(): String {
47 return "rr-processor-source-rest"
50 override fun process(resourceAssignment: ResourceAssignment) {
52 validate(resourceAssignment)
54 // Check if It has Input
55 val value = raRuntimeService.getInputValue(resourceAssignment.name)
56 if (value !is MissingNode && value !is NullNode) {
57 logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)")
58 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
60 val dName = resourceAssignment.dictionaryName
61 val dSource = resourceAssignment.dictionarySource
62 val resourceDefinition = resourceDictionaries[dName]
63 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
64 val resourceSource = resourceDefinition.sources[dSource]
65 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
66 val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
67 val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
69 val urlPath = checkNotNull(sourceProperties.urlPath) { "failed to get request urlPath for $dName under $dSource properties" }
70 val path = nullToEmpty(sourceProperties.path)
71 val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
73 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
74 // Get the Rest Client Service
75 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
76 val response = restClientService.getResource(urlPath, String::class.java)
77 if (response.isBlank()) {
78 logger.warn("Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath)")
80 populateResource(resourceAssignment, sourceProperties, response, path)
83 // Check the value has populated for mandatory case
84 ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
85 } catch (e: Exception) {
86 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
87 throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
91 open fun blueprintWebClientService(resourceAssignment: ResourceAssignment,
92 restResourceSource: RestResourceSource): BlueprintWebClientService {
93 return if (checkNotEmpty(restResourceSource.endpointSelector)) {
94 val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
95 blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
97 blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
101 @Throws(BluePrintProcessorException::class)
102 private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource, restResponse: String, path: String) {
103 val dName = resourceAssignment.dictionaryName
104 val dSource = resourceAssignment.dictionarySource
105 val type = nullToEmpty(resourceAssignment.property?.type)
106 lateinit var entrySchemaType: String
108 val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
109 logger.info("Response processing type($type)")
111 val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) { "Failed to find path ($path) in response ($restResponse)" }
112 logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
116 in BluePrintTypes.validPrimitiveTypes() -> {
117 logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
118 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
120 in BluePrintTypes.validCollectionTypes() -> {
122 entrySchemaType = returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
123 val arrayNode = responseNode as ArrayNode
125 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
126 val responseArrayNode = responseNode.toList()
127 for (responseSingleJsonNode in responseArrayNode) {
128 val arrayChildNode = JsonNodeFactory.instance.objectNode()
129 outputKeyMapping.map {
130 val responseKeyValue = responseSingleJsonNode.get(it.key)
131 val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
132 logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})")
133 JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, arrayChildNode)
135 arrayNode.add(arrayChildNode)
138 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
139 // Set the List of Complex Values
140 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
144 val objectNode = responseNode as ObjectNode
145 outputKeyMapping.map {
146 val responseKeyValue = responseNode.get(it.key)
147 val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
148 logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})")
149 JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
152 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
153 // Set the List of Complex Values
154 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
159 @Throws(BluePrintProcessorException::class)
160 private fun validate(resourceAssignment: ResourceAssignment) {
161 checkNotEmptyOrThrow(resourceAssignment.name, "resource assignment template key is not defined")
162 checkNotEmptyOrThrow(resourceAssignment.dictionaryName, "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
163 checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA, resourceAssignment.dictionarySource) {
164 "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA} but it is ${resourceAssignment.dictionarySource}"
166 checkNotEmptyOrThrow(resourceAssignment.dictionaryName, "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
169 override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {