2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.core.scripts
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.logger
23 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
27 import kotlin.script.experimental.api.ResultValue
28 import kotlin.script.experimental.api.resultOrNull
29 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
32 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
34 val log = logger(BluePrintScriptsServiceImpl::class)
36 override suspend fun <T> scriptInstance(bluePrintSourceCode: BluePrintSourceCode, scriptClassName: String): T {
37 val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>()
38 val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName)
40 val compiledResponse = BlueprintScriptingHost(scriptEvaluator)
41 .eval(bluePrintSourceCode, compilationConfiguration, null)
43 val returnValue = compiledResponse.resultOrNull()?.returnValue as? ResultValue.Value
44 return returnValue?.value!! as T
47 override suspend fun <T> scriptInstance(blueprintBasePath: String, artifactName: String, artifactVersion: String,
48 scriptClassName: String, reCompile: Boolean): T {
50 val sources: MutableList<String> = arrayListOf()
51 sources.add(normalizedPathName(blueprintBasePath, BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR))
53 val scriptSource = BluePrintSourceCode()
54 scriptSource.blueprintKotlinSources = sources
55 scriptSource.moduleName = "$artifactName-$artifactVersion-cba-kts"
56 scriptSource.cacheKey = BluePrintFileUtils.compileCacheKey(blueprintBasePath)
57 scriptSource.targetJarFile = BluePrintFileUtils.compileJarFile(blueprintBasePath, artifactName, artifactVersion)
58 scriptSource.regenerate = reCompile
59 return scriptInstance(scriptSource, scriptClassName)
62 override suspend fun <T> scriptInstance(blueprintBasePath: String, scriptClassName: String,
63 reCompile: Boolean): T {
64 val toscaMetaData = BluePrintMetadataUtils.toscaMetaData(blueprintBasePath)
65 checkNotNull(toscaMetaData.templateName) { "couldn't find 'Template-Name' key in TOSCA.meta" }
66 checkNotNull(toscaMetaData.templateVersion) { "couldn't find 'Template-Version' key in TOSCA.meta" }
67 return scriptInstance(blueprintBasePath, toscaMetaData.templateName!!, toscaMetaData.templateVersion!!,
68 scriptClassName, reCompile)
71 override suspend fun <T> scriptInstance(cacheKey: String, scriptClassName: String): T {
72 val args = ArrayList<Any?>()
73 return BluePrintCompileCache.classLoader(cacheKey).loadClass(scriptClassName).constructors
74 .single().newInstance(*args.toArray()) as T
77 override suspend fun <T> scriptInstance(scriptClassName: String): T {
78 val args = ArrayList<Any?>()
79 return Thread.currentThread().contextClassLoader.loadClass(scriptClassName).constructors
80 .single().newInstance(*args.toArray()) as T