diff options
author | bilal.iqbal <bilal.iqbal@est.tech> | 2019-03-19 19:16:03 +0000 |
---|---|---|
committer | Oren Kleks <orenkle@amdocs.com> | 2019-03-20 08:02:02 +0000 |
commit | 54b96133b33b3bf55e9670ed2a8c89996ac413b9 (patch) | |
tree | 5c1dd673a61938e68fb1f43f6199c847e2f4fe7a /openecomp-be/lib | |
parent | d0031501cd81a9046396358f42657f168b5a58a8 (diff) |
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 <bilal.iqbal@est.tech>
Diffstat (limited to 'openecomp-be/lib')
10 files changed, 773 insertions, 458 deletions
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<String, byte[]> 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<String, byte[]> csarFiles, + GlobalSubstitutionServiceTemplate gsst) { + try { + ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(key)); + Object nodeTypes = readerService.getNodeTypes(); + if (nodeTypes instanceof Map) { + Map<String, NodeType> nodeTypeMap = (Map<String, NodeType>) 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<String, ServiceTemplate> serviceTemplates, + FileContentHandler externalFilesHandler, + GlobalSubstitutionServiceTemplate globalSubstitutionServiceTemplate, + Map<String, byte[]> csarFiles, String entryDefinitionServiceTemplateName) { + Collection<ServiceTemplate> 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<ServiceTemplate> globalServiceTemplates, + Map<String, ServiceTemplate> serviceTemplates) { + for (ServiceTemplate serviceTemplate : globalServiceTemplates) { + serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate); + } + } + + protected void handleServiceTemplate(String serviceTemplateName, + String fileName, Map<String, byte[]> csarFiles, + Map<String, ServiceTemplate> serviceTemplates) { + Optional<ServiceTemplate> serviceTemplate = + getServiceTemplateFromCsar(fileName, csarFiles); + serviceTemplate.ifPresent( + serviceTemplateValue -> addServiceTemplate(serviceTemplateName, serviceTemplateValue, + serviceTemplates)); + } + + private void addServiceTemplate(String serviceTemplateName, + ServiceTemplate serviceTemplate, + Map<String, ServiceTemplate> serviceTemplates) { + serviceTemplates.put(serviceTemplateName, serviceTemplate); + } + + private Optional<ServiceTemplate> getServiceTemplateFromCsar(String fileName, + Map<String, byte[]> 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<String, Import> createImportMap(String key, String fileName) { + Map<String, Import> 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<String, Object> metadataToConvert = (Map<String, Object>) readerService.getMetadata(); + Map<String, String> finalMetadata = new HashMap<>(); + + if (MapUtils.isNotEmpty(metadataToConvert)) { + for (Map.Entry<String, Object> 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<String, Object> nodeTypes = readerService.getNodeTypes(); + if (MapUtils.isEmpty(nodeTypes)) { + return; + } + + for (Map.Entry<String, Object> nodeTypeEntry : nodeTypes.entrySet()) { + Optional<NodeType> 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<String, Object> inputs = readerService.getInputs(); + addInputsOrOutputsToServiceTemplate(serviceTemplate, inputs, Constants.inputs); + } + + private void convertOutputs(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map<String, Object> outputs = readerService.getOutputs(); + addInputsOrOutputsToServiceTemplate(serviceTemplate, outputs, Constants.outputs); + } + + private void addInputsOrOutputsToServiceTemplate(ServiceTemplate serviceTemplate, + Map<String, Object> mapToConvert, + String inputsOrOutputs) { + if (MapUtils.isEmpty(mapToConvert)) { + return; + } + + for (Map.Entry<String, Object> entry : mapToConvert.entrySet()) { + Optional<ParameterDefinition> parameterDefinition = + ToscaConverterUtil.createObjectFromClass( + entry.getKey(), entry.getValue(), ParameterDefinition.class); + + parameterDefinition.ifPresent(parameterDefinitionValue -> { + Optional<Object> 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<String, Object> nodeTemplates = readerService.getNodeTemplates(); + if (MapUtils.isEmpty(nodeTemplates)) { + return; + } + + for (Map.Entry<String, Object> nodeTemplateEntry : nodeTemplates.entrySet()) { + NodeTemplate nodeTemplate = convertNodeTemplate(nodeTemplateEntry.getValue()); + DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), nodeTemplate); + } + } + + private void convertSubstitutionMappings(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map<String, Object> substitutionMappings = readerService.getSubstitutionMappings(); + if (MapUtils.isEmpty(substitutionMappings)) { + return; + } + SubstitutionMapping substitutionMapping = convertSubstitutionMappings(substitutionMappings); + DataModelUtil.addSubstitutionMapping(serviceTemplate, substitutionMapping); + } + + private SubstitutionMapping convertSubstitutionMappings( + Map<String, Object> 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<String, List<String>> 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<String, List<String>> convertSection(Object sectionToConvert) { + + Map<String, Object> sectionAsMap = (Map<String, Object>) sectionToConvert; + Map<String, List<String>> convertedSection = new HashMap<>(); + + if (MapUtils.isEmpty(sectionAsMap)) { + return null; + } + + for (Map.Entry<String, Object> entry : sectionAsMap.entrySet()) { + if (entry.getValue() instanceof List) { + convertedSection.put(entry.getKey(), (List<String>) 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<String, Object> nodeTemplateAsMap = (Map<String, Object>) candidateNodeTemplate; + nodeTemplate.setArtifacts((Map<String, ArtifactDefinition>) nodeTemplateAsMap.get("artifacts")); + nodeTemplate.setAttributes((Map<String, Object>) nodeTemplateAsMap.get("attributes")); + nodeTemplate.setCopy((String) nodeTemplateAsMap.get("copy")); + nodeTemplate.setDescription((String) nodeTemplateAsMap.get("description")); + nodeTemplate.setDirectives((List<String>) nodeTemplateAsMap.get("directives")); + nodeTemplate.setInterfaces( + (Map<String, Object>) nodeTemplateAsMap.get("interfaces")); + nodeTemplate.setNode_filter((NodeFilter) nodeTemplateAsMap.get("node_filter")); + nodeTemplate.setProperties((Map<String, Object>) nodeTemplateAsMap.get("properties")); + nodeTemplate.setRequirements( + (List<Map<String, RequirementAssignment>>) nodeTemplateAsMap.get("requirements")); + nodeTemplate.setType((String) nodeTemplateAsMap.get("type")); + nodeTemplate.setCapabilities( + convertCapabilities((Map<String, Object>) nodeTemplateAsMap.get("capabilities"))); + + return nodeTemplate; + } + + private Map<String, CapabilityAssignment> convertCapabilities(Map<String, Object> capabilities) { + if (MapUtils.isEmpty(capabilities)) { + return null; + } + + Map<String, CapabilityAssignment> convertedCapabilities = new HashMap<>(); + for (Map.Entry<String, Object> capabilityAssignmentEntry : capabilities.entrySet()) { + Optional<CapabilityAssignment> 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<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles()); - ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); - Map<String, ServiceTemplate> serviceTemplates = new HashMap<>(); - FileContentHandler artifacts = new FileContentHandler(); - GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate(); - for (Map.Entry<String, byte[]> 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<String, byte[]> 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<String, byte[]> csarFiles, - GlobalSubstitutionServiceTemplate gsst) { - try { - ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(key)); - Object nodeTypes = readerService.getNodeTypes(); - if (nodeTypes instanceof Map) { - Map<String, NodeType> nodeTypeMap = (Map<String, NodeType>) 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<String, ServiceTemplate> serviceTemplates, - FileContentHandler externalFilesHandler, - GlobalSubstitutionServiceTemplate globalSubstitutionServiceTemplate, - Map<String, byte[]> csarFiles) { - Collection<ServiceTemplate> 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<ServiceTemplate> globalServiceTemplates, - Map<String, ServiceTemplate> serviceTemplates) { - for (ServiceTemplate serviceTemplate : globalServiceTemplates) { - serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate); - } - } - - private void handleServiceTemplate(String serviceTemplateName, - String fileName, Map<String, byte[]> csarFiles, - Map<String, ServiceTemplate> serviceTemplates) { - Optional<ServiceTemplate> serviceTemplate = - getServiceTemplateFromCsar(fileName, csarFiles); - serviceTemplate.ifPresent( - serviceTemplateValue -> addServiceTemplate(serviceTemplateName, serviceTemplateValue, - serviceTemplates)); - } - - private void addServiceTemplate(String serviceTemplateName, - ServiceTemplate serviceTemplate, - Map<String, ServiceTemplate> serviceTemplates) { - serviceTemplates.put(serviceTemplateName, serviceTemplate); - } - - private Optional<ServiceTemplate> getServiceTemplateFromCsar(String fileName, - Map<String, byte[]> 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<String, Import> createImportMap(String key, String fileName) { - Map<String, Import> 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<String, Object> metadataToConvert = (Map<String, Object>) readerService.getMetadata(); - Map<String, String> finalMetadata = new HashMap<>(); - - if (MapUtils.isNotEmpty(metadataToConvert)) { - for (Map.Entry<String, Object> 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<String, Object> nodeTypes = readerService.getNodeTypes(); - if (MapUtils.isEmpty(nodeTypes)) { - return; - } - - for (Map.Entry<String, Object> nodeTypeEntry : nodeTypes.entrySet()) { - Optional<NodeType> 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<String, Object> inputs = readerService.getInputs(); - addInputsOrOutputsToServiceTemplate(serviceTemplate, inputs, Constants.inputs); - } - - private void convertOutputs(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map<String, Object> outputs = readerService.getOutputs(); - addInputsOrOutputsToServiceTemplate(serviceTemplate, outputs, Constants.outputs); - } - - private void addInputsOrOutputsToServiceTemplate(ServiceTemplate serviceTemplate, - Map<String, Object> mapToConvert, - String inputsOrOutputs) { - if (MapUtils.isEmpty(mapToConvert)) { - return; - } - - for (Map.Entry<String, Object> entry : mapToConvert.entrySet()) { - Optional<ParameterDefinition> parameterDefinition = - ToscaConverterUtil.createObjectFromClass( - entry.getKey(), entry.getValue(), ParameterDefinition.class); - - parameterDefinition.ifPresent(parameterDefinitionValue -> { - Optional<Object> 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<String, Object> nodeTemplates = readerService.getNodeTemplates(); - if (MapUtils.isEmpty(nodeTemplates)) { - return; - } - - for (Map.Entry<String, Object> nodeTemplateEntry : nodeTemplates.entrySet()) { - NodeTemplate nodeTemplate = convertNodeTemplate(nodeTemplateEntry.getValue()); - DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), nodeTemplate); - } - } - - private void convertSubstitutionMappings(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map<String, Object> substitutionMappings = readerService.getSubstitutionMappings(); - if (MapUtils.isEmpty(substitutionMappings)) { - return; - } - SubstitutionMapping substitutionMapping = convertSubstitutionMappings(substitutionMappings); - DataModelUtil.addSubstitutionMapping(serviceTemplate, substitutionMapping); - } - - private SubstitutionMapping convertSubstitutionMappings( - Map<String, Object> 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<String, List<String>> 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<String, List<String>> convertSection(Object sectionToConvert) { - - Map<String, Object> sectionAsMap = (Map<String, Object>) sectionToConvert; - Map<String, List<String>> convertedSection = new HashMap<>(); - - if (MapUtils.isEmpty(sectionAsMap)) { - return null; - } - - for (Map.Entry<String, Object> entry : sectionAsMap.entrySet()) { - if (entry.getValue() instanceof List) { - convertedSection.put(entry.getKey(), (List<String>) entry.getValue()); + Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles()); + ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); + Map<String, ServiceTemplate> serviceTemplates = new HashMap<>(); + FileContentHandler artifacts = new FileContentHandler(); + GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate(); + csarFiles.putAll(fileContentHandler.getFiles()); + for (Map.Entry<String, byte[]> 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<String, Object> nodeTemplateAsMap = (Map<String, Object>) candidateNodeTemplate; - nodeTemplate.setArtifacts((Map<String, ArtifactDefinition>) nodeTemplateAsMap.get("artifacts")); - nodeTemplate.setAttributes((Map<String, Object>) nodeTemplateAsMap.get("attributes")); - nodeTemplate.setCopy((String) nodeTemplateAsMap.get("copy")); - nodeTemplate.setDescription((String) nodeTemplateAsMap.get("description")); - nodeTemplate.setDirectives((List<String>) nodeTemplateAsMap.get("directives")); - nodeTemplate.setInterfaces( - (Map<String, Object>) nodeTemplateAsMap.get("interfaces")); - nodeTemplate.setNode_filter((NodeFilter) nodeTemplateAsMap.get("node_filter")); - nodeTemplate.setProperties((Map<String, Object>) nodeTemplateAsMap.get("properties")); - nodeTemplate.setRequirements( - (List<Map<String, RequirementAssignment>>) nodeTemplateAsMap.get("requirements")); - nodeTemplate.setType((String) nodeTemplateAsMap.get("type")); - nodeTemplate.setCapabilities( - convertCapabilities((Map<String, Object>) nodeTemplateAsMap.get("capabilities"))); - - return nodeTemplate; - } - - private Map<String, CapabilityAssignment> convertCapabilities(Map<String, Object> capabilities) { - if (MapUtils.isEmpty(capabilities)) { - return null; - } - - Map<String, CapabilityAssignment> convertedCapabilities = new HashMap<>(); - for (Map.Entry<String, Object> capabilityAssignmentEntry : capabilities.entrySet()) { - Optional<CapabilityAssignment> 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<String> handledDefinitionFilesList = new HashSet<>(); + + @Override + public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException { + Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles()); + ToscaServiceModel toscaServiceModel = new ToscaServiceModel(); + Map<String, ServiceTemplate> 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<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, + GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) { + handleServiceTemplate(mServiceDefinitionFileName, mServiceDefinitionFileName, csarFiles, serviceTemplates); + handleImportDefinitions(mServiceDefinitionFileName, csarFiles, mServiceDefinitionFileName.substring(0, + mServiceDefinitionFileName.lastIndexOf("/")), gsst); + } + + private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) { + for(Map.Entry<String, byte[]> 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<String,byte[]> csarFiles, String parentDir, GlobalSubstitutionServiceTemplate gsst) { + handledDefinitionFilesList.add(fileName); + ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(fileName)); + List<Object> 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<String, Object> o1 = (Map) o; + for(Map.Entry<String, Object> 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<String, ServiceTemplate> 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 + |