2039d2e5ab3c6bcb78e5b6bdfef9e3c7d4077592
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
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
19
20 import com.fasterxml.jackson.databind.node.JsonNodeFactory
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
25 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.springframework.beans.factory.config.ConfigurableBeanFactory
28 import org.springframework.context.annotation.Scope
29 import org.springframework.stereotype.Component
30
31 @Component("component-resource-resolution")
32 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
33 open class ResourceResolutionComponent(private val resourceResolutionService: ResourceResolutionService) :
34     AbstractComponentFunction() {
35
36     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
37
38         val occurrence = getOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE)
39         val resolutionKey = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY)
40         val storeResult = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
41         val resourceId = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID)
42         val resourceType = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE)
43
44
45         val properties: MutableMap<String, Any> = mutableMapOf()
46         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = storeResult?.asBoolean() ?: false
47         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = resolutionKey?.asText() ?: ""
48         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId?.asText() ?: ""
49         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType?.asText() ?: ""
50
51         val artifactPrefixNamesNode = getOperationInput(ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES)
52         val artifactPrefixNames = JacksonUtils.getListFromJsonNode(artifactPrefixNamesNode, String::class.java)
53
54         val resourceAssignmentRuntimeService =
55             ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactPrefixNames.toString())
56
57         val jsonResponse = JsonNodeFactory.instance.objectNode()
58         for (j in 1..occurrence.asInt()) {
59             val key = resolutionKey?.asText() + "-" + j
60             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = key
61
62             val response = resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
63                 nodeTemplateName,
64                 artifactPrefixNames,
65                 properties)
66
67             // provide indexed result in output if we have multiple resolution
68             if (occurrence.asInt() != 1) {
69                 jsonResponse.set(key, response.asJsonNode())
70             } else {
71                 jsonResponse.setAll(response.asObjectNode())
72             }
73         }
74
75         // Set Output Attributes
76         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName,
77             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse)
78     }
79
80     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
81         bluePrintRuntimeService.getBluePrintError().addError(runtimeException.message!!)
82     }
83 }