diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java')
6 files changed, 441 insertions, 215 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; + } + +} |