Reformat catalog-be-plugins
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / test / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / generator / NsDescriptorGeneratorImplTest.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 static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.anEmptyMap;
24 import static org.hamcrest.Matchers.greaterThan;
25 import static org.hamcrest.Matchers.not;
26 import static org.hamcrest.core.Is.is;
27 import static org.hamcrest.core.IsNull.notNullValue;
28 import static org.hamcrest.core.IsNull.nullValue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyBoolean;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import com.google.common.collect.ImmutableList;
35 import com.google.common.collect.ImmutableMap;
36 import fj.data.Either;
37 import java.io.ByteArrayInputStream;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.openecomp.sdc.be.config.Configuration;
48 import org.openecomp.sdc.be.config.ConfigurationManager;
49 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
50 import org.openecomp.sdc.be.model.Component;
51 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
52 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd;
53 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
54 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml.ToscaTemplateYamlGenerator;
55 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
56 import org.openecomp.sdc.be.tosca.model.SubstitutionMapping;
57 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
58 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
59 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
60 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
61 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
62 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
63 import org.openecomp.sdc.be.tosca.model.ToscaTemplateCapability;
64 import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
65 import org.openecomp.sdc.common.api.ConfigurationSource;
66 import org.springframework.beans.factory.ObjectProvider;
67 import org.yaml.snakeyaml.Yaml;
68
69 class NsDescriptorGeneratorImplTest {
70
71     private static final String VNFD_AMF_NODE_NAME = "vnfd_amf";
72     private static final String VIRTUAL_LINK_REQUIREMENT_NAME = "virtual_link";
73     private final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider = new ObjectProvider<>() {
74         @Override
75         public ToscaTemplateYamlGenerator getObject(Object... args) {
76             return new ToscaTemplateYamlGenerator((ToscaTemplate) args[0]);
77         }
78
79         @Override
80         public ToscaTemplateYamlGenerator getIfAvailable() {
81             return null;
82         }
83
84         @Override
85         public ToscaTemplateYamlGenerator getIfUnique() {
86             return null;
87         }
88
89         @Override
90         public ToscaTemplateYamlGenerator getObject() {
91             return null;
92         }
93     };
94     @Mock
95     private ToscaExportHandler toscaExportHandler;
96     private NsDescriptorGeneratorImpl nsDescriptorGenerator;
97
98     @BeforeEach
99     void setUp() {
100         setUpConfigurationMock();
101         MockitoAnnotations.initMocks(this);
102         nsDescriptorGenerator = new NsDescriptorGeneratorImpl(toscaExportHandler, toscaTemplateYamlGeneratorProvider);
103     }
104
105     private void setUpConfigurationMock() {
106         final List<Map<String, Map<String, String>>> defaultImports = new ArrayList<>();
107         final Map<String, Map<String, String>> importMap = new HashMap<>();
108         final Map<String, String> nodeImportEntry = new HashMap<>();
109         nodeImportEntry.put("file", "nodes.yml");
110         importMap.put("nodes", nodeImportEntry);
111         defaultImports.add(importMap);
112         final ConfigurationSource configurationSource = mock(ConfigurationSource.class);
113         final Configuration configuration = new Configuration();
114         configuration.setDefaultImports(defaultImports);
115         configuration.setHeatEnvArtifactHeader("");
116         configuration.setHeatEnvArtifactFooter("");
117         when(configurationSource.getAndWatchConfiguration(any(), any())).thenReturn(configuration);
118         new ConfigurationManager(configurationSource);
119     }
120
121     @Test
122     void testGenerate() throws IOException, NsdException {
123         //given
124         final Component component = mock(Component.class);
125         when(component.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
126         final ToscaTemplate componentToscaTemplate = new ToscaTemplate("");
127         final ToscaTopolgyTemplate componentToscaTopologyTemplate = new ToscaTopolgyTemplate();
128         componentToscaTemplate.setTopology_template(componentToscaTopologyTemplate);
129         final HashMap<String, ToscaNodeTemplate> nodeTemplateMap = new HashMap<>();
130         final ToscaNodeTemplate vnfAmfNodeTemplate = new ToscaNodeTemplate();
131         vnfAmfNodeTemplate.setType("com.ericsson.resource.abstract.Ericsson.AMF");
132         //a property to be excluded
133         vnfAmfNodeTemplate.setProperties(ImmutableMap.of("nf_naming_code", new ToscaProperty()));
134         //a property that wont be excluded
135         vnfAmfNodeTemplate.setProperties(ImmutableMap.of("will_not_be_excluded", new ToscaProperty()));
136         nodeTemplateMap.put(VNFD_AMF_NODE_NAME, vnfAmfNodeTemplate);
137         final Map<String, ToscaTemplateCapability> vnfAmfCapabilities = new HashMap<>();
138         vnfAmfCapabilities.put("myCapability", new ToscaTemplateCapability());
139         vnfAmfNodeTemplate.setCapabilities(vnfAmfCapabilities);
140         componentToscaTopologyTemplate.setNode_templates(nodeTemplateMap);
141         final SubstitutionMapping substitutionMapping = mock(SubstitutionMapping.class);
142         Map<String, String[]> requirements = new HashMap<>();
143         String[] requirementAssignment = {"VNF1", VIRTUAL_LINK_REQUIREMENT_NAME};
144         requirements.put(VIRTUAL_LINK_REQUIREMENT_NAME, requirementAssignment);
145         when(substitutionMapping.getRequirements()).thenReturn(requirements);
146         Map<String, String[]> capabilities = new HashMap<>();
147         String[] capabilitiesAssignment = {"VNF1", "capability1"};
148         capabilities.put("capability", capabilitiesAssignment);
149         when(substitutionMapping.getCapabilities()).thenReturn(capabilities);
150         componentToscaTopologyTemplate.setSubstitution_mappings(substitutionMapping);
151         final ToscaTemplate componentInterfaceToscaTemplate = new ToscaTemplate("");
152         final String designerPropertyValue = "designerValue";
153         final String versionPropertyValue = "versionValue";
154         final String namePropertyValue = "nameValue";
155         final String invariantIdPropertyValue = "invariantIdValue";
156         final ToscaNodeType interfaceToscaNodeType = createDefaultInterfaceToscaNodeType(designerPropertyValue, versionPropertyValue,
157             namePropertyValue, invariantIdPropertyValue);
158         final String nsNodeTypeName = "nsNodeTypeName";
159         componentInterfaceToscaTemplate.setNode_types(ImmutableMap.of(nsNodeTypeName, interfaceToscaNodeType));
160         when(toscaExportHandler.convertToToscaTemplate(component)).thenReturn(Either.left(componentToscaTemplate));
161         when(toscaExportHandler.convertInterfaceNodeType(any(), any(), any(), any(), anyBoolean()))
162             .thenReturn(Either.left(componentInterfaceToscaTemplate));
163         final List<VnfDescriptor> vnfDescriptorList = new ArrayList<>();
164         VnfDescriptor vnfDescriptor1 = new VnfDescriptor();
165         vnfDescriptor1.setName(VNFD_AMF_NODE_NAME);
166         vnfDescriptor1.setVnfdFileName("vnfd_amf.yaml");
167         vnfDescriptor1.setNodeType("com.ericsson.resource.abstract.Ericsson.AMF");
168         vnfDescriptorList.add(vnfDescriptor1);
169         //when
170         final Nsd nsd = nsDescriptorGenerator.generate(component, vnfDescriptorList).orElse(null);
171         //then
172         assertNotEmpty(nsd);
173         assertThat("Nsd designer should be as expected", nsd.getDesigner(), is(designerPropertyValue));
174         assertThat("Nsd version should be as expected", nsd.getVersion(), is(versionPropertyValue));
175         assertThat("Nsd name should be as expected", nsd.getName(), is(namePropertyValue));
176         assertThat("Nsd invariantId should be as expected", nsd.getInvariantId(), is(invariantIdPropertyValue));
177         final Map<String, Object> toscaTemplateYaml = readYamlAsMap(nsd.getContents());
178         @SuppressWarnings("unchecked") final Map<String, Object> topologyTemplate = (Map<String, Object>) toscaTemplateYaml.get("topology_template");
179         assertThat("topology_template should not be empty", topologyTemplate, is(not(anEmptyMap())));
180         @SuppressWarnings("unchecked") final Map<String, Object> substitutionMappings = (Map<String, Object>) topologyTemplate
181             .get("substitution_mappings");
182         assertThat("substitution_mappings should not be empty", substitutionMappings, is(not(anEmptyMap())));
183         assertThat("substitution_mappings->node_type should not be null", substitutionMappings.get("node_type"), is(notNullValue()));
184         assertThat("substitution_mappings->node_type should be as expected", substitutionMappings.get("node_type"), is(nsNodeTypeName));
185         final Map<String, List<String>> subMappingRequirements = (Map<String, List<String>>) substitutionMappings.get("requirements");
186         assertThat(subMappingRequirements.get(VIRTUAL_LINK_REQUIREMENT_NAME).get(0), is("VNF1"));
187         assertThat(subMappingRequirements.get(VIRTUAL_LINK_REQUIREMENT_NAME).get(1), is(VIRTUAL_LINK_REQUIREMENT_NAME));
188         final Map<String, List<String>> subMappingCapabilities = (Map<String, List<String>>) substitutionMappings.get("capabilities");
189         assertThat(subMappingCapabilities.get("capability").get(0), is("VNF1"));
190         assertThat(subMappingCapabilities.get("capability").get(1), is("capability1"));
191         @SuppressWarnings("unchecked") final Map<String, Object> nodeTemplates = (Map<String, Object>) topologyTemplate.get("node_templates");
192         @SuppressWarnings("unchecked") final Map<String, Object> nodeTemplate = (Map<String, Object>) nodeTemplates.get(VNFD_AMF_NODE_NAME);
193         assertThat("capabilities should be null", nodeTemplate.get("capabilities"), is(nullValue()));
194     }
195
196     private ToscaNodeType createDefaultInterfaceToscaNodeType(final String designerPropertyValue, final String versionPropertyValue,
197                                                               final String namePropertyValue, final String invariantIdPropertyValue) {
198         final ToscaNodeType interfaceToscaNodeType = new ToscaNodeType();
199         interfaceToscaNodeType.setProperties(ImmutableMap
200             .of("designer", createToscaProperty(designerPropertyValue), "version", createToscaProperty(versionPropertyValue), "name",
201                 createToscaProperty(namePropertyValue), "invariant_id", createToscaProperty(invariantIdPropertyValue)));
202         return interfaceToscaNodeType;
203     }
204
205     private void assertNotEmpty(Nsd nsd) {
206         assertThat("Nsd should not be null", nsd, is(notNullValue()));
207         assertThat("Nsd content should not be empty", nsd.getContents(), is(notNullValue()));
208         assertThat("Nsd content should not be empty", nsd.getContents().length, is(greaterThan(0)));
209     }
210
211     private ToscaProperty createToscaProperty(final String value) {
212         final ToscaProperty toscaProperty = new ToscaProperty();
213         final ToscaPropertyConstraint toscaPropertyConstraint = new ToscaPropertyConstraintValidValues(ImmutableList.of(value));
214         toscaProperty.setConstraints(ImmutableList.of(toscaPropertyConstraint));
215         return toscaProperty;
216     }
217
218     @SuppressWarnings("unchecked")
219     private Map<String, Object> readYamlAsMap(final byte[] yamlContents) throws IOException {
220         final Yaml yaml = new Yaml();
221         try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(yamlContents)) {
222             return (Map<String, Object>) yaml.load(byteArrayInputStream);
223         }
224     }
225 }