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