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.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.anyBoolean;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
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;
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;
69 class NsDescriptorGeneratorImplTest {
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<>() {
75 public ToscaTemplateYamlGenerator getObject(Object... args) {
76 return new ToscaTemplateYamlGenerator((ToscaTemplate) args[0]);
80 public ToscaTemplateYamlGenerator getIfAvailable() {
85 public ToscaTemplateYamlGenerator getIfUnique() {
90 public ToscaTemplateYamlGenerator getObject() {
95 private ToscaExportHandler toscaExportHandler;
96 private NsDescriptorGeneratorImpl nsDescriptorGenerator;
100 setUpConfigurationMock();
101 MockitoAnnotations.initMocks(this);
102 nsDescriptorGenerator = new NsDescriptorGeneratorImpl(toscaExportHandler, toscaTemplateYamlGeneratorProvider);
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);
122 void testGenerate() throws IOException, NsdException {
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 final Map<String, Object> propertyMap = new HashMap<>();
133 //a property to be excluded
134 propertyMap.put("nf_naming_code", new ToscaProperty());
135 //a property that wont be excluded
136 propertyMap.put("will_not_be_excluded", new ToscaProperty());
137 vnfAmfNodeTemplate.setProperties(propertyMap);
138 nodeTemplateMap.put(VNFD_AMF_NODE_NAME, vnfAmfNodeTemplate);
139 final Map<String, ToscaTemplateCapability> vnfAmfCapabilities = new HashMap<>();
140 vnfAmfCapabilities.put("myCapability", new ToscaTemplateCapability());
141 vnfAmfNodeTemplate.setCapabilities(vnfAmfCapabilities);
142 componentToscaTopologyTemplate.setNode_templates(nodeTemplateMap);
143 final SubstitutionMapping substitutionMapping = mock(SubstitutionMapping.class);
144 Map<String, String[]> requirements = new HashMap<>();
145 String[] requirementAssignment = {"VNF1", VIRTUAL_LINK_REQUIREMENT_NAME};
146 requirements.put(VIRTUAL_LINK_REQUIREMENT_NAME, requirementAssignment);
147 when(substitutionMapping.getRequirements()).thenReturn(requirements);
148 Map<String, String[]> capabilities = new HashMap<>();
149 String[] capabilitiesAssignment = {"VNF1", "capability1"};
150 capabilities.put("capability", capabilitiesAssignment);
151 when(substitutionMapping.getCapabilities()).thenReturn(capabilities);
152 componentToscaTopologyTemplate.setSubstitution_mappings(substitutionMapping);
153 final ToscaTemplate componentInterfaceToscaTemplate = new ToscaTemplate("");
154 final String designerPropertyValue = "designerValue";
155 final String versionPropertyValue = "versionValue";
156 final String namePropertyValue = "nameValue";
157 final String invariantIdPropertyValue = "invariantIdValue";
158 final ToscaNodeType interfaceToscaNodeType = createDefaultInterfaceToscaNodeType(designerPropertyValue, versionPropertyValue,
159 namePropertyValue, invariantIdPropertyValue);
160 final String nsNodeTypeName = "nsNodeTypeName";
161 componentInterfaceToscaTemplate.setNode_types(ImmutableMap.of(nsNodeTypeName, interfaceToscaNodeType));
162 when(toscaExportHandler.convertToToscaTemplate(component)).thenReturn(Either.left(componentToscaTemplate));
163 when(toscaExportHandler.convertInterfaceNodeType(any(), any(), any(), any(), anyBoolean()))
164 .thenReturn(Either.left(componentInterfaceToscaTemplate));
165 final List<VnfDescriptor> vnfDescriptorList = new ArrayList<>();
166 VnfDescriptor vnfDescriptor1 = new VnfDescriptor();
167 vnfDescriptor1.setName(VNFD_AMF_NODE_NAME);
168 vnfDescriptor1.setVnfdFileName("vnfd_amf.yaml");
169 vnfDescriptor1.setNodeType("com.ericsson.resource.abstract.Ericsson.AMF");
170 vnfDescriptorList.add(vnfDescriptor1);
172 final Nsd nsd = nsDescriptorGenerator.generate(component, vnfDescriptorList).orElse(null);
175 assertThat("Nsd designer should be as expected", nsd.getDesigner(), is(designerPropertyValue));
176 assertThat("Nsd version should be as expected", nsd.getVersion(), is(versionPropertyValue));
177 assertThat("Nsd name should be as expected", nsd.getName(), is(namePropertyValue));
178 assertThat("Nsd invariantId should be as expected", nsd.getInvariantId(), is(invariantIdPropertyValue));
179 final Map<String, Object> toscaTemplateYaml = readYamlAsMap(nsd.getContents());
180 @SuppressWarnings("unchecked") final Map<String, Object> topologyTemplate = (Map<String, Object>) toscaTemplateYaml.get("topology_template");
181 assertThat("topology_template should not be empty", topologyTemplate, is(not(anEmptyMap())));
182 @SuppressWarnings("unchecked") final Map<String, Object> substitutionMappings = (Map<String, Object>) topologyTemplate
183 .get("substitution_mappings");
184 assertThat("substitution_mappings should not be empty", substitutionMappings, is(not(anEmptyMap())));
185 assertThat("substitution_mappings->node_type should not be null", substitutionMappings.get("node_type"), is(notNullValue()));
186 assertThat("substitution_mappings->node_type should be as expected", substitutionMappings.get("node_type"), is(nsNodeTypeName));
187 final Map<String, List<String>> subMappingRequirements = (Map<String, List<String>>) substitutionMappings.get("requirements");
188 assertThat(subMappingRequirements.get(VIRTUAL_LINK_REQUIREMENT_NAME).get(0), is("VNF1"));
189 assertThat(subMappingRequirements.get(VIRTUAL_LINK_REQUIREMENT_NAME).get(1), is(VIRTUAL_LINK_REQUIREMENT_NAME));
190 final Map<String, List<String>> subMappingCapabilities = (Map<String, List<String>>) substitutionMappings.get("capabilities");
191 assertThat(subMappingCapabilities.get("capability").get(0), is("VNF1"));
192 assertThat(subMappingCapabilities.get("capability").get(1), is("capability1"));
193 @SuppressWarnings("unchecked") final Map<String, Object> nodeTemplates = (Map<String, Object>) topologyTemplate.get("node_templates");
194 @SuppressWarnings("unchecked") final Map<String, Object> nodeTemplate = (Map<String, Object>) nodeTemplates.get(VNFD_AMF_NODE_NAME);
195 assertThat("capabilities should be null", nodeTemplate.get("capabilities"), is(nullValue()));
198 private ToscaNodeType createDefaultInterfaceToscaNodeType(final String designerPropertyValue, final String versionPropertyValue,
199 final String namePropertyValue, final String invariantIdPropertyValue) {
200 final ToscaNodeType interfaceToscaNodeType = new ToscaNodeType();
201 final Map<String, ToscaProperty> propertyMap = new HashMap<>();
202 propertyMap.put("designer", createToscaProperty(designerPropertyValue));
203 propertyMap.put("version", createToscaProperty(versionPropertyValue));
204 propertyMap.put("name", createToscaProperty(namePropertyValue));
205 propertyMap.put("invariant_id", createToscaProperty(invariantIdPropertyValue));
206 interfaceToscaNodeType.setProperties(propertyMap);
207 return interfaceToscaNodeType;
210 private void assertNotEmpty(Nsd nsd) {
211 assertThat("Nsd should not be null", nsd, is(notNullValue()));
212 assertThat("Nsd content should not be empty", nsd.getContents(), is(notNullValue()));
213 assertThat("Nsd content should not be empty", nsd.getContents().length, is(greaterThan(0)));
216 private ToscaProperty createToscaProperty(final String value) {
217 final ToscaProperty toscaProperty = new ToscaProperty();
218 final ToscaPropertyConstraint toscaPropertyConstraint = new ToscaPropertyConstraintValidValues(ImmutableList.of(value));
219 toscaProperty.setConstraints(ImmutableList.of(toscaPropertyConstraint));
220 return toscaProperty;
223 @SuppressWarnings("unchecked")
224 private Map<String, Object> readYamlAsMap(final byte[] yamlContents) throws IOException {
225 final Yaml yaml = new Yaml();
226 try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(yamlContents)) {
227 return (Map<String, Object>) yaml.load(byteArrayInputStream);