From d2ec19b59ee0bdc1ba3a22baff5d0d30bd8a9e19 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Mon, 19 Nov 2018 12:48:31 -0500 Subject: Implement Base Jython Executor function. Change-Id: I3fb066a021de4a7b3aa1fce7f6c191bc3944fb51 Issue-ID: CCSDK-696 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../functions/python-executor/pom.xml | 41 +++++++++++ .../python/executor/ComponentPythonExecutor.kt | 82 ++++++++++++++++++++++ .../python/executor/PythonExecutorConfiguration.kt | 36 ++++++++++ .../python/executor/utils/PythonExecutorUtils.kt | 77 ++++++++++++++++++++ .../python/executor/ComponentPythonExecutorTest.kt | 76 ++++++++++++++++++++ .../executor/utils/PythonExecutorUtilsTest.kt | 52 ++++++++++++++ .../src/test/resources/logback-test.xml | 35 +++++++++ 7 files changed, 399 insertions(+) create mode 100644 ms/blueprintsprocessor/functions/python-executor/pom.xml create mode 100644 ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentPythonExecutor.kt create mode 100644 ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt create mode 100644 ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt create mode 100644 ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentPythonExecutorTest.kt create mode 100644 ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtilsTest.kt create mode 100644 ms/blueprintsprocessor/functions/python-executor/src/test/resources/logback-test.xml (limited to 'ms/blueprintsprocessor/functions/python-executor') diff --git a/ms/blueprintsprocessor/functions/python-executor/pom.xml b/ms/blueprintsprocessor/functions/python-executor/pom.xml new file mode 100644 index 000000000..4f9cc7458 --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/pom.xml @@ -0,0 +1,41 @@ + + + + + + functions + org.onap.ccsdk.apps.blueprintsprocessor + 0.4.0-SNAPSHOT + + 4.0.0 + + org.onap.ccsdk.apps.blueprintsprocessor.functions + python-executor + Blueprints Processor Function - Python Executor + Blueprints Processor Function - Python Executor + + + + org.python + jython-standalone + + + + + \ 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/ComponentPythonExecutor.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentPythonExecutor.kt new file mode 100644 index 000000000..823b13f27 --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentPythonExecutor.kt @@ -0,0 +1,82 @@ +/* + * 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.python.executor + +import org.apache.commons.io.FilenameUtils +import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils.PythonExecutorUtils +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyNThrow +import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component + +@Component("component-python-executor") +class ComponentPythonExecutor(private val pythonExecutorProperty: PythonExecutorProperty) : AbstractComponentFunction() { + + private val log = LoggerFactory.getLogger(ComponentPythonExecutor::class.java) + + private var componentFunction: AbstractComponentFunction? = null + + + override fun process(executionServiceInput: ExecutionServiceInput) { + + log.info("Processing : ${executionServiceInput.metadata}") + checkNotNull(bluePrintRuntimeService) { "failed to get bluePrintRuntimeService" } + + val bluePrintContext = bluePrintRuntimeService!!.bluePrintContext() + + val operationAssignment: OperationAssignment = bluePrintContext + .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName) + + val blueprintBasePath: String = bluePrintRuntimeService!!.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)?.asText() + ?: throw BluePrintProcessorException("python execute path is missing for node template ($nodeTemplateName)") + + 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) + + val content: String? = bluePrintRuntimeService!!.resolveNodeTemplateArtifact(nodeTemplateName, artifactName) + + checkNotEmptyNThrow(content, "artifact ($artifactName) content is empty") + + val pythonPath: MutableList = operationAssignment.implementation?.dependencies ?: arrayListOf() + pythonPath.add(blueprintBasePath) + pythonPath.addAll(pythonExecutorProperty.modulePaths) + + val properties: MutableMap = hashMapOf() + properties["log"] = log + + componentFunction = PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath, pythonPath, content, pythonClassName, properties) + + componentFunction!!.process(executionServiceInput) + + } + + override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + componentFunction!!.recover(runtimeException, executionRequest) + } + +} \ 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/PythonExecutorConfiguration.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt new file mode 100644 index 000000000..dd80fb0ae --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt @@ -0,0 +1,36 @@ +/* + * 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.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 + +@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 + +} \ 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/utils/PythonExecutorUtils.kt b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt new file mode 100644 index 000000000..66c919d4d --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt @@ -0,0 +1,77 @@ +/* + * 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.python.executor.utils + +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.python.core.PyObject +import org.python.util.PythonInterpreter +import org.slf4j.LoggerFactory +import java.util.* + +class PythonExecutorUtils { + companion object { + + private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java) + + fun getPythonComponent(executePath: String, pythonPath: MutableList, content: String?, interfaceName: String, + properties: MutableMap): AbstractComponentFunction { + + initPython(executePath, pythonPath, arrayListOf()) + val pythonInterpreter = PythonInterpreter() + + properties.forEach { (name, value) -> + pythonInterpreter.set(name, value) + } + + pythonInterpreter.exec("import sys") + + content?.let { + pythonInterpreter.exec(content) + } + + val initCommand = interfaceName.plus(" = ").plus(interfaceName).plus("()") + pythonInterpreter.exec(initCommand) + val pyObject: PyObject = pythonInterpreter.get(interfaceName) + + log.info("Component Object {}", pyObject) + + return pyObject.__tojava__(AbstractComponentFunction::class.java) as AbstractComponentFunction + } + + private fun initPython(executablePath: String, + pythonPath: MutableList, argv: MutableList) { + + val props = Properties() + // Build up the python.path + val sb = StringBuilder() + sb.append(System.getProperty("java.class.path")) + + for (p in pythonPath) { + sb.append(":").append(p) + } + log.debug("Paths : $sb") + + props["python.import.site"] = "true" + props.setProperty("python.path", sb.toString()) + props.setProperty("python.verbose", "error") + props.setProperty("python.executable", executablePath) + + PythonInterpreter.initialize(System.getProperties(), props, argv.toTypedArray()) + } + + } +} \ 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/ComponentPythonExecutorTest.kt b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentPythonExecutorTest.kt new file mode 100644 index 000000000..e16f2f030 --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentPythonExecutorTest.kt @@ -0,0 +1,76 @@ +/* + * 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.python.executor + +import com.fasterxml.jackson.databind.JsonNode +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers +import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader +import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.controllerblueprints.core.putJsonElement +import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils +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 + +@RunWith(SpringRunner::class) +@ContextConfiguration(classes = [PythonExecutorConfiguration::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 ComponentPythonExecutorTest { + + @Autowired + lateinit var componentPythonExecutor: ComponentPythonExecutor + + + @Test + fun testPythonComponentInjection() { + + val executionServiceInput = ExecutionServiceInput() + val commonHeader = CommonHeader() + commonHeader.requestId = "1234" + executionServiceInput.commonHeader = commonHeader + + val actionIdentifiers = ActionIdentifiers() + actionIdentifiers.blueprintName = "baseconfiguration" + actionIdentifiers.blueprintVersion = "1.0.0" + actionIdentifiers.actionName = "activate" + executionServiceInput.actionIdentifiers = actionIdentifiers + + + val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(commonHeader.requestId, + "./../../../../components/model-catalog/blueprint-model/starter-blueprint/baseconfiguration") + + componentPythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService + + + val metaData: MutableMap = hashMapOf() + metaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_STEP, "resource-assignment-py") + metaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "resource-assignment-py") + metaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "DefaultComponentNode") + metaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + executionServiceInput.metadata = metaData + + componentPythonExecutor.apply(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/utils/PythonExecutorUtilsTest.kt b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtilsTest.kt new file mode 100644 index 000000000..6197c4b28 --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtilsTest.kt @@ -0,0 +1,52 @@ +/* + * 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.python.executor.utils + +import org.junit.Test +import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils +import org.slf4j.LoggerFactory +import kotlin.test.assertNotNull + + +class PythonExecutorUtilsTest { + + private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java) + + @Test + fun testGetPythonComponent() { + + val pythonPath: MutableList = arrayListOf() + pythonPath.add("./../../../../components/scripts/python/ccsdk_blueprints") + + val properties: MutableMap = hashMapOf() + properties["logger"] = log + + val content = JacksonUtils.getContent("./../../../../components/scripts/python/ccsdk_blueprints/sample_blueprint_component.py") + + val abstractComponentFunction = PythonExecutorUtils.getPythonComponent("/home/brindasanth/onap/apps/components/scripts/python/ccsdk_blueprints", pythonPath, content, + "SampleBlueprintComponent", properties) + + assertNotNull(abstractComponentFunction, "failed to get python component") + + abstractComponentFunction.process(ExecutionServiceInput()) + + } + + +} + diff --git a/ms/blueprintsprocessor/functions/python-executor/src/test/resources/logback-test.xml b/ms/blueprintsprocessor/functions/python-executor/src/test/resources/logback-test.xml new file mode 100644 index 000000000..a816a06c5 --- /dev/null +++ b/ms/blueprintsprocessor/functions/python-executor/src/test/resources/logback-test.xml @@ -0,0 +1,35 @@ + + + + + + + %d{HH:mm:ss.SSS} %-5level %logger{100} - %msg%n + + + + + + + + + + + + + -- cgit 1.2.3-korg