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
29 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
31 val log = logger(BluePrintScriptsServiceImpl::class)
33 override suspend fun <T> scriptInstance(bluePrintSourceCode: BluePrintSourceCode, scriptClassName: String): T {
34 val bluePrintCompileService = BluePrintCompileService()
35 return bluePrintCompileService.eval(bluePrintSourceCode, scriptClassName, null)
38 override suspend fun <T> scriptInstance(blueprintBasePath: String, artifactName: String, artifactVersion: String,
39 scriptClassName: String, reCompile: Boolean): T {
41 val sources: MutableList<String> = arrayListOf()
42 sources.add(normalizedPathName(blueprintBasePath, BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR))
44 val scriptSource = BluePrintSourceCode()
45 scriptSource.blueprintKotlinSources = sources
46 scriptSource.moduleName = "$artifactName-$artifactVersion-cba-kts"
47 scriptSource.cacheKey = BluePrintFileUtils.compileCacheKey(blueprintBasePath)
48 scriptSource.targetJarFile = BluePrintFileUtils.compileJarFile(blueprintBasePath, artifactName, artifactVersion)
49 scriptSource.regenerate = reCompile
50 return scriptInstance(scriptSource, scriptClassName)
53 override suspend fun <T> scriptInstance(blueprintBasePath: String, scriptClassName: String,
54 reCompile: Boolean): T {
55 val toscaMetaData = BluePrintMetadataUtils.toscaMetaData(blueprintBasePath)
56 checkNotNull(toscaMetaData.templateName) { "couldn't find 'Template-Name' key in TOSCA.meta" }
57 checkNotNull(toscaMetaData.templateVersion) { "couldn't find 'Template-Version' key in TOSCA.meta" }
58 return scriptInstance(blueprintBasePath, toscaMetaData.templateName!!, toscaMetaData.templateVersion!!,
59 scriptClassName, reCompile)
62 override suspend fun <T> scriptInstance(cacheKey: String, scriptClassName: String): T {
63 val args = ArrayList<Any?>()
64 return BluePrintCompileCache.classLoader(cacheKey).loadClass(scriptClassName).constructors
65 .single().newInstance(*args.toArray()) as T
68 override suspend fun <T> scriptInstance(scriptClassName: String): T {
69 val args = ArrayList<Any?>()
70 return Thread.currentThread().contextClassLoader.loadClass(scriptClassName).constructors
71 .single().newInstance(*args.toArray()) as T