Support unknown data types in service import
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / CsarInfo.java
index dc6f7c4..49b7bb2 100644 (file)
@@ -21,8 +21,6 @@
  */
 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;
@@ -37,11 +35,11 @@ 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;
 import org.apache.commons.collections.MapUtils;
+import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
+import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
 import org.openecomp.sdc.be.config.NonManoArtifactType;
 import org.openecomp.sdc.be.config.NonManoConfiguration;
@@ -51,13 +49,15 @@ import org.openecomp.sdc.be.dao.api.ActionStatus;
 import org.openecomp.sdc.be.model.NodeTypeInfo;
 import org.openecomp.sdc.be.model.Resource;
 import org.openecomp.sdc.be.model.User;
-import org.openecomp.sdc.be.tosca.CsarUtils;
 import org.openecomp.sdc.be.utils.TypeUtils;
 import org.openecomp.sdc.common.api.Constants;
 import org.openecomp.sdc.common.log.wrappers.Logger;
 import org.yaml.snakeyaml.Yaml;
 
-public class CsarInfo {
+/**
+ * Provides access to the contents of a CSAR
+ */
+public abstract class CsarInfo {
 
     private static final Logger log = Logger.getLogger(CsarInfo.class);
     private final NonManoConfiguration nonManoConfiguration;
@@ -74,22 +74,21 @@ public class CsarInfo {
     private String csarVersionId;
     @Getter
     @Setter
-    private Map<String, byte[]> csar;
+    protected Map<String, byte[]> csar;
     @Getter
     private String mainTemplateName;
     @Getter
     private String mainTemplateContent;
     @Getter
-    private Map<String, Object> mappedToscaMainTemplate;
+    protected Map<String, Object> mappedToscaMainTemplate;
     @Getter
     private Map<String, String> createdNodesToscaResourceNames;
     private Queue<String> cvfcToCreateQueue;
     private boolean isUpdate;
     @Getter
     private Map<String, Resource> createdNodes;
-    private Map<String, Object> datatypeDefinitions;
+    protected Map<String, Object> datatypeDefinitions;
     private Map<String, Object> policytypeDefinitions;
-    private List<Map.Entry<String, byte[]>> globalSubstitutes;
 
 
     public CsarInfo(User modifier, String csarUUID, Map<String, byte[]> csar, String vfResourceName, String mainTemplateName,
@@ -106,18 +105,11 @@ public class CsarInfo {
         this.isUpdate = isUpdate;
         this.createdNodes = new HashMap<>();
         this.nonManoConfiguration = NonManoConfigurationManager.getInstance().getNonManoConfiguration();
-        this.globalSubstitutes = getGlobalSubstitutes(csar);
     }
     
-    private List<Map.Entry<String, byte[]>> getGlobalSubstitutes(final Map<String, byte[]> csar){
-        final List<Map.Entry<String, byte[]>> globalSubstitutesInCsar = new ArrayList<>();
-        for (Map.Entry<String, byte[]> entry : csar.entrySet()) {
-            if (isAServiceTemplate(entry.getKey()) && isGlobalSubstitute(entry.getKey())) {
-                globalSubstitutesInCsar.add(entry);
-            }
-        }
-        return globalSubstitutesInCsar;
-    }    
+    public String getVfResourceName() {
+        return vfResourceName;
+    }
 
     public CsarInfo(final User modifier, final String csarUUID, final String csarVersionId, final Map<String, byte[]> csarContent,
                     final String vfResourceName, final String mainTemplateName, final String mainTemplateContent, final boolean isUpdate) {
@@ -174,34 +166,15 @@ public class CsarInfo {
     public void setUpdate(boolean isUpdate) {
         this.isUpdate = isUpdate;
     }
-
-    public Map<String, NodeTypeInfo> extractTypesInfo() {
-        Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>();
-        final Set<String> nodeTypesUsedInNodeTemplates = new HashSet<>();
-        for (Map.Entry<String, byte[]> entry : getCsar().entrySet()) {
-            extractNodeTypeInfo(nodeTypesInfo,  nodeTypesUsedInNodeTemplates, entry);
-        }
-        if (CollectionUtils.isNotEmpty(globalSubstitutes)) {
-            setDerivedFrom(nodeTypesInfo);
-            addGlobalSubstitutionsToNodeTypes(nodeTypesUsedInNodeTemplates, nodeTypesInfo);
-        }
-        
-        markNestedVfc(getMappedToscaMainTemplate(), nodeTypesInfo);
-        return nodeTypesInfo;
-    }
     
-    public Map<String, Object> getDataTypes() {
-        if (datatypeDefinitions == null) {
-            datatypeDefinitions = new HashMap<>();
-            for (Map.Entry<String, byte[]> entry : globalSubstitutes) {
-                final String yamlFileContents = new String(entry.getValue());
-                final Map<String, Object> mappedToscaTemplate = new Yaml().load(yamlFileContents);
-                datatypeDefinitions.putAll(getTypesFromTemplate(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES));
-            }
-            datatypeDefinitions.putAll(getTypesFromTemplate(mappedToscaMainTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES));
-        }
-        return datatypeDefinitions;
-    }
+    public abstract Map<String, NodeTypeInfo> extractTypesInfo();
+    
+    /**
+     * Get the data types defined in the CSAR
+     * 
+     * @return map with the data type name as key and representaion of the data type defintion as value
+     */
+    public abstract Map<String, Object> getDataTypes();
 
     public Map<String, Object> getPolicyTypes() {
         if (policytypeDefinitions == null) {
@@ -211,8 +184,8 @@ public class CsarInfo {
         return policytypeDefinitions;
     }
     
-    @SuppressWarnings("unchecked")    
-    private Map<String, Object> getTypesFromTemplate(final Map<String, Object> mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum type) {
+    @SuppressWarnings("unchecked")
+    protected Map<String, Object> getTypesFromTemplate(final Map<String, Object> mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum type) {
         final Either<Object, ResultStatusEnum> dataTypesEither = findToscaElement(mappedToscaTemplate, type,
                         ToscaElementTypeEnum.MAP);
         if (dataTypesEither != null && dataTypesEither.isLeft()) {
@@ -221,27 +194,7 @@ public class CsarInfo {
         return Collections.emptyMap();
     }
 
-    @SuppressWarnings("unchecked")
-    private void extractNodeTypeInfo(Map<String, NodeTypeInfo> nodeTypesInfo,
-                                     final Set<String> nodeTypesUsedInNodeTemplates, Map.Entry<String, byte[]> entry) {
-        if (isAServiceTemplate(entry.getKey()) && !isGlobalSubstitute(entry.getKey())) {
-            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));
-            final Either<Object, ResultStatusEnum> nodeTypesEither = findToscaElement(mappedToscaTemplate,
-                TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP);
-            if (nodeTypesEither.isLeft()) {
-                final Map<String, Map<String, Object>> nodeTemplates = (Map<String, Map<String, Object>>) nodeTypesEither.left().value();
-                nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates));
-            }
-        }
-    }
-
-    private boolean isAServiceTemplate(final String filePath) {
-        return Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(filePath).matches();
-    }
-
-    private Set<String> findNodeTypesUsedInNodeTemplates(final Map<String, Map<String, Object>> nodeTemplates) {
+    protected 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()));
@@ -249,84 +202,8 @@ public class CsarInfo {
         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()))) {
-            NodeTypeInfo nodeTypeInfo = new NodeTypeInfo();
-            nodeTypeInfo.setSubstitutionMapping(true);
-            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);
-        if (nodeTypesEither.isLeft()) {
-            final Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value();
-            return nodeTypes.keySet();
-        }
-        return Collections.emptySet();
-    }
-
-    private boolean isGlobalSubstitute(String fileName) {
-        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) {
-        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);
-            if (nodeTypesEither.isLeft()) {
-                Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value();
-                for (Map.Entry<String, Object> nodeType : nodeTypes.entrySet()) {
-                    processNodeType(nodeTypesInfo, nodeType);
-                }
-            }
-        }
-    }
-
-    @SuppressWarnings("unchecked")
-    private void processNodeType(Map<String, NodeTypeInfo> nodeTypesInfo, Map.Entry<String, Object> nodeType) {
-        Map<String, Object> nodeTypeMap = (Map<String, Object>) nodeType.getValue();
-        if (nodeTypeMap.containsKey(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName()) && nodeTypesInfo.containsKey(nodeType.getKey())) {
-            NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(nodeType.getKey());
-            List<String> derivedFrom = new ArrayList<>();
-            derivedFrom.add((String) nodeTypeMap.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName()));
-            nodeTypeInfo.setDerivedFrom(derivedFrom);
-        }
-    }
-
-    @SuppressWarnings("unchecked")
-    private void addGlobalSubstitutionsToNodeTypes(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);
-            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));
-                    }
-                }
-            }
-        }
-    }
-
     @SuppressWarnings("unchecked")
-    private NodeTypeInfo buildNodeTypeInfo(final Map.Entry<String, Object> nodeType, final String templateFileName,
+    protected NodeTypeInfo buildNodeTypeInfo(final Map.Entry<String, Object> nodeType, final String templateFileName,
                                            final Map<String, Object> mappedToscaTemplate) {
         final NodeTypeInfo nodeTypeInfo = new NodeTypeInfo();
         nodeTypeInfo.setSubstitutionMapping(false);