e1365523d87728896a988d6c1a0277d3327c4428
[ccsdk/cds.git] /
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.apps.controllerblueprints.scripts
18
19 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
20 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService
21 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
22 import org.springframework.stereotype.Service
23 import java.io.File
24 import kotlin.script.experimental.api.ResultValue
25 import kotlin.script.experimental.api.resultOrNull
26 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
27
28 @Service
29 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
30
31     override 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(getBluePrintScriptsJarName(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 }
61
62 fun getBluePrintScriptsJarName(blueprintContext: BluePrintContext): String {
63     return "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts.jar"
64 }