2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Modifications Copyright © 2019 IBM, Bell Canada.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.CapabilityResourceSource
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
24 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.annotation.Scope
31 import org.springframework.stereotype.Service
33 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-capability")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class CapabilityResourceResolutionProcessor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) :
36 ResourceAssignmentProcessor() {
38 private val log = LoggerFactory.getLogger(CapabilityResourceResolutionProcessor::class.java)
40 var componentResourceAssignmentProcessor: ResourceAssignmentProcessor? = null
42 override fun getName(): String {
43 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-capability"
46 override suspend fun processNB(resourceAssignment: ResourceAssignment) {
47 // Execute only if value is not in Input
48 if (!setFromInput(resourceAssignment)) {
49 val dName = resourceAssignment.dictionaryName!!
50 val dSource = resourceAssignment.dictionarySource!!
51 val resourceDefinition = resourceDefinition(resourceAssignment.dictionaryName!!)
53 /** Check Resource Assignment has the source definitions, If not get from Resource Definition **/
54 val resourceSource = resourceAssignment.dictionarySourceDefinition
55 ?: resourceDefinition?.sources?.get(dSource)
56 ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
58 val resourceSourceProps =
59 checkNotNull(resourceSource.properties) { "failed to get $resourceSource properties" }
61 * Get the Capability Resource Source Info from Property Definitions.
63 val capabilityResourceSourceProperty = JacksonUtils
64 .getInstanceFromMap(resourceSourceProps, CapabilityResourceSource::class.java)
66 val scriptType = capabilityResourceSourceProperty.scriptType
67 val scriptClassReference = capabilityResourceSourceProperty.scriptClassReference
68 val instanceDependencies = capabilityResourceSourceProperty.instanceDependencies ?: listOf()
70 componentResourceAssignmentProcessor =
71 scriptInstance(scriptType, scriptClassReference, instanceDependencies)
73 checkNotNull(componentResourceAssignmentProcessor) {
74 "failed to get capability resource assignment processor($scriptClassReference)"
77 // Assign Current Blueprint runtime and ResourceDictionaries
78 componentResourceAssignmentProcessor!!.scriptType = scriptType
79 componentResourceAssignmentProcessor!!.raRuntimeService = raRuntimeService
80 componentResourceAssignmentProcessor!!.resourceDictionaries = resourceDictionaries
82 // Invoke componentResourceAssignmentProcessor
83 componentResourceAssignmentProcessor!!.executeScript(resourceAssignment)
85 componentFunctionScriptingService.cleanupInstance(raRuntimeService.bluePrintContext(), scriptType)
89 override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
90 raRuntimeService.getBluePrintError()
91 .addError("Failed in CapabilityResourceResolutionProcessor : ${runtimeException.message}")
92 ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, runtimeException.message)
95 suspend fun scriptInstance(scriptType: String, scriptClassReference: String, instanceDependencies: List<String>):
96 ResourceAssignmentProcessor {
99 "creating resource resolution of script type($scriptType), reference name($scriptClassReference) and" +
100 "instanceDependencies($instanceDependencies)"
103 val scriptComponent = componentFunctionScriptingService
104 .scriptInstance<ResourceAssignmentProcessor>(
105 raRuntimeService.bluePrintContext(), scriptType,
109 return scriptComponent