df7e0482cdb7d52c40848423da5a17d59b66a76f
[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.services.execution.AbstractComponentFunction
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
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.returnNullIfMissing
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory
29 import org.springframework.context.annotation.Scope
30 import org.springframework.stereotype.Component
31
32 @Component("component-resource-resolution")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class ResourceResolutionComponent(private val resourceResolutionService: ResourceResolutionService) :
35         AbstractComponentFunction() {
36
37     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
38
39         val occurrence = getOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE).asInt()
40         val resolutionKey = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY)?.returnNullIfMissing()?.textValue() ?: ""
41         val storeResult = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)?.asBoolean() ?: false
42         val resourceId = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID)?.returnNullIfMissing()?.textValue() ?: ""
43
44         val resourceType = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE)?.returnNullIfMissing()?.textValue() ?: ""
45
46         val properties: MutableMap<String, Any> = mutableMapOf()
47         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = storeResult
48         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
49         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
50         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
51         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
52
53         val jsonResponse = JsonNodeFactory.instance.objectNode()
54         // Initialize Output Attribute to empty JSON
55         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName,
56             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse)
57
58         // validate inputs if we need to store the resource and template resolution.
59         if (storeResult) {
60             if (resolutionKey.isNotEmpty() && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
61                 throw BluePrintProcessorException("Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.")
62             } else if ((resourceType.isNotEmpty() && resourceId.isEmpty()) || (resourceType.isEmpty() && resourceId.isNotEmpty())) {
63                 throw BluePrintProcessorException("Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.")
64             } else if (resourceType.isEmpty() && resourceId.isEmpty() && resolutionKey.isEmpty()) {
65                 throw BluePrintProcessorException("Can't proceed with the resolution: can't persist resolution without a correlation key. " +
66                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false.")
67             }
68         }
69
70         val artifactPrefixNamesNode = getOperationInput(ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES)
71         val artifactPrefixNames = JacksonUtils.getListFromJsonNode(artifactPrefixNamesNode, String::class.java)
72
73         for (j in 1..occurrence) {
74             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = j
75
76             val response = resourceResolutionService.resolveResources(bluePrintRuntimeService,
77                     nodeTemplateName,
78                     artifactPrefixNames,
79                     properties)
80
81             // provide indexed result in output if we have multiple resolution
82             if (occurrence != 1) {
83                 jsonResponse.set(Integer.toString(j), response.asJsonNode())
84             } else {
85                 jsonResponse.setAll(response.asObjectNode())
86             }
87
88         }
89
90         // Set Output Attributes with resolved value
91         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName,
92                 ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse)
93     }
94
95     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
96         bluePrintRuntimeService.getBluePrintError().addError(runtimeException.message!!)
97     }
98 }