diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib')
3 files changed, 201 insertions, 200 deletions
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 a5421c0be8..f8b9f75b8f 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 @@ -79,21 +79,24 @@ import java.util.regex.Pattern; public class ToscaConverterImpl implements ToscaConverter { private static final String ORIGINAL = ".original"; - - @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(); - for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) { - CsarFileTypes fileType = getFileType(fileEntry.getKey()); - switch (fileType) { - case mainServiceTemplate: - handleServiceTemplate(mainStName, fileEntry.getKey(), csarFiles, serviceTemplates); - break; + public ToscaConverterImpl() { + + } + + @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(); + 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); @@ -284,58 +287,58 @@ public class ToscaConverterImpl implements ToscaConverter { return; } - for (Map.Entry<String, Object> nodeTypeEntry : nodeTypes.entrySet()) { - Optional<NodeType> nodeType = ToscaConverterUtil - .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), - NodeType.class); + 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)); - } + nodeType.ifPresent(nodeTypeValue -> DataModelUtil + .addNodeType(serviceTemplate, nodeTypeEntry.getKey(), nodeTypeValue)); } + } - private void convertTopologyTemplate(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { + private void convertTopologyTemplate(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { - convertInputs(serviceTemplate, readerService); - convertNodeTemplates(serviceTemplate, readerService); - convertOutputs(serviceTemplate, readerService); - convertSubstitutionMappings(serviceTemplate, 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 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 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; } - 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); - 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()); - } ); - } + 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, @@ -350,177 +353,175 @@ public class ToscaConverterImpl implements ToscaConverter { } } - private void convertNodeTemplates(ServiceTemplate serviceTemplate, - ServiceTemplateReaderService readerService) { - Map<String, Object> nodeTemplates = readerService.getNodeTemplates(); - if (MapUtils.isEmpty(nodeTemplates)) { - return; - } + 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); - } + 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 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(); - 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))); - substitutionMapping.setNode_type((String) substitutionMappings.get(nodeType)); - substitutionMapping.setCapabilities( - convertSubstitutionMappingsSections(capabilities, substitutionMappings.get(capabilities))); - substitutionMapping.setRequirements( - convertSubstitutionMappingsSections(requirements, substitutionMappings.get(requirements))); + return substitutionMapping; + } - return substitutionMapping; + private Map<String, List<String>> convertSubstitutionMappingsSections(String sectionName, + Object sectionToConvert) { + + if (Objects.isNull(sectionToConvert)) { + return null; } - private Map<String, List<String>> convertSubstitutionMappingsSections(String sectionName, - Object sectionToConvert) { + if (!(sectionToConvert instanceof Map)) { + throw new CoreException( + new SubstitutionMappingsConverterErrorBuilder( + sectionName, sectionToConvert.getClass().getSimpleName()).build()); + } - if(Objects.isNull(sectionToConvert)){ - return null; - } + return convertSection(sectionToConvert); + } - if(!(sectionToConvert instanceof Map)) { - throw new CoreException( - new SubstitutionMappingsConverterErrorBuilder( - sectionName, sectionToConvert.getClass().getSimpleName()).build()); - } + private Map<String, List<String>> convertSection(Object sectionToConvert) { + + Map<String, Object> sectionAsMap = (Map<String, Object>) sectionToConvert; + Map<String, List<String>> convertedSection = new HashMap<>(); - return convertSection(sectionToConvert); + if (MapUtils.isEmpty(sectionAsMap)) { + return null; } - private Map<String, List<String>> convertSection(Object sectionToConvert) { + for (Map.Entry<String, Object> entry : sectionAsMap.entrySet()) { + if (entry.getValue() instanceof List) { + convertedSection.put(entry.getKey(), (List<String>) entry.getValue()); + } + } - Map<String, Object> sectionAsMap = (Map<String, Object>)sectionToConvert; - Map<String, List<String>> convertedSection = new HashMap<>(); + return convertedSection; + } - if (MapUtils.isEmpty(sectionAsMap)) { - return null; - } + 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; + } - for (Map.Entry<String, Object> entry : sectionAsMap.entrySet()) { - if (entry.getValue() instanceof List) { - convertedSection.put(entry.getKey(), (List<String>) entry.getValue()); - } - } + private Optional<Manifest> getCsarManifest(Map<String, byte[]> csarFiles) throws IOException { + Optional<byte[]> manifestContent = getManifestContent(csarFiles); - return convertedSection; - } + if (manifestContent.isPresent()) { + ByteArrayInputStream byteInputStream = new ByteArrayInputStream(manifestContent.get()); - 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; + return Optional.of(new Manifest(byteInputStream)); } - private Optional<Manifest> getCsarManifest(Map<String, byte[]> csarFiles) throws IOException { - Optional<byte[]> manifestContent = getManifestContent(csarFiles); + return Optional.empty(); + } - if (manifestContent.isPresent()) { - ByteArrayInputStream byteInputStream = new ByteArrayInputStream(manifestContent.get()); + private NodeTemplate convertNodeTemplate(Object candidateNodeTemplate) { + NodeTemplate nodeTemplate = new NodeTemplate(); - return Optional.of(new Manifest(byteInputStream)); - } + 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, InterfaceDefinition>) 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 Optional.empty(); - } + return nodeTemplate; + } - 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, InterfaceDefinition>) 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; } - private List<Map<String, CapabilityAssignment>> convertCapabilities(Map<String, Object> capabilities) { - List<Map<String, CapabilityAssignment>> convertedCapabilities = new ArrayList<>(); - if (MapUtils.isEmpty(capabilities)) { - return null; - } - for (Map.Entry<String, Object> capabilityAssignmentEntry : capabilities.entrySet()) { - Map<String, CapabilityAssignment> tempMap = new HashMap<>(); - Optional<CapabilityAssignment> capabilityAssignment = ToscaConverterUtil.createObjectFromClass - (capabilityAssignmentEntry.getKey(), capabilityAssignmentEntry.getValue(), - CapabilityAssignment.class); - - capabilityAssignment.ifPresent(capabilityAssignmentValue -> { - tempMap.put(capabilityAssignmentEntry.getKey(), capabilityAssignmentValue); - convertedCapabilities.add(tempMap); - } - ); + 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; } + return convertedCapabilities; + } - private boolean isMainServiceTemplate(String fileName) { - return fileName.endsWith(mainStName); - } + private boolean isMainServiceTemplate(String fileName) { + return fileName.endsWith(mainStName); + } - private boolean isMetadataFile(String fileName) { - return fileName.equals(metadataFile); - } + private boolean isMetadataFile(String fileName) { + return fileName.equals(metadataFile); + } - private boolean isGlobalServiceTemplate(String fileName) { - return fileName.endsWith(globalStName); - } + private boolean isGlobalServiceTemplate(String fileName) { + return fileName.endsWith(globalStName); + } - private boolean isDefinitions(String fileName) { - return fileName.startsWith(definitionsDir); - } + 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 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; - } + 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/test/resources/mock/toscaConverter/conversionWithInt/out/MainServiceTemplate.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/conversionWithInt/out/MainServiceTemplate.yaml index 1a5c61efbe..9cf2427c2d 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/conversionWithInt/out/MainServiceTemplate.yaml +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/conversionWithInt/out/MainServiceTemplate.yaml @@ -34,7 +34,7 @@ topology_template: capability: virtual_storage node: SCU_Storage capabilities: - - virtual_compute: + virtual_compute: properties: virtual_memory: virtual_mem_size: 24G diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/convertMainSt/out/MainServiceTemplate.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/convertMainSt/out/MainServiceTemplate.yaml index 88ae60529d..0f41bb0d8b 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/convertMainSt/out/MainServiceTemplate.yaml +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/mock/toscaConverter/convertMainSt/out/MainServiceTemplate.yaml @@ -48,7 +48,7 @@ topology_template: - local_storage: node: tosca.nodes.Root capabilities: - - virtual_compute: + virtual_compute: properties: virtual_memory: virtual_mem_size: 24G @@ -116,7 +116,7 @@ topology_template: - local_storage: node: tosca.nodes.Root capabilities: - - virtual_compute: + virtual_compute: properties: virtual_memory: virtual_mem_size: 24G @@ -177,7 +177,7 @@ topology_template: - local_storage: node: tosca.nodes.Root capabilities: - - virtual_compute: + virtual_compute: properties: virtual_memory: virtual_mem_size: 24G @@ -410,7 +410,7 @@ topology_template: - local_storage: node: tosca.nodes.Root capabilities: - - virtual_compute: + virtual_compute: properties: virtual_memory: virtual_mem_size: 24G @@ -450,7 +450,7 @@ topology_template: - local_storage: node: tosca.nodes.Root capabilities: - - virtual_compute: + virtual_compute: properties: virtual_memory: virtual_mem_size: 16G |