From 6cd702b68bb4e0a479dcaf27b5d75a65982f6fe5 Mon Sep 17 00:00:00 2001 From: Steve Alphonse Siani Date: Mon, 11 Feb 2019 23:35:16 -0500 Subject: Jython execution component and service Change-Id: I2610e73a9c7ba073b5fa9d148dcd6fb5b9ad9ae3 Issue-ID: CCSDK-696 Signed-off-by: Steve Alphonse Siani --- .../functions/resource-resolution/pom.xml | 4 ++ .../resolution/ResourceSourceProperties.kt | 20 +++++-- .../CapabilityResourceAssignmentProcessor.kt | 62 +++++++++++++++++++++- .../processor/ResourceAssignmentProcessor.kt | 2 +- .../CapabilityResourceAssignmentProcessorTest.kt | 43 ++++++++++++++- .../src/test/resources/application-test.properties | 8 ++- .../capability/jython-resource-definitions.json | 18 +++++++ .../SampleResourceAssignmentProcessorScript.py | 13 +++++ 8 files changed, 161 insertions(+), 9 deletions(-) create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/python/SampleResourceAssignmentProcessorScript.py (limited to 'ms/blueprintsprocessor/functions/resource-resolution') diff --git a/ms/blueprintsprocessor/functions/resource-resolution/pom.xml b/ms/blueprintsprocessor/functions/resource-resolution/pom.xml index b29149d85..1e15ba274 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/pom.xml +++ b/ms/blueprintsprocessor/functions/resource-resolution/pom.xml @@ -34,6 +34,10 @@ db-resources ${project.version} --> + + org.onap.ccsdk.apps.blueprintsprocessor.functions + python-executor + org.onap.ccsdk.apps.controllerblueprints blueprint-scripts diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt index a44d366c3..0f1267cbb 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2018 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,43 +18,54 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution +import com.fasterxml.jackson.annotation.JsonProperty + open class ResourceSourceProperties open class InputResourceSource : ResourceSourceProperties() { lateinit var key: String + @get:JsonProperty("key-dependencies") lateinit var keyDependencies: MutableList } open class DefaultResourceSource : ResourceSourceProperties() { lateinit var key: String + @get:JsonProperty("key-dependencies") lateinit var keyDependencies: MutableList } open class DatabaseResourceSource : ResourceSourceProperties() { lateinit var type: String lateinit var query: String + @get:JsonProperty("input-key-mapping") var inputKeyMapping: MutableMap? = null + @get:JsonProperty("output-key-mapping") var outputKeyMapping: MutableMap? = null + @get:JsonProperty("key-dependencies") lateinit var keyDependencies: MutableList } open class RestResourceSource : ResourceSourceProperties() { lateinit var type: String + @get:JsonProperty("url-path") lateinit var urlPath: String lateinit var path: String + @get:JsonProperty("expression-type") lateinit var expressionType: String + @get:JsonProperty("input-key-mapping") var inputKeyMapping: MutableMap? = null + @get:JsonProperty("output-key-mapping") var outputKeyMapping: MutableMap? = null + @get:JsonProperty("key-dependencies") lateinit var keyDependencies: MutableList } open class CapabilityResourceSource : ResourceSourceProperties() { lateinit var type: String + @get:JsonProperty("instance-name") lateinit var instanceName: String + @get:JsonProperty("instance-dependencies") var instanceDependencies: List? = null - lateinit var path: String - lateinit var expressionType: String - var inputKeyMapping: MutableMap? = null - var outputKeyMapping: MutableMap? = null + @get:JsonProperty("key-dependencies") lateinit var keyDependencies: MutableList } \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt index 1370a4796..013039d66 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.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 @@ -16,17 +18,22 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor +import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.BlueprintPythonService import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.CapabilityResourceSource +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment +import org.slf4j.LoggerFactory import org.springframework.context.ApplicationContext import org.springframework.stereotype.Service +import java.io.File @Service("resource-assignment-processor-capability") open class CapabilityResourceAssignmentProcessor(private var applicationContext: ApplicationContext, - private val bluePrintScriptsService: BluePrintScriptsService) : + private val bluePrintScriptsService: BluePrintScriptsService, + private val bluePrintPythonService: BlueprintPythonService) : ResourceAssignmentProcessor() { companion object { @@ -70,7 +77,9 @@ open class CapabilityResourceAssignmentProcessor(private var applicationContext: componentResourceAssignmentProcessor = applicationContext.getBean(instanceName, ResourceAssignmentProcessor::class.java) } CAPABILITY_TYPE_JYTHON_COMPONENT -> { - TODO(" No implementation") + val content = getJythonContent(instanceName) + componentResourceAssignmentProcessor = getJythonResourceAssignmentProcessorInstance(instanceName, + content, capabilityResourceSourceProperty.instanceDependencies) } } @@ -120,4 +129,53 @@ open class CapabilityResourceAssignmentProcessor(private var applicationContext: return resourceAssignmentProcessor } + + private fun getJythonContent(instanceName: String): String { + val absolutePath = raRuntimeService.bluePrintContext().rootPath + .plus(File.separator) + .plus(BluePrintConstants.TOSCA_SCRIPTS_JYTHON_DIR) + .plus(File.separator) + .plus("$instanceName.py") + + return JacksonUtils.getContent(absolutePath) + + } + + /** + * getJythonResourceAssignmentProcessorInstance Purpose: prepare the jython + * executor component as a resource assignment processor + * + * @param pythonClassName String + * @param content String + * @param dependencyInstances List + * @return resourceAssignmentProcessor ResourceAssignmentProcessor + */ + private fun getJythonResourceAssignmentProcessorInstance(pythonClassName: String, content: String, + dependencyInstances: List?): + ResourceAssignmentProcessor { + val jythonContextInstance: MutableMap = hashMapOf() + jythonContextInstance["log"] = LoggerFactory.getLogger(pythonClassName) + jythonContextInstance["raRuntimeService"] = raRuntimeService + dependencyInstances?.forEach { instanceName -> + jythonContextInstance[instanceName] = applicationContext.getBean(instanceName) + } + + return getJythonResourceAssignmentProcessorInstance(pythonClassName, content, jythonContextInstance) + } + + fun getJythonResourceAssignmentProcessorInstance(pythonClassName: String, content: String, + dependencyInstances: MutableMap): + ResourceAssignmentProcessor { + + val resourceAssignmentProcessor = bluePrintPythonService + .jythonInstance(raRuntimeService.bluePrintContext(), pythonClassName, + content, dependencyInstances) + + // Add additional Instance + if (dependencyInstances != null) { + resourceAssignmentProcessor.scriptPropertyInstances = dependencyInstances + } + + return resourceAssignmentProcessor + } } \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt index d6f46a6f3..b07155a32 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt @@ -29,7 +29,7 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode + lateinit var resourceDictionaries: MutableMap var scriptPropertyInstances: Map = hashMapOf() diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt index c9d1c77df..3dda79553 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.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 @@ -18,19 +20,27 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import org.junit.Test import org.junit.runner.RunWith +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.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition import org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintScriptsServiceImpl 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 = [CapabilityResourceAssignmentProcessor::class, BluePrintScriptsServiceImpl::class, - MockCapabilityService::class]) + BlueprintPythonService::class, PythonExecutorProperty::class, MockCapabilityService::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 CapabilityResourceAssignmentProcessorTest { @Autowired @@ -72,6 +82,37 @@ class CapabilityResourceAssignmentProcessorTest { println(processorName) } + @Test + fun `test jython capability`() { + + val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext( + "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") + + val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext) + + capabilityResourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService + + val resourceDefinition = JacksonUtils + .readValueFromClassPathFile("mapping/capability/jython-resource-definitions.json", + ResourceDefinition::class.java)!! + val resourceDefinitions: MutableMap = mutableMapOf() + resourceDefinitions[resourceDefinition.name] = resourceDefinition + capabilityResourceAssignmentProcessor.resourceDictionaries = resourceDefinitions + + val resourceAssignment = ResourceAssignment().apply { + name = "country" + dictionaryName = "country" + dictionarySource = "capability" + property = PropertyDefinition().apply { + type = "string" + } + } + + val processorName = capabilityResourceAssignmentProcessor.apply(resourceAssignment) + assertNotNull(processorName, "couldn't get Jython script resource assignment processor name") + + } + } open class MockCapabilityService { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties index aed61c466..e2785c8a3 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties @@ -2,6 +2,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 @@ -29,4 +31,8 @@ blueprintsprocessor.blueprintArchivePath=./target/blueprints/archive blueprintsprocessor.restclient.primary-config-data.type=basic-auth blueprintsprocessor.restclient.primary-config-data.url=http://127.0.0.1:9111 blueprintsprocessor.restclient.primary-config-data.userId=sampleuser -blueprintsprocessor.restclient.primary-config-data.token=sampletoken \ No newline at end of file +blueprintsprocessor.restclient.primary-config-data.token=sampletoken + +# Python executor +blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints +blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json new file mode 100644 index 000000000..9d8344339 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json @@ -0,0 +1,18 @@ +{ + "tags": "country", + "name": "country", + "updated-by": "brindasanth@onap.com", + "property": { + "description": "description", + "type": "string" + }, + "sources": { + "capability": { + "properties": { + "type": "JYTHON-COMPONENT", + "instance-name": "SampleRAProcessor", + "instance-dependencies": [] + } + } + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/python/SampleResourceAssignmentProcessorScript.py b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/python/SampleResourceAssignmentProcessorScript.py new file mode 100644 index 000000000..bcf450e81 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/python/SampleResourceAssignmentProcessorScript.py @@ -0,0 +1,13 @@ +from resource_assignment_processor_function import AbstractJythonComponentFunction +from blueprint_constants import * + + +class SampleJythonComponentNode(AbstractJythonComponentFunction): + + 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 -- cgit 1.2.3-korg