d078a2d700aaa4f7da10a4f3ca81aba9a8a2b382
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / InputResourceResolutionProcessor.kt
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *  Modifications Copyright © 2018 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.processor
19
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.DatabaseResourceSource
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServiceDomains
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.isNotEmpty
26 import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.annotation.Scope
31 import org.springframework.stereotype.Service
32
33 /**
34  * InputResourceResolutionProcessor
35  *
36  * @author Kapil Singal
37  */
38 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input")
39 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
40 open class InputResourceResolutionProcessor : ResourceAssignmentProcessor() {
41
42     override fun getName(): String {
43         return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
44     }
45
46     override suspend fun processNB(resourceAssignment: ResourceAssignment) {
47         try {
48             if (isNotEmpty(resourceAssignment.name) && !setFromInput(resourceAssignment)) {
49                 setFromKeyDependencies(resourceAssignment)
50             }
51             // Check the value has populated for mandatory case
52             ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
53         } catch (e: BluePrintProcessorException) {
54             val errorMsg = "Failed to process input resource resolution in template key ($resourceAssignment) assignments."
55             throw e.updateErrorMessage(ExecutionServiceDomains.RESOURCE_RESOLUTION, errorMsg,
56                     "Wrong input value was set.")
57         } catch (e: Exception) {
58             ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
59             throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with : (${e.message})", e)
60         }
61     }
62
63     // usecase: where input data attribute doesn't match with resourceName, and needs an alternate mapping provided under key-dependencies.
64     private fun setFromKeyDependencies(resourceAssignment: ResourceAssignment) {
65         val dName = resourceAssignment.dictionaryName!!
66         val dSource = resourceAssignment.dictionarySource!!
67         val resourceDefinition = resourceDefinition(dName)
68
69         /** Check Resource Assignment has the source definitions, If not get from Resource Definition **/
70         val resourceSource = resourceAssignment.dictionarySourceDefinition
71             ?: resourceDefinition?.sources?.get(dSource)
72             ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
73         val resourceSourceProperties = checkNotNull(resourceSource.properties) {
74             "failed to get source properties for $dName "
75         }
76         val sourceProperties =
77             JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
78
79         val keyDependency = checkNotNull(sourceProperties.keyDependencies) {
80             "failed to get input-key-mappings for $dName under $dSource properties"
81         }
82         // keyDependency = service-instance.service-instance-id
83         setFromInputKeyDependencies(keyDependency, resourceAssignment); // New API which picks arrtibute from Input
84     }
85
86     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
87         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
88     }
89 }