Fix new Sonar code smell
[aai/babel.git] / src / main / java / org / onap / aai / babel / csar / vnfcatalog / VnfVendorImageExtractor.java
index 5d33bc2..870c8c3 100644 (file)
@@ -30,18 +30,17 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
-import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.time.StopWatch;
 import org.onap.aai.babel.logging.ApplicationMsgs;
 import org.onap.aai.babel.logging.LogHelper;
+import org.onap.aai.babel.parser.ToscaParser;
 import org.onap.aai.babel.service.data.BabelArtifact;
 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
 import org.onap.sdc.tosca.parser.enums.SdcTypes;
 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
-import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
 
@@ -188,19 +187,22 @@ public class VnfVendorImageExtractor {
      *            the path to the CSAR file
      * @return a List of Vendor Image Configurations
      * @throws SdcToscaParserException
+     *             if the SDC TOSCA parser determines that the CSAR is invalid
      * @throws ToscaToCatalogException
+     *             if there are no software versions defined for an image
      * @throws InvalidNumberOfNodesException
+     *             if multiple VNF configuration nodes are found in the CSAR
      */
     private List<VendorImageConfiguration> createVendorImageConfigurations(String csarFilepath)
-            throws SdcToscaParserException, InvalidNumberOfNodesException {
+            throws SdcToscaParserException, ToscaToCatalogException, InvalidNumberOfNodesException {
         ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath);
 
-        List<NodeTemplate> serviceVfList = csarHelper.getServiceNodeTemplates().stream()
-                .filter(filterOnType(SdcTypes.VF)).collect(Collectors.toList());
+        List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
+                .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
 
         List<NodeTemplate> vnfConfigs = serviceVfList.stream()
                 .flatMap(vf -> vf.getSubMappingToscaTemplate().getNodeTemplates().stream()
-                        .filter(filterOnType(SdcTypes.VFC)) //
+                        .filter(ToscaParser.filterOnType(SdcTypes.VFC)) //
                         .filter(vfc -> vfc.getType().endsWith("VnfConfiguration")))
                 .filter(Objects::nonNull) //
                 .collect(Collectors.toList());
@@ -216,7 +218,12 @@ public class VnfVendorImageExtractor {
                         + vnfConfigs.size() + " nodes were found in the CSAR.");
             }
 
-            return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
+            try {
+                return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
+            } catch (IllegalArgumentException e) {
+                applicationLogger.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
+                throw new ToscaToCatalogException(e.getMessage());
+            }
         }
 
         return Collections.emptyList();
@@ -255,11 +262,6 @@ public class VnfVendorImageExtractor {
         return vendorImageConfigurations;
     }
 
-    private Predicate<? super NodeTemplate> filterOnType(SdcTypes sdcType) {
-        return node -> (node.getMetaData() != null
-                && sdcType.getValue().equals(node.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)));
-    }
-
     /**
      * Builds the Vendor Image configurations.
      *
@@ -269,8 +271,11 @@ public class VnfVendorImageExtractor {
      *            the node template for the VF
      *
      * @return a stream of VendorImageConfiguration objects
+     * @throws IllegalArgumentException
+     *             if the VF has no child node templates which contain images (complex properties) that have software
+     *             version strings
      */
-    private Stream<VendorImageConfiguration> buildVendorImageConfigurations(
+    Stream<VendorImageConfiguration> buildVendorImageConfigurations(
             Collection<Map<String, Map<String, String>>> flavorMaps, NodeTemplate vfNodeTemplate) {
         String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor");
         applicationLogger.debug("Resource Vendor " + resourceVendor);
@@ -279,6 +284,10 @@ public class VnfVendorImageExtractor {
                 extractSoftwareVersions(vfNodeTemplate.getSubMappingToscaTemplate().getNodeTemplates());
         applicationLogger.debug("Software Versions: " + softwareVersions);
 
+        if (softwareVersions.isEmpty()) {
+            throw new IllegalArgumentException("No software versions could be found for this CSAR file");
+        }
+
         return flavorMaps.stream() //
                 .map(value -> value.entrySet().stream() //
                         .filter(entry -> VENDOR_INFO.equals(entry.getKey())) //