Improve service template access through cache. 58/91758/1
authorBrinda Santh <brindasanth@in.ibm.com>
Fri, 19 Jul 2019 17:26:14 +0000 (13:26 -0400)
committerBrinda Santh <brindasanth@in.ibm.com>
Fri, 19 Jul 2019 17:26:14 +0000 (13:26 -0400)
Change-Id: Ibcff2a27f338c6fd019545dac5457943eff4b024
Issue-ID: CCSDK-1503
Signed-off-by: Brinda Santh <brindasanth@in.ibm.com>
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintDefinitions.kt [new file with mode: 0644]
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompilerCache.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintContext.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintFileUtils.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintMetadataUtils.kt
ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintScriptsServiceImplTest.kt
ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintMetadataUtilsTest.kt
ms/controllerblueprints/modules/blueprint-core/src/test/resources/compile/Scripts/kotlin/ActivateBlueprintDefinitions.kt [new file with mode: 0644]

diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintDefinitions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintDefinitions.kt
new file mode 100644 (file)
index 0000000..8267e5d
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ *  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.
+ *  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.cds.controllerblueprints.core.interfaces
+
+import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
+
+interface BluePrintDefinitions {
+
+    /** Define the service Template Model */
+    fun serviceTemplate(): ServiceTemplate
+
+    /** Load Custom Definitions that may used during runtime **/
+    fun loadOtherDefinitions()
+
+    /** Utility Method to add the definition */
+    fun addOtherDefinition(key: String, definition: Any)
+
+    /** Utility method to get the definition */
+    fun <T : Any> otherDefinition(key: String): T
+
+    fun otherDefinitions(): MutableMap<String, Any>
+}
+
+abstract class AbstractBluePrintDefinitions : BluePrintDefinitions {
+
+    private val otherDefinitionMap: MutableMap<String, Any> = hashMapOf()
+
+    constructor() {
+        loadOtherDefinitions()
+    }
+
+    override fun loadOtherDefinitions() {
+    }
+
+    override fun <T : Any> otherDefinition(key: String): T {
+        return otherDefinitionMap[key] as T
+    }
+
+    override fun addOtherDefinition(key: String, definition: Any) {
+        otherDefinitionMap[key] = definition
+    }
+
+    override fun otherDefinitions(): MutableMap<String, Any> {
+        return otherDefinitionMap
+    }
+
+}
\ No newline at end of file
index 7305139..db139eb 100644 (file)
@@ -19,7 +19,6 @@ package org.onap.ccsdk.cds.controllerblueprints.core.scripts
 import com.google.common.cache.CacheBuilder
 import com.google.common.cache.CacheLoader
 import com.google.common.cache.LoadingCache
-import com.google.common.util.concurrent.ListenableFuture
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.cds.controllerblueprints.core.logger
 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
