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
26 import java.util.ArrayList
28 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
30 val log = logger(BluePrintScriptsServiceImpl::class)
32 override suspend fun <T> scriptInstance(bluePrintSourceCode: BluePrintSourceCode, scriptClassName: String): T {
33 val bluePrintCompileService = BluePrintCompileService()
34 return bluePrintCompileService.eval(bluePrintSourceCode, scriptClassName, null)
37 override suspend fun <T> scriptInstance(
38 blueprintBasePath: String,
40 artifactVersion: String,
41 scriptClassName: String,
45 val sources: MutableList<String> = arrayListOf()
46 sources.add(normalizedPathName(blueprintBasePath, BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR))
48 val scriptSource = BluePrintSourceCode()
49 scriptSource.blueprintKotlinSources = sources
50 scriptSource.moduleName = "$artifactName-$artifactVersion-cba-kts"
51 scriptSource.cacheKey = BluePrintFileUtils.compileCacheKey(blueprintBasePath)
52 scriptSource.targetJarFile = BluePrintFileUtils.compileJarFile(blueprintBasePath, artifactName, artifactVersion)
53 scriptSource.regenerate = reCompile
54 return scriptInstance(scriptSource, scriptClassName)
57 override suspend fun <T> scriptInstance(
58 blueprintBasePath: String,
59 scriptClassName: String,
62 val toscaMetaData = BluePrintMetadataUtils.toscaMetaData(blueprintBasePath)
63 checkNotNull(toscaMetaData.templateName) { "couldn't find 'Template-Name' key in TOSCA.meta" }
64 checkNotNull(toscaMetaData.templateVersion) { "couldn't find 'Template-Version' key in TOSCA.meta" }
65 return scriptInstance(
66 blueprintBasePath, toscaMetaData.templateName!!, toscaMetaData.templateVersion!!,
67 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