From 54b96133b33b3bf55e9670ed2a8c89996ac413b9 Mon Sep 17 00:00:00 2001 From: "bilal.iqbal" Date: Tue, 19 Mar 2019 19:16:03 +0000 Subject: Process CSAR 1. Handle Main service Template 2. keep copy of the onboarded package 3. Sonar fixes Issue-ID: SDC-2153 Issue-ID: SDC-2112 Change-Id: I7f84d5d6d7a4c6eeed317a7ddd19b53272182fd8 Signed-off-by: bilal.iqbal --- .../core/impl/AbstractToscaConverter.java | 466 +++++++++++++++++++ .../openecomp/core/impl/ToscaConverterImpl.java | 491 ++------------------- .../openecomp/core/impl/ToscaSolConverterImpl.java | 131 ++++++ .../core/impl/ToscaSolConverterImplTest.java | 105 +++++ .../src/test/resources/toscaSOlConverter/Main.yaml | 9 + .../toscaSOlConverter/invalidMainService.yaml | 7 + .../toscaSOlConverter/sample_import1.yaml | 5 + .../toscaSOlConverter/sample_import2.yaml | 6 + .../toscaSOlConverter/sample_import3.yaml | 5 + .../toscaSOlConverter/sample_import4.yaml | 6 + 10 files changed, 773 insertions(+), 458 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterImpl.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterImplTest.java create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/Main.yaml create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/invalidMainService.yaml create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import1.yaml create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import2.yaml create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import3.yaml create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import4.yaml (limited to 'openecomp-be/lib') diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java new file mode 100644 index 0000000000..0fc65c1f94 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java @@ -0,0 +1,466 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.core.impl; + +import org.apache.commons.collections.MapUtils; +import org.onap.sdc.tosca.datatypes.model.ArtifactDefinition; +import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment; +import org.onap.sdc.tosca.datatypes.model.Import; +import org.onap.sdc.tosca.datatypes.model.NodeFilter; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +import org.onap.sdc.tosca.datatypes.model.NodeType; +import org.onap.sdc.tosca.datatypes.model.ParameterDefinition; +import org.onap.sdc.tosca.datatypes.model.RequirementAssignment; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.converter.ToscaConverter; +import org.openecomp.core.converter.datatypes.Constants; +import org.openecomp.core.converter.datatypes.CsarFileTypes; +import org.openecomp.core.converter.errors.SubstitutionMappingsConverterErrorBuilder; +import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.services.DataModelUtil; +import org.openecomp.sdc.tosca.services.ToscaUtil; +import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator; +import org.yaml.snakeyaml.error.YAMLException; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Pattern; + +import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX; +import static org.openecomp.core.converter.datatypes.Constants.capabilities; +import static org.openecomp.core.converter.datatypes.Constants.definitionsDir; +import static org.openecomp.core.converter.datatypes.Constants.globalStName; +import static org.openecomp.core.converter.datatypes.Constants.globalSubstitution; +import static org.openecomp.core.converter.datatypes.Constants.inputs; +import static org.openecomp.core.converter.datatypes.Constants.mainStName; +import static org.openecomp.core.converter.datatypes.Constants.nodeType; +import static org.openecomp.core.converter.datatypes.Constants.openecompHeatIndex; +import static org.openecomp.core.converter.datatypes.Constants.outputs; +import static org.openecomp.core.converter.datatypes.Constants.requirements; +import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME; +import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.HEAT_INDEX_IMPORT_FILE; +import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.ONAP_INDEX_IMPORT_FILE; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; + +public abstract class AbstractToscaConverter implements ToscaConverter { + + @Override + public abstract ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException; + + protected void handleMetadataFile(Map csarFiles) { + byte[] bytes = csarFiles.remove(TOSCA_META_PATH_FILE_NAME); + if (bytes != null) { + csarFiles.put(TOSCA_META_ORIG_PATH_FILE_NAME, bytes); + } + } + + protected void handleDefintionTemplate(String key, Map csarFiles, + GlobalSubstitutionServiceTemplate gsst) { + try { + ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(key)); + Object nodeTypes = readerService.getNodeTypes(); + if (nodeTypes instanceof Map) { + Map nodeTypeMap = (Map) nodeTypes; + gsst.appendNodes(nodeTypeMap); + } + } catch (YAMLException ye) { + throw new CoreException(new ErrorCode.ErrorCodeBuilder() + .withMessage("Invalid YAML content in file " + key) + .withCategory(ErrorCategory.APPLICATION).build(), ye); + } + } + + protected String getConcreteArtifactFileName(String fileName){ + int artifactIndex = fileName.indexOf(CsarFileTypes.Artifacts.name()); + if(artifactIndex < 0){ + return fileName; + } + + int artifactDirectoryIndex = + artifactIndex + CsarFileTypes.Artifacts.name().length() + 1; + return fileName.substring(artifactDirectoryIndex); + } + + protected void updateToscaServiceModel(ToscaServiceModel toscaServiceModel, + Map serviceTemplates, + FileContentHandler externalFilesHandler, + GlobalSubstitutionServiceTemplate globalSubstitutionServiceTemplate, + Map csarFiles, String entryDefinitionServiceTemplateName) { + Collection globalServiceTemplates = + GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR).values(); + addGlobalServiceTemplates(globalServiceTemplates, serviceTemplates); + toscaServiceModel.setServiceTemplates(serviceTemplates); + toscaServiceModel.setEntryDefinitionServiceTemplate(entryDefinitionServiceTemplateName); + externalFilesHandler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, + csarFiles.get(TOSCA_META_ORIG_PATH_FILE_NAME)); + toscaServiceModel.setArtifactFiles(externalFilesHandler); + + if(MapUtils.isNotEmpty(globalSubstitutionServiceTemplate.getNode_types())) { + serviceTemplates + .put(GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME, globalSubstitutionServiceTemplate); + } + } + + private void addGlobalServiceTemplates(Collection globalServiceTemplates, + Map serviceTemplates) { + for (ServiceTemplate serviceTemplate : globalServiceTemplates) { + serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate); + } + } + + protected void handleServiceTemplate(String serviceTemplateName, + String fileName, Map csarFiles, + Map serviceTemplates) { + Optional serviceTemplate = + getServiceTemplateFromCsar(fileName, csarFiles); + serviceTemplate.ifPresent( + serviceTemplateValue -> addServiceTemplate(serviceTemplateName, serviceTemplateValue, + serviceTemplates)); + } + + private void addServiceTemplate(String serviceTemplateName, + ServiceTemplate serviceTemplate, + Map serviceTemplates) { + serviceTemplates.put(serviceTemplateName, serviceTemplate); + } + + private Optional getServiceTemplateFromCsar(String fileName, + Map csarFiles) { + byte[] fileContent = csarFiles.get(fileName); + ServiceTemplate serviceTemplate = convertServiceTemplate(fileName, fileContent); + + return Optional.of(serviceTemplate); + } + + private ServiceTemplate convertServiceTemplate(String serviceTemplateName, + byte[] fileContent) { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + try { + ServiceTemplateReaderService readerService = + new ServiceTemplateReaderServiceImpl(fileContent); + convertMetadata(serviceTemplateName, serviceTemplate, readerService); + convertToscaVersion(serviceTemplate, readerService); + convertImports(serviceTemplate); + convertNodeTypes(serviceTemplate, readerService); + convertTopologyTemplate(serviceTemplate, readerService); + + } catch (YAMLException ye) { + throw new CoreException(new ErrorCode.ErrorCodeBuilder() + .withMessage("Invalid YAML content in file" + serviceTemplateName) + .withCategory(ErrorCategory.APPLICATION).build(), ye); + } + + + return serviceTemplate; + } + + private void convertToscaVersion(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Object toscaVersion = readerService.getToscaVersion(); + serviceTemplate.setTosca_definitions_version((String) toscaVersion); + } + + private void convertImports(ServiceTemplate serviceTemplate) { + serviceTemplate.setImports(new ArrayList<>()); + serviceTemplate.getImports() + .add(createImportMap(openecompHeatIndex, HEAT_INDEX_IMPORT_FILE)); + serviceTemplate.getImports().add(createImportMap(ONAP_INDEX, ONAP_INDEX_IMPORT_FILE)); + serviceTemplate.getImports().add(createImportMap(globalSubstitution, globalStName)); + + } + + private Map createImportMap(String key, String fileName) { + Map importMap = new HashMap<>(); + Import anImport = new Import(); + anImport.setFile(fileName); + importMap.put(key, anImport); + + return importMap; + } + + private void convertMetadata(String serviceTemplateName, + ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map metadataToConvert = (Map) readerService.getMetadata(); + Map finalMetadata = new HashMap<>(); + + if (MapUtils.isNotEmpty(metadataToConvert)) { + for (Map.Entry metadataEntry : metadataToConvert.entrySet()) { + if (Objects.isNull(metadataEntry.getValue()) || + !(metadataEntry.getValue() instanceof String)) { + continue; + } + finalMetadata.put(metadataEntry.getKey(), (String) metadataEntry.getValue()); + } + } + + finalMetadata.put("template_name", getTemplateNameFromStName(serviceTemplateName)); + serviceTemplate.setMetadata(finalMetadata); + } + + private void convertNodeTypes(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { + Map nodeTypes = readerService.getNodeTypes(); + if (MapUtils.isEmpty(nodeTypes)) { + return; + } + + for (Map.Entry nodeTypeEntry : nodeTypes.entrySet()) { + Optional nodeType = ToscaConverterUtil + .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), + NodeType.class); + + nodeType.ifPresent(nodeTypeValue -> DataModelUtil + .addNodeType(serviceTemplate, nodeTypeEntry.getKey(), nodeTypeValue)); + } + } + + private void convertTopologyTemplate(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + + convertInputs(serviceTemplate, readerService); + convertNodeTemplates(serviceTemplate, readerService); + convertOutputs(serviceTemplate, readerService); + convertSubstitutionMappings(serviceTemplate, readerService); + } + + private void convertInputs(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map inputs = readerService.getInputs(); + addInputsOrOutputsToServiceTemplate(serviceTemplate, inputs, Constants.inputs); + } + + private void convertOutputs(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map outputs = readerService.getOutputs(); + addInputsOrOutputsToServiceTemplate(serviceTemplate, outputs, Constants.outputs); + } + + private void addInputsOrOutputsToServiceTemplate(ServiceTemplate serviceTemplate, + Map mapToConvert, + String inputsOrOutputs) { + if (MapUtils.isEmpty(mapToConvert)) { + return; + } + + for (Map.Entry entry : mapToConvert.entrySet()) { + Optional parameterDefinition = + ToscaConverterUtil.createObjectFromClass( + entry.getKey(), entry.getValue(), ParameterDefinition.class); + + parameterDefinition.ifPresent(parameterDefinitionValue -> { + Optional defaultValue = + ToscaConverterUtil.getDefaultValue(entry.getValue(), parameterDefinition.get()); + defaultValue.ifPresent(parameterDefinitionValue::set_default); + addToServiceTemplateAccordingToSection( + serviceTemplate, inputsOrOutputs, entry.getKey(), parameterDefinition.get()); + }); + } + } + + private void addToServiceTemplateAccordingToSection(ServiceTemplate serviceTemplate, + String inputsOrOutputs, + String parameterId, + ParameterDefinition parameterDefinition) { + if (inputsOrOutputs.equals(inputs)) { + DataModelUtil + .addInputParameterToTopologyTemplate(serviceTemplate, parameterId, parameterDefinition); + } else if (inputsOrOutputs.equals(outputs)) { + DataModelUtil + .addOutputParameterToTopologyTemplate(serviceTemplate, parameterId, parameterDefinition); + } + } + + private void convertNodeTemplates(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map nodeTemplates = readerService.getNodeTemplates(); + if (MapUtils.isEmpty(nodeTemplates)) { + return; + } + + for (Map.Entry nodeTemplateEntry : nodeTemplates.entrySet()) { + NodeTemplate nodeTemplate = convertNodeTemplate(nodeTemplateEntry.getValue()); + DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), nodeTemplate); + } + } + + private void convertSubstitutionMappings(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map substitutionMappings = readerService.getSubstitutionMappings(); + if (MapUtils.isEmpty(substitutionMappings)) { + return; + } + SubstitutionMapping substitutionMapping = convertSubstitutionMappings(substitutionMappings); + DataModelUtil.addSubstitutionMapping(serviceTemplate, substitutionMapping); + } + + private SubstitutionMapping convertSubstitutionMappings( + Map substitutionMappings) { + SubstitutionMapping substitutionMapping = new SubstitutionMapping(); + + substitutionMapping.setNode_type((String) substitutionMappings.get(nodeType)); + substitutionMapping.setCapabilities( + convertSubstitutionMappingsSections(capabilities, substitutionMappings.get(capabilities))); + substitutionMapping.setRequirements( + convertSubstitutionMappingsSections(requirements, substitutionMappings.get(requirements))); + + return substitutionMapping; + } + + private Map> convertSubstitutionMappingsSections(String sectionName, + Object sectionToConvert) { + + if (Objects.isNull(sectionToConvert)) { + return null; + } + + if (!(sectionToConvert instanceof Map)) { + throw new CoreException( + new SubstitutionMappingsConverterErrorBuilder( + sectionName, sectionToConvert.getClass().getSimpleName()).build()); + } + + return convertSection(sectionToConvert); + } + + private Map> convertSection(Object sectionToConvert) { + + Map sectionAsMap = (Map) sectionToConvert; + Map> convertedSection = new HashMap<>(); + + if (MapUtils.isEmpty(sectionAsMap)) { + return null; + } + + for (Map.Entry entry : sectionAsMap.entrySet()) { + if (entry.getValue() instanceof List) { + convertedSection.put(entry.getKey(), (List) entry.getValue()); + } + } + + return convertedSection; + } + + protected CsarFileTypes getFileType(String fileName){ + if (isMainServiceTemplate(fileName)) { + return CsarFileTypes.mainServiceTemplate; + } else if (isGlobalServiceTemplate(fileName)) { + return CsarFileTypes.globalServiceTemplate; + } else if (isDefinitions(fileName)) { + return CsarFileTypes.definitionsFile; + } else if (isMetadataFile(fileName)) { + return CsarFileTypes.toscaMetadata; + } + return CsarFileTypes.externalFile; + } + + private NodeTemplate convertNodeTemplate(Object candidateNodeTemplate) { + NodeTemplate nodeTemplate = new NodeTemplate(); + + Map nodeTemplateAsMap = (Map) candidateNodeTemplate; + nodeTemplate.setArtifacts((Map) nodeTemplateAsMap.get("artifacts")); + nodeTemplate.setAttributes((Map) nodeTemplateAsMap.get("attributes")); + nodeTemplate.setCopy((String) nodeTemplateAsMap.get("copy")); + nodeTemplate.setDescription((String) nodeTemplateAsMap.get("description")); + nodeTemplate.setDirectives((List) nodeTemplateAsMap.get("directives")); + nodeTemplate.setInterfaces( + (Map) nodeTemplateAsMap.get("interfaces")); + nodeTemplate.setNode_filter((NodeFilter) nodeTemplateAsMap.get("node_filter")); + nodeTemplate.setProperties((Map) nodeTemplateAsMap.get("properties")); + nodeTemplate.setRequirements( + (List>) nodeTemplateAsMap.get("requirements")); + nodeTemplate.setType((String) nodeTemplateAsMap.get("type")); + nodeTemplate.setCapabilities( + convertCapabilities((Map) nodeTemplateAsMap.get("capabilities"))); + + return nodeTemplate; + } + + private Map convertCapabilities(Map capabilities) { + if (MapUtils.isEmpty(capabilities)) { + return null; + } + + Map convertedCapabilities = new HashMap<>(); + for (Map.Entry capabilityAssignmentEntry : capabilities.entrySet()) { + Optional capabilityAssignment = ToscaConverterUtil.createObjectFromClass + (capabilityAssignmentEntry.getKey(), capabilityAssignmentEntry.getValue(), + CapabilityAssignment.class); + + capabilityAssignment.ifPresent(capabilityAssignmentValue -> + convertedCapabilities.put(capabilityAssignmentEntry.getKey(), capabilityAssignmentValue)); + + } + return convertedCapabilities; + } + + + protected boolean isMainServiceTemplate(String fileName) { + return fileName.endsWith(mainStName); + } + + protected boolean isMetadataFile(String fileName) { + return fileName.equals(TOSCA_META_PATH_FILE_NAME); + } + + protected boolean isGlobalServiceTemplate(String fileName) { + return fileName.endsWith(globalStName); + } + + protected boolean isDefinitions(String fileName) { + return fileName.startsWith(definitionsDir); + } + + private String getTemplateNameFromStName(String serviceTemplateName) { + String fileNameWithoutDirectories = getFileNameWithoutDirectories(serviceTemplateName); + return fileNameWithoutDirectories.split("ServiceTemplate")[0]; + } + + private String getFileNameWithoutDirectories(String serviceTemplateName) { + String fileNameWithoutDirectories; + if (serviceTemplateName.contains("/")) { + String[] split = serviceTemplateName.split("/"); + fileNameWithoutDirectories = split[split.length - 1]; + } else if (serviceTemplateName.contains(File.separator)) { + String[] split = serviceTemplateName.split(Pattern.quote(File.separator)); + fileNameWithoutDirectories = split[split.length - 1]; + } else { + fileNameWithoutDirectories = serviceTemplateName; + } + return fileNameWithoutDirectories; + } +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java index 2d4acdc87f..01738c1f8a 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java @@ -16,479 +16,54 @@ package org.openecomp.core.impl; -import org.apache.commons.collections.MapUtils; -import org.onap.sdc.tosca.datatypes.model.ArtifactDefinition; -import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment; -import org.onap.sdc.tosca.datatypes.model.Import; -import org.onap.sdc.tosca.datatypes.model.NodeFilter; -import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.onap.sdc.tosca.datatypes.model.NodeType; -import org.onap.sdc.tosca.datatypes.model.ParameterDefinition; -import org.onap.sdc.tosca.datatypes.model.RequirementAssignment; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping; -import org.openecomp.core.converter.ServiceTemplateReaderService; -import org.openecomp.core.converter.ToscaConverter; -import org.openecomp.core.converter.datatypes.Constants; import org.openecomp.core.converter.datatypes.CsarFileTypes; -import org.openecomp.core.converter.errors.SubstitutionMappingsConverterErrorBuilder; -import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCategory; -import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; -import org.openecomp.sdc.tosca.services.DataModelUtil; -import org.openecomp.sdc.tosca.services.ToscaUtil; -import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator; -import org.yaml.snakeyaml.error.YAMLException; -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.regex.Pattern; -import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX; -import static org.openecomp.core.converter.datatypes.Constants.capabilities; -import static org.openecomp.core.converter.datatypes.Constants.inputs; -import static org.openecomp.core.converter.datatypes.Constants.nodeType; -import static org.openecomp.core.converter.datatypes.Constants.definitionsDir; import static org.openecomp.core.converter.datatypes.Constants.globalStName; -import static org.openecomp.core.converter.datatypes.Constants.globalSubstitution; import static org.openecomp.core.converter.datatypes.Constants.mainStName; -import static org.openecomp.core.converter.datatypes.Constants.openecompHeatIndex; -import static org.openecomp.core.converter.datatypes.Constants.outputs; -import static org.openecomp.core.converter.datatypes.Constants.requirements; -import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME; -import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.HEAT_INDEX_IMPORT_FILE; -import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.ONAP_INDEX_IMPORT_FILE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; -public class ToscaConverterImpl implements ToscaConverter { +public class ToscaConverterImpl extends AbstractToscaConverter { @Override public ToscaServiceModel convert(FileContentHandler fileContentHandler) { - Map csarFiles = new HashMap<>(fileContentHandler.getFiles()); - ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); - Map serviceTemplates = new HashMap<>(); - FileContentHandler artifacts = new FileContentHandler(); - GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate(); - for (Map.Entry fileEntry : csarFiles.entrySet()) { - CsarFileTypes fileType = getFileType(fileEntry.getKey()); - switch (fileType) { - case mainServiceTemplate: - handleServiceTemplate(mainStName, fileEntry.getKey(), csarFiles, serviceTemplates); - break; - - case globalServiceTemplate: - handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates); - break; - - case externalFile: - artifacts.addFile( - getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue()); - break; - - case definitionsFile: - handleDefintionTemplate(fileEntry.getKey(), csarFiles, gsst); - break; - - default: - break; - } - } - handleMetadataFile(csarFiles); - updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles); - return toscaServiceModel; - } - - private void handleMetadataFile(Map csarFiles) { - byte[] bytes = csarFiles.remove(TOSCA_META_PATH_FILE_NAME); - if (bytes != null) { - csarFiles.put(TOSCA_META_ORIG_PATH_FILE_NAME, bytes); - } - } - - private void handleDefintionTemplate(String key, Map csarFiles, - GlobalSubstitutionServiceTemplate gsst) { - try { - ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(key)); - Object nodeTypes = readerService.getNodeTypes(); - if (nodeTypes instanceof Map) { - Map nodeTypeMap = (Map) nodeTypes; - gsst.appendNodes(nodeTypeMap); - } - } catch (YAMLException ye) { - throw new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage("Invalid YAML content in file " + key) - .withCategory(ErrorCategory.APPLICATION).build(), ye); - } - } - - private String getConcreteArtifactFileName(String fileName){ - int artifactIndex = fileName.indexOf(CsarFileTypes.Artifacts.name()); - if(artifactIndex < 0){ - return fileName; - } - - int artifactDirectoryIndex = - artifactIndex + CsarFileTypes.Artifacts.name().length() + 1; - return fileName.substring(artifactDirectoryIndex); - } - - private void updateToscaServiceModel(ToscaServiceModel toscaServiceModel, - Map serviceTemplates, - FileContentHandler externalFilesHandler, - GlobalSubstitutionServiceTemplate globalSubstitutionServiceTemplate, - Map csarFiles) { - Collection globalServiceTemplates = - GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR).values(); - addGlobalServiceTemplates(globalServiceTemplates, serviceTemplates); - toscaServiceModel.setEntryDefinitionServiceTemplate(mainStName); - toscaServiceModel.setServiceTemplates(serviceTemplates); - externalFilesHandler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, - csarFiles.get(TOSCA_META_ORIG_PATH_FILE_NAME)); - toscaServiceModel.setArtifactFiles(externalFilesHandler); - - if(MapUtils.isNotEmpty(globalSubstitutionServiceTemplate.getNode_types())) { - serviceTemplates - .put(GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME, globalSubstitutionServiceTemplate); - } - } - - private void addGlobalServiceTemplates(Collection globalServiceTemplates, - Map serviceTemplates) { - for (ServiceTemplate serviceTemplate : globalServiceTemplates) { - serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate); - } - } - - private void handleServiceTemplate(String serviceTemplateName, - String fileName, Map csarFiles, - Map serviceTemplates) { - Optional serviceTemplate = - getServiceTemplateFromCsar(fileName, csarFiles); - serviceTemplate.ifPresent( - serviceTemplateValue -> addServiceTemplate(serviceTemplateName, serviceTemplateValue, - serviceTemplates)); - } - - private void addServiceTemplate(String serviceTemplateName, - ServiceTemplate serviceTemplate, - Map serviceTemplates) { - serviceTemplates.put(serviceTemplateName, serviceTemplate); - } - - private Optional getServiceTemplateFromCsar(String fileName, - Map csarFiles) { - byte[] fileContent = csarFiles.get(fileName); - ServiceTemplate serviceTemplate = convertServiceTemplate(fileName, fileContent); - - return Optional.of(serviceTemplate); - } - - private ServiceTemplate convertServiceTemplate(String serviceTemplateName, - byte[] fileContent) { - ServiceTemplate serviceTemplate = new ServiceTemplate(); - try { - ServiceTemplateReaderService readerService = - new ServiceTemplateReaderServiceImpl(fileContent); - convertMetadata(serviceTemplateName, serviceTemplate, readerService); - convertToscaVersion(serviceTemplate, readerService); - convertImports(serviceTemplate); - convertNodeTypes(serviceTemplate, readerService); - convertTopologyTemplate(serviceTemplate, readerService); - - } catch (YAMLException ye) { - throw new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage("Invalid YAML content in file" + serviceTemplateName) - .withCategory(ErrorCategory.APPLICATION).build(), ye); - } - - - return serviceTemplate; - } - - private void convertToscaVersion(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Object toscaVersion = readerService.getToscaVersion(); - serviceTemplate.setTosca_definitions_version((String) toscaVersion); - } - - private void convertImports(ServiceTemplate serviceTemplate) { - serviceTemplate.setImports(new ArrayList<>()); - serviceTemplate.getImports() - .add(createImportMap(openecompHeatIndex, HEAT_INDEX_IMPORT_FILE)); - serviceTemplate.getImports().add(createImportMap(ONAP_INDEX, ONAP_INDEX_IMPORT_FILE)); - serviceTemplate.getImports().add(createImportMap(globalSubstitution, globalStName)); - - } - - private Map createImportMap(String key, String fileName) { - Map importMap = new HashMap<>(); - Import anImport = new Import(); - anImport.setFile(fileName); - importMap.put(key, anImport); - - return importMap; - } - - private void convertMetadata(String serviceTemplateName, - ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map metadataToConvert = (Map) readerService.getMetadata(); - Map finalMetadata = new HashMap<>(); - - if (MapUtils.isNotEmpty(metadataToConvert)) { - for (Map.Entry metadataEntry : metadataToConvert.entrySet()) { - if (Objects.isNull(metadataEntry.getValue()) || - !(metadataEntry.getValue() instanceof String)) { - continue; - } - finalMetadata.put(metadataEntry.getKey(), (String) metadataEntry.getValue()); - } - } - - finalMetadata.put("template_name", getTemplateNameFromStName(serviceTemplateName)); - serviceTemplate.setMetadata(finalMetadata); - } - - private void convertNodeTypes(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { - Map nodeTypes = readerService.getNodeTypes(); - if (MapUtils.isEmpty(nodeTypes)) { - return; - } - - for (Map.Entry nodeTypeEntry : nodeTypes.entrySet()) { - Optional nodeType = ToscaConverterUtil - .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), - NodeType.class); - - nodeType.ifPresent(nodeTypeValue -> DataModelUtil - .addNodeType(serviceTemplate, nodeTypeEntry.getKey(), nodeTypeValue)); - } - } - - private void convertTopologyTemplate(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - - convertInputs(serviceTemplate, readerService); - convertNodeTemplates(serviceTemplate, readerService); - convertOutputs(serviceTemplate, readerService); - convertSubstitutionMappings(serviceTemplate, readerService); - } - - private void convertInputs(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map inputs = readerService.getInputs(); - addInputsOrOutputsToServiceTemplate(serviceTemplate, inputs, Constants.inputs); - } - - private void convertOutputs(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map outputs = readerService.getOutputs(); - addInputsOrOutputsToServiceTemplate(serviceTemplate, outputs, Constants.outputs); - } - - private void addInputsOrOutputsToServiceTemplate(ServiceTemplate serviceTemplate, - Map mapToConvert, - String inputsOrOutputs) { - if (MapUtils.isEmpty(mapToConvert)) { - return; - } - - for (Map.Entry entry : mapToConvert.entrySet()) { - Optional parameterDefinition = - ToscaConverterUtil.createObjectFromClass( - entry.getKey(), entry.getValue(), ParameterDefinition.class); - - parameterDefinition.ifPresent(parameterDefinitionValue -> { - Optional defaultValue = - ToscaConverterUtil.getDefaultValue(entry.getValue(), parameterDefinition.get()); - defaultValue.ifPresent(parameterDefinitionValue::set_default); - addToServiceTemplateAccordingToSection( - serviceTemplate, inputsOrOutputs, entry.getKey(), parameterDefinition.get()); - }); - } - } - - private void addToServiceTemplateAccordingToSection(ServiceTemplate serviceTemplate, - String inputsOrOutputs, - String parameterId, - ParameterDefinition parameterDefinition) { - if (inputsOrOutputs.equals(inputs)) { - DataModelUtil - .addInputParameterToTopologyTemplate(serviceTemplate, parameterId, parameterDefinition); - } else if (inputsOrOutputs.equals(outputs)) { - DataModelUtil - .addOutputParameterToTopologyTemplate(serviceTemplate, parameterId, parameterDefinition); - } - } - - private void convertNodeTemplates(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map nodeTemplates = readerService.getNodeTemplates(); - if (MapUtils.isEmpty(nodeTemplates)) { - return; - } - - for (Map.Entry nodeTemplateEntry : nodeTemplates.entrySet()) { - NodeTemplate nodeTemplate = convertNodeTemplate(nodeTemplateEntry.getValue()); - DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), nodeTemplate); - } - } - - private void convertSubstitutionMappings(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map substitutionMappings = readerService.getSubstitutionMappings(); - if (MapUtils.isEmpty(substitutionMappings)) { - return; - } - SubstitutionMapping substitutionMapping = convertSubstitutionMappings(substitutionMappings); - DataModelUtil.addSubstitutionMapping(serviceTemplate, substitutionMapping); - } - - private SubstitutionMapping convertSubstitutionMappings( - Map substitutionMappings) { - SubstitutionMapping substitutionMapping = new SubstitutionMapping(); - - substitutionMapping.setNode_type((String) substitutionMappings.get(nodeType)); - substitutionMapping.setCapabilities( - convertSubstitutionMappingsSections(capabilities, substitutionMappings.get(capabilities))); - substitutionMapping.setRequirements( - convertSubstitutionMappingsSections(requirements, substitutionMappings.get(requirements))); - - return substitutionMapping; - } - - private Map> convertSubstitutionMappingsSections(String sectionName, - Object sectionToConvert) { - - if (Objects.isNull(sectionToConvert)) { - return null; - } - - if (!(sectionToConvert instanceof Map)) { - throw new CoreException( - new SubstitutionMappingsConverterErrorBuilder( - sectionName, sectionToConvert.getClass().getSimpleName()).build()); - } - - return convertSection(sectionToConvert); - } - - private Map> convertSection(Object sectionToConvert) { - - Map sectionAsMap = (Map) sectionToConvert; - Map> convertedSection = new HashMap<>(); - - if (MapUtils.isEmpty(sectionAsMap)) { - return null; - } - - for (Map.Entry entry : sectionAsMap.entrySet()) { - if (entry.getValue() instanceof List) { - convertedSection.put(entry.getKey(), (List) entry.getValue()); + Map csarFiles = new HashMap<>(fileContentHandler.getFiles()); + ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); + Map serviceTemplates = new HashMap<>(); + FileContentHandler artifacts = new FileContentHandler(); + GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate(); + csarFiles.putAll(fileContentHandler.getFiles()); + for (Map.Entry fileEntry : csarFiles.entrySet()) { + CsarFileTypes fileType = getFileType(fileEntry.getKey()); + switch (fileType) { + case mainServiceTemplate: + handleServiceTemplate(mainStName, fileEntry.getKey(), csarFiles, serviceTemplates); + break; + + case globalServiceTemplate: + handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates); + break; + + case externalFile: + artifacts.addFile( + getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue()); + break; + + case definitionsFile: + handleDefintionTemplate(fileEntry.getKey(), csarFiles, gsst); + break; + + default: + break; + } } + handleMetadataFile(csarFiles); + updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, mainStName); + return toscaServiceModel; } - return convertedSection; - } - - private CsarFileTypes getFileType(String fileName) { - if (isMainServiceTemplate(fileName)) { - return CsarFileTypes.mainServiceTemplate; - } else if (isGlobalServiceTemplate(fileName)) { - return CsarFileTypes.globalServiceTemplate; - } else if (isDefinitions(fileName)) { - return CsarFileTypes.definitionsFile; - } else if (isMetadataFile(fileName)) { - return CsarFileTypes.toscaMetadata; - } - return CsarFileTypes.externalFile; - } - - private NodeTemplate convertNodeTemplate(Object candidateNodeTemplate) { - NodeTemplate nodeTemplate = new NodeTemplate(); - - Map nodeTemplateAsMap = (Map) candidateNodeTemplate; - nodeTemplate.setArtifacts((Map) nodeTemplateAsMap.get("artifacts")); - nodeTemplate.setAttributes((Map) nodeTemplateAsMap.get("attributes")); - nodeTemplate.setCopy((String) nodeTemplateAsMap.get("copy")); - nodeTemplate.setDescription((String) nodeTemplateAsMap.get("description")); - nodeTemplate.setDirectives((List) nodeTemplateAsMap.get("directives")); - nodeTemplate.setInterfaces( - (Map) nodeTemplateAsMap.get("interfaces")); - nodeTemplate.setNode_filter((NodeFilter) nodeTemplateAsMap.get("node_filter")); - nodeTemplate.setProperties((Map) nodeTemplateAsMap.get("properties")); - nodeTemplate.setRequirements( - (List>) nodeTemplateAsMap.get("requirements")); - nodeTemplate.setType((String) nodeTemplateAsMap.get("type")); - nodeTemplate.setCapabilities( - convertCapabilities((Map) nodeTemplateAsMap.get("capabilities"))); - - return nodeTemplate; - } - - private Map convertCapabilities(Map capabilities) { - if (MapUtils.isEmpty(capabilities)) { - return null; - } - - Map convertedCapabilities = new HashMap<>(); - for (Map.Entry capabilityAssignmentEntry : capabilities.entrySet()) { - Optional capabilityAssignment = ToscaConverterUtil.createObjectFromClass - (capabilityAssignmentEntry.getKey(), capabilityAssignmentEntry.getValue(), - CapabilityAssignment.class); - - capabilityAssignment.ifPresent(capabilityAssignmentValue -> - convertedCapabilities.put(capabilityAssignmentEntry.getKey(), capabilityAssignmentValue)); - - } - return convertedCapabilities; - } - - - private boolean isMainServiceTemplate(String fileName) { - return fileName.endsWith(mainStName); - } - - private boolean isMetadataFile(String fileName) { - return fileName.equals(TOSCA_META_PATH_FILE_NAME); - } - - private boolean isGlobalServiceTemplate(String fileName) { - return fileName.endsWith(globalStName); - } - - private boolean isDefinitions(String fileName) { - return fileName.startsWith(definitionsDir); - } - - private String getTemplateNameFromStName(String serviceTemplateName) { - String fileNameWithoutDirectories; - fileNameWithoutDirectories = getFileNameWithoutDirectories(serviceTemplateName); - return fileNameWithoutDirectories.split("ServiceTemplate")[0]; - } - - private String getFileNameWithoutDirectories(String serviceTemplateName) { - String fileNameWithoutDirectories; - if (serviceTemplateName.contains("/")) { - String[] split = serviceTemplateName.split("/"); - fileNameWithoutDirectories = split[split.length - 1]; - } else if (serviceTemplateName.contains(File.separator)) { - String[] split = serviceTemplateName.split(Pattern.quote(File.separator)); - fileNameWithoutDirectories = split[split.length - 1]; - } else { - fileNameWithoutDirectories = serviceTemplateName; - } - return fileNameWithoutDirectories; - } } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterImpl.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterImpl.java new file mode 100644 index 0000000000..78a23f43ea --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterImpl.java @@ -0,0 +1,131 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.core.impl; + +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata; +import org.openecomp.sdc.tosca.csar.ToscaMetadata; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.openecomp.core.converter.datatypes.Constants.globalStName; +import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBUTES; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; + +public class ToscaSolConverterImpl extends AbstractToscaConverter { + + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaSolConverterImpl.class); + private final Set handledDefinitionFilesList = new HashSet<>(); + + @Override + public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException { + Map csarFiles = new HashMap<>(fileContentHandler.getFiles()); + ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); + Map serviceTemplates = new HashMap<>(); + FileContentHandler artifacts = new FileContentHandler(); + GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate(); + String mServiceDefinitionFileName = getMainServiceDefinitionFileName(fileContentHandler); + handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionFileName); + handleExternalArtifacts(csarFiles, serviceTemplates, artifacts); + handleMetadataFile(csarFiles); + updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, mServiceDefinitionFileName); + return toscaServiceModel; + } + + private void handleMainServiceTemplate(Map csarFiles, Map serviceTemplates, + GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) { + handleServiceTemplate(mServiceDefinitionFileName, mServiceDefinitionFileName, csarFiles, serviceTemplates); + handleImportDefinitions(mServiceDefinitionFileName, csarFiles, mServiceDefinitionFileName.substring(0, + mServiceDefinitionFileName.lastIndexOf("/")), gsst); + } + + private void handleExternalArtifacts(Map csarFiles, Map serviceTemplates, FileContentHandler artifacts) { + for(Map.Entry fileEntry: csarFiles.entrySet()){ + if(!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())){ + if(isGlobalServiceTemplate(fileEntry.getKey())){ + handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates); + }else{ + artifacts.addFile( + getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue()); + } + } + } + } + + private void handleImportDefinitions(String fileName, Map csarFiles, String parentDir, GlobalSubstitutionServiceTemplate gsst) { + handledDefinitionFilesList.add(fileName); + ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(fileName)); + List imports = (readerService).getImports(); + for (Object o : imports) { + String importPath = getImportedFilePath(o, parentDir); + if (importPath != null && !handledDefinitionFilesList.contains(importPath)) { + handleDefintionTemplate(importPath, csarFiles, gsst); + if (importPath.contains("/")) { + parentDir = importPath.substring(0, importPath.lastIndexOf("/")); + } + handleImportDefinitions(importPath, csarFiles, parentDir, gsst); + } + } + return; + } + + private String getImportedFilePath(Object o, String parentDir){ + if(o instanceof String){ + String fileName = (String) o; + if(!fileName.contains("/")){ + fileName = parentDir + "/" + fileName; + } + return fileName; + }else if(o instanceof Map){ + Map o1 = (Map) o; + for(Map.Entry entry: o1.entrySet()){ + if(NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))){ + getImportedFilePath(entry.getValue(), parentDir); + } + } + } + return null; + } + + private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException { + try { + ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile( + contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME)); + return toscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS); + }catch (IOException e){ + LOGGER.error(e.getMessage(), e); + throw new IOException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterImplTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterImplTest.java new file mode 100644 index 0000000000..ad2e61523f --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterImplTest.java @@ -0,0 +1,105 @@ +/* + * - + * * ============LICENSE_START======================================================= + * * Copyright (C) 2019 Nordix Foundation. + * * ================================================================================ + * * 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. + * * + * * SPDX-License-Identifier: Apache-2.0 + * * ============LICENSE_END========================================================= + * + */ + +package org.openecomp.core.impl; + +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; + + +public class ToscaSolConverterImplTest { + + private ToscaSolConverterImpl toscaSolConverter; + private FileContentHandler fileContentHandler; + + @Before + public void setUp(){ + toscaSolConverter = new ToscaSolConverterImpl(); + fileContentHandler = new FileContentHandler(); + } + + + @Test + public void testGivenSOL004WithMetadataDirectoryPackage_whenToscaSolConverterIsCalled_validToscaServiceModelIsReturned() + throws IOException{ + fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", + ("TOSCA-Meta-File-Version: 1.0\n " + + "CSAR-Version: 1.1\n" + + "Created-by: Ericsson\n" + + "Entry-Definitions: Definitions/Main.yaml\n" + + "Entry-Manifest: Main.mf\n" + + "Entry-Change-Log: Artifacts/ChangeLog.txt") + .getBytes(StandardCharsets.UTF_8)); + + fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/Main.yaml")); + fileContentHandler.addFile("Main.mf", "".getBytes()); + fileContentHandler.addFile("Definitions/sample_import1.yaml", getFileResource("/toscaSOlConverter/sample_import1.yaml")); + fileContentHandler.addFile("Definitions/sample_import2.yaml", getFileResource("/toscaSOlConverter/sample_import2.yaml")); + fileContentHandler.addFile("Artifacts/sample_import3.yaml", getFileResource("/toscaSOlConverter/sample_import3.yaml")); + fileContentHandler.addFile("Artifacts/sample_import4.yaml", getFileResource("/toscaSOlConverter/sample_import4.yaml")); + ToscaServiceModel toscaServiceModel = toscaSolConverter.convert(fileContentHandler); + FileContentHandler contentHandler = toscaServiceModel.getArtifactFiles(); + Map serviceTemplateMap = toscaServiceModel.getServiceTemplates(); + String entryDefinitionTemplateName = toscaServiceModel.getEntryDefinitionServiceTemplate(); + Assert.assertTrue("Artifacts should contain external files", contentHandler.containsFile("Main.mf")); + Assert.assertTrue("Main service template should exist", serviceTemplateMap.containsKey("Definitions/Main.yaml")); + Assert.assertEquals("Entry Definition name should be same as passed in TOSCA.meta", + "Definitions/Main.yaml", entryDefinitionTemplateName); + } + + @Test(expected = IOException.class) + public void testGivenMetaFileDoesNotExist_thenAnExceptionIsThrown() throws IOException{ + toscaSolConverter.convert(fileContentHandler); + } + + @Test(expected = CoreException.class) + public void testGivenInvalidServiceTemplate_thenAnExceptionIsThrown() throws IOException{ + + fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", + ("TOSCA-Meta-File-Version: 1.0\n " + + "CSAR-Version: 1.1\n" + + "Created-by: Ericsson\n" + + "Entry-Definitions: Definitions/Main.yaml\n" + + "Entry-Manifest: Main.mf\n" + + "Entry-Change-Log: Artifacts/ChangeLog.txt") + .getBytes(StandardCharsets.UTF_8)); + + fileContentHandler.addFile("Definitions/Main.yaml", getFileResource("/toscaSOlConverter/invalidMainService.yaml")); + toscaSolConverter.convert(fileContentHandler); + } + + private byte[] getFileResource(String filePath) throws IOException { + InputStream inputStream = ClassLoader.class.getClass().getResourceAsStream(filePath); + return IOUtils.toByteArray(inputStream); + } + +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/Main.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/Main.yaml new file mode 100644 index 0000000000..57b7724a8d --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/Main.yaml @@ -0,0 +1,9 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 +metadata: + template_name: vCPE_vgw + template_version: "1.0" + template_author: onap +description: vCPE_vgw + +imports: + - sample_import1.yaml \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/invalidMainService.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/invalidMainService.yaml new file mode 100644 index 0000000000..569d20487a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/invalidMainService.yaml @@ -0,0 +1,7 @@ +: tosca_simple_yaml_1_0 + + template_name: vCPE_vgw + template_version: "1.0" + template_author: onap + +description: vCPE_vgw \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import1.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import1.yaml new file mode 100644 index 0000000000..47d9b00967 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import1.yaml @@ -0,0 +1,5 @@ +tosca_definitions_version: tosca_simple_yaml_1_2 +description: ETSI NFV SOL 001 pnfd types definitions version 2.5.1 + +imports: + - sample_import2.yaml \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import2.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import2.yaml new file mode 100644 index 0000000000..42816eeb73 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import2.yaml @@ -0,0 +1,6 @@ +tosca_definitions_version: tosca_simple_yaml_1_2 +description: ETSI NFV SOL 001 pnfd types definitions version 2.5.1 + +imports: + - sample_import2.yaml + - Artifacts/sample_import3.yaml \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import3.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import3.yaml new file mode 100644 index 0000000000..99eb46be4d --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import3.yaml @@ -0,0 +1,5 @@ +tosca_definitions_version: tosca_simple_yaml_1_2 +description: ETSI NFV SOL 001 pnfd types definitions version 2.5.1 + +imports: + - sample_import4.yaml \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import4.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import4.yaml new file mode 100644 index 0000000000..ccaa1cafbb --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/toscaSOlConverter/sample_import4.yaml @@ -0,0 +1,6 @@ +tosca_definitions_version: tosca_simple_yaml_1_2 +description: ETSI NFV SOL 001 pnfd types definitions version 2.5.1 + +imports: + - sample_import3.yaml + -- cgit 1.2.3-korg