From eda5e9cff5e6815351fddfc936ea3c8de85df421 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Fri, 25 Jan 2019 20:17:35 -0500 Subject: blueprint scripting module Change-Id: Ibe7602bdb6708d9adbe1aecd26eb14e24872f75d Issue-ID: CCSDK-941 Signed-off-by: Muthuramalingam, Brinda Santh --- .../core/script/BluePrintScriptConfiguration.kt | 86 -------------------- .../core/script/BluePrintScriptService.kt | 78 ------------------ .../core/script/BlueprintScriptingHost.kt | 93 ---------------------- .../core/script/BluePrintScriptServiceTest.kt | 49 ------------ components/parent/pom.xml | 26 ++---- 5 files changed, 5 insertions(+), 327 deletions(-) delete mode 100644 components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptConfiguration.kt delete mode 100644 components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptService.kt delete mode 100644 components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BlueprintScriptingHost.kt delete mode 100644 components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptServiceTest.kt (limited to 'components') diff --git a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptConfiguration.kt b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptConfiguration.kt deleted file mode 100644 index f7bfb857c..000000000 --- a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptConfiguration.kt +++ /dev/null @@ -1,86 +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.core.script - -import org.jetbrains.kotlin.script.util.LocalFilesResolver -import java.io.File -import kotlin.script.dependencies.ScriptContents -import kotlin.script.dependencies.ScriptDependenciesResolver -import kotlin.script.experimental.annotations.KotlinScript -import kotlin.script.experimental.api.* -import kotlin.script.experimental.jvm.JvmDependency -import kotlin.script.experimental.jvm.dependenciesFromCurrentContext -import kotlin.script.experimental.jvm.jvm - - -@KotlinScript(fileExtension = "kts", - compilationConfiguration = ComponentScriptConfiguration::class) -abstract class ComponentScript { - -} - -object ComponentScriptConfiguration : ScriptCompilationConfiguration( - { - // defaultImports(DependsOn::class, Repository::class) - jvm { - dependenciesFromCurrentContext( - wholeClasspath = true - ) - } -// refineConfiguration { -// onAnnotations(DependsOn::class, Repository::class, handler = ::configureLocalFileDepsOnAnnotations) -// } - } -) - - -private val resolver = LocalFilesResolver() - -fun configureLocalFileDepsOnAnnotations(context: ScriptConfigurationRefinementContext): - ResultWithDiagnostics { - - val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() } - ?: return context.compilationConfiguration.asSuccess() - - val scriptContents = object : ScriptContents { - override val annotations: Iterable = annotations - override val file: File? = null - override val text: CharSequence? = null - } - - val diagnostics = arrayListOf() - - fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) { - //TODO - } - - return try { - val newDepsFromResolver = resolver.resolve(scriptContents, emptyMap(), ::report, null).get() - ?: return context.compilationConfiguration.asSuccess(diagnostics) - - val resolvedClasspath = newDepsFromResolver.classpath.toList().takeIf { it.isNotEmpty() } - ?: return context.compilationConfiguration.asSuccess(diagnostics) - - ScriptCompilationConfiguration(context.compilationConfiguration) { - dependencies.append(JvmDependency(resolvedClasspath)) - - }.asSuccess(diagnostics) - - } catch (e: Throwable) { - ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics()) - } -} \ No newline at end of file diff --git a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptService.kt b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptService.kt deleted file mode 100644 index 8ae091511..000000000 --- a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptService.kt +++ /dev/null @@ -1,78 +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.core.script - -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException -import java.io.File -import java.io.InputStream -import java.io.Reader -import javax.script.ScriptEngineManager -import kotlin.script.experimental.api.ResultValue -import kotlin.script.experimental.api.ResultWithDiagnostics -import kotlin.script.experimental.api.resultOrNull -import kotlin.script.experimental.host.toScriptSource -import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate - - -open class BluePrintScriptService(classLoader: ClassLoader? = Thread.currentThread().contextClassLoader) { - - /** - * Get the Script Class instance - */ - inline fun scriptClassNewInstance(scriptFile: File, scriptClassName: String): T { - - val compilationConfiguration = createJvmCompilationConfigurationFromTemplate() - - val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName) - - val evalResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptFile.toScriptSource(), compilationConfiguration, - null) - - when (evalResponse) { - is ResultWithDiagnostics.Success -> { - val returnValue = evalResponse.resultOrNull()?.returnValue as ResultValue.Value - return returnValue.value.castOrError() - } - is ResultWithDiagnostics.Failure -> { - throw BluePrintProcessorException(evalResponse.reports.joinToString("\n")) - } - else -> { - throw BluePrintProcessorException("Failed to process script ${scriptFile.absolutePath}") - } - } - - } - - val engine = ScriptEngineManager(classLoader).getEngineByExtension("kts") - - inline fun safeEval(evaluation: () -> R?) = try { - evaluation() - } catch (e: Exception) { - throw BluePrintProcessorException("Cannot load script", e) - } - - inline fun Any?.castOrError() = takeIf { it is T }?.let { it as T } - ?: throw IllegalArgumentException("Cannot cast $this to expected type ${T::class}") - - inline fun load(script: String): T = safeEval { engine.eval(script) }.castOrError() - - inline fun load(reader: Reader): T = safeEval { engine.eval(reader) }.castOrError() - - inline fun load(inputStream: InputStream): T = load(inputStream.reader()) - - inline fun loadAll(vararg inputStream: InputStream): List = inputStream.map(::load) -} diff --git a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BlueprintScriptingHost.kt b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BlueprintScriptingHost.kt deleted file mode 100644 index bda20a444..000000000 --- a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BlueprintScriptingHost.kt +++ /dev/null @@ -1,93 +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.core.script - -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException -import org.slf4j.LoggerFactory -import kotlin.script.experimental.api.* -import kotlin.script.experimental.host.BasicScriptingHost -import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration -import kotlin.script.experimental.jvmhost.JvmScriptCompiler - -val defaultBlueprintScriptCompiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration) - -open class BlueprintScriptingHost(evaluator: ScriptEvaluator -) : BasicScriptingHost(defaultBlueprintScriptCompiler, 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 { - - val log = LoggerFactory.getLogger(BluePrintScriptEvaluator::class.java)!! - - override suspend operator fun invoke( - compiledScript: CompiledScript<*>, - scriptEvaluationConfiguration: ScriptEvaluationConfiguration? - ): ResultWithDiagnostics = - try { - 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 completeScriptClass = "Script\$$scriptClassName" - log.info("Searching for class type($completeScriptClass)") - /** - * Search for Class Name - */ - val instanceClass = scriptClass.java.classes - .single { it.name == completeScriptClass } - //.single { it.name == "Script\$SampleBlueprintsFunctionNode" } - - - val instance = instanceClass.newInstance() - ?: throw BluePrintProcessorException("failed to create instance from the script") - - ResultWithDiagnostics.Success(EvaluationResult(ResultValue.Value(completeScriptClass, - instance, instance.javaClass.typeName), - scriptEvaluationConfiguration)) - } - } - } catch (e: Throwable) { - ResultWithDiagnostics.Failure(e.asDiagnostics("Error evaluating script")) - } -} \ No newline at end of file diff --git a/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptServiceTest.kt b/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptServiceTest.kt deleted file mode 100644 index 5c5ad3ba1..000000000 --- a/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/script/BluePrintScriptServiceTest.kt +++ /dev/null @@ -1,49 +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.core.script - -import org.junit.Ignore -import org.junit.Test -import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode -import java.io.File -import kotlin.test.assertEquals -import kotlin.test.assertNotNull - -class BluePrintScriptServiceTest { - - @Test - fun `invoke script`() { - val scriptContent = "11 + 11" - val value = BluePrintScriptService() - .load(scriptContent) - assertEquals(22, value, "failed to execute command") - } - - @Test - @Ignore - fun `invoke script component node`() { - - //println(classpathFromClasspathProperty()?.joinToString("\n")) - - val scriptFile = File("src/test/resources/scripts/SampleBlueprintFunctionNode.kts") - - val functionNode = BluePrintScriptService() - .scriptClassNewInstance>(scriptFile, - "SampleBlueprintFunctionNode") - assertNotNull(functionNode, "failed to get instance from script") - } -} \ No newline at end of file diff --git a/components/parent/pom.xml b/components/parent/pom.xml index b53f9f118..6d5c2c140 100644 --- a/components/parent/pom.xml +++ b/components/parent/pom.xml @@ -29,11 +29,11 @@ Components Parent pom - 2.1.1.RELEASE - 5.1.3.RELEASE - 1.3.11 - 1.3.11 - 1.1.0 + 2.1.2.RELEASE + 5.1.4.RELEASE + 1.3.20 + 1.3.20 + 1.1.1 1.18.0 3.6.1 1.0.0 @@ -307,22 +307,6 @@ com.fasterxml.jackson.module jackson-module-kotlin - - org.jetbrains.kotlin - kotlin-compiler-embeddable - - - org.jetbrains.kotlin - kotlin-scripting-jvm-host - - - - - org.jetbrains.kotlin - kotlin-compiler - - - io.grpc -- cgit 1.2.3-korg