d5ba7931a790e7e40f4d198ad7a110ef8b825aa9
[aai/babel.git] / src / main / java / org / onap / aai / babel / csar / vnfcatalog / VnfVendorImageExtractor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.babel.csar.vnfcatalog;
23
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;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Objects;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.lang3.time.StopWatch;
37 import org.onap.aai.babel.logging.ApplicationMsgs;
38 import org.onap.aai.babel.logging.LogHelper;
39 import org.onap.aai.babel.parser.ToscaParser;
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.SdcToscaParserFactory;
45 import org.onap.sdc.toscaparser.api.NodeTemplate;
46
47 /**
48  * This class is responsible for extracting Virtual Network Function (VNF) information from a TOSCA 1.1 CSAR package.
49  *
50  * <p>
51  * CSAR is a compressed format that stores multiple TOSCA files. Each TOSCA file may define Topology Templates and/or
52  * Node Templates along with other model data.
53  * </p>
54  *
55  * <p>
56  * A VNF is a virtualized functional capability (e.g. a Router) that may be defined by an external Vendor. Within the
57  * model defined by the TOSCA files the VNF is considered to be a Resource (part of a Service).
58  * </p>
59  *
60  * <p>
61  * A VNF is specified by a Topology Template. Because this TOSCA construct does not allow properties to be defined
62  * directly, Node Templates are defined (identified by a VNF type value) storing the VNF metadata and properties.
63  * </p>
64  *
65  * <p>
66  * A VNF may be composed of multiple images, each running on its own Virtual Machine. The function of a deployed image
67  * is designated the Virtual Function Component (VFC). A VFC is a sub-component of the VNF. A VFC may have different
68  * "Flavors" (see the Deployment Flavors description below).
69  * </p>
70  *
71  * <p>
72  * An individual VNF (template) may be deployed with varying configuration values, according to
73  * environment/customer/business needs. For example, a VNF deployed in a testing environment would typically use fewer
74  * computational resources than in a production setting.
75  * </p>
76  *
77  * <p>
78  * A Vendor may define one or more "Deployment Flavors". A Deployment Flavor describes a set of pre-determined
79  * parameterised values for a specific aspect of the model. Within the TOSCA model there is a DeploymentFlavor
80  * definition, which has its own data types, and also an ImageInfo definition.
81  * </p>
82  */
83 public class VnfVendorImageExtractor {
84
85     private static LogHelper applicationLogger = LogHelper.INSTANCE;
86
87     // The following constants describe the expected naming conventions for TOSCA Node Templates which
88     // store the VNF configuration and the VFC data items.
89
90     // The name of the property (for a VNF Configuration type) storing the Images Information
91     private static final String IMAGES = "images";
92
93     // Name of property key that contains the value of the software version
94     private static final String SOFTWARE_VERSION = "software_version";
95
96     // The name of the property (for a VNF Configuration type) storing the Vendor Information
97     private static final String VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER = "allowed_flavors";
98
99     // Name of property key that contains the Vendor Information
100     private static final String VENDOR_INFO = "vendor_info";
101
102     // Name of property key that contains the value of model of the Vendor application
103     private static final String VENDOR_MODEL = "vendor_model";
104
105     /**
106      * This method is responsible for parsing the vnfConfiguration entity in the same topology_template (there's an
107      * assumption that there's only one per file, awaiting verification).
108      *
109      * <p>
110      * It is possible that CSAR file does not contain a vnfConfiguration and this is valid.
111      * </p>
112      *
113      * <p>
114      * Where multiple vnfConfigurations are found an exception will be thrown.
115      * </p>
116      *
117      * <p>
118      * The ASDC model anticipates the following permutations of vnfConfiguration and multiflavorVFC:
119      *
120      * <pre>
121      * <ol>
122      * <li>Single vnfConfiguration, single multiFlavorVFC with multiple images.</li>
123      * <li>Single vnfConfiguration, multi multiFlavorVFC with single images.</li>
124      * </ol>
125      * </pre>
126      *
127      * All ImageInfo sections found apply to all "Deployment Flavors", therefore we can apply a cross product of
128      * "Deployment Flavors" x "ImageInfo"
129      * </p>
130      *
131      * @param csar
132      *            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
136      *             if the CSAR content is not valid
137      */
138     public BabelArtifact extract(byte[] csar) throws ToscaToCatalogException {
139         StopWatch stopwatch = new StopWatch();
140         stopwatch.start();
141
142         Objects.requireNonNull(csar, "A CSAR file must be supplied");
143         applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Extracting VNF Configuration data");
144
145         List<VendorImageConfiguration> vendorImageConfigurations;
146         Path path = null;
147
148         try {
149             path = createTempFile(csar);
150             vendorImageConfigurations = createVendorImageConfigurations(path.toAbsolutePath().toString());
151         } catch (InvalidNumberOfNodesException | IOException | SdcToscaParserException e) {
152             throw new ToscaToCatalogException(
153                     "An error occurred trying to get the VNF Catalog from a CSAR file. " + e.getLocalizedMessage(), e);
154         } finally {
155             if (path != null) {
156                 FileUtils.deleteQuietly(path.toFile());
157             }
158         }
159
160         applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, vendorImageConfigurations.toString());
161         applicationLogger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
162
163         return ConfigurationsToBabelArtifactConverter.convert(vendorImageConfigurations);
164     }
165
166     /**
167      * Creates a temporary file to store the CSAR content.
168      *
169      * @param bytes
170      *            the CSAR content
171      * @return Path to a temporary file containing the CSAR bytes
172      * @throws IOException
173      *             if an I/O error occurs or the temporary-file directory does not exist
174      */
175     private Path createTempFile(byte[] bytes) throws IOException {
176         Path path = Files.createTempFile("temp", ".csar");
177         applicationLogger.debug("Created temp file " + path);
178         Files.write(path, bytes);
179         return path;
180     }
181
182     /**
183      * Build VNF Vendor Image Configurations for the VNF Configuration node (if present) in the CSAR file referenced by
184      * the supplied Path.
185      *
186      * @param csarFilepath
187      *            the path to the CSAR file
188      * @return a List of Vendor Image Configurations
189      * @throws SdcToscaParserException
190      *             if the SDC TOSCA parser determines that the CSAR is invalid
191      * @throws ToscaToCatalogException
192      *             if there are no software versions defined for an image
193      * @throws InvalidNumberOfNodesException
194      *             if multiple VNF configuration nodes are found in the CSAR
195      */
196     private List<VendorImageConfiguration> createVendorImageConfigurations(String csarFilepath)
197             throws SdcToscaParserException, ToscaToCatalogException, InvalidNumberOfNodesException {
198         ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath);
199
200         List<NodeTemplate> serviceVfList = ToscaParser.getServiceNodeTemplates(csarHelper)
201                 .filter(ToscaParser.filterOnType(SdcTypes.VF)).collect(Collectors.toList());
202
203         List<NodeTemplate> vnfConfigs = serviceVfList.stream()
204                 .flatMap(vf -> vf.getSubMappingToscaTemplate().getNodeTemplates().stream()
205                         .filter(ToscaParser.filterOnType(SdcTypes.VFC)) //
206                         .filter(vfc -> vfc.getType().endsWith("VnfConfiguration")))
207                 .filter(Objects::nonNull) //
208                 .collect(Collectors.toList());
209
210         if (!vnfConfigs.isEmpty()) {
211             NodeTemplate vnfConfigurationNode = vnfConfigs.get(0);
212
213             applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
214                     String.format("Found VNF Configuration node \"%s\"", vnfConfigurationNode));
215
216             if (vnfConfigs.size() > 1) {
217                 throw new InvalidNumberOfNodesException("Only one VNF configuration node is allowed however "
218                         + vnfConfigs.size() + " nodes were found in the CSAR.");
219             }
220
221             try {
222                 return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
223             } catch (IllegalArgumentException e) {
224                 throw new ToscaToCatalogException(e.getMessage());
225             }
226         }
227
228         return Collections.emptyList();
229     }
230
231     /**
232      * Build VNF Vendor Image Configurations for each Service VF, using the flavors of the specified VNF Configuration
233      * node.
234      *
235      * @param serviceVfList
236      *            the Service level VF node templates
237      * @param vnfConfigurationNode
238      *            the VNF node template
239      * @return a new List of Vendor Image Configurations
240      */
241     private List<VendorImageConfiguration> createVendorImageConfigurations(List<NodeTemplate> serviceVfList,
242             NodeTemplate vnfConfigurationNode) {
243         List<VendorImageConfiguration> vendorImageConfigurations = Collections.emptyList();
244
245         Object allowedFlavorProperties =
246                 vnfConfigurationNode.getPropertyValue(VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER);
247
248         if (allowedFlavorProperties instanceof Map) {
249             @SuppressWarnings("unchecked")
250             Collection<Map<String, Map<String, String>>> flavorMaps =
251                     ((Map<?, Map<String, Map<String, String>>>) allowedFlavorProperties).values();
252
253             vendorImageConfigurations = serviceVfList.stream() //
254                     .flatMap(node -> buildVendorImageConfigurations(flavorMaps, node)) //
255                     .collect(Collectors.toList());
256
257             applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
258                     "Built " + vendorImageConfigurations.size() + " Vendor Image Configurations");
259         }
260
261         return vendorImageConfigurations;
262     }
263
264     /**
265      * Builds the Vendor Image configurations.
266      *
267      * @param flavorMaps
268      *            the collection of flavors and the properties for those flavors
269      * @param vfNodeTemplate
270      *            the node template for the VF
271      *
272      * @return a stream of VendorImageConfiguration objects
273      * @throws IllegalArgumentException
274      *             if the VF has no child node templates which contain images (complex properties) that have software
275      *             version strings
276      */
277     Stream<VendorImageConfiguration> buildVendorImageConfigurations(
278             Collection<Map<String, Map<String, String>>> flavorMaps, NodeTemplate vfNodeTemplate) {
279         String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor");
280         applicationLogger.debug("Resource Vendor " + resourceVendor);
281
282         List<String> softwareVersions =
283                 extractSoftwareVersions(vfNodeTemplate.getSubMappingToscaTemplate().getNodeTemplates());
284         applicationLogger.debug("Software Versions: " + softwareVersions);
285
286         if (softwareVersions.isEmpty()) {
287             throw new IllegalArgumentException("No software versions could be found for this CSAR file");
288         }
289
290         return flavorMaps.stream() //
291                 .map(value -> value.entrySet().stream() //
292                         .filter(entry -> VENDOR_INFO.equals(entry.getKey())) //
293                         .map(e -> e.getValue().get(VENDOR_MODEL)) //
294                         .findFirst()) //
295                 .flatMap(vendorModel -> softwareVersions.stream().map(
296                         version -> new VendorImageConfiguration(vendorModel.orElse(null), resourceVendor, version)));
297     }
298
299     /**
300      * Extract Image software versions from node templates.
301      *
302      * @param nodeTemplates
303      *            the node templates to search for software versions
304      * @return a List of Software Version Strings
305      */
306     @SuppressWarnings("unchecked")
307     List<String> extractSoftwareVersions(Collection<NodeTemplate> nodeTemplates) {
308         return nodeTemplates.stream() //
309                 .filter(nodeTemplate -> nodeTemplate.getPropertyValue(IMAGES) != null) //
310                 .flatMap(imagesNode -> ((Map<String, Object>) imagesNode.getPropertyValue(IMAGES)).entrySet().stream())
311                 .map(property -> findSoftwareVersion((Map<String, Object>) property.getValue()))
312                 .collect(Collectors.toList());
313     }
314
315     /**
316      * Get the first software_version value from the properties Map.
317      *
318      * @param properties
319      *            the properties map containing the software_version key
320      * @return the software_version value as a String, or else null
321      */
322     private String findSoftwareVersion(Map<String, Object> properties) {
323         applicationLogger.debug("Finding " + SOFTWARE_VERSION + " from " + properties);
324
325         return properties.entrySet().stream() //
326                 .filter(entry -> SOFTWARE_VERSION.equals(entry.getKey())) //
327                 .map(Entry::getValue).findFirst() //
328                 .map(Object::toString).orElse(null);
329     }
330 }