Add resource definition resolution service.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / RestResourceResolutionProcessor.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *  Modifications Copyright © 2017-2019 AT&T, Bell Canada
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
19
20 import com.fasterxml.jackson.databind.node.MissingNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.RestResourceSource
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.*
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.config.ConfigurableBeanFactory
32 import org.springframework.context.annotation.Scope
33 import org.springframework.stereotype.Service
34
35 /**
36  * RestResourceResolutionProcessor
37  *
38  * @author Kapil Singal
39  */
40 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest")
41 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
42 open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
43     : ResourceAssignmentProcessor() {
44
45     private val logger = LoggerFactory.getLogger(RestResourceResolutionProcessor::class.java)
46
47     override fun getName(): String {
48         return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
49     }
50
51     override suspend fun processNB(resourceAssignment: ResourceAssignment) {
52         try {
53             validate(resourceAssignment)
54
55             // Check if It has Input
56             val value = getFromInput(resourceAssignment)
57             if (value == null || value is MissingNode || value is NullNode) {
58                 val dName = resourceAssignment.dictionaryName!!
59                 val dSource = resourceAssignment.dictionarySource!!
60                 val resourceDefinition = resourceDefinition(dName)
61
62                 /** Check Resource Assignment has the source definitions, If not get from Resource Definitions **/
63                 val resourceSource = resourceAssignment.dictionarySourceDefinition
64                         ?: resourceDefinition?.sources?.get(dSource)
65                         ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
66
67                 val resourceSourceProperties =
68                         checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
69
70                 val sourceProperties =
71                         JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
72
73                 val path = nullToEmpty(sourceProperties.path)
74                 val inputKeyMapping =
75                         checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
76                 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping).toMutableMap()
77
78                 // Resolving content Variables
79                 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
80                 val urlPath =
81                         resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
82                 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
83
84                 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
85                 // Get the Rest Client Service
86                 val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties)
87
88                 val response = restClientService.exchangeResource(verb, urlPath, payload)
89                 val responseStatusCode = response.status
90                 val responseBody = response.body
91                 val outputKeyMapping = sourceProperties.outputKeyMapping
92                 if (responseStatusCode in 200..299 && outputKeyMapping.isNullOrEmpty()) {
93                     logger.info("AS>> outputKeyMapping==null, will not populateResource")
94                 } else if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
95                     populateResource(resourceAssignment, sourceProperties, responseBody, path)
96                 } else {
97                     val errMsg =
98                             "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)"
99                     logger.warn(errMsg)
100                     throw BluePrintProcessorException(errMsg)
101                 }
102             }
103             // Check the value has populated for mandatory case
104             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
105         } catch (e: Exception) {
106             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
107             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}",
108                     e)
109         }
110     }
111
112     fun blueprintWebClientService(resourceAssignment: ResourceAssignment,
113                                   restResourceSource: RestResourceSource): BlueprintWebClientService {
114         return if (isNotEmpty(restResourceSource.endpointSelector)) {
115             val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!)
116             blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson)
117         } else {
118             blueprintRestLibPropertyService.blueprintWebClientService(resourceAssignment.dictionarySource!!)
119         }
120     }
121
122     @Throws(BluePrintProcessorException::class)
123     private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
124                                  restResponse: String, path: String) {
125         val dName = resourceAssignment.dictionaryName
126         val dSource = resourceAssignment.dictionarySource
127         val type = nullToEmpty(resourceAssignment.property?.type)
128         lateinit var entrySchemaType: String
129
130         val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) {
131             "failed to get output-key-mappings for $dName under $dSource properties"
132         }
133         logger.info("Response processing type($type)")
134
135         val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) {
136             "Failed to find path ($path) in response ($restResponse)"
137         }
138         logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
139
140
141         when (type) {
142             in BluePrintTypes.validPrimitiveTypes() -> {
143                 logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
144                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
145             }
146             in BluePrintTypes.validCollectionTypes() -> {
147                 // Array Types
148                 entrySchemaType = checkNotEmpty(resourceAssignment.property?.entrySchema?.type) {
149                     "Entry schema is not defined for dictionary ($dName) info"
150                 }
151                 val arrayNode = JacksonUtils.objectMapper.createArrayNode()
152
153                 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
154
155                     val responseArrayNode = responseNode.toList()
156                     for (responseSingleJsonNode in responseArrayNode) {
157
158                         val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
159
160                         outputKeyMapping.map {
161                             val responseKeyValue = responseSingleJsonNode.get(it.key)
162                             val propertyTypeForDataType = ResourceAssignmentUtils
163                                     .getPropertyType(raRuntimeService, entrySchemaType, it.key)
164
165                             logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), " +
166                                     "type  ({$propertyTypeForDataType})")
167
168                             JacksonUtils.populateJsonNodeValues(it.value,
169                                     responseKeyValue, propertyTypeForDataType, arrayChildNode)
170                         }
171                         arrayNode.add(arrayChildNode)
172                     }
173                 }
174                 logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
175                 // Set the List of Complex Values
176                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
177             }
178             else -> {
179                 // Complex Types
180                 entrySchemaType = checkNotEmpty(resourceAssignment.property?.type) {
181                     "Entry schema is not defined for dictionary ($dName) info"
182                 }
183                 val objectNode = JacksonUtils.objectMapper.createObjectNode()
184                 outputKeyMapping.map {
185                     val responseKeyValue = responseNode.get(it.key)
186                     val propertyTypeForDataType = ResourceAssignmentUtils
187                             .getPropertyType(raRuntimeService, entrySchemaType, it.key)
188
189                     logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type  ({$propertyTypeForDataType})")
190                     JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
191                 }
192
193                 logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
194                 // Set the List of Complex Values
195                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
196             }
197         }
198     }
199
200     @Throws(BluePrintProcessorException::class)
201     private fun validate(resourceAssignment: ResourceAssignment) {
202         checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" }
203         checkNotEmpty(resourceAssignment.dictionaryName) {
204             "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})"
205         }
206         checkNotEmpty(resourceAssignment.dictionarySource) {
207             "resource assignment dictionary source is not defined for template key (${resourceAssignment.name})"
208         }
209     }
210
211     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
212         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
213     }
214
215 }