2f85f30dc53a5c9e93fa2e2f36f9a107d0b0a7ce
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.composition;
22
23 import org.apache.commons.io.FileUtils;
24 import org.mockito.InjectMocks;
25 import org.mockito.MockitoAnnotations;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
29 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
30 import org.openecomp.sdc.tosca.services.ToscaUtil;
31 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
36 import org.testng.Assert;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.net.URL;
46 import java.nio.file.NotDirectoryException;
47 import java.util.Collection;
48 import java.util.HashMap;
49 import java.util.Map;
50
51 /**
52  * @author shiria
53  * @since July 17, 2016.
54  */
55
56 public class CompositionDataExtractorImplTest {
57
58   private static final Logger log = (Logger) LoggerFactory.getLogger
59       (CompositionDataExtractorImplTest.class.getName());
60
61   @InjectMocks
62   private static CompositionDataExtractorImpl compositionDataExtractor;
63
64   @BeforeMethod
65   public void setUp() throws Exception {
66     MockitoAnnotations.initMocks(this);
67   }
68
69   private static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
70                                                          String globalServiceTemplatesPath,
71                                                          String entryDefinitionServiceTemplate)
72       throws IOException {
73     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
74     Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
75     if (entryDefinitionServiceTemplate == null) {
76       entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
77     }
78
79     loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
80     if (globalServiceTemplatesPath != null) {
81       loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
82     }
83
84     return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
85   }
86
87   private static void loadServiceTemplates(String serviceTemplatesPath,
88                                            ToscaExtensionYamlUtil toscaExtensionYamlUtil,
89                                            Map<String, ServiceTemplate> serviceTemplates)
90       throws IOException {
91     URL urlFile = CompositionDataExtractorImplTest.class.getResource(serviceTemplatesPath);
92     if (urlFile != null) {
93       File pathFile = new File(urlFile.getFile());
94       Collection<File> files = FileUtils.listFiles(pathFile, null, true);
95       if (files != null) {
96         addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
97       } else {
98         throw new NotDirectoryException(serviceTemplatesPath);
99       }
100     } else {
101       throw new NotDirectoryException(serviceTemplatesPath);
102     }
103   }
104
105   private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
106                                               Collection<File> files,
107                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil)
108       throws IOException {
109     for (File file : files) {
110       try (InputStream yamlFile = new FileInputStream(file)) {
111         ServiceTemplate serviceTemplateFromYaml =
112             toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
113         serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
114         try {
115           yamlFile.close();
116         } catch (IOException ignore) {
117           log.debug("",ignore);
118         }
119       } catch (FileNotFoundException exception) {
120         throw exception;
121       } catch (IOException exception) {
122         throw exception;
123       }
124     }
125   }
126
127   @Test
128   public void testExtractNetworks() throws Exception {
129     ToscaServiceModel toscaServiceModel =
130         loadToscaServiceModel("/extractServiceComposition/networks/",
131             "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
132     CompositionData compositionData =
133         compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
134     Assert.assertEquals(compositionData.getComponents().size(), 0);
135     Assert.assertEquals(compositionData.getNetworks().size(), 7);
136
137     for (Network network : compositionData.getNetworks()) {
138       boolean dhcp = network.isDhcp();
139       switch (network.getName()) {
140         case "contail-net-default-true-dhcp":
141           Assert.assertEquals(dhcp, true);
142           break;
143         case "contail-net-dhcp-false-param":
144           Assert.assertEquals(dhcp, false);
145           break;
146         case "contail-net-dhcp-false":
147           Assert.assertEquals(dhcp, false);
148           break;
149         case "contail-net-dhcp-true-param":
150           Assert.assertEquals(dhcp, true);
151           break;
152         case "contail-net-dhcp-true":
153           Assert.assertEquals(dhcp, true);
154           break;
155         case "contail-net-dhcp-default-true-param":
156           Assert.assertEquals(dhcp, true);
157           break;
158         case "neutron-net-default-dhcp":
159           Assert.assertEquals(dhcp, true);
160           break;
161         default:
162           throw new Exception("Unexpected Network Name " + network.getName());
163       }
164
165     }
166   }
167
168   @Test
169   public void testExtractOnlyComponents() throws Exception {
170     ToscaServiceModel toscaServiceModel =
171         loadToscaServiceModel("/extractServiceComposition/onlyComponents/",
172             "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
173     CompositionData compositionData =
174         compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
175     Assert.assertEquals(compositionData.getComponents().size(), 3);
176     Assert.assertEquals(compositionData.getNetworks().size(), 0);
177
178     for (Component component : compositionData.getComponents()) {
179       switch (component.getData().getName()) {
180         case "org.openecomp.resource.vfc.nodes.heat.pcrf_psm":
181           Assert.assertNull(component.getNics());
182           Assert.assertEquals(component.getData().getDisplayName(), "pcrf_psm");
183           break;
184         case "org.openecomp.resource.vfc.nodes.heat.nova.Server":
185           Assert.assertNull(component.getNics());
186           Assert.assertEquals(component.getData().getDisplayName(), "Server");
187           break;
188         case "org.openecomp.resource.vfc.nodes.heat.pcm":
189           Assert.assertNull(component.getNics());
190           Assert.assertEquals(component.getData().getDisplayName(), "pcm");
191           break;
192         default:
193           throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
194       }
195     }
196   }
197
198   @Test
199   public void testExtractComponentsWithPorts() throws Exception {
200
201     ToscaServiceModel toscaServiceModel =
202         loadToscaServiceModel("/extractServiceComposition/componentsWithPort/",
203             "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
204     CompositionData compositionData =
205         compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
206
207     Assert.assertEquals(compositionData.getComponents().size(), 3);
208     Assert.assertEquals(compositionData.getNetworks().size(), 0);
209
210     for (Component component : compositionData.getComponents()) {
211       switch (component.getData().getName()) {
212         case "org.openecomp.resource.vfc.nodes.heat.pcrf_psm":
213           Assert.assertEquals(component.getNics().size(), 1);
214           Assert.assertEquals(component.getNics().get(0).getName(), "psm01_port_0");
215           Assert.assertNull(component.getNics().get(0).getNetworkName());
216           Assert.assertEquals(component.getData().getDisplayName(), "pcrf_psm");
217           break;
218         case "org.openecomp.resource.vfc.nodes.heat.nova.Server":
219           Assert.assertEquals(component.getNics().size(), 2);
220           Assert.assertEquals(component.getNics().get(0).getName(), "template_VMInt_OAM_lb_2");
221           Assert.assertNull(component.getNics().get(0).getNetworkName());
222           Assert.assertEquals(component.getNics().get(1).getName(), "FSB1_Internal2");
223           Assert.assertNull(component.getNics().get(1).getNetworkName());
224           Assert.assertEquals(component.getData().getDisplayName(), "Server");
225           break;
226         case "org.openecomp.resource.vfc.nodes.heat.pcm":
227           Assert.assertEquals(component.getNics().size(), 2);
228           Assert.assertEquals(component.getData().getDisplayName(), "pcm");
229           break;
230         default:
231           throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
232       }
233     }
234   }
235
236   @Test
237   public void testExtractFullComposition() throws Exception {
238
239     ToscaServiceModel toscaServiceModel =
240         loadToscaServiceModel("/extractServiceComposition/fullComposition/",
241             "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
242     CompositionData compositionData =
243         compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
244     Assert.assertEquals(compositionData.getComponents().size(), 3);
245     Assert.assertEquals(compositionData.getNetworks().size(), 4);
246
247     for (Component component : compositionData.getComponents()) {
248       switch (component.getData().getName()) {
249         case "org.openecomp.resource.vfc.nodes.heat.pcrf_psm":
250           Assert.assertEquals(component.getNics().size(), 1);
251           Assert.assertEquals(component.getNics().get(0).getName(), "psm01_port_0");
252           Assert.assertNull(component.getNics().get(0).getNetworkName());
253           Assert.assertEquals(component.getData().getDisplayName(), "pcrf_psm");
254           break;
255         case "org.openecomp.resource.vfc.nodes.heat.nova.Server":
256           Assert.assertEquals(component.getNics().size(), 4);
257           Assert.assertEquals(component.getData().getDisplayName(), "Server");
258           for (Nic port : component.getNics()) {
259             switch (port.getName()) {
260               case "FSB1_Internal2_port":
261                 Assert.assertEquals(port.getNetworkName(), "Internal2-net");
262                 break;
263               case "FSB1_OAM_Port":
264                 Assert.assertNull(port.getNetworkName());
265                 break;
266               case "FSB1_Internal1_port":
267                 Assert.assertEquals(port.getNetworkName(), "Internal1-net");
268                 break;
269               case "template_VMInt_OAM_lb_2":
270                 Assert.assertEquals(port.getNetworkName(), "jsa_net1");
271                 break;
272               default:
273                 throw new Exception("Unexpected Nic " + port.getName());
274             }
275           }
276           break;
277         case "org.openecomp.resource.vfc.nodes.heat.pcm":
278           Assert.assertEquals(component.getNics().size(), 2);
279           Assert.assertEquals(component.getData().getDisplayName(), "pcm");
280           break;
281         default:
282           throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
283       }
284     }
285   }
286
287   @Test
288   public void testExtractSubstitutionComposition() throws Exception {
289
290     ToscaServiceModel toscaServiceModel =
291         loadToscaServiceModel("/extractServiceComposition/substitution/",
292             "/extractServiceComposition/toscaGlobalServiceTemplates/", null);
293     CompositionData compositionData =
294         compositionDataExtractor.extractServiceCompositionData(toscaServiceModel);
295     Assert.assertEquals(compositionData.getComponents().size(), 2);
296     Assert.assertEquals(compositionData.getNetworks().size(), 4);
297
298     for (Component component : compositionData.getComponents()) {
299       switch (component.getData().getName()) {
300         case "org.openecomp.resource.vfc.nodes.heat.cmaui_image":
301           Assert.assertEquals(component.getNics().size(), 1);
302           Assert.assertEquals(component.getNics().get(0).getName(), "cmaui_port_1");
303           Assert.assertEquals(component.getNics().get(0).getNetworkName(), "test_net1");
304           Assert.assertEquals(component.getData().getDisplayName(), "cmaui_image");
305           break;
306         case "org.openecomp.resource.vfc.nodes.heat.abc_image":
307           Assert.assertEquals(component.getNics().size(), 1);
308           Assert.assertEquals(component.getNics().get(0).getName(), "abc_port_1");
309           Assert.assertEquals(component.getNics().get(0).getNetworkName(), "test_net2");
310           Assert.assertEquals(component.getData().getDisplayName(), "abc_image");
311           break;
312         default:
313           throw new Exception("Unexpected ComponentData Name " + component.getData().getName());
314       }
315     }
316     for (Network network : compositionData.getNetworks()) {
317       boolean dhcp = network.isDhcp();
318       switch (network.getName()) {
319         case "test_net2":
320           Assert.assertEquals(dhcp, true);
321           break;
322         case "test_net1":
323           Assert.assertEquals(dhcp, true);
324           break;
325         case "Internal1-net": // same network display twice since define in 2 nested files with the same key
326           Assert.assertEquals(dhcp, true);
327           break;
328         default:
329           throw new Exception("Unexpected Network Name " + network.getName());
330       }
331
332     }
333   }
334 }