Publish babel 1.13.4 container release
[aai/babel.git] / src / main / java / org / onap / aai / babel / csar / vnfcatalog / VnfVendorImageExtractor.java
index 5f3d15b..8973f57 100644 (file)
@@ -44,6 +44,8 @@ import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
 
+import javax.ws.rs.core.Response;
+
 /**
  * This class is responsible for extracting Virtual Network Function (VNF) information from a TOSCA 1.1 CSAR package.
  *
@@ -158,7 +160,6 @@ public class VnfVendorImageExtractor {
         }
 
         applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, vendorImageConfigurations.toString());
-        applicationLogger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
 
         return ConfigurationsToBabelArtifactConverter.convert(vendorImageConfigurations);
     }
@@ -187,11 +188,14 @@ 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 = ToscaParser.getServiceNodeTemplates(csarHelper)
@@ -215,7 +219,14 @@ public class VnfVendorImageExtractor {
                         + vnfConfigs.size() + " nodes were found in the CSAR.");
             }
 
-            return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
+            try {
+                return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
+            } catch (IllegalArgumentException e) {
+                applicationLogger.setContextValue(LogHelper.MdcParameter.RESPONSE_CODE, String.valueOf(Response.Status.BAD_REQUEST.getStatusCode()));
+                applicationLogger.setContextValue(LogHelper.MdcParameter.RESPONSE_DESCRIPTION, "Invalid Csar");
+                applicationLogger.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
+                throw new ToscaToCatalogException(e.getMessage());
+            }
         }
 
         return Collections.emptyList();
@@ -232,7 +243,7 @@ public class VnfVendorImageExtractor {
      * @return a new List of Vendor Image Configurations
      */
     private List<VendorImageConfiguration> createVendorImageConfigurations(List<NodeTemplate> serviceVfList,
-            NodeTemplate vnfConfigurationNode) {
+            NodeTemplate vnfConfigurationNode) throws IllegalArgumentException{
         List<VendorImageConfiguration> vendorImageConfigurations = Collections.emptyList();
 
         Object allowedFlavorProperties =
@@ -263,9 +274,12 @@ 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(
-            Collection<Map<String, Map<String, String>>> flavorMaps, NodeTemplate vfNodeTemplate) {
+    Stream<VendorImageConfiguration> buildVendorImageConfigurations(
+            Collection<Map<String, Map<String, String>>> flavorMaps, NodeTemplate vfNodeTemplate) throws IllegalArgumentException {
         String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor");
         applicationLogger.debug("Resource Vendor " + resourceVendor);
 
@@ -273,6 +287,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())) //