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 mockit.Deencapsulation;
24 import org.apache.commons.collections.MapUtils;
25 import org.apache.commons.lang3.StringUtils;
26 import org.assertj.core.util.Lists;
27 import org.junit.jupiter.api.BeforeAll;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
38 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
39 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
40 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
41 import org.openecomp.sdc.be.model.CapabilityDefinition;
42 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
43 import org.openecomp.sdc.be.model.GroupProperty;
44 import org.openecomp.sdc.be.model.GroupTypeDefinition;
45 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
46 import org.openecomp.sdc.be.model.PolicyDefinition;
47 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
48 import org.openecomp.sdc.be.model.Resource;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.be.model.UploadArtifactInfo;
51 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
52 import org.openecomp.sdc.be.model.UploadReqInfo;
53 import org.openecomp.sdc.be.model.User;
54 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
55 import org.openecomp.sdc.be.ui.model.OperationUi;
56 import org.openecomp.sdc.common.zip.ZipUtils;
57 import org.openecomp.sdc.common.zip.exception.ZipException;
58 import org.springframework.test.util.ReflectionTestUtils;
61 import java.net.URISyntaxException;
62 import java.util.ArrayList;
63 import java.util.Collections;
64 import java.util.HashMap;
65 import java.util.List;
67 import java.util.Optional;
68 import java.util.stream.Collectors;
70 import static org.assertj.core.api.Assertions.assertThat;
71 import static org.junit.jupiter.api.Assertions.assertEquals;
72 import static org.junit.jupiter.api.Assertions.assertNotNull;
73 import static org.junit.jupiter.api.Assertions.assertTrue;
74 import static org.mockito.ArgumentMatchers.any;
75 import static org.mockito.ArgumentMatchers.eq;
76 import static org.mockito.Mockito.when;
77 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
79 @ExtendWith(MockitoExtension.class)
80 public class YamlTemplateParsingHandlerTest {
83 private GroupTypeBusinessLogic groupTypeBusinessLogic;
85 private AnnotationTypeOperations annotationTypeOperations;
87 private AnnotationValidator annotationValidator;
89 private JanusGraphDao janusGraphDao;
93 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
95 private ServiceBusinessLogic serviceBusinessLogic;
97 private ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
99 private YamlTemplateParsingHandler handler;
101 private static Map<String, byte[]> csar;
102 private static String resourceYml;
104 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
105 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
106 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
107 private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
108 private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
109 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
110 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
111 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
112 private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
113 private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
114 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
115 private final static String CAPABILITY_NAME = "vlan_assignment";
116 private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
117 private static final String FILE_NAME = "MainServiceTemplate.yaml";
118 private static final String CSAR_UUID = "csarUUID";
119 private static final String RESOURCE_NAME = "resourceName";
120 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
121 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
122 private static final String MAIN_GROUP_NAME = "x_group";
123 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
126 private YamlTemplateParsingHandler testSubject;
129 public static void prepareData() throws URISyntaxException, ZipException {
130 final File csarFile = new File(
131 YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
132 csar = ZipUtils.readZip(csarFile, false);
134 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
135 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
136 assertNotNull(mainTemplateService);
138 resourceYml = new String(mainTemplateService);
142 public void setup() {
143 final var annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
144 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic,
145 serviceBusinessLogic, toscaFunctionYamlParsingHandler);
146 ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
150 void parseResourceInfoFromOneNodeTest() {
151 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
153 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
154 CsarInfo csarInfo = new OnboardedCsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
155 MAIN_TEMPLATE_NAME, main_template_content, true);
157 Resource resource = new Resource();
158 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
159 csarInfo.extractTypesInfo(), NODE_NAME, resource, getInterfaceTemplateYaml(csarInfo).get());
161 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
162 Lists.newArrayList("heat_file", "description"));
166 void parseServicePropertiesInfoFromYamlTest() {
167 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
168 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
169 CsarInfo csarInfo = new OnboardedCsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
170 MAIN_TEMPLATE_NAME, main_template_content, true);
172 Service service = new Service();
173 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
174 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
176 assertThat(parsedYaml.getProperties()).isNotNull();
177 assertEquals(5, parsedYaml.getProperties().size());
178 assertTrue(parsedYaml.getProperties().containsKey("skip_post_instantiation_configuration"));
179 assertTrue(parsedYaml.getProperties().containsKey("controller_actor"));
180 assertTrue(parsedYaml.getProperties().containsKey("cds_model_version"));
181 assertTrue(parsedYaml.getProperties().containsKey("cds_model_name"));
182 assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
186 void parseRelationshipTemplateInfoFromYamlTest() {
187 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
188 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
189 CsarInfo csarInfo = new OnboardedCsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
190 MAIN_TEMPLATE_NAME, main_template_content, true);
192 Service service = new Service();
193 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
194 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
196 assertThat(parsedYaml.getInstances()).isNotNull();
197 final Map<String, List<OperationUi>> operations = new HashMap<>();
198 for (UploadComponentInstanceInfo instance : parsedYaml.getInstances().values()) {
199 final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
200 if (MapUtils.isNotEmpty(requirements)) {
201 requirements.values()
202 .forEach(requirementInfoList -> requirementInfoList.stream()
203 .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
204 .forEach(requirement -> operations.putAll(instance.getOperations())));
207 assertEquals(1, operations.size());
211 void parseResourceInfoFromYAMLTest() {
215 Resource resource = new Resource();
216 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
217 new HashMap<>(), "", resource, null);
218 validateParsedYamlWithCapability(parsedYaml);
222 void testSetArtifacts() {
223 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
224 Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
225 Map<String, String> nodeMap = new HashMap<>();
226 nodeMap.put("name", "test_name");
227 nodeMap.put("type", "test_type");
228 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
229 Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
230 assertNotNull(nodeTemplateInfo.getArtifacts());
234 void testCreateArtifactsModuleFromYaml() {
235 Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
236 Map<String, Map<String, String>> map0 = new HashMap<>();
237 Map<String, String> map1 = new HashMap<>();
238 map1.put("file", "test_file");
239 map1.put("type", "test_type");
240 map0.put("test_art", map1);
241 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
242 Map<String, Map<String, UploadArtifactInfo>> result;
243 result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
244 assertTrue(MapUtils.isNotEmpty(result));
245 assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
246 assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
247 assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
251 void testAddModuleNodeTemplateArtifacts() {
252 Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
253 Map<String, String> map1 = new HashMap<>();
254 map1.put("file", "test_file");
255 map1.put("type", "test_type");
256 Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
257 assertTrue(MapUtils.isNotEmpty(result));
258 assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
259 assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
260 assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
264 void testBuildModuleNodeTemplateArtifact() {
265 Map<String, String> map1 = new HashMap<>();
266 map1.put("file", "test_file");
267 map1.put("type", "test_type");
268 UploadArtifactInfo result;
269 result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
270 assertNotNull(result);
271 assertEquals("test_file", result.getFile());
272 assertEquals("test_type", result.getType());
276 void testFillArtifact() {
277 Map<String, String> map1 = new HashMap<>();
278 map1.put("file", "test_file");
279 map1.put("type", "test_type");
280 UploadArtifactInfo result = new UploadArtifactInfo();
281 Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
282 assertNotNull(result);
283 assertEquals("test_file", result.getFile());
284 assertEquals("test_type", result.getType());
288 void parseResourceWithPoliciesDefined() {
291 Resource resource = new Resource();
292 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
293 new HashMap<>(), "", resource, "");
294 validateParsedYamlWithPolicies(parsedYaml);
298 void parseResourceInstanceWithAttributesTest() {
301 Resource resource = new Resource();
302 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
303 new HashMap<>(), "", resource, null);
304 validateParsedYamlWithAttributes(parsedYaml);
307 private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) {
308 ArrayList<String> expectedSubnetsShowList = new ArrayList<>();
309 expectedSubnetsShowList.add("val1");
310 expectedSubnetsShowList.add("val2");
312 HashMap<String, String> expectedSubnetsNameMap = new HashMap<>();
313 expectedSubnetsNameMap.put("name1", "name_val1");
314 expectedSubnetsNameMap.put("name2", "name_val2");
317 assertThat(parsedYaml.getInstances().get("resource_instance_with_attributes")).isNotNull();
318 UploadComponentInstanceInfo resourceInstanceWithAttributes = parsedYaml.getInstances().get("resource_instance_with_attributes");
319 assertEquals(5, resourceInstanceWithAttributes.getAttributes().size());
321 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("fq_name"));
322 assertEquals("fq_name_value", resourceInstanceWithAttributes.getAttributes().get("fq_name").getValue());
323 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("tosca_name"));
324 assertEquals("tosca_name_value", resourceInstanceWithAttributes.getAttributes().get("tosca_name").getValue());
325 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_show"));
326 assertEquals(expectedSubnetsShowList, resourceInstanceWithAttributes.getAttributes().get("subnets_show").getValue());
327 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_name"));
328 assertEquals(expectedSubnetsNameMap, resourceInstanceWithAttributes.getAttributes().get("subnets_name").getValue());
329 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("new_attribute"));
330 assertEquals("new_attribute_value", resourceInstanceWithAttributes.getAttributes().get("new_attribute").getValue());
333 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
334 assertThat(parsedYaml).isNotNull();
335 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
336 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
338 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
339 assertThat(parsedYaml.getGroups().get(group).getProperties()
341 .map(PropertyDataDefinition::getName)
342 .collect(Collectors.toList()))
343 .containsAll(expectedProp);
345 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
348 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
350 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
351 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
353 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
355 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
356 .get(CAPABILITY_TYPE)
357 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
358 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
359 assertThat(parsedYaml.getSubstitutionMappingNodeType()).isEqualTo("org.openecomp.resource.abstract.nodes.VF");
362 private void stubGetGroupType() {
363 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE), any())).thenReturn(VfcInstanceGroupType);
364 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
365 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE), any())).thenReturn(rootGroupType);
368 private static GroupTypeDefinition buildRootGroupType() {
369 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
370 "The TOSCA Group Type all other TOSCA Group Types derive from");
373 private static GroupTypeDefinition buildHeatStackGroupType() {
374 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
375 "Grouped all heat resources which are in the same heat stack");
377 GroupProperty property1 = createGroupProperty("heat_file",
378 "Heat file which associate to this group/heat stack", "SUPPORTED");
380 GroupProperty property2 = createGroupProperty("description",
381 "Group description", "SUPPORTED");
383 groupType.setProperties(Lists.newArrayList(property1, property2));
387 private static GroupTypeDefinition buildVfcInstanceGroupType() {
388 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
389 "Groups of VFCs with same parent port role");
391 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
392 "Function of this VFC group", null);
394 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
395 "Common role of parent ports of VFCs in this group", null);
397 GroupProperty property3 = createGroupProperty("network_collection_function",
398 "Network collection function assigned to this group", null);
400 GroupProperty property4 = createGroupProperty("subinterface_role",
401 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
403 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
405 CapabilityDefinition capability = new CapabilityDefinition();
406 capability.setType(CAPABILITY_TYPE);
407 capability.setName(CAPABILITY_NAME);
408 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
409 capabilityProperty.setName("vfc_instance_group_reference");
410 capabilityProperty.setType("string");
411 capability.setProperties(Collections.singletonList(capabilityProperty));
413 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
414 capabilityMap.put(CAPABILITY_NAME, capability);
415 groupType.setCapabilities(capabilityMap);
419 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description) {
420 GroupTypeDefinition property = new GroupTypeDefinition();
423 property.setType(type);
426 if (derivedFrom != null) {
427 property.setDerivedFrom(derivedFrom);
430 if (description != null) {
431 property.setDescription(description);
437 private static GroupProperty createGroupProperty(String name, String description,
439 GroupProperty property = new GroupProperty();
441 property.setName(name);
444 if (description != null) {
445 property.setDescription(description);
448 if (status != null) {
449 property.setStatus(status);
452 property.setType("string");
453 property.setRequired(true);
458 private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
460 assertThat(parsedYaml.getPolicies()).isNotNull();
461 assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
462 assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
465 private void stubGetPolicyType() {
466 when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any()))
467 .thenReturn(OPENECOMP_POLICY_TYPE);
470 private static PolicyTypeDefinition buildOpenecompPolicyType() {
471 return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
472 "The Openecomp Antilocate policy");
475 private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
476 PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
477 if (policyName != null && !policyName.isEmpty()) {
478 policyTypeDefinition.setName(policyName);
480 if (policyType != null) {
481 policyTypeDefinition.setType(policyType);
483 if (derivedFrom != null) {
484 policyTypeDefinition.setDerivedFrom(derivedFrom);
486 if (description != null) {
487 policyTypeDefinition.setDescription(description);
489 return policyTypeDefinition;
492 private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
494 String interfaceTemplateYaml = "";
495 if (csarInfo.getMainTemplateName().contains(".yml")) {
496 yamlFile = csarInfo.getMainTemplateName().split(".yml");
497 interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
498 } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
499 yamlFile = csarInfo.getMainTemplateName().split(".yaml");
500 interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
502 if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
503 return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
505 return Optional.empty();