summaryrefslogtreecommitdiffstats
path: root/asdc-tests/src/main/java/org/openecomp/sdc/ci/tests/utils/validation/CsarValidationUtils.java
blob: 29f9e84dd8eb3c24452fe246a28293bfd8c808e6 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*-
 * ============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.utils.validation;

import static org.testng.AssertJUnit.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.ci.tests.datatypes.GroupHeatMetaDefinition;
import org.openecomp.sdc.ci.tests.datatypes.PropertyHeatMetaDefinition;
import org.openecomp.sdc.ci.tests.datatypes.TypeHeatMetaDefinition;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTemplatesTopologyTemplateDefinition;
import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaRequirementsNodeTemplatesDefinition;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ImportRestUtils;
import org.openecomp.sdc.common.rest.api.RestResponseAsByteArray;
import org.openecomp.sdc.common.util.ZipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CsarValidationUtils {
	private static Logger log = LoggerFactory.getLogger(CsarValidationUtils.class.getName());

	public static String getCsarPayload(String csarName, String fileLocation) throws Exception {

		RestResponseAsByteArray csar = ImportRestUtils.getCsar(csarName, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
		assertTrue("Return response code different from 200", csar.getHttpStatusCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
		Map<String, byte[]> readZip = null;
		byte[] data = csar.getResponse();
		if (data != null && data.length > 0) {
			readZip = ZipUtil.readZip(data);

		}

		byte[] artifactsBs = readZip.get(fileLocation);
		String str = new String(artifactsBs, StandardCharsets.UTF_8);

		return str;

	}

	public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(String csarUUID) throws Exception {

		String artifactHeatMetaLocation = "Artifacts/HEAT.meta";
		JSONParser parser = new JSONParser();
		String csarPayload = getCsarPayload(csarUUID, artifactHeatMetaLocation);
		if (csarPayload != null) {
			Object parse = parser.parse(csarPayload);
			JSONObject jsonObject = (JSONObject) parse;
			JSONObject jsonObjectImportStructure = (JSONObject) jsonObject.get("importStructure");
			List<TypeHeatMetaDefinition> listHeatMetaDefenition = new ArrayList<TypeHeatMetaDefinition>();
			listHeatMetaDefenition = getArtifactsByGroup(jsonObjectImportStructure, listHeatMetaDefenition);
			return listHeatMetaDefenition;
		}
		return null;

	}

	protected static List<TypeHeatMetaDefinition> getArtifactsByGroup(JSONObject jsonObjectImportStructure, List<TypeHeatMetaDefinition> listHeatMetaDefenition) {

		@SuppressWarnings("unchecked")
		Set<Object> typeSet = jsonObjectImportStructure.keySet();
		for (Object type : typeSet) {
			TypeHeatMetaDefinition heatMetaDefenition = new TypeHeatMetaDefinition();
			log.debug(type.toString());
			log.debug("{}", jsonObjectImportStructure.get(type));
			JSONArray array = (JSONArray) jsonObjectImportStructure.get(type);
			heatMetaDefenition.setTypeName((String) type);
			List<GroupHeatMetaDefinition> groupHeatMetaDefinitions = new ArrayList<GroupHeatMetaDefinition>();
			heatMetaDefenition.setGroupHeatMetaDefinition(fetchArtifactByGroup(array, groupHeatMetaDefinitions, true));
			listHeatMetaDefenition.add(heatMetaDefenition);
		}
		return listHeatMetaDefenition;
	}

	protected static List<GroupHeatMetaDefinition> fetchArtifactByGroup(JSONArray array, List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, Boolean openNewGroup) {

		GroupHeatMetaDefinition groupHeatMetaDefinition;

		if (array != null) {
			for (int i = 0; i < array.size(); i++) {
				if (openNewGroup) {
					groupHeatMetaDefinition = new GroupHeatMetaDefinition();
					int groupNumber = listGroupHeatMetaDefinition.size() + 1;
					log.debug("groupName={}", groupNumber);
					groupHeatMetaDefinition.setGroup(groupNumber);
					listGroupHeatMetaDefinition.add(groupHeatMetaDefinition);
					PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
					propertyHeatMetaDefinition.setName("isBase");
					propertyHeatMetaDefinition.setValue(false);
					groupHeatMetaDefinition.setPropertyHeatMetaDefinition(propertyHeatMetaDefinition);
				}
				groupHeatMetaDefinition = listGroupHeatMetaDefinition.get(listGroupHeatMetaDefinition.size() - 1);
				JSONObject jsonObject = (JSONObject) array.get(i);
				@SuppressWarnings("unchecked")
				Set<Object> groupsKey = jsonObject.keySet();
				for (Object groupKey : groupsKey) {
					String groupKeyStr = (String) groupKey;
					if (groupKeyStr.equals("isBase")) {
						PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
						propertyHeatMetaDefinition.setName(groupKeyStr);
						propertyHeatMetaDefinition.setValue((boolean) jsonObject.get(groupKeyStr));
						if (!groupHeatMetaDefinition.getPropertyHeatMetaDefinition().equals(propertyHeatMetaDefinition)) {
							groupHeatMetaDefinition.getPropertyHeatMetaDefinition().setValue((boolean) jsonObject.get(groupKeyStr));
						}
					}
					if (groupKeyStr.equals("fileName") || groupKeyStr.equals("env")) {
						String artifactName = (String) jsonObject.get(groupKeyStr);
						List<String> listArtifactNames = groupHeatMetaDefinition.getArtifactList();
						listArtifactNames.add(artifactName);
						groupHeatMetaDefinition.setArtifactList(listArtifactNames);
					} else {
						if (!groupKeyStr.equals("isBase")) {
							fetchArtifactByGroup((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition, false);
						}
					}
				}
			}
		}
		return listGroupHeatMetaDefinition;
	}

	private static Integer getArtifactCount(List<TypeHeatMetaDefinition> listHeatMetaDefenition, Boolean isEnvIncluded) {
		int count = 0;
		List<String> uniqeArtifactList = new ArrayList<>();

		for (TypeHeatMetaDefinition typeHeatMetaDefinition : listHeatMetaDefenition) {
			for (GroupHeatMetaDefinition groupHeatMetaDefinition : typeHeatMetaDefinition.getGroupHeatMetaDefinition()) {
				if (isEnvIncluded) {
					count = count + groupHeatMetaDefinition.getArtifactList().size();
				} else {
					for (String fileName : groupHeatMetaDefinition.getArtifactList()) {
						if (!fileName.contains(".env") && !uniqeArtifactList.contains(fileName)) {
							uniqeArtifactList.add(fileName);
							count = count + 1;
						}
					}
				}
			}
		}
		return count;
	}

	private static Integer getGroupCount(List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
		int count = 0;
		for (TypeHeatMetaDefinition typeHeatMetaDefinition : listHeatMetaDefenition) {
			count = count + typeHeatMetaDefinition.getGroupHeatMetaDefinition().size();
		}
		return count;
	}

	private static String groupNameBuilder(Resource resource) {
		String separator = "::";
		String module = "module-";
		String groupName = resource.getSystemName() + separator + module;
		return groupName;
	}

	public static void validateCsarVfArtifact(String csarUUID, Resource resource) throws Exception {

		List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition = getListTypeHeatMetaDefinition(csarUUID);
		assertTrue("check group count, expected: " + getGroupCount(listTypeHeatMetaDefinition) + ", actual: " + resource.getGroups().size(), getGroupCount(listTypeHeatMetaDefinition) == resource.getGroups().size());
		assertTrue("check artifact count, expected: " + getArtifactCount(listTypeHeatMetaDefinition, false) + ", actual: " + resource.getDeploymentArtifacts().size(),
				getArtifactCount(listTypeHeatMetaDefinition, false) == resource.getDeploymentArtifacts().size());

	}

	public static void validateToscaDefinitonObjectVsResource(ToscaDefinition toscaDefinition, Resource resource) throws Exception {

		assertTrue("check resource instance count, expected: " + getResourceInstanceCount(toscaDefinition) + ", actual: " + resource.getComponentInstances().size(),
				getResourceInstanceCount(toscaDefinition) == resource.getComponentInstances().size());
		assertTrue("check resource instance relation count, expected: " + getResourceInstanceRelationCount(toscaDefinition) + ", actual: " + resource.getComponentInstancesRelations().size(),
				getResourceInstanceRelationCount(toscaDefinition) == resource.getComponentInstancesRelations().size());

	}

	public static Integer getResourceInstanceCount(ToscaDefinition toscaDefinition) {

		return toscaDefinition.getToscaTopologyTemplate().getToscaNodeTemplatesTopologyTemplateDefinition().size();
	}

	public static Integer getResourceInstanceRelationCount(ToscaDefinition toscaDefinition) {
		int count = 0;
		List<ToscaNodeTemplatesTopologyTemplateDefinition> toscaNodeTemplatesTopologyTemplateDefinition = toscaDefinition.getToscaTopologyTemplate().getToscaNodeTemplatesTopologyTemplateDefinition();
		for (int i = 0; i < toscaNodeTemplatesTopologyTemplateDefinition.size(); i++) {
			List<ToscaRequirementsNodeTemplatesDefinition> requirements = toscaNodeTemplatesTopologyTemplateDefinition.get(i).getRequirements();
			if (requirements != null) {
				for (ToscaRequirementsNodeTemplatesDefinition requirement : requirements) {
					if (requirement.getNode() != null) {
						count = count + 1;
					}
				}
			}
		}
		return count;
	}

	// not finished yet
	private static void validateCsarVfgroup(String csarUUID, Resource resource) {

		List<GroupDefinition> groups = resource.getGroups();
		for (GroupDefinition groupDefinition : groups) {
			List<String> artifacts = groupDefinition.getArtifacts();
			assertTrue("group description is null", groupDefinition.getDescription() != null);
			assertTrue("InvariantUUID is null", groupDefinition.getInvariantUUID() != null);
			// groupDefinition.getMembers();
			assertTrue("name format mismatch, expected: " + groupNameBuilder(resource) + "[0-9], actual: " + groupDefinition.getName(), groupDefinition.getName().contains(groupNameBuilder(resource)));
			// groupDefinition.getProperties();
			// groupDefinition.getPropertyValueCounter();
			assertTrue(groupDefinition.getType().equals(getGroupType()));
		}

		String expectedCsarUUID = csarUUID;
		// String expectedToscaResourceName = "org.openecomp.resource.vf." +
		// WordUtils.capitalize(resourceDetails.getName().toLowerCase());
		//
		// assertTrue("csarUUID : " + buildAssertMessage(expectedCsarUUID,
		// resource.getCsarUUID()),
		// expectedCsarUUID.equals(resource.getCsarUUID()));
		// assertTrue("toscaResourceName : " +
		// buildAssertMessage(expectedToscaResourceName,
		// resource.getToscaResourceName()),
		// expectedToscaResourceName.equals(resource.getToscaResourceName()));
		//
		// RestResponse getResourceResponse =
		// ResourceRestUtils.getResource(resource.getUniqueId());
		// Resource getResource =
		// ResponseParser.parseToObjectUsingMapper(getResourceResponse.getResponse(),
		// Resource.class);
		// assertTrue("csarUUID : " + buildAssertMessage(expectedCsarUUID,
		// getResource.getCsarUUID()),
		// expectedCsarUUID.equals(getResource.getCsarUUID()));
		// assertTrue("toscaResourceName : " +
		// buildAssertMessage(expectedToscaResourceName,
		// getResource.getToscaResourceName()),
		// expectedToscaResourceName.equals(getResource.getToscaResourceName()));

	}

	private static String getGroupType() {
		return "org.openecomp.groups.VfModule";
	}

}