@@ -52,10 +51,6 @@ object BluePrintClassLoader : CacheLoader<String, URLClassLoader>() {
 
     val log = logger(BluePrintClassLoader::class)
 
-    override fun reload(key: String, oldValue: URLClassLoader): ListenableFuture<URLClassLoader> {
-        return reload(key, oldValue)
-    }
-
     override fun load(key: String): URLClassLoader {
         log.info("loading cache key($key)")
         val keyPath = normalizedFile(key)
index 26181bb..066516f 100644 (file)
 
 package org.onap.ccsdk.cds.controllerblueprints.core.service
 
-import org.slf4j.LoggerFactory
 import com.fasterxml.jackson.databind.JsonNode
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
+import org.slf4j.LoggerFactory
 
 /**
  *
@@ -32,7 +32,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
  */
 class BluePrintContext(val serviceTemplate: ServiceTemplate) {
 
-    private val log= LoggerFactory.getLogger(this::class.toString())
+    private val log = LoggerFactory.getLogger(this::class.toString())
 
     /**
      * Blueprint CBA extracted file location
@@ -43,6 +43,13 @@ class BluePrintContext(val serviceTemplate: ServiceTemplate) {
      */
     var entryDefinition = ""
 
+    /** Other definitions along with model, It may Resource Definition, Resource Assignments, Configurations etc..*/
+    var otherDefinitions: MutableMap<String, Any> = hashMapOf()
+
+    fun <T> otherDefinition(key: String) = otherDefinitions[key] as T
+
+    fun checkOtherDefinition(key: String) = otherDefinitions.containsKey(key)
+
     fun imports(): List<ImportDefinition>? = serviceTemplate.imports
 
     fun dslDefinitions() = serviceTemplate.dslDefinitions
index ad91d45..17a7fd3 100755 (executable)
@@ -1,6 +1,7 @@
 /*
  * Copyright © 2017-2018 AT&T Intellectual Property.
  * Modifications Copyright © 2019 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.
@@ -215,6 +216,8 @@ class BluePrintFileUtils {
                     "\nCSAR-Version: <VERSION>" +
                     "\nCreated-By: <AUTHOR NAME>" +
                     "\nEntry-Definitions: Definitions/<BLUEPRINT_NAME>.json" +
+                    "\nTemplate-Name: <BLUEPRINT_NAME>"+
+                    "\nTemplate-Tags: <BLUEPRINT_VERSION>"+
                     "\nTemplate-Tags: <TAGS>"
         }
 
index 6f09078..3a1edcc 100644 (file)
@@ -20,11 +20,10 @@ package org.onap.ccsdk.cds.controllerblueprints.core.utils
 
 import com.fasterxml.jackson.databind.JsonNode
 import kotlinx.coroutines.runBlocking
-import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
-import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
+import org.onap.ccsdk.cds.controllerblueprints.core.*
 import org.onap.ccsdk.cds.controllerblueprints.core.data.ToscaMetaData
-import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
-import org.onap.ccsdk.cds.controllerblueprints.core.readNBLines
+import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintDefinitions
+import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintImportService
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
@@ -138,7 +137,12 @@ class BluePrintMetadataUtils {
 
             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
 
-            readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
+            // If the EntryDefinition is Kotlin file, compile and get Service Template
+            if (toscaMetaData.entityDefinitions.endsWith("kt")) {
+                readBlueprintKotlinFile(toscaMetaData, blueprintBasePath)
+            } else {
+                readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
+            }
         }
 
         private suspend fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
@@ -158,15 +162,42 @@ class BluePrintMetadataUtils {
         }
 
         private suspend fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
-            val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
+            val normalizedBasePath = normalizedPathName(basePath)
+            val rootFilePath = normalizedPathName(normalizedBasePath, entityDefinitions)
             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
             // Recursively Import Template files
-            val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
+            val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, normalizedBasePath)
             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
             val blueprintContext = BluePrintContext(completeServiceTemplate)
-            blueprintContext.rootPath = basePath
+            blueprintContext.rootPath = normalizedBasePath
             blueprintContext.entryDefinition = entityDefinitions
             return blueprintContext
         }
+
+        /** Reade the Service Template Definitions from the Kotlin file */
+        private suspend fun readBlueprintKotlinFile(toscaMetaData: ToscaMetaData, basePath: String): BluePrintContext {
+
+            checkNotNull(toscaMetaData.templateName) { "couldn't find 'Template-Name' key in TOSCA.meta" }
+            checkNotNull(toscaMetaData.templateVersion) { "couldn't find 'Template-Version' key in TOSCA.meta" }
+
+            val definitionClassName = toscaMetaData.entityDefinitions.removeSuffix(".kt")
+            val normalizedBasePath = normalizedPathName(basePath)
+
+            val bluePrintScriptsService = BluePrintScriptsServiceImpl()
+            val bluePrintDefinitions = bluePrintScriptsService
+                    .scriptInstance<BluePrintDefinitions>(normalizedBasePath, toscaMetaData.templateName!!,
+                            toscaMetaData.templateVersion!!, definitionClassName, true)
+            // Get the Service Template
+            val serviceTemplate = bluePrintDefinitions.serviceTemplate()
+
+            // Clean the Default type import Definitions
+            BluePrintFileUtils.cleanImportTypes(serviceTemplate)
+
+            val blueprintContext = BluePrintContext(serviceTemplate)
+            blueprintContext.rootPath = normalizedBasePath
+            blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
+            blueprintContext.otherDefinitions = bluePrintDefinitions.otherDefinitions()
+            return blueprintContext
+        }
     }
 }
\ No newline at end of file
index 9d4ef69..66fec75 100644 (file)
@@ -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.
@@ -19,6 +20,8 @@ package org.onap.ccsdk.cds.controllerblueprints.core.scripts
 
 import kotlinx.coroutines.runBlocking
 import org.junit.Test
+import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
+import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintDefinitions
 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
 import kotlin.script.experimental.jvm.util.classpathFromClass
@@ -51,10 +54,21 @@ class BluePrintScriptsServiceImplTest {
             val bluePrintScriptsService = BluePrintScriptsServiceImpl()
 
             val basePath = normalizedPathName("src/test/resources/compile")
+            /** Load the Definitions */
+            val bluePrintDefinitions = bluePrintScriptsService
+                    .scriptInstance<BluePrintDefinitions>(basePath,
+                            "cba.scripts.ActivateBlueprintDefinitions", true)
+            assertNotNull(bluePrintDefinitions, "failed to get blueprint definitions")
+
+            val serviceTemplate = bluePrintDefinitions.serviceTemplate()
+            assertNotNull(serviceTemplate, "failed to get service template")
+
+            val customDataType = bluePrintDefinitions.otherDefinition<DataType>("datatype-custom-datatype")
+            assertNotNull(customDataType, "failed to get custom definitions")
 
             val instance = bluePrintScriptsService
                     .scriptInstance<BlueprintFunctionNode<String, String>>(basePath,
-                            "cba.scripts.SampleBlueprintFunctionNode", true)
+                            "cba.scripts.SampleBlueprintFunctionNode", false)
             assertNotNull(instance, "failed to get compiled instance")
 
             val cachedInstance = bluePrintScriptsService
index 1a6ccfa..6c0c30e 100644 (file)
@@ -21,9 +21,12 @@ package org.onap.ccsdk.cds.controllerblueprints.core.utils
 import kotlinx.coroutines.runBlocking
 import org.junit.Test
 import org.onap.ccsdk.cds.controllerblueprints.core.data.ToscaMetaData
+import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
+import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintCompileCache
 import kotlin.test.assertEquals
 import kotlin.test.assertNotNull
 import kotlin.test.assertNull
+import kotlin.test.assertTrue
 
 class BluePrintMetadataUtilsTest {
 
@@ -44,6 +47,24 @@ class BluePrintMetadataUtilsTest {
 
     }
 
+    @Test
+    fun testKotlinBluePrintContext() {
+        val path = normalizedPathName("src/test/resources/compile")
+        val blueprintContext = BluePrintMetadataUtils.getBluePrintContext(path)
+        assertNotNull(blueprintContext, "failed to get blueprint context")
+        assertNotNull(blueprintContext.serviceTemplate, "failed to get blueprint context service template")
+        assertNotNull(blueprintContext.serviceTemplate, "failed to get blueprint context service template")
+        assertNotNull(blueprintContext.otherDefinitions, "failed to get blueprint contextother definitions")
+
+        var cachePresent = BluePrintCompileCache.hasClassLoader(path)
+        assertTrue(cachePresent, "failed to generate cache key ($path)")
+
+        /** Cleaning Cache */
+        BluePrintCompileCache.cleanClassLoader(path)
+        cachePresent = BluePrintCompileCache.hasClassLoader(path)
+        assertTrue(!cachePresent, "failed to remove cache key ($path)")
+    }
+
     @Test
     fun environmentDataTest() {
         val environmentPath = "./src/test/resources/environments"
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/resources/compile/Scripts/kotlin/ActivateBlueprintDefinitions.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/compile/Scripts/kotlin/ActivateBlueprintDefinitions.kt
new file mode 100644 (file)
index 0000000..4f4d210
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  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.
+ *  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 cba.scripts
+
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
+import org.onap.ccsdk.cds.controllerblueprints.core.dsl.dataType
+import org.onap.ccsdk.cds.controllerblueprints.core.dsl.serviceTemplate
+import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.AbstractBluePrintDefinitions
+
+class ActivateBlueprintDefinitions : AbstractBluePrintDefinitions() {
+
+    override fun serviceTemplate(): ServiceTemplate {
+
+        return serviceTemplate("sample-blue-print", "1.0.0",
+                "brindasanth@onap.com", "sample, blueprints") {
+            topologyTemplate {
+                workflowNodeTemplate("activate", "component-resource-resolution", "") {
+                    operation("ResourceResolutionExecutor", "") {
+                        inputs {
+                            property("string-value", "sample")
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    override fun loadOtherDefinitions() {
+        /** Sample Definitions */
+        val customDataType = dataType("custom-datatype", "1.0.0",
+                BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT, "") {
+            property("name", BluePrintConstants.DATA_TYPE_STRING, true, "")
+            property("value", BluePrintConstants.DATA_TYPE_STRING, true, "")
+        }
+        /** Loading to definitions */
+        addOtherDefinition("datatype-custom-datatype", customDataType)
+    }
+}
\ No newline at end of file