Aligned attributes of CDS components
[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.JsonNode
21 import com.fasterxml.jackson.databind.node.JsonNodeFactory
22 import com.fasterxml.jackson.databind.node.ObjectNode
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
27 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
28 import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.springframework.beans.factory.config.ConfigurableBeanFactory
31 import org.springframework.context.annotation.Scope
32 import org.springframework.stereotype.Component
33
34 @Component("component-resource-resolution")
35 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
36 open class ResourceResolutionComponent(private val resourceResolutionService: ResourceResolutionService) :
37     AbstractComponentFunction() {
38
39     companion object {
40
41         const val INPUT_REQUEST_ID = "request-id"
42         const val INPUT_RESOURCE_ID = "resource-id"
43         const val INPUT_ACTION_NAME = "action-name"
44         const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
45         const val INPUT_RESOURCE_TYPE = "resource-type"
46         const val INPUT_ARTIFACT_PREFIX_NAMES = "artifact-prefix-names"
47         const val INPUT_RESOLUTION_KEY = "resolution-key"
48         const val INPUT_RESOLUTION_SUMMARY = "resolution-summary"
49         const val INPUT_STORE_RESULT = "store-result"
50         const val INPUT_OCCURRENCE = "occurrence"
51
52         const val ATTRIBUTE_ASSIGNMENT_PARAM = "assignment-params"
53         const val ATTRIBUTE_ASSIGNMENT_MAP = "assignment-map"
54         const val ATTRIBUTE_STATUS = "status"
55
56         const val OUTPUT_RESOURCE_ASSIGNMENT_PARAMS = "resource-assignment-params"
57         const val OUTPUT_RESOURCE_ASSIGNMENT_MAP = "resource-assignment-map"
58         const val OUTPUT_STATUS = "status"
59     }
60
61     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
62
63         val occurrence = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE)?.asInt() ?: 1
64         val resolutionKey =
65             getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY)?.returnNullIfMissing()?.textValue() ?: ""
66         val storeResult = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)?.asBoolean() ?: false
67         val forceResolution = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_FORCE_RESOLUTION)?.asBoolean() ?: false
68         val resourceId =
69             getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID)?.returnNullIfMissing()?.textValue() ?: ""
70
71         val resourceType =
72             getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE)?.returnNullIfMissing()?.textValue() ?: ""
73         val resolutionSummary =
74             getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY)?.asBoolean() ?: false
75
76         val properties: MutableMap<String, Any> = mutableMapOf()
77         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = storeResult
78         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_FORCE_RESOLUTION] = forceResolution
79         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
80         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
81         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
82         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
83         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = resolutionSummary
84
85         val jsonResponse = JsonNodeFactory.instance.objectNode()
86         val assignmentMap = JsonNodeFactory.instance.objectNode()
87         // Initialize Output Attributes to empty JSON
88         bluePrintRuntimeService.setNodeTemplateAttributeValue(
89             nodeTemplateName,
90             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse
91         )
92         bluePrintRuntimeService.setNodeTemplateAttributeValue(
93             nodeTemplateName,
94             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_MAP, assignmentMap
95         )
96
97         // validate inputs if we need to store the resource and template resolution.
98         if (storeResult) {
99             if (resolutionKey.isNotEmpty() && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) {
100                 throw BluePrintProcessorException("Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.")
101             } else if ((resourceType.isNotEmpty() && resourceId.isEmpty()) || (resourceType.isEmpty() && resourceId.isNotEmpty())) {
102                 throw BluePrintProcessorException("Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.")
103             } else if (resourceType.isEmpty() && resourceId.isEmpty() && resolutionKey.isEmpty()) {
104                 throw BluePrintProcessorException(
105                     "Can't proceed with the resolution: can't persist resolution without a correlation key. " +
106                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false."
107                 )
108             }
109         }
110
111         val artifactPrefixNamesNode = getOperationInput(ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES)
112         val artifactPrefixNames = JacksonUtils.getListFromJsonNode(artifactPrefixNamesNode, String::class.java)
113         val alwaysPerformNewResolution = occurrence <= 0
114         val resolutionsToPerform: Int = if (alwaysPerformNewResolution) 1 else occurrence
115         for (j in 1..resolutionsToPerform) {
116
117             if (!alwaysPerformNewResolution) {
118                 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = j
119             }
120
121             val result = resourceResolutionService.resolveResources(
122                 bluePrintRuntimeService,
123                 nodeTemplateName,
124                 artifactPrefixNames,
125                 properties,
126                 stepName
127             )
128
129             // provide indexed result in output if we have multiple resolution.
130             if (resolutionsToPerform != 1) {
131                 jsonResponse.set<JsonNode>(j.toString(), result.templateMap.asJsonNode())
132                 assignmentMap.set<JsonNode>(j.toString(), result.assignmentMap.asJsonNode())
133             } else {
134                 jsonResponse.setAll<ObjectNode>(result.templateMap.asObjectNode())
135                 assignmentMap.setAll<ObjectNode>(result.assignmentMap.asObjectNode())
136             }
137         }
138
139         // Set Output Attributes with resolved value
140         bluePrintRuntimeService.setNodeTemplateAttributeValue(
141             nodeTemplateName,
142             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse
143         )
144         bluePrintRuntimeService.setNodeTemplateAttributeValue(
145             nodeTemplateName,
146             ResourceResolutionConstants.OUTPUT_ASSIGNMENT_MAP, assignmentMap
147         )
148     }
149
150     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
151         addError(runtimeException.message!!)
152     }
153 }