public class TypeUtils {
-
private static final String FIRST_CERTIFIED_VERSION_VERSION = "1.0";
public static <FieldType> void setField(Map<String, Object> toscaJson, ToscaTagNamesEnum tagName, Consumer<FieldType> setter) {
// Heat env Validation
PARAMETERS("parameters"),
// Import Validations
- TOSCA_VERSION("tosca_definitions_version"), TOPOLOGY_TEMPLATE("topology_template"), NODE_TYPES("node_types"), OCCURRENCES("occurrences"), NODE_TEMPLATES("node_templates"), GROUPS("groups"), INPUTS("inputs"),
+ TOSCA_VERSION("tosca_definitions_version"), TOPOLOGY_TEMPLATE("topology_template"), OCCURRENCES("occurrences"), NODE_TEMPLATES("node_templates"), GROUPS("groups"), INPUTS("inputs"),
SUBSTITUTION_MAPPINGS("substitution_mappings"), NODE_TYPE("node_type"), DIRECTIVES("directives"),
// Attributes
ATTRIBUTES("attributes"), LABEL("label"), HIDDEN("hidden"), IMMUTABLE("immutable"), ANNOTATIONS("annotations"),
VERSION("version"), OPERATIONS("operations"), NOTIFICATIONS("notifications"),
//functions
- GET_INPUT("get_input");
+ GET_INPUT("get_input"),
+ // Definitions
+ DATA_TYPES("data_types"), NODE_TYPES("node_types"), IMPORTS("imports");
private final String elementName;
public String getElementName() {
return elementName;
}
+
}
}
/*definition errors*/
MISSING_DEFINITION_FILE("Definition file %s referenced in TOSCA.meta does not exist"),
MISSING_IMPORT_FILE("Package must contain the referenced import file '%s'"),
+ MISSING_MAIN_DEFINITION_FILE("Package must contain the given main definition file '%s'"),
INVALID_IMPORT_STATEMENT("Definition file '%s' contains an invalid import statement: '%s'"),
INVALID_YAML_EXTENSION("Expecting yaml or yml extension for file: %s"),
-
-/*-
+/*
* ============LICENSE_START=======================================================
* Modification Copyright (C) 2019 Nordix Foundation.
* ================================================================================
public interface ServiceTemplateReaderService {
- Map<String, Object> readServiceTemplate(byte[] serivceTemplateContent);
+ Map<String, Object> readServiceTemplate(byte[] serviceTemplateContent);
Object getMetadata();
List<Object> getImports();
Map<String, Object> getPolicies();
+
+ Map<String, Object> getDataTypes();
}
import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.ONAP_INDEX_IMPORT_FILE;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
-
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
+import javax.validation.constraints.NotNull;
import org.apache.commons.collections.MapUtils;
+import org.onap.sdc.tosca.datatypes.model.DataType;
import org.onap.sdc.tosca.datatypes.model.Import;
import org.onap.sdc.tosca.datatypes.model.NodeType;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.error.YAMLException;
public abstract class AbstractToscaConverter implements ToscaConverter {
+ private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaConverter.class);
- public abstract void convertTopologyTemplate(ServiceTemplate serviceTemplate,
+ public abstract void convertTopologyTemplate(@NotNull ServiceTemplate serviceTemplate,
ServiceTemplateReaderService readerService);
protected void handleMetadataFile(Map<String, byte[]> csarFiles) {
}
}
- protected void handleDefintionTemplate(String key, Map<String, byte[]> csarFiles,
- GlobalSubstitutionServiceTemplate gsst) {
+ protected void handleDefinitionTemplate(String key, Map<String, byte[]> csarFiles,
+ GlobalSubstitutionServiceTemplate gsst) {
try {
ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(key));
Object nodeTypes = readerService.getNodeTypes();
Map<String, NodeType> nodeTypeMap = (Map<String, NodeType>) nodeTypes;
gsst.appendNodes(nodeTypeMap);
}
+ gsst.appendDataTypes((Map) readerService.getDataTypes());
} catch (YAMLException ye) {
throw new CoreException(new ErrorCode.ErrorCodeBuilder()
.withMessage("Invalid YAML content in file " + key)
convertToscaVersion(serviceTemplate, readerService);
convertImports(serviceTemplate);
convertNodeTypes(serviceTemplate, readerService);
+ convertDataTypes(serviceTemplate, readerService);
convertTopologyTemplate(serviceTemplate, readerService);
} catch (YAMLException ye) {
throw new CoreException(new ErrorCode.ErrorCodeBuilder()
}
}
+ protected void convertDataTypes(final ServiceTemplate serviceTemplate,
+ final ServiceTemplateReaderService readerService) {
+ try {
+ final Map<String, Object> dataTypes = readerService.getDataTypes();
+ for (final Map.Entry<String, Object> entry : dataTypes.entrySet()) {
+ final Optional<DataType> dataType =
+ ToscaConverterUtil.createObjectFromClass(entry.getKey(), entry.getValue(), DataType.class);
+
+ dataType.ifPresent(
+ nodeTypeValue -> DataModelUtil.addDataType(serviceTemplate, entry.getKey(), nodeTypeValue));
+ }
+ } catch (final Exception ex) {
+ LOGGER.error("Unable to process data types: ", ex);
+ }
+ }
+
protected CsarFileTypes getFileType(String fileName) {
if (isMainServiceTemplate(fileName)) {
return CsarFileTypes.mainServiceTemplate;
import java.util.Map;
import java.util.Set;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
+import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
if (toscaDefinitionImportHandler.hasError()) {
throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors());
}
- handledDefinitionFilesList.addAll(toscaDefinitionImportHandler.getHandledDefinitionFilesList());
+ final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
+ handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet());
for (final String file : handledDefinitionFilesList) {
- handleDefintionTemplate(file, csarFiles, gsst);
+ handleDefinitionTemplate(file, csarFiles, gsst);
}
}
package org.openecomp.core.impl;
+import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.stream.Collectors;
import org.apache.commons.collections4.MapUtils;
+import org.onap.sdc.tosca.datatypes.model.DataType;
import org.onap.sdc.tosca.datatypes.model.Import;
import org.onap.sdc.tosca.datatypes.model.NodeType;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
-import java.util.*;
-
-import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;
-
public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
- private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME =
"GlobalSubstitutionTypesServiceTemplate.yaml";
public static final String TEMPLATE_NAME_PROPERTY = "template_name";
- public static final String DEFININTION_VERSION = "tosca_simple_yaml_1_0_0";
+ public static final String DEFINITION_VERSION = "tosca_simple_yaml_1_0_0";
public static final String HEAT_INDEX = "openecomp_heat_index";
public static final String HEAT_INDEX_IMPORT_FILE = "openecomp-heat/_index.yml";
public static final String ONAP_INDEX_IMPORT_FILE = "onap/_index.yml";
- private static final Map<String, ServiceTemplate> globalServiceTemplates =
- GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
+
+ // transient needed to avoid being parsed as a YAML String. Used parser is reading fields instead of getters,
+ // although it ignores static or transient fields.
+ private final transient Map<String, ServiceTemplate> globalServiceTemplates;
+ private final transient Map<String, DataType> globalDataTypeMap;
public GlobalSubstitutionServiceTemplate() {
super();
init();
- }
-
-
- public void appendNodes(Map<String, NodeType> nodes) {
- Optional<Map<String, NodeType>> nodeTypesToAdd =
- removeExistingGlobalTypes(nodes);
-
- nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
+ globalServiceTemplates =
+ GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
+ globalDataTypeMap = loadGlobalDataTypes();
}
public void init() {
writeMetadataSection();
writeImportsSection();
setNode_types(new HashMap<>());
+ setData_types(new HashMap<>());
+ }
+
+ public void appendNodes(final Map<String, NodeType> nodes) {
+ final Optional<Map<String, NodeType>> nodeTypesToAdd = findNonGlobalTypesNodes(nodes);
+ nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
+ }
+
+ public void appendDataTypes(final Map<String, DataType> dataTypeMap) {
+ if (MapUtils.isEmpty(dataTypeMap)) {
+ return;
+ }
+ dataTypeMap.entrySet().stream()
+ .filter(dataTypeEntry -> !isGlobalDataType(dataTypeEntry.getKey()))
+ .forEach(dataTypeEntry -> {
+ final Optional<DataType> dataType = parseDataTypeToYamlObject(dataTypeEntry);
+ dataType.ifPresent(dataType1 -> getData_types().put(dataTypeEntry.getKey(), dataType1));
+ });
}
private void writeImportsSection() {
}
private void writeDefinitionSection() {
- setTosca_definitions_version(DEFININTION_VERSION);
+ setTosca_definitions_version(DEFINITION_VERSION);
}
- private Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){
- Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
- ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml");
-
- if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
+ private Optional<Map<String, NodeType>> findNonGlobalTypesNodes(final Map<String, NodeType> nodes){
+ final Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
+ if (MapUtils.isEmpty(globalNodeTypes)) {
return Optional.of(nodes);
}
- Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
+ final Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
+
for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
- Optional<NodeType> nodeType =
- ToscaConverterUtil
- .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
-
+ Optional<NodeType> nodeType = parseNodeTypeToYamlObject(nodeTypeEntry);
nodeType
.ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
}
return Optional.of(nodeTypesToAdd);
}
+ private boolean isGlobalDataType(final String dataType) {
+ if (MapUtils.isEmpty(globalDataTypeMap)) {
+ return false;
+ }
+
+ return globalDataTypeMap.containsKey(dataType);
+ }
+
+ private Optional<NodeType> parseNodeTypeToYamlObject(final Entry<String, NodeType> nodeTypeEntry) {
+ return ToscaConverterUtil
+ .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
+ }
+
+ private Optional<DataType> parseDataTypeToYamlObject(final Entry<String, DataType> dataTypeEntry) {
+ return ToscaConverterUtil
+ .createObjectFromClass(dataTypeEntry.getKey(), dataTypeEntry.getValue(), DataType.class);
+ }
+
+ private Map<String, DataType> loadGlobalDataTypes() {
+ return globalServiceTemplates.values().stream()
+ .map(ServiceTemplate::getData_types)
+ .filter(MapUtils::isNotEmpty)
+ .flatMap(stringDataTypeMap -> stringDataTypeMap.entrySet().stream())
+ .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (dataType, dataType2) -> dataType));
+ }
+
private Map<String, NodeType> getAllGlobalNodeTypes(){
Map<String, NodeType> globalNodeTypes = new HashMap<>();
break;
case definitionsFile:
- handleDefintionTemplate(fileEntry.getKey(), csarFiles, gsst);
+ handleDefinitionTemplate(fileEntry.getKey(), csarFiles, gsst);
break;
default:
import java.net.URI;
import java.util.ArrayList;
-import java.util.LinkedHashSet;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Set;
+import java.util.Map.Entry;
+import java.util.Optional;
+import lombok.Getter;
+import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Handles TOSCA definition imports, checking for import definition errors.
*/
public class ToscaDefinitionImportHandler {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDefinitionImportHandler.class);
+
private final Map<String, byte[]> fileMap;
- private final Set<String> handledDefinitionFilesList = new LinkedHashSet<>();
+
+ /**
+ * Stores all processed files during the import handling
+ */
+ @Getter
+ private final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap = new HashMap<>();
private final List<ErrorMessage> validationErrorList = new ArrayList<>();
private String currentFile;
* @param fileStructureMap The package structure with file path and respective file byte
* @param mainDefinitionFilePath The main descriptor yaml file to start the reading
*/
- public ToscaDefinitionImportHandler(final Map<String, byte[]> fileStructureMap, final String mainDefinitionFilePath) {
+ public ToscaDefinitionImportHandler(final Map<String, byte[]> fileStructureMap,
+ final String mainDefinitionFilePath) {
this.fileMap = fileStructureMap;
- handleImports(mainDefinitionFilePath);
+ readImportsFromMainDefinition(mainDefinitionFilePath);
+ }
+
+ private void readImportsFromMainDefinition(final String mainDefinitionFilePath) {
+ if(!checkMainDefinitionExists(mainDefinitionFilePath)) {
+ return;
+ }
+ final ServiceTemplateReaderService readerService = parseToServiceTemplate(mainDefinitionFilePath).orElse(null);
+ if (readerService == null) {
+ return;
+ }
+ final List<String> importFileList = extractFileImports(readerService.getImports());
+ if (CollectionUtils.isNotEmpty(importFileList)) {
+ for (final String importFilePath : importFileList) {
+ final String resolvedPath = resolveImportPath(FilenameUtils.getPath(mainDefinitionFilePath), importFilePath);
+ handleImports(resolvedPath);
+ }
+ }
+ }
+
+ private Optional<ServiceTemplateReaderService> parseToServiceTemplate(final String definitionFile) {
+ try {
+ return Optional.of(new ServiceTemplateReaderServiceImpl(fileMap.get(definitionFile)));
+ } catch (final Exception ex) {
+ LOGGER.debug("Could not parse '{}' to a ServiceTemplateReader", definitionFile, ex);
+ reportError(ErrorLevel.ERROR,
+ String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), ex.getMessage()));
+ }
+
+ return Optional.empty();
}
/**
if (!checkImportExists(fileName)) {
return;
}
- final ServiceTemplateReaderService readerService;
- try {
- readerService = new ServiceTemplateReaderServiceImpl(fileMap.get(fileName));
- } catch (final Exception ex) {
- reportError(ErrorLevel.ERROR,
- String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), ex.getMessage()));
+ final ServiceTemplateReaderService readerService = parseToServiceTemplate(fileName).orElse(null);
+ if (readerService == null) {
return;
}
- handledDefinitionFilesList.add(fileName);
+
+ handledImportDefinitionFileMap.put(fileName, readerService);
final List<Object> imports = readerService.getImports();
final List<String> extractImportFiles = extractFileImports(imports);
- for (final String importedFile : extractImportFiles) {
- final String resolvedPath = resolveImportPath(FilenameUtils.getPath(fileName), importedFile);
- if (!handledDefinitionFilesList.contains(resolvedPath)) {
- handleImports(resolvedPath);
- }
- }
+ extractImportFiles.stream()
+ .map(importedFile -> resolveImportPath(FilenameUtils.getPath(fileName), importedFile))
+ .filter(resolvedPath -> !handledImportDefinitionFileMap.containsKey(resolvedPath))
+ .forEach(this::handleImports);
}
/**
importedFileList.add((String) importObject);
} else if (importObject instanceof Map) {
final Map<String, Object> importObjectMap = (Map) importObject;
- for (final Map.Entry entry : importObjectMap.entrySet()) {
+ for (final Entry<String, Object> entry : importObjectMap.entrySet()) {
if (NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))) {
importedFileList.addAll(readImportStatement(entry.getValue()));
}
return resolvedImportPath;
}
+ private boolean checkImportExists(final String filePath) {
+ return checkFileExists(filePath, Messages.MISSING_IMPORT_FILE.formatMessage(filePath));
+ }
+
+ private boolean checkMainDefinitionExists(final String filePath) {
+ return checkFileExists(filePath, Messages.MISSING_MAIN_DEFINITION_FILE.formatMessage(filePath));
+ }
+
/**
* Checks if the given file path exists inside the file structure.
* Reports an error if the file was not found.
*
* @param filePath file path to check inside the file structure
+ * @param errorMsg the error message to report
* @return
* {@code true} if the file exists, {@code false} otherwise
*/
- private boolean checkImportExists(final String filePath) {
- if (!fileMap.keySet().contains(filePath)) {
- reportError(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(filePath));
+ private boolean checkFileExists(final String filePath, final String errorMsg) {
+ if (!fileMap.containsKey(filePath)) {
+ reportError(ErrorLevel.ERROR, errorMsg);
return false;
}
return true;
}
- /**
- * Gets all processed files during the import handling.
- * @return
- * A list containing the processed files paths
- */
- public Set<String> getHandledDefinitionFilesList() {
- return handledDefinitionFilesList;
- }
-
/**
* Adds an error to the validation error list.
*
package org.openecomp.core.impl.services;
-import org.onap.sdc.tosca.services.YamlUtil;
-import org.openecomp.core.converter.ServiceTemplateReaderService;
+import static org.openecomp.core.converter.datatypes.Constants.POLICIES;
+import static org.openecomp.core.converter.datatypes.Constants.inputs;
+import static org.openecomp.core.converter.datatypes.Constants.metadata;
+import static org.openecomp.core.converter.datatypes.Constants.nodeTemplates;
+import static org.openecomp.core.converter.datatypes.Constants.nodeTypes;
+import static org.openecomp.core.converter.datatypes.Constants.outputs;
+import static org.openecomp.core.converter.datatypes.Constants.substitutionMappings;
+import static org.openecomp.core.converter.datatypes.Constants.topologyTemplate;
+import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES;
+import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IMPORTS;
+import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
-import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
-
-import static org.openecomp.core.converter.datatypes.Constants.*;
+import org.onap.sdc.tosca.services.YamlUtil;
+import org.openecomp.core.converter.ServiceTemplateReaderService;
public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderService {
- private Map<String, Object> readServiceTemplate = new HashMap<>();
+ private final Map<String, Object> readServiceTemplate;
public ServiceTemplateReaderServiceImpl(byte[] serviceTemplateContent) {
this.readServiceTemplate = readServiceTemplate(serviceTemplateContent);
@Override
public Map<String, Object> readServiceTemplate(byte[] serviceTemplateContent) {
-
return new YamlUtil().yamlToObject(new String(serviceTemplateContent), Map.class);
-
}
@Override
public List<Object> getImports() {
- return Objects.isNull(this.readServiceTemplate.get("imports")) ? new ArrayList<>()
- : (List<Object>) this.readServiceTemplate.get("imports");
+ final String importsElementName = IMPORTS.getElementName();
+ return Objects.isNull(this.readServiceTemplate.get(importsElementName)) ? new ArrayList<>()
+ : (List<Object>) this.readServiceTemplate.get(importsElementName);
}
@Override
public Map<String, Object> getPolicies() {
Map<String, Object> policiesAsMap = new HashMap<>();
if (!Objects.isNull(this.getTopologyTemplate()) && !Objects.isNull(
- ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES))) {
+ ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES))) {
policiesAsMap = (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES);
}
return policiesAsMap;
@Override
public Object getToscaVersion() {
- return this.readServiceTemplate.get(ToscaTagNamesEnum.TOSCA_VERSION.getElementName());
+ return this.readServiceTemplate.get(TOSCA_VERSION.getElementName());
}
@Override
public Map<String, Object> getNodeTypes() {
return Objects.isNull(this.readServiceTemplate.get(nodeTypes)) ? new HashMap<>()
- : (Map<String, Object>) this.readServiceTemplate.get(nodeTypes);
+ : (Map<String, Object>) this.readServiceTemplate.get(nodeTypes);
}
@Override
@Override
public Map<String, Object> getNodeTemplates() {
return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
- : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(nodeTemplates);
+ : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(nodeTemplates);
}
@Override
public Map<String, Object> getInputs() {
return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
- : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(inputs);
+ : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(inputs);
}
@Override
public Map<String, Object> getOutputs() {
return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
- : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(outputs);
+ : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(outputs);
}
@Override
public Map<String, Object> getSubstitutionMappings() {
return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
- : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate())
- .get(substitutionMappings);
+ : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate())
+ .get(substitutionMappings);
+ }
+
+ @Override
+ public Map<String, Object> getDataTypes() {
+ final String dataTypesElementName = DATA_TYPES.getElementName();
+ return Objects.isNull(this.readServiceTemplate.get(dataTypesElementName)) ? new HashMap<>()
+ : (Map<String, Object>) this.readServiceTemplate.get(dataTypesElementName);
}
}
assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(HEAT_INDEX));
assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(ONAP_INDEX));
- assertEquals(DEFININTION_VERSION, serviceTemplate.getTosca_definitions_version());
+ assertEquals(DEFINITION_VERSION, serviceTemplate.getTosca_definitions_version());
assertNotNull(serviceTemplate.getNode_types());
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Set;
+import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
+import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
*/
@Test
public void testGivenDescriptorFiles_whenMainDescriptorImportsAreHandled_allDescriptorsAreProcessedWithoutError() {
- final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml", "Definitions/descriptorBasicImport.yaml",
+ final String mainDefinitionFilePath = "Definitions/Main.yaml";
+ descriptorFileMap
+ .put(mainDefinitionFilePath, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFilePath)));
+ final List<String> filesToHandleList = Arrays.asList("Definitions/descriptorBasicImport.yaml",
"Definitions/descriptorWithRelativePaths.yaml", "Artifacts/descriptorWithAbsolutePaths.yaml",
"Artifacts/descriptorCyclicReference.yaml");
final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
descriptorFileMap,
- "Definitions/Main.yaml");
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ mainDefinitionFilePath);
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
+ assertThat("The handled files should be the same",
+ actualHandledFiles.keySet(), hasSize(filesToHandleList.size()));
assertThat("The handled files should be the same"
- , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
+ , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0]))
);
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
public void testGivenEmptyPackage_whenMainDescriptorIsHandled_aMissingFileErrorIsReported() {
final List<String> filesToHandleList = Collections.emptyList();
- final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
- descriptorFileMap,
- "Definitions/Main.yaml");
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
+ new ToscaDefinitionImportHandler(descriptorFileMap, "Definitions/Main.yaml");
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
+ assertThat("The handled files should be the same",
+ actualHandledFiles.keySet(), hasSize(filesToHandleList.size()));
assertThat("The handled files should be the same"
- , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
+ , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0]))
);
final List<ErrorMessage> expectedErrorList = new ArrayList<>();
expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
- Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/Main.yaml")));
+ Messages.MISSING_MAIN_DEFINITION_FILE.formatMessage("Definitions/Main.yaml")));
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
*/
@Test
public void testGivenOneMissingDescriptorFile_whenMainDescriptorImportsAreHandled_aMissingFileErrorIsReported() {
- final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml",
- "Definitions/descriptorBasicImport.yaml", "Definitions/descriptorWithRelativePaths.yaml",
- "Artifacts/descriptorWithAbsolutePaths.yaml");
+ descriptorFileMap
+ .put("Definitions/Main.yaml",
+ getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve("Definitions/Main.yaml")));
+
+ final List<String> filesToHandleList = Arrays.asList("Definitions/descriptorBasicImport.yaml",
+ "Definitions/descriptorWithRelativePaths.yaml", "Artifacts/descriptorWithAbsolutePaths.yaml");
filesToHandleList.forEach(file ->
descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
);
final List<ErrorMessage> expectedErrorList = new ArrayList<>();
expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
- String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Artifacts/descriptorCyclicReference.yaml")));
+ Messages.MISSING_IMPORT_FILE.formatMessage("Artifacts/descriptorCyclicReference.yaml")));
final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
descriptorFileMap,
"Definitions/Main.yaml");
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
+ assertThat("The handled files should be the same",
+ actualHandledFiles.keySet(), hasSize(filesToHandleList.size()));
assertThat("The handled files should be the same"
- , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
+ , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0]))
);
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
@Test
public void testGivenDescriptorWithInvalidImportStatement_whenMainDescriptorImportsAreHandled_aInvalidImportStatementErrorIsReported() {
final String mainDefinitionFile = "Definitions/MainWithInvalidImportedFile.yaml";
+ descriptorFileMap
+ .put(mainDefinitionFile, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile)));
- final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile,
- "Definitions/descriptorInvalidImportStatement.yaml");
+ final List<String> filesToHandleList =
+ Collections.singletonList("Definitions/descriptorInvalidImportStatement.yaml");
filesToHandleList.forEach(file ->
descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
);
final List<ErrorMessage> expectedErrorList = new ArrayList<>();
expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
- Messages.INVALID_IMPORT_STATEMENT.formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null")));
+ Messages.INVALID_IMPORT_STATEMENT
+ .formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null"))
+ );
final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
descriptorFileMap,
mainDefinitionFile);
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles = toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
+ assertThat("The handled files should be the same",
+ actualHandledFiles.keySet(), hasSize(filesToHandleList.size()));
assertThat("The handled files should be the same"
- , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
+ , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0]))
);
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
final String mainDefinitionFilePath = "Definitions/Main1.yaml";
final String invalidMainDefinitionFilePath = "../Definitions/InvalidMainDefinitionFile.yaml";
- final List<String> filesToHandleList = Arrays.asList(mainDefinitionFilePath);
+ final List<String> filesToHandleList = Collections.singletonList(mainDefinitionFilePath);
filesToHandleList.forEach(file ->
descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
);
final List<ErrorMessage> expectedErrorList = new ArrayList<>();
- expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(invalidMainDefinitionFilePath)));
+ expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
+ Messages.MISSING_MAIN_DEFINITION_FILE.formatMessage(invalidMainDefinitionFilePath)));
final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
descriptorFileMap,
invalidMainDefinitionFilePath);
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("No files should be handled", actualHandledFiles, hasSize(0));
+ assertThat("No files should be handled", actualHandledFiles.keySet(), hasSize(0));
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
@Test
public void testGivenInvalidYamlDescriptorFile_whenDescriptorIsHandled_aInvalidYamlFormatErrorIsReported() {
final String mainDefinitionFile = "Definitions/descriptorInvalid.yaml";
-
- final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile);
- filesToHandleList.forEach(file ->
- descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
- );
-
+ descriptorFileMap.put(mainDefinitionFile,
+ getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile)));
final List<ErrorMessage> expectedErrorList = new ArrayList<>();
- expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage()
- , "while scanning a simple key\n"
- + " in 'string', line 5, column 3:\n"
- + " template_author= onap\n"
- + " ^\n"
- + "could not find expected ':'\n"
- + " in 'string', line 6, column 1:\n"
- + " description: vCPE_vgw\n"
- + " ^\n")));
+ expectedErrorList
+ .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_YAML_FORMAT.formatMessage("")));
- final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
- descriptorFileMap,
- mainDefinitionFile);
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
+ new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile);
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("No files should be handled", actualHandledFiles, hasSize(0));
+ assertThat("No files should be handled", actualHandledFiles.keySet(), hasSize(0));
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
- assertThat("The errors should be the same"
- , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
- );
+ final List<String> actualErrorListAsStringList = validationErrorList.stream()
+ .map(ErrorMessage::getMessage)
+ .collect(Collectors.toList());
+ expectedErrorList.forEach(expectedString ->
+ assertThat(actualErrorListAsStringList, hasItem(containsString(expectedString.getMessage()))));
}
/**
@Test
public void testGivenDescriptorFiles_whenMainDescriptorWithDifferentImportStatementsIsHandled_noErrorsAreReported() {
final String mainDefinitionFile = "Definitions/descriptorFileWithValidImportStatements.yaml";
+ descriptorFileMap.put(mainDefinitionFile,
+ getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile)));
- final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, "Artifacts/descriptorCyclicReference.yaml");
- filesToHandleList.forEach(file ->
+ final List<String> importDescriptorToHandleList =
+ Collections.singletonList("Artifacts/descriptorCyclicReference.yaml");
+ importDescriptorToHandleList.forEach(file ->
descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
);
final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile);
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
+ assertThat("The handled files should be the same",
+ actualHandledFiles.keySet(), hasSize(importDescriptorToHandleList.size()));
assertThat("The handled files should be the same"
- , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
+ , actualHandledFiles.keySet(), containsInAnyOrder(importDescriptorToHandleList.toArray(new String[0]))
);
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
@Test
public void testGivenDescriptorFileWithNonexistentRelativeImport_whenIncorrectMainDescriptorIsHandled_aMissingFileErrorIsReported() {
final String mainDefinitionFile = "Definitions/MainWithNonexistentReferences.yaml";
-
- final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile,
+ descriptorFileMap
+ .put(mainDefinitionFile, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile)));
+ final List<String> filesToHandleList = Arrays.asList(
"Definitions/descriptorNonexistentImport.yaml", "Artifacts/descriptorCyclicReference.yaml");
filesToHandleList.forEach(file ->
descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
final List<ErrorMessage> expectedErrorList = new ArrayList<>();
expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
- String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
- "Definitions/descriptorCyclicReference.yaml"))
- );
+ Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/descriptorCyclicReference.yaml")));
expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
- String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
- "Definitions/descriptorCyclicReference.yaml"))
+ Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/descriptorCyclicReference.yaml"))
);
expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
- String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
- "Definitions/descriptorCyclicReference.yaml"))
+ Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/descriptorCyclicReference.yaml"))
);
final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
descriptorFileMap,
mainDefinitionFile);
- final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
+ final Map<String, ServiceTemplateReaderService> actualHandledFiles =
+ toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
- assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
+ assertThat("The handled files should be the same",
+ actualHandledFiles.keySet(), hasSize(filesToHandleList.size()));
assertThat("The handled files should be the same"
- , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
+ , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0]))
);
final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment;
import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
import org.onap.sdc.tosca.datatypes.model.Constraint;
+import org.onap.sdc.tosca.datatypes.model.DataType;
import org.onap.sdc.tosca.datatypes.model.EntrySchema;
import org.onap.sdc.tosca.datatypes.model.GroupDefinition;
import org.onap.sdc.tosca.datatypes.model.Import;
serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
}
+ public static void addDataType(final ServiceTemplate serviceTemplate, final String key,
+ final DataType nodeTypeValue) {
+ if (serviceTemplate == null) {
+ throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Data Type", SERVICE_TEMPLATE).build());
+ }
+
+ if (serviceTemplate.getData_types() == null) {
+ serviceTemplate.setData_types(new HashMap<>());
+ }
+
+ serviceTemplate.getData_types().put(key, nodeTypeValue);
+ }
+
+
/**
* Add relationship template.
*
</modules>
</profile>
+ <profile>
+ <id>catalog-be</id>
+ <activation>
+ <activeByDefault>false</activeByDefault>
+ </activation>
+ <modules>
+ <module>common-app-logging</module>
+ <module>common-app-api</module>
+ <module>common-be</module>
+ <module>catalog-dao</module>
+ <module>catalog-model</module>
+ <module>catalog-be</module>
+ </modules>
+ </profile>
+
<profile>
<id>onboarding</id>
<activation>