e2c02603a11709f0f5798b6caef37896dc718551
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
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.controllerblueprints.core.scripts
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintScriptsService
22 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
23 import java.io.File
24 import java.util.*
25 import kotlin.script.experimental.api.ResultValue
26 import kotlin.script.experimental.api.resultOrNull
27 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
28
29 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
30
31     override suspend fun <T> scriptInstance(blueprintContext: BluePrintContext, scriptClassName: String,
32                                             reCompile: Boolean): T {
33
34         val kotlinScriptPath = blueprintContext.rootPath.plus(File.separator)
35                 .plus(BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR)
36
37         val compiledJar = kotlinScriptPath.plus(File.separator)
38                 .plus(bluePrintScriptsJarName(blueprintContext))
39
40         val scriptSource = BluePrintSourceCode()
41
42         val sources: MutableList<String> = arrayListOf()
43         sources.add(kotlinScriptPath)
44         scriptSource.blueprintKotlinSources = sources
45         scriptSource.moduleName = "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts"
46         scriptSource.targetJarFile = File(compiledJar)
47         scriptSource.regenerate = reCompile
48
49         val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>()
50         val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName)
51
52         val compiledResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource, compilationConfiguration,
53                 null)
54
55         val returnValue = compiledResponse.resultOrNull()?.returnValue as? ResultValue.Value
56
57         return returnValue?.value!! as T
58     }
59
60     override suspend fun <T> scriptInstance(scriptClassName: String): T {
61         val args = ArrayList<Any?>()
62         return Thread.currentThread().contextClassLoader.loadClass(scriptClassName).constructors
63                 .single().newInstance(*args.toArray()) as T
64     }
65
66     private fun bluePrintScriptsJarName(blueprintContext: BluePrintContext): String {
67         return "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts.jar"
68     }
69 }