diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core')
20 files changed, 838 insertions, 218 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 index 0fc65c1f94..859d3da9dc 100644 --- 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 @@ -23,21 +23,12 @@ 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; @@ -49,28 +40,23 @@ 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; @@ -82,6 +68,9 @@ public abstract class AbstractToscaConverter implements ToscaConverter { @Override public abstract ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException; + public abstract void convertTopologyTemplate(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService); + protected void handleMetadataFile(Map<String, byte[]> csarFiles) { byte[] bytes = csarFiles.remove(TOSCA_META_PATH_FILE_NAME); if (bytes != null) { @@ -105,9 +94,9 @@ public abstract class AbstractToscaConverter implements ToscaConverter { } } - protected String getConcreteArtifactFileName(String fileName){ + protected String getConcreteArtifactFileName(String fileName) { int artifactIndex = fileName.indexOf(CsarFileTypes.Artifacts.name()); - if(artifactIndex < 0){ + if (artifactIndex < 0) { return fileName; } @@ -130,7 +119,7 @@ public abstract class AbstractToscaConverter implements ToscaConverter { csarFiles.get(TOSCA_META_ORIG_PATH_FILE_NAME)); toscaServiceModel.setArtifactFiles(externalFilesHandler); - if(MapUtils.isNotEmpty(globalSubstitutionServiceTemplate.getNode_types())) { + if (MapUtils.isNotEmpty(globalSubstitutionServiceTemplate.getNode_types())) { serviceTemplates .put(GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME, globalSubstitutionServiceTemplate); } @@ -146,8 +135,8 @@ public abstract class AbstractToscaConverter implements ToscaConverter { protected void handleServiceTemplate(String serviceTemplateName, String fileName, Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates) { - Optional<ServiceTemplate> serviceTemplate = - getServiceTemplateFromCsar(fileName, csarFiles); + final byte[] inputServiceTemplate = getServiceTemplateFromCsar(fileName, csarFiles); + Optional<ServiceTemplate> serviceTemplate = convertServiceTemplate(fileName, inputServiceTemplate); serviceTemplate.ifPresent( serviceTemplateValue -> addServiceTemplate(serviceTemplateName, serviceTemplateValue, serviceTemplates)); @@ -159,16 +148,12 @@ public abstract class AbstractToscaConverter implements ToscaConverter { 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 byte[] getServiceTemplateFromCsar(String fileName, Map<String, byte[]> csarFiles) { + return csarFiles.get(fileName); } - private ServiceTemplate convertServiceTemplate(String serviceTemplateName, - byte[] fileContent) { + private Optional<ServiceTemplate> convertServiceTemplate(String serviceTemplateName, + byte[] fileContent) { ServiceTemplate serviceTemplate = new ServiceTemplate(); try { ServiceTemplateReaderService readerService = @@ -178,7 +163,6 @@ public abstract class AbstractToscaConverter implements ToscaConverter { convertImports(serviceTemplate); convertNodeTypes(serviceTemplate, readerService); convertTopologyTemplate(serviceTemplate, readerService); - } catch (YAMLException ye) { throw new CoreException(new ErrorCode.ErrorCodeBuilder() .withMessage("Invalid YAML content in file" + serviceTemplateName) @@ -186,7 +170,7 @@ public abstract class AbstractToscaConverter implements ToscaConverter { } - return serviceTemplate; + return Optional.of(serviceTemplate); } private void convertToscaVersion(ServiceTemplate serviceTemplate, @@ -249,133 +233,7 @@ public abstract class AbstractToscaConverter implements ToscaConverter { } } - 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){ + protected CsarFileTypes getFileType(String fileName) { if (isMainServiceTemplate(fileName)) { return CsarFileTypes.mainServiceTemplate; } else if (isGlobalServiceTemplate(fileName)) { @@ -388,47 +246,6 @@ public abstract class AbstractToscaConverter implements ToscaConverter { 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); } 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/AbstractToscaSolConverter.java index 46a5904299..6371ba67d9 100644 --- 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/AbstractToscaSolConverter.java @@ -31,6 +31,7 @@ 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; @@ -43,9 +44,9 @@ import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBU 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 { +public abstract class AbstractToscaSolConverter extends AbstractToscaConverter { - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaSolConverterImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaSolConverter.class); private final Set<String> handledDefinitionFilesList = new HashSet<>(); @Override @@ -65,7 +66,7 @@ public class ToscaSolConverterImpl extends AbstractToscaConverter { private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) { - if(mServiceDefinitionFileName != null){ + if (mServiceDefinitionFileName != null) { handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates); String parentDir = mServiceDefinitionFileName.substring(0, mServiceDefinitionFileName.lastIndexOf("/")); handleImportDefinitions(mServiceDefinitionFileName, csarFiles, parentDir, gsst); @@ -73,11 +74,11 @@ public class ToscaSolConverterImpl extends AbstractToscaConverter { } 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())){ + 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{ + } else { artifacts.addFile( getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue()); } @@ -85,7 +86,7 @@ public class ToscaSolConverterImpl extends AbstractToscaConverter { } } - private void handleImportDefinitions(String fileName, Map<String,byte[]> csarFiles, String parentDir, GlobalSubstitutionServiceTemplate gsst) { + 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(); @@ -102,17 +103,17 @@ public class ToscaSolConverterImpl extends AbstractToscaConverter { return; } - private String getImportedFilePath(Object o, String parentDir){ - if(o instanceof String){ + private String getImportedFilePath(Object o, String parentDir) { + if (o instanceof String) { String fileName = (String) o; - if(!fileName.contains("/")){ + if (!fileName.contains("/")) { fileName = parentDir + "/" + fileName; } return fileName; - }else if(o instanceof Map){ + } 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))){ + for (Map.Entry<String, Object> entry : o1.entrySet()) { + if (NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))) { getImportedFilePath(entry.getValue(), parentDir); } } @@ -125,16 +126,17 @@ public class ToscaSolConverterImpl extends AbstractToscaConverter { ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile( contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME)); return toscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS); - }catch (IOException e){ + } catch (IOException e) { LOGGER.error(e.getMessage(), e); throw new IOException(e.getMessage()); } } - private String getSimpleName(String path){ - if(path != null && path.contains("/")){ + private String getSimpleName(String path) { + if (path != null && path.contains("/")) { path = path.substring(path.lastIndexOf("/") + 1); } return path; } + }
\ No newline at end of file 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 01738c1f8a..8d4b9850ea 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 @@ -17,6 +17,7 @@ package org.openecomp.core.impl; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; import org.openecomp.core.converter.datatypes.CsarFileTypes; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; @@ -66,4 +67,8 @@ public class ToscaConverterImpl extends AbstractToscaConverter { return toscaServiceModel; } + @Override + public void convertTopologyTemplate(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { + new VnfTopologyTemplateConverter().convertTopologyTemplate(serviceTemplate, readerService); + } } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java new file mode 100644 index 0000000000..0258b42a3d --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java @@ -0,0 +1,147 @@ +/* + * - + * * ============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.NodeTemplate; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.sdc.tosca.services.DataModelUtil; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +public class ToscaSolConverterPnf extends AbstractToscaSolConverter { + + private static final String PNF_EXT_CP_TYPE = "tosca.nodes.nfv.PnfExtCp"; + private static final String EXT_CP_TYPE = "org.openecomp.resource.cp.v2.extCP"; + private static final String LAYER_PROTOCOLS = "layer_protocols"; + private static final String IP_V4 = "ipv4"; + private static final String IP_V6 = "ipv6"; + private static final String IP_VERSION = "ip_version"; + private static final String ASSIGNMENT_METHOD = "assingment_method"; + private static final String DHCP = "dhcp"; + + /** + * For PNF the node templates are converted ETSI node types to ecomp node types + * All other data i.e. inputs, substitution mappings and outputs are simply dropped at this stage. The equivalent + * ecomp data will be added when the vsp is imported into the catalog. + * @param serviceTemplate + * @param readerService + */ + @Override + public void convertTopologyTemplate(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { + convertNodeTemplatesToEcompTypes(serviceTemplate, readerService); + addEmptyNodeTemplatesIfNoneDefined(serviceTemplate); + } + + /** + * PNF only has nfv.PNF and nfv.PnfExtCp types defined in ETSI SOL001 v2.5.1. + * - The PNF is mapped to the outer Abstract PNF container in ecomp model and hence nfv.PNF is dropped here. + * - nfv.PnfExtCp is mapped to ecomp v2.extCp type. + * + * @param serviceTemplate + * @param readerService + */ + private void convertNodeTemplatesToEcompTypes(ServiceTemplate serviceTemplate, + ServiceTemplateReaderService readerService) { + Map<String, Object> nodeTemplates = readerService.getNodeTemplates(); + if (MapUtils.isEmpty(nodeTemplates)) { + return; + } + + for (Map.Entry<String, Object> nodeTemplateEntry : nodeTemplates.entrySet()) { + Map<String, Object> inputNodeTemplate = (Map<String, Object>) nodeTemplateEntry.getValue(); + if (PNF_EXT_CP_TYPE.equals((String) inputNodeTemplate.get("type"))) { + NodeTemplate nodeTemplate = convertToEcompConnectionPointNodeType(inputNodeTemplate); + DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), nodeTemplate); + } + } + addEmptyNodeTemplatesIfNoneDefined(serviceTemplate); + } + + + /** + * Converts from the ETSI PnfExtCp node type to ecomp v2.extCP node type + * The following properties are mapped + * - layer_protocols is mapped to ip_requirements if it contains the values ipv4 and/or ipv6 + * <p> + * All other data e.g. remaining properties, requirements, capabilities are + * not mapped over to ecomp equivalent + * + * @param pnfExtCp + * @return ecomp v2.extCP node type + */ + private NodeTemplate convertToEcompConnectionPointNodeType(Map<String, Object> pnfExtCp) { + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setType(EXT_CP_TYPE); + Map<String, Object> properties = (Map<String, Object>) pnfExtCp.get("properties"); + for (Map.Entry<String, Object> property : properties.entrySet()) { + final String propertyName = property.getKey(); + if (LAYER_PROTOCOLS.equals(propertyName)) { + List<Map<String, Object>> ipRequirements = convertToIpRequirementsProperty((List<String>) property.getValue()); + if (isNotEmpty(ipRequirements)) { + Map<String, Object> convertedProperties = new HashMap<>(); + convertedProperties.put("ip_requirements", ipRequirements); + nodeTemplate.setProperties(convertedProperties); + } + } + } + return nodeTemplate; + } + + private List<Map<String, Object>> convertToIpRequirementsProperty(List<String> layerProtocols) { + return layerProtocols.stream() + .filter(layerProtocol -> IP_V4.equals(layerProtocol) || IP_V6.equals(layerProtocol)) + .map(this::createIpPropertyElement) + .collect(Collectors.toList()); + } + + private Map<String, Object> createIpPropertyElement(String ipVersion) { + final int version = ipVersion.equals(IP_V4) ? 4 : 6; + Map<String, Object> result = new HashMap<>(); + result.put(IP_VERSION, version); + result.put(ASSIGNMENT_METHOD, DHCP); + return result; + } + + private boolean isNotEmpty(List<?> list) { + return !list.isEmpty(); + } + + private void addEmptyNodeTemplatesIfNoneDefined(ServiceTemplate serviceTemplate) { + TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); + if (Objects.isNull(topologyTemplate)) { + topologyTemplate = new TopologyTemplate(); + serviceTemplate.setTopology_template(topologyTemplate); + } + if (topologyTemplate.getNode_templates() == null) { + topologyTemplate.setNode_templates(new HashMap<>()); + } + } + +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterVnf.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterVnf.java new file mode 100644 index 0000000000..7d2a38f4f0 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterVnf.java @@ -0,0 +1,35 @@ +/* + * - + * * ============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; + +public class ToscaSolConverterVnf extends AbstractToscaSolConverter { + + @Override + public void convertTopologyTemplate(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) { + new VnfTopologyTemplateConverter().convertTopologyTemplate(serviceTemplate, readerService); + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/VnfTopologyTemplateConverter.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/VnfTopologyTemplateConverter.java new file mode 100644 index 0000000000..e6978bcfe1 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/VnfTopologyTemplateConverter.java @@ -0,0 +1,220 @@ +/* + * - + * * ============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.NodeFilter; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +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.datatypes.Constants; +import org.openecomp.core.converter.errors.SubstitutionMappingsConverterErrorBuilder; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.tosca.services.DataModelUtil; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +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.outputs; +import static org.openecomp.core.converter.datatypes.Constants.requirements; + +public class VnfTopologyTemplateConverter { + + public 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 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 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; + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfMultipleNodeTemplatesTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfMultipleNodeTemplatesTest.java new file mode 100644 index 0000000000..21394c9c47 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfMultipleNodeTemplatesTest.java @@ -0,0 +1,74 @@ +/* + * - + * * ============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.Test; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ToscaSolConverterPnfMultipleNodeTemplatesTest { + + private static final String PNF_EXT_CP_1 = "pnfExtCp_1"; + private static final String PNF_EXT_CP_2 = "pnfExtCp_2"; + + @Test + public void testGivenDescriptorWithPnfAndTwoPnfExts_WhenConvertTopologyTemplate_ThenTwoExtCpsInOutput() throws IOException { + // Added this as separate test as data-driven tests compare strings and as order of nodeTemplates + // can be different in hashMap and hence test may fail + final byte[] descriptor = getFileResource("pnfDescriptor/other/pnfDescriptor_PnfAnd2ExtCps.yaml"); + ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + ToscaSolConverterPnf toscaSolConverter = new ToscaSolConverterPnf(); + toscaSolConverter.convertTopologyTemplate(serviceTemplate, serviceTemplateReaderService); + Map<String, NodeTemplate> nodeTemplates = serviceTemplate.getTopology_template().getNode_templates(); + assertEquals(2, nodeTemplates.size()); + + nodeTemplates.entrySet().stream() + .map(Map.Entry::getKey) + .forEach((key -> assertTrue(getErrorString(), hasCorrectName(key)))); + } + + private byte[] getFileResource(String filePath) throws IOException { + try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath)) { + return IOUtils.toByteArray(inputStream); + } + } + + private boolean hasCorrectName(String name) { + return PNF_EXT_CP_1.equals(name) || PNF_EXT_CP_2.equals(name); + } + + private String getErrorString() { + return "node template name should be either " + PNF_EXT_CP_1 + " or " + PNF_EXT_CP_2; + } +} diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfTest.java new file mode 100644 index 0000000000..c3f38fe3cd --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaSolConverterPnfTest.java @@ -0,0 +1,114 @@ +/* + * - + * * ============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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.converter.ServiceTemplateReaderService; +import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class ToscaSolConverterPnfTest { + + public static final String INPUT_DIR = "pnfDescriptor/in/"; + public static final String OUTPUT_DIR = "pnfDescriptor/out/"; + private String inputFilename; + private YamlUtil yamlUtil = new YamlUtil(); + private ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + + public ToscaSolConverterPnfTest(String inputFilename) { + this.inputFilename = inputFilename; + } + + @Parameterized.Parameters(name = "{index}: {0}") + public static Collection<String> input() throws IOException { + try (Stream<Path> files = Files.list(getPathFromClasspath(INPUT_DIR))) { + return files.map(path -> path.getFileName().toString()) + .collect(Collectors.toList()); + } + } + + @Test + public void testTopologyTemplateConversions() throws IOException { + final byte[] descriptor = getInputFileResource(inputFilename); + ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor); + ServiceTemplate serviceTemplate = new ServiceTemplate(); + ToscaSolConverterPnf toscaSolConverter = new ToscaSolConverterPnf(); + toscaSolConverter.convertTopologyTemplate(serviceTemplate, serviceTemplateReaderService); + + String result = yamlUtil.objectToYaml(serviceTemplate); + String expectedResult = getExpectedResultFor(inputFilename); + assertEquals(expectedResult, result); + } + + private String getExpectedResultFor(String inputFilename) throws IOException { + try (InputStream inputStream = getOutputFileResourceCorrespondingTo(inputFilename)) { + ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class); + return yamlUtil.objectToYaml(serviceTemplate); + } + } + + private static Path getPathFromClasspath(String location) { + return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath()); + } + + private byte[] getInputFileResource(String inputFilename) throws IOException { + return getFileResource(INPUT_DIR + inputFilename); + } + + private InputStream getOutputFileResourceCorrespondingTo(String inputFilename) { + String outputFilename = getOutputFilenameFrom(inputFilename); + return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename); + } + + private String getOutputFilenameFrom(String inputFilename) { + return inputFilename.replace("pnfDescriptor", "topologyTemplate"); + } + + private byte[] getFileResource(String filePath) throws IOException { + try (InputStream inputStream = getFileResourceAsInputStream(filePath)) { + return IOUtils.toByteArray(inputStream); + } + } + + private InputStream getFileResourceAsInputStream(String filePath) { + return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); + } + +}
\ 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/ToscaSolConverterVnfTest.java index 45d6332e06..bfc1fa9d85 100644 --- 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/ToscaSolConverterVnfTest.java @@ -36,14 +36,14 @@ import java.nio.charset.StandardCharsets; import java.util.Map; -public class ToscaSolConverterImplTest { +public class ToscaSolConverterVnfTest { - private ToscaSolConverterImpl toscaSolConverter; + private AbstractToscaSolConverter toscaSolConverter; private FileContentHandler fileContentHandler; @Before public void setUp(){ - toscaSolConverter = new ToscaSolConverterImpl(); + toscaSolConverter = new ToscaSolConverterVnf(); fileContentHandler = new FileContentHandler(); } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_ExtCpWithNoIpv4OrIpv6Values.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_ExtCpWithNoIpv4OrIpv6Values.yaml new file mode 100644 index 0000000000..f78b763672 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_ExtCpWithNoIpv4OrIpv6Values.yaml @@ -0,0 +1,26 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +description: service template of a PNF + +topology_template: + node_templates: + pnf_mainPart: + type: tosca.nodes.nfv.PNF + properties: + descriptor_id: b1bb0ce7-ebca-4fa7-95ed-4840d70a2233 + function_description: an example PNF + provider: Mycompany + version: 1.0 + descriptor_invariant_id: 1111-2222-ccaa-bbdd + name: ExamplePnf + requirements: + - virtual_link: + capability: tosca.capabilities.nfv.VirtualLinkable + node: pnfExtCp_1 + pnfExtCp_1: + type: tosca.nodes.nfv.PnfExtCp + properties: + trunk_mode: false + layer_protocols: [ otherProtocol ] + role: leaf + description: External connection point to access this pnf
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_PnfAndExtCp.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_PnfAndExtCp.yaml new file mode 100644 index 0000000000..83c7d9c0d4 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_PnfAndExtCp.yaml @@ -0,0 +1,26 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +description: service template of a PNF + +topology_template: + node_templates: + pnf_mainPart: + type: tosca.nodes.nfv.PNF + properties: + descriptor_id: b1bb0ce7-ebca-4fa7-95ed-4840d70a2233 + function_description: an example PNF + provider: Mycompany + version: 1.0 + descriptor_invariant_id: 1111-2222-ccaa-bbdd + name: ExamplePnf + requirements: + - virtual_link: + capability: tosca.capabilities.nfv.VirtualLinkable + node: pnfExtCp_1 + pnfExtCp_1: + type: tosca.nodes.nfv.PnfExtCp + properties: + trunk_mode: false + layer_protocols: [ ipv4, ipv6, otherProtocol ] + role: leaf + description: External connection point to access this pnf
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_PnfOnly.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_PnfOnly.yaml new file mode 100644 index 0000000000..1b6ab8b95f --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_PnfOnly.yaml @@ -0,0 +1,19 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +description: service template of a PNF + +topology_template: + node_templates: + pnf_mainPart: + type: tosca.nodes.nfv.PNF + properties: + descriptor_id: b1bb0ce7-ebca-4fa7-95ed-4840d70a2233 + function_description: an example PNF + provider: Mycompany + version: 1.0 + descriptor_invariant_id: 1111-2222-ccaa-bbdd + name: ExamplePnf + requirements: + - virtual_link: + capability: tosca.capabilities.nfv.VirtualLinkable + node: pnfExtCp_1
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_withExtraSpaces.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_withExtraSpaces.yaml new file mode 100644 index 0000000000..913e200ead --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_withExtraSpaces.yaml @@ -0,0 +1,26 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +description: service template of a PNF + +topology_template: + node_templates: + pnf_mainPart: + type: tosca.nodes.nfv.PNF # This is a comment + properties: + descriptor_id: b1bb0ce7-ebca-4fa7-95ed-4840d70a2233 + function_description: an example PNF + provider: Mycompany + version: 1.0 + descriptor_invariant_id: 1111-2222-ccaa-bbdd + name: ExamplePnf + requirements: + - virtual_link: + capability: tosca.capabilities.nfv.VirtualLinkable + node: pnfExtCp_1 + pnfExtCp_1: # This is another comment here + type: tosca.nodes.nfv.PnfExtCp + properties: + trunk_mode: false + layer_protocols: [ ipv4 , ipv6 , otherProtocol ] + role: leaf + description: External connection point to access this pnf
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_withInputsAndSubstitutionMappings.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_withInputsAndSubstitutionMappings.yaml new file mode 100644 index 0000000000..79108d9084 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/in/pnfDescriptor_withInputsAndSubstitutionMappings.yaml @@ -0,0 +1,40 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +description: service template of a PNF + +topology_template: + inputs: + my_input1: + type: string + description: this is input1 + default: defaultValue + my_input2: + type: string + description: this is input2 + default: defaultValue2 + + node_templates: + pnf_mainPart: + type: tosca.nodes.nfv.PNF + properties: + descriptor_id: b1bb0ce7-ebca-4fa7-95ed-4840d70a2233 + function_description: an example PNF + provider: Mycompany + version: 1.0 + descriptor_invariant_id: 1111-2222-ccaa-bbdd + name: ExamplePnf + + pnfExtCp_1: + type: tosca.nodes.nfv.PnfExtCp + properties: + trunk_mode: false + layer_protocols: [ ipv4, ipv6, otherProtocol ] + role: leaf + description: External connection point to access this pnf + requirements: + - virtual_link: + + substitution_mappings: + node_type: tosca.nodes.nfv.PNF + requirements: + virtual_link: [ pnfExtCp_1, virtual_link ]
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/other/pnfDescriptor_PnfAnd2ExtCps.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/other/pnfDescriptor_PnfAnd2ExtCps.yaml new file mode 100644 index 0000000000..26255ddd84 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/other/pnfDescriptor_PnfAnd2ExtCps.yaml @@ -0,0 +1,33 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +description: service template of a PNF + +topology_template: + node_templates: + pnf_mainPart: + type: tosca.nodes.nfv.PNF + properties: + descriptor_id: b1bb0ce7-ebca-4fa7-95ed-4840d70a2233 + function_description: an example PNF + provider: Mycompany + version: 1.0 + descriptor_invariant_id: 1111-2222-ccaa-bbdd + name: ExamplePnf + requirements: + - virtual_link: + capability: tosca.capabilities.nfv.VirtualLinkable + node: pnfExtCp_1 + pnfExtCp_1: + type: tosca.nodes.nfv.PnfExtCp + properties: + trunk_mode: false + layer_protocols: [ ipv4, ipv6, otherProtocol ] + role: leaf + description: External connection point to access this pnf + pnfExtCp_2: + type: tosca.nodes.nfv.PnfExtCp + properties: + trunk_mode: false + layer_protocols: [ ipv4, ipv6, otherProtocol ] + role: leaf + description: External connection point to access this pnf
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_ExtCpWithNoIpv4OrIpv6Values.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_ExtCpWithNoIpv4OrIpv6Values.yaml new file mode 100644 index 0000000000..6b1c45b6c9 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_ExtCpWithNoIpv4OrIpv6Values.yaml @@ -0,0 +1,4 @@ +topology_template: + node_templates: + pnfExtCp_1: + type: org.openecomp.resource.cp.v2.extCP
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_PnfAndExtCp.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_PnfAndExtCp.yaml new file mode 100644 index 0000000000..d04b06a89a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_PnfAndExtCp.yaml @@ -0,0 +1,10 @@ +topology_template: + node_templates: + pnfExtCp_1: + type: org.openecomp.resource.cp.v2.extCP + properties: + ip_requirements: + - assingment_method: dhcp + ip_version: 4 + - assingment_method: dhcp + ip_version: 6
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_PnfOnly.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_PnfOnly.yaml new file mode 100644 index 0000000000..d47ce66ace --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_PnfOnly.yaml @@ -0,0 +1,2 @@ +topology_template: + node_templates: {}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_withExtraSpaces.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_withExtraSpaces.yaml new file mode 100644 index 0000000000..d04b06a89a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_withExtraSpaces.yaml @@ -0,0 +1,10 @@ +topology_template: + node_templates: + pnfExtCp_1: + type: org.openecomp.resource.cp.v2.extCP + properties: + ip_requirements: + - assingment_method: dhcp + ip_version: 4 + - assingment_method: dhcp + ip_version: 6
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_withInputsAndSubstitutionMappings.yaml b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_withInputsAndSubstitutionMappings.yaml new file mode 100644 index 0000000000..d04b06a89a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/resources/pnfDescriptor/out/topologyTemplate_withInputsAndSubstitutionMappings.yaml @@ -0,0 +1,10 @@ +topology_template: + node_templates: + pnfExtCp_1: + type: org.openecomp.resource.cp.v2.extCP + properties: + ip_requirements: + - assingment_method: dhcp + ip_version: 4 + - assingment_method: dhcp + ip_version: 6
\ No newline at end of file |