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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
28 import java.io.IOException;
29 import java.net.URISyntaxException;
31 import java.util.stream.Collectors;
33 import org.assertj.core.util.Lists;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
41 import org.openecomp.sdc.ZipUtil;
42 import org.openecomp.sdc.be.components.csar.CsarInfo;
43 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
44 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
46 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
47 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
48 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
49 import org.openecomp.sdc.be.model.*;
50 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
52 @RunWith(MockitoJUnitRunner.class)
53 public class YamlTemplateParsingHandlerTest {
56 private GroupTypeBusinessLogic groupTypeBusinessLogic;
58 private AnnotationTypeOperations annotationTypeOperations;
60 private AnnotationValidator annotationValidator;
62 private JanusGraphDao janusGraphDao;
66 private YamlTemplateParsingHandler handler;
68 private static Map<String, byte[]> csar;
69 private static String resourceYml;
71 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
72 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
73 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
74 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
75 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
76 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
77 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
78 private final static String CAPABILITY_NAME = "vlan_assignment";
79 private static final String CSAR_FILE_PATH = "/csars/with_groups.csar";
81 private static final String FILE_NAME = "MainServiceTemplate.yaml";
83 private static final String CSAR_UUID = "csarUUID";
84 private static final String RESOURCE_NAME = "resourceName";
85 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
86 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
87 private static final String MAIN_GROUP_NAME = "x_group";
88 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
91 public static void prepareData() throws IOException, URISyntaxException {
92 csar = ZipUtil.readData(CSAR_FILE_PATH);
94 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
95 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
96 assertNotNull(mainTemplateService);
98 resourceYml = new String(mainTemplateService);
102 public void setup() {
104 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
105 annotationValidator);
106 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
111 public void parseResourceInfoFromOneNodeTest() {
113 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
114 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
115 MAIN_TEMPLATE_NAME, main_template_content, true);
117 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
118 csarInfo.extractNodeTypesInfo(), NODE_NAME);
120 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
121 Lists.newArrayList("heat_file", "description"));
125 public void parseResourceInfoFromYAMLTest() {
127 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
128 new HashMap<>(), "");
129 validateParsedYamlWithCapability(parsedYaml);
132 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
133 assertThat(parsedYaml).isNotNull();
134 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
135 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
137 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
138 assertThat(parsedYaml.getGroups().get(group).getProperties()
140 .map(PropertyDataDefinition::getName)
141 .collect(Collectors.toList()))
142 .containsAll(expectedProp);
144 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
147 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
149 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
150 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
152 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
154 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
155 .get(CAPABILITY_TYPE)
156 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
157 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
160 private void stubGetGroupType() {
161 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
162 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
163 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
166 private static GroupTypeDefinition buildRootGroupType() {
167 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
168 "The TOSCA Group Type all other TOSCA Group Types derive from");
171 private static GroupTypeDefinition buildHeatStackGroupType() {
172 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
173 "Grouped all heat resources which are in the same heat stack");
175 GroupProperty property1 = createGroupProperty("heat_file",
176 "Heat file which associate to this group/heat stack", "SUPPORTED");
178 GroupProperty property2 = createGroupProperty("description",
179 "Group description", "SUPPORTED");
181 groupType.setProperties(Lists.newArrayList(property1, property2));
185 private static GroupTypeDefinition buildVfcInstanceGroupType() {
186 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
187 "Groups of VFCs with same parent port role");
189 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
190 "Function of this VFC group", null);
192 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
193 "Common role of parent ports of VFCs in this group", null);
195 GroupProperty property3 = createGroupProperty("network_collection_function",
196 "Network collection function assigned to this group", null);
198 GroupProperty property4 = createGroupProperty("subinterface_role",
199 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
201 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
203 CapabilityDefinition capability = new CapabilityDefinition();
204 capability.setType(CAPABILITY_TYPE);
205 capability.setName(CAPABILITY_NAME);
206 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
207 capabilityProperty.setName("vfc_instance_group_reference");
208 capabilityProperty.setType("string");
209 capability.setProperties(Collections.singletonList(capabilityProperty));
211 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
212 capabilityMap.put(CAPABILITY_NAME, capability);
213 groupType.setCapabilities(capabilityMap);
217 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
218 GroupTypeDefinition property = new GroupTypeDefinition();
221 property.setType(type);
223 if (derivedFrom != null) {
224 property.setDerivedFrom(derivedFrom);
227 if (description != null) {
228 property.setDescription(description);
233 private static GroupProperty createGroupProperty(String name, String description,
235 GroupProperty property = new GroupProperty();
237 property.setName(name);
239 if (description != null) {
240 property.setDescription(description);
243 if (status != null) {
244 property.setStatus(status);
247 property.setType("string");
248 property.setRequired(true);