diff options
author | 2019-01-30 15:52:30 -0500 | |
---|---|---|
committer | 2019-02-04 09:45:59 -0500 | |
commit | 148c18464cc0ac9948950009084d4338e456321b (patch) | |
tree | 92945cba15aec3676f87cfc6b6ea37bbbee6f99b /ms/controllerblueprints/modules/blueprint-scripts/src | |
parent | b8f27514062a7acd17cf7de8d3a7eab3ba2eb180 (diff) |
blueprint scripting services
Change-Id: I834b83e0c2716eceadeec8a5f17a7604e938166a
Issue-ID: CCSDK-941
Signed-off-by: Muthuramalingam, Brinda Santh <bs2796@att.com>
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-scripts/src')
6 files changed, 83 insertions, 26 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintCompilerProxy.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintCompilerProxy.kt index 7e9e86882..ce9553c0e 100644 --- a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintCompilerProxy.kt +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintCompilerProxy.kt @@ -16,7 +16,6 @@ package org.onap.ccsdk.apps.controllerblueprints.scripts -import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoots import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback @@ -28,6 +27,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot +import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer import org.jetbrains.kotlin.config.* import org.slf4j.LoggerFactory import java.io.File @@ -94,13 +94,11 @@ open class BluePrintsCompilerProxy(private val hostConfiguration: ScriptingHostC // Compile Kotlin Sources val compiled = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment) - log.info("Generated jar(${compiledJarFile.absolutePath}) status : $compiled}") - val analyzerWithCompilerReport = AnalyzerWithCompilerReport(messageCollector, environment.configuration.languageVersionSettings) if (analyzerWithCompilerReport.hasErrors()) { - return failure() + return ResultWithDiagnostics.Failure(messageCollector.diagnostics) } } diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHost.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptingHost.kt index 59ce4abb5..df721e0f2 100644 --- a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHost.kt +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptingHost.kt @@ -41,12 +41,14 @@ open class BlueprintScriptingHost(evaluator: ScriptEvaluator) : BasicScriptingHo compiler(script, scriptCompilationConfiguration) .onSuccess { evaluator(it, configuration) + }.onFailure { failedResult -> + val messages = failedResult.reports?.joinToString("\n") + throw BluePrintProcessorException(messages) } } } - -open class BluePrintScriptEvaluator<T>(private val scriptClassName: String) : ScriptEvaluator { +open class BluePrintScriptEvaluator(private val scriptClassName: String) : ScriptEvaluator { private val log = LoggerFactory.getLogger(BluePrintScriptEvaluator::class.java)!! @@ -55,7 +57,7 @@ open class BluePrintScriptEvaluator<T>(private val scriptClassName: String) : Sc scriptEvaluationConfiguration: ScriptEvaluationConfiguration? ): ResultWithDiagnostics<EvaluationResult> = try { - log.info("Getting class name($scriptClassName) of type() from the compiled sources ") + log.debug("Getting script class name($scriptClassName) from the compiled sources ") val bluePrintCompiledScript = compiledScript as BluePrintCompiledScript bluePrintCompiledScript.scriptClassFQName = scriptClassName @@ -76,10 +78,10 @@ open class BluePrintScriptEvaluator<T>(private val scriptClassName: String) : Sc args.addAll(it) } - val instance = scriptClass.java.newInstance() as? T + val instance = scriptClass.java.constructors.single().newInstance(*args.toArray()) ?: throw BluePrintProcessorException("failed to create instance from the script") - log.info("Created script instance successfully....") + log.info("Created script instance of type ${instance.javaClass}") ResultWithDiagnostics.Success(EvaluationResult(ResultValue.Value(scriptClass.qualifiedName!!, instance, "", instance), diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptConfiguration.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsConfiguration.kt index 3b3a59079..c4e2cfba4 100644 --- a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptConfiguration.kt +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsConfiguration.kt @@ -18,11 +18,9 @@ package org.onap.ccsdk.apps.controllerblueprints.scripts import java.io.File import kotlin.script.experimental.annotations.KotlinScript -import kotlin.script.experimental.api.ScriptCompilationConfiguration -import kotlin.script.experimental.api.SourceCode -import kotlin.script.experimental.api.defaultImports +import kotlin.script.experimental.api.* import kotlin.script.experimental.jvm.jvm -import kotlin.script.experimental.jvm.util.classpathFromClassloader +import kotlin.script.experimental.jvm.util.classpathFromClasspathProperty @KotlinScript( fileExtension = "cba.kts", @@ -33,15 +31,14 @@ abstract class BluePrintKotlinScript object BluePrintScripCompilationConfiguration : ScriptCompilationConfiguration( { - defaultImports( - "org.onap.ccsdk.apps.controllerblueprints.core.*", - "org.onap.ccsdk.apps.controllerblueprints.core.data.*", - "org.onap.ccsdk.apps.controllerblueprints.core.interfaces.*", - "org.onap.ccsdk.apps.controllerblueprints.core.services.*", - "org.onap.ccsdk.apps.controllerblueprints.core.utils.*") jvm { - classpathFromClassloader(BluePrintScripCompilationConfiguration::class.java.classLoader) + //classpathFromClassloader(BluePrintScripCompilationConfiguration::class.java.classLoader) + classpathFromClasspathProperty() } + ide{ + acceptedLocations(ScriptAcceptedLocation.Everywhere) + } + } ) diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsServiceImpl.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsServiceImpl.kt new file mode 100644 index 000000000..e1365523d --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsServiceImpl.kt @@ -0,0 +1,64 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.apps.controllerblueprints.scripts + +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService +import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext +import org.springframework.stereotype.Service +import java.io.File +import kotlin.script.experimental.api.ResultValue +import kotlin.script.experimental.api.resultOrNull +import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate + +@Service +open class BluePrintScriptsServiceImpl : BluePrintScriptsService { + + override fun <T> scriptInstance(blueprintContext: BluePrintContext, scriptClassName: String, + reCompile: Boolean): T { + + val kotlinScriptPath = blueprintContext.rootPath.plus(File.separator) + .plus(BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR) + + val compiledJar = kotlinScriptPath.plus(File.separator) + .plus(getBluePrintScriptsJarName(blueprintContext)) + + val scriptSource = BluePrintSourceCode() + + val sources: MutableList<String> = arrayListOf() + sources.add(kotlinScriptPath) + scriptSource.blueprintKotlinSources = sources + scriptSource.moduleName = "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts" + scriptSource.targetJarFile = File(compiledJar) + scriptSource.regenerate = reCompile + + val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>() + val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName) + + val compiledResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource, compilationConfiguration, + null) + + val returnValue = compiledResponse.resultOrNull()?.returnValue as? ResultValue.Value + + return returnValue?.value!! as T + } + +} + +fun getBluePrintScriptsJarName(blueprintContext: BluePrintContext): String { + return "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts.jar" +}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/main/resources/META-INF/kotlin/script/templates/org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintKotlinScript b/ms/controllerblueprints/modules/blueprint-scripts/src/main/resources/META-INF/kotlin/script/templates/org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintKotlinScript new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/resources/META-INF/kotlin/script/templates/org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintKotlinScript diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHostTest.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHostTest.kt index 4b6f2a47d..e84347d23 100644 --- a/ms/controllerblueprints/modules/blueprint-scripts/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHostTest.kt +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHostTest.kt @@ -18,9 +18,7 @@ package org.onap.ccsdk.apps.controllerblueprints.scripts import org.apache.commons.io.FileUtils -import org.junit.Ignore import org.junit.Test -import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode import java.io.File import kotlin.script.experimental.jvm.util.classpathFromClass import kotlin.script.experimental.jvm.util.classpathFromClassloader @@ -29,9 +27,7 @@ import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromT class BlueprintScriptingHostTest { - @Test - @Ignore - fun `test classpaths`() { + private fun viewClassPathInfo() { println(" *********** classpathFromClass *********** ") classpathFromClass(BlueprintScriptingHostTest::class.java.classLoader, @@ -65,7 +61,7 @@ class BlueprintScriptingHostTest { val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>() - val scriptEvaluator = BluePrintScriptEvaluator<BlueprintFunctionNode<String, String>>(scriptClassName) + val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName) val scriptSource2 = BluePrintSourceCode() scriptSource2.moduleName = "blueprint-test-script" |