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