diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java | 184 |
1 files changed, 86 insertions, 98 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java index feb18c22e6..f5a1409e68 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java @@ -21,8 +21,23 @@ */ package org.openecomp.sdc.be.components.csar; +import static org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; +import static org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum; +import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement; + import com.google.common.annotations.VisibleForTesting; import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Set; +import java.util.regex.Pattern; import lombok.Getter; import lombok.Setter; import org.apache.commons.collections.CollectionUtils; @@ -42,25 +57,10 @@ import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.log.wrappers.Logger; import org.yaml.snakeyaml.Yaml; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.PriorityQueue; -import java.util.Queue; -import java.util.Set; -import java.util.regex.Pattern; - -import static org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; -import static org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum; -import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement; - public class CsarInfo { - private static final Logger log = Logger.getLogger(CsarInfo.class); + private static final Logger log = Logger.getLogger(CsarInfo.class); + private final NonManoConfiguration nonManoConfiguration; @Getter @Setter private String vfResourceName; @@ -85,10 +85,10 @@ public class CsarInfo { private boolean isUpdate; @Getter private Map<String, Resource> createdNodes; - private final NonManoConfiguration nonManoConfiguration; @SuppressWarnings("unchecked") - public CsarInfo(User modifier, String csarUUID, Map<String, byte[]> csar, String vfResourceName, String mainTemplateName, String mainTemplateContent, boolean isUpdate){ + public CsarInfo(User modifier, String csarUUID, Map<String, byte[]> csar, String vfResourceName, String mainTemplateName, + String mainTemplateContent, boolean isUpdate) { this.vfResourceName = vfResourceName; this.modifier = modifier; this.csarUUID = csarUUID; @@ -99,7 +99,7 @@ public class CsarInfo { this.createdNodesToscaResourceNames = new HashMap<>(); this.cvfcToCreateQueue = new PriorityQueue<>(); this.isUpdate = isUpdate; - this.createdNodes = new HashMap<>(); + this.createdNodes = new HashMap<>(); this.nonManoConfiguration = NonManoConfigurationManager.getInstance().getNonManoConfiguration(); } @@ -108,14 +108,36 @@ public class CsarInfo { this.nonManoConfiguration = nonManoConfiguration; } + @SuppressWarnings("unchecked") + public static void markNestedVfc(Map<String, Object> mappedToscaTemplate, Map<String, NodeTypeInfo> nodeTypesInfo) { + findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP).right() + .on(nts -> processNodeTemplates((Map<String, Object>) nts, nodeTypesInfo)); + } + + @SuppressWarnings("unchecked") + private static ResultStatusEnum processNodeTemplates(Map<String, Object> nodeTemplates, Map<String, NodeTypeInfo> nodeTypesInfo) { + nodeTemplates.values().forEach(nt -> processNodeTemplate(nodeTypesInfo, (Map<String, Object>) nt)); + return ResultStatusEnum.OK; + } + + private static void processNodeTemplate(Map<String, NodeTypeInfo> nodeTypesInfo, Map<String, Object> nodeTemplate) { + if (nodeTemplate.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { + String type = (String) nodeTemplate.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()); + if (nodeTypesInfo.containsKey(type)) { + NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(type); + if (nodeTypeInfo.isSubstitutionMapping() && type.contains(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX)) { + nodeTypeInfo.setNested(true); + } + } + } + } + public void addNodeToQueue(String nodeName) { - if(!cvfcToCreateQueue.contains(nodeName)) { + if (!cvfcToCreateQueue.contains(nodeName)) { cvfcToCreateQueue.add(nodeName); } else { - log.debug("Failed to validate complex VFC {}. Loop detected, VSP {}. ", nodeName, - getVfResourceName()); - throw new ByActionStatusComponentException(ActionStatus.CFVC_LOOP_DETECTED, - getVfResourceName(), nodeName); + log.debug("Failed to validate complex VFC {}. Loop detected, VSP {}. ", nodeName, getVfResourceName()); + throw new ByActionStatusComponentException(ActionStatus.CFVC_LOOP_DETECTED, getVfResourceName(), nodeName); } } @@ -131,7 +153,7 @@ public class CsarInfo { this.isUpdate = isUpdate; } - public Map<String,NodeTypeInfo> extractNodeTypesInfo() { + public Map<String, NodeTypeInfo> extractNodeTypesInfo() { Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>(); List<Map.Entry<String, byte[]>> globalSubstitutes = new ArrayList<>(); final Set<String> nodeTypesUsedInNodeTemplates = new HashSet<>(); @@ -147,17 +169,15 @@ public class CsarInfo { } @SuppressWarnings("unchecked") - private void extractNodeTypeInfo(Map<String, NodeTypeInfo> nodeTypesInfo, - List<Map.Entry<String, byte[]>> globalSubstitutes, final Set<String> nodeTypesUsedInNodeTemplates, Map.Entry<String, byte[]> entry) { + private void extractNodeTypeInfo(Map<String, NodeTypeInfo> nodeTypesInfo, List<Map.Entry<String, byte[]>> globalSubstitutes, + final Set<String> nodeTypesUsedInNodeTemplates, Map.Entry<String, byte[]> entry) { if (isAServiceTemplate(entry.getKey())) { if (isGlobalSubstitute(entry.getKey())) { globalSubstitutes.add(entry); } else { Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(new String(entry.getValue())); - findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP) - .right() - .on(sub->handleSubstitutionMappings(nodeTypesInfo, entry, mappedToscaTemplate, (Map<String, Object>)sub)); - + findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP).right() + .on(sub -> handleSubstitutionMappings(nodeTypesInfo, entry, mappedToscaTemplate, (Map<String, Object>) sub)); final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); if (nodeTypesEither.isLeft()) { @@ -167,37 +187,38 @@ public class CsarInfo { } } } - + private boolean isAServiceTemplate(final String filePath) { - return Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(filePath).matches(); + return Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(filePath).matches(); } - + private Set<String> findNodeTypesUsedInNodeTemplates(final Map<String, Map<String, Object>> nodeTemplates) { - final Set<String> nodeTypes = new HashSet<>(); - for (final Map<String, Object> nodeTemplate : nodeTemplates.values()) { - nodeTypes.add((String)nodeTemplate.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())); - } - return nodeTypes; + final Set<String> nodeTypes = new HashSet<>(); + for (final Map<String, Object> nodeTemplate : nodeTemplates.values()) { + nodeTypes.add((String) nodeTemplate.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())); + } + return nodeTypes; } - private ResultStatusEnum handleSubstitutionMappings(Map<String, NodeTypeInfo> nodeTypesInfo, Map.Entry<String, byte[]> entry, Map<String, Object> mappedToscaTemplate, Map<String, Object> substitutionMappings) { - final Set<String> nodeTypesDefinedInTemplate = findNodeTypesDefinedInTemplate(mappedToscaTemplate); - if (substitutionMappings.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName()) && !nodeTypesDefinedInTemplate.contains(substitutionMappings.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName()))) { + private ResultStatusEnum handleSubstitutionMappings(Map<String, NodeTypeInfo> nodeTypesInfo, Map.Entry<String, byte[]> entry, + Map<String, Object> mappedToscaTemplate, Map<String, Object> substitutionMappings) { + final Set<String> nodeTypesDefinedInTemplate = findNodeTypesDefinedInTemplate(mappedToscaTemplate); + if (substitutionMappings.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName()) && !nodeTypesDefinedInTemplate + .contains(substitutionMappings.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName()))) { NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); nodeTypeInfo.setSubstitutionMapping(true); - nodeTypeInfo.setType( - (String) substitutionMappings.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName())); + nodeTypeInfo.setType((String) substitutionMappings.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName())); nodeTypeInfo.setTemplateFileName(entry.getKey()); nodeTypeInfo.setMappedToscaTemplate(mappedToscaTemplate); nodeTypesInfo.put(nodeTypeInfo.getType(), nodeTypeInfo); } return ResultStatusEnum.OK; } - + @SuppressWarnings("unchecked") - private Set<String> findNodeTypesDefinedInTemplate(final Map<String, Object> mappedToscaTemplate) { - final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, - TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); + private Set<String> findNodeTypesDefinedInTemplate(final Map<String, Object> mappedToscaTemplate) { + final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, + ToscaElementTypeEnum.MAP); if (nodeTypesEither.isLeft()) { final Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value(); return nodeTypes.keySet(); @@ -206,18 +227,17 @@ public class CsarInfo { } private boolean isGlobalSubstitute(String fileName) { - return fileName.equalsIgnoreCase(Constants.GLOBAL_SUBSTITUTION_TYPES_SERVICE_TEMPLATE) - || fileName.equalsIgnoreCase(Constants.ABSTRACT_SUBSTITUTE_GLOBAL_TYPES_SERVICE_TEMPLATE); + return fileName.equalsIgnoreCase(Constants.GLOBAL_SUBSTITUTION_TYPES_SERVICE_TEMPLATE) || fileName + .equalsIgnoreCase(Constants.ABSTRACT_SUBSTITUTE_GLOBAL_TYPES_SERVICE_TEMPLATE); } @SuppressWarnings("unchecked") - private void setDerivedFrom(Map<String, NodeTypeInfo> nodeTypesInfo, - List<Map.Entry<String, byte[]>> globalSubstitutes) { + private void setDerivedFrom(Map<String, NodeTypeInfo> nodeTypesInfo, List<Map.Entry<String, byte[]>> globalSubstitutes) { for (Map.Entry<String, byte[]> entry : globalSubstitutes) { String yamlFileContents = new String(entry.getValue()); Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(yamlFileContents); - Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, - TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); + Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, + ToscaElementTypeEnum.MAP); if (nodeTypesEither.isLeft()) { Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value(); for (Map.Entry<String, Object> nodeType : nodeTypes.entrySet()) { @@ -239,24 +259,16 @@ public class CsarInfo { } @SuppressWarnings("unchecked") - public static void markNestedVfc(Map<String, Object> mappedToscaTemplate, Map<String, NodeTypeInfo> nodeTypesInfo) { - findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, - ToscaElementTypeEnum.MAP) - .right() - .on(nts-> processNodeTemplates((Map<String, Object>)nts, nodeTypesInfo)); - } - - @SuppressWarnings("unchecked") - private void addGlobalSubstitutionsToNodeTypes(final List<Map.Entry<String, byte[]>> globalSubstitutes, final Set<String> nodeTypesUsedInNodeTemplates, final Map<String, NodeTypeInfo> nodeTypesInfo) { + private void addGlobalSubstitutionsToNodeTypes(final List<Map.Entry<String, byte[]>> globalSubstitutes, + final Set<String> nodeTypesUsedInNodeTemplates, final Map<String, NodeTypeInfo> nodeTypesInfo) { for (Map.Entry<String, byte[]> entry : globalSubstitutes) { final String yamlFileContents = new String(entry.getValue()); final Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(yamlFileContents); - final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, - TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); + final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, + ToscaElementTypeEnum.MAP); if (nodeTypesEither.isLeft()) { final Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value(); for (final Map.Entry<String, Object> nodeType : nodeTypes.entrySet()) { - if (!nodeTypesInfo.containsKey(nodeType.getKey()) && nodeTypesUsedInNodeTemplates.contains(nodeType.getKey())) { nodeTypesInfo.put(nodeType.getKey(), buildNodeTypeInfo(nodeType, entry.getKey(), mappedToscaTemplate)); } @@ -266,56 +278,35 @@ public class CsarInfo { } @SuppressWarnings("unchecked") - private static ResultStatusEnum processNodeTemplates( Map<String, Object> nodeTemplates, Map<String, NodeTypeInfo> nodeTypesInfo) { - nodeTemplates.values().forEach(nt->processNodeTemplate(nodeTypesInfo, (Map<String, Object>) nt)); - return ResultStatusEnum.OK; - } - - @SuppressWarnings("unchecked") - private NodeTypeInfo buildNodeTypeInfo(final Map.Entry<String, Object> nodeType, final String templateFileName, final Map<String, Object> mappedToscaTemplate ) { + private NodeTypeInfo buildNodeTypeInfo(final Map.Entry<String, Object> nodeType, final String templateFileName, + final Map<String, Object> mappedToscaTemplate) { final NodeTypeInfo nodeTypeInfo = new NodeTypeInfo(); nodeTypeInfo.setSubstitutionMapping(false); nodeTypeInfo.setNested(true); nodeTypeInfo.setType(nodeType.getKey()); nodeTypeInfo.setTemplateFileName(templateFileName); nodeTypeInfo.setMappedToscaTemplate(buildToscaTemplateForNode(nodeType.getKey(), mappedToscaTemplate)); - final Map<String, Object> nodeTypeMap = (Map<String, Object>) nodeType.getValue(); final List<String> derivedFrom = new ArrayList<>(); derivedFrom.add((String) nodeTypeMap.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName())); nodeTypeInfo.setDerivedFrom(derivedFrom); - return nodeTypeInfo; } - + @SuppressWarnings("unchecked") private Map<String, Object> buildToscaTemplateForNode(final String nodeTypeName, final Map<String, Object> mappedToscaTemplate) { final Map<String, Object> mappedToscaTemplateforNode = new HashMap<>(mappedToscaTemplate); - - final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, - TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP); + final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, + ToscaElementTypeEnum.MAP); final Map<String, Object> nodeTypes = new HashMap<>(); if (nodeTypesEither.isLeft()) { final Map<String, Object> allNodeTypes = (Map<String, Object>) nodeTypesEither.left().value(); nodeTypes.put(nodeTypeName, allNodeTypes.get(nodeTypeName)); } - mappedToscaTemplateforNode.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), nodeTypes); return mappedToscaTemplateforNode; } - private static void processNodeTemplate(Map<String, NodeTypeInfo> nodeTypesInfo, Map<String, Object> nodeTemplate) { - if (nodeTemplate.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { - String type = (String) nodeTemplate.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()); - if (nodeTypesInfo.containsKey(type)) { - NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(type); - if (nodeTypeInfo.isSubstitutionMapping() && type.contains(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX)) { - nodeTypeInfo.setNested(true); - } - } - } - } - /** * Gets the software information yaml path from the csar file map. * @@ -325,10 +316,7 @@ public class CsarInfo { if (MapUtils.isEmpty(csar)) { return Optional.empty(); } - final NonManoFolderType softwareInformationType = - nonManoConfiguration.getNonManoType(NonManoArtifactType.ONAP_SW_INFORMATION); - return csar.keySet().stream() - .filter(filePath -> filePath.startsWith(softwareInformationType.getPath())) - .findFirst(); + final NonManoFolderType softwareInformationType = nonManoConfiguration.getNonManoType(NonManoArtifactType.ONAP_SW_INFORMATION); + return csar.keySet().stream().filter(filePath -> filePath.startsWith(softwareInformationType.getPath())).findFirst(); } } |