diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java')
2 files changed, 677 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/services/ManualVspToscaGenerationService.java b/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/services/ManualVspToscaGenerationService.java new file mode 100644 index 0000000000..6d488c0221 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/services/ManualVspToscaGenerationService.java @@ -0,0 +1,433 @@ +package org.openecomp.sdc.generator.core.services; + +import static org.openecomp.sdc.generator.util.GeneratorConstants.ALLOWED_FLAVORS_PROPERTY; +import static org.openecomp.sdc.generator.util.GeneratorConstants.DISK_SIZE; +import static org.openecomp.sdc.generator.util.GeneratorConstants.DISK_SIZE_PROP_DESC_PREFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.IMAGES_PROPERTY; +import static org.openecomp.sdc.generator.util.GeneratorConstants.MEM_SIZE; +import static org.openecomp.sdc.generator.util.GeneratorConstants.MEM_SIZE_PROP_DESC_PREFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.NUM_CPUS; +import static org.openecomp.sdc.generator.util.GeneratorConstants.NUM_CPUS_PROP_DESC_PREFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.PORT_NODE_TEMPLATE_ID_SUFFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.RELEASE_VENDOR; +import static org.openecomp.sdc.generator.util.GeneratorConstants.TOSCA_SERVICE_TEMPLATE_FILE_NAME_SUFFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.VFC_NODE_TEMPLATE_ID_SUFFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.VNF_CONFIG_NODE_TEMPLATE_ID_SUFFIX; +import static org.openecomp.sdc.generator.util.GeneratorConstants.VNF_NODE_TEMPLATE_ID_SUFFIX; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.openecomp.sdc.generator.core.utils.GeneratorUtils; +import org.openecomp.sdc.generator.datatypes.tosca.MultiFlavorVfcImage; +import org.openecomp.sdc.generator.datatypes.tosca.VspModelInfo; +import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; +import org.openecomp.sdc.tosca.datatypes.model.PropertyType; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate; +import org.openecomp.sdc.tosca.services.DataModelUtil; +import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; +import org.openecomp.sdc.tosca.services.ToscaConstants; +import org.openecomp.sdc.tosca.services.ToscaUtil; +import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl; +import org.openecomp.sdc.translator.services.heattotosca.Constants; +import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +/** + * The type Manual vsp tosca generator. + */ +public class ManualVspToscaGenerationService { + + //Map of the abstract node template id and substitution node type + private Map<String, String> abstractSubstitutionIdTypes = new HashMap<>(); + //Map of service template name and service template for the generated service templates + private Map<String, ServiceTemplate> generatedServiceTemplates = new HashMap<>(); + + + /** + * Create manual vsp tosca service model tosca service model. + * + * @param vspModelInfo the vsp model info + * @return the tosca service model + */ + public ToscaServiceModel createManualVspToscaServiceModel(VspModelInfo vspModelInfo) { + ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); + ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); + Map<String, ServiceTemplate> serviceTemplates = new HashMap<>(GlobalTypesGenerator + .getGlobalTypesServiceTemplate()); + toscaServiceModel.setServiceTemplates(serviceTemplates); + toscaServiceModel.setEntryDefinitionServiceTemplate(Constants.MAIN_TEMPLATE_NAME + + TOSCA_SERVICE_TEMPLATE_FILE_NAME_SUFFIX); + createToscaFromVspData(vspModelInfo, toscaServiceModel); + for (Map.Entry<String, ServiceTemplate> serviceTemplateEntry : + generatedServiceTemplates.entrySet()) { + ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplateEntry + .getValue()); + } + return toscaServiceModel; + } + + private void createToscaFromVspData(VspModelInfo vspModelInfo, + ToscaServiceModel toscaServiceModel) { + List<ServiceTemplate> serviceTemplates = new ArrayList<>(); + //Only one component supported + Optional<String> componentName = getComponentNameFromVspModel(vspModelInfo); + if (componentName.isPresent()) { + //Create main service template + ServiceTemplate mainServiceTemplate = createMainServiceTemplate(vspModelInfo); + generatedServiceTemplates.put(Constants.MAIN_TEMPLATE_NAME, mainServiceTemplate); + //Create substitution service template + for (Map.Entry<String, String> entry : abstractSubstitutionIdTypes.entrySet()) { + ServiceTemplate substitutionServiceTemplate = + createSubstitutionServiceTemplate(componentName.get(), entry.getValue(), vspModelInfo, + toscaServiceModel); + generatedServiceTemplates.put(componentName.get(), substitutionServiceTemplate); + } + } + + } + + private Optional<String> getComponentNameFromVspModel(VspModelInfo vspModelInfo) { + String componentName = null; + if (MapUtils.isNotEmpty(vspModelInfo.getComponents())) { + //supported for single component only + componentName = vspModelInfo.getComponents().entrySet().iterator().next().getValue(); + } + return Optional.ofNullable(componentName); + } + + private ServiceTemplate createMainServiceTemplate(VspModelInfo vspModelInfo) { + ServiceTemplate mainServiceTemplate = createInitMainServiceTemplate(vspModelInfo + .getReleaseVendor()); + Map<String, String> components = vspModelInfo.getComponents(); + if (MapUtils.isNotEmpty(components)) { + //Currently since only one component is supported we can fetch the component in this manner. + // May be need to revisited for supporting multiple components + String componentId = components.entrySet().iterator().next().getKey(); + createVnfConfigurationNodeTemplate(mainServiceTemplate, vspModelInfo); + createComponentNodeTemplate(mainServiceTemplate, vspModelInfo, componentId); + createVnfNodeTemplate(mainServiceTemplate, vspModelInfo); + } + return mainServiceTemplate; + } + + private void createVnfConfigurationNodeTemplate(ServiceTemplate mainServiceTemplate, + VspModelInfo vspModelInfo) { + Optional<String> componentName = getComponentNameFromVspModel(vspModelInfo); + if (componentName.isPresent()) { + NodeTemplate vnfConfigurationNodeTemplate = new NodeTemplate(); + vnfConfigurationNodeTemplate.setType(ToscaNodeType.VNF_CONFIG_NODE_TYPE); + if (Objects.nonNull(vspModelInfo.getAllowedFlavors())) { + Map<String, Object> properties = new LinkedHashMap<>(); + properties.put(ALLOWED_FLAVORS_PROPERTY, vspModelInfo.getAllowedFlavors()); + vnfConfigurationNodeTemplate.setProperties(properties); + } + String nodeTemplateId = componentName.get() + VNF_CONFIG_NODE_TEMPLATE_ID_SUFFIX; + DataModelUtil.addNodeTemplate(mainServiceTemplate, nodeTemplateId, + vnfConfigurationNodeTemplate); + } + } + + private void createComponentNodeTemplate(ServiceTemplate mainServiceTemplate, + VspModelInfo vspModelInfo, + String componentId) { + Optional<String> componentName = getComponentNameFromVspModel(vspModelInfo); + if (componentName.isPresent()) { + NodeTemplate vfcNodeTemplate = new NodeTemplate(); + vfcNodeTemplate.setType(ToscaNodeType.MULTIFLAVOR_VFC_NODE_TYPE); + Map<String, Object> properties = new LinkedHashMap<>(); + if (MapUtils.isNotEmpty(vspModelInfo.getMultiFlavorVfcImages())) { + List<MultiFlavorVfcImage> componentImages = + vspModelInfo.getMultiFlavorVfcImages().get(componentId); + Map<String, MultiFlavorVfcImage> vfcImages = new HashMap<>(); + for (MultiFlavorVfcImage image : componentImages) { + vfcImages.put(image.getSoftware_version(), image); + } + properties.put(IMAGES_PROPERTY, vfcImages); + vfcNodeTemplate.setProperties(properties); + } + String nodeTemplateId = componentName.get() + VFC_NODE_TEMPLATE_ID_SUFFIX; + DataModelUtil.addNodeTemplate(mainServiceTemplate, nodeTemplateId, + vfcNodeTemplate); + } + } + + private void createVnfNodeTemplate(ServiceTemplate mainServiceTemplate, + VspModelInfo vspModelInfo) { + Optional<String> componentName = getComponentNameFromVspModel(vspModelInfo); + if (componentName.isPresent()) { + NodeTemplate vnfNodeTemplate = new NodeTemplate(); + String vnfNodeTemplateType = + ToscaNodeType.MULTIDEPLOYMENTFLAVOR_NODE_TYPE + "." + componentName.get(); + vnfNodeTemplate.setType(vnfNodeTemplateType); + List<String> directiveList = new ArrayList<>(); + directiveList.add(ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE); + vnfNodeTemplate.setDirectives(directiveList); + vnfNodeTemplate.setProperties(new LinkedHashMap<>()); + DataModelUtil + .addSubstitutionFilteringProperty(getSubstitutionServiceTemplateFileName(componentName + .get()), vnfNodeTemplate, 1); + //Enable below if we need "abstract_" as prefix like we have in Unified model + //String nodeTemplateId = + // Constants.ABSTRACT_NODE_TEMPLATE_ID_PREFIX + componentName + "VNF_NODE_TEMPLATE_ID_SUFFIX"; + String nodeTemplateId = componentName.get() + VNF_NODE_TEMPLATE_ID_SUFFIX; + DataModelUtil.addNodeTemplate(mainServiceTemplate, nodeTemplateId, + vnfNodeTemplate); + abstractSubstitutionIdTypes.put(componentName.get(), vnfNodeTemplateType); + } + } + + private String getSubstitutionServiceTemplateFileName(String componentName) { + return componentName + TOSCA_SERVICE_TEMPLATE_FILE_NAME_SUFFIX; + } + + private String getNodeTemplateId(String componentName, String idPrefix, String idSuffix) { + StringBuilder builder = new StringBuilder(); + //builder.append(idPrefix); + builder.append(componentName); + builder.append(idSuffix); + return builder.toString(); + } + + private ServiceTemplate createInitMainServiceTemplate(String releaseVendor) { + ServiceTemplate mainServiceTemplate = new ServiceTemplate(); + Map<String, String> templateMetadata = new HashMap<>(); + templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.MAIN_TEMPLATE_NAME); + if (Objects.nonNull(releaseVendor)) { + templateMetadata.put(RELEASE_VENDOR, releaseVendor); + } + mainServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION); + mainServiceTemplate.setMetadata(templateMetadata); + mainServiceTemplate.setTopology_template(new TopologyTemplate()); + mainServiceTemplate.setImports(getImports()); + return mainServiceTemplate; + } + + private List<Map<String, Import>> getImports() { + Map<String, Import> globalSubstitutionTypeImportMap = new HashMap<>(); + Import globalSubstitutionTypeImport = new Import(); + globalSubstitutionTypeImport.setFile(ToscaUtil.getServiceTemplateFileName(Constants + .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME)); + globalSubstitutionTypeImportMap.put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME, + globalSubstitutionTypeImport); + Map<String, Import> globalImports = new HashMap<>(); + List<Map<String, Import>> manualVspGlobalTypesImportList = GlobalTypesGenerator + .getManualVspGlobalTypesImportList(); + manualVspGlobalTypesImportList.add(globalSubstitutionTypeImportMap); + return manualVspGlobalTypesImportList; + } + + private ServiceTemplate createSubstitutionServiceTemplate(String serviceTemplateName, + String substitutionNodeTypeId, + VspModelInfo vspModelInfo, + ToscaServiceModel toscaServiceModel) { + ServiceTemplate substitutionServiceTemplate = + createInitSubstitutionServiceTemplate(serviceTemplateName); + createSubstitutionServiceTemplateComponents(substitutionServiceTemplate, vspModelInfo, + toscaServiceModel); + createSubstitutionServiceTemplateNics(substitutionServiceTemplate, vspModelInfo, + toscaServiceModel); + handleSubstitutionMapping(substitutionServiceTemplate, toscaServiceModel, + substitutionNodeTypeId, serviceTemplateName); + return substitutionServiceTemplate; + } + + private void createSubstitutionServiceTemplateComponents(ServiceTemplate + substitutionServiceTemplate, + VspModelInfo vspModelInfo, + ToscaServiceModel toscaServiceModel) { + Map<String, String> components = vspModelInfo.getComponents(); + if (MapUtils.isNotEmpty(components)) { + for (String componentId : components.keySet()) { + String componentName = components.get(componentId); + String localNodeTypeId = + createComponentDefinitionNodeTemplate(substitutionServiceTemplate, componentName); + createLocalNodeType(substitutionServiceTemplate, localNodeTypeId); + } + } + } + + private void createSubstitutionServiceTemplateNics(ServiceTemplate substitutionServiceTemplate, + VspModelInfo vspModelInfo, + ToscaServiceModel toscaServiceModel) { + Map<String, List<Nic>> nics = vspModelInfo.getNics(); + if (MapUtils.isNotEmpty(nics)) { + for (Map.Entry<String, List<Nic>> entry : nics.entrySet()) { + String componentId = entry.getKey(); + String componentNodeTemplateId = getSubstitutionComponentNodeTemplateId( + vspModelInfo.getComponents().get(componentId)); + List<Nic> nicList = entry.getValue(); + if (CollectionUtils.isNotEmpty(nicList)) { + for (Nic nic : nicList) { + NodeTemplate nicNodeTemplate = new NodeTemplate(); + nicNodeTemplate.setType(ToscaNodeType.NETWORK_PORT); + DataModelUtil.addBindingReqFromPortToCompute(componentNodeTemplateId, nicNodeTemplate); + DataModelUtil.addNodeTemplate(substitutionServiceTemplate, + getNicNodeTemplateId(nic.getName()), nicNodeTemplate); + } + } + } + } + } + + private String getSubstitutionComponentNodeTemplateId(String componentName) { + //TODO: Confirm if anything else is needed here + return componentName; + } + + private String getNicNodeTemplateId(String nicName) { + StringBuilder builder = new StringBuilder(); + builder.append(nicName); + builder.append(PORT_NODE_TEMPLATE_ID_SUFFIX); + return builder.toString(); + } + + private String createComponentDefinitionNodeTemplate(ServiceTemplate substitutionServiceTemplate, + String componentName) { + NodeTemplate nodeTemplate = new NodeTemplate(); + String localNodeTypeId = getLocalNodeTypeId(componentName); + nodeTemplate.setType(localNodeTypeId); + DataModelUtil.addNodeTemplate(substitutionServiceTemplate, componentName, nodeTemplate); + return localNodeTypeId; + } + + private void createLocalNodeType(ServiceTemplate substitutionServiceTemplate, + String localNodeTypeId) { + NodeType localNodeType = new NodeType(); + localNodeType.setDerived_from(ToscaNodeType.COMPUTE); + DataModelUtil.addNodeType(substitutionServiceTemplate, localNodeTypeId, localNodeType ); + } + + private String getLocalNodeTypeId(String componentName) { + return ToscaNodeType.VFC_NODE_TYPE_PREFIX + componentName; + } + + private ServiceTemplate createInitSubstitutionServiceTemplate(String serviceTemplateName) { + ServiceTemplate substitutionServiceTemplate = new ServiceTemplate(); + Map<String, String> templateMetadata = new HashMap<>(); + substitutionServiceTemplate.setTosca_definitions_version(ToscaConstants + .TOSCA_DEFINITIONS_VERSION); + templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, serviceTemplateName); + substitutionServiceTemplate.setMetadata(templateMetadata); + substitutionServiceTemplate.setTopology_template(new TopologyTemplate()); + substitutionServiceTemplate.setImports(getImports()); + return substitutionServiceTemplate; + } + + private void handleSubstitutionMapping(ServiceTemplate substitutionServiceTemplate, + ToscaServiceModel toscaServiceModel, + String substitutionNodeTypeId, + String componentName) { + ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); + ServiceTemplate globalSubstitutionServiceTemplate = fetchGlobalSubstitutionServiceTemplate(); + NodeType substitutionNodeType = + createGlobalSubstitutionNodeType(substitutionServiceTemplate, componentName); + DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, substitutionNodeTypeId, + substitutionNodeType); + + Map<String, Map<String, List<String>>> substitutionMapping = + GeneratorUtils.getSubstitutionNodeTypeExposedConnectionPoints(substitutionNodeType, + substitutionServiceTemplate, toscaServiceModel); + if (Objects.nonNull(substitutionMapping)) { + //add substitution mapping after capability and requirement expose calculation + substitutionServiceTemplate.getTopology_template().setSubstitution_mappings( + DataModelUtil.createSubstitutionTemplateSubMapping(substitutionNodeTypeId, + substitutionNodeType, substitutionMapping)); + } + } + + //*************** CREATE GLOBAL SUBSTITUTION SERVICE TEMPLATE ********************** + + private ServiceTemplate createGlobalSubstitutionServiceTemplate(ServiceTemplate + substitutionServiceTemplate, + String componentName) { + ServiceTemplate globalSubstitutionServiceTemplate = fetchGlobalSubstitutionServiceTemplate(); + NodeType substitutionNodeType = + createGlobalSubstitutionNodeType(substitutionServiceTemplate, componentName); + String substitutionNodeTypeId = getSubstitutionNodeTypeId(componentName); + DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, substitutionNodeTypeId, + substitutionNodeType); + return globalSubstitutionServiceTemplate; + } + + private ServiceTemplate createInitGlobalSubstitutionServiceTemplate() { + ServiceTemplate globalSubstitutionServiceTemplate = new ServiceTemplate(); + Map<String, String> templateMetadata = new HashMap<>(); + globalSubstitutionServiceTemplate.setTosca_definitions_version(ToscaConstants + .TOSCA_DEFINITIONS_VERSION); + templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, + Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME); + globalSubstitutionServiceTemplate.setMetadata(templateMetadata); + globalSubstitutionServiceTemplate.setImports(getImports()); + return globalSubstitutionServiceTemplate; + } + + private NodeType createGlobalSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate, + String componentName) { + NodeType substitutionNodeType = new ToscaAnalyzerServiceImpl() + .createInitSubstitutionNodeType(substitutionServiceTemplate, + ToscaNodeType.VFC_ABSTRACT_SUBSTITUTE); + substitutionNodeType.setProperties( + getManualVspSubstitutionNodeTypeProperties(substitutionNodeType, componentName)); + return substitutionNodeType; + } + + private Map<String, PropertyDefinition> getManualVspSubstitutionNodeTypeProperties( + NodeType substitutionNodeType, String componentName) { + //Create num_cpus property + PropertyDefinition numCpus = new PropertyDefinition(); + numCpus.setType(PropertyType.INTEGER.getDisplayName()); + numCpus.setDescription(NUM_CPUS_PROP_DESC_PREFIX + componentName); + numCpus.setRequired(true); + //Create disk_size property + PropertyDefinition diskSize = new PropertyDefinition(); + diskSize.setType(PropertyType.SCALAR_UNIT_SIZE.getDisplayName()); + diskSize.setDescription(DISK_SIZE_PROP_DESC_PREFIX + componentName); + diskSize.setRequired(true); + //Create mem_size property + PropertyDefinition memSize = new PropertyDefinition(); + memSize.setType(PropertyType.SCALAR_UNIT_SIZE.getDisplayName()); + memSize.setDescription(MEM_SIZE_PROP_DESC_PREFIX + componentName); + memSize.setRequired(true); + + Map<String, PropertyDefinition> manualVspProperties = new LinkedHashMap<>(); + manualVspProperties.put(NUM_CPUS, numCpus); + manualVspProperties.put(DISK_SIZE, diskSize); + manualVspProperties.put(MEM_SIZE, memSize); + + return manualVspProperties; + } + + private String getSubstitutionNodeTypeId(String componentName) { + return ToscaNodeType.MULTIDEPLOYMENTFLAVOR_NODE_TYPE + "." + componentName; + } + + /** + * Fetch global substitution service template service template. + * + * @return the global substitution service template + */ + private ServiceTemplate fetchGlobalSubstitutionServiceTemplate() { + ServiceTemplate globalSubstitutionServiceTemplate = + generatedServiceTemplates.get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME); + if (Objects.isNull(globalSubstitutionServiceTemplate)) { + globalSubstitutionServiceTemplate = createInitGlobalSubstitutionServiceTemplate(); + generatedServiceTemplates.put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME, + globalSubstitutionServiceTemplate); + } + return globalSubstitutionServiceTemplate; + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/utils/GeneratorUtils.java b/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/utils/GeneratorUtils.java new file mode 100644 index 0000000000..c13997b47c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/utils/GeneratorUtils.java @@ -0,0 +1,244 @@ +package org.openecomp.sdc.generator.core.utils; + +import static org.openecomp.sdc.tosca.services.DataModelUtil.addSubstitutionNodeTypeRequirements; + +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; +import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage; +import org.openecomp.sdc.logging.types.LoggerConstants; +import org.openecomp.sdc.logging.types.LoggerErrorCode; +import org.openecomp.sdc.logging.types.LoggerTragetServiceName; +import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition; +import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; +import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.DataModelUtil; +import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; +import org.openecomp.sdc.tosca.services.ToscaUtil; +import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl; +import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * The type Generator utils. + */ +public class GeneratorUtils { + + public static List<String> supportedCapabilities = new ArrayList<>(); + public static List<String> supportedRequirements = new ArrayList<>(); + protected static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); + + static { + //TODO : Read from configuration + supportedCapabilities.addAll(Arrays.asList("host", "os", "endpoint", "scalable")); + supportedRequirements.addAll(Arrays.asList("link")); + } + + + /** + * Add service template to tosca service model. + * + * @param toscaServiceModel the tosca service model + * @param serviceTemplate the service template + */ + public static void addServiceTemplateToToscaServiceModel(ToscaServiceModel toscaServiceModel, + ServiceTemplate serviceTemplate) { + + String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate); + Map<String, ServiceTemplate> serviceTemplates = toscaServiceModel.getServiceTemplates(); + if (!serviceTemplates.containsKey(serviceTemplateFileName)) { + ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplate); + } + toscaServiceModel.setServiceTemplates(serviceTemplates); + } + + /** + * Gets substitution node type exposed connection points. + * + * @param substitutionNodeType the substitution node type + * @param substitutionServiceTemplate the substitution service template + * @param toscaServiceModel the tosca service model + * @return the substitution node type exposed connection points + */ + public static Map<String, Map<String, List<String>>> + getSubstitutionNodeTypeExposedConnectionPoints(NodeType substitutionNodeType, + ServiceTemplate substitutionServiceTemplate, + ToscaServiceModel toscaServiceModel) { + + Map<String, NodeTemplate> nodeTemplates = + substitutionServiceTemplate.getTopology_template().getNode_templates(); + String nodeTemplateId; + NodeTemplate nodeTemplate; + String nodeType; + Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>(); + if (nodeTemplates == null) { + return substitutionMapping; + } + + try { + Map<String, List<String>> capabilitySubstitutionMapping = new HashMap<>(); + Map<String, List<String>> requirementSubstitutionMapping = new HashMap<>(); + substitutionMapping.put("capability", capabilitySubstitutionMapping); + substitutionMapping.put("requirement", requirementSubstitutionMapping); + List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition; + Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment; + List<Map<String, RequirementDefinition>> exposedRequirementsDefinition; + Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = + new HashMap<>(); + Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>(); + Map<String, CapabilityDefinition> exposedCapabilitiesDefinition; + + ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); + for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) { + nodeTemplateId = entry.getKey(); + nodeTemplate = entry.getValue(); + nodeType = nodeTemplate.getType(); + NodeType flatNodeType = (NodeType) toscaAnalyzerService + .getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeType, substitutionServiceTemplate, + toscaServiceModel); + // get requirements + nodeTypeRequirementsDefinition = + getNodeTypeRequirements(flatNodeType, nodeTemplateId, substitutionServiceTemplate, + requirementSubstitutionMapping); + nodeTemplateRequirementsAssignment = + DataModelUtil.getNodeTemplateRequirements(nodeTemplate); + fullFilledRequirementsDefinition.put(nodeTemplateId, nodeTemplateRequirementsAssignment); + //set substitution node type requirements + exposedRequirementsDefinition = + toscaAnalyzerService.calculateExposedRequirements(nodeTypeRequirementsDefinition, + nodeTemplateRequirementsAssignment); + + + //Filter unsupported requirements + Iterator<Map<String, RequirementDefinition>> iterator = + exposedRequirementsDefinition.iterator(); + while (iterator.hasNext()) { + Map<String, RequirementDefinition> requirementDefinitionMap = iterator.next(); + for (Map.Entry<String, RequirementDefinition> requirementDefinitionEntry : + requirementDefinitionMap.entrySet()) { + String requirementKey = requirementDefinitionEntry.getKey(); + if (!supportedRequirements.contains(requirementKey)) { + iterator.remove(); + } + } + } + addSubstitutionNodeTypeRequirements(substitutionNodeType, exposedRequirementsDefinition, + nodeTemplateId); + //get capabilities + addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition, + capabilitySubstitutionMapping, nodeType, + nodeTemplateId, substitutionServiceTemplate, toscaServiceModel); + } + + exposedCapabilitiesDefinition = + toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition, + fullFilledRequirementsDefinition); + + //Filter unsupported capabilities + Iterator<Map.Entry<String, CapabilityDefinition>> iterator = exposedCapabilitiesDefinition + .entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry<String, CapabilityDefinition> capabilityDefinitionEntry = iterator.next(); + //Expected Capability is of the format <capabilityId>_<componentName> + String capabilityKey = capabilityDefinitionEntry.getKey().split("_")[0]; + if (!supportedCapabilities.contains(capabilityKey)) { + iterator.remove(); + } + } + + DataModelUtil.addNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition); + } catch (Exception ex) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API, + LoggerTragetServiceName.GENERATE_MANUAL_VSP_TOSCA, ErrorLevel.INFO.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), "Error occured in get node type exposed " + + "points"); + return null; + } + return substitutionMapping; + } + + /** + * Gets node type requirements. + * + * @param flatNodeType the flat node type + * @param templateName the template name + * @param serviceTemplate the service template + * @param requirementSubstitutionMapping the requirement substitution mapping + * @return the node type requirements + */ + public static List<Map<String, RequirementDefinition>> getNodeTypeRequirements( + NodeType flatNodeType, + String templateName, + ServiceTemplate serviceTemplate, + Map<String, List<String>> requirementSubstitutionMapping) { + List<Map<String, RequirementDefinition>> requirementList = new ArrayList<>(); + List<String> requirementMapping; + if (flatNodeType.getRequirements() != null) { + for (Map<String, RequirementDefinition> requirementMap : flatNodeType.getRequirements()) { + for (Map.Entry<String, RequirementDefinition> requirementNodeEntry : requirementMap + .entrySet()) { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + RequirementDefinition requirementNodeEntryValue = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(requirementNodeEntry.getValue()), + RequirementDefinition.class); + if (requirementNodeEntryValue.getOccurrences() == null) { + requirementNodeEntryValue.setOccurrences(new Object[]{1, 1}); + } + Map<String, RequirementDefinition> requirementDef = new HashMap<>(); + requirementDef.put(requirementNodeEntry.getKey(), requirementNodeEntryValue); + DataModelUtil.addRequirementToList(requirementList, requirementDef); + requirementMapping = new ArrayList<>(); + requirementMapping.add(templateName); + requirementMapping.add(requirementNodeEntry.getKey()); + requirementSubstitutionMapping + .put(requirementNodeEntry.getKey() + "_" + templateName, requirementMapping); + if (requirementNodeEntryValue.getNode() == null) { + requirementNodeEntryValue.setOccurrences(new Object[]{1, 1}); + } + } + } + } + return requirementList; + } + + private static void addNodeTypeCapabilitiesToSubMapping( + Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, + Map<String, List<String>> capabilitySubstitutionMapping, String type, String templateName, + ServiceTemplate substitutionServiceTemplate, ToscaServiceModel toscaServiceModel) { + mdcDataDebugMessage.debugEntryMessage(null, null); + + ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); + NodeType flatNodeType = (NodeType) toscaAnalyzerService + .getFlatEntity(ToscaElementTypes.NODE_TYPE, type, substitutionServiceTemplate, + toscaServiceModel); + String capabilityKey; + List<String> capabilityMapping; + if (flatNodeType.getCapabilities() != null) { + for (Map.Entry<String, CapabilityDefinition> capabilityNodeEntry : flatNodeType + .getCapabilities() + .entrySet()) { + capabilityKey = capabilityNodeEntry.getKey() + "_" + templateName; + nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityNodeEntry.getValue().clone()); + capabilityMapping = new ArrayList<>(); + capabilityMapping.add(templateName); + capabilityMapping.add(capabilityNodeEntry.getKey()); + capabilitySubstitutionMapping.put(capabilityKey, capabilityMapping); + } + } + mdcDataDebugMessage.debugExitMessage(null, null); + } + +} |