diff options
author | Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-02-11 23:35:16 -0500 |
---|---|---|
committer | Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-02-12 08:07:00 -0500 |
commit | dba22de28e496ae2d6d2fde9384fd1187e4b8fc1 (patch) | |
tree | 3c9edcec4e2f6fd013ad6514c7678979734b44f0 /ms/blueprintsprocessor/functions/netconf-executor/src | |
parent | d4b9a331a88feea62872c4ae85f4ae1b738c6f60 (diff) |
Jython execution component and service
Change-Id: I2610e73a9c7ba073b5fa9d148dcd6fb5b9ad9ae3
Issue-ID: CCSDK-696
Signed-off-by: Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com>
Diffstat (limited to 'ms/blueprintsprocessor/functions/netconf-executor/src')
6 files changed, 12 insertions, 281 deletions
diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt index 4612ddaf..c007914a 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt @@ -1,6 +1,8 @@ /* * 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 @@ -17,19 +19,20 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.BlueprintPythonService import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.ComponentJythonExecutor -import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.PythonExecutorProperty import org.slf4j.LoggerFactory import org.springframework.beans.factory.config.ConfigurableBeanFactory +import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Scope import org.springframework.stereotype.Component @Component("component-netconf-executor") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -open class ComponentNetconfExecutor(private val netconfExecutorConfiguration: NetconfExecutorConfiguration, - private val pythonExecutorProperty: PythonExecutorProperty) - : ComponentJythonExecutor(pythonExecutorProperty) { +open class ComponentNetconfExecutor(private var applicationContext: ApplicationContext, private val netconfExecutorConfiguration: NetconfExecutorConfiguration, + private val blueprintPythonService: BlueprintPythonService) + : ComponentJythonExecutor(applicationContext, blueprintPythonService) { private val log = LoggerFactory.getLogger(ComponentNetconfExecutor::class.java) lateinit var deviceInfo: DeviceInfo diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt deleted file mode 100644 index 06c1c752..00000000 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt +++ /dev/null @@ -1,104 +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.blueprintsprocessor.functions.python.executor - -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.functions.python.executor.utils.PythonExecutorUtils -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.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.beans.factory.config.ConfigurableBeanFactory -import org.springframework.context.ApplicationContext -import org.springframework.context.annotation.Scope -import org.springframework.stereotype.Component - -@Component("component-jython-executor") -@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -open class ComponentJythonExecutor(private val pythonExecutorProperty: PythonExecutorProperty) : AbstractComponentFunction() { - - private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java) - - private var componentFunction: AbstractComponentFunction? = null - - @Autowired - lateinit var applicationContext: ApplicationContext - - fun populateJythonComponentInstance(executionServiceInput: ExecutionServiceInput) { - val bluePrintContext = bluePrintRuntimeService.bluePrintContext() - - val operationAssignment: OperationAssignment = bluePrintContext - .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, 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) - - 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) - jythonInstances["bluePrintRuntimeService"] = bluePrintRuntimeService - - 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()) - } - - componentFunction = PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath, - pythonPath, content, pythonClassName, jythonInstances) - } - - - override fun process(executionServiceInput: ExecutionServiceInput) { - - log.info("Processing : $operationInputs") - checkNotNull(bluePrintRuntimeService) { "failed to get bluePrintRuntimeService" } - - // Populate Component Instance - populateJythonComponentInstance(executionServiceInput) - - // Invoke Jython Component Script - 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/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/JythonExecutionService.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/JythonExecutionService.kt deleted file mode 100644 index 6618d13c..00000000 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/JythonExecutionService.kt +++ /dev/null @@ -1,52 +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.blueprintsprocessor.functions.python.executor - -import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils.PythonExecutorUtils -import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.context.ApplicationContext -import org.springframework.stereotype.Service - -@Service -class JythonExecutionService(private val pythonExecutorProperty: PythonExecutorProperty) { - - - private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java) - - @Autowired - lateinit var applicationContext: ApplicationContext - - - fun getJythonComponentFunction(pythonClassName: String, content: String, pythonPath: MutableList<String>, - jythonContextInstance: MutableMap<String, Any>, - dependencyInstanceNames: List<String>): AbstractComponentFunction { - - pythonPath.addAll(pythonExecutorProperty.modulePaths) - - dependencyInstanceNames.forEach { instanceName -> - jythonContextInstance[instanceName] = applicationContext.getBean(instanceName) - - } - - return PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath, - pythonPath, content, pythonClassName, jythonContextInstance) - - } - -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt deleted file mode 100644 index be7374c5..00000000 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt +++ /dev/null @@ -1,42 +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.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<String> - -} - -class PythonExecutorConstants { - companion object { - const val INPUT_INSTANCE_DEPENDENCIES = "instance-dependencies" - } -}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt deleted file mode 100644 index 341ede58..00000000 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.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.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.io.File -import java.util.* - -class PythonExecutorUtils { - companion object { - - private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java) - - fun getPythonComponent(executePath: String, pythonPath: MutableList<String>, content: String?, interfaceName: String, - properties: MutableMap<String, Any>): 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<String>, argv: MutableList<String>) { - - val props = Properties() - // Build up the python.path - val sb = StringBuilder() - sb.append(System.getProperty("java.class.path")) - - for (p in pythonPath) { - sb.append(File.pathSeparator).append(p) - } - log.debug("Python 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/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt index 5f58dd38..c33bfcfa 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt @@ -1,6 +1,8 @@ /* * 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 @@ -20,6 +22,7 @@ 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.BlueprintPythonService import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.PythonExecutorProperty import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.asJsonNode @@ -32,7 +35,8 @@ import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.SpringRunner @RunWith(SpringRunner::class) -@ContextConfiguration(classes = [NetconfExecutorConfiguration::class, PythonExecutorProperty::class]) +@ContextConfiguration(classes = [NetconfExecutorConfiguration::class, BlueprintPythonService::class, + PythonExecutorProperty::class]) @TestPropertySource(properties = ["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_netconf;./../../../../components/scripts/python/ccsdk_blueprints", "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_netconf"]) |