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