131e2ae9aa8fd312d44edc79fd65b11a971746af
[ccsdk/cds.git] /
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(
56                 ExecutionServiceDomains.RESOURCE_RESOLUTION, errorMsg,
57                 "Wrong input value was set."
58             )
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     private 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         val resourceSourceProperties = checkNotNull(resourceSource.properties) {
76             "failed to get source properties for $dName "
77         }
78         val sourceProperties =
79             JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
80
81         val keyDependency = checkNotNull(sourceProperties.keyDependencies) {
82             "failed to get input-key-mappings for $dName under $dSource properties"
83         }
84         // keyDependency = service-instance.service-instance-id
85         setFromInputKeyDependencies(keyDependency, resourceAssignment); // New API which picks arrtibute from Input
86     }
87
88     override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
89         raRuntimeService.getBluePrintError().addError(runtimeException.message!!)
90     }
91 }