Fix changed instance attribute value not visible in generated tosca
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / YamlTemplateParsingHandlerTest.java
1 /*-
2  * ============LICENSE_START===============================================
3  * ONAP SDC
4  * ========================================================================
5  * Modifications Copyright (c) 2019 Samsung
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.be.components.csar;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.when;
30 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
31
32 import java.io.File;
33 import java.net.URISyntaxException;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41 import mockit.Deencapsulation;
42 import org.apache.commons.collections.MapUtils;
43 import org.assertj.core.util.Lists;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.junit.jupiter.api.extension.ExtendWith;
48 import org.mockito.InjectMocks;
49 import org.mockito.Mock;
50 import org.mockito.junit.jupiter.MockitoExtension;
51 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
53 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
54 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
55 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
56 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
57 import org.openecomp.sdc.be.model.CapabilityDefinition;
58 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
59 import org.openecomp.sdc.be.model.GroupProperty;
60 import org.openecomp.sdc.be.model.GroupTypeDefinition;
61 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
62 import org.openecomp.sdc.be.model.PolicyDefinition;
63 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
64 import org.openecomp.sdc.be.model.Resource;
65 import org.openecomp.sdc.be.model.Service;
66 import org.openecomp.sdc.be.model.UploadArtifactInfo;
67 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
68 import org.openecomp.sdc.be.model.User;
69 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
70 import org.openecomp.sdc.common.zip.ZipUtils;
71 import org.openecomp.sdc.common.zip.exception.ZipException;
72 import org.springframework.test.util.ReflectionTestUtils;
73
74 @ExtendWith(MockitoExtension.class)
75 public class YamlTemplateParsingHandlerTest {
76
77     @Mock
78     private GroupTypeBusinessLogic groupTypeBusinessLogic;
79     @Mock
80     private AnnotationTypeOperations annotationTypeOperations;
81     @Mock
82     private AnnotationValidator annotationValidator;
83     @Mock
84     private JanusGraphDao janusGraphDao;
85     @Mock
86     private User user;
87     @Mock
88     private PolicyTypeBusinessLogic policyTypeBusinessLogic;
89
90     private YamlTemplateParsingHandler handler;
91
92     private static Map<String, byte[]> csar;
93     private static String resourceYml;
94
95     private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
96     private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
97     private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
98     private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
99     private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
100     private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
101     private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
102     private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
103     private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
104     private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
105     private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
106     private final static String CAPABILITY_NAME = "vlan_assignment";
107     private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
108     private static final String FILE_NAME = "MainServiceTemplate.yaml";
109     private static final String CSAR_UUID = "csarUUID";
110     private static final String RESOURCE_NAME = "resourceName";
111     private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
112     private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
113     private static final String MAIN_GROUP_NAME = "x_group";
114     private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
115
116     @InjectMocks
117     private YamlTemplateParsingHandler testSubject;
118
119     @BeforeAll
120     public static void prepareData() throws URISyntaxException, ZipException {
121         final File csarFile = new File(
122             YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
123         csar = ZipUtils.readZip(csarFile, false);
124
125         Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
126         byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
127         assertNotNull(mainTemplateService);
128
129         resourceYml = new String(mainTemplateService);
130     }
131
132     @BeforeEach
133     public void setup() {
134
135         AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
136             annotationValidator);
137         handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
138         ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
139     }
140
141     @Test
142     void parseResourceInfoFromOneNodeTest() {
143         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
144
145         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
146         CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
147             MAIN_TEMPLATE_NAME, main_template_content, true);
148
149         Resource resource = new Resource();
150         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
151             csarInfo.extractTypesInfo(), NODE_NAME, resource, getInterfaceTemplateYaml(csarInfo).get());
152
153         validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
154             Lists.newArrayList("heat_file", "description"));
155     }
156
157     @Test
158     void parseServicePropertiesInfoFromYamlTest() {
159         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
160         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
161         CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
162             MAIN_TEMPLATE_NAME, main_template_content, true);
163
164         Service service = new Service();
165         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
166             csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
167
168         assertThat(parsedYaml.getProperties()).isNotNull();
169         assertEquals(5, parsedYaml.getProperties().size());
170         assertTrue(parsedYaml.getProperties().containsKey("skip_post_instantiation_configuration"));
171         assertTrue(parsedYaml.getProperties().containsKey("controller_actor"));
172         assertTrue(parsedYaml.getProperties().containsKey("cds_model_version"));
173         assertTrue(parsedYaml.getProperties().containsKey("cds_model_name"));
174         assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
175     }
176
177     @Test
178     void parseResourceInfoFromYAMLTest() {
179         stubGetGroupType();
180         stubGetPolicyType();
181
182         Resource resource = new Resource();
183         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
184             new HashMap<>(), "", resource, null);
185         validateParsedYamlWithCapability(parsedYaml);
186     }
187
188     @Test
189     void testSetArtifacts() {
190         UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
191         Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
192         Map<String, String> nodeMap = new HashMap<>();
193         nodeMap.put("name", "test_name");
194         nodeMap.put("type", "test_type");
195         nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
196         Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
197         assertNotNull(nodeTemplateInfo.getArtifacts());
198     }
199
200     @Test
201     void testCreateArtifactsModuleFromYaml() {
202         Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
203         Map<String, Map<String, String>> map0 = new HashMap<>();
204         Map<String, String> map1 = new HashMap<>();
205         map1.put("file", "test_file");
206         map1.put("type", "test_type");
207         map0.put("test_art", map1);
208         nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
209         Map<String, Map<String, UploadArtifactInfo>> result;
210         result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
211         assertTrue(MapUtils.isNotEmpty(result));
212         assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
213         assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
214         assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
215     }
216
217     @Test
218     void testAddModuleNodeTemplateArtifacts() {
219         Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
220         Map<String, String> map1 = new HashMap<>();
221         map1.put("file", "test_file");
222         map1.put("type", "test_type");
223         Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
224         assertTrue(MapUtils.isNotEmpty(result));
225         assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
226         assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
227         assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
228     }
229
230     @Test
231     void testBuildModuleNodeTemplateArtifact() {
232         Map<String, String> map1 = new HashMap<>();
233         map1.put("file", "test_file");
234         map1.put("type", "test_type");
235         UploadArtifactInfo result;
236         result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
237         assertNotNull(result);
238         assertEquals("test_file", result.getFile());
239         assertEquals("test_type", result.getType());
240     }
241
242     @Test
243     void testFillArtifact() {
244         Map<String, String> map1 = new HashMap<>();
245         map1.put("file", "test_file");
246         map1.put("type", "test_type");
247         UploadArtifactInfo result = new UploadArtifactInfo();
248         Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
249         assertNotNull(result);
250         assertEquals("test_file", result.getFile());
251         assertEquals("test_type", result.getType());
252     }
253
254     @Test
255     void parseResourceWithPoliciesDefined() {
256         stubGetGroupType();
257         stubGetPolicyType();
258         Resource resource = new Resource();
259         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
260             new HashMap<>(), "", resource, "");
261         validateParsedYamlWithPolicies(parsedYaml);
262     }
263
264     @Test
265     void parseResourceInstanceWithAttributesTest() {
266         stubGetGroupType();
267         stubGetPolicyType();
268         Resource resource = new Resource();
269         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
270                 new HashMap<>(), "", resource, null);
271         validateParsedYamlWithAttributes(parsedYaml);
272     }
273
274     private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) {
275         ArrayList<String> expectedSubnetsShowList = new ArrayList<>();
276         expectedSubnetsShowList.add("val1");
277         expectedSubnetsShowList.add("val2");
278
279         HashMap<String, String> expectedSubnetsNameMap = new HashMap<>();
280         expectedSubnetsNameMap.put("name1", "name_val1");
281         expectedSubnetsNameMap.put("name2", "name_val2");
282
283
284         assertThat(parsedYaml.getInstances().get("resource_instance_with_attributes")).isNotNull();
285         UploadComponentInstanceInfo resourceInstanceWithAttributes = parsedYaml.getInstances().get("resource_instance_with_attributes");
286         assertEquals(5, resourceInstanceWithAttributes.getAttributes().size());
287
288         assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("fq_name"));
289         assertEquals(resourceInstanceWithAttributes.getAttributes().get("fq_name").getValue(), "fq_name_value");
290         assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("tosca_name"));
291         assertEquals(resourceInstanceWithAttributes.getAttributes().get("tosca_name").getValue(), "tosca_name_value");
292         assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_show"));
293         assertEquals(resourceInstanceWithAttributes.getAttributes().get("subnets_show").getValue(), expectedSubnetsShowList);
294         assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_name"));
295         assertEquals(resourceInstanceWithAttributes.getAttributes().get("subnets_name").getValue(), expectedSubnetsNameMap);
296         assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("new_attribute"));
297         assertEquals(resourceInstanceWithAttributes.getAttributes().get("new_attribute").getValue(), "new_attribute_value");
298     }
299
300     private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
301         assertThat(parsedYaml).isNotNull();
302         assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
303         assertThat(parsedYaml.getGroups().get(group)).isNotNull();
304
305         assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
306         assertThat(parsedYaml.getGroups().get(group).getProperties()
307             .stream()
308             .map(PropertyDataDefinition::getName)
309             .collect(Collectors.toList()))
310             .containsAll(expectedProp);
311
312         assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
313     }
314
315     private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
316
317         final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
318             "network_collection_function", "vfc_instance_group_function", "subinterface_role");
319
320         validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
321
322         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
323             .get(CAPABILITY_TYPE)
324             .get(0).getProperties().get(0).getValue()).isEqualTo("success");
325         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
326         assertThat(parsedYaml.getSubstitutionMappingNodeType()).isEqualTo("org.openecomp.resource.abstract.nodes.VF");
327     }
328
329     private void stubGetGroupType() {
330         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE), any())).thenReturn(VfcInstanceGroupType);
331         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
332         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE), any())).thenReturn(rootGroupType);
333     }
334
335     private static GroupTypeDefinition buildRootGroupType() {
336         return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
337             "The TOSCA Group Type all other TOSCA Group Types derive from");
338     }
339
340     private static GroupTypeDefinition buildHeatStackGroupType() {
341         GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
342             "Grouped all heat resources which are in the same heat stack");
343
344         GroupProperty property1 = createGroupProperty("heat_file",
345             "Heat file which associate to this group/heat stack", "SUPPORTED");
346
347         GroupProperty property2 = createGroupProperty("description",
348             "Group description", "SUPPORTED");
349
350         groupType.setProperties(Lists.newArrayList(property1, property2));
351         return groupType;
352     }
353
354     private static GroupTypeDefinition buildVfcInstanceGroupType() {
355         GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
356             "Groups of VFCs with same parent port role");
357
358         GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
359             "Function of this VFC group", null);
360
361         GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
362             "Common role of parent ports of VFCs in this group", null);
363
364         GroupProperty property3 = createGroupProperty("network_collection_function",
365             "Network collection function assigned to this group", null);
366
367         GroupProperty property4 = createGroupProperty("subinterface_role",
368             "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
369
370         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
371
372         CapabilityDefinition capability = new CapabilityDefinition();
373         capability.setType(CAPABILITY_TYPE);
374         capability.setName(CAPABILITY_NAME);
375         ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
376         capabilityProperty.setName("vfc_instance_group_reference");
377         capabilityProperty.setType("string");
378         capability.setProperties(Collections.singletonList(capabilityProperty));
379
380         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
381         capabilityMap.put(CAPABILITY_NAME, capability);
382         groupType.setCapabilities(capabilityMap);
383         return groupType;
384     }
385
386     private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description) {
387         GroupTypeDefinition property = new GroupTypeDefinition();
388
389         if (type != null) {
390             property.setType(type);
391         }
392
393         if (derivedFrom != null) {
394             property.setDerivedFrom(derivedFrom);
395         }
396
397         if (description != null) {
398             property.setDescription(description);
399         }
400
401         return property;
402     }
403
404     private static GroupProperty createGroupProperty(String name, String description,
405                                                      String status) {
406         GroupProperty property = new GroupProperty();
407         if (name != null) {
408             property.setName(name);
409         }
410
411         if (description != null) {
412             property.setDescription(description);
413         }
414
415         if (status != null) {
416             property.setStatus(status);
417         }
418
419         property.setType("string");
420         property.setRequired(true);
421
422         return property;
423     }
424
425     private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
426         // validate  policies
427         assertThat(parsedYaml.getPolicies()).isNotNull();
428         assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
429         assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
430     }
431
432     private void stubGetPolicyType() {
433         when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any()))
434             .thenReturn(OPENECOMP_POLICY_TYPE);
435     }
436
437     private static PolicyTypeDefinition buildOpenecompPolicyType() {
438         return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
439             "The Openecomp Antilocate policy");
440     }
441
442     private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
443         PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
444         if (policyName != null && !policyName.isEmpty()) {
445             policyTypeDefinition.setName(policyName);
446         }
447         if (policyType != null) {
448             policyTypeDefinition.setType(policyType);
449         }
450         if (derivedFrom != null) {
451             policyTypeDefinition.setDerivedFrom(derivedFrom);
452         }
453         if (description != null) {
454             policyTypeDefinition.setDescription(description);
455         }
456         return policyTypeDefinition;
457     }
458
459     private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
460         String[] yamlFile;
461         String interfaceTemplateYaml = "";
462         if (csarInfo.getMainTemplateName().contains(".yml")) {
463             yamlFile = csarInfo.getMainTemplateName().split(".yml");
464             interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
465         } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
466             yamlFile = csarInfo.getMainTemplateName().split(".yaml");
467             interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
468         }
469         if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
470             return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
471         }
472         return Optional.empty();
473     }
474 }