3482d4534faee79fd1309c51518b7281c4398f71
[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  *
4  *  Modifications Copyright © 2017-2019 AT&T, Bell Canada
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
20
21 import com.fasterxml.jackson.databind.JsonNode
22 import org.apache.commons.collections.MapUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
34 import org.slf4j.LoggerFactory
35 import java.util.*
36
37 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
38
39     private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
40
41     lateinit var raRuntimeService: ResourceAssignmentRuntimeService
42     lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
43
44     var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf()
45     lateinit var scriptType: String
46
47     /**
48      * This will be called from the scripts to serve instance from runtime to scripts.
49      */
50     open fun <T> scriptPropertyInstanceType(name: String): T {
51         return scriptPropertyInstances as? T
52                 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
53     }
54
55     open fun getFromInput(resourceAssignment: ResourceAssignment): JsonNode? {
56         var value: JsonNode? = null
57         try {
58             value = raRuntimeService.getInputValue(resourceAssignment.name)
59             ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
60         } catch (e: BluePrintProcessorException) {
61             // NoOp - couldn't find value from input
62         }
63         return value
64     }
65
66     open fun resourceDefinition(name: String): ResourceDefinition {
67         return resourceDictionaries[name]
68                 ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
69     }
70
71     //TODO("Convert return Map<String, JsonNode>")
72     open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
73         val resolvedInputKeyMapping = HashMap<String, Any>()
74         if (MapUtils.isNotEmpty(inputKeyMapping)) {
75             for ((key, value) in inputKeyMapping) {
76                 val resultValue = raRuntimeService.getResolutionStore(value)
77                 val expressionValue = JacksonUtils.getValue(resultValue)
78                 log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
79                 resolvedInputKeyMapping[key] = expressionValue
80             }
81         }
82         return resolvedInputKeyMapping
83     }
84
85     //TODO("Convert keyMapping =  MutableMap<String, JsonNode>")
86     open suspend fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: MutableMap<String, Any>):
87             String {
88         if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
89             return valueToResolve
90         }
91         //TODO("Optimize to JSON Node directly")
92         return BluePrintVelocityTemplateService.generateContent(valueToResolve, keyMapping.asJsonNode().toString())
93     }
94
95     final override suspend fun applyNB(resourceAssignment: ResourceAssignment): Boolean {
96         try {
97             processNB(resourceAssignment)
98         } catch (runtimeException: RuntimeException) {
99             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
100             recoverNB(runtimeException, resourceAssignment)
101         }
102         return true
103     }
104
105     suspend fun executeScript(resourceAssignment: ResourceAssignment) {
106         return when (scriptType) {
107             BluePrintConstants.SCRIPT_JYTHON -> {
108                 executeScriptBlocking(resourceAssignment)
109             }
110             else -> {
111                 executeScriptNB(resourceAssignment)
112             }
113         }
114     }
115
116     private suspend fun executeScriptNB(resourceAssignment: ResourceAssignment) {
117         try {
118             processNB(resourceAssignment)
119         } catch (runtimeException: RuntimeException) {
120             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
121             recoverNB(runtimeException, resourceAssignment)
122         }
123     }
124
125     private fun executeScriptBlocking(resourceAssignment: ResourceAssignment) {
126         try {
127             process(resourceAssignment)
128         } catch (runtimeException: RuntimeException) {
129             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
130             recover(runtimeException, resourceAssignment)
131         }
132     }
133
134     /**
135      * If Jython Script, Override Blocking methods(process() and recover())
136      * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default
137      * blocking
138      * methods will have default implementation,
139      *
140      * Always applyNB() method will be invoked, apply() won't be called from parent
141      */
142
143     final override fun apply(resourceAssignment: ResourceAssignment): Boolean {
144         throw BluePrintException("Not Implemented, use applyNB method")
145     }
146
147     final override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
148         throw BluePrintException("Not Implemented required")
149     }
150
151     final override fun prepareResponse(): Boolean {
152         throw BluePrintException("Not Implemented required")
153     }
154
155     final override suspend fun prepareRequestNB(resourceAssignment: ResourceAssignment): ResourceAssignment {
156         throw BluePrintException("Not Implemented required")
157     }
158
159     final override suspend fun prepareResponseNB(): Boolean {
160         throw BluePrintException("Not Implemented required")
161     }
162
163     override fun process(resourceAssignment: ResourceAssignment) {
164         throw BluePrintException("Not Implemented, child class will implement this")
165     }
166
167     override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
168         throw BluePrintException("Not Implemented, child class will implement this")
169     }
170
171     fun addError(type: String, name: String, error: String) {
172         raRuntimeService.getBluePrintError().addError(type, name, error)
173     }
174
175     fun addError(error: String) {
176         raRuntimeService.getBluePrintError().addError(error)
177     }
178 }