blueprint scripting services
authorMuthuramalingam, Brinda Santh <bs2796@att.com>
Wed, 30 Jan 2019 20:52:30 +0000 (15:52 -0500)
committerSteve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com>
Mon, 4 Feb 2019 14:45:59 +0000 (09:45 -0500)
Change-Id: I834b83e0c2716eceadeec8a5f17a7604e938166a
Issue-ID: CCSDK-941
Signed-off-by: Muthuramalingam, Brinda Santh <bs2796@att.com>
ms/blueprintsprocessor/functions/resource-resolution/pom.xml
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessor.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt
ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceAssignmentProcessorTest.kt [new file with mode: 0644]
ms/blueprintsprocessor/parent/pom.xml

index 925f9e3..b29149d 100644 (file)
             <artifactId>db-resources</artifactId>
             <version>${project.version}</version>
         </dependency>-->
+        <dependency>
+            <groupId>org.onap.ccsdk.apps.controllerblueprints</groupId>
+            <artifactId>blueprint-scripts</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-jpa</artifactId>
index 9bad099..a44d366 100644 (file)
  * 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<String>? = null
     lateinit var path: String
     lateinit var expressionType: String
     var inputKeyMapping: MutableMap<String, String>? = null
index 6c23559..1370a47 100644 (file)
@@ -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<String>? = null): ResourceAssignmentProcessor {
+        var scriptPropertyInstances: MutableMap<String, Any>? = 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<String, Any>? = null):
+            ResourceAssignmentProcessor {
+
+        val resourceAssignmentProcessor = bluePrintScriptsService
+                .scriptInstance<ResourceAssignmentProcessor>(raRuntimeService.bluePrintContext(),
+                        scriptClassName, false)
+
+        // Add additional Instance
+        if (scriptPropertyInstances != null) {
+            resourceAssignmentProcessor.scriptPropertyInstances = scriptPropertyInstances
+        }
+
+        return resourceAssignmentProcessor
+    }
 }
\ No newline at end of file
index 43238a5..d6f46a6 100644 (file)
@@ -31,6 +31,16 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig
     lateinit var raRuntimeService: ResourceAssignmentRuntimeService
     lateinit var resourceDictionaries: Map<String, ResourceDefinition>
 
+    var scriptPropertyInstances: Map<String, Any> = hashMapOf()
+
+    /**
+     * This will be called from the scripts to serve instance from runtime to scripts.
+     */
+    open fun <T> 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 (file)
index 0000000..ce05c34
--- /dev/null
@@ -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<String, Any> = 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
index 472b466..0de4b42 100755 (executable)
@@ -18,7 +18,7 @@
   ~  limitations under the License.
   -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.onap.ccsdk.apps</groupId>
                 <artifactId>core</artifactId>
                 <version>${project.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.onap.ccsdk.apps.controllerblueprints</groupId>
+                <artifactId>blueprint-scripts</artifactId>
+                <version>${project.version}</version>
+            </dependency>
 
             <!-- Database -->
             <dependency>