diff options
author | Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com> | 2019-02-18 15:10:25 -0500 |
---|---|---|
committer | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-02-20 21:02:05 +0000 |
commit | 0613ae8f927834f7621011fda3f0cb1b7f8e07ad (patch) | |
tree | 79010a222bfb62ad2cb61d88cccfea8f76dfc186 /ms/blueprintsprocessor/functions/python-executor | |
parent | 183e2b48ceb4ae58b85f76f9bf182a5958fc92e6 (diff) |
Add component function scripting service
Change-Id: I7c5b49617823dd623566fb4be4d431012420e17c
Issue-ID: CCSDK-959
Signed-off-by: Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
Diffstat (limited to 'ms/blueprintsprocessor/functions/python-executor')
7 files changed, 7 insertions, 318 deletions
diff --git a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/BlueprintJythonService.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/BlueprintJythonService.kt deleted file mode 100644 index fcaa57b30..000000000 --- a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/BlueprintJythonService.kt +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright © 2019 IBM, Bell Canada. - * - * 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.python.executor - -import com.fasterxml.jackson.databind.JsonNode -import com.fasterxml.jackson.databind.node.ArrayNode -import org.apache.commons.io.FilenameUtils -import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.plugin.BlueprintPythonHost -import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException -import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow -import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment -import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext -import org.slf4j.Logger -import org.slf4j.LoggerFactory -import org.springframework.context.ApplicationContext -import org.springframework.stereotype.Service - -@Service -class BlueprintJythonService(val pythonExecutorProperty: PythonExecutorProperty, - private val applicationContext: ApplicationContext) { - - val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java) - - inline fun <reified T> jythonInstance(blueprintContext: BluePrintContext, pythonClassName: String, content: String, - dependencyInstanceNames: MutableMap<String, Any>?): T { - - val blueprintBasePath: String = blueprintContext.rootPath - val pythonPath: MutableList<String> = arrayListOf() - pythonPath.add(blueprintBasePath) - pythonPath.addAll(pythonExecutorProperty.modulePaths) - - val blueprintPythonConfigurations = BluePrintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf()) - - val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations) - val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames) - - log.info("Component Object {}", pyObject) - - return pyObject.__tojava__(T::class.java) as T - } - - fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction { - - val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService - val bluePrintContext = bluePrintRuntimeService.bluePrintContext() - val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName - val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs - - val operationAssignment: OperationAssignment = bluePrintContext - .nodeTemplateInterfaceOperation(abstractComponentFunction.nodeTemplateName, - abstractComponentFunction.interfaceName, abstractComponentFunction.operationName) - - val blueprintBasePath: String = bluePrintContext.rootPath - - val artifactName: String = operationAssignment.implementation?.primary - ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)") - - val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName) - - val pythonFileName = artifactDefinition.file - ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)") - - val pythonClassName = FilenameUtils.getBaseName(pythonFileName) - log.info("Getting Jython Script Class($pythonClassName)") - - val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName) - - checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty") - - val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf() - pythonPath.add(blueprintBasePath) - pythonPath.addAll(pythonExecutorProperty.modulePaths) - - val jythonInstances: MutableMap<String, Any> = hashMapOf() - jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName) - - val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode - ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})") - - instanceDependenciesNode.forEach { instanceName -> - jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue()) - } - - val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(bluePrintContext, pythonClassName, - content!!, jythonInstances) - - return scriptComponentFunction - - } - -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt index a74a77901..b7f77719e 100644 --- a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt +++ b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt @@ -22,6 +22,8 @@ import com.fasterxml.jackson.databind.node.ArrayNode import org.apache.commons.io.FilenameUtils import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.BlueprintJythonService +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.PythonExecutorConstants import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment diff --git a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt deleted file mode 100644 index 6d17f9320..000000000 --- a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * - * Modifications Copyright © 2019 IBM, Bell Canada. - * - * 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.python.executor - -import org.springframework.beans.factory.annotation.Value -import org.springframework.boot.context.properties.EnableConfigurationProperties -import org.springframework.context.annotation.ComponentScan -import org.springframework.context.annotation.Configuration -import java.io.File -import java.util.* - -@Configuration -@ComponentScan -@EnableConfigurationProperties -open class PythonExecutorConfiguration - -@Configuration -open class PythonExecutorProperty { - @Value("\${blueprints.processor.functions.python.executor.executionPath}") - lateinit var executionPath: String - @Value("#{'\${blueprints.processor.functions.python.executor.modulePaths}'.split(',')}") - lateinit var modulePaths: List<String> -} - -class PythonExecutorConstants { - companion object { - const val INPUT_INSTANCE_DEPENDENCIES = "instance-dependencies" - } -} - -open class BluePrintPython(executablePath: String, blueprintPythonPlatform: MutableList<String>, - val argv: MutableList<String>){ - lateinit var moduleName: String - lateinit var pythonClassName: String - lateinit var content: String - var props: Properties = Properties() - - init { - // Build up the python.path - val sb = StringBuilder() - sb.append(System.getProperty("java.class.path")) - - for (p in blueprintPythonPlatform) { - sb.append(File.pathSeparator).append(p) - } - - props["python.import.site"] = "true" - props.setProperty("python.path", sb.toString()) - props.setProperty("python.verbose", "error") - props.setProperty("python.executable", executablePath) - } -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/plugin/BlueprintPythonHost.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/plugin/BlueprintPythonHost.kt deleted file mode 100644 index 7278ced51..000000000 --- a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/plugin/BlueprintPythonHost.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright © 2019 IBM, Bell Canada. - * - * 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.python.executor.plugin - -import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.BluePrintPython -import org.python.core.PyObject -import org.python.util.PythonInterpreter - -open class BlueprintPythonHost(private val bluePrintPython: BluePrintPython){ - private val blueprintPythonInterpreterProxy: BlueprintPythonInterpreterProxy - - init { - PythonInterpreter.initialize(System.getProperties(), bluePrintPython.props, bluePrintPython.argv.toTypedArray()) - blueprintPythonInterpreterProxy = BlueprintPythonInterpreterProxy(bluePrintPython) - } - - /** - * getPythonComponent Purpose: execute the python script and return the python interpreter object - * - * @param content String - * @param interfaceName String - * @param properties MutableMap<String, Any> - * @return pyObject PyObject - */ - fun getPythonComponent(content: String?, interfaceName: String, properties: MutableMap<String, Any>?): PyObject { - bluePrintPython.content = content!! - bluePrintPython.pythonClassName = interfaceName - bluePrintPython.moduleName = "Blueprint Python Script [Class Name = $interfaceName]" - - return blueprintPythonInterpreterProxy.getPythonInstance(properties) - } - - //TODO Check potential errors in python scripts -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/plugin/BlueprintPythonInterpreterProxy.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/plugin/BlueprintPythonInterpreterProxy.kt deleted file mode 100644 index d3b433af9..000000000 --- a/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/plugin/BlueprintPythonInterpreterProxy.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright © 2019 IBM, Bell Canada. - * - * 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.python.executor.plugin - -import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.BluePrintPython -import org.python.core.PyObject -import org.python.util.PythonInterpreter - -open class BlueprintPythonInterpreterProxy(private val bluePrintPython: BluePrintPython): PythonInterpreter(){ - - fun getPythonInstance(properties: MutableMap<String, Any>?): PyObject{ - properties?.forEach { (name, value) -> - this.set(name, value) - } - - this.exec("import sys") - - bluePrintPython.content.let { - this.exec(bluePrintPython.content) - } - - val initCommand = bluePrintPython.pythonClassName.plus(" = ").plus( - bluePrintPython.pythonClassName).plus("()") - this.exec(initCommand) - - return this.get(bluePrintPython.pythonClassName) - } -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/BlueprintJythonServiceTest.kt b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/BlueprintJythonServiceTest.kt deleted file mode 100644 index f6103a4da..000000000 --- a/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/BlueprintJythonServiceTest.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright © 2019 IBM, Bell Canada. - * - * 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.python.executor - -import org.junit.Test -import org.junit.runner.RunWith -import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput -import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils -import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.test.context.ContextConfiguration -import org.springframework.test.context.TestPropertySource -import org.springframework.test.context.junit4.SpringRunner -import kotlin.test.assertNotNull - -@RunWith(SpringRunner::class) -@ContextConfiguration(classes = [BlueprintJythonService::class, PythonExecutorProperty::class]) -@TestPropertySource(properties = -["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints", - "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints"]) -class BlueprintJythonServiceTest { - - @Autowired - private lateinit var blueprintJythonService: BlueprintJythonService - - @Test - fun testGetAbstractPythonPlugin() { - val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext( - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") - - val dependencies: MutableMap<String, Any> = hashMapOf() - - val content = JacksonUtils.getContent("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration/Scripts/python/SamplePythonComponentNode.py") - - val abstractComponentFunction = blueprintJythonService.jythonInstance<AbstractComponentFunction>(bluePrintContext, "SamplePythonComponentNode", content, dependencies) - - assertNotNull(abstractComponentFunction, "failed to get python component") - - abstractComponentFunction.process(ExecutionServiceInput()) - - } -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutorTest.kt b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutorTest.kt index 67a3d9558..dd8eb5033 100644 --- a/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutorTest.kt @@ -20,6 +20,9 @@ import com.fasterxml.jackson.databind.JsonNode import org.junit.Test import org.junit.runner.RunWith import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.mock.MockInstanceConfiguration +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.PythonExecutorConfiguration +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.PythonExecutorProperty import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.asJsonNode import org.onap.ccsdk.apps.controllerblueprints.core.putJsonElement @@ -31,7 +34,8 @@ import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.SpringRunner @RunWith(SpringRunner::class) -@ContextConfiguration(classes = [PythonExecutorConfiguration::class, PythonExecutorProperty::class]) +@ContextConfiguration(classes = [PythonExecutorConfiguration::class, PythonExecutorProperty::class, + ComponentJythonExecutor::class, MockInstanceConfiguration::class]) @TestPropertySource(properties = ["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints", "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints"]) |