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