From 6342b6f7e46266caf72cbee7c96f782cc5093d80 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Mon, 1 Apr 2019 19:07:53 +0100 Subject: [PATCH] Exception handling for image software versions Throw an exception when no software versions are present for a VNF image. This needs to be an unchecked exception when using Streams. Change-Id: If7a429c79a614eed1dad6ed8ac1993caca47ab6d Issue-ID: AAI-2306 Signed-off-by: mark.j.leonard --- .../csar/vnfcatalog/VnfVendorImageExtractor.java | 20 +++++++++++++++++--- .../aai/babel/csar/vnfcatalog/SdcToscaHelper.java | 20 ++++++++++++-------- ...torTest.java => TestVnfVendorImageExtractor.java} | 17 ++++++++++++++++- 3 files changed, 45 insertions(+), 12 deletions(-) rename src/test/java/org/onap/aai/babel/csar/vnfcatalog/{VnfVendorImageExtractorTest.java => TestVnfVendorImageExtractor.java} (88%) diff --git a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java index 5f3d15b..d5ba793 100644 --- a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java @@ -187,11 +187,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 createVendorImageConfigurations(String csarFilepath) - throws SdcToscaParserException, InvalidNumberOfNodesException { + throws SdcToscaParserException, ToscaToCatalogException, InvalidNumberOfNodesException { ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath); List serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper) @@ -215,7 +218,11 @@ public class VnfVendorImageExtractor { + vnfConfigs.size() + " nodes were found in the CSAR."); } - return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode); + try { + return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode); + } catch (IllegalArgumentException e) { + throw new ToscaToCatalogException(e.getMessage()); + } } return Collections.emptyList(); @@ -263,8 +270,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 buildVendorImageConfigurations( + Stream buildVendorImageConfigurations( Collection>> flavorMaps, NodeTemplate vfNodeTemplate) { String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor"); applicationLogger.debug("Resource Vendor " + resourceVendor); @@ -273,6 +283,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())) // diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java index 6fbb1f1..d285aca 100644 --- a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java @@ -32,7 +32,7 @@ public class SdcToscaHelper { /** * Create the test SubstitutionMappings. - * + * * @return the new Substitution Mappings */ public SubstitutionMappings buildMappings() { @@ -81,28 +81,32 @@ public class SdcToscaHelper { /** * Create a new NodeTemplate and add it to the list (for populating the Substitution Mappings). + * + * @return the new NodeTemplate */ - public void addNodeTemplate() { + public NodeTemplate addNodeTemplate() { String name = "node name"; String type = "tosca.nodes.custom"; - LinkedHashMap nodeTemplate = new LinkedHashMap<>(); - nodeTemplate.put("type", type); - nodeTemplate.put("properties", null); + LinkedHashMap ntMap = new LinkedHashMap<>(); + ntMap.put("type", type); + ntMap.put("properties", null); - LinkedHashMap ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate); + LinkedHashMap ntnodeTemplates = buildCustomTypeDefinitions(name, ntMap); ntnodeTemplates.put("derived_from", null); ntnodeTemplates.put("properties", getImagesDefProps()); LinkedHashMap typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps()); LinkedHashMap customDefs = buildCustomTypeDefinitions(type, typeInfo); - smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null)); + NodeTemplate nodeTemplate = new NodeTemplate(name, ntnodeTemplates, customDefs, null, null); + smnodetemplates.add(nodeTemplate); + return nodeTemplate; } /** * Simulate the creation of a NodeTemplate by the SDC TOSCA parser. Populate the properties of the NodeTemplate with * the supplied images. - * + * * @param images * the value of the images property */ diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/TestVnfVendorImageExtractor.java similarity index 88% rename from src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java rename to src/test/java/org/onap/aai/babel/csar/vnfcatalog/TestVnfVendorImageExtractor.java index 7a076c5..d76551a 100644 --- a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/TestVnfVendorImageExtractor.java @@ -28,6 +28,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; +import com.google.common.collect.ImmutableMap; import java.io.IOException; import java.util.HashMap; import java.util.LinkedHashMap; @@ -37,11 +38,13 @@ import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType; import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; /** * Tests {@link VnfVendorImageExtractor}. */ -public class VnfVendorImageExtractorTest { +public class TestVnfVendorImageExtractor { @Test(expected = NullPointerException.class) public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException { @@ -89,6 +92,18 @@ public class VnfVendorImageExtractorTest { is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json")))); } + /** + * Test that an Exception is created when there are no software versions defined for a VF. + */ + @Test(expected = IllegalArgumentException.class) + public void testBuildVendorImageConfigurations() { + SdcToscaHelper helper = new SdcToscaHelper(); + NodeTemplate vf = helper.addNodeTemplate(); + vf.setMetaData(new Metadata(ImmutableMap.of("resourceVendor", "vendor"))); + vf.setSubMappingToscaTemplate(helper.buildMappings()); + new VnfVendorImageExtractor().buildVendorImageConfigurations(null, vf); + } + @Test public void testSoftwareVersions() throws ToscaToCatalogException { VnfVendorImageExtractor extractor = new VnfVendorImageExtractor(); -- 2.16.6