aboutsummaryrefslogtreecommitdiffstats
path: root/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/verificator/VfModuleVerificator.java
blob: 9426fc1cde060b9442d66cc05e651c1206ef3eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.verificator;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertTrue;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.GroupInstance;
import org.openecomp.sdc.be.model.GroupProperty;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.ci.tests.datatypes.TypeHeatMetaDefinition;
import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaServiceGroupsMetadataDefinition;
import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaGroupsTopologyTemplateDefinition;
import org.openecomp.sdc.ci.tests.utils.ToscaParserUtils;

import com.aventstack.extentreports.Status;

public class VfModuleVerificator {

	private static final String [] PROPERTY_TYPES = {"vf_module_label", "min_vf_module_instances", "max_vf_module_instances", "initial_count"};
	private static final String VF_MODULE_TYPE = "org.openecomp.groups.VfModule";
	
	/**
	 * compare number of groups from HEAT.meta file vs TOSCA yaml groups generated by ASDC
	 * @param listTypeHeatMetaDefinition - java object created from HEAT.meta file
	 * @param toscaDefinition - java object created from TOSCA yaml
	 */
	public static void compareNumberOfVfModules(List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition, ToscaDefinition toscaDefinition) {

		int heatMetaGroupCount = 0;
		int toscaDefinitionGroupCount = 0;
		for (TypeHeatMetaDefinition typeHeatMetaDefinition : listTypeHeatMetaDefinition) {
			heatMetaGroupCount = typeHeatMetaDefinition.getGroupHeatMetaDefinition().size();
		}
		toscaDefinitionGroupCount = toscaDefinition.getTopology_template().getGroups().size();
		assertEquals("Expected num of groups in HEAT.meta file is " + heatMetaGroupCount + ", but was in TOSCA yaml file " + toscaDefinitionGroupCount, heatMetaGroupCount, toscaDefinitionGroupCount);
	}

	/**
	 * check group structure and "metadata" parameters vs data on the service object
	 * @param toscaDefinition
	 */
	public static void verifyGroupMetadata(ToscaDefinition toscaDefinition, Service service) {

		Map<String, ToscaGroupsTopologyTemplateDefinition> groups = toscaDefinition.getTopology_template().getGroups();
		for (Map.Entry<String, ToscaGroupsTopologyTemplateDefinition> groupTopologyTemplateDefinition : groups.entrySet()) {
			String key = groupTopologyTemplateDefinition.getKey();
			GroupInstance groupInstanceObject = getGroupInstanceByKey(key, service);
			ToscaServiceGroupsMetadataDefinition metadata = groupTopologyTemplateDefinition.getValue().getMetadata();
			assertNotNull("groupInstanceObject is null", groupInstanceObject);
			assertTrue("expected vfModuleModelName " + groupInstanceObject.getGroupName() + ", actual " + metadata.getVfModuleModelName(), groupInstanceObject.getGroupName().equals(metadata.getVfModuleModelName()));
			assertTrue("expected vfModuleModelInvariantUUID " + groupInstanceObject.getInvariantUUID() + ", actual " + metadata.getVfModuleModelInvariantUUID(), groupInstanceObject.getInvariantUUID().equals(metadata.getVfModuleModelInvariantUUID()));
			assertTrue("expected vfModuleModelCustomizationUUID " + groupInstanceObject.getCustomizationUUID() + ", actual " + metadata.getVfModuleModelCustomizationUUID(), groupInstanceObject.getCustomizationUUID().equals(metadata.getVfModuleModelCustomizationUUID()));
			assertTrue("expected vfModuleModelUUID " + groupInstanceObject.getGroupUUID() + ", actual " + metadata.getVfModuleModelUUID(), groupInstanceObject.getGroupUUID().equals(metadata.getVfModuleModelUUID()));
			assertTrue("expected vfModuleModelVersion " + groupInstanceObject.getVersion() + ", actual " + metadata.getVfModuleModelVersion(), groupInstanceObject.getVersion().equals(metadata.getVfModuleModelVersion()));
		}
	}

	
	/**
	 * @param key
	 * @param service
	 * @return
	 */
	public static GroupInstance getGroupInstanceByKey(String key, Service service) {
		for(ComponentInstance componentInstance : service.getComponentInstances()){
			for(GroupInstance groupInstance : componentInstance.getGroupInstances()){
				if( key.equals(groupInstance.getName())){
					return groupInstance;
				}
			}
		}
		return null;
	}

	public static void validateSpecificModulePropertiesFromRequest(Resource resource) {
		List<List<PropertyDataDefinition>> allProperties = resource.getGroups().stream().
											                     filter(e -> e.getType().equals(VF_MODULE_TYPE)).
											                     map(e -> e.getProperties()).
											                     collect(Collectors.toList());
		for(String propertyType :PROPERTY_TYPES){
			int numberOfTypes = getPropertyType(allProperties, propertyType).size();
			SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating VF property %s exist, Expected: %s, Actual: %s  ", propertyType, allProperties.size(), numberOfTypes));
			assertTrue(numberOfTypes == allProperties.size());
		}		
	}

	public static List<PropertyDataDefinition> getPropertyType(List<List<PropertyDataDefinition>> allProperties, String propertyType) {
		return allProperties.stream().
				             flatMap(List::stream).
				             filter(e -> e.getName().equals(propertyType)).
				             collect(Collectors.toList());
	}
	
	public static void validateSpecificModulePropertiesFromFile(ToscaDefinition toscaDefinition){
		List<ToscaGroupsTopologyTemplateDefinition> vfModules = toscaDefinition.getTopology_template().getGroups().values().stream().
											                                                            filter(e -> e.getType().equals(VF_MODULE_TYPE)).
											                                                            collect(Collectors.toList());
		
		for(String propertyType :PROPERTY_TYPES){
			int numberOfTypes = (int) vfModules.stream().
												filter(e -> e.getProperties().containsKey(propertyType)).
												count();
			SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating VF property %s exist, Expected: %s, Actual: %s  ", propertyType, vfModules.size(), numberOfTypes));
			assertTrue(numberOfTypes == vfModules.size());
		}		
	}

	public static ToscaDefinition getToscaTemplate(String pathToCsar) throws Exception {
		String outputFolder = DeploymentViewVerificator.unzipCsarFile(pathToCsar);
		String templateFileName = VfModuleVerificator.getTemplateFilenname(pathToCsar);
		
		File pathToMainServiceTemplate = new File(outputFolder + File.separator + "Definitions" + File.separator + templateFileName);
		ToscaDefinition toscaDefinition = ToscaParserUtils.parseToscaYamlToJavaObject(pathToMainServiceTemplate);
		
		DeploymentViewVerificator.cleanFolders(new File(outputFolder).getParent());
		
		return toscaDefinition;
	}

	public static String getTemplateFilenname(String pathToCsar) {
		File csarFile = new File(pathToCsar);
		String templateFileName = csarFile.getName().replaceAll("-csar.csar", "-template.yml");
		return templateFileName;
	}
	
}