/**
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.aai.babel.csar.vnfcatalog;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
-import java.util.function.Consumer;
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.apache.commons.lang3.tuple.ImmutablePair;
-import org.apache.commons.lang3.tuple.Pair;
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.SdcToscaParserFactory;
-import org.onap.sdc.tosca.parser.impl.SdcTypes;
import org.onap.sdc.toscaparser.api.NodeTemplate;
-import org.onap.sdc.toscaparser.api.SubstitutionMappings;
+
+import javax.ws.rs.core.Response;
/**
* This class is responsible for extracting Virtual Network Function (VNF) information from a TOSCA 1.1 CSAR package.
* <p>
* CSAR is a compressed format that stores multiple TOSCA files. Each TOSCA file may define Topology Templates and/or
* Node Templates along with other model data.
+ * </p>
*
* <p>
* A VNF is a virtualized functional capability (e.g. a Router) that may be defined by an external Vendor. Within the
* model defined by the TOSCA files the VNF is considered to be a Resource (part of a Service).
+ * </p>
*
* <p>
* A VNF is specified by a Topology Template. Because this TOSCA construct does not allow properties to be defined
* directly, Node Templates are defined (identified by a VNF type value) storing the VNF metadata and properties.
+ * </p>
*
* <p>
* A VNF may be composed of multiple images, each running on its own Virtual Machine. The function of a deployed image
* is designated the Virtual Function Component (VFC). A VFC is a sub-component of the VNF. A VFC may have different
* "Flavors" (see the Deployment Flavors description below).
+ * </p>
*
* <p>
* An individual VNF (template) may be deployed with varying configuration values, according to
* environment/customer/business needs. For example, a VNF deployed in a testing environment would typically use fewer
* computational resources than in a production setting.
+ * </p>
*
* <p>
* A Vendor may define one or more "Deployment Flavors". A Deployment Flavor describes a set of pre-determined
* parameterised values for a specific aspect of the model. Within the TOSCA model there is a DeploymentFlavor
* definition, which has its own data types, and also an ImageInfo definition.
+ * </p>
*/
public class VnfVendorImageExtractor {
- private static LogHelper applicationLogger = LogHelper.INSTANCE;
- private static final String AN_ERROR_OCCURRED = "An error occurred trying to get the vnf catalog from a csar file.";
+ private static LogHelper applicationLogger = LogHelper.INSTANCE;
// The following constants describe the expected naming conventions for TOSCA Node Templates which
- // store the VNF
- // configuration and the VFC data items.
+ // store the VNF configuration and the VFC data items.
// The name of the property (for a VNF Configuration type) storing the Images Information
private static final String IMAGES = "images";
// The name of the property (for a VNF Configuration type) storing the Vendor Information
private static final String VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER = "allowed_flavors";
- // Name of property key that contains the value of model of the vendor application
+ // Name of property key that contains the Vendor Information
+ private static final String VENDOR_INFO = "vendor_info";
+
+ // Name of property key that contains the value of model of the Vendor application
private static final String VENDOR_MODEL = "vendor_model";
/**
* assumption that there's only one per file, awaiting verification).
*
* <p>
- * It is possible that csar file does not contain a vnfConfiguration and this is valid.
+ * It is possible that CSAR file does not contain a vnfConfiguration and this is valid.
+ * </p>
*
* <p>
* Where multiple vnfConfigurations are found an exception will be thrown.
+ * </p>
*
* <p>
* The ASDC model anticipates the following permutations of vnfConfiguration and multiflavorVFC:
* </pre>
*
* All ImageInfo sections found apply to all "Deployment Flavors", therefore we can apply a cross product of
- * "Deployment Flavors" x "ImageInfo" - concretely
+ * "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
*/
public BabelArtifact extract(byte[] csar) throws ToscaToCatalogException {
StopWatch stopwatch = new StopWatch();
stopwatch.start();
Objects.requireNonNull(csar, "A CSAR file must be supplied");
-
- applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
- "Starting to find and extract any vnf configuration data");
+ applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Extracting VNF Configuration data");
List<VendorImageConfiguration> vendorImageConfigurations;
Path path = null;
path = createTempFile(csar);
vendorImageConfigurations = createVendorImageConfigurations(path.toAbsolutePath().toString());
} catch (InvalidNumberOfNodesException | IOException | SdcToscaParserException e) {
- throw new ToscaToCatalogException(AN_ERROR_OCCURRED + " " + e.getLocalizedMessage(), e);
+ throw new ToscaToCatalogException(
+ "An error occurred trying to get the VNF Catalog from a CSAR file. " + e.getLocalizedMessage(), e);
} finally {
if (path != null) {
FileUtils.deleteQuietly(path.toFile());
}
}
- String msg = vendorImageConfigurations.isEmpty() ? "No Vnf Configuration has been found in the csar"
- : "Vnf Configuration has been found in the csar";
- applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, msg);
- applicationLogger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
+ applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, vendorImageConfigurations.toString());
return ConfigurationsToBabelArtifactConverter.convert(vendorImageConfigurations);
}
+ /**
+ * Creates a temporary file to store 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
+ */
private Path createTempFile(byte[] bytes) throws IOException {
- applicationLogger.debug("Creating temp file on file system for the csar");
Path path = Files.createTempFile("temp", ".csar");
+ applicationLogger.debug("Created temp file " + path);
Files.write(path, bytes);
return path;
}
+ /**
+ * Build VNF Vendor Image Configurations for the VNF Configuration node (if present) in the CSAR file referenced by
+ * the supplied Path.
+ *
+ * @param csarFilepath
+ * 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, ToscaToCatalogException, InvalidNumberOfNodesException {
- applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
- "Getting instance of ISdcCsarHelper to use in finding vnf configuration data");
ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath);
- List<VendorImageConfiguration> vendorImageConfigurations = new ArrayList<>();
- NodeTemplate vnfConfigurationNode = findVnfConfigurationNode(csarHelper);
+ List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
+ .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
- if (vnfConfigurationNode != null) {
- List<NodeTemplate> serviceVfNodes = csarHelper.getServiceVfList();
+ List<NodeTemplate> vnfConfigs = serviceVfList.stream()
+ .flatMap(vf -> vf.getSubMappingToscaTemplate().getNodeTemplates().stream()
+ .filter(ToscaParser.filterOnType(SdcTypes.VFC)) //
+ .filter(vfc -> vfc.getType().endsWith("VnfConfiguration")))
+ .filter(Objects::nonNull) //
+ .collect(Collectors.toList());
- for (NodeTemplate node : serviceVfNodes) {
- vendorImageConfigurations.addAll(buildVendorImageConfigurations(vnfConfigurationNode, node));
- }
- }
+ if (!vnfConfigs.isEmpty()) {
+ NodeTemplate vnfConfigurationNode = vnfConfigs.get(0);
- return vendorImageConfigurations;
- }
+ applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
+ String.format("Found VNF Configuration node \"%s\"", vnfConfigurationNode));
- private NodeTemplate findVnfConfigurationNode(ISdcCsarHelper csarHelper) throws InvalidNumberOfNodesException {
- applicationLogger.debug("Tryng to find the vnf configuration node");
-
- List<NodeTemplate> configNodes =
- csarHelper.getServiceNodeTemplateBySdcType(SdcTypes.VF).stream().map(serviceNodeTemplate -> {
- String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNodeTemplate);
- applicationLogger.debug("Node Template Customization Uuid is " + uuid);
- return csarHelper.getVnfConfig(uuid);
- }).filter(Objects::nonNull).collect(Collectors.toList());
+ if (vnfConfigs.size() > 1) {
+ throw new InvalidNumberOfNodesException("Only one VNF configuration node is allowed however "
+ + vnfConfigs.size() + " nodes were found in the CSAR.");
+ }
- if (configNodes.size() > 1) {
- throw new InvalidNumberOfNodesException("Only one vnf configuration node is allowed however "
- + configNodes.size() + " nodes were found in the csar.");
+ 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 configNodes.size() == 1 ? configNodes.get(0) : null;
+ return Collections.emptyList();
}
- private List<VendorImageConfiguration> buildVendorImageConfigurations(NodeTemplate vendorInfoNode,
- NodeTemplate node) throws ToscaToCatalogException {
- List<VendorImageConfiguration> vendorImageConfigurations = new ArrayList<>();
+ /**
+ * Build VNF Vendor Image Configurations for each Service VF, using the flavors of the specified VNF Configuration
+ * node.
+ *
+ * @param serviceVfList
+ * the Service level VF node templates
+ * @param vnfConfigurationNode
+ * the VNF node template
+ * @return a new List of Vendor Image Configurations
+ */
+ private List<VendorImageConfiguration> createVendorImageConfigurations(List<NodeTemplate> serviceVfList,
+ NodeTemplate vnfConfigurationNode) throws IllegalArgumentException{
+ List<VendorImageConfiguration> vendorImageConfigurations = Collections.emptyList();
+
+ Object allowedFlavorProperties =
+ vnfConfigurationNode.getPropertyValue(VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER);
- List<String> softwareVersions = extractSoftwareVersions(node.getSubMappingToscaTemplate());
+ if (allowedFlavorProperties instanceof Map) {
+ @SuppressWarnings("unchecked")
+ Collection<Map<String, Map<String, String>>> flavorMaps =
+ ((Map<?, Map<String, Map<String, String>>>) allowedFlavorProperties).values();
- Consumer<? super Pair<String, String>> buildConfigurations = vi -> vendorImageConfigurations.addAll(
- softwareVersions.stream().map(sv -> (new VendorImageConfiguration(vi.getRight(), vi.getLeft(), sv)))
- .collect(Collectors.toList()));
+ vendorImageConfigurations = serviceVfList.stream() //
+ .flatMap(node -> buildVendorImageConfigurations(flavorMaps, node)) //
+ .collect(Collectors.toList());
- String resourceVendor = node.getMetaData().getValue("resourceVendor");
- buildVendorInfo(resourceVendor, vendorInfoNode).forEach(buildConfigurations);
+ applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
+ "Built " + vendorImageConfigurations.size() + " Vendor Image Configurations");
+ }
return vendorImageConfigurations;
}
- @SuppressWarnings("unchecked")
- private List<Pair<String, String>> buildVendorInfo(String resourceVendor, NodeTemplate vendorInfoNode) {
- Map<String, Object> otherFlavorProperties =
- (Map<String, Object>) vendorInfoNode.getPropertyValue(VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER);
-
- return otherFlavorProperties.keySet().stream()
- .map(key -> createVendorInfoPair((Map<String, Object>) otherFlavorProperties.get(key), resourceVendor))
- .collect(Collectors.toList());
- }
+ /**
+ * Builds the Vendor Image configurations.
+ *
+ * @param flavorMaps
+ * the collection of flavors and the properties for those flavors
+ * @param vfNodeTemplate
+ * 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
+ */
+ 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);
- private Pair<String, String> createVendorInfoPair(Map<String, Object> otherFlavor, String resourceVendor) {
- @SuppressWarnings("unchecked")
- String vendorModel = otherFlavor.entrySet().stream() //
- .filter(entry -> "vendor_info".equals(entry.getKey()))
- .map(e -> ((Map<String, String>) e.getValue()).get(VENDOR_MODEL)) //
- .findFirst().orElse(null);
+ List<String> softwareVersions =
+ extractSoftwareVersions(vfNodeTemplate.getSubMappingToscaTemplate().getNodeTemplates());
+ applicationLogger.debug("Software Versions: " + softwareVersions);
- applicationLogger.debug("Creating vendor info pair object for vendorModel = " + vendorModel
- + " and resourceVendor = " + resourceVendor);
+ if (softwareVersions.isEmpty()) {
+ throw new IllegalArgumentException("No software versions could be found for this CSAR file");
+ }
- return new ImmutablePair<>(resourceVendor, vendorModel);
+ return flavorMaps.stream() //
+ .map(value -> value.entrySet().stream() //
+ .filter(entry -> VENDOR_INFO.equals(entry.getKey())) //
+ .map(e -> e.getValue().get(VENDOR_MODEL)) //
+ .findFirst()) //
+ .flatMap(vendorModel -> softwareVersions.stream().map(
+ version -> new VendorImageConfiguration(vendorModel.orElse(null), resourceVendor, version)));
}
+ /**
+ * Extract Image software versions from node templates.
+ *
+ * @param nodeTemplates
+ * the node templates to search for software versions
+ * @return a List of Software Version Strings
+ */
@SuppressWarnings("unchecked")
- private List<String> extractSoftwareVersions(SubstitutionMappings sm) throws ToscaToCatalogException {
- applicationLogger.debug("Trying to extract the software versions for the vnf configuration");
-
- List<NodeTemplate> imagesNodes = sm.getNodeTemplates().stream()
- .filter(nodeTemplate -> nodeTemplate.getPropertyValue(IMAGES) != null).collect(Collectors.toList());
-
- if (imagesNodes != null && !imagesNodes.isEmpty()) {
- applicationLogger.debug("Found NodeTemplates containing properties with a key called 'images'");
- return imagesNodes.stream()
- .flatMap(imagesNode -> ((Map<String, Object>) imagesNode.getPropertyValue(IMAGES)) //
- .entrySet().stream())
- .map(property -> findSoftwareVersion((Map<String, Object>) property.getValue()))
- .collect(Collectors.toList());
- } else {
- throw new ToscaToCatalogException("No software versions could be found for this csar file");
- }
+ List<String> extractSoftwareVersions(Collection<NodeTemplate> nodeTemplates) {
+ return nodeTemplates.stream() //
+ .filter(nodeTemplate -> nodeTemplate.getPropertyValue(IMAGES) != null) //
+ .flatMap(imagesNode -> ((Map<String, Object>) imagesNode.getPropertyValue(IMAGES)).entrySet().stream())
+ .map(property -> findSoftwareVersion((Map<String, Object>) property.getValue()))
+ .collect(Collectors.toList());
}
- private String findSoftwareVersion(Map<String, Object> image) {
- applicationLogger.debug("Getting the software version value from the map of image properties");
+ /**
+ * Get the first software_version value from the properties Map.
+ *
+ * @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> properties) {
+ applicationLogger.debug("Finding " + SOFTWARE_VERSION + " from " + properties);
- return (String) image.entrySet().stream().filter(entry -> SOFTWARE_VERSION.equals(entry.getKey()))
- .map(Entry::getValue).findFirst().orElse(null);
+ return properties.entrySet().stream() //
+ .filter(entry -> SOFTWARE_VERSION.equals(entry.getKey())) //
+ .map(Entry::getValue).findFirst() //
+ .map(Object::toString).orElse(null);
}
}