2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
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.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.ArgumentMatchers.anyBoolean;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableMap;
40 import fj.data.Either;
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.openecomp.sdc.be.config.Configuration;
52 import org.openecomp.sdc.be.config.ConfigurationManager;
53 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
54 import org.openecomp.sdc.be.model.Component;
55 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
56 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd;
57 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
58 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml.ToscaTemplateYamlGenerator;
59 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
60 import org.openecomp.sdc.be.tosca.model.SubstitutionMapping;
61 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
62 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
63 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
64 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
65 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
66 import org.openecomp.sdc.be.tosca.model.ToscaRequirement;
67 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
68 import org.openecomp.sdc.be.tosca.model.ToscaTemplateCapability;
69 import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
70 import org.openecomp.sdc.common.api.ConfigurationSource;
71 import org.springframework.beans.factory.ObjectProvider;
72 import org.yaml.snakeyaml.Yaml;
74 class NsDescriptorGeneratorImplTest {
76 private static final String VNFD_AMF_NODE_NAME = "vnfd_amf";
77 private static final String VIRTUAL_LINK_REQUIREMENT_NAME = "virtual_link";
78 private static final String VIRTUAL_BINDING_REQUIREMENT_NAME = "virtual_binding";
79 private static final String DOT = ".";
80 private static final String PREFIX = "VNF";
81 private final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider = new ObjectProvider<>() {
83 public ToscaTemplateYamlGenerator getObject(Object... args) {
84 return new ToscaTemplateYamlGenerator((ToscaTemplate) args[0]);
88 public ToscaTemplateYamlGenerator getIfAvailable() {
93 public ToscaTemplateYamlGenerator getIfUnique() {
98 public ToscaTemplateYamlGenerator getObject() {
103 private ToscaExportHandler toscaExportHandler;
104 private NsDescriptorGeneratorImpl nsDescriptorGenerator;
108 setUpConfigurationMock();
109 MockitoAnnotations.initMocks(this);
110 nsDescriptorGenerator = new NsDescriptorGeneratorImpl(toscaExportHandler, toscaTemplateYamlGeneratorProvider);
113 private void setUpConfigurationMock() {
114 final List<Map<String, Map<String, String>>> defaultImports = new ArrayList<>();
115 final Map<String, Map<String, String>> importMap = new HashMap<>();
116 final Map<String, String> nodeImportEntry = new HashMap<>();
117 nodeImportEntry.put("file", "nodes.yml");
118 importMap.put("nodes", nodeImportEntry);
119 defaultImports.add(importMap);
120 final ConfigurationSource configurationSource = mock(ConfigurationSource.class);
121 final Configuration configuration = new Configuration();
122 configuration.setDefaultImports(defaultImports);
123 configuration.setHeatEnvArtifactHeader("");
124 configuration.setHeatEnvArtifactFooter("");
125 when(configurationSource.getAndWatchConfiguration(any(), any())).thenReturn(configuration);
126 new ConfigurationManager(configurationSource);
130 void testGenerate() throws IOException, NsdException {
132 final Component component = mock(Component.class);
133 when(component.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
134 final ToscaTemplate componentToscaTemplate = new ToscaTemplate("");
135 final ToscaTopolgyTemplate componentToscaTopologyTemplate = new ToscaTopolgyTemplate();
136 componentToscaTemplate.setTopology_template(componentToscaTopologyTemplate);
137 final HashMap<String, ToscaNodeTemplate> nodeTemplateMap = new HashMap<>();
138 final ToscaNodeTemplate vnfAmfNodeTemplate = new ToscaNodeTemplate();
139 vnfAmfNodeTemplate.setType("com.ericsson.resource.abstract.Ericsson.AMF");
140 final Map<String, Object> propertyMap = new HashMap<>();
141 //a property to be excluded
142 propertyMap.put("nf_naming_code", new ToscaProperty());
143 //a property that wont be excluded
144 propertyMap.put("will_not_be_excluded", new ToscaProperty());
145 vnfAmfNodeTemplate.setProperties(propertyMap);
146 nodeTemplateMap.put(VNFD_AMF_NODE_NAME, vnfAmfNodeTemplate);
147 final Map<String, ToscaTemplateCapability> vnfAmfCapabilities = new HashMap<>();
148 vnfAmfCapabilities.put("myCapability", new ToscaTemplateCapability());
149 vnfAmfNodeTemplate.setCapabilities(vnfAmfCapabilities);
150 componentToscaTopologyTemplate.setNode_templates(nodeTemplateMap);
151 final SubstitutionMapping substitutionMapping = mock(SubstitutionMapping.class);
152 Map<String, String[]> requirements = new HashMap<>();
153 String[] requirementAssignmentVl = {"VNF1", PREFIX + DOT + VIRTUAL_LINK_REQUIREMENT_NAME};
154 requirements.put("VNF1" + DOT + VIRTUAL_LINK_REQUIREMENT_NAME, requirementAssignmentVl);
155 when(substitutionMapping.getRequirements()).thenReturn(requirements);
156 Map<String, String[]> capabilities = new HashMap<>();
157 String[] capabilitiesAssignment = {"VNF1", "capability1"};
158 capabilities.put("capability", capabilitiesAssignment);
159 when(substitutionMapping.getCapabilities()).thenReturn(capabilities);
160 componentToscaTopologyTemplate.setSubstitution_mappings(substitutionMapping);
161 final ToscaTemplate componentInterfaceToscaTemplate = new ToscaTemplate("");
162 final String designerPropertyValue = "designerValue";
163 final String versionPropertyValue = "versionValue";
164 final String namePropertyValue = "nameValue";
165 final String invariantIdPropertyValue = "invariantIdValue";
166 final ToscaNodeType interfaceToscaNodeType = createDefaultInterfaceToscaNodeType(designerPropertyValue, versionPropertyValue,
167 namePropertyValue, invariantIdPropertyValue);
168 List<Map<String, ToscaRequirement>> interfaceNodeTypeRequirements = new ArrayList<>();
169 Map<String, ToscaRequirement> interfaceNodeTypeRequirementMap = new HashMap<>();
170 interfaceNodeTypeRequirementMap.put("VNF1" + DOT + VIRTUAL_LINK_REQUIREMENT_NAME, mock(ToscaRequirement.class));
171 interfaceNodeTypeRequirementMap.put("VNF1" + DOT + VIRTUAL_BINDING_REQUIREMENT_NAME, mock(ToscaRequirement.class));
172 interfaceNodeTypeRequirements.add(interfaceNodeTypeRequirementMap);
173 interfaceToscaNodeType.setRequirements(interfaceNodeTypeRequirements);
174 final String nsNodeTypeName = "nsNodeTypeName";
175 componentInterfaceToscaTemplate.setNode_types(ImmutableMap.of(nsNodeTypeName, interfaceToscaNodeType));
176 when(toscaExportHandler.convertToToscaTemplate(component)).thenReturn(Either.left(componentToscaTemplate));
177 when(toscaExportHandler.convertInterfaceNodeType(any(), any(), any(), any(), anyBoolean()))
178 .thenReturn(Either.left(componentInterfaceToscaTemplate));
179 final List<VnfDescriptor> vnfDescriptorList = new ArrayList<>();
180 VnfDescriptor vnfDescriptor1 = new VnfDescriptor();
181 vnfDescriptor1.setName(VNFD_AMF_NODE_NAME);
182 vnfDescriptor1.setVnfdFileName("vnfd_amf.yaml");
183 vnfDescriptor1.setNodeType("com.ericsson.resource.abstract.Ericsson.AMF");
184 vnfDescriptorList.add(vnfDescriptor1);
186 final Nsd nsd = nsDescriptorGenerator.generate(component, vnfDescriptorList).orElse(null);
189 assertThat("Nsd designer should be as expected", nsd.getDesigner(), is(designerPropertyValue));
190 assertThat("Nsd version should be as expected", nsd.getVersion(), is(versionPropertyValue));
191 assertThat("Nsd name should be as expected", nsd.getName(), is(namePropertyValue));
192 assertThat("Nsd invariantId should be as expected", nsd.getInvariantId(), is(invariantIdPropertyValue));
193 final Map<String, Object> toscaTemplateYaml = readYamlAsMap(nsd.getContents());
194 @SuppressWarnings("unchecked") final Map<String, Object> topologyTemplate = (Map<String, Object>) toscaTemplateYaml.get("topology_template");
195 assertThat("topology_template should not be empty", topologyTemplate, is(not(anEmptyMap())));
196 @SuppressWarnings("unchecked") final Map<String, Object> substitutionMappings = (Map<String, Object>) topologyTemplate
197 .get("substitution_mappings");
198 assertThat("substitution_mappings should not be empty", substitutionMappings, is(not(anEmptyMap())));
199 assertThat("substitution_mappings->node_type should not be null", substitutionMappings.get("node_type"), is(notNullValue()));
200 assertThat("substitution_mappings->node_type should be as expected", substitutionMappings.get("node_type"), is(nsNodeTypeName));
201 final Map<String, List<String>> subMappingRequirements = (Map<String, List<String>>) substitutionMappings.get("requirements");
202 assertThat(subMappingRequirements.get("VNF1" + DOT + VIRTUAL_LINK_REQUIREMENT_NAME).get(0), is("VNF1"));
203 assertThat(subMappingRequirements.get("VNF1" + DOT + VIRTUAL_LINK_REQUIREMENT_NAME).get(1), is(VIRTUAL_LINK_REQUIREMENT_NAME));
204 assertEquals(1, subMappingRequirements.size());
205 final Map<String, List<String>> subMappingCapabilities = (Map<String, List<String>>) substitutionMappings.get("capabilities");
206 assertNull(subMappingCapabilities);
208 @SuppressWarnings("unchecked") final Map<String, Object> nodeType = (Map<String, Object>) ((Map<String, Object>) toscaTemplateYaml.get("node_types")).get(nsNodeTypeName);
209 assertTrue(((List<Map<String, Map>>)nodeType.get("requirements")).get(0).containsKey("VNF1" + DOT + VIRTUAL_LINK_REQUIREMENT_NAME));
210 assertFalse(((List<Map<String, Map>>)nodeType.get("requirements")).get(0).containsKey("VNF1" + DOT + VIRTUAL_BINDING_REQUIREMENT_NAME));
213 private ToscaNodeType createDefaultInterfaceToscaNodeType(final String designerPropertyValue, final String versionPropertyValue,
214 final String namePropertyValue, final String invariantIdPropertyValue) {
215 final ToscaNodeType interfaceToscaNodeType = new ToscaNodeType();
216 final Map<String, ToscaProperty> propertyMap = new HashMap<>();
217 propertyMap.put("designer", createToscaProperty(designerPropertyValue));
218 propertyMap.put("version", createToscaProperty(versionPropertyValue));
219 propertyMap.put("name", createToscaProperty(namePropertyValue));
220 propertyMap.put("invariant_id", createToscaProperty(invariantIdPropertyValue));
221 interfaceToscaNodeType.setProperties(propertyMap);
222 return interfaceToscaNodeType;
225 private void assertNotEmpty(Nsd nsd) {
226 assertThat("Nsd should not be null", nsd, is(notNullValue()));
227 assertThat("Nsd content should not be empty", nsd.getContents(), is(notNullValue()));
228 assertThat("Nsd content should not be empty", nsd.getContents().length, is(greaterThan(0)));
231 private ToscaProperty createToscaProperty(final String value) {
232 final ToscaProperty toscaProperty = new ToscaProperty();
233 final ToscaPropertyConstraint toscaPropertyConstraint = new ToscaPropertyConstraintValidValues(ImmutableList.of(value));
234 toscaProperty.setConstraints(ImmutableList.of(toscaPropertyConstraint));
235 return toscaProperty;
238 @SuppressWarnings("unchecked")
239 private Map<String, Object> readYamlAsMap(final byte[] yamlContents) throws IOException {
240 final Yaml yaml = new Yaml();
241 try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(yamlContents)) {
242 return (Map<String, Object>) yaml.load(byteArrayInputStream);