2 * ============LICENSE_START===============================================
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
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.be.components.csar;
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;
33 import java.net.URISyntaxException;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
42 import mockit.Deencapsulation;
43 import org.apache.commons.collections.MapUtils;
44 import org.apache.commons.lang3.StringUtils;
45 import org.assertj.core.util.Lists;
46 import org.junit.jupiter.api.BeforeAll;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.junit.jupiter.api.extension.ExtendWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.junit.jupiter.MockitoExtension;
53 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
54 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
55 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
56 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
57 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
58 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
59 import org.openecomp.sdc.be.model.CapabilityDefinition;
60 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
61 import org.openecomp.sdc.be.model.GroupProperty;
62 import org.openecomp.sdc.be.model.GroupTypeDefinition;
63 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
64 import org.openecomp.sdc.be.model.PolicyDefinition;
65 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
66 import org.openecomp.sdc.be.model.Resource;
67 import org.openecomp.sdc.be.model.Service;
68 import org.openecomp.sdc.be.model.UploadArtifactInfo;
69 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
70 import org.openecomp.sdc.be.model.UploadReqInfo;
71 import org.openecomp.sdc.be.model.User;
72 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
73 import org.openecomp.sdc.be.ui.model.OperationUi;
74 import org.openecomp.sdc.common.zip.ZipUtils;
75 import org.openecomp.sdc.common.zip.exception.ZipException;
76 import org.springframework.test.util.ReflectionTestUtils;
78 @ExtendWith(MockitoExtension.class)
79 public class YamlTemplateParsingHandlerTest {
82 private GroupTypeBusinessLogic groupTypeBusinessLogic;
84 private AnnotationTypeOperations annotationTypeOperations;
86 private AnnotationValidator annotationValidator;
88 private JanusGraphDao janusGraphDao;
92 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
94 private YamlTemplateParsingHandler handler;
96 private static Map<String, byte[]> csar;
97 private static String resourceYml;
99 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
100 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
101 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
102 private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
103 private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
104 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
105 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
106 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
107 private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
108 private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
109 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
110 private final static String CAPABILITY_NAME = "vlan_assignment";
111 private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
112 private static final String FILE_NAME = "MainServiceTemplate.yaml";
113 private static final String CSAR_UUID = "csarUUID";
114 private static final String RESOURCE_NAME = "resourceName";
115 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
116 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
117 private static final String MAIN_GROUP_NAME = "x_group";
118 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
121 private YamlTemplateParsingHandler testSubject;
124 public static void prepareData() throws URISyntaxException, ZipException {
125 final File csarFile = new File(
126 YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
127 csar = ZipUtils.readZip(csarFile, false);
129 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
130 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
131 assertNotNull(mainTemplateService);
133 resourceYml = new String(mainTemplateService);
137 public void setup() {
139 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
140 annotationValidator);
141 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
142 ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
146 void parseResourceInfoFromOneNodeTest() {
147 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
149 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
150 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
151 MAIN_TEMPLATE_NAME, main_template_content, true);
153 Resource resource = new Resource();
154 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
155 csarInfo.extractTypesInfo(), NODE_NAME, resource, getInterfaceTemplateYaml(csarInfo).get());
157 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
158 Lists.newArrayList("heat_file", "description"));
162 void parseServicePropertiesInfoFromYamlTest() {
163 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
164 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
165 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
166 MAIN_TEMPLATE_NAME, main_template_content, true);
168 Service service = new Service();
169 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
170 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
172 assertThat(parsedYaml.getProperties()).isNotNull();
173 assertEquals(5, parsedYaml.getProperties().size());
174 assertTrue(parsedYaml.getProperties().containsKey("skip_post_instantiation_configuration"));
175 assertTrue(parsedYaml.getProperties().containsKey("controller_actor"));
176 assertTrue(parsedYaml.getProperties().containsKey("cds_model_version"));
177 assertTrue(parsedYaml.getProperties().containsKey("cds_model_name"));
178 assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
182 void parseRelationshipTemplateInfoFromYamlTest() {
183 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
184 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
185 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
186 MAIN_TEMPLATE_NAME, main_template_content, true);
188 Service service = new Service();
189 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
190 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
192 assertThat(parsedYaml.getInstances()).isNotNull();
193 final Map<String, List<OperationUi>> operations = new HashMap<>();
194 for (UploadComponentInstanceInfo instance : parsedYaml.getInstances().values()) {
195 final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
196 if (MapUtils.isNotEmpty(requirements)) {
197 requirements.values()
198 .forEach(requirementInfoList -> requirementInfoList.stream()
199 .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
200 .forEach(requirement -> operations.putAll(instance.getOperations())));
203 assertEquals(1, operations.size());
207 void parseResourceInfoFromYAMLTest() {
211 Resource resource = new Resource();
212 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
213 new HashMap<>(), "", resource, null);
214 validateParsedYamlWithCapability(parsedYaml);
218 void testSetArtifacts() {
219 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
220 Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
221 Map<String, String> nodeMap = new HashMap<>();
222 nodeMap.put("name", "test_name");
223 nodeMap.put("type", "test_type");
224 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
225 Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
226 assertNotNull(nodeTemplateInfo.getArtifacts());
230 void testCreateArtifactsModuleFromYaml() {
231 Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
232 Map<String, Map<String, String>> map0 = new HashMap<>();
233 Map<String, String> map1 = new HashMap<>();
234 map1.put("file", "test_file");
235 map1.put("type", "test_type");
236 map0.put("test_art", map1);
237 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
238 Map<String, Map<String, UploadArtifactInfo>> result;
239 result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
240 assertTrue(MapUtils.isNotEmpty(result));
241 assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
242 assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
243 assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
247 void testAddModuleNodeTemplateArtifacts() {
248 Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
249 Map<String, String> map1 = new HashMap<>();
250 map1.put("file", "test_file");
251 map1.put("type", "test_type");
252 Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
253 assertTrue(MapUtils.isNotEmpty(result));
254 assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
255 assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
256 assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
260 void testBuildModuleNodeTemplateArtifact() {
261 Map<String, String> map1 = new HashMap<>();
262 map1.put("file", "test_file");
263 map1.put("type", "test_type");
264 UploadArtifactInfo result;
265 result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
266 assertNotNull(result);
267 assertEquals("test_file", result.getFile());
268 assertEquals("test_type", result.getType());
272 void testFillArtifact() {
273 Map<String, String> map1 = new HashMap<>();
274 map1.put("file", "test_file");
275 map1.put("type", "test_type");
276 UploadArtifactInfo result = new UploadArtifactInfo();
277 Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
278 assertNotNull(result);
279 assertEquals("test_file", result.getFile());
280 assertEquals("test_type", result.getType());
284 void parseResourceWithPoliciesDefined() {
287 Resource resource = new Resource();
288 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
289 new HashMap<>(), "", resource, "");
290 validateParsedYamlWithPolicies(parsedYaml);
294 void parseResourceInstanceWithAttributesTest() {
297 Resource resource = new Resource();
298 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
299 new HashMap<>(), "", resource, null);
300 validateParsedYamlWithAttributes(parsedYaml);
303 private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) {
304 ArrayList<String> expectedSubnetsShowList = new ArrayList<>();
305 expectedSubnetsShowList.add("val1");
306 expectedSubnetsShowList.add("val2");
308 HashMap<String, String> expectedSubnetsNameMap = new HashMap<>();
309 expectedSubnetsNameMap.put("name1", "name_val1");
310 expectedSubnetsNameMap.put("name2", "name_val2");
313 assertThat(parsedYaml.getInstances().get("resource_instance_with_attributes")).isNotNull();
314 UploadComponentInstanceInfo resourceInstanceWithAttributes = parsedYaml.getInstances().get("resource_instance_with_attributes");
315 assertEquals(5, resourceInstanceWithAttributes.getAttributes().size());
317 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("fq_name"));
318 assertEquals(resourceInstanceWithAttributes.getAttributes().get("fq_name").getValue(), "fq_name_value");
319 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("tosca_name"));
320 assertEquals(resourceInstanceWithAttributes.getAttributes().get("tosca_name").getValue(), "tosca_name_value");
321 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_show"));
322 assertEquals(resourceInstanceWithAttributes.getAttributes().get("subnets_show").getValue(), expectedSubnetsShowList);
323 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_name"));
324 assertEquals(resourceInstanceWithAttributes.getAttributes().get("subnets_name").getValue(), expectedSubnetsNameMap);
325 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("new_attribute"));
326 assertEquals(resourceInstanceWithAttributes.getAttributes().get("new_attribute").getValue(), "new_attribute_value");
329 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
330 assertThat(parsedYaml).isNotNull();
331 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
332 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
334 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
335 assertThat(parsedYaml.getGroups().get(group).getProperties()
337 .map(PropertyDataDefinition::getName)
338 .collect(Collectors.toList()))
339 .containsAll(expectedProp);
341 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
344 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
346 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
347 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
349 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
351 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
352 .get(CAPABILITY_TYPE)
353 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
354 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
355 assertThat(parsedYaml.getSubstitutionMappingNodeType()).isEqualTo("org.openecomp.resource.abstract.nodes.VF");
358 private void stubGetGroupType() {
359 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE), any())).thenReturn(VfcInstanceGroupType);
360 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
361 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE), any())).thenReturn(rootGroupType);
364 private static GroupTypeDefinition buildRootGroupType() {
365 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
366 "The TOSCA Group Type all other TOSCA Group Types derive from");
369 private static GroupTypeDefinition buildHeatStackGroupType() {
370 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
371 "Grouped all heat resources which are in the same heat stack");
373 GroupProperty property1 = createGroupProperty("heat_file",
374 "Heat file which associate to this group/heat stack", "SUPPORTED");
376 GroupProperty property2 = createGroupProperty("description",
377 "Group description", "SUPPORTED");
379 groupType.setProperties(Lists.newArrayList(property1, property2));
383 private static GroupTypeDefinition buildVfcInstanceGroupType() {
384 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
385 "Groups of VFCs with same parent port role");
387 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
388 "Function of this VFC group", null);
390 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
391 "Common role of parent ports of VFCs in this group", null);
393 GroupProperty property3 = createGroupProperty("network_collection_function",
394 "Network collection function assigned to this group", null);
396 GroupProperty property4 = createGroupProperty("subinterface_role",
397 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
399 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
401 CapabilityDefinition capability = new CapabilityDefinition();
402 capability.setType(CAPABILITY_TYPE);
403 capability.setName(CAPABILITY_NAME);
404 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
405 capabilityProperty.setName("vfc_instance_group_reference");
406 capabilityProperty.setType("string");
407 capability.setProperties(Collections.singletonList(capabilityProperty));
409 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
410 capabilityMap.put(CAPABILITY_NAME, capability);
411 groupType.setCapabilities(capabilityMap);
415 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description) {
416 GroupTypeDefinition property = new GroupTypeDefinition();
419 property.setType(type);
422 if (derivedFrom != null) {
423 property.setDerivedFrom(derivedFrom);
426 if (description != null) {
427 property.setDescription(description);
433 private static GroupProperty createGroupProperty(String name, String description,
435 GroupProperty property = new GroupProperty();
437 property.setName(name);
440 if (description != null) {
441 property.setDescription(description);
444 if (status != null) {
445 property.setStatus(status);
448 property.setType("string");
449 property.setRequired(true);
454 private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
456 assertThat(parsedYaml.getPolicies()).isNotNull();
457 assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
458 assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
461 private void stubGetPolicyType() {
462 when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any()))
463 .thenReturn(OPENECOMP_POLICY_TYPE);
466 private static PolicyTypeDefinition buildOpenecompPolicyType() {
467 return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
468 "The Openecomp Antilocate policy");
471 private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
472 PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
473 if (policyName != null && !policyName.isEmpty()) {
474 policyTypeDefinition.setName(policyName);
476 if (policyType != null) {
477 policyTypeDefinition.setType(policyType);
479 if (derivedFrom != null) {
480 policyTypeDefinition.setDerivedFrom(derivedFrom);
482 if (description != null) {
483 policyTypeDefinition.setDescription(description);
485 return policyTypeDefinition;
488 private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
490 String interfaceTemplateYaml = "";
491 if (csarInfo.getMainTemplateName().contains(".yml")) {
492 yamlFile = csarInfo.getMainTemplateName().split(".yml");
493 interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
494 } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
495 yamlFile = csarInfo.getMainTemplateName().split(".yaml");
496 interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
498 if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
499 return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
501 return Optional.empty();