From 73528fe9ce4471be5e76c042ea02b40199d36532 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Wed, 30 Jan 2019 15:52:30 -0500 Subject: blueprint scripting services Change-Id: I834b83e0c2716eceadeec8a5f17a7604e938166a Issue-ID: CCSDK-941 Signed-off-by: Muthuramalingam, Brinda Santh --- .../functions/resource-resolution/pom.xml | 4 ++ .../resolution/ResourceSourceProperties.kt | 6 +- .../CapabilityResourceAssignmentProcessor.kt | 56 ++++++++++++--- .../processor/ResourceAssignmentProcessor.kt | 10 +++ .../CapabilityResourceAssignmentProcessorTest.kt | 79 ++++++++++++++++++++++ 5 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt (limited to 'ms/blueprintsprocessor/functions') diff --git a/ms/blueprintsprocessor/functions/resource-resolution/pom.xml b/ms/blueprintsprocessor/functions/resource-resolution/pom.xml index 925f9e33..b29149d8 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.controllerblueprints + blueprint-scripts + org.springframework.boot spring-boot-starter-data-jpa 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 9bad0998..a44d366c 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 @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +@file:Suppress("unused") package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution -open class ResourceSourceProperties { - -} +open class ResourceSourceProperties open class InputResourceSource : ResourceSourceProperties() { lateinit var key: String @@ -51,6 +50,7 @@ open class RestResourceSource : ResourceSourceProperties() { open class CapabilityResourceSource : ResourceSourceProperties() { lateinit var type: String lateinit var instanceName: String + var instanceDependencies: List? = null lateinit var path: String lateinit var expressionType: String var inputKeyMapping: MutableMap? = null 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 6c235599..1370a479 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 @@ -18,14 +18,16 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.CapabilityResourceSource 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.springframework.beans.factory.annotation.Autowired import org.springframework.context.ApplicationContext import org.springframework.stereotype.Service @Service("resource-assignment-processor-capability") -open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() { +open class CapabilityResourceAssignmentProcessor(private var applicationContext: ApplicationContext, + private val bluePrintScriptsService: BluePrintScriptsService) : + ResourceAssignmentProcessor() { companion object { const val CAPABILITY_TYPE_KOTLIN_COMPONENT = "KOTLIN-COMPONENT" @@ -33,9 +35,6 @@ open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() const val CAPABILITY_TYPE_JYTHON_COMPONENT = "JYTHON-COMPONENT" } - @Autowired - private lateinit var applicationContext: ApplicationContext - override fun getName(): String { return "resource-assignment-processor-capability" } @@ -46,20 +45,25 @@ open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() ?: throw BluePrintProcessorException("couldn't get resource definition for ${resourceAssignment.dictionaryName}") val resourceSource = resourceDefinition.sources[resourceAssignment.dictionarySource] - ?: throw BluePrintProcessorException("couldn't get resource definition ${resourceAssignment.dictionaryName} source(${resourceAssignment.dictionarySource})") + ?: throw BluePrintProcessorException("couldn't get resource definition " + + "${resourceAssignment.dictionaryName} source(${resourceAssignment.dictionarySource})") val resourceSourceProps = checkNotNull(resourceSource.properties) { "failed to get $resourceSource properties" } - val capabilityResourceSourceProperty = JacksonUtils.getInstanceFromMap(resourceSourceProps, CapabilityResourceSource::class.java) + /** + * Get the Capability Resource Source Info from Property Definitions. + */ + val capabilityResourceSourceProperty = JacksonUtils + .getInstanceFromMap(resourceSourceProps, CapabilityResourceSource::class.java) val instanceType = capabilityResourceSourceProperty.type val instanceName = capabilityResourceSourceProperty.instanceName - var componentResourceAssignmentProcessor: ResourceAssignmentProcessor? = null when (instanceType) { - CAPABILITY_TYPE_KOTLIN_COMPONENT ->{ - TODO("NO implementation") + CAPABILITY_TYPE_KOTLIN_COMPONENT -> { + componentResourceAssignmentProcessor = getKotlinResourceAssignmentProcessorInstance(instanceName, + capabilityResourceSourceProperty.instanceDependencies) } CAPABILITY_TYPE_JAVA_COMPONENT -> { // Initialize Capability Resource Assignment Processor @@ -84,4 +88,36 @@ open class CapabilityResourceAssignmentProcessor : ResourceAssignmentProcessor() TODO("To Implement") } + + private fun getKotlinResourceAssignmentProcessorInstance(scriptClassName: String, + instanceNames: List? = null): ResourceAssignmentProcessor { + var scriptPropertyInstances: MutableMap? = null + + if (instanceNames != null && instanceNames.isNotEmpty()) { + scriptPropertyInstances = hashMapOf() + instanceNames.forEach { + scriptPropertyInstances[it] = applicationContext.getBean(it) + ?: throw BluePrintProcessorException("couldn't get the dependency instance($it)") + } + } + + return getKotlinResourceAssignmentProcessorInstance(scriptClassName, scriptPropertyInstances) + + } + + fun getKotlinResourceAssignmentProcessorInstance(scriptClassName: String, + scriptPropertyInstances: MutableMap? = null): + ResourceAssignmentProcessor { + + val resourceAssignmentProcessor = bluePrintScriptsService + .scriptInstance(raRuntimeService.bluePrintContext(), + scriptClassName, false) + + // Add additional Instance + if (scriptPropertyInstances != null) { + resourceAssignmentProcessor.scriptPropertyInstances = scriptPropertyInstances + } + + 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 43238a53..d6f46a6f 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 @@ -31,6 +31,16 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode + var scriptPropertyInstances: Map = hashMapOf() + + /** + * This will be called from the scripts to serve instance from runtime to scripts. + */ + open fun scriptPropertyInstanceType(name: String): T { + return scriptPropertyInstances as? T + ?: throw BluePrintProcessorException("couldn't get script property instance ($name)") + } + open fun resourceDefinition(name: String): ResourceDefinition { return resourceDictionaries[name] ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)") 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 new file mode 100644 index 00000000..ce05c34b --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt @@ -0,0 +1,79 @@ +/* + * 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.resource.resolution.processor + +import org.junit.Test +import org.junit.runner.RunWith +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.resource.dict.ResourceAssignment +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.junit4.SpringRunner +import kotlin.test.assertNotNull + +@RunWith(SpringRunner::class) +@ContextConfiguration(classes = [CapabilityResourceAssignmentProcessor::class, BluePrintScriptsServiceImpl::class, + MockCapabilityService::class]) +class CapabilityResourceAssignmentProcessorTest { + + @Autowired + lateinit var capabilityResourceAssignmentProcessor: CapabilityResourceAssignmentProcessor + + @Test + fun `test kotlin capability`() { + + val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext( + "./../../../../components/model-catalog/blueprint-model/starter-blueprint/baseconfiguration") + + val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext) + + capabilityResourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService + capabilityResourceAssignmentProcessor.resourceDictionaries = hashMapOf() + + + val scriptPropertyInstances: MutableMap = mutableMapOf() + scriptPropertyInstances["mock-service1"] = MockCapabilityService() + scriptPropertyInstances["mock-service2"] = MockCapabilityService() + + val resourceAssignmentProcessor = capabilityResourceAssignmentProcessor + .getKotlinResourceAssignmentProcessorInstance( + "ResourceAssignmentProcessor_cba\$ScriptResourceAssignmentProcessor", scriptPropertyInstances) + + assertNotNull(resourceAssignmentProcessor, "couldn't get kotlin script resource assignment processor") + + val resourceAssignment = ResourceAssignment().apply { + name = "ra-name" + dictionaryName = "ra-dict-name" + dictionarySource = "capability" + property = PropertyDefinition().apply { + type = "string" + } + } + + val processorName = resourceAssignmentProcessor.apply(resourceAssignment) + assertNotNull(processorName, "couldn't get kotlin script resource assignment processor name") + println(processorName) + } + +} + +open class MockCapabilityService { + +} \ No newline at end of file -- cgit 1.2.3-korg