Revert "Renaming Files having BluePrint to have Blueprint"
[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
29     private val log = LoggerFactory.getLogger(AbstractScriptComponentFunction::class.java)!!
30
31     companion object {
32
33         const val DYNAMIC_PROPERTIES = "dynamic-properties"
34     }
35
36     lateinit var scriptType: String
37
38     /**
39      * Store Dynamic Script Dependency Instances, Objects present inside won't be persisted or state maintained.
40      */
41     @Deprecated("Dependencies will be resolved dynamically")
42     var functionDependencyInstances: MutableMap<String, Any> = hashMapOf()
43
44     /**
45      * This will be called from the scripts to serve instance from runtime to scripts.
46      */
47     @Deprecated("Dependencies will be resolved dynamically")
48     open fun <T> functionDependencyInstanceAsType(name: String): T {
49         return functionDependencyInstances[name] as? T
50             ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
51     }
52
53     fun checkDynamicProperties(key: String): Boolean {
54         return operationInputs[DYNAMIC_PROPERTIES]?.has(key) ?: false
55     }
56
57     fun getDynamicProperties(key: String): JsonNode {
58         return operationInputs[DYNAMIC_PROPERTIES]!!.get(key)
59     }
60
61     suspend fun executeScript(executionServiceInput: ExecutionServiceInput) {
62         return when (scriptType) {
63             BluePrintConstants.SCRIPT_JYTHON -> {
64                 executeScriptBlocking(executionServiceInput)
65             }
66             else -> {
67                 executeScriptNB(executionServiceInput)
68             }
69         }
70     }
71
72     private suspend fun executeScriptNB(executionServiceInput: ExecutionServiceInput) {
73         try {
74             processNB(executionServiceInput)
75         } catch (runtimeException: RuntimeException) {
76             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
77             recoverNB(runtimeException, executionServiceInput)
78         }
79     }
80
81     private fun executeScriptBlocking(executionServiceInput: ExecutionServiceInput) {
82         try {
83             process(executionServiceInput)
84         } catch (runtimeException: RuntimeException) {
85             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
86             recover(runtimeException, executionServiceInput)
87         }
88     }
89
90     /**
91      * If Jython Script, Override Blocking methods(process() and recover())
92      * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default
93      * blocking
94      * methods will have default implementation,
95      *
96      * Always applyNB() method will be invoked, apply() won't be called from parent
97      */
98
99     final override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
100         throw BluePrintException("Not Implemented, use applyNB method")
101     }
102
103     final override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
104         throw BluePrintException("Not Implemented required")
105     }
106
107     final override fun prepareResponse(): ExecutionServiceOutput {
108         throw BluePrintException("Not Implemented required")
109     }
110
111     final override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
112         throw BluePrintException("Not Implemented required")
113     }
114
115     final override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
116         throw BluePrintException("Not Implemented required")
117     }
118
119     final override suspend fun prepareResponseNB(): ExecutionServiceOutput {
120         throw BluePrintException("Not Implemented required")
121     }
122
123     override fun process(executionRequest: ExecutionServiceInput) {
124         throw BluePrintException("Not Implemented, child class will implement this")
125     }
126
127     override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
128         throw BluePrintException("Not Implemented, child class will implement this")
129     }
130 }