From ed2e6c9ab708184398718ed0c112806e32a23d05 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Fri, 14 Dec 2018 16:41:37 -0500 Subject: Add blueprint resource definition enrichment. Change-Id: I01234093028ffdc8bf1688e41baba20fae7da5ce Issue-ID: CCSDK-747 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../service/BluePrintEnhancerService.java | 174 --------------------- .../enhancer/BluePrintEnhancerServiceImpl.kt | 31 ++-- .../enhancer/ResourceDefinitionEnhancerService.kt | 124 +++++++++++++++ .../enhancer/BluePrintEnhancerServiceImplTest.java | 4 +- .../modules/service/src/test/resources/logback.xml | 2 +- 5 files changed, 148 insertions(+), 187 deletions(-) delete mode 100644 ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java create mode 100644 ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt (limited to 'ms/controllerblueprints/modules/service') diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java deleted file mode 100644 index 930c88d8d..000000000 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * 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. - * 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.controllerblueprints.service; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.base.Preconditions; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.jetbrains.annotations.NotNull; -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException; -import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant; -import org.onap.ccsdk.apps.controllerblueprints.core.data.*; -import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils; -import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment; -import org.onap.ccsdk.apps.controllerblueprints.service.enhancer.ResourceAssignmentEnhancerService; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * BluePrintEnhancerService - * - * @author Brinda Santh DATE : 8/8/2018 - */ - -@Deprecated -public class BluePrintEnhancerService { - - private static EELFLogger log = EELFManager.getInstance().getLogger(BluePrintEnhancerService.class); - - private ResourceAssignmentEnhancerService resourceAssignmentEnhancerService; - - private Map recipeDataTypes = new HashMap<>(); - - - private void populateArtifactTemplateMappingDataType(@NotNull String nodeTemplateName, @NotNull NodeTemplate nodeTemplate) - throws BluePrintException { - log.info("****** Processing Artifact Node Template : {}", nodeTemplateName); - - if (nodeTemplate.getProperties() != null) { - - if (!nodeTemplate.getProperties().containsKey(ConfigModelConstant.PROPERTY_RECIPE_NAMES)) { - throw new BluePrintException("Node Template (" + nodeTemplateName + ") doesn't have " - + ConfigModelConstant.PROPERTY_RECIPE_NAMES + " property."); - } - - // Modified for ONAP converted Object to JsonNode - JsonNode recipeNames = nodeTemplate.getProperties().get(ConfigModelConstant.PROPERTY_RECIPE_NAMES); - - log.info("Processing Recipe Names : {} ", recipeNames); - - if (recipeNames != null && recipeNames.isArray() && recipeNames.size() > 0) { - - Map mappingProperties = - getCapabilityMappingProperties(nodeTemplateName, nodeTemplate); - - for (JsonNode recipeNameNode : recipeNames) { - String recipeName = recipeNameNode.textValue(); - processRecipe(nodeTemplateName, mappingProperties, recipeName); - } - } - } - } - - private void processRecipe(@NotNull String nodeTemplateName, Map mappingProperties, String recipeName) { - if (StringUtils.isNotBlank(recipeName)) { - DataType recipeDataType = this.recipeDataTypes.get(recipeName); - if (recipeDataType == null) { - log.info("DataType not present for the recipe({})", recipeName); - recipeDataType = new DataType(); - recipeDataType.setVersion("1.0.0"); - recipeDataType.setDescription( - "This is Dynamic Data type definition generated from resource mapping for the config template name " - + nodeTemplateName + "."); - recipeDataType.setDerivedFrom(ConfigModelConstant.MODEL_TYPE_DATA_TYPE_DYNAMIC); - Map dataTypeProperties = new HashMap<>(); - recipeDataType.setProperties(dataTypeProperties); - } else { - log.info("DataType Already present for the recipe({})", recipeName); - } - - // Merge all the Recipe Properties - mergeDataTypeProperties(recipeDataType, mappingProperties); - - // Overwrite Recipe DataType - this.recipeDataTypes.put(recipeName, recipeDataType); - - } - } - - private Map getCapabilityMappingProperties(String nodeTemplateName, - NodeTemplate nodeTemplate) throws BluePrintException { - - Map dataTypeProperties = null; - if (nodeTemplate != null && MapUtils.isNotEmpty(nodeTemplate.getCapabilities())) { - CapabilityAssignment capability = - nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING); - - if (capability != null && capability.getProperties() != null) { - - String resourceAssignmentContent = JacksonUtils - .getJson(capability.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING)); - - List resourceAssignments = - JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment.class); - - Preconditions.checkNotNull(resourceAssignments, "Failed to Processing Resource Mapping " + resourceAssignmentContent); - // Enhance Resource Assignment TODO("Plug Resource Assignment Enhancer Service") - //resourceAssignmentEnhancerService.enhanceBluePrint(this, resourceAssignments); - - dataTypeProperties = new HashMap<>(); - - for (ResourceAssignment resourceAssignment : resourceAssignments) { - if (resourceAssignment != null - // && Boolean.valueOf(resourceAssignment.getInputParameter()) - && resourceAssignment.getProperty() != null - && StringUtils.isNotBlank(resourceAssignment.getName())) { - - dataTypeProperties.put(resourceAssignment.getName(), resourceAssignment.getProperty()); - - } - } - - } - } - return dataTypeProperties; - } - - private void mergeDataTypeProperties(DataType dataType, Map mergeProperties) { - if (dataType != null && dataType.getProperties() != null && mergeProperties != null) { - // Add the Other Template Properties - mergeProperties.forEach((mappingKey, propertyDefinition) -> dataType.getProperties().put(mappingKey, propertyDefinition)); - } - } - - private void populateRecipeInputs(ServiceTemplate serviceTemplate) { - if (serviceTemplate.getTopologyTemplate() != null - && MapUtils.isNotEmpty(serviceTemplate.getTopologyTemplate().getInputs()) - && MapUtils.isNotEmpty(this.recipeDataTypes) - && MapUtils.isNotEmpty(serviceTemplate.getDataTypes())) { - this.recipeDataTypes.forEach((recipeName, recipeDataType) -> { - String dataTypePrefix = recipeName.replace("-action", "") + "-request"; - String dataTypeName = "dt-" + dataTypePrefix; - - serviceTemplate.getDataTypes().put(dataTypeName, recipeDataType); - - PropertyDefinition customInputProperty = new PropertyDefinition(); - customInputProperty.setDescription("This is Dynamic Data type for the receipe " + recipeName + "."); - customInputProperty.setRequired(Boolean.FALSE); - customInputProperty.setType(dataTypeName); - serviceTemplate.getTopologyTemplate().getInputs().put(dataTypePrefix, customInputProperty); - - }); - } - } -} diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt index 0b42a26ee..6c7b6e08b 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt @@ -24,6 +24,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintEnhance import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext +import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils import org.springframework.stereotype.Service @@ -31,37 +32,42 @@ import java.util.* @Service open class BluePrintEnhancerServiceImpl(private val bluePrintRepoService: BluePrintRepoService, - private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService) : BluePrintEnhancerService { + private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService, + private val resourceDefinitionEnhancerService: ResourceDefinitionEnhancerService) : BluePrintEnhancerService { private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintEnhancerServiceImpl::class.toString()) override fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext { + // Copy the Blueprint Content to Target Location BluePrintFileUtils.copyBluePrint(basePath, enrichedBasePath) // Enhance the Blueprint - val enhancedBluePrintContext = enhance(enrichedBasePath) - - // Delete the Old Type files - BluePrintFileUtils.deleteBluePrintTypes(enrichedBasePath) - - // Write the Type File Definitions - BluePrintFileUtils.writeBluePrintTypes(enhancedBluePrintContext) - return enhancedBluePrintContext + return enhance(enrichedBasePath) } @Throws(BluePrintException::class) override fun enhance(basePath: String): BluePrintContext { + log.info("Enhancing blueprint($basePath)") - val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(UUID.randomUUID().toString(), basePath) + val blueprintRuntimeService = BluePrintMetadataUtils + .getBaseEnhancementBluePrintRuntime(UUID.randomUUID().toString(), basePath) + try { bluePrintTypeEnhancerService.enhanceServiceTemplate(blueprintRuntimeService, "service_template", blueprintRuntimeService.bluePrintContext().serviceTemplate) + // Write the Type File Definitions + BluePrintFileUtils.writeBluePrintTypes(blueprintRuntimeService.bluePrintContext()) + + // Enhance Resource Dictionary + enhanceResourceDefinition(blueprintRuntimeService) + if (blueprintRuntimeService.getBluePrintError().errors.isNotEmpty()) { throw BluePrintException(blueprintRuntimeService.getBluePrintError().errors.toString()) } + } catch (e: Exception) { log.error("failed in blueprint enhancement", e) } @@ -69,5 +75,10 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintRepoService: BluePr return blueprintRuntimeService.bluePrintContext() } + private fun enhanceResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>) { + + resourceDefinitionEnhancerService.enhance(blueprintRuntimeService) + } + } diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt new file mode 100644 index 000000000..ab5ca74cb --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt @@ -0,0 +1,124 @@ +/* + * 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.controllerblueprints.service.enhancer + +import com.att.eelf.configuration.EELFLogger +import com.att.eelf.configuration.EELFManager +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext +import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils +import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDefinitionRepoService +import org.springframework.stereotype.Service + +interface ResourceDefinitionEnhancerService { + + @Throws(BluePrintException::class) + fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) +} + +@Service +class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) : + ResourceDefinitionEnhancerService { + + private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDefinitionEnhancerService::class.toString()) + + companion object { + const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource" + const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates" + } + + // Enhance the Resource Definition + // 1. Get the Resource Mapping files from all NodeTemplates. + // 2. Get all the Unique Resource assignments from all mapping files + // 3. Collect the Resource Definition for Resource Assignment names from database. + // 4. Create the Resource Definition under blueprint base path. + override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) { + + val blueprintContext = bluePrintRuntimeService.bluePrintContext() + + val mappingFiles = getAllResourceMappingFiles(blueprintContext) + log.info("resources assignment files ($mappingFiles)") + if (mappingFiles != null) { + getResourceDefinition(blueprintContext, mappingFiles) + } + } + + // Get all the Mapping files from all node templates. + private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List? { + + return blueprintContext.nodeTemplates?.mapNotNull { nodeTemplateMap -> + + // Return only Mapping Artifact File Names + nodeTemplateMap.value.artifacts?.filter { artifactDefinitionMap -> + artifactDefinitionMap.value.type == ARTIFACT_TYPE_MAPPING_SOURCE + }?.mapNotNull { artifactDefinitionMap -> + artifactDefinitionMap.value.file + } + + }?.single { it.isNotEmpty() }?.distinct() + } + + // Convert file content to ResourceAssignments asynchronously + private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List) { + runBlocking { + val blueprintBasePath = blueprintContext.rootPath + val deferredResourceAssignments = mutableListOf>>() + for (file in files) { + log.info("processing file ($file)") + deferredResourceAssignments += async { + ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file") + } + } + + val resourceAssignments = mutableListOf() + for (deferredResourceAssignment in deferredResourceAssignments) { + resourceAssignments.addAll(deferredResourceAssignment.await()) + } + + val distinctResourceAssignments = resourceAssignments.distinctBy { it.name } + generateResourceDictionaryFile(blueprintBasePath, distinctResourceAssignments) + //log.info("distinct Resource assignment ($distinctResourceAssignments)") + } + } + + // Read the Resource Definitions from the Database and write to type file. + private fun generateResourceDictionaryFile(blueprintBasePath: String, resourceAssignments: List) { + val resourcekeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct() + log.info("distinct resource keys ($resourcekeys)") + + //TODO("Optimise DB single Query to multiple Query") + // Collect the Resource Definition from database and convert to map to save in file + val resourceDefinitionMap = resourcekeys.map { resourceKey -> + getResourceDefinition(resourceKey) + }.map { it.name to it }.toMap() + + // Recreate the Resource Definition File + ResourceDictionaryUtils.writeResourceDefinitionTypes(blueprintBasePath, resourceDefinitionMap) + log.info("resource definition file created successfully") + } + + // Get the Resource Definition from Database + private fun getResourceDefinition(name: String): ResourceDefinition { + return resourceDefinitionRepoService.getResourceDefinition(name) + } +} \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.java b/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.java index 7d9c2e1a2..06f2f9088 100644 --- a/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.java +++ b/ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.java @@ -67,7 +67,7 @@ public class BluePrintEnhancerServiceImplTest { Assert.assertNotNull("failed to get blueprintContext ", bluePrintContext); // Validate the Generated BluePrints - - bluePrintValidatorService.validateBluePrints(targetPath); + Boolean valid = bluePrintValidatorService.validateBluePrints(targetPath); + Assert.assertTrue("blueprint validation failed ", valid); } } \ No newline at end of file diff --git a/ms/controllerblueprints/modules/service/src/test/resources/logback.xml b/ms/controllerblueprints/modules/service/src/test/resources/logback.xml index 7b7ef7565..fc1f6698e 100644 --- a/ms/controllerblueprints/modules/service/src/test/resources/logback.xml +++ b/ms/controllerblueprints/modules/service/src/test/resources/logback.xml @@ -17,7 +17,7 @@ - +