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.Collections;
36 import java.util.HashMap;
37 import java.util.List;
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;
74 @ExtendWith(MockitoExtension.class)
75 public class YamlTemplateParsingHandlerTest {
78 private GroupTypeBusinessLogic groupTypeBusinessLogic;
80 private AnnotationTypeOperations annotationTypeOperations;
82 private AnnotationValidator annotationValidator;
84 private JanusGraphDao janusGraphDao;
88 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
90 private YamlTemplateParsingHandler handler;
92 private static Map<String, byte[]> csar;
93 private static String resourceYml;
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";
117 private YamlTemplateParsingHandler testSubject;
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);
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);
129 resourceYml = new String(mainTemplateService);
133 public void setup() {
135 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
136 annotationValidator);
137 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
138 ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
142 void parseResourceInfoFromOneNodeTest() {
143 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
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);
149 Resource resource = new Resource();
150 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
151 csarInfo.extractTypesInfo(), NODE_NAME, resource, getInterfaceTemplateYaml(csarInfo).get());
153 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
154 Lists.newArrayList("heat_file", "description"));
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);
164 Service service = new Service();
165 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
166 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
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"));
178 void parseResourceInfoFromYAMLTest() {
182 Resource resource = new Resource();
183 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
184 new HashMap<>(), "", resource, null);
185 validateParsedYamlWithCapability(parsedYaml);
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());
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());
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());
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());
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());
255 void parseResourceWithPoliciesDefined() {
258 Resource resource = new Resource();
259 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
260 new HashMap<>(), "", resource, "");
261 validateParsedYamlWithPolicies(parsedYaml);
265 void parseResourceInstanceWithAttributesTest() {
268 Resource resource = new Resource();
269 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
270 new HashMap<>(), "", resource, null);
271 validateParsedYamlWithAttributes(parsedYaml);
274 private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) {
275 ArrayList<String> expectedSubnetsShowList = new ArrayList<>();
276 expectedSubnetsShowList.add("val1");
277 expectedSubnetsShowList.add("val2");
279 HashMap<String, String> expectedSubnetsNameMap = new HashMap<>();
280 expectedSubnetsNameMap.put("name1", "name_val1");
281 expectedSubnetsNameMap.put("name2", "name_val2");
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());
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");
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();
305 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
306 assertThat(parsedYaml.getGroups().get(group).getProperties()
308 .map(PropertyDataDefinition::getName)
309 .collect(Collectors.toList()))
310 .containsAll(expectedProp);
312 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
315 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
317 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
318 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
320 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
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");
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);
335 private static GroupTypeDefinition buildRootGroupType() {
336 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
337 "The TOSCA Group Type all other TOSCA Group Types derive from");
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");
344 GroupProperty property1 = createGroupProperty("heat_file",
345 "Heat file which associate to this group/heat stack", "SUPPORTED");
347 GroupProperty property2 = createGroupProperty("description",
348 "Group description", "SUPPORTED");
350 groupType.setProperties(Lists.newArrayList(property1, property2));
354 private static GroupTypeDefinition buildVfcInstanceGroupType() {
355 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
356 "Groups of VFCs with same parent port role");
358 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
359 "Function of this VFC group", null);
361 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
362 "Common role of parent ports of VFCs in this group", null);
364 GroupProperty property3 = createGroupProperty("network_collection_function",
365 "Network collection function assigned to this group", null);
367 GroupProperty property4 = createGroupProperty("subinterface_role",
368 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
370 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
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));
380 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
381 capabilityMap.put(CAPABILITY_NAME, capability);
382 groupType.setCapabilities(capabilityMap);
386 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description) {
387 GroupTypeDefinition property = new GroupTypeDefinition();
390 property.setType(type);
393 if (derivedFrom != null) {
394 property.setDerivedFrom(derivedFrom);
397 if (description != null) {
398 property.setDescription(description);
404 private static GroupProperty createGroupProperty(String name, String description,
406 GroupProperty property = new GroupProperty();
408 property.setName(name);
411 if (description != null) {
412 property.setDescription(description);
415 if (status != null) {
416 property.setStatus(status);
419 property.setType("string");
420 property.setRequired(true);
425 private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
427 assertThat(parsedYaml.getPolicies()).isNotNull();
428 assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
429 assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
432 private void stubGetPolicyType() {
433 when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any()))
434 .thenReturn(OPENECOMP_POLICY_TYPE);
437 private static PolicyTypeDefinition buildOpenecompPolicyType() {
438 return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
439 "The Openecomp Antilocate policy");
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);
447 if (policyType != null) {
448 policyTypeDefinition.setType(policyType);
450 if (derivedFrom != null) {
451 policyTypeDefinition.setDerivedFrom(derivedFrom);
453 if (description != null) {
454 policyTypeDefinition.setDescription(description);
456 return policyTypeDefinition;
459 private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
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";
469 if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
470 return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
472 return Optional.empty();