2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
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
32 * InputResourceResolutionProcessor
34 * @author Kapil Singal
36 @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input")
37 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
38 open class InputResourceResolutionProcessor : ResourceAssignmentProcessor() {
40 override fun getName(): String {
41 return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
44 override suspend fun processNB(resourceAssignment: ResourceAssignment) {
46 if (isNotEmpty(resourceAssignment.name) && !setFromInput(resourceAssignment)) {
47 setFromKeyDependencies(resourceAssignment)
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)
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)
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 "
70 val sourceProperties =
71 JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
73 val keyDependency = checkNotNull(sourceProperties.keyDependencies) {
74 "failed to get input-key-mappings for $dName under $dSource properties"
76 // keyDependency = service-instance.service-instance-id
77 setFromInputKeyDependencies(keyDependency, resourceAssignment); // New API which picks arrtibute from Input
80 override suspend fun recoverNB(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
81 raRuntimeService.getBluePrintError().addError(runtimeException.message!!)