From 73528fe9ce4471be5e76c042ea02b40199d36532 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Wed, 30 Jan 2019 15:52:30 -0500 Subject: blueprint scripting services Change-Id: I834b83e0c2716eceadeec8a5f17a7604e938166a Issue-ID: CCSDK-941 Signed-off-by: Muthuramalingam, Brinda Santh --- .../functions/resource-resolution/pom.xml | 4 + .../resolution/ResourceSourceProperties.kt | 6 +- .../CapabilityResourceAssignmentProcessor.kt | 56 ++++++++++--- .../processor/ResourceAssignmentProcessor.kt | 10 +++ .../CapabilityResourceAssignmentProcessorTest.kt | 79 ++++++++++++++++++ ms/blueprintsprocessor/parent/pom.xml | 7 +- .../modules/blueprint-scripts/pom.xml | 4 + .../scripts/BluePrintCompilerProxy.kt | 6 +- .../scripts/BluePrintScriptConfiguration.kt | 61 -------------- .../scripts/BluePrintScriptingHost.kt | 94 ++++++++++++++++++++++ .../scripts/BluePrintScriptsConfiguration.kt | 58 +++++++++++++ .../scripts/BluePrintScriptsServiceImpl.kt | 64 +++++++++++++++ .../scripts/BlueprintScriptingHost.kt | 92 --------------------- ...trollerblueprints.scripts.BluePrintKotlinScript | 0 .../scripts/BlueprintScriptingHostTest.kt | 8 +- 15 files changed, 372 insertions(+), 177 deletions(-) create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt delete mode 100644 ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptConfiguration.kt create mode 100644 ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptingHost.kt create mode 100644 ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsConfiguration.kt create mode 100644 ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsServiceImpl.kt delete mode 100644 ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHost.kt create mode 100644 ms/controllerblueprints/modules/blueprint-scripts/src/main/resources/META-INF/kotlin/script/templates/org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintKotlinScript (limited to 'ms') diff --git a/ms/blueprintsprocessor/functions/resource-resolution/pom.xml b/ms/blueprintsprocessor/functions/resource-resolution/pom.xml index 925f9e33..b29149d8 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/pom.xml +++ b/ms/blueprintsprocessor/functions/resource-resolution/pom.xml @@ -34,6 +34,10 @@ db-resources ${project.version} --> + + org.onap.ccsdk.apps.controllerblueprints + blueprint-scripts + org.springframework.boot spring-boot-starter-data-jpa diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt index 9bad0998..a44d366c 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +@file:Suppress("unused") package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution -open class ResourceSourceProperties { - -} +open class ResourceSourceProperties open class InputResourceSource : ResourceSourceProperties() { lateinit var key: String @@ -51,6 +50,7 @@ open class RestResourceSource : ResourceSourceProperties() { open class CapabilityResourceSource : ResourceSourceProperties() { lateinit var type: String lateinit var instanceName: String + var instanceDependencies: List? = null lateinit var path: String lateinit var expressionType: String var inputKeyMapping: MutableMap? = null diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt index 6c235599..1370a479 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt @@ -18,14 +18,16 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.CapabilityResourceSource import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment -import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.ApplicationContext import org.springframework.stereotype.Service @Service("resource-assignment-processor-capability") -open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() { +open class CapabilityResourceAssignmentProcessor(private var applicationContext: ApplicationContext, + private val bluePrintScriptsService: BluePrintScriptsService) : + ResourceAssignmentProcessor() { companion object { const val CAPABILITY_TYPE_KOTLIN_COMPONENT = "KOTLIN-COMPONENT" @@ -33,9 +35,6 @@ open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() const val CAPABILITY_TYPE_JYTHON_COMPONENT = "JYTHON-COMPONENT" } - @Autowired - private lateinit var applicationContext: ApplicationContext - override fun getName(): String { return "resource-assignment-processor-capability" } @@ -46,20 +45,25 @@ open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() ?: throw BluePrintProcessorException("couldn't get resource definition for ${resourceAssignment.dictionaryName}") val resourceSource = resourceDefinition.sources[resourceAssignment.dictionarySource] - ?: throw BluePrintProcessorException("couldn't get resource definition ${resourceAssignment.dictionaryName} source(${resourceAssignment.dictionarySource})") + ?: throw BluePrintProcessorException("couldn't get resource definition " + + "${resourceAssignment.dictionaryName} source(${resourceAssignment.dictionarySource})") val resourceSourceProps = checkNotNull(resourceSource.properties) { "failed to get $resourceSource properties" } - val capabilityResourceSourceProperty = JacksonUtils.getInstanceFromMap(resourceSourceProps, CapabilityResourceSource::class.java) + /** + * Get the Capability Resource Source Info from Property Definitions. + */ + val capabilityResourceSourceProperty = JacksonUtils + .getInstanceFromMap(resourceSourceProps, CapabilityResourceSource::class.java) val instanceType = capabilityResourceSourceProperty.type val instanceName = capabilityResourceSourceProperty.instanceName - var componentResourceAssignmentProcessor: ResourceAssignmentProcessor? = null when (instanceType) { - CAPABILITY_TYPE_KOTLIN_COMPONENT ->{ - TODO("NO implementation") + CAPABILITY_TYPE_KOTLIN_COMPONENT -> { + componentResourceAssignmentProcessor = getKotlinResourceAssignmentProcessorInstance(instanceName, + capabilityResourceSourceProperty.instanceDependencies) } CAPABILITY_TYPE_JAVA_COMPONENT -> { // Initialize Capability Resource Assignment Processor @@ -84,4 +88,36 @@ open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() TODO("To Implement") } + + private fun getKotlinResourceAssignmentProcessorInstance(scriptClassName: String, + instanceNames: List? = null): ResourceAssignmentProcessor { + var scriptPropertyInstances: MutableMap? = null + + if (instanceNames != null && instanceNames.isNotEmpty()) { + scriptPropertyInstances = hashMapOf() + instanceNames.forEach { + scriptPropertyInstances[it] = applicationContext.getBean(it) + ?: throw BluePrintProcessorException("couldn't get the dependency instance($it)") + } + } + + return getKotlinResourceAssignmentProcessorInstance(scriptClassName, scriptPropertyInstances) + + } + + fun getKotlinResourceAssignmentProcessorInstance(scriptClassName: String, + scriptPropertyInstances: MutableMap? = null): + ResourceAssignmentProcessor { + + val resourceAssignmentProcessor = bluePrintScriptsService + .scriptInstance(raRuntimeService.bluePrintContext(), + scriptClassName, false) + + // Add additional Instance + if (scriptPropertyInstances != null) { + resourceAssignmentProcessor.scriptPropertyInstances = scriptPropertyInstances + } + + return resourceAssignmentProcessor + } } \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt index 43238a53..d6f46a6f 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt @@ -31,6 +31,16 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode + var scriptPropertyInstances: Map = hashMapOf() + + /** + * This will be called from the scripts to serve instance from runtime to scripts. + */ + open fun scriptPropertyInstanceType(name: String): T { + return scriptPropertyInstances as? T + ?: throw BluePrintProcessorException("couldn't get script property instance ($name)") + } + open fun resourceDefinition(name: String): ResourceDefinition { return resourceDictionaries[name] ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)") diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt new file mode 100644 index 00000000..ce05c34b --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt @@ -0,0 +1,79 @@ +/* + * 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.blueprintsprocessor.functions.resource.resolution.processor + +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService +import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment +import org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintScriptsServiceImpl +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.junit4.SpringRunner +import kotlin.test.assertNotNull + +@RunWith(SpringRunner::class) +@ContextConfiguration(classes = [CapabilityResourceAssignmentProcessor::class, BluePrintScriptsServiceImpl::class, + MockCapabilityService::class]) +class CapabilityResourceAssignmentProcessorTest { + + @Autowired + lateinit var capabilityResourceAssignmentProcessor: CapabilityResourceAssignmentProcessor + + @Test + fun `test kotlin capability`() { + + val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext( + "./../../../../components/model-catalog/blueprint-model/starter-blueprint/baseconfiguration") + + val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext) + + capabilityResourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService + capabilityResourceAssignmentProcessor.resourceDictionaries = hashMapOf() + + + val scriptPropertyInstances: MutableMap = mutableMapOf() + scriptPropertyInstances["mock-service1"] = MockCapabilityService() + scriptPropertyInstances["mock-service2"] = MockCapabilityService() + + val resourceAssignmentProcessor = capabilityResourceAssignmentProcessor + .getKotlinResourceAssignmentProcessorInstance( + "ResourceAssignmentProcessor_cba\$ScriptResourceAssignmentProcessor", scriptPropertyInstances) + + assertNotNull(resourceAssignmentProcessor, "couldn't get kotlin script resource assignment processor") + + val resourceAssignment = ResourceAssignment().apply { + name = "ra-name" + dictionaryName = "ra-dict-name" + dictionarySource = "capability" + property = PropertyDefinition().apply { + type = "string" + } + } + + val processorName = resourceAssignmentProcessor.apply(resourceAssignment) + assertNotNull(processorName, "couldn't get kotlin script resource assignment processor name") + println(processorName) + } + +} + +open class MockCapabilityService { + +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/parent/pom.xml b/ms/blueprintsprocessor/parent/pom.xml index 472b466b..0de4b429 100755 --- a/ms/blueprintsprocessor/parent/pom.xml +++ b/ms/blueprintsprocessor/parent/pom.xml @@ -18,7 +18,7 @@ ~ limitations under the License. --> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.onap.ccsdk.apps @@ -327,6 +327,11 @@ core ${project.version} + + org.onap.ccsdk.apps.controllerblueprints + blueprint-scripts + ${project.version} + diff --git a/ms/controllerblueprints/modules/blueprint-scripts/pom.xml b/ms/controllerblueprints/modules/blueprint-scripts/pom.xml index 46c88b4c..e0858d20 100644 --- a/ms/controllerblueprints/modules/blueprint-scripts/pom.xml +++ b/ms/controllerblueprints/modules/blueprint-scripts/pom.xml @@ -54,6 +54,10 @@ org.jetbrains.kotlin kotlin-script-runtime + + org.jetbrains.kotlin + kotlin-test-junit + \ No newline at end of file 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 7e9e8688..ce9553c0 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/BluePrintScriptConfiguration.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptConfiguration.kt deleted file mode 100644 index 3b3a5907..00000000 --- a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptConfiguration.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 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.jvm.jvm -import kotlin.script.experimental.jvm.util.classpathFromClassloader - -@KotlinScript( - fileExtension = "cba.kts", - compilationConfiguration = BluePrintScripCompilationConfiguration::class, - displayName = "Controller Blueprint Archive Kotlin Scripts" -) -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) - } - } -) - -open class BluePrintSourceCode : SourceCode { - lateinit var blueprintKotlinSources: MutableList - lateinit var moduleName: String - lateinit var targetJarFile: File - var regenerate: Boolean = false - - override val text: String - get() = "" - - override val locationId: String? = null - - override val name: String? - get() = moduleName -} 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 new file mode 100644 index 00000000..df721e0f --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptingHost.kt @@ -0,0 +1,94 @@ +/* + * 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.BluePrintProcessorException +import org.slf4j.LoggerFactory +import java.util.* +import kotlin.script.experimental.api.* +import kotlin.script.experimental.host.BasicScriptingHost +import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration +import kotlin.script.experimental.jvmhost.JvmScriptCompiler +import kotlin.script.experimental.jvmhost.impl.withDefaults + +val blueprintScriptCompiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration, + BluePrintsCompilerProxy(defaultJvmScriptingHostConfiguration.withDefaults())) + +open class BlueprintScriptingHost(evaluator: ScriptEvaluator) : BasicScriptingHost(blueprintScriptCompiler, evaluator) { + + override fun eval( + script: SourceCode, + scriptCompilationConfiguration: ScriptCompilationConfiguration, + configuration: ScriptEvaluationConfiguration? + ): ResultWithDiagnostics = + + runInCoroutineContext { + + compiler(script, scriptCompilationConfiguration) + .onSuccess { + evaluator(it, configuration) + }.onFailure { failedResult -> + val messages = failedResult.reports?.joinToString("\n") + throw BluePrintProcessorException(messages) + } + } +} + +open class BluePrintScriptEvaluator(private val scriptClassName: String) : ScriptEvaluator { + + private val log = LoggerFactory.getLogger(BluePrintScriptEvaluator::class.java)!! + + override suspend operator fun invoke( + compiledScript: CompiledScript<*>, + scriptEvaluationConfiguration: ScriptEvaluationConfiguration? + ): ResultWithDiagnostics = + try { + log.debug("Getting script class name($scriptClassName) from the compiled sources ") + val bluePrintCompiledScript = compiledScript as BluePrintCompiledScript + bluePrintCompiledScript.scriptClassFQName = scriptClassName + + val res = compiledScript.getClass(scriptEvaluationConfiguration) + when (res) { + is ResultWithDiagnostics.Failure -> res + is ResultWithDiagnostics.Success -> { + + val scriptClass = res.value + val args = ArrayList() + scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.providedProperties)?.forEach { + args.add(it.value) + } + scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.implicitReceivers)?.let { + args.addAll(it) + } + scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.constructorArgs)?.let { + args.addAll(it) + } + + val instance = scriptClass.java.constructors.single().newInstance(*args.toArray()) + ?: throw BluePrintProcessorException("failed to create instance from the script") + + log.info("Created script instance of type ${instance.javaClass}") + + ResultWithDiagnostics.Success(EvaluationResult(ResultValue.Value(scriptClass.qualifiedName!!, + instance, "", instance), + scriptEvaluationConfiguration)) + } + } + } catch (e: Throwable) { + ResultWithDiagnostics.Failure(e.asDiagnostics("Error evaluating script")) + } +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsConfiguration.kt b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsConfiguration.kt new file mode 100644 index 00000000..c4e2cfba --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BluePrintScriptsConfiguration.kt @@ -0,0 +1,58 @@ +/* + * 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 java.io.File +import kotlin.script.experimental.annotations.KotlinScript +import kotlin.script.experimental.api.* +import kotlin.script.experimental.jvm.jvm +import kotlin.script.experimental.jvm.util.classpathFromClasspathProperty + +@KotlinScript( + fileExtension = "cba.kts", + compilationConfiguration = BluePrintScripCompilationConfiguration::class, + displayName = "Controller Blueprint Archive Kotlin Scripts" +) +abstract class BluePrintKotlinScript + +object BluePrintScripCompilationConfiguration : ScriptCompilationConfiguration( + { + jvm { + //classpathFromClassloader(BluePrintScripCompilationConfiguration::class.java.classLoader) + classpathFromClasspathProperty() + } + ide{ + acceptedLocations(ScriptAcceptedLocation.Everywhere) + } + + } +) + +open class BluePrintSourceCode : SourceCode { + lateinit var blueprintKotlinSources: MutableList + lateinit var moduleName: String + lateinit var targetJarFile: File + var regenerate: Boolean = false + + override val text: String + get() = "" + + override val locationId: String? = null + + override val name: String? + get() = moduleName +} 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 00000000..e1365523 --- /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 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 = 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() + 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/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 deleted file mode 100644 index 59ce4abb..00000000 --- a/ms/controllerblueprints/modules/blueprint-scripts/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/scripts/BlueprintScriptingHost.kt +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.BluePrintProcessorException -import org.slf4j.LoggerFactory -import java.util.* -import kotlin.script.experimental.api.* -import kotlin.script.experimental.host.BasicScriptingHost -import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration -import kotlin.script.experimental.jvmhost.JvmScriptCompiler -import kotlin.script.experimental.jvmhost.impl.withDefaults - -val blueprintScriptCompiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration, - BluePrintsCompilerProxy(defaultJvmScriptingHostConfiguration.withDefaults())) - -open class BlueprintScriptingHost(evaluator: ScriptEvaluator) : BasicScriptingHost(blueprintScriptCompiler, evaluator) { - - override fun eval( - script: SourceCode, - scriptCompilationConfiguration: ScriptCompilationConfiguration, - configuration: ScriptEvaluationConfiguration? - ): ResultWithDiagnostics = - - runInCoroutineContext { - - compiler(script, scriptCompilationConfiguration) - .onSuccess { - evaluator(it, configuration) - } - } -} - - -open class BluePrintScriptEvaluator(private val scriptClassName: String) : ScriptEvaluator { - - private val log = LoggerFactory.getLogger(BluePrintScriptEvaluator::class.java)!! - - override suspend operator fun invoke( - compiledScript: CompiledScript<*>, - scriptEvaluationConfiguration: ScriptEvaluationConfiguration? - ): ResultWithDiagnostics = - try { - log.info("Getting class name($scriptClassName) of type() from the compiled sources ") - val bluePrintCompiledScript = compiledScript as BluePrintCompiledScript - bluePrintCompiledScript.scriptClassFQName = scriptClassName - - val res = compiledScript.getClass(scriptEvaluationConfiguration) - when (res) { - is ResultWithDiagnostics.Failure -> res - is ResultWithDiagnostics.Success -> { - - val scriptClass = res.value - val args = ArrayList() - scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.providedProperties)?.forEach { - args.add(it.value) - } - scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.implicitReceivers)?.let { - args.addAll(it) - } - scriptEvaluationConfiguration?.get(ScriptEvaluationConfiguration.constructorArgs)?.let { - args.addAll(it) - } - - val instance = scriptClass.java.newInstance() as? T - ?: throw BluePrintProcessorException("failed to create instance from the script") - - log.info("Created script instance successfully....") - - ResultWithDiagnostics.Success(EvaluationResult(ResultValue.Value(scriptClass.qualifiedName!!, - instance, "", instance), - scriptEvaluationConfiguration)) - } - } - } catch (e: Throwable) { - ResultWithDiagnostics.Failure(e.asDiagnostics("Error evaluating script")) - } -} \ 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 00000000..e69de29b 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 4b6f2a47..e84347d2 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() - val scriptEvaluator = BluePrintScriptEvaluator>(scriptClassName) + val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName) val scriptSource2 = BluePrintSourceCode() scriptSource2.moduleName = "blueprint-test-script" -- cgit 1.2.3-korg