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 ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
96 private YamlTemplateParsingHandler handler;
98 private static Map<String, byte[]> csar;
99 private static String resourceYml;
101 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
102 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
103 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
104 private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
105 private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
106 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
107 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
108 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
109 private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
110 private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
111 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
112 private final static String CAPABILITY_NAME = "vlan_assignment";
113 private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
114 private static final String FILE_NAME = "MainServiceTemplate.yaml";
115 private static final String CSAR_UUID = "csarUUID";
116 private static final String RESOURCE_NAME = "resourceName";
117 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
118 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
119 private static final String MAIN_GROUP_NAME = "x_group";
120 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
123 private YamlTemplateParsingHandler testSubject;
126 public static void prepareData() throws URISyntaxException, ZipException {
127 final File csarFile = new File(
128 YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
129 csar = ZipUtils.readZip(csarFile, false);
131 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
132 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
133 assertNotNull(mainTemplateService);
135 resourceYml = new String(mainTemplateService);
139 public void setup() {
140 final var annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
141 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic,
142 toscaFunctionYamlParsingHandler);
143 ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
147 void parseResourceInfoFromOneNodeTest() {
148 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
150 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
151 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
152 MAIN_TEMPLATE_NAME, main_template_content, true);
154 Resource resource = new Resource();
155 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
156 csarInfo.extractTypesInfo(), NODE_NAME, resource, getInterfaceTemplateYaml(csarInfo).get());
158 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
159 Lists.newArrayList("heat_file", "description"));
163 void parseServicePropertiesInfoFromYamlTest() {
164 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
165 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
166 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
167 MAIN_TEMPLATE_NAME, main_template_content, true);
169 Service service = new Service();
170 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
171 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
173 assertThat(parsedYaml.getProperties()).isNotNull();
174 assertEquals(5, parsedYaml.getProperties().size());
175 assertTrue(parsedYaml.getProperties().containsKey("skip_post_instantiation_configuration"));
176 assertTrue(parsedYaml.getProperties().containsKey("controller_actor"));
177 assertTrue(parsedYaml.getProperties().containsKey("cds_model_version"));
178 assertTrue(parsedYaml.getProperties().containsKey("cds_model_name"));
179 assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
183 void parseRelationshipTemplateInfoFromYamlTest() {
184 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
185 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
186 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
187 MAIN_TEMPLATE_NAME, main_template_content, true);
189 Service service = new Service();
190 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
191 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
193 assertThat(parsedYaml.getInstances()).isNotNull();
194 final Map<String, List<OperationUi>> operations = new HashMap<>();
195 for (UploadComponentInstanceInfo instance : parsedYaml.getInstances().values()) {
196 final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
197 if (MapUtils.isNotEmpty(requirements)) {
198 requirements.values()
199 .forEach(requirementInfoList -> requirementInfoList.stream()
200 .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
201 .forEach(requirement -> operations.putAll(instance.getOperations())));
204 assertEquals(1, operations.size());
208 void parseResourceInfoFromYAMLTest() {
212 Resource resource = new Resource();
213 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
214 new HashMap<>(), "", resource, null);
215 validateParsedYamlWithCapability(parsedYaml);
219 void testSetArtifacts() {
220 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
221 Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
222 Map<String, String> nodeMap = new HashMap<>();
223 nodeMap.put("name", "test_name");
224 nodeMap.put("type", "test_type");
225 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
226 Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
227 assertNotNull(nodeTemplateInfo.getArtifacts());
231 void testCreateArtifactsModuleFromYaml() {
232 Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
233 Map<String, Map<String, String>> map0 = new HashMap<>();
234 Map<String, String> map1 = new HashMap<>();
235 map1.put("file", "test_file");
236 map1.put("type", "test_type");
237 map0.put("test_art", map1);
238 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
239 Map<String, Map<String, UploadArtifactInfo>> result;
240 result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
241 assertTrue(MapUtils.isNotEmpty(result));
242 assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
243 assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
244 assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
248 void testAddModuleNodeTemplateArtifacts() {
249 Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
250 Map<String, String> map1 = new HashMap<>();
251 map1.put("file", "test_file");
252 map1.put("type", "test_type");
253 Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
254 assertTrue(MapUtils.isNotEmpty(result));
255 assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
256 assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
257 assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
261 void testBuildModuleNodeTemplateArtifact() {
262 Map<String, String> map1 = new HashMap<>();
263 map1.put("file", "test_file");
264 map1.put("type", "test_type");
265 UploadArtifactInfo result;
266 result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
267 assertNotNull(result);
268 assertEquals("test_file", result.getFile());
269 assertEquals("test_type", result.getType());
273 void testFillArtifact() {
274 Map<String, String> map1 = new HashMap<>();
275 map1.put("file", "test_file");
276 map1.put("type", "test_type");
277 UploadArtifactInfo result = new UploadArtifactInfo();
278 Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
279 assertNotNull(result);
280 assertEquals("test_file", result.getFile());
281 assertEquals("test_type", result.getType());
285 void parseResourceWithPoliciesDefined() {
288 Resource resource = new Resource();
289 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
290 new HashMap<>(), "", resource, "");
291 validateParsedYamlWithPolicies(parsedYaml);
295 void parseResourceInstanceWithAttributesTest() {
298 Resource resource = new Resource();
299 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
300 new HashMap<>(), "", resource, null);
301 validateParsedYamlWithAttributes(parsedYaml);
304 private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) {
305 ArrayList<String> expectedSubnetsShowList = new ArrayList<>();
306 expectedSubnetsShowList.add("val1");
307 expectedSubnetsShowList.add("val2");
309 HashMap<String, String> expectedSubnetsNameMap = new HashMap<>();
310 expectedSubnetsNameMap.put("name1", "name_val1");
311 expectedSubnetsNameMap.put("name2", "name_val2");
314 assertThat(parsedYaml.getInstances().get("resource_instance_with_attributes")).isNotNull();
315 UploadComponentInstanceInfo resourceInstanceWithAttributes = parsedYaml.getInstances().get("resource_instance_with_attributes");
316 assertEquals(5, resourceInstanceWithAttributes.getAttributes().size());
318 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("fq_name"));
319 assertEquals("fq_name_value", resourceInstanceWithAttributes.getAttributes().get("fq_name").getValue());
320 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("tosca_name"));
321 assertEquals("tosca_name_value", resourceInstanceWithAttributes.getAttributes().get("tosca_name").getValue());
322 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_show"));
323 assertEquals(expectedSubnetsShowList, resourceInstanceWithAttributes.getAttributes().get("subnets_show").getValue());
324 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_name"));
325 assertEquals(expectedSubnetsNameMap, resourceInstanceWithAttributes.getAttributes().get("subnets_name").getValue());
326 assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("new_attribute"));
327 assertEquals("new_attribute_value", resourceInstanceWithAttributes.getAttributes().get("new_attribute").getValue());
330 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
331 assertThat(parsedYaml).isNotNull();
332 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
333 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
335 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
336 assertThat(parsedYaml.getGroups().get(group).getProperties()
338 .map(PropertyDataDefinition::getName)
339 .collect(Collectors.toList()))
340 .containsAll(expectedProp);
342 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
345 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
347 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
348 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
350 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
352 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
353 .get(CAPABILITY_TYPE)
354 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
355 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
356 assertThat(parsedYaml.getSubstitutionMappingNodeType()).isEqualTo("org.openecomp.resource.abstract.nodes.VF");
359 private void stubGetGroupType() {
360 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE), any())).thenReturn(VfcInstanceGroupType);
361 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
362 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE), any())).thenReturn(rootGroupType);
365 private static GroupTypeDefinition buildRootGroupType() {
366 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
367 "The TOSCA Group Type all other TOSCA Group Types derive from");
370 private static GroupTypeDefinition buildHeatStackGroupType() {
371 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
372 "Grouped all heat resources which are in the same heat stack");
374 GroupProperty property1 = createGroupProperty("heat_file",
375 "Heat file which associate to this group/heat stack", "SUPPORTED");
377 GroupProperty property2 = createGroupProperty("description",
378 "Group description", "SUPPORTED");
380 groupType.setProperties(Lists.newArrayList(property1, property2));
384 private static GroupTypeDefinition buildVfcInstanceGroupType() {
385 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
386 "Groups of VFCs with same parent port role");
388 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
389 "Function of this VFC group", null);
391 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
392 "Common role of parent ports of VFCs in this group", null);
394 GroupProperty property3 = createGroupProperty("network_collection_function",
395 "Network collection function assigned to this group", null);
397 GroupProperty property4 = createGroupProperty("subinterface_role",
398 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
400 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
402 CapabilityDefinition capability = new CapabilityDefinition();
403 capability.setType(CAPABILITY_TYPE);
404 capability.setName(CAPABILITY_NAME);
405 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
406 capabilityProperty.setName("vfc_instance_group_reference");
407 capabilityProperty.setType("string");
408 capability.setProperties(Collections.singletonList(capabilityProperty));
410 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
411 capabilityMap.put(CAPABILITY_NAME, capability);
412 groupType.setCapabilities(capabilityMap);
416 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description) {
417 GroupTypeDefinition property = new GroupTypeDefinition();
420 property.setType(type);
423 if (derivedFrom != null) {
424 property.setDerivedFrom(derivedFrom);
427 if (description != null) {
428 property.setDescription(description);
434 private static GroupProperty createGroupProperty(String name, String description,
436 GroupProperty property = new GroupProperty();
438 property.setName(name);
441 if (description != null) {
442 property.setDescription(description);
445 if (status != null) {
446 property.setStatus(status);
449 property.setType("string");
450 property.setRequired(true);
455 private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
457 assertThat(parsedYaml.getPolicies()).isNotNull();
458 assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
459 assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
462 private void stubGetPolicyType() {
463 when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any()))
464 .thenReturn(OPENECOMP_POLICY_TYPE);
467 private static PolicyTypeDefinition buildOpenecompPolicyType() {
468 return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
469 "The Openecomp Antilocate policy");
472 private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
473 PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
474 if (policyName != null && !policyName.isEmpty()) {
475 policyTypeDefinition.setName(policyName);
477 if (policyType != null) {
478 policyTypeDefinition.setType(policyType);
480 if (derivedFrom != null) {
481 policyTypeDefinition.setDerivedFrom(derivedFrom);
483 if (description != null) {
484 policyTypeDefinition.setDescription(description);
486 return policyTypeDefinition;
489 private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
491 String interfaceTemplateYaml = "";
492 if (csarInfo.getMainTemplateName().contains(".yml")) {
493 yamlFile = csarInfo.getMainTemplateName().split(".yml");
494 interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
495 } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
496 yamlFile = csarInfo.getMainTemplateName().split(".yaml");
497 interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
499 if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
500 return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
502 return Optional.empty();