Improve javadoc comments and formatting
[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.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;
47
48 /**
49  * This class is responsible for extracting Virtual Network Function (VNF) information from a TOSCA 1.1 CSAR package.
50  *
51  * <p>
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.
54  * </p>
55  *
56  * <p>
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).
59  * </p>
60  *
61  * <p>
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.
64  * </p>
65  *
66  * <p>
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).
70  * </p>
71  *
72  * <p>
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.
76  * </p>
77  *
78  * <p>
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.
82  * </p>
83  */
84 public class VnfVendorImageExtractor {
85
86     private static LogHelper applicationLogger = LogHelper.INSTANCE;
87
88     // The following constants describe the expected naming conventions for TOSCA Node Templates which
89     // store the VNF configuration and the VFC data items.
90
91     // The name of the property (for a VNF Configuration type) storing the Images Information
92     private static final String IMAGES = "images";
93
94     // Name of property key that contains the value of the software version
95     private static final String SOFTWARE_VERSION = "software_version";
96
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";
99
100     // Name of property key that contains the Vendor Information
101     private static final String VENDOR_INFO = "vendor_info";
102
103     // Name of property key that contains the value of model of the Vendor application
104     private static final String VENDOR_MODEL = "vendor_model";
105
106     /**
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).
109      *
110      * <p>
111      * It is possible that CSAR file does not contain a vnfConfiguration and this is valid.
112      * </p>
113      *
114      * <p>
115      * Where multiple vnfConfigurations are found an exception will be thrown.
116      * </p>
117      *
118      * <p>
119      * The ASDC model anticipates the following permutations of vnfConfiguration and multiflavorVFC:
120      *
121      * <pre>
122      * <ol>
123      * <li>Single vnfConfiguration, single multiFlavorVFC with multiple images.</li>
124      * <li>Single vnfConfiguration, multi multiFlavorVFC with single images.</li>
125      * </ol>
126      * </pre>
127      *
128      * All ImageInfo sections found apply to all "Deployment Flavors", therefore we can apply a cross product of
129      * "Deployment Flavors" x "ImageInfo"
130      * </p>
131      *
132      * @param csar
133      *            compressed format that stores multiple TOSCA files and in particular a vnfConfiguration
134      * @return BabelArtifact VendorImageConfiguration objects created during processing represented as the Babel service
135      *         public data structure
136      * @throws ToscaToCatalogException
137      *             if the CSAR content is not valid
138      */
139     public BabelArtifact extract(byte[] csar) throws ToscaToCatalogException {
140         StopWatch stopwatch = new StopWatch();
141         stopwatch.start();
142
143         Objects.requireNonNull(csar, "A CSAR file must be supplied");
144         applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Extracting VNF Configuration data");
145
146         List<VendorImageConfiguration> vendorImageConfigurations;
147         Path path = null;
148
149         try {
150             path = createTempFile(csar);
151             vendorImageConfigurations = createVendorImageConfigurations(path.toAbsolutePath().toString());
152         } catch (InvalidNumberOfNodesException | IOException | SdcToscaParserException e) {
153             throw new ToscaToCatalogException(
154                     "An error occurred trying to get the VNF Catalog from a CSAR file. " + e.getLocalizedMessage(), e);
155         } finally {
156             if (path != null) {
157                 FileUtils.deleteQuietly(path.toFile());
158             }
159         }
160
161         applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT, vendorImageConfigurations.toString());
162         applicationLogger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0));
163
164         return ConfigurationsToBabelArtifactConverter.convert(vendorImageConfigurations);
165     }
166
167     /**
168      * Creates a temporary file to store the CSAR content.
169      *
170      * @param bytes
171      *            the CSAR content
172      * @return Path to a temporary file containing the CSAR bytes
173      * @throws IOException
174      *             if an I/O error occurs or the temporary-file directory does not exist
175      */
176     private Path createTempFile(byte[] bytes) throws IOException {
177         Path path = Files.createTempFile("temp", ".csar");
178         applicationLogger.debug("Created temp file " + path);
179         Files.write(path, bytes);
180         return path;
181     }
182
183     /**
184      * Build VNF Vendor Image Configurations for the VNF Configuration node (if present) in the CSAR file referenced by
185      * the supplied Path.
186      *
187      * @param csarFilepath
188      *            the path to the CSAR file
189      * @return a List of Vendor Image Configurations
190      * @throws SdcToscaParserException
191      * @throws ToscaToCatalogException
192      * @throws InvalidNumberOfNodesException
193      */
194     private List<VendorImageConfiguration> createVendorImageConfigurations(String csarFilepath)
195             throws SdcToscaParserException, InvalidNumberOfNodesException {
196         ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarFilepath);
197
198         List<NodeTemplate> serviceVfList = csarHelper.getServiceNodeTemplates().stream()
199                 .filter(filterOnType(SdcTypes.VF)).collect(Collectors.toList());
200
201         List<NodeTemplate> vnfConfigs = serviceVfList.stream()
202                 .flatMap(vf -> vf.getSubMappingToscaTemplate().getNodeTemplates().stream()
203                         .filter(filterOnType(SdcTypes.VFC)) //
204                         .filter(vfc -> vfc.getType().endsWith("VnfConfiguration")))
205                 .filter(Objects::nonNull) //
206                 .collect(Collectors.toList());
207
208         if (!vnfConfigs.isEmpty()) {
209             NodeTemplate vnfConfigurationNode = vnfConfigs.get(0);
210
211             applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
212                     String.format("Found VNF Configuration node \"%s\"", vnfConfigurationNode));
213
214             if (vnfConfigs.size() > 1) {
215                 throw new InvalidNumberOfNodesException("Only one VNF configuration node is allowed however "
216                         + vnfConfigs.size() + " nodes were found in the CSAR.");
217             }
218
219             return createVendorImageConfigurations(serviceVfList, vnfConfigurationNode);
220         }
221
222         return Collections.emptyList();
223     }
224
225     /**
226      * Build VNF Vendor Image Configurations for each Service VF, using the flavors of the specified VNF Configuration
227      * node.
228      *
229      * @param serviceVfList
230      *            the Service level VF node templates
231      * @param vnfConfigurationNode
232      *            the VNF node template
233      * @return a new List of Vendor Image Configurations
234      */
235     private List<VendorImageConfiguration> createVendorImageConfigurations(List<NodeTemplate> serviceVfList,
236             NodeTemplate vnfConfigurationNode) {
237         List<VendorImageConfiguration> vendorImageConfigurations = Collections.emptyList();
238
239         Object allowedFlavorProperties =
240                 vnfConfigurationNode.getPropertyValue(VNF_CONF_TYPE_PROPERTY_VENDOR_INFO_CONTAINER);
241
242         if (allowedFlavorProperties instanceof Map) {
243             @SuppressWarnings("unchecked")
244             Collection<Map<String, Map<String, String>>> flavorMaps =
245                     ((Map<?, Map<String, Map<String, String>>>) allowedFlavorProperties).values();
246
247             vendorImageConfigurations = serviceVfList.stream() //
248                     .flatMap(node -> buildVendorImageConfigurations(flavorMaps, node)) //
249                     .collect(Collectors.toList());
250
251             applicationLogger.info(ApplicationMsgs.DISTRIBUTION_EVENT,
252                     "Built " + vendorImageConfigurations.size() + " Vendor Image Configurations");
253         }
254
255         return vendorImageConfigurations;
256     }
257
258     private Predicate<? super NodeTemplate> filterOnType(SdcTypes sdcType) {
259         return node -> (node.getMetaData() != null
260                 && sdcType.getValue().equals(node.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)));
261     }
262
263     /**
264      * Builds the Vendor Image configurations.
265      *
266      * @param flavorMaps
267      *            the collection of flavors and the properties for those flavors
268      * @param vfNodeTemplate
269      *            the node template for the VF
270      *
271      * @return a stream of VendorImageConfiguration objects
272      */
273     private Stream<VendorImageConfiguration> buildVendorImageConfigurations(
274             Collection<Map<String, Map<String, String>>> flavorMaps, NodeTemplate vfNodeTemplate) {
275         String resourceVendor = vfNodeTemplate.getMetaData().getValue("resourceVendor");
276         applicationLogger.debug("Resource Vendor " + resourceVendor);
277
278         List<String> softwareVersions =
279                 extractSoftwareVersions(vfNodeTemplate.getSubMappingToscaTemplate().getNodeTemplates());
280         applicationLogger.debug("Software Versions: " + softwareVersions);
281
282         return flavorMaps.stream() //
283                 .map(value -> value.entrySet().stream() //
284                         .filter(entry -> VENDOR_INFO.equals(entry.getKey())) //
285                         .map(e -> e.getValue().get(VENDOR_MODEL)) //
286                         .findFirst()) //
287                 .flatMap(vendorModel -> softwareVersions.stream().map(
288                         version -> new VendorImageConfiguration(vendorModel.orElse(null), resourceVendor, version)));
289     }
290
291     /**
292      * Extract Image software versions from node templates.
293      *
294      * @param nodeTemplates
295      *            the node templates to search for software versions
296      * @return a List of Software Version Strings
297      */
298     @SuppressWarnings("unchecked")
299     List<String> extractSoftwareVersions(Collection<NodeTemplate> nodeTemplates) {
300         return nodeTemplates.stream() //
301                 .filter(nodeTemplate -> nodeTemplate.getPropertyValue(IMAGES) != null) //
302                 .flatMap(imagesNode -> ((Map<String, Object>) imagesNode.getPropertyValue(IMAGES)).entrySet().stream())
303                 .map(property -> findSoftwareVersion((Map<String, Object>) property.getValue()))
304                 .collect(Collectors.toList());
305     }
306
307     /**
308      * Get the first software_version value from the properties Map.
309      *
310      * @param properties
311      *            the properties map containing the software_version key
312      * @return the software_version value as a String, or else null
313      */
314     private String findSoftwareVersion(Map<String, Object> properties) {
315         applicationLogger.debug("Finding " + SOFTWARE_VERSION + " from " + properties);
316
317         return properties.entrySet().stream() //
318                 .filter(entry -> SOFTWARE_VERSION.equals(entry.getKey())) //
319                 .map(Entry::getValue).findFirst() //
320                 .map(Object::toString).orElse(null);
321     }
322 }