3022ca524a6edfc7435084b2421ec2d0e11e3b61
[ccsdk/apps.git] /
1 /*
2  *  Copyright © 2018 IBM.
3  *  Modifications Copyright © 2017-2018 AT&T Intellectual Property.
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.apps.blueprintsprocessor.functions.resource.resolution.processor
19
20 import org.apache.commons.collections.MapUtils
21 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode
24 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintTemplateService
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
28 import org.slf4j.LoggerFactory
29 import java.util.*
30
31 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, ResourceAssignment> {
32
33     private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
34
35     lateinit var raRuntimeService: ResourceAssignmentRuntimeService
36     lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
37
38     var scriptPropertyInstances: Map<String, Any> = hashMapOf()
39
40     /**
41      * This will be called from the scripts to serve instance from runtime to scripts.
42      */
43     open fun <T> scriptPropertyInstanceType(name: String): T {
44         return scriptPropertyInstances as? T
45             ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
46     }
47
48     open fun resourceDefinition(name: String): ResourceDefinition {
49         return resourceDictionaries[name]
50             ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
51     }
52
53     open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
54         val resolvedInputKeyMapping = HashMap<String, Any>()
55         if (MapUtils.isNotEmpty(inputKeyMapping)) {
56             for ((key, value) in inputKeyMapping) {
57                 val resultValue = raRuntimeService.getResolutionStore(value)
58                 val expressionValue = JacksonUtils.getValue(resultValue)
59                 log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
60                 resolvedInputKeyMapping[key] = expressionValue
61             }
62         }
63         return resolvedInputKeyMapping
64     }
65
66     open fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: Map<String, Any>): String {
67         if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
68             return valueToResolve
69         }
70         return BluePrintTemplateService.generateContent(valueToResolve, additionalContext = keyMapping)
71     }
72
73     override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
74         log.info("prepareRequest for ${resourceAssignment.name}, dictionary(${resourceAssignment.dictionaryName})," +
75                 "source(${resourceAssignment.dictionarySource})")
76         return resourceAssignment
77     }
78
79     override fun prepareResponse(): ResourceAssignment {
80         log.info("Preparing Response...")
81         return ResourceAssignment()
82     }
83
84     override fun apply(resourceAssignment: ResourceAssignment): ResourceAssignment {
85         try {
86             prepareRequest(resourceAssignment)
87             process(resourceAssignment)
88         } catch (runtimeException: RuntimeException) {
89             recover(runtimeException, resourceAssignment)
90         }
91         return prepareResponse()
92     }
93
94 }