2 * Copyright © 2019 IBM, Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.mock
18 import com.fasterxml.jackson.databind.JsonNode
19 import com.fasterxml.jackson.databind.node.ArrayNode
20 import org.apache.commons.collections.MapUtils
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.RestResourceSource
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
27 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
28 import org.onap.ccsdk.cds.controllerblueprints.core.nullToEmpty
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
31 import org.slf4j.LoggerFactory
34 class MockRestResourceResolutionProcessor(private val blueprintRestLibPropertyService:
35 MockBluePrintRestLibPropertyService) : ResourceAssignmentProcessor() {
37 private val logger = LoggerFactory.getLogger(MockRestResourceResolutionProcessor::class.java)
39 override fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, JsonNode> {
40 val resolvedInputKeyMapping = HashMap<String, JsonNode>()
41 if (MapUtils.isNotEmpty(inputKeyMapping)) {
43 resolvedInputKeyMapping["service-instance-id"] = "10".asJsonPrimitive()
44 resolvedInputKeyMapping["vnf_name"] = "vnf1".asJsonPrimitive()
45 resolvedInputKeyMapping["vnf-id"] = "123456".asJsonPrimitive()
47 return resolvedInputKeyMapping
50 override fun getName(): String {
51 return "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
54 override suspend fun processNB(executionRequest: ResourceAssignment) {
56 // Check if It has Input
57 if (!setFromInput(executionRequest)) {
58 val dName = executionRequest.dictionaryName
59 val dSource = executionRequest.dictionarySource
60 val resourceDefinition = resourceDictionaries[dName]
62 val resourceSource = resourceDefinition!!.sources[dSource]
64 val resourceSourceProperties = resourceSource!!.properties
66 val sourceProperties =
67 JacksonUtils.getInstanceFromMap(resourceSourceProperties!!, RestResourceSource::class.java)
69 val path = nullToEmpty(sourceProperties.path)
70 val inputKeyMapping = sourceProperties.inputKeyMapping
72 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping!!).toMutableMap()
74 // Resolving content Variables
75 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
77 resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
78 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
80 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
82 // Get the Rest Client Service
83 val restClientService = blueprintWebClientService(executionRequest)
85 val response = restClientService.exchangeResource(verb, urlPath, payload)
86 val responseStatusCode = response.status
87 val responseBody = response.body
88 if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
89 populateResource(executionRequest, sourceProperties, responseBody, path)
90 restClientService.tearDown()
92 val errMsg = "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)"
94 throw BluePrintProcessorException(errMsg)
97 } catch (e: Exception) {
98 ResourceAssignmentUtils.setFailedResourceDataValue(executionRequest, e.message)
99 throw BluePrintProcessorException("Failed in template resolutionKey ($executionRequest) assignments with: ${e.message}", e)
103 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
104 raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
107 private fun blueprintWebClientService(resourceAssignment: ResourceAssignment): MockBlueprintWebClientService {
108 return blueprintRestLibPropertyService.mockBlueprintWebClientService(resourceAssignment.dictionarySource!!)
111 @Throws(BluePrintProcessorException::class)
112 private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
113 restResponse: String, path: String) {
114 val type = nullToEmpty(resourceAssignment.property?.type)
115 lateinit var entrySchemaType: String
117 val outputKeyMapping = sourceProperties.outputKeyMapping
119 val responseNode = JacksonUtils.jsonNode(restResponse).at(path)
122 in BluePrintTypes.validPrimitiveTypes() -> {
123 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
125 in BluePrintTypes.validCollectionTypes() -> {
127 entrySchemaType = resourceAssignment.property!!.entrySchema!!.type
128 val arrayNode = responseNode as ArrayNode
130 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
131 val responseArrayNode = responseNode.toList()
132 for (responseSingleJsonNode in responseArrayNode) {
134 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
136 outputKeyMapping!!.map {
137 val responseKeyValue = responseSingleJsonNode.get(it.key)
138 val propertyTypeForDataType = ResourceAssignmentUtils
139 .getPropertyType(raRuntimeService, entrySchemaType, it.key)
141 JacksonUtils.populateJsonNodeValues(
143 responseKeyValue, propertyTypeForDataType, arrayChildNode
146 arrayNode.add(arrayChildNode)
149 // Set the List of Complex Values
150 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
154 entrySchemaType = resourceAssignment.property!!.type
155 val objectNode = JacksonUtils.objectMapper.createObjectNode()
156 outputKeyMapping!!.map {
157 val responseKeyValue = responseNode.get(it.key)
158 val propertyTypeForDataType = ResourceAssignmentUtils
159 .getPropertyType(raRuntimeService, entrySchemaType, it.key)
160 JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
162 // Set the List of Complex Values
163 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)