Reformat catalog-be-plugins
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / main / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / generator / NsDescriptorGeneratorImpl.java
1  
2 /*
3  * ============LICENSE_START=======================================================
4  *  Copyright (C) 2020 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
21
22 import com.google.common.collect.ImmutableMap;
23 import fj.data.Either;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Optional;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.collections4.MapUtils;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
39 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
40 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd;
41 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
42 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml.ToscaTemplateYamlGenerator;
43 import org.openecomp.sdc.be.tosca.ToscaError;
44 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
45 import org.openecomp.sdc.be.tosca.model.SubstitutionMapping;
46 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
47 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
48 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
49 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
50 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
51 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
52 import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.ObjectProvider;
56 import org.springframework.beans.factory.config.BeanDefinition;
57 import org.springframework.context.annotation.Scope;
58
59 @org.springframework.stereotype.Component("nsDescriptorGenerator")
60 @Scope(BeanDefinition.SCOPE_PROTOTYPE)
61 public class NsDescriptorGeneratorImpl implements NsDescriptorGenerator {
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(NsDescriptorGeneratorImpl.class);
64     private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
65     private static final String NS_TOSCA_TYPE = "tosca.nodes.nfv.NS";
66     private static final List<Map<String, Map<String, String>>> DEFAULT_IMPORTS = ConfigurationManager.getConfigurationManager().getConfiguration()
67         .getDefaultImports();
68     private static final List<String> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TYPE = Arrays
69         .asList("cds_model_name", "cds_model_version", "skip_post_instantiation_configuration", "controller_actor");
70     private static final List<String> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TEMPLATE = Arrays
71         .asList("nf_function", "nf_role", "nf_naming_code", "nf_type", "nf_naming", "availability_zone_max_count", "min_instances", "max_instances",
72             "multi_stage_design", "sdnc_model_name", "sdnc_model_version", "sdnc_artifact_name", "skip_post_instantiation_configuration",
73             "controller_actor");
74     private final ToscaExportHandler toscaExportHandler;
75     private final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider;
76
77     public NsDescriptorGeneratorImpl(final ToscaExportHandler toscaExportHandler,
78                                      final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider) {
79         this.toscaExportHandler = toscaExportHandler;
80         this.toscaTemplateYamlGeneratorProvider = toscaTemplateYamlGeneratorProvider;
81     }
82
83     public Optional<Nsd> generate(final Component component, final List<VnfDescriptor> vnfDescriptorList) throws NsdException {
84         if (!ComponentTypeEnum.SERVICE.equals(component.getComponentType())) {
85             return Optional.empty();
86         }
87         final ToscaTemplate toscaTemplate = createNetworkServiceDescriptor(component, vnfDescriptorList);
88         final ToscaNodeType nsNodeType = toscaTemplate.getNode_types().values().stream()
89             .filter(toscaNodeType -> NS_TOSCA_TYPE.equals(toscaNodeType.getDerived_from())).findFirst().orElse(null);
90         if (nsNodeType == null) {
91             return Optional.empty();
92         }
93         return Optional.of(buildNsd(toscaTemplate, nsNodeType));
94     }
95
96     private Nsd buildNsd(final ToscaTemplate toscaTemplate, final ToscaNodeType nsNodeType) {
97         final Nsd nsd = new Nsd();
98         nsd.setDesigner(getProperty(nsNodeType, Nsd.DESIGNER_PROPERTY));
99         nsd.setVersion(getProperty(nsNodeType, Nsd.VERSION_PROPERTY));
100         nsd.setName(getProperty(nsNodeType, Nsd.NAME_PROPERTY));
101         nsd.setInvariantId(getProperty(nsNodeType, Nsd.INVARIANT_ID_PROPERTY));
102         final ToscaTemplateYamlGenerator yamlParserProvider = toscaTemplateYamlGeneratorProvider.getObject(toscaTemplate);
103         final byte[] contents = yamlParserProvider.parseToYamlString().getBytes();
104         nsd.setContents(contents);
105         final List<String> interfaceImplementations = getInterfaceImplementations(toscaTemplate);
106         nsd.setArtifactReferences(interfaceImplementations);
107         return nsd;
108     }
109
110     private List<String> getInterfaceImplementations(final ToscaTemplate template) {
111         if (template.getTopology_template().getNode_templates() == null) {
112             return Collections.emptyList();
113         }
114         final List<String> interfaceImplementations = new ArrayList<>();
115         final Collection<ToscaNodeTemplate> nodeTemplates = template.getTopology_template().getNode_templates().values();
116         nodeTemplates.stream().filter(toscaNodeTemplate -> toscaNodeTemplate.getInterfaces() != null).forEach(
117             toscaNodeTemplate -> toscaNodeTemplate.getInterfaces().values()
118                 .forEach(interfaceInstance -> interfaceImplementations.addAll(getInterfaceImplementations(interfaceInstance))));
119         return interfaceImplementations;
120     }
121
122     private Collection<String> getInterfaceImplementations(final Object interfaceInstance) {
123         final Collection<String> interfaceImplementations = new ArrayList<>();
124         if (interfaceInstance instanceof Map) {
125             for (final Object value : ((Map<?, ?>) interfaceInstance).values()) {
126                 if (value instanceof Map && ((Map<?, ?>) value).get("implementation") != null) {
127                     interfaceImplementations.add(((Map<?, ?>) value).get("implementation").toString());
128                 }
129             }
130         }
131         return interfaceImplementations;
132     }
133
134     private String getProperty(final ToscaNodeType nodeType, final String propertyName) {
135         final ToscaProperty toscaProperty = nodeType.getProperties().get(propertyName);
136         final String errorMsg = String.format("Property '%s' must be defined and must have a valid values constraint", propertyName);
137         final String returnValueOnError = "unknown";
138         if (toscaProperty == null || CollectionUtils.isEmpty(toscaProperty.getConstraints())) {
139             LOGGER.error(errorMsg);
140             return returnValueOnError;
141         }
142         final ToscaPropertyConstraint toscaPropertyConstraint = toscaProperty.getConstraints().get(0);
143         if (ConstraintType.VALID_VALUES != toscaPropertyConstraint.getConstraintType()) {
144             LOGGER.error(errorMsg);
145             return returnValueOnError;
146         }
147         final ToscaPropertyConstraintValidValues validValuesConstraint = (ToscaPropertyConstraintValidValues) toscaPropertyConstraint;
148         final List<String> validValues = validValuesConstraint.getValidValues();
149         if (CollectionUtils.isEmpty(validValues)) {
150             LOGGER.error(errorMsg);
151             return returnValueOnError;
152         }
153         return validValues.get(0);
154     }
155
156     private ToscaTemplate createNetworkServiceDescriptor(final Component component, final List<VnfDescriptor> vnfDescriptorList) throws NsdException {
157         final ToscaTemplate componentToscaTemplate = parseToToscaTemplate(component);
158         final ToscaTemplate componentToscaTemplateInterface = exportComponentInterfaceAsToscaTemplate(component);
159         final Entry<String, ToscaNodeType> firstNodeTypeEntry = componentToscaTemplateInterface.getNode_types().entrySet().stream().findFirst()
160             .orElse(null);
161         if (firstNodeTypeEntry == null) {
162             throw new NsdException("Could not find abstract Service type");
163         }
164         final String nsNodeTypeName = firstNodeTypeEntry.getKey();
165         final ToscaNodeType nsNodeType = firstNodeTypeEntry.getValue();
166         final Map<String, ToscaNodeType> nodeTypeMap = new HashMap<>();
167         nodeTypeMap.put(nsNodeTypeName, createEtsiSolNsNodeType(nsNodeType));
168         if (componentToscaTemplate.getNode_types() == null) {
169             componentToscaTemplate.setNode_types(nodeTypeMap);
170         } else {
171             componentToscaTemplate.getNode_types().putAll(nodeTypeMap);
172         }
173         setPropertiesForNodeTemplates(componentToscaTemplate);
174         removeCapabilitiesFromNodeTemplates(componentToscaTemplate);
175         removeOnapPropertiesFromInputs(componentToscaTemplate);
176         handleSubstitutionMappings(componentToscaTemplate, nsNodeTypeName);
177         final Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
178         nodeTemplates.put(nsNodeTypeName,
179             createNodeTemplateForNsNodeType(nsNodeTypeName, componentToscaTemplateInterface.getNode_types().get(nsNodeTypeName)));
180         if (componentToscaTemplate.getTopology_template().getNode_templates() == null) {
181             componentToscaTemplate.getTopology_template().setNode_templates(nodeTemplates);
182         } else {
183             setNodeTemplateTypesForVnfs(componentToscaTemplate, vnfDescriptorList);
184             componentToscaTemplate.getTopology_template().getNode_templates().putAll(nodeTemplates);
185         }
186         removeOnapMetaData(componentToscaTemplate);
187         setDefaultImportsForEtsiSolNsNsd(componentToscaTemplate, vnfDescriptorList);
188         return componentToscaTemplate;
189     }
190
191     private void handleSubstitutionMappings(final ToscaTemplate componentToscaTemplate, final String nsNodeTypeName) {
192         final SubstitutionMapping substitutionMapping = new SubstitutionMapping();
193         substitutionMapping.setNode_type(nsNodeTypeName);
194         final SubstitutionMapping onapSubstitutionMapping = componentToscaTemplate.getTopology_template().getSubstitution_mappings();
195         if (onapSubstitutionMapping != null) {
196             substitutionMapping.setRequirements(onapSubstitutionMapping.getRequirements());
197             substitutionMapping.setCapabilities(onapSubstitutionMapping.getCapabilities());
198         }
199         componentToscaTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
200     }
201
202     private void setNodeTemplateTypesForVnfs(final ToscaTemplate template, final List<VnfDescriptor> vnfDescriptorList) {
203         if (CollectionUtils.isEmpty(vnfDescriptorList)) {
204             return;
205         }
206         final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
207         if (MapUtils.isEmpty(nodeTemplateMap)) {
208             return;
209         }
210         nodeTemplateMap.forEach(
211             (key, toscaNodeTemplate) -> vnfDescriptorList.stream().filter(vnfDescriptor -> key.equals(vnfDescriptor.getName())).findFirst()
212                 .ifPresent(vnfDescriptor -> toscaNodeTemplate.setType(vnfDescriptor.getNodeType())));
213     }
214
215     private void setPropertiesForNodeTemplates(final ToscaTemplate template) {
216         final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
217         if (MapUtils.isEmpty(nodeTemplateMap)) {
218             return;
219         }
220         for (final Entry<String, ToscaNodeTemplate> nodeTemplate : nodeTemplateMap.entrySet()) {
221             final Map<String, Object> propertyMap = nodeTemplate.getValue().getProperties();
222             if (MapUtils.isEmpty(propertyMap)) {
223                 nodeTemplate.getValue().setProperties(null);
224                 continue;
225             }
226             final Map<String, Object> editedPropertyMap = new HashMap<>();
227             for (final Entry<String, Object> property : propertyMap.entrySet()) {
228                 if (!PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TEMPLATE.contains(property.getKey()) && propertyIsDefinedInNodeType(
229                     property.getKey())) {
230                     editedPropertyMap.put(property.getKey().substring(property.getKey().indexOf('_') + 1), property.getValue());
231                 }
232             }
233             if (editedPropertyMap.isEmpty()) {
234                 nodeTemplate.getValue().setProperties(null);
235             } else {
236                 nodeTemplate.getValue().setProperties(editedPropertyMap);
237             }
238         }
239     }
240
241     private void removeCapabilitiesFromNodeTemplates(final ToscaTemplate template) {
242         final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
243         if (MapUtils.isEmpty(nodeTemplateMap)) {
244             return;
245         }
246         for (final Entry<String, ToscaNodeTemplate> nodeTemplate : nodeTemplateMap.entrySet()) {
247             nodeTemplate.getValue().setCapabilities(null);
248         }
249     }
250
251     private void removeOnapPropertiesFromInputs(final ToscaTemplate template) {
252         final ToscaTopolgyTemplate topologyTemplate = template.getTopology_template();
253         final Map<String, ToscaProperty> inputMap = topologyTemplate.getInputs();
254         if (MapUtils.isNotEmpty(inputMap)) {
255             inputMap.entrySet().removeIf(entry -> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TYPE.contains(entry.getKey()));
256         }
257         if (MapUtils.isEmpty(inputMap)) {
258             topologyTemplate.setInputs(null);
259         }
260     }
261
262     private void removeOnapMetaData(final ToscaTemplate template) {
263         template.setMetadata(null);
264         final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
265         if (MapUtils.isEmpty(nodeTemplateMap)) {
266             return;
267         }
268         nodeTemplateMap.values().forEach(toscaNodeTemplate -> toscaNodeTemplate.setMetadata(null));
269     }
270
271     private void setDefaultImportsForEtsiSolNsNsd(final ToscaTemplate template, final List<VnfDescriptor> vnfDescriptorList) {
272         final List<Map<String, Map<String, String>>> importEntryMap = new ArrayList<>();
273         final Map<String, Map<String, String>> defaultImportEntryMap = generateDefaultImportEntry();
274         if (MapUtils.isNotEmpty(defaultImportEntryMap)) {
275             importEntryMap.add(defaultImportEntryMap);
276         }
277         if (CollectionUtils.isNotEmpty(vnfDescriptorList)) {
278             for (final VnfDescriptor vnfDescriptor : vnfDescriptorList) {
279                 final Map<String, String> vnfImportChildEntry = new HashMap<>();
280                 vnfImportChildEntry.put("file", vnfDescriptor.getVnfdFileName());
281                 final Map<String, Map<String, String>> vnfdImportVnfdEntry = new HashMap<>();
282                 vnfdImportVnfdEntry.put(vnfDescriptor.getName(), vnfImportChildEntry);
283                 importEntryMap.add(vnfdImportVnfdEntry);
284             }
285         }
286         template.setImports(importEntryMap);
287     }
288
289     private Map<String, Map<String, String>> generateDefaultImportEntry() {
290         return ImmutableMap.of("etsi_nfv_sol001_nsd_types", ImmutableMap.of("file", "etsi_nfv_sol001_nsd_types.yaml"));
291     }
292
293     private ToscaNodeType createEtsiSolNsNodeType(final ToscaNodeType nsNodeType) {
294         final ToscaNodeType toscaNodeType = new ToscaNodeType();
295         toscaNodeType.setDerived_from(NS_TOSCA_TYPE);
296         final Map<String, ToscaProperty> propertiesInNsNodeType = nsNodeType.getProperties();
297         for (final Entry<String, ToscaProperty> property : propertiesInNsNodeType.entrySet()) {
298             final ToscaProperty toscaProperty = property.getValue();
299             if (toscaProperty.getDefaultp() != null) {
300                 final ToscaPropertyConstraintValidValues constraint = new ToscaPropertyConstraintValidValues(
301                     Collections.singletonList(toscaProperty.getDefaultp().toString()));
302                 toscaProperty.setConstraints(Collections.singletonList(constraint));
303             }
304         }
305         propertiesInNsNodeType.entrySet().removeIf(entry -> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TYPE.contains(entry.getKey()));
306         toscaNodeType.setProperties(propertiesInNsNodeType);
307         return toscaNodeType;
308     }
309
310     private boolean propertyIsDefinedInNodeType(final String propertyName) {
311         // This will achieve what we want for now, but will look into a more generic solution which would involve
312
313         // checking the node_type definition in the VNFD
314         return !propertyName.equals("additional_parameters");
315     }
316
317     private ToscaNodeTemplate createNodeTemplateForNsNodeType(final String nodeType, final ToscaNodeType toscaNodeType) {
318         final ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
319         nodeTemplate.setType(nodeType);
320         final Map<String, ToscaProperty> properties = toscaNodeType.getProperties();
321         final Map<String, Object> nodeTemplateProperties = new HashMap<>();
322         for (final Entry<String, ToscaProperty> property : properties.entrySet()) {
323             nodeTemplateProperties.put(property.getKey(), property.getValue().getDefaultp());
324         }
325         if (!nodeTemplateProperties.isEmpty()) {
326             nodeTemplate.setProperties(nodeTemplateProperties);
327         }
328         final Map<String, Object> interfaces = toscaNodeType.getInterfaces();
329         if (interfaces != null) {
330             for (final Entry<String, Object> nodeInterface : interfaces.entrySet()) {
331                 if ("Nslcm".equals(nodeInterface.getKey()) && nodeInterface.getValue() instanceof Map) {
332                     ((Map<?, ?>) nodeInterface.getValue()).remove("type");
333                 }
334             }
335             nodeTemplate.setInterfaces(interfaces);
336         }
337         return nodeTemplate;
338     }
339
340     private ToscaTemplate parseToToscaTemplate(final Component component) throws NsdException {
341         final Either<ToscaTemplate, ToscaError> toscaTemplateRes = toscaExportHandler.convertToToscaTemplate(component);
342         if (toscaTemplateRes.isRight()) {
343             String errorMsg = String
344                 .format("Could not parse component '%s' to tosca template. Error '%s'", component.getName(), toscaTemplateRes.right().value().name());
345             throw new NsdException(errorMsg);
346         }
347         return toscaTemplateRes.left().value();
348     }
349
350     private ToscaTemplate exportComponentInterfaceAsToscaTemplate(final Component component) throws NsdException {
351         if (null == DEFAULT_IMPORTS) {
352             throw new NsdException("Could not load default CSAR imports from configuration");
353         }
354         final ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
355         toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
356         final Either<ToscaTemplate, ToscaError> toscaTemplateRes = toscaExportHandler
357             .convertInterfaceNodeType(new HashMap<>(), component, toscaTemplate, new HashMap<>(), false);
358         if (toscaTemplateRes.isRight()) {
359             throw new NsdException(String.format("Could not create abstract service from component '%s'", component.getName()));
360         }
361         return toscaTemplateRes.left().value();
362     }
363 }