[SDC] rebase 1710 code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / CsarParserUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.json.simple.JSONArray;
31 import org.json.simple.JSONObject;
32 import org.json.simple.parser.JSONParser;
33 import org.json.simple.parser.ParseException;
34 import org.openecomp.sdc.ci.tests.datatypes.GroupHeatMetaDefinition;
35 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
36 import org.openecomp.sdc.ci.tests.datatypes.PropertyHeatMetaDefinition;
37 import org.openecomp.sdc.ci.tests.datatypes.TypeHeatMetaDefinition;
38 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
39 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaParameterConstants;
40 import org.openecomp.sdc.ci.tests.utils.validation.CsarValidationUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class CsarParserUtils {
45         private static Logger log = LoggerFactory.getLogger(CsarValidationUtils.class.getName());
46
47         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(File csarFileLocation) throws Exception {
48
49                 String artifactHeatMetaLocation = ToscaParameterConstants.HEAT_META_PATH;
50                 String csarPayload = ToscaParserUtils.getYamlPayloadFromCsar(csarFileLocation, artifactHeatMetaLocation);
51                 return getListTypeHeatMetaDefinitionByPayload(csarPayload);
52         }
53
54         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(String csarUUID) throws Exception {
55                 
56                 String artifactHeatMetaLocation = ToscaParameterConstants.HEAT_META_PATH;
57                 String csarPayload = ToscaParserUtils.getCsarPayload(csarUUID, artifactHeatMetaLocation);
58                 return getListTypeHeatMetaDefinitionByPayload(csarPayload);
59         }
60
61         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinitionByPayload(String csarPayload) throws ParseException {
62                 JSONParser parser = new JSONParser();
63                 if (csarPayload != null) {
64                         Object parse = parser.parse(csarPayload);
65                         JSONObject jsonObject = (JSONObject) parse;
66                         JSONObject jsonObjectImportStructure = (JSONObject) jsonObject.get(ToscaParameterConstants.IMPORT_STRUCTURE);
67                         List<TypeHeatMetaDefinition> listHeatMetaDefenition = new ArrayList<TypeHeatMetaDefinition>();
68                         listHeatMetaDefenition = getArtifactsByGroup(jsonObjectImportStructure, listHeatMetaDefenition);
69                         return listHeatMetaDefenition;
70                 }
71                 return null;
72         }
73         
74         
75         protected static List<TypeHeatMetaDefinition> getArtifactsByGroup(JSONObject jsonObjectImportStructure, List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
76
77                 @SuppressWarnings("unchecked")
78                 Set<Object> typeSet = jsonObjectImportStructure.keySet();
79                 for (Object type : typeSet) {
80                         TypeHeatMetaDefinition heatMetaDefenition = new TypeHeatMetaDefinition();
81                         log.debug(type.toString());
82                         log.debug("{}", jsonObjectImportStructure.get(type));
83                         JSONArray array = (JSONArray) jsonObjectImportStructure.get(type);
84                         heatMetaDefenition.setTypeName((String) type);
85                         List<GroupHeatMetaDefinition> groupHeatMetaDefinitions = new ArrayList<GroupHeatMetaDefinition>();
86                         heatMetaDefenition.setGroupHeatMetaDefinition(fetchArtifactByGroupFromJsonArray(array, groupHeatMetaDefinitions, true, false));
87                         listHeatMetaDefenition.add(heatMetaDefenition);
88                 }
89                 return listHeatMetaDefenition;
90         }
91         
92         protected static List<GroupHeatMetaDefinition> fetchArtifactByGroupFromJsonArray(JSONArray array, List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, Boolean openNewGroup, Boolean isNested) {
93
94                 GroupHeatMetaDefinition groupHeatMetaDefinition;
95
96                 if (array != null) {
97                         for (int i = 0; i < array.size(); i++) {
98                                 if (openNewGroup) {
99                                         groupHeatMetaDefinition = new GroupHeatMetaDefinition();
100                                         int groupNumber = listGroupHeatMetaDefinition.size() + 1;
101                                         log.debug("groupName={}", groupNumber);
102                                         groupHeatMetaDefinition.setGroup(groupNumber);
103                                         listGroupHeatMetaDefinition.add(groupHeatMetaDefinition);
104                                         PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
105                                         propertyHeatMetaDefinition.setName("isBase");
106                                         propertyHeatMetaDefinition.setValue(false);
107                                         groupHeatMetaDefinition.setPropertyHeatMetaDefinition(propertyHeatMetaDefinition);
108                                 }
109                                 groupHeatMetaDefinition = listGroupHeatMetaDefinition.get(listGroupHeatMetaDefinition.size() - 1);
110                                 JSONObject jsonObject = (JSONObject) array.get(i);
111                                 fetchArtifactByGroupFromJsonObject(listGroupHeatMetaDefinition, groupHeatMetaDefinition, jsonObject, isNested);
112                         }
113                 }
114                 return listGroupHeatMetaDefinition;
115         }
116         
117         
118         public static void fetchArtifactByGroupFromJsonObject(List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, GroupHeatMetaDefinition groupHeatMetaDefinition, JSONObject jsonObject, Boolean isNested) {
119                 @SuppressWarnings("unchecked")
120                 Set<Object> groupsKey = jsonObject.keySet();
121                 for (Object groupKey : groupsKey) {
122                         String groupKeyStr = (String) groupKey;
123                         if (groupKeyStr.equals("isBase")) {
124                                 PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
125                                 propertyHeatMetaDefinition.setName(groupKeyStr);
126                                 propertyHeatMetaDefinition.setValue((boolean) jsonObject.get(groupKeyStr));
127                                 if (!groupHeatMetaDefinition.getPropertyHeatMetaDefinition().equals(propertyHeatMetaDefinition)) {
128                                         groupHeatMetaDefinition.getPropertyHeatMetaDefinition().setValue((boolean) jsonObject.get(groupKeyStr));
129                                 }
130                         }
131                         if (groupKeyStr.equals("fileName")) {
132                                 String artifactName = (String) jsonObject.get(groupKeyStr);
133                                 String artifactType = ArtifactTypeEnum.HEAT_ARTIFACT.getType();
134                                 if(isNested){
135                                         artifactType = ArtifactTypeEnum.HEAT_NESTED.getType();
136                                 }
137                                 if(jsonObject.get("type") != null && isNested == false){
138                                         artifactType = (String) jsonObject.get("type");
139                                 }
140                                 HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition = new HeatMetaFirstLevelDefinition(artifactName, artifactType, null);
141                                 List<HeatMetaFirstLevelDefinition> listArtifactNames = groupHeatMetaDefinition.getArtifactList();
142                                 listArtifactNames.add(heatMetaFirstLevelDefinition);
143                                 groupHeatMetaDefinition.setArtifactList(listArtifactNames);
144                         } else {
145                                 if((groupKeyStr.equals("env"))){
146                                         if (jsonObject.get(groupKeyStr) instanceof JSONObject){
147                                                 fetchArtifactByGroupFromJsonObject(listGroupHeatMetaDefinition, groupHeatMetaDefinition, (JSONObject) jsonObject.get(groupKeyStr), false);
148                                         }else{
149                                                 assertTrue("Expected object is JSONObject, but actual: " + jsonObject.get(groupKeyStr).getClass(), jsonObject.get(groupKeyStr).getClass().equals("JSONObject"));
150                                         }
151                                 }
152                                 if((groupKeyStr.equals("nested"))){
153                                         if (jsonObject.get(groupKeyStr) instanceof JSONArray){
154                                                 fetchArtifactByGroupFromJsonArray((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition, false, true);
155                                         }else{
156                                                 assertTrue("Expected object is JSONArray, but actual: " + jsonObject.get(groupKeyStr).getClass(), jsonObject.get(groupKeyStr).getClass().equals("JSONArray"));
157                                         }
158                                         
159                                 }else if (!(groupKeyStr.equals("isBase") || groupKeyStr.equals("type") || groupKeyStr.equals("env"))) {
160                                         if (jsonObject.get(groupKeyStr) instanceof JSONArray){
161                                                 fetchArtifactByGroupFromJsonArray((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition, false, false);
162                                         }else{
163                                                 assertTrue("Expected object is JSONArray, but actual: " + jsonObject.get(groupKeyStr).getClass(), jsonObject.get(groupKeyStr).getClass().equals("JSONArray"));
164                                         }
165                                 }
166                         }
167                 }
168         }
169         
170 }