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 com.fasterxml.jackson.databind.node.MissingNode
21 import org.apache.commons.collections.MapUtils
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.RestResourceSource
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
29 import org.onap.ccsdk.cds.controllerblueprints.core.nullToEmpty
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
32 import org.slf4j.LoggerFactory
35 class MockRestResourceResolutionProcessor(private val blueprintRestLibPropertyService:
36 MockBluePrintRestLibPropertyService) : ResourceAssignmentProcessor() {
38 private val logger = LoggerFactory.getLogger(MockRestResourceResolutionProcessor::class.java)
40 override fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, JsonNode> {
41 val resolvedInputKeyMapping = HashMap<String, JsonNode>()
42 if (MapUtils.isNotEmpty(inputKeyMapping)) {
44 resolvedInputKeyMapping["service-instance-id"] = "10".asJsonPrimitive()
45 resolvedInputKeyMapping["vnf_name"] = "vnf1".asJsonPrimitive()
46 resolvedInputKeyMapping["vnf-id"] = "123456".asJsonPrimitive()
48 return resolvedInputKeyMapping
51 override fun getName(): String {
52 return "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-rest"
55 override suspend fun processNB(executionRequest: ResourceAssignment) {
57 // Check if It has Input
58 val value = getFromInput(executionRequest)
59 if (value == null || value is MissingNode) {
60 val dName = executionRequest.dictionaryName
61 val dSource = executionRequest.dictionarySource
62 val resourceDefinition = resourceDictionaries[dName]
64 val resourceSource = resourceDefinition!!.sources[dSource]
66 val resourceSourceProperties = resourceSource!!.properties
68 val sourceProperties =
69 JacksonUtils.getInstanceFromMap(resourceSourceProperties!!, RestResourceSource::class.java)
71 val path = nullToEmpty(sourceProperties.path)
72 val inputKeyMapping = sourceProperties.inputKeyMapping
74 val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping!!).toMutableMap()
76 // Resolving content Variables
77 val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping)
79 resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping)
80 val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping)
82 logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
84 // Get the Rest Client Service
85 val restClientService = blueprintWebClientService(executionRequest)
87 val response = restClientService.exchangeResource(verb, urlPath, payload)
88 val responseStatusCode = response.status
89 val responseBody = response.body
90 if (responseStatusCode in 200..299 && !responseBody.isBlank()) {
91 populateResource(executionRequest, sourceProperties, responseBody, path)
92 restClientService.tearDown()
94 val errMsg = "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)"
96 throw BluePrintProcessorException(errMsg)
99 } catch (e: Exception) {
100 ResourceAssignmentUtils.setFailedResourceDataValue(executionRequest, e.message)
101 throw BluePrintProcessorException("Failed in template resolutionKey ($executionRequest) assignments with: ${e.message}",
106 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
107 raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
110 private fun blueprintWebClientService(resourceAssignment: ResourceAssignment): MockBlueprintWebClientService {
111 return blueprintRestLibPropertyService.mockBlueprintWebClientService(resourceAssignment.dictionarySource!!)
114 @Throws(BluePrintProcessorException::class)
115 private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource,
116 restResponse: String, path: String) {
117 val type = nullToEmpty(resourceAssignment.property?.type)
118 lateinit var entrySchemaType: String
120 val outputKeyMapping = sourceProperties.outputKeyMapping
122 val responseNode = JacksonUtils.jsonNode(restResponse).at(path)
125 in BluePrintTypes.validPrimitiveTypes() -> {
126 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
128 in BluePrintTypes.validCollectionTypes() -> {
130 entrySchemaType = resourceAssignment.property!!.entrySchema!!.type
131 val arrayNode = responseNode as ArrayNode
133 if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
134 val responseArrayNode = responseNode.toList()
135 for (responseSingleJsonNode in responseArrayNode) {
137 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
139 outputKeyMapping!!.map {
140 val responseKeyValue = responseSingleJsonNode.get(it.key)
141 val propertyTypeForDataType = ResourceAssignmentUtils
142 .getPropertyType(raRuntimeService, entrySchemaType, it.key)
144 JacksonUtils.populateJsonNodeValues(it.value,
145 responseKeyValue, propertyTypeForDataType, arrayChildNode)
147 arrayNode.add(arrayChildNode)
150 // Set the List of Complex Values
151 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
155 entrySchemaType = resourceAssignment.property!!.type
156 val objectNode = JacksonUtils.objectMapper.createObjectNode()
157 outputKeyMapping!!.map {
158 val responseKeyValue = responseNode.get(it.key)
159 val propertyTypeForDataType = ResourceAssignmentUtils
160 .getPropertyType(raRuntimeService, entrySchemaType, it.key)
161 JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
163 // Set the List of Complex Values
164 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)