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