Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / ResourceAssignmentProcessor.kt
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.cds.blueprintsprocessor.functions.resource.resolution.processor
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import org.apache.commons.collections.MapUtils
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
30 import org.slf4j.LoggerFactory
31 import java.util.*
32
33 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
34
35     private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
36
37     lateinit var raRuntimeService: ResourceAssignmentRuntimeService
38     lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
39
40     var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf()
41
42     /**
43      * This will be called from the scripts to serve instance from runtime to scripts.
44      */
45     open fun <T> scriptPropertyInstanceType(name: String): T {
46         return scriptPropertyInstances as? T
47             ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
48     }
49
50     open fun getFromInput(resourceAssignment: ResourceAssignment): JsonNode? {
51         var value: JsonNode? = null
52         try {
53             value = raRuntimeService.getInputValue(resourceAssignment.name)
54             ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
55         } catch (e: BluePrintProcessorException) {
56             // NoOp - couldn't find value from input
57         }
58         return value
59     }
60
61     open fun resourceDefinition(name: String): ResourceDefinition {
62         return resourceDictionaries[name]
63             ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
64     }
65
66     open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
67         val resolvedInputKeyMapping = HashMap<String, Any>()
68         if (MapUtils.isNotEmpty(inputKeyMapping)) {
69             for ((key, value) in inputKeyMapping) {
70                 val resultValue = raRuntimeService.getResolutionStore(value)
71                 val expressionValue = JacksonUtils.getValue(resultValue)
72                 log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
73                 resolvedInputKeyMapping[key] = expressionValue
74             }
75         }
76         return resolvedInputKeyMapping
77     }
78
79     open fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: Map<String, Any>): String {
80         if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
81             return valueToResolve
82         }
83         return BluePrintTemplateService.generateContent(valueToResolve, additionalContext = keyMapping)
84     }
85
86     override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
87         log.info("prepareRequest for ${resourceAssignment.name}, dictionary(${resourceAssignment.dictionaryName})," +
88                 "source(${resourceAssignment.dictionarySource})")
89         return resourceAssignment
90     }
91
92     override fun prepareResponse(): Boolean {
93         log.info("Preparing Response...")
94         return true
95     }
96
97     override fun apply(resourceAssignment: ResourceAssignment): Boolean {
98         try {
99             prepareRequest(resourceAssignment)
100             process(resourceAssignment)
101         } catch (runtimeException: RuntimeException) {
102             recover(runtimeException, resourceAssignment)
103         }
104         return prepareResponse()
105     }
106
107     fun addError(type: String, name: String, error: String) {
108         raRuntimeService.getBluePrintError().addError(type, name, error)
109     }
110
111     fun addError(error: String) {
112         raRuntimeService.getBluePrintError().addError(error)
113     }
114
115 }