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.impl.utils;
23 import mockit.Deencapsulation;
24 import org.apache.commons.collections.MapUtils;
25 import org.assertj.core.util.Lists;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.openecomp.sdc.be.components.csar.CsarInfo;
35 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
36 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
39 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
40 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
41 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
42 import org.openecomp.sdc.be.model.CapabilityDefinition;
43 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
44 import org.openecomp.sdc.be.model.GroupProperty;
45 import org.openecomp.sdc.be.model.GroupTypeDefinition;
46 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
47 import org.openecomp.sdc.be.model.PolicyDefinition;
48 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
49 import org.openecomp.sdc.be.model.Resource;
50 import org.openecomp.sdc.be.model.Service;
51 import org.openecomp.sdc.be.model.UploadArtifactInfo;
52 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
53 import org.openecomp.sdc.be.model.User;
54 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
55 import org.openecomp.sdc.common.zip.ZipUtils;
56 import org.openecomp.sdc.common.zip.exception.ZipException;
59 import java.net.URISyntaxException;
60 import java.util.Collections;
61 import java.util.HashMap;
62 import java.util.List;
64 import java.util.Optional;
65 import java.util.stream.Collectors;
66 import org.springframework.test.util.ReflectionTestUtils;
68 import static org.assertj.core.api.Assertions.assertThat;
69 import static org.junit.Assert.assertEquals;
70 import static org.junit.Assert.assertNotNull;
71 import static org.junit.Assert.assertTrue;
72 import static org.mockito.ArgumentMatchers.any;
73 import static org.mockito.ArgumentMatchers.eq;
74 import static org.mockito.Mockito.when;
75 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
77 @RunWith(MockitoJUnitRunner.class)
78 public class YamlTemplateParsingHandlerTest {
81 private GroupTypeBusinessLogic groupTypeBusinessLogic;
83 private AnnotationTypeOperations annotationTypeOperations;
85 private AnnotationValidator annotationValidator;
87 private JanusGraphDao janusGraphDao;
91 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
93 private YamlTemplateParsingHandler handler;
95 private static Map<String, byte[]> csar;
96 private static String resourceYml;
98 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
99 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
100 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
101 private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
102 private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
103 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
104 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
105 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
106 private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
107 private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
108 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
109 private final static String CAPABILITY_NAME = "vlan_assignment";
110 private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
111 private static final String FILE_NAME = "MainServiceTemplate.yaml";
112 private static final String CSAR_UUID = "csarUUID";
113 private static final String RESOURCE_NAME = "resourceName";
114 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
115 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
116 private static final String MAIN_GROUP_NAME = "x_group";
117 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
120 YamlTemplateParsingHandler testSubject;
123 public static void prepareData() throws URISyntaxException, ZipException {
124 final File csarFile = new File(
125 YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
126 csar = ZipUtils.readZip(csarFile, false);
128 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
129 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
130 assertNotNull(mainTemplateService);
132 resourceYml = new String(mainTemplateService);
136 public void setup() {
138 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
139 annotationValidator);
140 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
141 ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
147 public void parseResourceInfoFromOneNodeTest() {
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 public void parseServicePropertiesInfoFromYamlTest() {
163 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
164 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
165 MAIN_TEMPLATE_NAME, main_template_content, true);
167 Service service = new Service();
168 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
169 csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
171 assertThat(parsedYaml.getProperties()).isNotNull();
172 assertEquals(5, parsedYaml.getProperties().size());
173 assertTrue(parsedYaml.getProperties().containsKey("skip_post_instantiation_configuration"));
174 assertTrue(parsedYaml.getProperties().containsKey("controller_actor"));
175 assertTrue(parsedYaml.getProperties().containsKey("cds_model_version"));
176 assertTrue(parsedYaml.getProperties().containsKey("cds_model_name"));
177 assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
181 public void parseResourceInfoFromYAMLTest() {
183 Resource resource = new Resource();
184 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
185 new HashMap<>(), "", resource, null);
186 validateParsedYamlWithCapability(parsedYaml);
190 public void testSetArtifacts() {
191 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
192 Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
193 Map<String, String> nodeMap = new HashMap<>();
194 nodeMap.put("name","test_name");
195 nodeMap.put("type","test_type");
196 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
197 Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
198 assertNotNull(nodeTemplateInfo.getArtifacts());
202 public void testCreateArtifactsModuleFromYaml() {
203 Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
204 Map<String, Map<String,String>> map0 = new HashMap<>();
205 Map<String, String> map1 = new HashMap<>();
206 map1.put("file", "test_file");
207 map1.put("type", "test_type");
208 map0.put("test_art", map1);
209 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
210 Map<String, Map<String, UploadArtifactInfo>> result;
211 result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
212 Assert.assertTrue(MapUtils.isNotEmpty(result));
213 Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
214 Assert.assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
215 Assert.assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
219 public void testAddModuleNodeTemplateArtifacts() {
220 Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
221 Map<String, String> map1 = new HashMap<>();
222 map1.put("file", "test_file");
223 map1.put("type", "test_type");
224 Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
225 Assert.assertTrue(MapUtils.isNotEmpty(result));
226 Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
227 Assert.assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
228 Assert.assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
232 public void testBuildModuleNodeTemplateArtifact() {
233 Map<String, String> map1 = new HashMap<>();
234 map1.put("file", "test_file");
235 map1.put("type", "test_type");
236 UploadArtifactInfo result;
237 result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
238 assertNotNull(result);
239 Assert.assertEquals("test_file", result.getFile());
240 Assert.assertEquals("test_type", result.getType());
244 public void testFillArtifact() {
245 Map<String, String> map1 = new HashMap<>();
246 map1.put("file", "test_file");
247 map1.put("type", "test_type");
248 UploadArtifactInfo result = new UploadArtifactInfo();
249 Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
250 assertNotNull(result);
251 Assert.assertEquals("test_file", result.getFile());
252 Assert.assertEquals("test_type", result.getType());
256 public void parseResourceWithPoliciesDefined() {
257 Resource resource = new Resource();
258 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
259 new HashMap<>(), "", resource, "");
260 validateParsedYamlWithPolicies(parsedYaml);
263 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
264 assertThat(parsedYaml).isNotNull();
265 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
266 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
268 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
269 assertThat(parsedYaml.getGroups().get(group).getProperties()
271 .map(PropertyDataDefinition::getName)
272 .collect(Collectors.toList()))
273 .containsAll(expectedProp);
275 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
278 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
280 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
281 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
283 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
285 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
286 .get(CAPABILITY_TYPE)
287 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
288 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
289 assertThat(parsedYaml.getSubstitutionMappingNodeType()).isEqualTo("org.openecomp.resource.abstract.nodes.VF");
292 private void stubGetGroupType() {
293 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE), any())).thenReturn(VfcInstanceGroupType);
294 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
295 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE), any())).thenReturn(rootGroupType);
298 private static GroupTypeDefinition buildRootGroupType() {
299 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
300 "The TOSCA Group Type all other TOSCA Group Types derive from");
303 private static GroupTypeDefinition buildHeatStackGroupType() {
304 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
305 "Grouped all heat resources which are in the same heat stack");
307 GroupProperty property1 = createGroupProperty("heat_file",
308 "Heat file which associate to this group/heat stack", "SUPPORTED");
310 GroupProperty property2 = createGroupProperty("description",
311 "Group description", "SUPPORTED");
313 groupType.setProperties(Lists.newArrayList(property1, property2));
317 private static GroupTypeDefinition buildVfcInstanceGroupType() {
318 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
319 "Groups of VFCs with same parent port role");
321 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
322 "Function of this VFC group", null);
324 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
325 "Common role of parent ports of VFCs in this group", null);
327 GroupProperty property3 = createGroupProperty("network_collection_function",
328 "Network collection function assigned to this group", null);
330 GroupProperty property4 = createGroupProperty("subinterface_role",
331 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
333 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
335 CapabilityDefinition capability = new CapabilityDefinition();
336 capability.setType(CAPABILITY_TYPE);
337 capability.setName(CAPABILITY_NAME);
338 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
339 capabilityProperty.setName("vfc_instance_group_reference");
340 capabilityProperty.setType("string");
341 capability.setProperties(Collections.singletonList(capabilityProperty));
343 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
344 capabilityMap.put(CAPABILITY_NAME, capability);
345 groupType.setCapabilities(capabilityMap);
349 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
350 GroupTypeDefinition property = new GroupTypeDefinition();
353 property.setType(type);
355 if (derivedFrom != null) {
356 property.setDerivedFrom(derivedFrom);
359 if (description != null) {
360 property.setDescription(description);
365 private static GroupProperty createGroupProperty(String name, String description,
367 GroupProperty property = new GroupProperty();
369 property.setName(name);
371 if (description != null) {
372 property.setDescription(description);
375 if (status != null) {
376 property.setStatus(status);
379 property.setType("string");
380 property.setRequired(true);
385 private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
387 assertThat(parsedYaml.getPolicies()).isNotNull();
388 assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
389 assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
392 private void stubGetPolicyType () {
393 when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any())).thenReturn(
394 OPENECOMP_POLICY_TYPE);
397 private static PolicyTypeDefinition buildOpenecompPolicyType() {
398 return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
399 "The Openecomp Antilocate policy");
402 private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
403 PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
404 if (policyName != null && !policyName.isEmpty()) {
405 policyTypeDefinition.setName(policyName);
407 if (policyType != null) {
408 policyTypeDefinition.setType(policyType);
410 if (derivedFrom != null) {
411 policyTypeDefinition.setDerivedFrom(derivedFrom);
413 if (description != null) {
414 policyTypeDefinition.setDescription(description);
416 return policyTypeDefinition;
419 private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
421 String interfaceTemplateYaml = "";
422 if (csarInfo.getMainTemplateName().contains(".yml")) {
423 yamlFile = csarInfo.getMainTemplateName().split(".yml");
424 interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
425 } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
426 yamlFile = csarInfo.getMainTemplateName().split(".yaml");
427 interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
429 if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
430 return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
432 return Optional.empty();