2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.controllerblueprints.scripts
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
24 import kotlin.script.experimental.api.ResultValue
25 import kotlin.script.experimental.api.resultOrNull
26 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
29 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
31 override fun <T> scriptInstance(blueprintContext: BluePrintContext, scriptClassName: String,
32 reCompile: Boolean): T {
34 val kotlinScriptPath = blueprintContext.rootPath.plus(File.separator)
35 .plus(BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR)
37 val compiledJar = kotlinScriptPath.plus(File.separator)
38 .plus(getBluePrintScriptsJarName(blueprintContext))
40 val scriptSource = BluePrintSourceCode()
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
49 val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>()
50 val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName)
52 val compiledResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource, compilationConfiguration,
55 val returnValue = compiledResponse.resultOrNull()?.returnValue as? ResultValue.Value
57 return returnValue?.value!! as T
62 fun getBluePrintScriptsJarName(blueprintContext: BluePrintContext): String {
63 return "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts.jar"