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