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