From 257dff5659f752d98dfe9421e9761aa5d84bd294 Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Fri, 19 Jul 2019 13:26:14 -0400 Subject: Improve service template access through cache. Change-Id: Ibcff2a27f338c6fd019545dac5457943eff4b024 Issue-ID: CCSDK-1503 Signed-off-by: Brinda Santh --- .../core/interfaces/BluePrintDefinitions.kt | 61 ++++++++++++++++++++++ .../core/scripts/BluePrintCompilerCache.kt | 5 -- .../core/service/BluePrintContext.kt | 11 +++- .../core/utils/BluePrintFileUtils.kt | 3 ++ .../core/utils/BluePrintMetadataUtils.kt | 47 ++++++++++++++--- 5 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintDefinitions.kt (limited to 'ms/controllerblueprints/modules/blueprint-core/src/main') 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 index 000000000..8267e5dcc --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintDefinitions.kt @@ -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 otherDefinition(key: String): T + + fun otherDefinitions(): MutableMap +} + +abstract class AbstractBluePrintDefinitions : BluePrintDefinitions { + + private val otherDefinitionMap: MutableMap = hashMapOf() + + constructor() { + loadOtherDefinitions() + } + + override fun loadOtherDefinitions() { + } + + override fun otherDefinition(key: String): T { + return otherDefinitionMap[key] as T + } + + override fun addOtherDefinition(key: String, definition: Any) { + otherDefinitionMap[key] = definition + } + + override fun otherDefinitions(): MutableMap { + return otherDefinitionMap + } + +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompilerCache.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompilerCache.kt index 73051392c..db139eb59 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompilerCache.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompilerCache.kt @@ -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() { val log = logger(BluePrintClassLoader::class) - override fun reload(key: String, oldValue: URLClassLoader): ListenableFuture { - return reload(key, oldValue) - } - override fun load(key: String): URLClassLoader { log.info("loading cache key($key)") val keyPath = normalizedFile(key) diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintContext.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintContext.kt index 26181bb19..066516fcc 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintContext.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintContext.kt @@ -18,12 +18,12 @@ 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 = hashMapOf() + + fun otherDefinition(key: String) = otherDefinitions[key] as T + + fun checkOtherDefinition(key: String) = otherDefinitions.containsKey(key) + fun imports(): List? = serviceTemplate.imports fun dslDefinitions() = serviceTemplate.dslDefinitions diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintFileUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintFileUtils.kt index ad91d45c3..17a7fd348 100755 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintFileUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintFileUtils.kt @@ -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: " + "\nCreated-By: " + "\nEntry-Definitions: Definitions/.json" + + "\nTemplate-Name: "+ + "\nTemplate-Tags: "+ "\nTemplate-Tags: " } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintMetadataUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintMetadataUtils.kt index 6f090783a..3a1edccc0 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintMetadataUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintMetadataUtils.kt @@ -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(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 -- cgit 1.2.3-korg