875b9ae321ac6a5ff73ff9cf6593df01bf7db1c9
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceResolutionComponent.kt
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     companion object{
37         const val INPUT_REQUEST_ID = "request-id"
38         const val INPUT_RESOURCE_ID = "resource-id"
39         const val INPUT_ACTION_NAME = "action-name"
40         const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
41         const val INPUT_RESOURCE_TYPE = "resource-type"
42         const val INPUT_ARTIFACT_PREFIX_NAMES = "artifact-prefix-names"
43         const val INPUT_RESOLUTION_KEY = "resolution-key"
44         const val INPUT_STORE_RESULT = "store-result"
45         const val INPUT_OCCURRENCE = "occurrence"
46
47         const val ATTRIBUTE_ASSIGNMENT_PARAM = "assignment-params"
48         const val ATTRIBUTE_STATUS = "status"
49
50         const val OUTPUT_RESOURCE_ASSIGNMENT_PARAMS = "resource-assignment-params"
51         const val OUTPUT_STATUS = "status"
52     }
53
54     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
55
56         val occurrence = getOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE).asInt()
57         val resolutionKey = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY)?.returnNullIfMissing()?.textValue() ?: ""
58         val storeResult = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)?.asBoolean() ?: false
59         val resourceId = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID)?.returnNullIfMissing()?.textValue() ?: ""
60
61         val resourceType = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE)?.returnNullIfMissing()?.textValue() ?: ""
62
63         val properties: MutableMap<String, Any> = mutableMapOf()
64         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = storeResult
65         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
66         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
67         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
68         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
69
70         val jsonResponse = JsonNodeFactory.instance.objectNode()
71         // Initialize Output Attribute to empty JSON
72         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName,
73             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse)
74
75         // validate inputs if we need to store the resource and template resolution.
76         if (storeResult) {
77             if (resolutionKey.isNotEmpty() && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
78                 throw BluePrintProcessorException("Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.")
79             } else if ((resourceType.isNotEmpty() && resourceId.isEmpty()) || (resourceType.isEmpty() && resourceId.isNotEmpty())) {
80                 throw BluePrintProcessorException("Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.")
81             } else if (resourceType.isEmpty() && resourceId.isEmpty() && resolutionKey.isEmpty()) {
82                 throw BluePrintProcessorException("Can't proceed with the resolution: can't persist resolution without a correlation key. " +
83                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false.")
84             }
85         }
86
87         val artifactPrefixNamesNode = getOperationInput(ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES)
88         val artifactPrefixNames = JacksonUtils.getListFromJsonNode(artifactPrefixNamesNode, String::class.java)
89
90         for (j in 1..occurrence) {
91             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = j
92
93             val response = resourceResolutionService.resolveResources(bluePrintRuntimeService,
94                     nodeTemplateName,
95                     artifactPrefixNames,
96                     properties)
97
98             // provide indexed result in output if we have multiple resolution
99             if (occurrence != 1) {
100                 jsonResponse.set(Integer.toString(j), response.asJsonNode())
101             } else {
102                 jsonResponse.setAll(response.asObjectNode())
103             }
104
105         }
106
107         // Set Output Attributes with resolved value
108         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName,
109                 ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse)
110     }
111
112     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
113         bluePrintRuntimeService.getBluePrintError().addError(runtimeException.message!!)
114     }
115 }