2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.babel.csar.vnfcatalog;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.Objects;
33 import java.util.function.Predicate;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.lang3.time.StopWatch;
38 import org.onap.aai.babel.logging.ApplicationMsgs;
39 import org.onap.aai.babel.logging.LogHelper;
40 import org.onap.aai.babel.service.data.BabelArtifact;
41 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
42 import org.onap.sdc.tosca.parser.enums.SdcTypes;
43 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
44 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
45 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
46 import org.onap.sdc.toscaparser.api.NodeTemplate;
49 * This class is responsible for extracting Virtual Network Function (VNF) information from a TOSCA 1.1 CSAR package.
52 * CSAR is a compressed format that stores multiple TOSCA files. Each TOSCA file may define Topology Templates and/or
53 * Node Templates along with other model data.
57 * A VNF is a virtualized functional capability (e.g. a Router) that may be defined by an external Vendor. Within the
58 * model defined by the TOSCA files the VNF is considered to be a Resource (part of a Service).
62 * A VNF is specified by a Topology Template. Because this TOSCA construct does not allow properties to be defined
63 * directly, Node Templates are defined (identified by a VNF type value) storing the VNF metadata and properties.
67 * A VNF may be composed of multiple images, each running on its own Virtual Machine. The function of a deployed image
68 * is designated the Virtual Function Component (VFC). A VFC is a sub-component of the VNF. A VFC may have different
69 * "Flavors" (see the Deployment Flavors description below).
73 * An individual VNF (template) may be deployed with varying configuration values, according to
74 * environment/customer/business needs. For example, a VNF deployed in a testing environment would typically use fewer
75 * computational resources than in a production setting.
79 * A Vendor may define one or more "Deployment Flavors". A Deployment Flavor describes a set of pre-determined
80 * parameterised values for a specific aspect of the model. Within the TOSCA model there is a DeploymentFlavor
81 * definition, which has its own data types, and also an ImageInfo definition.
84 public class VnfVendorImageExtractor {
86 private static LogHelper applicationLogger = LogHelper.INSTANCE;
88 // The following constants describe the expected naming conventions for TOSCA Node Templates which
89 // store the VNF configuration and the VFC data items.
91 // The name of the property (for a VNF Configuration type) storing the Images Information
92 private static final String IMAGES = "images";
94 // Name of property key that contains the value of the software version
95 private static final String SOFTWARE_VERSION = "software_version";
97 // The name of the property (for a VNF Configuration type) storing the Vendor Information
98 private static final String VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER = "allowed_flavors";
100 // Name of property key that contains the Vendor Information
101 private static final String VENDOR_INFO = "vendor_info";
103 // Name of property key that contains the value of model of the Vendor application
104 private static final String VENDOR_MODEL = "vendor_model";
107 * This method is responsible for parsing the vnfConfiguration entity in the same topology_template (there's an
108 * assumption that there's only one per file, awaiting verification).
111 * It is possible that CSAR file does not contain a vnfConfiguration and this is valid.
115 * Where multiple vnfConfigurations are found an exception will be thrown.
119 * The ASDC model anticipates the following permutations of vnfConfiguration and multiflavorVFC:
123 * <li>Single vnfConfiguration, single multiFlavorVFC with multiple images.</li>
124 * <li>Single vnfConfiguration, multi multiFlavorVFC with single images.</li>
128 * All ImageInfo sections found apply to all "Deployment Flavors", therefore we can apply a cross product of
129 * "Deployment Flavors" x "ImageInfo"
132 * @param csar compressed format that stores multiple TOSCA files and in particular a vnfConfiguration
133 * @return BabelArtifact VendorImageConfiguration objects created during processing represented as the Babel service
134 * public data structure
135 * @throws ToscaToCatalogException if the CSAR content is not valid
137 public BabelArtifact extract(byte[] csar) throws ToscaToCatalogException {
138 StopWatch stopwatch = new StopWatch();
141 Objects.requireNonNull(csar, "A CSAR file must be supplied");
142 applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Extracting VNF Configuration data");
144 List<VendorImageConfiguration> vendorImageConfigurations;
148 path = createTempFile(csar);
149 vendorImageConfigurations = createVendorImageConfigurations(path.toAbsolutePath().toString());
150 } catch (InvalidNumberOfNodesException | IOException | SdcToscaParserException e) {
151 throw new ToscaToCatalogException(
152 "An error occurred trying to get the VNF Catalog from a CSAR file. " + e.getLocalizedMessage(), e);
155 FileUtils.deleteQuietly(path.toFile());
159 applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, vendorImageConfigurations.toString());
160 applicationLogger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
162 return ConfigurationsToBabelArtifactConverter.convert(vendorImageConfigurations);
166 * Creates a temporary file to store the CSAR content.
168 * @param bytes the CSAR content
169 * @return Path to a temporary file containing the CSAR bytes
170 * @throws IOException if an I/O error occurs or the temporary-file directory does not exist
172 private Path createTempFile(byte[] bytes) throws IOException {
173 Path path = Files.createTempFile("temp", ".csar");
174 applicationLogger.debug("Created temp file " + path);
175 Files.write(path, bytes);
180 * Build VNF Vendor Image Configurations for the VNF Configuration node (if present) in the CSAR file referenced by
183 * @param csarFilepath
184 * the path to the CSAR file
185 * @return a List of Vendor Image Configurations
186 * @throws SdcToscaParserException
187 * @throws ToscaToCatalogException
188 * @throws InvalidNumberOfNodesException
190 private List<VendorImageConfiguration> createVendorImageConfigurations(String csarFilepath)
191 throws SdcToscaParserException, InvalidNumberOfNodesException {
192 ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath);
194 List<NodeTemplate> serviceVfList = csarHelper.getServiceNodeTemplates().stream() //
195 .filter(filterOnType(SdcTypes.VF)).collect(Collectors.toList());
197 List<NodeTemplate> vnfConfigs = serviceVfList.stream()
198 .flatMap(vf -> vf.getSubMappingToscaTemplate().getNodeTemplates().stream()
199 .filter(filterOnType(SdcTypes.VFC)) //
200 .filter(vfc -> vfc.getType().endsWith("VnfConfiguration")))
201 .filter(Objects::nonNull) //
202 .collect(Collectors.toList());
204 if (!vnfConfigs.isEmpty()) {
205 NodeTemplate vnfConfigurationNode = vnfConfigs.get(0);
207 applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
208 String.format("Found VNF Configuration node \"%s\"", vnfConfigurationNode));
210 if (vnfConfigs.size() > 1) {
211 throw new InvalidNumberOfNodesException("Only one VNF configuration node is allowed however "
212 + vnfConfigs.size() + " nodes were found in the CSAR.");
215 return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
218 return Collections.emptyList();
222 * Build VNF Vendor Image Configurations for each Service VF, using the flavors of the specified VNF Configuration
225 * @param serviceVfList
226 * the Service level VF node templates
227 * @param vnfConfigurationNode
228 * the VNF node template
229 * @return a new List of Vendor Image Configurations
231 private List<VendorImageConfiguration> createVendorImageConfigurations(List<NodeTemplate> serviceVfList,
232 NodeTemplate vnfConfigurationNode) {
233 List<VendorImageConfiguration> vendorImageConfigurations = Collections.emptyList();
235 Object allowedFlavorProperties =
236 vnfConfigurationNode.getPropertyValue(VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER);
238 if (allowedFlavorProperties instanceof Map) {
239 @SuppressWarnings("unchecked")
240 Collection<Map<String, Map<String, String>>> flavorMaps =
241 ((Map<?, Map<String, Map<String, String>>>) allowedFlavorProperties).values();
243 vendorImageConfigurations = serviceVfList.stream() //
244 .flatMap(node -> buildVendorImageConfigurations(flavorMaps, node)) //
245 .collect(Collectors.toList());
247 applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
248 "Built " + vendorImageConfigurations.size() + " Vendor Image Configurations");
251 return vendorImageConfigurations;
254 private Predicate<? super NodeTemplate> filterOnType(SdcTypes sdcType) {
255 return node -> (node.getMetaData() != null
256 && sdcType.getValue().equals(node.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)));
260 * Builds the Vendor Image configurations.
263 * the collection of flavors and the properties for those flavors
264 * @param vfNodeTemplate
265 * the node template for the VF
267 * @return a stream of VendorImageConfiguration objects
269 private Stream<VendorImageConfiguration> buildVendorImageConfigurations(
270 Collection<Map<String, Map<String, String>>> flavorMaps, NodeTemplate vfNodeTemplate) {
271 String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor");
272 applicationLogger.debug("Resource Vendor " + resourceVendor);
274 List<String> softwareVersions =
275 extractSoftwareVersions(vfNodeTemplate.getSubMappingToscaTemplate().getNodeTemplates());
276 applicationLogger.debug("Software Versions: " + softwareVersions);
278 return flavorMaps.stream() //
279 .map(value -> value.entrySet().stream() //
280 .filter(entry -> VENDOR_INFO.equals(entry.getKey())) //
281 .map(e -> e.getValue().get(VENDOR_MODEL)) //
283 .flatMap(vendorModel -> softwareVersions.stream().map(
284 version -> new VendorImageConfiguration(vendorModel.orElse(null), resourceVendor, version)));
288 * Extract Image software versions from node templates.
290 * @param nodeTemplates
291 * the node templates to search for software versions
292 * @return a List of Software Version Strings
294 @SuppressWarnings("unchecked")
295 List<String> extractSoftwareVersions(Collection<NodeTemplate> nodeTemplates) {
296 return nodeTemplates.stream() //
297 .filter(nodeTemplate -> nodeTemplate.getPropertyValue(IMAGES) != null) //
298 .flatMap(imagesNode -> ((Map<String, Object>) imagesNode.getPropertyValue(IMAGES)).entrySet().stream())
299 .map(property -> findSoftwareVersion((Map<String, Object>) property.getValue()))
300 .collect(Collectors.toList());
304 * Get the first software version value from the properties Map.
306 * @param image the properties Map
307 * @return the software version value as a String
309 private String findSoftwareVersion(Map<String, Object> image) {
310 applicationLogger.debug("Finding " + SOFTWARE_VERSION + " from " + image);
312 return (String) image.entrySet().stream()//
313 .filter(entry -> SOFTWARE_VERSION.equals(entry.getKey())) //
314 .map(Entry::getValue).findFirst().orElse(null);