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 com.fasterxml.jackson.databind.node.MissingNode
23 import com.fasterxml.jackson.databind.node.NullNode
24 import com.fasterxml.jackson.databind.node.ObjectNode
25 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
26 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.RestResourceSource
27 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
28 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
29 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BlueprintWebClientService
30 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
32 import org.onap.ccsdk.apps.controllerblueprints.core.checkEqualsOrThrow
33 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty
34 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
35 import org.onap.ccsdk.apps.controllerblueprints.core.nullToEmpty
36 import org.onap.ccsdk.apps.controllerblueprints.core.returnNotEmptyOrThrow
37 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
38 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
39 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
40 import org.slf4j.LoggerFactory
41 import org.springframework.beans.factory.config.ConfigurableBeanFactory
42 import org.springframework.context.annotation.Scope
43 import org.springframework.stereotype.Service
46 * RestResourceResolutionProcessor
48 * @author Kapil Singal
50 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest")
51 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
52 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
53 : ResourceAssignmentProcessor() {
55 private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
57 override fun getName(): String {
58 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
61 override fun process(resourceAssignment: ResourceAssignment) {
63 validate(resourceAssignment)
65 // Check if It has Input
66 val value = raRuntimeService.getInputValue(resourceAssignment.name)
67 if (value !is MissingNode && value !is NullNode) {
68 logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)")
69 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
71 val dName = resourceAssignment.dictionaryName
72 val dSource = resourceAssignment.dictionarySource
73 val resourceDefinition = resourceDictionaries[dName]
74 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
75 val resourceSource = resourceDefinition.sources[dSource]
76 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
77 val resourceSourceProperties =
78 checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
79 val sourceProperties =
80 JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
83 checkNotNull(sourceProperties.urlPath) { "failed to get request urlPath for $dName under $dSource properties" }
84 val path = nullToEmpty(sourceProperties.path)
86 checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
88 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
89 // Get the Rest Client Service
90 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
91 val response = restClientService.getResource(urlPath, String::class.java)
92 if (response.isBlank()) {
93 logger.warn("Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath)")
95 populateResource(resourceAssignment, sourceProperties, response, path)
98 // Check the value has populated for mandatory case
99 ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
100 } catch (e: Exception) {
101 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
102 throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}",
107 open fun blueprintWebClientService(resourceAssignment: ResourceAssignment,
108 restResourceSource: RestResourceSource): BlueprintWebClientService {
109 return if (checkNotEmpty(restResourceSource.endpointSelector)) {
110 val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
111 blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
113 blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
117 @Throws(BluePrintProcessorException::class)
118 private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
119 restResponse: String, path: String) {
120 val dName = resourceAssignment.dictionaryName
121 val dSource = resourceAssignment.dictionarySource
122 val type = nullToEmpty(resourceAssignment.property?.type)
123 lateinit var entrySchemaType: String
125 val outputKeyMapping =
126 checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
127 logger.info("Response processing type($type)")
130 checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) { "Failed to find path ($path) in response ($restResponse)" }
131 logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
135 in BluePrintTypes.validPrimitiveTypes() -> {
136 logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
137 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
139 in BluePrintTypes.validCollectionTypes() -> {
142 returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
143 val arrayNode = responseNode as ArrayNode
145 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
146 val responseArrayNode = responseNode.toList()
147 for (responseSingleJsonNode in responseArrayNode) {
148 val arrayChildNode = JsonNodeFactory.instance.objectNode()
149 outputKeyMapping.map {
150 val responseKeyValue = responseSingleJsonNode.get(it.key)
151 val propertyTypeForDataType =
152 ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
153 logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})")
154 JacksonUtils.populateJsonNodeValues(it.value,
156 propertyTypeForDataType,
159 arrayNode.add(arrayChildNode)
162 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
163 // Set the List of Complex Values
164 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
168 val objectNode = responseNode as ObjectNode
169 outputKeyMapping.map {
170 val responseKeyValue = responseNode.get(it.key)
171 val propertyTypeForDataType =
172 ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
173 logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})")
174 JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
177 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
178 // Set the List of Complex Values
179 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
184 @Throws(BluePrintProcessorException::class)
185 private fun validate(resourceAssignment: ResourceAssignment) {
186 checkNotEmptyOrThrow(resourceAssignment.name, "resource assignment template key is not defined")
187 checkNotEmptyOrThrow(resourceAssignment.dictionaryName,
188 "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
189 checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA,
190 resourceAssignment.dictionarySource) {
191 "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA} but it is ${resourceAssignment.dictionarySource}"
193 checkNotEmptyOrThrow(resourceAssignment.dictionaryName,
194 "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
197 override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {