2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.composition;
23 import org.apache.commons.io.FileUtils;
24 import org.mockito.InjectMocks;
25 import org.mockito.MockitoAnnotations;
26 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.tosca.services.ToscaUtil;
29 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
34 import org.testng.Assert;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
39 import java.io.FileInputStream;
40 import java.io.FileNotFoundException;
41 import java.io.IOException;
42 import java.io.InputStream;
44 import java.nio.file.NotDirectoryException;
45 import java.util.Collection;
46 import java.util.HashMap;
51 * @since July 17, 2016.
54 public class CompositionDataExtractorImplTest {
56 private static CompositionDataExtractorImpl compositionDataExtractor;
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
63 private static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
64 String globalServiceTemplatesPath,
65 String entryDefinitionServiceTemplate)
67 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
68 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
69 if (entryDefinitionServiceTemplate == null) {
70 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
73 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
74 if (globalServiceTemplatesPath != null) {
75 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
78 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
81 private static void loadServiceTemplates(String serviceTemplatesPath,
82 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
83 Map<String, ServiceTemplate> serviceTemplates)
85 URL urlFile = CompositionDataExtractorImplTest.class.getResource(serviceTemplatesPath);
86 if (urlFile != null) {
87 File pathFile = new File(urlFile.getFile());
88 Collection<File> files = FileUtils.listFiles(pathFile, null, true);
90 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
92 throw new NotDirectoryException(serviceTemplatesPath);
95 throw new NotDirectoryException(serviceTemplatesPath);
99 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
100 Collection<File> files,
101 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
103 for (File file : files) {
104 try (InputStream yamlFile = new FileInputStream(file)) {
105 ServiceTemplate serviceTemplateFromYaml =
106 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
107 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
110 } catch (IOException ignore) {
112 } catch (FileNotFoundException exception) {
114 } catch (IOException exception) {
121 public void testExtractNetworks() throws Exception {
122 ToscaServiceModel toscaServiceModel =
123 loadToscaServiceModel("/extractServiceComposition/networks/",
124 "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
125 CompositionData compositionData =
126 compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
127 Assert.assertEquals(compositionData.getComponents().size(), 0);
128 Assert.assertEquals(compositionData.getNetworks().size(), 7);
130 for (Network network : compositionData.getNetworks()) {
131 boolean dhcp = network.isDhcp();
132 switch (network.getName()) {
133 case "contail-net-default-true-dhcp":
134 Assert.assertEquals(dhcp, true);
136 case "contail-net-dhcp-false-param":
137 Assert.assertEquals(dhcp, false);
139 case "contail-net-dhcp-false":
140 Assert.assertEquals(dhcp, false);
142 case "contail-net-dhcp-true-param":
143 Assert.assertEquals(dhcp, true);
145 case "contail-net-dhcp-true":
146 Assert.assertEquals(dhcp, true);
148 case "contail-net-dhcp-default-true-param":
149 Assert.assertEquals(dhcp, true);
151 case "neutron-net-default-dhcp":
152 Assert.assertEquals(dhcp, true);
155 throw new Exception("Unexpected Network Name " + network.getName());
162 public void testExtractOnlyComponents() throws Exception {
163 ToscaServiceModel toscaServiceModel =
164 loadToscaServiceModel("/extractServiceComposition/onlyComponents/",
165 "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
166 CompositionData compositionData =
167 compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
168 Assert.assertEquals(compositionData.getComponents().size(), 3);
169 Assert.assertEquals(compositionData.getNetworks().size(), 0);
171 for (Component component : compositionData.getComponents()) {
172 switch (component.getData().getName()) {
173 case "org.openecomp.resource.vfc.nodes.heat.pcrf_psm":
174 Assert.assertNull(component.getNics());
175 Assert.assertEquals(component.getData().getDisplayName(), "pcrf_psm");
177 case "org.openecomp.resource.vfc.nodes.heat.nova.Server":
178 Assert.assertNull(component.getNics());
179 Assert.assertEquals(component.getData().getDisplayName(), "Server");
181 case "org.openecomp.resource.vfc.nodes.heat.pcm":
182 Assert.assertNull(component.getNics());
183 Assert.assertEquals(component.getData().getDisplayName(), "pcm");
186 throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
192 public void testExtractComponentsWithPorts() throws Exception {
194 ToscaServiceModel toscaServiceModel =
195 loadToscaServiceModel("/extractServiceComposition/componentsWithPort/",
196 "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
197 CompositionData compositionData =
198 compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
200 Assert.assertEquals(compositionData.getComponents().size(), 3);
201 Assert.assertEquals(compositionData.getNetworks().size(), 0);
203 for (Component component : compositionData.getComponents()) {
204 switch (component.getData().getName()) {
205 case "org.openecomp.resource.vfc.nodes.heat.pcrf_psm":
206 Assert.assertEquals(component.getNics().size(), 1);
207 Assert.assertEquals(component.getNics().get(0).getName(), "psm01_port_0");
208 Assert.assertNull(component.getNics().get(0).getNetworkName());
209 Assert.assertEquals(component.getData().getDisplayName(), "pcrf_psm");
211 case "org.openecomp.resource.vfc.nodes.heat.nova.Server":
212 Assert.assertEquals(component.getNics().size(), 2);
213 Assert.assertEquals(component.getNics().get(0).getName(), "template_VMInt_OAM_lb_2");
214 Assert.assertNull(component.getNics().get(0).getNetworkName());
215 Assert.assertEquals(component.getNics().get(1).getName(), "FSB1_Internal2");
216 Assert.assertNull(component.getNics().get(1).getNetworkName());
217 Assert.assertEquals(component.getData().getDisplayName(), "Server");
219 case "org.openecomp.resource.vfc.nodes.heat.pcm":
220 Assert.assertEquals(component.getNics().size(), 2);
221 Assert.assertEquals(component.getData().getDisplayName(), "pcm");
224 throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
230 public void testExtractFullComposition() throws Exception {
232 ToscaServiceModel toscaServiceModel =
233 loadToscaServiceModel("/extractServiceComposition/fullComposition/",
234 "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
235 CompositionData compositionData =
236 compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
237 Assert.assertEquals(compositionData.getComponents().size(), 3);
238 Assert.assertEquals(compositionData.getNetworks().size(), 4);
240 for (Component component : compositionData.getComponents()) {
241 switch (component.getData().getName()) {
242 case "org.openecomp.resource.vfc.nodes.heat.pcrf_psm":
243 Assert.assertEquals(component.getNics().size(), 1);
244 Assert.assertEquals(component.getNics().get(0).getName(), "psm01_port_0");
245 Assert.assertNull(component.getNics().get(0).getNetworkName());
246 Assert.assertEquals(component.getData().getDisplayName(), "pcrf_psm");
248 case "org.openecomp.resource.vfc.nodes.heat.nova.Server":
249 Assert.assertEquals(component.getNics().size(), 4);
250 Assert.assertEquals(component.getData().getDisplayName(), "Server");
251 for (Nic port : component.getNics()) {
252 switch (port.getName()) {
253 case "FSB1_Internal2_port":
254 Assert.assertEquals(port.getNetworkName(), "Internal2-net");
256 case "FSB1_OAM_Port":
257 Assert.assertNull(port.getNetworkName());
259 case "FSB1_Internal1_port":
260 Assert.assertEquals(port.getNetworkName(), "Internal1-net");
262 case "template_VMInt_OAM_lb_2":
263 Assert.assertEquals(port.getNetworkName(), "jsa_net1");
266 throw new Exception("Unexpected Nic " + port.getName());
270 case "org.openecomp.resource.vfc.nodes.heat.pcm":
271 Assert.assertEquals(component.getNics().size(), 2);
272 Assert.assertEquals(component.getData().getDisplayName(), "pcm");
275 throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
281 public void testExtractSubstitutionComposition() throws Exception {
283 ToscaServiceModel toscaServiceModel =
284 loadToscaServiceModel("/extractServiceComposition/substitution/",
285 "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
286 CompositionData compositionData =
287 compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
288 Assert.assertEquals(compositionData.getComponents().size(), 2);
289 Assert.assertEquals(compositionData.getNetworks().size(), 4);
291 for (Component component : compositionData.getComponents()) {
292 switch (component.getData().getName()) {
293 case "org.openecomp.resource.vfc.nodes.heat.cmaui_image":
294 Assert.assertEquals(component.getNics().size(), 1);
295 Assert.assertEquals(component.getNics().get(0).getName(), "cmaui_port_1");
296 Assert.assertEquals(component.getNics().get(0).getNetworkName(), "test_net1");
297 Assert.assertEquals(component.getData().getDisplayName(), "cmaui_image");
299 case "org.openecomp.resource.vfc.nodes.heat.abc_image":
300 Assert.assertEquals(component.getNics().size(), 1);
301 Assert.assertEquals(component.getNics().get(0).getName(), "abc_port_1");
302 Assert.assertEquals(component.getNics().get(0).getNetworkName(), "test_net2");
303 Assert.assertEquals(component.getData().getDisplayName(), "abc_image");
306 throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
309 for (Network network : compositionData.getNetworks()) {
310 boolean dhcp = network.isDhcp();
311 switch (network.getName()) {
313 Assert.assertEquals(dhcp, true);
316 Assert.assertEquals(dhcp, true);
318 case "Internal1-net": // same network display twice since define in 2 nested files with the same key
319 Assert.assertEquals(dhcp, true);
322 throw new Exception("Unexpected Network Name " + network.getName());