diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services')
6 files changed, 691 insertions, 656 deletions
diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java index a44b0196d4..4563e686e5 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java @@ -21,6 +21,8 @@ import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; public class HeatResourceUtil { @@ -31,9 +33,9 @@ public class HeatResourceUtil { private static final String PORT_RESOURCE_ID_REGEX_PREFIX = WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX; private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX = PORT_RESOURCE_ID_REGEX_PREFIX - + UNDERSCORE + "int_"+ WORDS_REGEX + UNDERSCORE; + + UNDERSCORE + "int_" + WORDS_REGEX + UNDERSCORE; private static final String SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX = - PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_"+ WORDS_REGEX + UNDERSCORE; + PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_" + WORDS_REGEX + UNDERSCORE; public static Optional<String> evaluateNetworkRoleFromResourceId(String resourceId, String resourceType) { @@ -56,17 +58,23 @@ public class HeatResourceUtil { } private static Optional<PortType> getPortType(String resourceType) { - if (resourceType.equals( - HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())) { + if (HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource() + .equals(resourceType)) { return Optional.of(PortType.VMI); - } else if (resourceType.equals( - HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) { + } else if (HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource().equals(resourceType)) { return Optional.of(PortType.PORT); } return Optional.empty(); } - public static Optional<String> extractNetworkRoleFromSubInterfaceId(String resourceId, + /** + * Extract network role from sub interface id optional. + * + * @param resourceId the resource id + * @param resourceType the resource type + * @return the optional + */ + public static Optional<String> extractNetworkRoleFromSubInterfaceId(String resourceId, String resourceType) { Optional<PortType> portType = getPortType(resourceType); if (portType.isPresent()) { @@ -79,19 +87,13 @@ public class HeatResourceUtil { return Optional.empty(); } + @AllArgsConstructor + @Getter private enum PortType { PORT("port"), VMI("vmi"); private String portTypeName; - - PortType(String portTypeName) { - this.portTypeName = portTypeName; - } - - public String getPortTypeName() { - return portTypeName; - } } private static String getNetworkRole(String portResourceId, String portIdRegex) { diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatStructureUtil.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatStructureUtil.java index ef09841727..b73c7e8b8b 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatStructureUtil.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatStructureUtil.java @@ -7,9 +7,9 @@ * 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. @@ -20,6 +20,12 @@ package org.openecomp.sdc.heat.services; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + import org.apache.commons.collections4.CollectionUtils; import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; import org.openecomp.core.validation.types.GlobalValidationContext; @@ -27,12 +33,6 @@ import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; - /** * Created by TALIO on 2/19/2017. */ @@ -42,77 +42,83 @@ public class HeatStructureUtil { // prevent instantiation } - /** - * Gets referenced values by function name. - * - * @param filename the filename - * @param functionName the function name - * @param propertyValue the property value - * @param globalContext the global context - * @return the referenced values by function name - */ - public static Set<String> getReferencedValuesByFunctionName(String filename, String functionName, - Object propertyValue, - GlobalValidationContext globalContext) { - Set<String> valuesNames = new HashSet<>(); - if (propertyValue instanceof Map) { - Map<String, Object> currPropertyMap = (Map<String, Object>) propertyValue; - if (currPropertyMap.containsKey(functionName)) { - Object getFunctionValue = currPropertyMap.get(functionName); - if (!(getFunctionValue instanceof String) && functionName.equals( - ResourceReferenceFunctions.GET_RESOURCE.getFunction())) { - globalContext.addMessage(filename, ErrorLevel.ERROR, ErrorMessagesFormatBuilder - .getErrorWithParameters(globalContext.getMessageCode(), - Messages.INVALID_GET_RESOURCE_SYNTAX.getErrorMessage(), - getFunctionValue == null ? "null" : getFunctionValue.toString())); - return valuesNames; - } - if (getFunctionValue instanceof String) { + /** + * Gets referenced values by function name. + * + * @param filename the filename + * @param functionName the function name + * @param propertyValue the property value + * @param globalContext the global context + * @return the referenced values by function name + */ + public static Set<String> getReferencedValuesByFunctionName(String filename, String functionName, + Object propertyValue, + GlobalValidationContext globalContext) { + Set<String> valuesNames = new HashSet<>(); + if (propertyValue instanceof Map) { + Map<String, Object> currPropertyMap = (Map<String, Object>) propertyValue; + if (currPropertyMap.containsKey(functionName)) { + Object getFunctionValue = currPropertyMap.get(functionName); + if (!(getFunctionValue instanceof String) && functionName.equals( + ResourceReferenceFunctions.GET_RESOURCE.getFunction())) { + globalContext.addMessage(filename, ErrorLevel.ERROR, ErrorMessagesFormatBuilder + .getErrorWithParameters(globalContext.getMessageCode(), + Messages.INVALID_GET_RESOURCE_SYNTAX.getErrorMessage(), + getFunctionValue == null ? "null" : getFunctionValue.toString())); + return valuesNames; + } + if (getFunctionValue instanceof String) { - if (functionName.equals(ResourceReferenceFunctions.GET_FILE.getFunction())) { - getFunctionValue = ((String) getFunctionValue).replace("file:///", ""); - } + if (functionName.equals(ResourceReferenceFunctions.GET_FILE.getFunction())) { + getFunctionValue = ((String) getFunctionValue).replace("file:///", ""); + } - valuesNames.add((String) getFunctionValue); - } else if (getFunctionValue instanceof List) { - if (CollectionUtils.isNotEmpty((List) getFunctionValue)) { - if (((List) getFunctionValue).get(0) instanceof String) { - valuesNames.add(((String) ((List) getFunctionValue).get(0)).replace("file:///", "")); + valuesNames.add((String) getFunctionValue); + } else if (getFunctionValue instanceof List) { + if (CollectionUtils.isNotEmpty((List) getFunctionValue)) { + if (((List) getFunctionValue).get(0) instanceof String) { + valuesNames.add(((String) ((List) getFunctionValue).get(0)).replace("file:///", "")); + } else { + valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName, + ((List) getFunctionValue).get(0), globalContext)); + } + + } + } else { + valuesNames.addAll( + getReferencedValuesByFunctionName(filename, functionName, getFunctionValue, + globalContext)); + } } else { - valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName, - ((List) getFunctionValue).get(0), globalContext)); + for (Map.Entry<String, Object> nestedPropertyMap : currPropertyMap.entrySet()) { + valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName, + nestedPropertyMap.getValue(), globalContext)); + } + } + } else if (propertyValue instanceof List) { + List propertyValueArray = (List) propertyValue; + for (Object propValue : propertyValueArray) { + valuesNames.addAll( + getReferencedValuesByFunctionName(filename, functionName, propValue, + globalContext)); } - - } - } else { - valuesNames.addAll( - getReferencedValuesByFunctionName(filename, functionName, getFunctionValue, - globalContext)); - } - } else { - for (Map.Entry<String, Object> nestedPropertyMap : currPropertyMap.entrySet()) { - valuesNames.addAll(getReferencedValuesByFunctionName(filename, functionName, - nestedPropertyMap.getValue(), globalContext)); } - } - } else if (propertyValue instanceof List) { - List propertyValueArray = (List) propertyValue; - for (Object propValue : propertyValueArray) { - valuesNames.addAll( - getReferencedValuesByFunctionName(filename, functionName, propValue, - globalContext)); - } - } - return valuesNames; - } + return valuesNames; + } - public static boolean isNestedResource(String resourceType) { - if(Objects.isNull(resourceType)){ - return false; + /** + * Is nested resource. + * + * @param resourceType the resource type + * @return the boolean + */ + public static boolean isNestedResource(String resourceType) { + if (Objects.isNull(resourceType)) { + return false; + } + return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml"); } - return resourceType.endsWith(".yaml") || resourceType.endsWith(".yml"); - } } diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/manifest/ManifestUtil.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/manifest/ManifestUtil.java index ef057a9fc9..8c6daac71e 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/manifest/ManifestUtil.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/manifest/ManifestUtil.java @@ -7,9 +7,9 @@ * 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. @@ -20,156 +20,151 @@ package org.openecomp.sdc.heat.services.manifest; - -import org.apache.commons.collections4.CollectionUtils; -import org.openecomp.sdc.heat.datatypes.manifest.FileData; -import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; - import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; + public class ManifestUtil { - /** - * Gets file and its env. - * - * @param manifestContent the manifest content - * @return the file and its env - */ - public static Map<String, FileData> getFileAndItsEnv(ManifestContent manifestContent) { - Map<String, FileData> fileEnvMap = new HashMap<>(); - scanFileEnvMap(null, manifestContent.getData(), fileEnvMap); - return fileEnvMap; - } - - - /** - * Scan file env map. - * - * @param fileData the file data - * @param fileDataList the file data list - * @param fileEnvMap the file env map - */ - public static void scanFileEnvMap(FileData fileData, List<FileData> fileDataList, - Map<String, FileData> fileEnvMap) { - - if (CollectionUtils.isEmpty(fileDataList)) { - return; + /** + * Gets file and its env. + * + * @param manifestContent the manifest content + * @return the file and its env + */ + public static Map<String, FileData> getFileAndItsEnv(ManifestContent manifestContent) { + Map<String, FileData> fileEnvMap = new HashMap<>(); + scanFileEnvMap(null, manifestContent.getData(), fileEnvMap); + return fileEnvMap; } - for (FileData childFileData : fileDataList) { - FileData.Type childType = childFileData.getType(); - if (fileData != null) { - if (childType != null && childType.equals(FileData.Type.HEAT_ENV)) { - fileEnvMap.put(fileData.getFile(), childFileData); + + /** + * Scan file env map. + * + * @param fileData the file data + * @param fileDataList the file data list + * @param fileEnvMap the file env map + */ + private static void scanFileEnvMap(FileData fileData, List<FileData> fileDataList, + Map<String, FileData> fileEnvMap) { + + if (CollectionUtils.isEmpty(fileDataList)) { + return; + } + + for (FileData childFileData : fileDataList) { + FileData.Type childType = childFileData.getType(); + if (fileData != null) { + if (childType != null && childType.equals(FileData.Type.HEAT_ENV)) { + fileEnvMap.put(fileData.getFile(), childFileData); + } + } + scanFileEnvMap(childFileData, childFileData.getData(), fileEnvMap); } - } - scanFileEnvMap(childFileData, childFileData.getData(), fileEnvMap); - } - } - - - /** - * Gets file type map. - * - * @param manifestContent the manifest content - * @return the file type map - */ - public static Map<String, FileData.Type> getFileTypeMap(ManifestContent manifestContent) { - Map<String, FileData.Type> fileTypeMap = new HashMap<>(); - scanFileTypeMap(null, manifestContent.getData(), fileTypeMap); - return fileTypeMap; - } - - private static FileData.Type scanFileTypeMap(FileData fileData, List<FileData> data, - Map<String, FileData.Type> fileTypeMap) { - if (fileData != null) { - fileTypeMap.put(fileData.getFile(), fileData.getType()); - } - if (data == null) { - return null; } - for (FileData chileFileData : data) { - FileData.Type type = scanFileTypeMap(chileFileData, chileFileData.getData(), fileTypeMap); - if (type != null) { - return type; - } + + /** + * Gets file type map. + * + * @param manifestContent the manifest content + * @return the file type map + */ + public static Map<String, FileData.Type> getFileTypeMap(ManifestContent manifestContent) { + Map<String, FileData.Type> fileTypeMap = new HashMap<>(); + scanFileTypeMap(null, manifestContent.getData(), fileTypeMap); + return fileTypeMap; } - return null; - } + private static FileData.Type scanFileTypeMap(FileData fileData, List<FileData> data, + Map<String, FileData.Type> fileTypeMap) { + if (fileData != null) { + fileTypeMap.put(fileData.getFile(), fileData.getType()); + } + if (data == null) { + return null; + } - /** - * Gets artifacts. - * - * @param manifestContent the manifest content - * @return the artifacts - */ - public static Set<String> getArtifacts(ManifestContent manifestContent) { - Set<String> artifacts = new HashSet<>(); - scanArtifacts(null, manifestContent.getData(), artifacts); + for (FileData chileFileData : data) { + FileData.Type type = scanFileTypeMap(chileFileData, chileFileData.getData(), fileTypeMap); + if (type != null) { + return type; + } + } + return null; + } - return artifacts; - } + /** + * Gets artifacts. + * + * @param manifestContent the manifest content + * @return the artifacts + */ + public static Set<String> getArtifacts(ManifestContent manifestContent) { + Set<String> artifacts = new HashSet<>(); + scanArtifacts(null, manifestContent.getData(), artifacts); - private static void scanArtifacts(FileData fileData, List<FileData> data, Set<String> artifacts) { - if (fileData != null && fileData.getType() != null) { - if (isArtifact(fileData)) { - artifacts.add(fileData.getFile()); - } + return artifacts; } - if (data == null) { - return; - } + private static void scanArtifacts(FileData fileData, List<FileData> data, Set<String> artifacts) { + if (fileData != null && fileData.getType() != null) { + if (isArtifact(fileData)) { + artifacts.add(fileData.getFile()); + } + } - for (FileData chileFileData : data) { - scanArtifacts(chileFileData, chileFileData.getData(), artifacts); - } - } - - private static boolean isArtifact(FileData fileData) { - if (FileData.Type.valueOf(fileData.getType().name()) != null - && !fileData.getType().equals(FileData.Type.HEAT) - && !fileData.getType().equals(FileData.Type.HEAT_ENV) - && !fileData.getType().equals(FileData.Type.HEAT_NET) - && !fileData.getType().equals(FileData.Type.HEAT_VOL)) { - return true; + if (data == null) { + return; + } + + for (FileData chileFileData : data) { + scanArtifacts(chileFileData, chileFileData.getData(), artifacts); + } } - return false; - } - - /** - * Gets base files. - * - * @param manifestContent the manifest content - * @return the base files - */ - public static Set<String> getBaseFiles(ManifestContent manifestContent) { - Set<String> baseFiles = new HashSet<>(); - scanBase(null, manifestContent.getData(), baseFiles); - return baseFiles; - } - - private static void scanBase(FileData fileData, List<FileData> data, Set<String> baseFiles) { - if (fileData != null && fileData.getBase() != null) { - if (fileData.getBase()) { - baseFiles.add(fileData.getFile()); - } + + private static boolean isArtifact(FileData fileData) { + return FileData.Type.valueOf(fileData.getType().name()) != null + && !fileData.getType().equals(FileData.Type.HEAT) + && !fileData.getType().equals(FileData.Type.HEAT_ENV) + && !fileData.getType().equals(FileData.Type.HEAT_NET) + && !fileData.getType().equals(FileData.Type.HEAT_VOL); } - if (data == null) { - return; + /** + * Gets base files. + * + * @param manifestContent the manifest content + * @return the base files + */ + public static Set<String> getBaseFiles(ManifestContent manifestContent) { + Set<String> baseFiles = new HashSet<>(); + scanBase(null, manifestContent.getData(), baseFiles); + return baseFiles; } - for (FileData chileFileData : data) { - scanBase(chileFileData, chileFileData.getData(), baseFiles); + private static void scanBase(FileData fileData, List<FileData> data, Set<String> baseFiles) { + if (fileData != null && fileData.getBase() != null) { + if (fileData.getBase()) { + baseFiles.add(fileData.getFile()); + } + } + + if (data == null) { + return; + } + + for (FileData chileFileData : data) { + scanBase(chileFileData, chileFileData.getData(), baseFiles); + } } - } } diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManager.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManager.java index e8fe034176..936f02be67 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManager.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManager.java @@ -12,10 +12,18 @@ * 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. -*/ + */ package org.openecomp.sdc.heat.services.tree; +import java.io.InputStream; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; @@ -31,257 +39,247 @@ import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import java.io.InputStream; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; - public class HeatTreeManager { - private static final Logger LOGGER = LoggerFactory.getLogger(HeatTreeManager.class); - - - private FileContentHandler heatContentMap = new FileContentHandler(); - private byte[] manifest; - private HeatStructureTree tree = new HeatStructureTree(); - private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>(); - private Map<String, Artifact> artifactRef = new HashMap<>(); - private Map<String, Artifact> candidateOrphanArtifacts = new HashMap<>(); - private Map<String, HeatStructureTree> nestedFiles = new HashMap<>(); - private Map<HeatStructureTree, HeatStructureTree> volumeFileToParent = new HashMap<>(); - private Map<HeatStructureTree, HeatStructureTree> networkFileToParent = new HashMap<>(); - private Set<String> manifestFiles = new HashSet<>(); - - /** - * Add file. - * - * @param fileName the file name - * @param content the content - */ - public void addFile(String fileName, InputStream content) { - if (fileName.equals(SdcCommon.MANIFEST_NAME)) { - manifest = FileUtils.toByteArray(content); - - } else { - heatContentMap.addFile(fileName, content); + private static final Logger LOGGER = LoggerFactory.getLogger(HeatTreeManager.class); + + + private FileContentHandler heatContentMap = new FileContentHandler(); + private byte[] manifest; + private HeatStructureTree tree = new HeatStructureTree(); + private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>(); + private Map<String, Artifact> artifactRef = new HashMap<>(); + private Map<String, Artifact> candidateOrphanArtifacts = new HashMap<>(); + private Map<String, HeatStructureTree> nestedFiles = new HashMap<>(); + private Map<HeatStructureTree, HeatStructureTree> volumeFileToParent = new HashMap<>(); + private Map<HeatStructureTree, HeatStructureTree> networkFileToParent = new HashMap<>(); + private Set<String> manifestFiles = new HashSet<>(); + + /** + * Add file. + * + * @param fileName the file name + * @param content the content + */ + public void addFile(String fileName, InputStream content) { + if (fileName.equals(SdcCommon.MANIFEST_NAME)) { + manifest = FileUtils.toByteArray(content); + + } else { + heatContentMap.addFile(fileName, content); + } + } + + /** + * Create tree. + */ + public void createTree() { + if (manifest == null) { + LOGGER.error("Missing manifest file in the zip."); + return; + } + ManifestContent manifestData = + JsonUtil.json2Object(new String(manifest), ManifestContent.class); + scanTree(null, manifestData.getData()); + addNonNestedVolumeNetworkToTree(volumeFileToParent, nestedFiles.keySet(), true); + addNonNestedVolumeNetworkToTree(networkFileToParent, nestedFiles.keySet(), false); + handleOrphans(); + + tree = fileTreeRef.get(SdcCommon.PARENT); } - } - - /** - * Create tree. - */ - public void createTree() { - if (manifest == null) { - LOGGER.error("Missing manifest file in the zip."); - return; + + private void handleOrphans() { + tree = fileTreeRef.get(SdcCommon.PARENT); + candidateOrphanArtifacts.forEach((key, value) -> tree.addArtifactToArtifactList(value)); + nestedFiles + .values().stream().filter(tree.getHeat()::contains) + .forEach(tree.getHeat()::remove); + + heatContentMap.getFileList().stream().filter(this::isNotInManifestFiles) + .forEach(this::addTreeOther); } - ManifestContent manifestData = - JsonUtil.json2Object(new String(manifest), ManifestContent.class); - scanTree(null, manifestData.getData()); - addNonNestedVolumeNetworkToTree(volumeFileToParent, nestedFiles.keySet(), true); - addNonNestedVolumeNetworkToTree(networkFileToParent, nestedFiles.keySet(), false); - handleOrphans(); - - tree = fileTreeRef.get(SdcCommon.PARENT); - } - - private void handleOrphans() { - tree = fileTreeRef.get(SdcCommon.PARENT); - candidateOrphanArtifacts.entrySet().stream() - .forEach(entry -> tree.addArtifactToArtifactList(entry.getValue())); - nestedFiles - .values().stream().filter(tree.getHeat()::contains) - .forEach(tree.getHeat()::remove); - - heatContentMap.getFileList().stream().filter(this::isNotInManifestFiles) - .forEach(this::addTreeOther); - } - - private boolean isNotInManifestFiles(String fileName) { - return !manifestFiles.contains(fileName); - } - - private void addTreeOther(String fileName) { - if (tree.getOther() == null) { - tree.setOther(new HashSet<>()); + + private boolean isNotInManifestFiles(String fileName) { + return !manifestFiles.contains(fileName); } - HeatStructureTree other = new HeatStructureTree(fileName, false); - fileTreeRef.put(fileName, other); - tree.getOther().add(other); - } + private void addTreeOther(String fileName) { + if (tree.getOther() == null) { + tree.setOther(new HashSet<>()); + } + HeatStructureTree other = new HeatStructureTree(fileName, false); + fileTreeRef.put(fileName, other); + tree.getOther().add(other); + } - private void handleHeatContentReference(HeatStructureTree fileHeatStructureTree, - GlobalValidationContext globalContext) { - String fileName = fileHeatStructureTree.getFileName(); + private void handleHeatContentReference(HeatStructureTree fileHeatStructureTree, + GlobalValidationContext globalContext) { - try (InputStream fileContent = this.heatContentMap.getFileContent(fileName)) { - HeatOrchestrationTemplate hot = - new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class); + String fileName = fileHeatStructureTree.getFileName(); + try (InputStream fileContent = this.heatContentMap.getFileContent(fileName)) { + HeatOrchestrationTemplate hot = + new YamlUtil().yamlToObject(fileContent, HeatOrchestrationTemplate.class); - Set<String> nestedSet = HeatTreeManagerUtil.getNestedFiles(fileName, hot, globalContext); - addHeatNestedFiles(fileHeatStructureTree, nestedSet); + Set<String> nestedSet = HeatTreeManagerUtil.getNestedFiles(hot); + addHeatNestedFiles(fileHeatStructureTree, nestedSet); - Set<String> artifactSet = HeatTreeManagerUtil.getArtifactFiles(fileName, hot, globalContext); - addHeatArtifactFiles(fileHeatStructureTree, artifactSet); - } catch (Exception ignore) { - LOGGER.debug("Invalid YAML received. No need to process content reference - ignoring", ignore); - } - } - - - private void addHeatArtifactFiles(HeatStructureTree fileHeatStructureTree, - Set<String> artifactSet) { - Artifact artifact; - for (String artifactName : artifactSet) { - FileData.Type type = - candidateOrphanArtifacts.get(artifactName) != null ? candidateOrphanArtifacts - .get(artifactName).getType() : null; - artifact = new Artifact(artifactName, type); - artifactRef.put(artifactName, artifact); - candidateOrphanArtifacts.remove(artifactName); - fileHeatStructureTree.addArtifactToArtifactList(artifact); - } - } - - - private void addHeatNestedFiles(HeatStructureTree fileHeatStructureTree, Set<String> nestedSet) { - HeatStructureTree childHeatStructureTree; - for (String nestedName : nestedSet) { - childHeatStructureTree = fileTreeRef.get(nestedName); - if (childHeatStructureTree == null) { - childHeatStructureTree = new HeatStructureTree(); - childHeatStructureTree.setFileName(nestedName); - fileTreeRef.put(nestedName, childHeatStructureTree); - } - fileHeatStructureTree.addHeatStructureTreeToNestedHeatList(childHeatStructureTree); - nestedFiles.put(childHeatStructureTree.getFileName(), childHeatStructureTree); + Set<String> artifactSet = HeatTreeManagerUtil.getArtifactFiles(fileName, hot, globalContext); + addHeatArtifactFiles(fileHeatStructureTree, artifactSet); + } catch (Exception exp) { + LOGGER.debug("Invalid YAML received. No need to process content reference - ignoring", exp); + } } - } - - - /** - * Add errors. - * - * @param validationErrors the validation errors - */ - public void addErrors(Map<String, List<ErrorMessage>> validationErrors) { - - validationErrors.entrySet().stream().filter(entry ->{ - return fileTreeRef.get(entry.getKey()) != null; - }).forEach(entry -> entry.getValue().stream().forEach(fileTreeRef.get(entry.getKey())::addErrorToErrorsList)); - - validationErrors.entrySet().stream().filter(entry -> { - return artifactRef.get(entry.getKey()) != null; - }).forEach(entry -> artifactRef.get(entry.getKey()).setErrors(entry.getValue())); - - } - - /** - * Scan tree. - * - * @param parent the parent - * @param data the data - */ - public void scanTree(String parent, List<FileData> data) { - String fileName; - FileData.Type type; - HeatStructureTree parentHeatStructureTree; - HeatStructureTree fileHeatStructureTree; - HeatStructureTree childHeatStructureTree; - Artifact artifact; - if (parent == null) { - parentHeatStructureTree = new HeatStructureTree(); - fileTreeRef.put(SdcCommon.PARENT, parentHeatStructureTree); - } else { - parentHeatStructureTree = fileTreeRef.get(parent); + + + private void addHeatArtifactFiles(HeatStructureTree fileHeatStructureTree, + Set<String> artifactSet) { + Artifact artifact; + for (String artifactName : artifactSet) { + FileData.Type type = + candidateOrphanArtifacts.get(artifactName) != null ? candidateOrphanArtifacts + .get(artifactName).getType() : null; + artifact = new Artifact(artifactName, type); + artifactRef.put(artifactName, artifact); + candidateOrphanArtifacts.remove(artifactName); + fileHeatStructureTree.addArtifactToArtifactList(artifact); + } } - for (FileData fileData : data) { - fileName = fileData.getFile(); - manifestFiles.add(fileName); - type = fileData.getType(); - if (Objects.nonNull(type) && FileData.Type.HEAT.equals(type)) { - fileHeatStructureTree = fileTreeRef.get(fileName); - if (fileHeatStructureTree == null) { - fileHeatStructureTree = new HeatStructureTree(); - fileTreeRef.put(fileName, fileHeatStructureTree); + private void addHeatNestedFiles(HeatStructureTree fileHeatStructureTree, Set<String> nestedSet) { + HeatStructureTree childHeatStructureTree; + for (String nestedName : nestedSet) { + childHeatStructureTree = fileTreeRef.get(nestedName); + if (childHeatStructureTree == null) { + childHeatStructureTree = new HeatStructureTree(); + childHeatStructureTree.setFileName(nestedName); + fileTreeRef.put(nestedName, childHeatStructureTree); + } + fileHeatStructureTree.addHeatStructureTreeToNestedHeatList(childHeatStructureTree); + nestedFiles.put(childHeatStructureTree.getFileName(), childHeatStructureTree); } - fileHeatStructureTree.setFileName(fileName); - fileHeatStructureTree.setBase(fileData.getBase()); - fileHeatStructureTree.setType(type); - handleHeatContentReference(fileHeatStructureTree, null); - parentHeatStructureTree.addHeatToHeatList(fileHeatStructureTree); - if (fileData.getData() != null) { - scanTree(fileName, fileData.getData()); + } + + + /** + * Add errors. + * + * @param validationErrors the validation errors + */ + public void addErrors(Map<String, List<ErrorMessage>> validationErrors) { + + validationErrors.entrySet().stream() + .filter(entry -> fileTreeRef.get(entry.getKey()) != null) + .forEach(entry -> entry.getValue().forEach(fileTreeRef.get(entry.getKey())::addErrorToErrorsList)); + + validationErrors.entrySet().stream() + .filter(entry -> artifactRef.get(entry.getKey()) != null) + .forEach(entry -> artifactRef.get(entry.getKey()).setErrors(entry.getValue())); + + } + + /** + * Scan tree. + * + * @param parent the parent + * @param data the data + */ + public void scanTree(String parent, List<FileData> data) { + String fileName; + FileData.Type type; + HeatStructureTree parentHeatStructureTree; + HeatStructureTree fileHeatStructureTree; + HeatStructureTree childHeatStructureTree; + Artifact artifact; + if (parent == null) { + parentHeatStructureTree = new HeatStructureTree(); + fileTreeRef.put(SdcCommon.PARENT, parentHeatStructureTree); + } else { + parentHeatStructureTree = fileTreeRef.get(parent); } - } else { - childHeatStructureTree = new HeatStructureTree(); - childHeatStructureTree.setFileName(fileName); - childHeatStructureTree.setBase(fileData.getBase()); - childHeatStructureTree.setType(type); - fileTreeRef.put(childHeatStructureTree.getFileName(), childHeatStructureTree); - - if (type == null) { - parentHeatStructureTree.addOtherToOtherList(childHeatStructureTree); - } else if (FileData.Type.HEAT_NET.equals(type)) { - networkFileToParent.put(childHeatStructureTree, parentHeatStructureTree); - if (fileData.getData() != null) { - scanTree(fileName, fileData.getData()); - } - handleHeatContentReference(childHeatStructureTree, null); - - } else if (FileData.Type.HEAT_VOL.equals(type)) { - volumeFileToParent.put(childHeatStructureTree, parentHeatStructureTree); - if (fileData.getData() != null) { - scanTree(fileName, fileData.getData()); - } - handleHeatContentReference(childHeatStructureTree, null); - } else if (FileData.Type.HEAT_ENV.equals(type)) { - if (parentHeatStructureTree != null && parentHeatStructureTree.getFileName() != null) { - parentHeatStructureTree.setEnv(childHeatStructureTree); - } else { - if (parentHeatStructureTree.getOther() == null) { - parentHeatStructureTree.setOther(new HashSet<>()); + + for (FileData fileData : data) { + fileName = fileData.getFile(); + manifestFiles.add(fileName); + type = fileData.getType(); + + if (Objects.nonNull(type) && FileData.Type.HEAT.equals(type)) { + fileHeatStructureTree = fileTreeRef.get(fileName); + if (fileHeatStructureTree == null) { + fileHeatStructureTree = new HeatStructureTree(); + fileTreeRef.put(fileName, fileHeatStructureTree); + } + fileHeatStructureTree.setFileName(fileName); + fileHeatStructureTree.setBase(fileData.getBase()); + fileHeatStructureTree.setType(type); + handleHeatContentReference(fileHeatStructureTree, null); + parentHeatStructureTree.addHeatToHeatList(fileHeatStructureTree); + if (fileData.getData() != null) { + scanTree(fileName, fileData.getData()); + } + } else { + childHeatStructureTree = new HeatStructureTree(); + childHeatStructureTree.setFileName(fileName); + childHeatStructureTree.setBase(fileData.getBase()); + childHeatStructureTree.setType(type); + fileTreeRef.put(childHeatStructureTree.getFileName(), childHeatStructureTree); + + if (type == null) { + parentHeatStructureTree.addOtherToOtherList(childHeatStructureTree); + } else if (FileData.Type.HEAT_NET.equals(type)) { + networkFileToParent.put(childHeatStructureTree, parentHeatStructureTree); + if (fileData.getData() != null) { + scanTree(fileName, fileData.getData()); + } + handleHeatContentReference(childHeatStructureTree, null); + + } else if (FileData.Type.HEAT_VOL.equals(type)) { + volumeFileToParent.put(childHeatStructureTree, parentHeatStructureTree); + if (fileData.getData() != null) { + scanTree(fileName, fileData.getData()); + } + handleHeatContentReference(childHeatStructureTree, null); + } else if (FileData.Type.HEAT_ENV.equals(type)) { + if (parentHeatStructureTree != null && parentHeatStructureTree.getFileName() != null) { + parentHeatStructureTree.setEnv(childHeatStructureTree); + } else { + if (parentHeatStructureTree.getOther() == null) { + parentHeatStructureTree.setOther(new HashSet<>()); + } + parentHeatStructureTree.getOther().add(childHeatStructureTree); + } + } else { + artifact = new Artifact(fileName, type); + if (!artifactRef.keySet().contains(fileName)) { + artifactRef.put(fileName, artifact); + candidateOrphanArtifacts.put(fileName, artifact); + } + } } - parentHeatStructureTree.getOther().add(childHeatStructureTree); - } - } else { - artifact = new Artifact(fileName, type); - if (!artifactRef.keySet().contains(fileName)) { - artifactRef.put(fileName, artifact); - candidateOrphanArtifacts.put(fileName, artifact); - } } - } } - } - - - private void addNonNestedVolumeNetworkToTree( - Map<HeatStructureTree, HeatStructureTree> netVolToParent, Set<String> nestedFileNames, - boolean isVolume) { - for (Map.Entry<HeatStructureTree, HeatStructureTree> entry : netVolToParent.entrySet()) { - HeatStructureTree netOrVolNode = entry.getKey(); - HeatStructureTree parent = entry.getValue(); - if (!nestedFileNames.contains(netOrVolNode.getFileName())) { - if (isVolume) { - parent.addVolumeFileToVolumeList(netOrVolNode); - } else { - parent.addNetworkToNetworkList(netOrVolNode); + + + private void addNonNestedVolumeNetworkToTree( + Map<HeatStructureTree, HeatStructureTree> netVolToParent, Set<String> nestedFileNames, + boolean isVolume) { + for (Map.Entry<HeatStructureTree, HeatStructureTree> entry : netVolToParent.entrySet()) { + HeatStructureTree netOrVolNode = entry.getKey(); + HeatStructureTree parent = entry.getValue(); + if (!nestedFileNames.contains(netOrVolNode.getFileName())) { + if (isVolume) { + parent.addVolumeFileToVolumeList(netOrVolNode); + } else { + parent.addNetworkToNetworkList(netOrVolNode); + } + } } - } } - } - public HeatStructureTree getTree() { - return tree; - } + public HeatStructureTree getTree() { + return tree; + } } diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtil.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtil.java index 7a2dfc971b..8c6d1354c4 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtil.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerUtil.java @@ -12,11 +12,17 @@ * 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. -*/ + */ package org.openecomp.sdc.heat.services.tree; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.utilities.file.FileContentHandler; @@ -30,196 +36,185 @@ import org.openecomp.sdc.heat.datatypes.model.PropertiesMapKeyTypes; import org.openecomp.sdc.heat.datatypes.model.Resource; import org.openecomp.sdc.heat.services.HeatStructureUtil; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Objects; -import java.util.Set; - public class HeatTreeManagerUtil { - private static final String TYPE = "type"; - private HeatTreeManagerUtil() { - - } - - /** - * Init heat tree manager heat tree manager. - * - * @param fileContentMap the file content map - * @return the heat tree manager - */ - public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) { - - HeatTreeManager heatTreeManager = new HeatTreeManager(); - fileContentMap.getFileList().stream().forEach( - fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName))); - - return heatTreeManager; - } - - /** - * Gets nested files. - * - * @param filename the filename - * @param hot the hot - * @param globalContext the global context - * @return the nested files - */ - public static Set<String> getNestedFiles(String filename, HeatOrchestrationTemplate hot, - GlobalValidationContext globalContext) { - Set<String> nestedFileList = new HashSet<>(); - hot.getResources().values().stream().filter( - resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml")) - .forEach(resource -> nestedFileList.add(resource.getType())); - - Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot); - nestedFileList.addAll(resourceDefNestedFiles); - return nestedFileList; - } - - /** - * Gets artifact files. - * - * @param filename the filename - * @param hot the hot - * @param globalContext the global context - * @return the artifact files - */ - public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot, - GlobalValidationContext globalContext) { - Set<String> artifactSet = new HashSet<>(); - Collection<Resource> resourcesValue = - hot.getResources() == null ? null : hot.getResources().values(); - if (CollectionUtils.isNotEmpty(resourcesValue)) { - for (Resource resource : resourcesValue) { - Collection<Object> properties = - resource.getProperties() == null ? null : resource.getProperties().values(); - - artifactSet.addAll(getArtifactsFromPropertiesAndAddInArtifactSet(properties, - filename, globalContext)); - } + private static final String TYPE = "type"; + + private HeatTreeManagerUtil() { + } - return artifactSet; - } - - private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties, - String filename, - GlobalValidationContext globalContext ){ - Set<String> artifactSet = new HashSet<>(); - if (CollectionUtils.isNotEmpty(properties)) { - - for (Object property : properties) { - Set<String> artifactNames = - HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property, - globalContext); - artifactSet.addAll(artifactNames); - } + + /** + * Init heat tree manager heat tree manager. + * + * @param fileContentMap the file content map + * @return the heat tree manager + */ + public static HeatTreeManager initHeatTreeManager(FileContentHandler fileContentMap) { + + HeatTreeManager heatTreeManager = new HeatTreeManager(); + fileContentMap.getFileList().forEach( + fileName -> heatTreeManager.addFile(fileName, fileContentMap.getFileContent(fileName))); + + return heatTreeManager; } - return artifactSet; - } - - private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) { - Set<String> resourceDefNestedFiles = new HashSet<>(); - hot.getResources() - .entrySet().stream().filter(entry -> entry.getValue().getType() - .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) - .filter(entry -> - getResourceDef(entry.getValue()) != null - && HeatStructureUtil.isNestedResource( - getResourceDef(entry.getValue()) - .getType())) - .forEach(entry -> resourceDefNestedFiles.add( - getResourceDef( entry.getValue()).getType())); - return resourceDefNestedFiles; - } - - /** - * Gets resource def. - * - * @param resource the resource - * @return the resource def - */ - @SuppressWarnings("unchecked") - public static Resource getResourceDef( Resource resource) { - Resource resourceDef = null; - Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null - : (Map<String, Object>) resource.getProperties().get( - PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); - if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) { - Object resourceDefType = resourceDefValueMap.get(TYPE); - if ( resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) { - resourceDef = new Resource(); - resourceDef.setType((String) resourceDefType); - //noinspection unchecked - resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties")); - } + /** + * Will verify the resource type and if type is of nested will return the nested file name + * + * @param hot Containing all translated data from heat file + * @return the nested files + */ + public static Set<String> getNestedFiles(HeatOrchestrationTemplate hot) { + Set<String> nestedFileList = new HashSet<>(); + hot.getResources().values().stream().filter( + resource -> resource.getType().endsWith(".yaml") || resource.getType().endsWith(".yml")) + .forEach(resource -> nestedFileList.add(resource.getType())); + + Set<String> resourceDefNestedFiles = getResourceDefNestedFiles(hot); + nestedFileList.addAll(resourceDefNestedFiles); + return nestedFileList; + } + + /** + * Verify if any artifact present in file whose detail is provided and return Artifact name + * + * @param filename name of file where artifact is too be looked for + * @param hot translated heat data + * @param globalContext the global context + * @return the artifact files name + */ + public static Set<String> getArtifactFiles(String filename, HeatOrchestrationTemplate hot, + GlobalValidationContext globalContext) { + Set<String> artifactSet = new HashSet<>(); + Collection<Resource> resourcesValue = + hot.getResources() == null ? null : hot.getResources().values(); + if (CollectionUtils.isNotEmpty(resourcesValue)) { + for (Resource resource : resourcesValue) { + Collection<Object> properties = + resource.getProperties() == null ? null : resource.getProperties().values(); + + artifactSet.addAll(getArtifactsFromPropertiesAndAddInArtifactSet(properties, + filename, globalContext)); + } + } + return artifactSet; + } + + private static Set<String> getArtifactsFromPropertiesAndAddInArtifactSet(Collection<Object> properties, + String filename, + GlobalValidationContext globalContext) { + Set<String> artifactSet = new HashSet<>(); + if (CollectionUtils.isNotEmpty(properties)) { + + for (Object property : properties) { + Set<String> artifactNames = + HeatStructureUtil.getReferencedValuesByFunctionName(filename, "get_file", property, + globalContext); + artifactSet.addAll(artifactNames); + } + } + + return artifactSet; + } + private static Set<String> getResourceDefNestedFiles(HeatOrchestrationTemplate hot) { + Set<String> resourceDefNestedFiles = new HashSet<>(); + hot.getResources() + .entrySet().stream().filter(entry -> entry.getValue().getType() + .equals(HeatResourcesTypes.RESOURCE_GROUP_RESOURCE_TYPE.getHeatResource())) + .filter(entry -> + getResourceDef(entry.getValue()) != null + && HeatStructureUtil.isNestedResource( + getResourceDef(entry.getValue()) + .getType())) + .forEach(entry -> resourceDefNestedFiles.add( + getResourceDef(entry.getValue()).getType())); + return resourceDefNestedFiles; } - return resourceDef; - } - - @SuppressWarnings("unchecked") - public static void checkResourceGroupTypeValid(String filename, String resourceName, - Resource resource, - GlobalValidationContext globalContext) { - Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null - : (Map<String, Object>) resource.getProperties().get( - PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); - if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) { - Object resourceDefType = resourceDefValueMap.get(TYPE); - if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String) ) { - globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder + + /** + * Gets resource def. + * + * @param resource the resource + * @return the resource def + */ + @SuppressWarnings("unchecked") + public static Resource getResourceDef(Resource resource) { + Resource resourceDef = null; + Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null + : (Map<String, Object>) resource.getProperties().get( + PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); + if (MapUtils.isNotEmpty(resourceDefValueMap)) { + Object resourceDefType = resourceDefValueMap.get(TYPE); + if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) { + resourceDef = new Resource(); + resourceDef.setType((String) resourceDefType); + //noinspection unchecked + resourceDef.setProperties((Map<String, Object>) resourceDefValueMap.get("properties")); + } + + } + return resourceDef; + } + + @SuppressWarnings("unchecked") + public static void checkResourceGroupTypeValid(String filename, String resourceName, + Resource resource, + GlobalValidationContext globalContext) { + Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null + : (Map<String, Object>) resource.getProperties().get( + PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); + if (MapUtils.isNotEmpty(resourceDefValueMap)) { + Object resourceDefType = resourceDefValueMap.get(TYPE); + if (Objects.nonNull(resourceDefType) && !(resourceDefType instanceof String)) { + globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder .getErrorWithParameters( globalContext.getMessageCode(), Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(), resourceName, resourceDefType.toString())); - } + } + } } - } - - @SuppressWarnings("unchecked") - public static void checkResourceTypeValid(String filename, String resourceName, - Resource resource, - GlobalValidationContext globalContext) { - Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null - : (Map<String, Object>) resource.getProperties().get( - PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); - if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) { - Object resourceDefType = resourceDefValueMap.get(TYPE); - if (Objects.isNull(resourceDefType)) { - globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder + + @SuppressWarnings("unchecked") + public static void checkResourceTypeValid(String filename, String resourceName, + Resource resource, + GlobalValidationContext globalContext) { + Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null + : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); + if (MapUtils.isNotEmpty(resourceDefValueMap)) { + Object resourceDefType = resourceDefValueMap.get(TYPE); + if (Objects.isNull(resourceDefType)) { + globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder .getErrorWithParameters( globalContext.getMessageCode(), Messages.INVALID_RESOURCE_TYPE.getErrorMessage(), "null", resourceName)); - } + } + } } - } - - public static boolean isResourceGroupTypeNested(String resourceDefType) { - return HeatStructureUtil.isNestedResource(resourceDefType); - } - - public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName, - Resource resource, - GlobalValidationContext globalContext) { - //noinspection unchecked - Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null - : (Map<String, Object>) resource.getProperties().get( - PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); - if (resourceDefValueMap != null && MapUtils.isNotEmpty(resourceDefValueMap) ) { - Object resourceDefType = resourceDefValueMap.get(TYPE); - if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) { - - globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder + + public static boolean isResourceGroupTypeNested(String resourceDefType) { + return HeatStructureUtil.isNestedResource(resourceDefType); + } + + public static boolean checkIfResourceGroupTypeIsNested(String filename, String resourceName, + Resource resource, + GlobalValidationContext globalContext) { + //noinspection unchecked + Map<String, Object> resourceDefValueMap = resource.getProperties() == null ? null + : (Map<String, Object>) resource.getProperties().get(PropertiesMapKeyTypes.RESOURCE_DEF.getKeyMap()); + if (MapUtils.isNotEmpty(resourceDefValueMap)) { + Object resourceDefType = resourceDefValueMap.get(TYPE); + if (resourceDefType instanceof String && isResourceGroupTypeNested((String) resourceDefType)) { + globalContext.addMessage(filename, ErrorLevel.WARNING, ErrorMessagesFormatBuilder .getErrorWithParameters( globalContext.getMessageCode(), Messages.INVALID_RESOURCE_GROUP_TYPE.getErrorMessage(), resourceName, resourceDefType.toString())); - return true; - } + return true; + } + } + return false; } - return false; - } } diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManager.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManager.java index c711c72faf..944ab62f9f 100644 --- a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManager.java +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/tree/ToscaTreeManager.java @@ -1,9 +1,20 @@ -package org.openecomp.sdc.heat.services.tree; +/* + * Copyright © 2017-2018 European Support Limited + * + * 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. +**/ -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.common.utils.SdcCommon; -import org.openecomp.sdc.datatypes.error.ErrorMessage; -import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +package org.openecomp.sdc.heat.services.tree; import java.io.File; import java.util.HashMap; @@ -12,56 +23,84 @@ import java.util.Map; import java.util.Objects; import java.util.regex.Pattern; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; + +/** + * The type Tosca tree manager. + */ public class ToscaTreeManager { - private FileContentHandler csarContentMap = new FileContentHandler(); - private HeatStructureTree tree = new HeatStructureTree(); - private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>(); + private FileContentHandler csarContentMap = new FileContentHandler(); + private HeatStructureTree tree = new HeatStructureTree(); + private Map<String, HeatStructureTree> fileTreeRef = new HashMap<>(); - public void addFile(String fileName, byte[] content) { - if (!fileName.equals(SdcCommon.CSAR_MANIFEST_NAME)) { - csarContentMap.addFile(fileName, content); - } - } - public void createTree() { - for (Map.Entry<String, byte[]> fileEntry : csarContentMap.getFiles().entrySet()) { - String[] splitFilename = getFullFileNameAsArray(fileEntry.getKey()); - addFileToTree(splitFilename, 0, splitFilename[0], tree); + /** + * Add file. + * + * @param fileName the file name + * @param content the content + */ + public void addFile(String fileName, byte[] content) { + if (!fileName.equals(SdcCommon.CSAR_MANIFEST_NAME)) { + csarContentMap.addFile(fileName, content); + } } - } - private void addFileToTree(String[] splitFilename, int startIndex, String fullFileName, - HeatStructureTree parent) { - fileTreeRef.putIfAbsent(fullFileName, new HeatStructureTree()); - HeatStructureTree heatStructureTree = fileTreeRef.get(fullFileName); - heatStructureTree.setFileName(splitFilename[startIndex]); - if (startIndex < splitFilename.length - 1) { - addFileToTree(splitFilename, startIndex + 1, - getFullFileName(fullFileName, splitFilename[startIndex + 1]), heatStructureTree); + /** + * Create tree. + */ + public void createTree() { + for (Map.Entry<String, byte[]> fileEntry : csarContentMap.getFiles().entrySet()) { + String[] splitFilename = getFullFileNameAsArray(fileEntry.getKey()); + addFileToTree(splitFilename, 0, splitFilename[0], tree); + } } - parent.addHeatStructureTreeToNestedHeatList(heatStructureTree); - } - public void addErrors(Map<String, List<ErrorMessage>> validationErrors) { - validationErrors.entrySet().stream().filter(entry -> - Objects.nonNull(fileTreeRef.get(entry.getKey()))).forEach(entry -> entry.getValue() - .forEach(error -> fileTreeRef.get(entry.getKey()).addErrorToErrorsList(error))); - } + private void addFileToTree(String[] splitFilename, int startIndex, String fullFileName, + HeatStructureTree parent) { + fileTreeRef.putIfAbsent(fullFileName, new HeatStructureTree()); + HeatStructureTree heatStructureTree = fileTreeRef.get(fullFileName); + heatStructureTree.setFileName(splitFilename[startIndex]); + if (startIndex < splitFilename.length - 1) { + addFileToTree(splitFilename, startIndex + 1, + getFullFileName(fullFileName, splitFilename[startIndex + 1]), heatStructureTree); + } + parent.addHeatStructureTreeToNestedHeatList(heatStructureTree); + } - private String getFullFileName(String parentFullName, String fileName) { - return parentFullName + File.separator + fileName; - } + /** + * Add errors. + * + * @param validationErrors the validation errors + */ + public void addErrors(Map<String, List<ErrorMessage>> validationErrors) { + validationErrors.entrySet().stream().filter(entry -> + Objects.nonNull(fileTreeRef.get(entry.getKey()))).forEach(entry -> entry.getValue() + .forEach(error -> fileTreeRef.get(entry.getKey()).addErrorToErrorsList(error))); + } - private String[] getFullFileNameAsArray(String filename) { - if (filename.contains("/")) { - return filename.split("/"); + private String getFullFileName(String parentFullName, String fileName) { + return parentFullName + File.separator + fileName; } - return filename.split(Pattern.quote(File.separator)); - } + private String[] getFullFileNameAsArray(String filename) { + if (filename.contains("/")) { + return filename.split("/"); + } + + return filename.split(Pattern.quote(File.separator)); + } - public HeatStructureTree getTree() { - return tree; - } + /** + * Gets tree. + * + * @return the tree + */ + public HeatStructureTree getTree() { + return tree; + } } |