Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-scripts / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / scripts / BluePrintScriptsServiceImpl.kt
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.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 org.springframework.stereotype.Service
24 import java.io.File
25 import java.util.*
26 import kotlin.script.experimental.api.ResultValue
27 import kotlin.script.experimental.api.resultOrNull
28 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
29
30 @Service
31 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
32
33     override fun <T> scriptInstance(blueprintContext: BluePrintContext, scriptClassName: String,
34                                     reCompile: Boolean): T {
35
36         val kotlinScriptPath = blueprintContext.rootPath.plus(File.separator)
37                 .plus(BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR)
38
39         val compiledJar = kotlinScriptPath.plus(File.separator)
40                 .plus(getBluePrintScriptsJarName(blueprintContext))
41
42         val scriptSource = BluePrintSourceCode()
43
44         val sources: MutableList<String> = arrayListOf()
45         sources.add(kotlinScriptPath)
46         scriptSource.blueprintKotlinSources = sources
47         scriptSource.moduleName = "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts"
48         scriptSource.targetJarFile = File(compiledJar)
49         scriptSource.regenerate = reCompile
50
51         val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>()
52         val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName)
53
54         val compiledResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource, compilationConfiguration,
55                 null)
56
57         val returnValue = compiledResponse.resultOrNull()?.returnValue as? ResultValue.Value
58
59         return returnValue?.value!! as T
60     }
61
62     override fun <T> scriptInstance(scriptClassName: String): T {
63         val args = ArrayList<Any?>()
64         return Thread.currentThread().contextClassLoader.loadClass(scriptClassName).constructors
65                 .single().newInstance(*args.toArray()) as T
66     }
67 }
68
69 fun getBluePrintScriptsJarName(blueprintContext: BluePrintContext): String {
70     return "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts.jar"
71 }