Fix new Sonar code smell
[aai/babel.git] / src / main / java / org / onap / aai / babel / csar / vnfcatalog / VnfVendorImageExtractor.java
index 49dd19f..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;
 
@@ -129,10 +128,12 @@ public class VnfVendorImageExtractor {
      * "Deployment Flavors" x "ImageInfo"
      * </p>
      *
-     * @param csar compressed format that stores multiple TOSCA files and in particular a vnfConfiguration
+     * @param csar
+     *            compressed format that stores multiple TOSCA files and in particular a vnfConfiguration
      * @return BabelArtifact VendorImageConfiguration objects created during processing represented as the Babel service
      *         public data structure
-     * @throws ToscaToCatalogException if the CSAR content is not valid
+     * @throws ToscaToCatalogException
+     *             if the CSAR content is not valid
      */
     public BabelArtifact extract(byte[] csar) throws ToscaToCatalogException {
         StopWatch stopwatch = new StopWatch();
@@ -165,9 +166,11 @@ public class VnfVendorImageExtractor {
     /**
      * Creates a temporary file to store the CSAR content.
      *
-     * @param bytes the CSAR content
+     * @param bytes
+     *            the CSAR content
      * @return Path to a temporary file containing the CSAR bytes
-     * @throws IOException if an I/O error occurs or the temporary-file directory does not exist
+     * @throws IOException
+     *             if an I/O error occurs or the temporary-file directory does not exist
      */
     private Path createTempFile(byte[] bytes) throws IOException {
         Path path = Files.createTempFile("temp", ".csar");
@@ -184,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());
@@ -212,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();
@@ -251,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.
      *
@@ -265,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);
@@ -275,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())) //
@@ -301,16 +314,18 @@ public class VnfVendorImageExtractor {
     }
 
     /**
-     * Get the first software version value from the properties Map.
+     * Get the first software_version value from the properties Map.
      *
-     * @param image the properties Map
-     * @return the software version value as a String
+     * @param properties
+     *            the properties map containing the software_version key
+     * @return the software_version value as a String, or else null
      */
-    private String findSoftwareVersion(Map<String, Object> image) {
-        applicationLogger.debug("Finding " + SOFTWARE_VERSION + " from " + image);
+    private String findSoftwareVersion(Map<String, Object> properties) {
+        applicationLogger.debug("Finding " + SOFTWARE_VERSION + " from " + properties);
 
-        return (String) image.entrySet().stream()//
+        return properties.entrySet().stream() //
                 .filter(entry -> SOFTWARE_VERSION.equals(entry.getKey())) //
-                .map(Entry::getValue).findFirst().orElse(null);
+                .map(Entry::getValue).findFirst() //
+                .map(Object::toString).orElse(null);
     }
 }