Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-scripts / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / scripts / BluePrintScriptingHost.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.controllerblueprints.scripts
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
20 import org.slf4j.LoggerFactory
21 import java.util.*
22 import kotlin.script.experimental.api.*
23 import kotlin.script.experimental.host.BasicScriptingHost
24 import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
25 import kotlin.script.experimental.jvmhost.JvmScriptCompiler
26 import kotlin.script.experimental.jvmhost.impl.withDefaults
27
28 val blueprintScriptCompiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration,
29         BluePrintsCompilerProxy(defaultJvmScriptingHostConfiguration.withDefaults()))
30
31 open class BlueprintScriptingHost(evaluator: ScriptEvaluator) : BasicScriptingHost(blueprintScriptCompiler, evaluator) {
32
33     override fun eval(
34             script: SourceCode,
35             scriptCompilationConfiguration: ScriptCompilationConfiguration,
36             configuration: ScriptEvaluationConfiguration?
37     ): ResultWithDiagnostics<EvaluationResult> =
38
39             runInCoroutineContext {
40
41                 compiler(script, scriptCompilationConfiguration)
42                         .onSuccess {
43                             evaluator(it, configuration)
44                         }.onFailure { failedResult ->
45                             val messages = failedResult.reports?.joinToString("\n")
46                             throw BluePrintProcessorException(messages)
47                         }
48             }
49 }
50
51 open class BluePrintScriptEvaluator(private val scriptClassName: String) : ScriptEvaluator {
52
53     private val log = LoggerFactory.getLogger(BluePrintScriptEvaluator::class.java)!!
54
55     override suspend operator fun invoke(
56             compiledScript: CompiledScript<*>,
57             scriptEvaluationConfiguration: ScriptEvaluationConfiguration?
58     ): ResultWithDiagnostics<EvaluationResult> =
59             try {
60                 log.debug("Getting script class name($scriptClassName) from the compiled sources ")
61                 val bluePrintCompiledScript = compiledScript as BluePrintCompiledScript
62                 bluePrintCompiledScript.scriptClassFQName = scriptClassName
63
64                 val res = compiledScript.getClass(scriptEvaluationConfiguration)
65                 when (res) {
66                     is ResultWithDiagnostics.Failure -> res
67                     is ResultWithDiagnostics.Success -> {
68
69                         val scriptClass = res.value
70                         val args = ArrayList<Any?>()
71                         scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.providedProperties)?.forEach {
72                             args.add(it.value)
73                         }
74                         scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.implicitReceivers)?.let {
75                             args.addAll(it)
76                         }
77                         scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.constructorArgs)?.let {
78                             args.addAll(it)
79                         }
80
81                         val instance = scriptClass.java.constructors.single().newInstance(*args.toArray())
82                                 ?: throw BluePrintProcessorException("failed to create instance from the script")
83
84                         log.info("Created script instance of type ${instance.javaClass}")
85
86                         ResultWithDiagnostics.Success(EvaluationResult(ResultValue.Value(scriptClass.qualifiedName!!,
87                                 instance, "", instance),
88                                 scriptEvaluationConfiguration))
89                     }
90                 }
91             } catch (e: Throwable) {
92                 ResultWithDiagnostics.Failure(e.asDiagnostics("Error evaluating script"))
93             }
94 }