Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / scripts / BluePrintScriptsServiceImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.scripts
19
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
27
28 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
29
30     val log = logger(BluePrintScriptsServiceImpl::class)
31
32     override suspend fun <T> scriptInstance(bluePrintSourceCode: BluePrintSourceCode, scriptClassName: String): T {
33         val bluePrintCompileService = BluePrintCompileService()
34         return bluePrintCompileService.eval(bluePrintSourceCode, scriptClassName, null)
35     }
36
37     override suspend fun <T> scriptInstance(
38         blueprintBasePath: String,
39         artifactName: String,
40         artifactVersion: String,
41         scriptClassName: String,
42         reCompile: Boolean
43     ): T {
44
45         val sources: MutableList<String> = arrayListOf()
46         sources.add(normalizedPathName(blueprintBasePath, BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR))
47
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)
55     }
56
57     override suspend fun <T> scriptInstance(
58         blueprintBasePath: String,
59         scriptClassName: String,
60         reCompile: Boolean
61     ): T {
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
68         )
69     }
70
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
75     }
76
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
81     }
82 }