f17085ef1d79bb24c3ac386679b93ec9fff32855
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / AbstractScriptComponentFunction.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
25 import org.slf4j.LoggerFactory
26
27 abstract class AbstractScriptComponentFunction : AbstractComponentFunction() {
28     private val log = LoggerFactory.getLogger(AbstractScriptComponentFunction::class.java)!!
29
30     companion object {
31         const val DYNAMIC_PROPERTIES = "dynamic-properties"
32     }
33
34     lateinit var scriptType: String
35
36     /**
37      * Store Dynamic Script Dependency Instances, Objects present inside won't be persisted or state maintained.
38      */
39     @Deprecated("Dependencies will be resolved dynamically")
40     var functionDependencyInstances: MutableMap<String, Any> = hashMapOf()
41
42     /**
43      * This will be called from the scripts to serve instance from runtime to scripts.
44      */
45     @Deprecated("Dependencies will be resolved dynamically")
46     open fun <T> functionDependencyInstanceAsType(name: String): T {
47         return functionDependencyInstances[name] as? T
48                 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
49     }
50
51     fun checkDynamicProperties(key: String): Boolean {
52         return operationInputs[DYNAMIC_PROPERTIES]?.has(key) ?: false
53     }
54
55     fun getDynamicProperties(key: String): JsonNode {
56         return operationInputs[DYNAMIC_PROPERTIES]!!.get(key)
57     }
58
59     suspend fun executeScript(executionServiceInput: ExecutionServiceInput) {
60         return when (scriptType) {
61             BluePrintConstants.SCRIPT_JYTHON -> {
62                 executeScriptBlocking(executionServiceInput)
63             }
64             else -> {
65                 executeScriptNB(executionServiceInput)
66             }
67         }
68     }
69
70     private suspend fun executeScriptNB(executionServiceInput: ExecutionServiceInput) {
71         try {
72             processNB(executionServiceInput)
73         } catch (runtimeException: RuntimeException) {
74             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
75             recoverNB(runtimeException, executionServiceInput)
76         }
77     }
78
79     private fun executeScriptBlocking(executionServiceInput: ExecutionServiceInput) {
80         try {
81             process(executionServiceInput)
82         } catch (runtimeException: RuntimeException) {
83             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
84             recover(runtimeException, executionServiceInput)
85         }
86     }
87
88     /**
89      * If Jython Script, Override Blocking methods(process() and recover())
90      * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default
91      * blocking
92      * methods will have default implementation,
93      *
94      * Always applyNB() method will be invoked, apply() won't be called from parent
95      */
96
97     final override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
98         throw BluePrintException("Not Implemented, use applyNB method")
99     }
100
101     final override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
102         throw BluePrintException("Not Implemented required")
103     }
104
105     final override fun prepareResponse(): ExecutionServiceOutput {
106         throw BluePrintException("Not Implemented required")
107     }
108
109     final override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
110         throw BluePrintException("Not Implemented required")
111     }
112
113     final override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
114         throw BluePrintException("Not Implemented required")
115     }
116
117     final override suspend fun prepareResponseNB(): ExecutionServiceOutput {
118         throw BluePrintException("Not Implemented required")
119     }
120
121     override fun process(executionRequest: ExecutionServiceInput) {
122         throw BluePrintException("Not Implemented, child class will implement this")
123     }
124
125     override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
126         throw BluePrintException("Not Implemented, child class will implement this")
127     }
128 }