2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
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
33 @Component("component-resource-resolution")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class ResourceResolutionComponent(private val resourceResolutionService: ResourceResolutionService) :
36 AbstractComponentFunction() {
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"
49 const val ATTRIBUTE_ASSIGNMENT_PARAM = "assignment-params"
50 const val ATTRIBUTE_STATUS = "status"
52 const val OUTPUT_RESOURCE_ASSIGNMENT_PARAMS = "resource-assignment-params"
53 const val OUTPUT_STATUS = "status"
56 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
58 val occurrence = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE)?.asInt() ?: 1
60 getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY)?.returnNullIfMissing()?.textValue() ?: ""
61 val storeResult = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)?.asBoolean() ?: false
63 getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID)?.returnNullIfMissing()?.textValue() ?: ""
66 getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE)?.returnNullIfMissing()?.textValue() ?: ""
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
75 val jsonResponse = JsonNodeFactory.instance.objectNode()
76 // Initialize Output Attribute to empty JSON
77 bluePrintRuntimeService.setNodeTemplateAttributeValue(
79 ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse
82 // validate inputs if we need to store the resource and template resolution.
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."
96 val artifactPrefixNamesNode = getOperationInput(ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES)
97 val artifactPrefixNames = JacksonUtils.getListFromJsonNode(artifactPrefixNamesNode, String::class.java)
99 for (j in 1..occurrence) {
100 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = j
102 val response = resourceResolutionService.resolveResources(
103 bluePrintRuntimeService,
109 // provide indexed result in output if we have multiple resolution
110 if (occurrence != 1) {
111 jsonResponse.set<JsonNode>(Integer.toString(j), response.asJsonNode())
113 jsonResponse.setAll(response.asObjectNode())
117 // Set Output Attributes with resolved value
118 bluePrintRuntimeService.setNodeTemplateAttributeValue(
120 ResourceResolutionConstants.OUTPUT_ASSIGNMENT_PARAMS, jsonResponse
124 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
125 bluePrintRuntimeService.getBluePrintError().addError(runtimeException.message!!)