From 40072d3dcc1d0193bba1ea9432c13ac24857be55 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Wed, 27 Mar 2019 13:22:51 -0400 Subject: Improve function interfaces Change-Id: I24f45d39ac05491a4217101e00bcbf8d122e4e1a Issue-ID: CCSDK-1137 Signed-off-by: Muthuramalingam, Brinda Santh --- .../execution/AbstractComponentFunction.kt | 14 ++-- .../execution/AbstractScriptComponentFunction.kt | 76 ++++++++++++++++++++++ .../execution/ComponentFunctionScriptingService.kt | 6 +- .../scripts/BlueprintJythonServiceTest.kt | 17 +++-- .../src/test/resources/logback-test.xml | 34 ++++++++++ .../resources/scripts/SamplePythonComponentNode.py | 14 ++++ .../workflow/NodeTemplateExecutionService.kt | 6 +- .../workflow/mock/MockComponentFunction.kt | 13 ++-- 8 files changed, 154 insertions(+), 26 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/logback-test.xml create mode 100644 ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/scripts/SamplePythonComponentNode.py (limited to 'ms/blueprintsprocessor/modules/services') diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt index be4327bfe..e78e87523 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt @@ -54,7 +54,7 @@ abstract class AbstractComponentFunction : BlueprintFunctionNode { + executeScriptBlocking(executionServiceInput) + } + else -> { + executeScriptNB(executionServiceInput) + } + } + } + + private suspend fun executeScriptNB(executionServiceInput: ExecutionServiceInput) { + try { + processNB(executionServiceInput) + } catch (runtimeException: RuntimeException) { + log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException) + recoverNB(runtimeException, executionServiceInput) + } + } + + private fun executeScriptBlocking(executionServiceInput: ExecutionServiceInput) { + try { + process(executionServiceInput) + } catch (runtimeException: RuntimeException) { + log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException) + recover(runtimeException, executionServiceInput) + } + } + + /** + * If Jython Script, Override Blocking methods(process() and recover()) + * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default + * blocking + * methods will have default implementation, + * + * Always applyNB() method will be invoked, apply() won't be called from parent + */ + + final override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput { + throw BluePrintException("Not Implemented, use applyNB method") + } + + final override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput { + throw BluePrintException("Not Implemented required") + } + + final override fun prepareResponse(): ExecutionServiceOutput { + throw BluePrintException("Not Implemented required") + } + + final override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput { + throw BluePrintException("Not Implemented required") + } + + final override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput { + throw BluePrintException("Not Implemented required") + } + + final override suspend fun prepareResponseNB(): ExecutionServiceOutput { + throw BluePrintException("Not Implemented required") + } + override fun process(executionRequest: ExecutionServiceInput) { + throw BluePrintException("Not Implemented, child class will implement this") + } + + override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + throw BluePrintException("Not Implemented, child class will implement this") + } } \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentFunctionScriptingService.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentFunctionScriptingService.kt index 907aae4cd..b2991be5e 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentFunctionScriptingService.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentFunctionScriptingService.kt @@ -33,7 +33,8 @@ class ComponentFunctionScriptingService(private val applicationContext: Applicat private val log = LoggerFactory.getLogger(ComponentFunctionScriptingService::class.java) - fun scriptInstance(componentFunction: AbstractComponentFunction, scriptType: String, + suspend fun scriptInstance(componentFunction: AbstractComponentFunction, + scriptType: String, scriptClassReference: String, instanceDependencies: List): T { @@ -53,6 +54,7 @@ class ComponentFunctionScriptingService(private val applicationContext: Applicat scriptComponent.operationName = componentFunction.operationName scriptComponent.nodeTemplateName = componentFunction.nodeTemplateName scriptComponent.operationInputs = componentFunction.operationInputs + scriptComponent.scriptType = scriptType // Populate Instance Properties instanceDependencies.forEach { instanceDependency -> @@ -63,7 +65,7 @@ class ComponentFunctionScriptingService(private val applicationContext: Applicat } - fun > scriptInstance(bluePrintContext: BluePrintContext, scriptType: String, + suspend fun > scriptInstance(bluePrintContext: BluePrintContext, scriptType: String, scriptClassReference: String): T { var scriptComponent: T? = null diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/BlueprintJythonServiceTest.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/BlueprintJythonServiceTest.kt index d6c0cf58b..c8c9f0b6a 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/BlueprintJythonServiceTest.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/BlueprintJythonServiceTest.kt @@ -1,5 +1,6 @@ /* * Copyright © 2019 IBM, Bell Canada. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +16,14 @@ */ package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts +import io.mockk.every +import io.mockk.mockk import org.junit.Test import org.junit.runner.RunWith import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.ContextConfiguration @@ -39,15 +43,16 @@ class BlueprintJythonServiceTest { @Test fun testGetAbstractPythonPlugin() { - val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext( - "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") + val bluePrintContext = mockk() + every { bluePrintContext.rootPath } returns normalizedPathName("target") val dependencies: MutableMap = hashMapOf() - val content = JacksonUtils.getContent("./../../../../." + - "./components/model-catalog/blueprint-model/test-blueprint/baseconfiguration/Scripts/python/SamplePythonComponentNode.py") + val content = JacksonUtils.getClassPathFileContent("scripts/SamplePythonComponentNode.py") - val abstractComponentFunction = blueprintJythonService.jythonInstance(bluePrintContext, "SamplePythonComponentNode", content, dependencies) + val abstractComponentFunction = blueprintJythonService + .jythonInstance(bluePrintContext, "SamplePythonComponentNode", + content, dependencies) assertNotNull(abstractComponentFunction, "failed to get python component") diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/logback-test.xml b/ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/logback-test.xml new file mode 100644 index 000000000..da1553d2c --- /dev/null +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/logback-test.xml @@ -0,0 +1,34 @@ + + + + + + + %d{HH:mm:ss.SSS} %-5level %logger{100} - %msg%n + + + + + + + + + + + + diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/scripts/SamplePythonComponentNode.py b/ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/scripts/SamplePythonComponentNode.py new file mode 100644 index 000000000..f1b614a59 --- /dev/null +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/test/resources/scripts/SamplePythonComponentNode.py @@ -0,0 +1,14 @@ +from abstract_blueprint_function import AbstractPythonComponentFunction +from blueprint_constants import * + + +class SamplePythonComponentNode(AbstractPythonComponentFunction): + + def process(self, execution_request): + print "Processing calling..." + PROPERTY_BLUEPRINT_BASE_PATH + return None + + def recover(self, runtime_exception, execution_request): + print "Recovering calling..." + PROPERTY_BLUEPRINT_BASE_PATH + return None + diff --git a/ms/blueprintsprocessor/modules/services/workflow-service/src/main/kotlin/org/onap/cds/blueprintsprocessor/services/workflow/NodeTemplateExecutionService.kt b/ms/blueprintsprocessor/modules/services/workflow-service/src/main/kotlin/org/onap/cds/blueprintsprocessor/services/workflow/NodeTemplateExecutionService.kt index 67be26e6d..8f47a0460 100644 --- a/ms/blueprintsprocessor/modules/services/workflow-service/src/main/kotlin/org/onap/cds/blueprintsprocessor/services/workflow/NodeTemplateExecutionService.kt +++ b/ms/blueprintsprocessor/modules/services/workflow-service/src/main/kotlin/org/onap/cds/blueprintsprocessor/services/workflow/NodeTemplateExecutionService.kt @@ -17,8 +17,6 @@ package org.onap.ccsdk.cds.blueprintsprocessor.services.workflow import com.fasterxml.jackson.databind.JsonNode -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction @@ -64,9 +62,7 @@ open class NodeTemplateExecutionService(private val applicationContext: Applicat plugin.bluePrintRuntimeService.put("$nodeTemplateName-step-inputs", stepInputs.asJsonNode()) // Get the Request from the Context and Set to the Function Input and Invoke the function - return withContext(Dispatchers.Default) { - plugin.apply(executionServiceInput) - } + return plugin.apply(executionServiceInput) } } \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/services/workflow-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/workflow/mock/MockComponentFunction.kt b/ms/blueprintsprocessor/modules/services/workflow-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/workflow/mock/MockComponentFunction.kt index 1cae01ffa..5dc5b9dba 100644 --- a/ms/blueprintsprocessor/modules/services/workflow-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/workflow/mock/MockComponentFunction.kt +++ b/ms/blueprintsprocessor/modules/services/workflow-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/workflow/mock/MockComponentFunction.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,14 +40,14 @@ class MockComponentFunction : AbstractComponentFunction() { private val log = LoggerFactory.getLogger(MockComponentFunction::class.java) - override fun process(executionRequest: ExecutionServiceInput) { + override suspend fun processNB(executionRequest: ExecutionServiceInput) { log.info("Processing component : $operationInputs") bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, "assignment-params", "params".asJsonPrimitive()) } - override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { log.info("Recovering component..") } } @@ -57,11 +58,11 @@ class SingletonComponentFunction : AbstractComponentFunction() { private val log = LoggerFactory.getLogger(MockComponentFunction::class.java) - override fun process(executionRequest: ExecutionServiceInput) { + override suspend fun processNB(executionRequest: ExecutionServiceInput) { log.info("Processing component : $operationInputs") } - override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { log.info("Recovering component..") } } @@ -72,11 +73,11 @@ class PrototypeComponentFunction : AbstractComponentFunction() { private val log = LoggerFactory.getLogger(MockComponentFunction::class.java) - override fun process(executionRequest: ExecutionServiceInput) { + override suspend fun processNB(executionRequest: ExecutionServiceInput) { log.info("Processing component : $operationInputs") } - override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { log.info("Recovering component..") } } \ No newline at end of file -- cgit 1.2.3-korg